Skip to content

get branch of commit on detached head #326

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 5 commits into from
Jan 6, 2015
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
56 changes: 56 additions & 0 deletions GitVersionCore.Tests/Fixtures/RemoteRepositoryFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

namespace GitVersionCore.Tests.Fixtures
{
using GitVersion;
using LibGit2Sharp;

public class RemoteRepositoryFixture : RepositoryFixtureBase
{
public string LocalRepositoryPath;
public IRepository LocalRepository;

public RemoteRepositoryFixture(Config configuration)
: base(CreateNewRepository, configuration)
{

CloneRepository();
}



static IRepository CreateNewRepository(string path)
{
LibGit2Sharp.Repository.Init(path);
Console.WriteLine("Created git repository at '{0}'", path);

var repo = new Repository(path);
repo.MakeCommits(5);
return repo;
}


void CloneRepository()
{
LocalRepositoryPath = PathHelper.GetTempPath();
LibGit2Sharp.Repository.Clone(RepositoryPath, LocalRepositoryPath);
LocalRepository = new Repository(LocalRepositoryPath);
}


public override void Dispose()
{
LocalRepository.Dispose();
try
{
DirectoryHelper.DeleteDirectory(LocalRepositoryPath);
}
catch (Exception e)
{
Console.WriteLine("Failed to clean up repository path at {0}. Received exception: {1}", RepositoryPath, e.Message);
}

base.Dispose();
}
}
}
13 changes: 8 additions & 5 deletions GitVersionCore.Tests/Fixtures/RepositoryFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder, Config co
Repository = repoBuilder(RepositoryPath);
Repository.Config.Set("user.name", "Test");
Repository.Config.Set("user.email", "[email protected]");
IsForTrackedBranchOnly = true;
}

public SemanticVersion ExecuteGitVersion()
public bool IsForTrackedBranchOnly { private get; set; }

public SemanticVersion ExecuteGitVersion(IRepository repository = null)
{
var vf = new GitVersionFinder();
return vf.FindVersion(new GitVersionContext(Repository, configuration));
return vf.FindVersion(new GitVersionContext(repository ?? Repository, configuration, IsForTrackedBranchOnly));
}

public void AssertFullSemver(string fullSemver)
public void AssertFullSemver(string fullSemver, IRepository repository = null)
{
ExecuteGitVersion().ToString("f").ShouldBe(fullSemver);
ExecuteGitVersion(repository).ToString("f").ShouldBe(fullSemver);
}

public void Dispose()
public virtual void Dispose()
{
Repository.Dispose();

Expand Down
2 changes: 2 additions & 0 deletions GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@
</Compile>
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
<Compile Include="ConfigProviderTests.cs" />
<Compile Include="Fixtures\RemoteRepositoryFixture.cs" />
<Compile Include="GitDirFinderTests.cs" />
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
<Compile Include="IntegrationTests\RemoteRepositoryTests.cs" />
<Compile Include="IntegrationTests\GitFlow\DevelopScenarios.cs" />
<Compile Include="IntegrationTests\GitFlow\SupportBranchScenarios.cs" />
<Compile Include="IntegrationTests\GitFlow\MetaDataByCommitScenarios.cs" />
Expand Down
63 changes: 63 additions & 0 deletions GitVersionCore.Tests/IntegrationTests/RemoteRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

namespace GitVersionCore.Tests.IntegrationTests
{
using System;
using GitVersion;
using GitVersionCore.Tests.Fixtures;
using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class RemoteRepositoryTests
{
[Test]
public void GivenARemoteGitRepositoryWithCommits_ThenClonedLocalShouldMatchRemoteVersion()
{
using (var fixture = new RemoteRepositoryFixture(new Config()))
{
fixture.AssertFullSemver("0.1.0+4");
fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository);
}
}

[Test]
public void GivenARemoteGitRepositoryAheadOfLocalRepository_ThenChangesShouldPull()
{
using (var fixture = new RemoteRepositoryFixture(new Config()))
{
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("0.1.0+5");
fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository);
fixture.LocalRepository.Network.Pull(fixture.LocalRepository.Config.BuildSignature(new DateTimeOffset(DateTime.Now)), new PullOptions());
fixture.AssertFullSemver("0.1.0+5", fixture.LocalRepository);
}
}

[Test]
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedhead_UsingExistingImplemenationThrowsException()
{

using (var fixture = new RemoteRepositoryFixture(new Config()))
{
fixture.IsForTrackedBranchOnly = false;
fixture.LocalRepository.Checkout(fixture.LocalRepository.Head.Tip);

Assert.Throws<WarningException>(() => fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository), "It looks like the branch being examined is a detached Head pointing to commit '{0}'. Without a proper branch name GitVersion cannot determine the build version.", fixture.LocalRepository.Head.Tip.Id.ToString(7));
}
}

[Test]
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedhead_UsingTrackingBranchOnlyBehaviourShouldReturnVersion_0_1_4plus5()
{

using (var fixture = new RemoteRepositoryFixture(new Config()))
{

fixture.LocalRepository.Checkout(fixture.LocalRepository.Head.Tip);

fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository);
}
}
}

}
12 changes: 8 additions & 4 deletions GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
/// </summary>
public class GitVersionContext
{
public GitVersionContext(IRepository repository, Config configuration)
: this(repository, repository.Head, configuration)
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true)
: this(repository, repository.Head, configuration, isForTrackingBranchOnly)
{
Configuration = configuration;
}

public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration)
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool isForTrackingBranchOnly = true)
{
Repository = repository;
Configuration = configuration;
IsContextForTrackedBranchesOnly = isForTrackingBranchOnly;

if (currentBranch == null)
return;
Expand All @@ -40,12 +41,15 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
public Branch CurrentBranch { get; private set; }
public Commit CurrentCommit { get; private set; }

readonly bool IsContextForTrackedBranchesOnly = true;


IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
{
var directBranchHasBeenFound = false;
foreach (var branch in Repository.Branches)
{
if (branch.Tip.Sha != commitSha)
if (branch.Tip.Sha != commitSha || (IsContextForTrackedBranchesOnly && !branch.IsTracking))
{
continue;
}
Expand Down