Skip to content

Commit 48e7905

Browse files
committed
Guess when there are two branches pointing at the same commit. Trying to go for most stable branch
Still cases where it will fail build, but this should fix the most common cases
1 parent 2379c9d commit 48e7905

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

GitVersionCore.Tests/Fixtures/RemoteRepositoryFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public RemoteRepositoryFixture(Config configuration)
2020
CloneRepository();
2121
}
2222

23+
/// <summary>
24+
/// Simulates running on build server
25+
/// </summary>
26+
public void InitialiseRepo()
27+
{
28+
new GitPreparer(null, null, new Authentication(), null, false, LocalRepositoryPath).Initialise(true);
29+
}
30+
2331
static IRepository CreateNewRepository(string path)
2432
{
2533
LibGit2Sharp.Repository.Init(path);

GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
8686
<Compile Include="GitVersionContextTests.cs" />
8787
<Compile Include="Helpers\DirectoryHelper.cs" />
88+
<Compile Include="IntegrationTests\OtherScenarios.cs" />
8889
<Compile Include="IntegrationTests\PullRequestScenarios.cs" />
8990
<Compile Include="IntegrationTests\RemoteRepositoryScenarios.cs" />
9091
<Compile Include="IntegrationTests\DevelopScenarios.cs" />
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace GitVersionCore.Tests.IntegrationTests
2+
{
3+
using System.Linq;
4+
using GitVersion;
5+
using LibGit2Sharp;
6+
using NUnit.Framework;
7+
8+
[TestFixture]
9+
public class OtherScenarios
10+
{
11+
// This is an attempt to automatically resolve the issue where you cannot build
12+
// when multiple branches point at the same commit
13+
// Current implementation favors master, then branches without - or / in their name
14+
15+
[Test]
16+
public void DoNotBlowUpWhenMasterAndDevelopPointAtSameCommit()
17+
{
18+
using (var fixture = new RemoteRepositoryFixture(new Config()))
19+
{
20+
fixture.Repository.MakeACommit();
21+
fixture.Repository.MakeATaggedCommit("1.0.0");
22+
fixture.Repository.MakeACommit();
23+
fixture.Repository.CreateBranch("develop");
24+
25+
fixture.LocalRepository.Network.Fetch(fixture.LocalRepository.Network.Remotes.First());
26+
fixture.LocalRepository.Checkout(fixture.Repository.Head.Tip);
27+
fixture.LocalRepository.Branches.Remove("master");
28+
fixture.InitialiseRepo();
29+
fixture.AssertFullSemver("1.0.1+1");
30+
}
31+
}
32+
33+
[Test]
34+
public void DoNotBlowUpWhenDevelopAndFeatureBranchPointAtSameCommit()
35+
{
36+
using (var fixture = new RemoteRepositoryFixture(new Config()))
37+
{
38+
fixture.Repository.MakeACommit();
39+
fixture.Repository.CreateBranch("develop").Checkout();
40+
fixture.Repository.MakeACommit();
41+
fixture.Repository.MakeATaggedCommit("1.0.0");
42+
fixture.Repository.MakeACommit();
43+
fixture.Repository.CreateBranch("feature/someFeature");
44+
45+
fixture.LocalRepository.Network.Fetch(fixture.LocalRepository.Network.Remotes.First());
46+
fixture.LocalRepository.Checkout(fixture.Repository.Head.Tip);
47+
fixture.LocalRepository.Branches.Remove("master");
48+
fixture.InitialiseRepo();
49+
fixture.AssertFullSemver("1.1.0-unstable.1");
50+
}
51+
}
52+
}
53+
}

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,33 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
5050

5151
if (localBranchesWhereCommitShaIsHead.Count > 1)
5252
{
53-
var names = string.Join(", ", localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName));
54-
var message = string.Format("Found more than one local branch pointing at the commit '{0}'. Unable to determine which one to use ({1}).", headSha, names);
55-
throw new WarningException(message);
53+
var branchNames = localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName);
54+
var csvNames = string.Join(", ", branchNames);
55+
const string moveBranchMsg = "Move one of the branches along a commit to remove warning";
56+
57+
Logger.WriteWarning(string.Format("Found more than one local branch pointing at the commit '{0}' ({1}).", headSha, csvNames));
58+
var master = localBranchesWhereCommitShaIsHead.SingleOrDefault(n => n.Name == "master");
59+
if (master != null)
60+
{
61+
Logger.WriteWarning("Because one of the branches is 'master', will build master." + moveBranchMsg);
62+
master.Checkout();
63+
}
64+
else
65+
{
66+
var branchesWithoutSeparators = localBranchesWhereCommitShaIsHead.Where(b => !b.Name.Contains('/') && !b.Name.Contains('-')).ToList();
67+
if (branchesWithoutSeparators.Count == 1)
68+
{
69+
var branchWithoutSeparator = branchesWithoutSeparators[0];
70+
Logger.WriteWarning(string.Format("Choosing {0} as it is the only branch without / or - in it. " + moveBranchMsg, branchWithoutSeparator.CanonicalName));
71+
branchWithoutSeparator.Checkout();
72+
}
73+
else
74+
{
75+
throw new WarningException("Failed to try and guess branch to use. " + moveBranchMsg);
76+
}
77+
}
5678
}
57-
58-
if (localBranchesWhereCommitShaIsHead.Count == 0)
79+
else if (localBranchesWhereCommitShaIsHead.Count == 0)
5980
{
6081
Logger.WriteInfo(string.Format("No local branch pointing at the commit '{0}'. Fake branch needs to be created.", headSha));
6182
CreateFakeBranchPointingAtThePullRequestTip(repo, authentication);

0 commit comments

Comments
 (0)