Skip to content

Commit 6a0558b

Browse files
GeertvanHorrikarturcic
authored andcommitted
#1754 Allow head to move when switching branches for dynamic repositories
1 parent 69bd537 commit 6a0558b

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using GitVersion;
2+
using NUnit.Framework;
3+
4+
namespace GitVersionCore.Tests
5+
{
6+
[TestFixture]
7+
public class ExtensionMethodsTests
8+
{
9+
[TestCase("develop", "develop", true)]
10+
[TestCase("develop", "master", false)]
11+
[TestCase("/refs/head/develop", "develop", true)]
12+
[TestCase("/refs/head/master", "develop", false)]
13+
[TestCase("superdevelop", "develop", false)]
14+
[TestCase("/refs/head/superdevelop", "develop", false)]
15+
public void TheIsBranchMethod(string input1, string input2, bool expectedOutput)
16+
{
17+
var actualOutput = input1.IsBranch(input2);
18+
19+
Assert.AreEqual(expectedOutput, actualOutput);
20+
}
21+
}
22+
}

src/GitVersionCore/Extensions/ExtensionMethods.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ namespace GitVersion
77

88
static class ExtensionMethods
99
{
10+
public static bool IsBranch(this string branchName, string branchNameToCompareAgainst)
11+
{
12+
// "develop" == "develop"
13+
if (string.Equals(branchName, branchNameToCompareAgainst, StringComparison.OrdinalIgnoreCase))
14+
{
15+
return true;
16+
}
17+
18+
// "refs/head/develop" == "develop"
19+
if (branchName.EndsWith($"/{branchNameToCompareAgainst}", StringComparison.OrdinalIgnoreCase))
20+
{
21+
return true;
22+
}
23+
24+
return false;
25+
}
26+
1027
public static void AppendLineFormat(this StringBuilder stringBuilder, string format, params object[] args)
1128
{
1229
stringBuilder.AppendFormat(format, args);

src/GitVersionCore/GitVersionContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GitVersion
1+
namespace GitVersion
22
{
33
using LibGit2Sharp;
44
using System;
@@ -163,7 +163,9 @@ private static Branch GetTargetBranch(IRepository repository, string targetBranc
163163
{
164164
// There are some edge cases where HEAD is not pointing to the desired branch.
165165
// Therefore it's important to verify if 'currentBranch' is indeed the desired branch.
166-
if (desiredBranch.CanonicalName != targetBranch)
166+
167+
// CanonicalName can be "refs/heads/develop", so we need to check for "/{TargetBranch}" as well
168+
if (!desiredBranch.CanonicalName.IsBranch(targetBranch))
167169
{
168170
// In the case where HEAD is not the desired branch, try to find the branch with matching name
169171
desiredBranch = repository?.Branches?

src/GitVersionCore/Helpers/GitRepositoryHelper.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo
1515
{
1616
using (var repo = new Repository(gitDirectory))
1717
{
18-
// Need to unsure the HEAD does not move, this is essentially a BugCheck
18+
// Need to ensure the HEAD does not move, this is essentially a BugCheck
1919
var expectedSha = repo.Head.Tip.Sha;
20+
var expectedBranchName = repo.Head.CanonicalName;
21+
2022
try
2123
{
2224
var remote = EnsureOnlyOneRemoteIsDefined(repo);
@@ -36,6 +38,20 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo
3638
EnsureLocalBranchExistsForCurrentBranch(repo, remote, currentBranch);
3739
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
3840

41+
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
42+
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
43+
if (expectedSha != repo.Head.Tip.Sha &&
44+
!expectedBranchName.IsBranch(currentBranch))
45+
{
46+
var newExpectedSha = repo.Head.Tip.Sha;
47+
var newExpectedBranchName = repo.Head.CanonicalName;
48+
49+
Logger.WriteInfo($"Head has moved from '{expectedBranchName} | {expectedSha}' => '{newExpectedBranchName} | {newExpectedSha}', allowed since this is a dynamic repository");
50+
51+
expectedSha = newExpectedSha;
52+
expectedBranchName = newExpectedBranchName;
53+
}
54+
3955
var headSha = repo.Refs.Head.TargetIdentifier;
4056

4157
if (!repo.Info.IsHeadDetached)

0 commit comments

Comments
 (0)