Skip to content

Commit 46ce1d4

Browse files
committed
introduce ReferenceName
1 parent 0a707b6 commit 46ce1d4

23 files changed

+171
-129
lines changed

src/GitVersion.LibGit2Sharp/Git/Branch.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,24 @@ namespace GitVersion
55
{
66
internal sealed class Branch : IBranch
77
{
8-
private static readonly LambdaEqualityHelper<IBranch> equalityHelper = new(x => x.CanonicalName);
9-
private static readonly LambdaKeyComparer<IBranch, string> comparerHelper = new(x => x.CanonicalName);
8+
private static readonly LambdaEqualityHelper<IBranch> equalityHelper = new(x => x.Name.CanonicalName);
9+
private static readonly LambdaKeyComparer<IBranch, string> comparerHelper = new(x => x.Name.CanonicalName);
1010

1111
private readonly LibGit2Sharp.Branch innerBranch;
1212

13-
internal Branch(LibGit2Sharp.Branch branch) => innerBranch = branch;
13+
internal Branch(LibGit2Sharp.Branch branch)
14+
{
15+
innerBranch = branch;
16+
Name = new ReferenceName(branch.CanonicalName);
17+
}
18+
public ReferenceName Name { get; }
1419

1520
public int CompareTo(IBranch other) => comparerHelper.Compare(this, other);
1621
public override bool Equals(object obj) => Equals((obj as IBranch)!);
1722
public bool Equals(IBranch other) => equalityHelper.Equals(this, other);
1823
public override int GetHashCode() => equalityHelper.GetHashCode(this);
1924
public static implicit operator LibGit2Sharp.Branch(Branch d) => d.innerBranch;
2025

21-
public string CanonicalName => innerBranch.CanonicalName;
22-
public string FriendlyName => innerBranch.FriendlyName;
23-
24-
public string NameWithoutRemote =>
25-
IsRemote
26-
? FriendlyName.Substring(FriendlyName.IndexOf("/", StringComparison.Ordinal) + 1)
27-
: FriendlyName;
28-
2926
public ICommit? Tip
3027
{
3128
get
@@ -44,7 +41,7 @@ public ICommitCollection? Commits
4441
}
4542
}
4643

47-
public bool IsDetachedHead => CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
44+
public bool IsDetachedHead => Name.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
4845

4946
public bool IsRemote => innerBranch.IsRemote;
5047
public bool IsTracking => innerBranch.IsTracking;

src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public IEnumerator<IBranch> GetEnumerator()
1414
return innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
1515
}
1616
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
17-
public IBranch? this[string friendlyName]
17+
public IBranch? this[string name]
1818
{
1919
get
2020
{
21-
var branch = innerCollection[friendlyName];
21+
var branch = innerCollection[name];
2222
return branch is null ? null : new Branch(branch);
2323
}
2424
}

src/GitVersion.LibGit2Sharp/Git/Reference.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@ namespace GitVersion
55
{
66
internal sealed class Reference : IReference
77
{
8-
private static readonly LambdaEqualityHelper<IReference> equalityHelper = new(x => x.CanonicalName);
9-
private static readonly LambdaKeyComparer<IReference, string> comparerHelper = new(x => x.CanonicalName);
8+
private static readonly LambdaEqualityHelper<IReference> equalityHelper = new(x => x.Name.CanonicalName);
9+
private static readonly LambdaKeyComparer<IReference, string> comparerHelper = new(x => x.Name.CanonicalName);
1010

1111
internal readonly LibGit2Sharp.Reference innerReference;
1212
private DirectReference directReference => innerReference.ResolveToDirectReference();
1313

14-
internal Reference(LibGit2Sharp.Reference reference) => innerReference = reference;
15-
14+
internal Reference(LibGit2Sharp.Reference reference)
15+
{
16+
innerReference = reference;
17+
Name = new ReferenceName(reference.CanonicalName);
18+
}
19+
public ReferenceName Name { get; }
1620
public int CompareTo(IReference other) => comparerHelper.Compare(this, other);
1721
public override bool Equals(object obj) => Equals((obj as IReference)!);
1822
public bool Equals(IReference other) => equalityHelper.Equals(this, other);
1923
public override int GetHashCode() => equalityHelper.GetHashCode(this);
20-
21-
public string CanonicalName => innerReference.CanonicalName;
2224
public string TargetIdentifier => innerReference.TargetIdentifier;
2325
public string DirectReferenceTargetIdentifier => directReference.TargetIdentifier;
2426
public IObjectId DirectReferenceTargetId => new ObjectId(directReference.Target.Id);
25-
public IReference ResolveToDirectReference() => new Reference(directReference);
2627
public static implicit operator LibGit2Sharp.Reference(Reference d) => d.innerReference;
2728
}
2829
}

src/GitVersion.LibGit2Sharp/Git/Tag.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ namespace GitVersion
55
{
66
internal sealed class Tag : ITag
77
{
8-
private static readonly LambdaEqualityHelper<ITag> equalityHelper = new(x => x.CanonicalName);
9-
private static readonly LambdaKeyComparer<ITag, string> comparerHelper = new(x => x.CanonicalName);
8+
private static readonly LambdaEqualityHelper<ITag> equalityHelper = new(x => x.Name.CanonicalName);
9+
private static readonly LambdaKeyComparer<ITag, string> comparerHelper = new(x => x.Name.CanonicalName);
1010

1111
private readonly LibGit2Sharp.Tag innerTag;
12-
internal Tag(LibGit2Sharp.Tag tag) => innerTag = tag;
12+
internal Tag(LibGit2Sharp.Tag tag)
13+
{
14+
innerTag = tag;
15+
Name = new ReferenceName(innerTag.CanonicalName);
16+
}
17+
public ReferenceName Name { get; }
1318

1419
public int CompareTo(ITag other) => comparerHelper.Compare(this, other);
1520
public override bool Equals(object obj) => Equals((obj as ITag)!);
1621
public bool Equals(ITag other) => equalityHelper.Equals(this, other);
1722
public override int GetHashCode() => equalityHelper.GetHashCode(this);
18-
public string CanonicalName => innerTag.CanonicalName;
19-
public string FriendlyName => innerTag.FriendlyName;
2023
public string TargetSha => innerTag.Target.Sha;
2124

2225
public ICommit? PeeledTargetCommit()

src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,
4747

4848
var repoTipId = repoTip.Id;
4949

50-
if (repo.Branches.All(b => !b.CanonicalName.IsEquivalentTo(localCanonicalName)))
50+
if (repo.Branches.All(b => !b.Name.CanonicalName.IsEquivalentTo(localCanonicalName)))
5151
{
5252
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
5353
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
@@ -88,14 +88,14 @@ private static IRemote MockRemote(IGitRepository repository)
8888

8989
var branch = Substitute.For<IBranch>();
9090
branch.Tip.Returns(tip);
91-
branch.CanonicalName.Returns("refs/heads/feature/feat-test");
91+
branch.Name.Returns(new ReferenceName("refs/heads/feature/feat-test"));
9292

9393
var branches = Substitute.For<IBranchCollection>();
94-
branches[branch.CanonicalName].Returns(branch);
94+
branches[branch.Name.CanonicalName].Returns(branch);
9595
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { branch }).GetEnumerator());
9696

9797
var reference = Substitute.For<IReference>();
98-
reference.CanonicalName.Returns("refs/heads/develop");
98+
reference.Name.Returns(new ReferenceName("refs/heads/develop"));
9999

100100
var references = Substitute.For<IReferenceCollection>();
101101
references["develop"].Returns(reference);

src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public static ICommit CreateMockCommit()
4040
public static IBranch CreateMockBranch(string name, params ICommit[] commits)
4141
{
4242
var branch = Substitute.For<IBranch>();
43-
branch.FriendlyName.Returns(name);
44-
branch.CanonicalName.Returns(name);
45-
branch.NameWithoutRemote.Returns(name);
43+
branch.Name.Returns(new ReferenceName(name));
4644
branch.IsTracking.Returns(true);
4745
branch.IsRemote.Returns(false);
4846
branch.IsDetachedHead.Returns(false);
@@ -57,7 +55,7 @@ public static IBranch CreateMockBranch(string name, params ICommit[] commits)
5755

5856
public static IBranch FindBranch(this IGitRepository repository, string branchName)
5957
{
60-
return repository.Branches.FirstOrDefault(x => x.NameWithoutRemote == branchName);
58+
return repository.Branches.FirstOrDefault(x => x.Name.NameWithoutRemote == branchName);
6159
}
6260

6361
public static void DumpGraph(this IGitRepository repository, Action<string> writer = null, int? maxCommits = null)

src/GitVersionCore.Tests/Model/GitVersionContextTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
146146
mockRepository.Head.Returns(releaseLatestBranch);
147147
mockRepository.Commits.Returns(releaseLatestBranch.Commits);
148148

149-
var latestContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseLatestBranch.CanonicalName, config);
149+
var latestContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseLatestBranch.Name.CanonicalName, config);
150150
latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None);
151151

152152
mockRepository.Head.Returns(releaseVersionBranch);
153-
var versionContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseVersionBranch.CanonicalName, config);
153+
var versionContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseVersionBranch.Name.CanonicalName, config);
154154
versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch);
155155
}
156156

@@ -196,7 +196,7 @@ private static GitVersionContext GetGitVersionContext(string workingDirectory, I
196196
services.AddSingleton(repository);
197197
});
198198

199-
return sp.GetService<Lazy<GitVersionContext>>().Value;
199+
return sp.GetService<Lazy<GitVersionContext>>()?.Value;
200200
}
201201
}
202202
}

src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public BranchConfigurationCalculator(ILog log, IRepositoryMetadataProvider repos
2727
/// </summary>
2828
public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList<IBranch> excludedInheritBranches = null)
2929
{
30-
var matchingBranches = configuration.GetConfigForBranch(targetBranch.NameWithoutRemote);
30+
var matchingBranches = configuration.GetConfigForBranch(targetBranch.Name.NameWithoutRemote);
3131

3232
if (matchingBranches == null)
3333
{
34-
log.Info($"No branch configuration found for branch {targetBranch.FriendlyName}, falling back to default configuration");
34+
log.Info($"No branch configuration found for branch {targetBranch.Name.FriendlyName}, falling back to default configuration");
3535

3636
matchingBranches = BranchConfig.CreateDefaultBranchConfig(FallbackConfigName)
3737
.Apply(new BranchConfig
@@ -103,7 +103,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
103103
}
104104
}
105105

106-
log.Info("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.FriendlyName)));
106+
log.Info("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.Name.FriendlyName)));
107107

108108
if (possibleParents.Count == 1)
109109
{
@@ -125,7 +125,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
125125
// if develop exists and master if not
126126
var errorMessage = possibleParents.Count == 0
127127
? "Failed to inherit Increment branch configuration, no branches found."
128-
: "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.FriendlyName));
128+
: "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.Name.FriendlyName));
129129

130130
var chosenBranch = repositoryMetadataProvider.GetChosenBranch(configuration);
131131
if (chosenBranch == null)
@@ -135,7 +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.FriendlyName;
138+
var branchName = chosenBranch.Name.FriendlyName;
139139
log.Warning(errorMessage + System.Environment.NewLine + "Falling back to " + branchName + " branch config");
140140

141141
// To prevent infinite loops, make sure that a new branch was chosen.
@@ -189,22 +189,22 @@ private IBranch[] CalculateWhenMultipleParents(ICommit currentCommit, ref IBranc
189189
}
190190
else if (branches.Count > 1)
191191
{
192-
currentBranch = branches.FirstOrDefault(b => b.NameWithoutRemote == Config.MasterBranchKey) ?? branches.First();
192+
currentBranch = branches.FirstOrDefault(b => b.Name.NameWithoutRemote == Config.MasterBranchKey) ?? branches.First();
193193
}
194194
else
195195
{
196196
var possibleTargetBranches = repositoryMetadataProvider.GetBranchesForCommit(parents[0]).ToList();
197197
if (possibleTargetBranches.Count > 1)
198198
{
199-
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.NameWithoutRemote == Config.MasterBranchKey) ?? possibleTargetBranches.First();
199+
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.Name.NameWithoutRemote == Config.MasterBranchKey) ?? possibleTargetBranches.First();
200200
}
201201
else
202202
{
203203
currentBranch = possibleTargetBranches.FirstOrDefault() ?? currentBranch;
204204
}
205205
}
206206

207-
log.Info("HEAD is merge commit, this is likely a pull request using " + currentBranch.FriendlyName + " as base");
207+
log.Info("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name.FriendlyName + " as base");
208208

209209
return excludedBranches;
210210
}
@@ -216,7 +216,7 @@ private static BranchConfig ChooseMasterOrDevelopIncrementStrategyIfTheChosenBra
216216
BranchConfig masterOrDevelopConfig = null;
217217
var developBranchRegex = config.Branches[Config.DevelopBranchKey].Regex;
218218
var masterBranchRegex = config.Branches[Config.MasterBranchKey].Regex;
219-
if (Regex.IsMatch(chosenBranch.FriendlyName, developBranchRegex, RegexOptions.IgnoreCase))
219+
if (Regex.IsMatch(chosenBranch.Name.FriendlyName, developBranchRegex, RegexOptions.IgnoreCase))
220220
{
221221
// Normally we would not expect this to happen but for safety we add a check
222222
if (config.Branches[Config.DevelopBranchKey].Increment !=
@@ -228,7 +228,7 @@ private static BranchConfig ChooseMasterOrDevelopIncrementStrategyIfTheChosenBra
228228
};
229229
}
230230
}
231-
else if (Regex.IsMatch(chosenBranch.FriendlyName, masterBranchRegex, RegexOptions.IgnoreCase))
231+
else if (Regex.IsMatch(chosenBranch.Name.FriendlyName, masterBranchRegex, RegexOptions.IgnoreCase))
232232
{
233233
// Normally we would not expect this to happen but for safety we add a check
234234
if (config.Branches[Config.MasterBranchKey].Increment !=

src/GitVersionCore/Core/Abstractions/IRepositoryMetadataProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface IRepositoryMetadataProvider
1616
ICommit GetBaseVersionSource(ICommit currentBranchTip);
1717
IEnumerable<ICommit> GetMainlineCommitLog(ICommit baseVersionSource, ICommit mainlineTip);
1818
IEnumerable<ICommit> GetMergeBaseCommits(ICommit mergeCommit, ICommit mergedHead, ICommit findMergeBase);
19+
IEnumerable<ICommit> GetCommitLog(ICommit baseVersionSource, ICommit currentCommit);
1920

2021
IBranch GetTargetBranch(string targetBranchName);
2122
IBranch FindBranch(string branchName);
@@ -38,7 +39,6 @@ public interface IRepositoryMetadataProvider
3839
IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, string tagPrefixRegex);
3940
IEnumerable<Tuple<ITag, SemanticVersion>> GetValidVersionTags(string tagPrefixRegex, DateTimeOffset? olderThan = null);
4041

41-
IEnumerable<ICommit> GetCommitLog(ICommit baseVersionSource, ICommit currentCommit);
4242
bool GetMatchingCommitBranch(ICommit baseVersionSource, IBranch branch, ICommit firstMatchingCommit);
4343
string ShortenObjectId(ICommit commit);
4444
VersionField? DetermineIncrementedField(BaseVersion baseVersion, GitVersionContext context);

0 commit comments

Comments
 (0)