Skip to content

Commit ecdd0a0

Browse files
committed
remove IsBranch method in favor of IBranch.Equals
1 parent 1ebd0e5 commit ecdd0a0

File tree

5 files changed

+23
-47
lines changed

5 files changed

+23
-47
lines changed

src/GitVersionCore.Tests/Extensions/ExtensionMethodsTests.cs

-22
This file was deleted.

src/GitVersionCore/Core/Abstractions/IRepositoryMetadataProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IRepositoryMetadataProvider
1717
IEnumerable<ICommit> GetMainlineCommitLog(ICommit baseVersionSource, ICommit mainlineTip);
1818
IEnumerable<ICommit> GetMergeBaseCommits(ICommit mergeCommit, ICommit mergedHead, ICommit findMergeBase);
1919

20-
IBranch GetTargetBranch(string targetBranch);
20+
IBranch GetTargetBranch(string targetBranchName);
2121
IBranch FindBranch(string branchName);
2222
IBranch GetChosenBranch(Config configuration);
2323
IEnumerable<IBranch> GetBranchesForCommit(ICommit commit);

src/GitVersionCore/Core/GitPreparer.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using GitVersion.Common;
45
using GitVersion.Extensions;
56
using GitVersion.Logging;
67
using GitVersion.Model.Configuration;
@@ -15,18 +16,20 @@ public class GitPreparer : IGitPreparer
1516
private readonly IGitRepository repository;
1617
private readonly IOptions<GitVersionOptions> options;
1718
private readonly IGitRepositoryInfo repositoryInfo;
19+
private readonly IRepositoryMetadataProvider repositoryMetadataProvider;
1820
private readonly ICurrentBuildAgent buildAgent;
1921

2022
private const string DefaultRemoteName = "origin";
2123

22-
public GitPreparer(ILog log, IEnvironment environment, ICurrentBuildAgent buildAgent,
23-
IOptions<GitVersionOptions> options, IGitRepository repository, IGitRepositoryInfo repositoryInfo)
24+
public GitPreparer(ILog log, IEnvironment environment, ICurrentBuildAgent buildAgent, IOptions<GitVersionOptions> options,
25+
IGitRepository repository, IGitRepositoryInfo repositoryInfo, IRepositoryMetadataProvider repositoryMetadataProvider)
2426
{
2527
this.log = log ?? throw new ArgumentNullException(nameof(log));
2628
this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
2729
this.repository = repository ?? throw new ArgumentNullException(nameof(repository));
2830
this.options = options ?? throw new ArgumentNullException(nameof(options));
2931
this.repositoryInfo = repositoryInfo ?? throw new ArgumentNullException(nameof(repositoryInfo));
32+
this.repositoryMetadataProvider = repositoryMetadataProvider ?? throw new ArgumentNullException(nameof(repositoryMetadataProvider));
3033
this.buildAgent = buildAgent;
3134
}
3235

@@ -141,7 +144,7 @@ private void CloneRepository(string repositoryUrl, string gitDirectory, Authenti
141144
/// Normalization of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways.
142145
/// It is not recommended to run normalization against a local repository
143146
/// </summary>
144-
private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string currentBranch, bool isDynamicRepository)
147+
private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string currentBranchName, bool isDynamicRepository)
145148
{
146149
var authentication = options.Value.Authentication;
147150
using var repository = this.repository.CreateNew(gitDirectory);
@@ -164,13 +167,15 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
164167
repository.Fetch(remote.Name, new string[0], authentication, null);
165168
}
166169

167-
EnsureLocalBranchExistsForCurrentBranch(repository, log, remote, currentBranch);
170+
EnsureLocalBranchExistsForCurrentBranch(repository, log, remote, currentBranchName);
168171
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(repository, log, remote.Name);
169172

173+
var currentBranch = repositoryMetadataProvider.FindBranch(currentBranchName);
170174
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
171175
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
172176
if (expectedSha != repository.Head.Tip.Sha &&
173-
(isDynamicRepository || !expectedBranchName.IsBranch(currentBranch)))
177+
(isDynamicRepository || currentBranch is null
178+
|| !repository.Head.Equals(currentBranch)))
174179
{
175180
var newExpectedSha = repository.Head.Tip.Sha;
176181
var newExpectedBranchName = repository.Head.CanonicalName;
@@ -196,12 +201,12 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
196201
// If no, go ahead and check out a new branch, using the known commit SHA as the pointer
197202
var localBranchesWhereCommitShaIsHead = repository.Branches.Where(b => !b.IsRemote && b.Tip.Sha == headSha).ToList();
198203

199-
var matchingCurrentBranch = !string.IsNullOrEmpty(currentBranch)
200-
? localBranchesWhereCommitShaIsHead.SingleOrDefault(b => b.CanonicalName.Replace("/heads/", "/") == currentBranch.Replace("/heads/", "/"))
204+
var matchingCurrentBranch = !string.IsNullOrEmpty(currentBranchName)
205+
? localBranchesWhereCommitShaIsHead.SingleOrDefault(b => b.CanonicalName.Replace("/heads/", "/") == currentBranchName.Replace("/heads/", "/"))
201206
: null;
202207
if (matchingCurrentBranch != null)
203208
{
204-
log.Info($"Checking out local branch '{currentBranch}'.");
209+
log.Info($"Checking out local branch '{currentBranchName}'.");
205210
repository.Checkout(matchingCurrentBranch);
206211
}
207212
else if (localBranchesWhereCommitShaIsHead.Count > 1)

src/GitVersionCore/Core/RepositoryMetadataProvider.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,27 @@ public IEnumerable<ICommit> GetMergeBaseCommits(ICommit mergeCommit, ICommit mer
139139
return repository.GetMergeBaseCommits(mergeCommit, mergedHead, findMergeBase);
140140
}
141141

142-
public IBranch GetTargetBranch(string targetBranch)
142+
public IBranch GetTargetBranch(string targetBranchName)
143143
{
144144
// By default, we assume HEAD is pointing to the desired branch
145145
var desiredBranch = repository.Head;
146+
var targetBranch = FindBranch(targetBranchName);
146147

147148
// Make sure the desired branch has been specified
148-
if (!string.IsNullOrEmpty(targetBranch))
149+
if (!string.IsNullOrEmpty(targetBranchName))
149150
{
150151
// There are some edge cases where HEAD is not pointing to the desired branch.
151152
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch.
152153

153154
// CanonicalName can be "refs/heads/develop", so we need to check for "/{TargetBranch}" as well
154-
if (!desiredBranch.CanonicalName.IsBranch(targetBranch))
155+
if (!desiredBranch.Equals(targetBranch))
155156
{
156157
// In the case where HEAD is not the desired branch, try to find the branch with matching name
157158
desiredBranch = repository.Branches?
158159
.Where(b =>
159-
b.CanonicalName.IsEquivalentTo(targetBranch) ||
160-
b.FriendlyName.IsEquivalentTo(targetBranch) ||
161-
b.NameWithoutRemote.IsEquivalentTo(targetBranch))
160+
b.CanonicalName.IsEquivalentTo(targetBranchName) ||
161+
b.FriendlyName.IsEquivalentTo(targetBranchName) ||
162+
b.NameWithoutRemote.IsEquivalentTo(targetBranchName))
162163
.OrderBy(b => b.IsRemote)
163164
.FirstOrDefault();
164165

@@ -172,7 +173,8 @@ public IBranch GetTargetBranch(string targetBranch)
172173

173174
public IBranch FindBranch(string branchName)
174175
{
175-
return repository.Branches.FirstOrDefault(x => x.NameWithoutRemote == branchName);
176+
return repository.Branches.FirstOrDefault(x => x.CanonicalName.IsEquivalentTo(branchName)
177+
|| x.NameWithoutRemote.IsEquivalentTo(branchName));
176178
}
177179

178180
public IBranch GetChosenBranch(Config configuration)

src/GitVersionCore/Extensions/GitExtensions.cs

-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ public static void DumpGraph(string workingDirectory, Action<string> writer = nu
4141
}
4242
}
4343

44-
public static bool IsBranch(this string branchName, string branchNameToCompare)
45-
{
46-
// "develop" == "develop"
47-
// "refs/head/develop" == "develop"
48-
return string.Equals(branchName, branchNameToCompare, StringComparison.OrdinalIgnoreCase)
49-
|| branchName.EndsWith($"/{branchNameToCompare}", StringComparison.OrdinalIgnoreCase);
50-
51-
}
52-
5344
public static string CreateGitLogArgs(int? maxCommits)
5445
{
5546
var commits = maxCommits != null ? $" -n {maxCommits}" : null;

0 commit comments

Comments
 (0)