Skip to content

Commit 2705108

Browse files
committed
Merge pull request #500 from JakeGinnivan/BranchesPointingAtSameCommitFix
Guess when there are two branches pointing at the same commit. Trying…
2 parents ab02c8c + 324ffe0 commit 2705108

File tree

7 files changed

+135
-35
lines changed

7 files changed

+135
-35
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" />

GitVersionCore.Tests/Helpers/GitTestExtensions.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
using System;
2-
using System.Diagnostics;
32
using System.IO;
43
using System.Linq;
5-
using System.Text;
64
using GitVersion;
7-
using GitVersion.Helpers;
85
using LibGit2Sharp;
96

107
public static class GitTestExtensions
118
{
129
static int pad = 1;
1310

14-
public static void DumpGraph(this IRepository repository)
15-
{
16-
var output = new StringBuilder();
17-
18-
try
19-
{
20-
ProcessHelper.Run(
21-
o => output.AppendLine(o),
22-
e => output.AppendLineFormat("ERROR: {0}", e),
23-
null,
24-
"git",
25-
@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
26-
repository.Info.Path);
27-
}
28-
catch (FileNotFoundException exception)
29-
{
30-
if (exception.FileName != "git")
31-
throw;
32-
33-
output.AppendLine("Could not execute 'git log' due to the following error:");
34-
output.AppendLine(exception.ToString());
35-
}
36-
37-
Trace.Write(output.ToString());
38-
}
39-
4011
public static Commit MakeACommit(this IRepository repository)
4112
{
4213
return CreateFileAndCommit(repository, Guid.NewGuid().ToString());
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);

GitVersionCore/LibGitExtensions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ namespace GitVersion
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
8+
using System.Text;
9+
using GitVersion.Helpers;
710
using LibGit2Sharp;
811

912
static class LibGitExtensions
@@ -174,5 +177,40 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
174177
}
175178
}
176179
}
180+
181+
public static void DumpGraph(this IRepository repository, Action<string> writer = null, int? maxCommits = null)
182+
{
183+
DumpGraph(repository.Info.Path, writer, maxCommits);
184+
}
185+
186+
public static void DumpGraph(string workingDirectory, Action<string> writer = null, int? maxCommits = null)
187+
{
188+
var output = new StringBuilder();
189+
190+
try
191+
{
192+
ProcessHelper.Run(
193+
o => output.AppendLine(o),
194+
e => output.AppendLineFormat("ERROR: {0}", e),
195+
null,
196+
"git",
197+
@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*" + (maxCommits != null ? string.Format(" -n {0}", maxCommits) : null),
198+
//@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
199+
workingDirectory);
200+
}
201+
catch (FileNotFoundException exception)
202+
{
203+
if (exception.FileName != "git")
204+
{
205+
throw;
206+
}
207+
208+
output.AppendLine("Could not execute 'git log' due to the following error:");
209+
output.AppendLine(exception.ToString());
210+
}
211+
212+
if (writer != null) writer(output.ToString());
213+
else Trace.Write(output.ToString());
214+
}
177215
}
178216
}

GitVersionExe/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ static void Main()
3232

3333
static int VerifyArgumentsAndRun()
3434
{
35+
Arguments arguments = null;
3536
try
3637
{
3738
var fileSystem = new FileSystem();
3839

39-
Arguments arguments;
4040
var argumentsWithoutExeName = GetArgumentsWithoutExeName();
4141
try
4242
{
@@ -99,6 +99,14 @@ static int VerifyArgumentsAndRun()
9999
{
100100
var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
101101
Logger.WriteError(error);
102+
103+
if (arguments != null)
104+
{
105+
Logger.WriteInfo(string.Empty);
106+
Logger.WriteInfo("Here is the current git graph (please include in issue): ");
107+
Logger.WriteInfo("Showing max of 100 commits");
108+
LibGitExtensions.DumpGraph(arguments.TargetPath, Logger.WriteInfo, 100);
109+
}
102110
return 1;
103111
}
104112

0 commit comments

Comments
 (0)