Skip to content

#1754 Allow head to move when switching branches for dynamic repositories #1758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/GitVersionCore.Tests/Extensions/ExtensionMethodsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using GitVersion;
using NUnit.Framework;

namespace GitVersionCore.Tests
{
[TestFixture]
public class ExtensionMethodsTests
{
[TestCase("develop", "develop", true)]
[TestCase("develop", "master", false)]
[TestCase("/refs/head/develop", "develop", true)]
[TestCase("/refs/head/master", "develop", false)]
[TestCase("superdevelop", "develop", false)]
[TestCase("/refs/head/superdevelop", "develop", false)]
public void TheIsBranchMethod(string input1, string input2, bool expectedOutput)
{
var actualOutput = input1.IsBranch(input2);

Assert.AreEqual(expectedOutput, actualOutput);
}
}
}
17 changes: 17 additions & 0 deletions src/GitVersionCore/Extensions/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ namespace GitVersion

static class ExtensionMethods
{
public static bool IsBranch(this string branchName, string branchNameToCompareAgainst)
{
// "develop" == "develop"
if (string.Equals(branchName, branchNameToCompareAgainst, StringComparison.OrdinalIgnoreCase))
{
return true;
}

// "refs/head/develop" == "develop"
if (branchName.EndsWith($"/{branchNameToCompareAgainst}", StringComparison.OrdinalIgnoreCase))
{
return true;
}

return false;
}

public static void AppendLineFormat(this StringBuilder stringBuilder, string format, params object[] args)
{
stringBuilder.AppendFormat(format, args);
Expand Down
6 changes: 4 additions & 2 deletions src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GitVersion
namespace GitVersion
{
using LibGit2Sharp;
using System;
Expand Down Expand Up @@ -163,7 +163,9 @@ private static Branch GetTargetBranch(IRepository repository, string targetBranc
{
// 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.
if (desiredBranch.CanonicalName != targetBranch)

// CanonicalName can be "refs/heads/develop", so we need to check for "/{TargetBranch}" as well
if (!desiredBranch.CanonicalName.IsBranch(targetBranch))
{
// In the case where HEAD is not the desired branch, try to find the branch with matching name
desiredBranch = repository?.Branches?
Expand Down
18 changes: 17 additions & 1 deletion src/GitVersionCore/Helpers/GitRepositoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo
{
using (var repo = new Repository(gitDirectory))
{
// Need to unsure the HEAD does not move, this is essentially a BugCheck
// Need to ensure the HEAD does not move, this is essentially a BugCheck
var expectedSha = repo.Head.Tip.Sha;
var expectedBranchName = repo.Head.CanonicalName;

try
{
var remote = EnsureOnlyOneRemoteIsDefined(repo);
Expand All @@ -36,6 +38,20 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo
EnsureLocalBranchExistsForCurrentBranch(repo, remote, currentBranch);
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);

// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
if (expectedSha != repo.Head.Tip.Sha &&
!expectedBranchName.IsBranch(currentBranch))
{
var newExpectedSha = repo.Head.Tip.Sha;
var newExpectedBranchName = repo.Head.CanonicalName;

Logger.WriteInfo($"Head has moved from '{expectedBranchName} | {expectedSha}' => '{newExpectedBranchName} | {newExpectedSha}', allowed since this is a dynamic repository");

expectedSha = newExpectedSha;
expectedBranchName = newExpectedBranchName;
}

var headSha = repo.Refs.Head.TargetIdentifier;

if (!repo.Info.IsHeadDetached)
Expand Down