Skip to content

Commit 519916b

Browse files
committed
added ReferenceName.Parse, added ReferenceName.EquivalentTo
added ReferenceName.Parse that parse canonical Name into ReferenceName instance
1 parent 6392cd9 commit 519916b

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using GitVersion;
5-
using GitVersion.Extensions;
65
using GitVersion.Logging;
76
using GitVersionCore.Tests.Helpers;
87
using NSubstitute;
@@ -47,7 +46,8 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,
4746

4847
var repoTipId = repoTip.Id;
4948

50-
if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName)))
49+
var referenceName = ReferenceName.Parse(localCanonicalName);
50+
if (repo.Branches.All(b => !b.Name.Equals(referenceName)))
5151
{
5252
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
5353
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");

src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
135135
throw new InvalidOperationException("Could not find a 'develop' or 'master' branch, neither locally nor remotely.");
136136
}
137137

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

141140
// To prevent infinite loops, make sure that a new branch was chosen.
142141
if (targetBranch.Equals(chosenBranch))

src/GitVersionCore/Core/GitPreparer.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
216216
const string moveBranchMsg = "Move one of the branches along a commit to remove warning";
217217

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

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

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

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

339341
var repoTipId = repoTip.Id;
340342

341-
if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName)))
343+
var referenceName = ReferenceName.Parse(localCanonicalName);
344+
if (repo.Branches.All(b => !b.Name.Equals(referenceName)))
342345
{
343-
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
344-
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
346+
log.Info(isBranch ? $"Creating local branch {referenceName}"
347+
: $"Creating local branch {referenceName} pointing at {repoTipId}");
345348
repo.Refs.Add(localCanonicalName, repoTipId.Sha);
346349
}
347350
else
348351
{
349-
log.Info(isBranch ? $"Updating local branch {localCanonicalName} to point at {repoTip}"
350-
: $"Updating local branch {localCanonicalName} to match ref {currentBranch}");
352+
log.Info(isBranch ? $"Updating local branch {referenceName} to point at {repoTip}"
353+
: $"Updating local branch {referenceName} to match ref {currentBranch}");
351354
var localRef = repo.Refs[localCanonicalName];
352355
repo.Refs.UpdateTarget(localRef, repoTipId);
353356
}

src/GitVersionCore/Core/RepositoryMetadataProvider.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Text.RegularExpressions;
55
using GitVersion.Common;
66
using GitVersion.Configuration;
7-
using GitVersion.Extensions;
87
using GitVersion.Logging;
98
using GitVersion.Model.Configuration;
109
using GitVersion.VersionCalculation;
@@ -42,7 +41,7 @@ public ICommit FindMergeBase(IBranch branch, IBranch otherBranch)
4241

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

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

152+
var targetBranch = FindBranch(targetBranchName);
154153
// CanonicalName can be "refs/heads/develop", so we need to check for "/{TargetBranch}" as well
155154
if (!desiredBranch.Equals(targetBranch))
156155
{
157156
// In the case where HEAD is not the desired branch, try to find the branch with matching name
158157
desiredBranch = repository.Branches?
159-
.Where(b =>
160-
b.Name.Canonical.IsEquivalentTo(targetBranchName) ||
161-
b.Name.Friendly.IsEquivalentTo(targetBranchName) ||
162-
b.Name.WithoutRemote.IsEquivalentTo(targetBranchName))
158+
.Where(b => b.Name.EquivalentTo(targetBranchName))
163159
.OrderBy(b => b.IsRemote)
164160
.FirstOrDefault();
165161

@@ -171,11 +167,7 @@ public IBranch GetTargetBranch(string targetBranchName)
171167
return desiredBranch;
172168
}
173169

174-
public IBranch FindBranch(string branchName)
175-
{
176-
return repository.Branches.FirstOrDefault(x => x.Name.Canonical.IsEquivalentTo(branchName)
177-
|| x.Name.WithoutRemote.IsEquivalentTo(branchName));
178-
}
170+
public IBranch FindBranch(string branchName) => repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));
179171

180172
public IBranch GetChosenBranch(Config configuration)
181173
{

src/GitVersionCore/Git/ReferenceName.cs

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ public ReferenceName(string canonical)
1818
Friendly = Shorten();
1919
WithoutRemote = RemoveRemote();
2020
}
21+
22+
public static ReferenceName Parse(string canonicalName)
23+
{
24+
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
25+
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
26+
|| IsPrefixedBy(canonicalName, TagPrefix))
27+
{
28+
return new ReferenceName(canonicalName);
29+
}
30+
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
31+
}
2132
public string Canonical { get; }
2233
public string Friendly { get; }
2334
public string WithoutRemote { get; }
@@ -28,6 +39,13 @@ public ReferenceName(string canonical)
2839
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2940
public override string ToString() => Friendly;
3041

42+
public bool EquivalentTo(string name)
43+
{
44+
return Canonical.Equals(name, StringComparison.OrdinalIgnoreCase)
45+
|| Friendly.Equals(name, StringComparison.OrdinalIgnoreCase)
46+
|| WithoutRemote.Equals(name, StringComparison.OrdinalIgnoreCase);
47+
}
48+
3149
private string Shorten()
3250
{
3351
if (IsPrefixedBy(Canonical, LocalBranchPrefix))

0 commit comments

Comments
 (0)