Skip to content
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
8 changes: 8 additions & 0 deletions GitVersionCore.Tests/Fixtures/RemoteRepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public RemoteRepositoryFixture(Config configuration)
CloneRepository();
}

/// <summary>
/// Simulates running on build server
/// </summary>
public void InitialiseRepo()
{
new GitPreparer(null, null, new Authentication(), null, false, LocalRepositoryPath).Initialise(true);
}

static IRepository CreateNewRepository(string path)
{
LibGit2Sharp.Repository.Init(path);
Expand Down
1 change: 1 addition & 0 deletions GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
<Compile Include="GitVersionContextTests.cs" />
<Compile Include="Helpers\DirectoryHelper.cs" />
<Compile Include="IntegrationTests\OtherScenarios.cs" />
<Compile Include="IntegrationTests\PullRequestScenarios.cs" />
<Compile Include="IntegrationTests\RemoteRepositoryScenarios.cs" />
<Compile Include="IntegrationTests\DevelopScenarios.cs" />
Expand Down
29 changes: 0 additions & 29 deletions GitVersionCore.Tests/Helpers/GitTestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using GitVersion;
using GitVersion.Helpers;
using LibGit2Sharp;

public static class GitTestExtensions
{
static int pad = 1;

public static void DumpGraph(this IRepository repository)
{
var output = new StringBuilder();

try
{
ProcessHelper.Run(
o => output.AppendLine(o),
e => output.AppendLineFormat("ERROR: {0}", e),
null,
"git",
@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
repository.Info.Path);
}
catch (FileNotFoundException exception)
{
if (exception.FileName != "git")
throw;

output.AppendLine("Could not execute 'git log' due to the following error:");
output.AppendLine(exception.ToString());
}

Trace.Write(output.ToString());
}

public static Commit MakeACommit(this IRepository repository)
{
return CreateFileAndCommit(repository, Guid.NewGuid().ToString());
Expand Down
53 changes: 53 additions & 0 deletions GitVersionCore.Tests/IntegrationTests/OtherScenarios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace GitVersionCore.Tests.IntegrationTests
{
using System.Linq;
using GitVersion;
using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class OtherScenarios
{
// This is an attempt to automatically resolve the issue where you cannot build
// when multiple branches point at the same commit
// Current implementation favors master, then branches without - or / in their name

[Test]
public void DoNotBlowUpWhenMasterAndDevelopPointAtSameCommit()
{
using (var fixture = new RemoteRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
fixture.Repository.MakeATaggedCommit("1.0.0");
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("develop");

fixture.LocalRepository.Network.Fetch(fixture.LocalRepository.Network.Remotes.First());
fixture.LocalRepository.Checkout(fixture.Repository.Head.Tip);
fixture.LocalRepository.Branches.Remove("master");
fixture.InitialiseRepo();
fixture.AssertFullSemver("1.0.1+1");
}
}

[Test]
public void DoNotBlowUpWhenDevelopAndFeatureBranchPointAtSameCommit()
{
using (var fixture = new RemoteRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("develop").Checkout();
fixture.Repository.MakeACommit();
fixture.Repository.MakeATaggedCommit("1.0.0");
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch("feature/someFeature");

fixture.LocalRepository.Network.Fetch(fixture.LocalRepository.Network.Remotes.First());
fixture.LocalRepository.Checkout(fixture.Repository.Head.Tip);
fixture.LocalRepository.Branches.Remove("master");
fixture.InitialiseRepo();
fixture.AssertFullSemver("1.1.0-unstable.1");
}
}
}
}
31 changes: 26 additions & 5 deletions GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,33 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut

if (localBranchesWhereCommitShaIsHead.Count > 1)
{
var names = string.Join(", ", localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName));
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);
throw new WarningException(message);
var branchNames = localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName);
var csvNames = string.Join(", ", branchNames);
const string moveBranchMsg = "Move one of the branches along a commit to remove warning";

Logger.WriteWarning(string.Format("Found more than one local branch pointing at the commit '{0}' ({1}).", headSha, csvNames));
var master = localBranchesWhereCommitShaIsHead.SingleOrDefault(n => n.Name == "master");
if (master != null)
{
Logger.WriteWarning("Because one of the branches is 'master', will build master." + moveBranchMsg);
master.Checkout();
}
else
{
var branchesWithoutSeparators = localBranchesWhereCommitShaIsHead.Where(b => !b.Name.Contains('/') && !b.Name.Contains('-')).ToList();
if (branchesWithoutSeparators.Count == 1)
{
var branchWithoutSeparator = branchesWithoutSeparators[0];
Logger.WriteWarning(string.Format("Choosing {0} as it is the only branch without / or - in it. " + moveBranchMsg, branchWithoutSeparator.CanonicalName));
branchWithoutSeparator.Checkout();
}
else
{
throw new WarningException("Failed to try and guess branch to use. " + moveBranchMsg);
}
}
}

if (localBranchesWhereCommitShaIsHead.Count == 0)
else if (localBranchesWhereCommitShaIsHead.Count == 0)
{
Logger.WriteInfo(string.Format("No local branch pointing at the commit '{0}'. Fake branch needs to be created.", headSha));
CreateFakeBranchPointingAtThePullRequestTip(repo, authentication);
Expand Down
38 changes: 38 additions & 0 deletions GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ namespace GitVersion
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using GitVersion.Helpers;
using LibGit2Sharp;

static class LibGitExtensions
Expand Down Expand Up @@ -174,5 +177,40 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
}
}
}

public static void DumpGraph(this IRepository repository, Action<string> writer = null, int? maxCommits = null)
{
DumpGraph(repository.Info.Path, writer, maxCommits);
}

public static void DumpGraph(string workingDirectory, Action<string> writer = null, int? maxCommits = null)
{
var output = new StringBuilder();

try
{
ProcessHelper.Run(
o => output.AppendLine(o),
e => output.AppendLineFormat("ERROR: {0}", e),
null,
"git",
@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*" + (maxCommits != null ? string.Format(" -n {0}", maxCommits) : null),
//@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
workingDirectory);
}
catch (FileNotFoundException exception)
{
if (exception.FileName != "git")
{
throw;
}

output.AppendLine("Could not execute 'git log' due to the following error:");
output.AppendLine(exception.ToString());
}

if (writer != null) writer(output.ToString());
else Trace.Write(output.ToString());
}
}
}
10 changes: 9 additions & 1 deletion GitVersionExe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ static void Main()

static int VerifyArgumentsAndRun()
{
Arguments arguments = null;
try
{
var fileSystem = new FileSystem();

Arguments arguments;
var argumentsWithoutExeName = GetArgumentsWithoutExeName();
try
{
Expand Down Expand Up @@ -99,6 +99,14 @@ static int VerifyArgumentsAndRun()
{
var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
Logger.WriteError(error);

if (arguments != null)
{
Logger.WriteInfo(string.Empty);
Logger.WriteInfo("Here is the current git graph (please include in issue): ");
Logger.WriteInfo("Showing max of 100 commits");
LibGitExtensions.DumpGraph(arguments.TargetPath, Logger.WriteInfo, 100);
}
return 1;
}

Expand Down