Skip to content

Commit

Permalink
added ReferenceName.Parse, added ReferenceName.EquivalentTo
Browse files Browse the repository at this point in the history
added ReferenceName.Parse that parse canonical Name into ReferenceName instance
  • Loading branch information
arturcic committed Jan 19, 2021
1 parent 6392cd9 commit 519916b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using GitVersion;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersionCore.Tests.Helpers;
using NSubstitute;
Expand Down Expand Up @@ -47,7 +46,8 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,

var repoTipId = repoTip.Id;

if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName)))
var referenceName = ReferenceName.Parse(localCanonicalName);
if (repo.Branches.All(b => !b.Name.Equals(referenceName)))
{
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
throw new InvalidOperationException("Could not find a 'develop' or 'master' branch, neither locally nor remotely.");
}

var branchName = chosenBranch.Name.Friendly;
log.Warning(errorMessage + System.Environment.NewLine + "Falling back to " + branchName + " branch config");
log.Warning($"{errorMessage}{System.Environment.NewLine}Falling back to {chosenBranch} branch config");

// To prevent infinite loops, make sure that a new branch was chosen.
if (targetBranch.Equals(chosenBranch))
Expand Down
21 changes: 12 additions & 9 deletions src/GitVersionCore/Core/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
const string moveBranchMsg = "Move one of the branches along a commit to remove warning";

log.Warning($"Found more than one local branch pointing at the commit '{headSha}' ({csvNames}).");
var master = localBranchesWhereCommitShaIsHead.SingleOrDefault(n => n.Name.Friendly.IsEquivalentTo(Config.MasterBranchKey));
var master = localBranchesWhereCommitShaIsHead.SingleOrDefault(n => n.Name.EquivalentTo(Config.MasterBranchKey));
if (master != null)
{
log.Warning("Because one of the branches is 'master', will build master." + moveBranchMsg);
Expand Down Expand Up @@ -269,20 +269,22 @@ private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(IGitReposi
{
var prefix = $"refs/remotes/{remoteName}/";
var remoteHeadCanonicalName = $"{prefix}HEAD";
var headReferenceName = ReferenceName.Parse(remoteHeadCanonicalName);
var remoteTrackingReferences = repo.Refs
.FromGlob(prefix + "*")
.Where(r => !r.Name.Canonical.IsEquivalentTo(remoteHeadCanonicalName));
.Where(r => !r.Name.Equals(headReferenceName));

foreach (var remoteTrackingReference in remoteTrackingReferences)
{
var remoteTrackingReferenceName = remoteTrackingReference.Name.Canonical;
var branchName = remoteTrackingReferenceName.Substring(prefix.Length);
var localCanonicalName = "refs/heads/" + branchName;

var referenceName = ReferenceName.Parse(localCanonicalName);
// We do not want to touch our current branch
if (branchName.IsEquivalentTo(repo.Head.Name.Friendly)) continue;
if (repo.Head.Name.EquivalentTo(branchName)) continue;

if (repo.Refs.Any(x => x.Name.Canonical.IsEquivalentTo(localCanonicalName)))
if (repo.Refs.Any(x => x.Name.Equals(referenceName)))
{
var localRef = repo.Refs[localCanonicalName];
if (localRef.DirectReferenceTargetIdentifier == remoteTrackingReference.DirectReferenceTargetIdentifier)
Expand Down Expand Up @@ -338,16 +340,17 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,

var repoTipId = repoTip.Id;

if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName)))
var referenceName = ReferenceName.Parse(localCanonicalName);
if (repo.Branches.All(b => !b.Name.Equals(referenceName)))
{
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
log.Info(isBranch ? $"Creating local branch {referenceName}"
: $"Creating local branch {referenceName} pointing at {repoTipId}");
repo.Refs.Add(localCanonicalName, repoTipId.Sha);
}
else
{
log.Info(isBranch ? $"Updating local branch {localCanonicalName} to point at {repoTip}"
: $"Updating local branch {localCanonicalName} to match ref {currentBranch}");
log.Info(isBranch ? $"Updating local branch {referenceName} to point at {repoTip}"
: $"Updating local branch {referenceName} to match ref {currentBranch}");
var localRef = repo.Refs[localCanonicalName];
repo.Refs.UpdateTarget(localRef, repoTipId);
}
Expand Down
16 changes: 4 additions & 12 deletions src/GitVersionCore/Core/RepositoryMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text.RegularExpressions;
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.Model.Configuration;
using GitVersion.VersionCalculation;
Expand Down Expand Up @@ -42,7 +41,7 @@ public ICommit FindMergeBase(IBranch branch, IBranch otherBranch)

using (log.IndentLog($"Finding merge base between '{branch}' and '{otherBranch}'."))
{
// Otherbranch tip is a forward merge
// Other branch tip is a forward merge
var commitToFindCommonBase = otherBranch.Tip;
var commit = branch.Tip;
if (otherBranch.Tip.Parents.Contains(commit))
Expand Down Expand Up @@ -143,23 +142,20 @@ public IBranch GetTargetBranch(string targetBranchName)
{
// By default, we assume HEAD is pointing to the desired branch
var desiredBranch = repository.Head;
var targetBranch = FindBranch(targetBranchName);

// Make sure the desired branch has been specified
if (!string.IsNullOrEmpty(targetBranchName))
{
// There are some edge cases where HEAD is not pointing to the desired branch.
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch.

var targetBranch = FindBranch(targetBranchName);
// CanonicalName can be "refs/heads/develop", so we need to check for "/{TargetBranch}" as well
if (!desiredBranch.Equals(targetBranch))
{
// In the case where HEAD is not the desired branch, try to find the branch with matching name
desiredBranch = repository.Branches?
.Where(b =>
b.Name.Canonical.IsEquivalentTo(targetBranchName) ||
b.Name.Friendly.IsEquivalentTo(targetBranchName) ||
b.Name.WithoutRemote.IsEquivalentTo(targetBranchName))
.Where(b => b.Name.EquivalentTo(targetBranchName))
.OrderBy(b => b.IsRemote)
.FirstOrDefault();

Expand All @@ -171,11 +167,7 @@ public IBranch GetTargetBranch(string targetBranchName)
return desiredBranch;
}

public IBranch FindBranch(string branchName)
{
return repository.Branches.FirstOrDefault(x => x.Name.Canonical.IsEquivalentTo(branchName)
|| x.Name.WithoutRemote.IsEquivalentTo(branchName));
}
public IBranch FindBranch(string branchName) => repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));

public IBranch GetChosenBranch(Config configuration)
{
Expand Down
18 changes: 18 additions & 0 deletions src/GitVersionCore/Git/ReferenceName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public ReferenceName(string canonical)
Friendly = Shorten();
WithoutRemote = RemoveRemote();
}

public static ReferenceName Parse(string canonicalName)
{
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
|| IsPrefixedBy(canonicalName, TagPrefix))
{
return new ReferenceName(canonicalName);
}
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
}
public string Canonical { get; }
public string Friendly { get; }
public string WithoutRemote { get; }
Expand All @@ -28,6 +39,13 @@ public ReferenceName(string canonical)
public override int GetHashCode() => equalityHelper.GetHashCode(this);
public override string ToString() => Friendly;

public bool EquivalentTo(string name)
{
return Canonical.Equals(name, StringComparison.OrdinalIgnoreCase)
|| Friendly.Equals(name, StringComparison.OrdinalIgnoreCase)
|| WithoutRemote.Equals(name, StringComparison.OrdinalIgnoreCase);
}

private string Shorten()
{
if (IsPrefixedBy(Canonical, LocalBranchPrefix))
Expand Down

0 comments on commit 519916b

Please sign in to comment.