Skip to content

Commit b68ccae

Browse files
committed
Merge pull request #326 from grufffta/master
get branch of commit on detached head
2 parents 50d6f03 + d367101 commit b68ccae

File tree

5 files changed

+137
-9
lines changed

5 files changed

+137
-9
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
3+
namespace GitVersionCore.Tests.Fixtures
4+
{
5+
using GitVersion;
6+
using LibGit2Sharp;
7+
8+
public class RemoteRepositoryFixture : RepositoryFixtureBase
9+
{
10+
public string LocalRepositoryPath;
11+
public IRepository LocalRepository;
12+
13+
public RemoteRepositoryFixture(Config configuration)
14+
: base(CreateNewRepository, configuration)
15+
{
16+
17+
CloneRepository();
18+
}
19+
20+
21+
22+
static IRepository CreateNewRepository(string path)
23+
{
24+
LibGit2Sharp.Repository.Init(path);
25+
Console.WriteLine("Created git repository at '{0}'", path);
26+
27+
var repo = new Repository(path);
28+
repo.MakeCommits(5);
29+
return repo;
30+
}
31+
32+
33+
void CloneRepository()
34+
{
35+
LocalRepositoryPath = PathHelper.GetTempPath();
36+
LibGit2Sharp.Repository.Clone(RepositoryPath, LocalRepositoryPath);
37+
LocalRepository = new Repository(LocalRepositoryPath);
38+
}
39+
40+
41+
public override void Dispose()
42+
{
43+
LocalRepository.Dispose();
44+
try
45+
{
46+
DirectoryHelper.DeleteDirectory(LocalRepositoryPath);
47+
}
48+
catch (Exception e)
49+
{
50+
Console.WriteLine("Failed to clean up repository path at {0}. Received exception: {1}", RepositoryPath, e.Message);
51+
}
52+
53+
base.Dispose();
54+
}
55+
}
56+
}

GitVersionCore.Tests/Fixtures/RepositoryFixtureBase.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder, Config co
1616
Repository = repoBuilder(RepositoryPath);
1717
Repository.Config.Set("user.name", "Test");
1818
Repository.Config.Set("user.email", "[email protected]");
19+
IsForTrackedBranchOnly = true;
1920
}
2021

21-
public SemanticVersion ExecuteGitVersion()
22+
public bool IsForTrackedBranchOnly { private get; set; }
23+
24+
public SemanticVersion ExecuteGitVersion(IRepository repository = null)
2225
{
2326
var vf = new GitVersionFinder();
24-
return vf.FindVersion(new GitVersionContext(Repository, configuration));
27+
return vf.FindVersion(new GitVersionContext(repository ?? Repository, configuration, IsForTrackedBranchOnly));
2528
}
2629

27-
public void AssertFullSemver(string fullSemver)
30+
public void AssertFullSemver(string fullSemver, IRepository repository = null)
2831
{
29-
ExecuteGitVersion().ToString("f").ShouldBe(fullSemver);
32+
ExecuteGitVersion(repository).ToString("f").ShouldBe(fullSemver);
3033
}
3134

32-
public void Dispose()
35+
public virtual void Dispose()
3336
{
3437
Repository.Dispose();
3538

GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@
7171
</Compile>
7272
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
7373
<Compile Include="ConfigProviderTests.cs" />
74+
<Compile Include="Fixtures\RemoteRepositoryFixture.cs" />
7475
<Compile Include="GitDirFinderTests.cs" />
7576
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
77+
<Compile Include="IntegrationTests\RemoteRepositoryTests.cs" />
7678
<Compile Include="IntegrationTests\GitFlow\DevelopScenarios.cs" />
7779
<Compile Include="IntegrationTests\GitFlow\SupportBranchScenarios.cs" />
7880
<Compile Include="IntegrationTests\GitFlow\MetaDataByCommitScenarios.cs" />
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+

2+
namespace GitVersionCore.Tests.IntegrationTests
3+
{
4+
using System;
5+
using GitVersion;
6+
using GitVersionCore.Tests.Fixtures;
7+
using LibGit2Sharp;
8+
using NUnit.Framework;
9+
10+
[TestFixture]
11+
public class RemoteRepositoryTests
12+
{
13+
[Test]
14+
public void GivenARemoteGitRepositoryWithCommits_ThenClonedLocalShouldMatchRemoteVersion()
15+
{
16+
using (var fixture = new RemoteRepositoryFixture(new Config()))
17+
{
18+
fixture.AssertFullSemver("0.1.0+4");
19+
fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository);
20+
}
21+
}
22+
23+
[Test]
24+
public void GivenARemoteGitRepositoryAheadOfLocalRepository_ThenChangesShouldPull()
25+
{
26+
using (var fixture = new RemoteRepositoryFixture(new Config()))
27+
{
28+
fixture.Repository.MakeACommit();
29+
fixture.AssertFullSemver("0.1.0+5");
30+
fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository);
31+
fixture.LocalRepository.Network.Pull(fixture.LocalRepository.Config.BuildSignature(new DateTimeOffset(DateTime.Now)), new PullOptions());
32+
fixture.AssertFullSemver("0.1.0+5", fixture.LocalRepository);
33+
}
34+
}
35+
36+
[Test]
37+
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedhead_UsingExistingImplemenationThrowsException()
38+
{
39+
40+
using (var fixture = new RemoteRepositoryFixture(new Config()))
41+
{
42+
fixture.IsForTrackedBranchOnly = false;
43+
fixture.LocalRepository.Checkout(fixture.LocalRepository.Head.Tip);
44+
45+
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));
46+
}
47+
}
48+
49+
[Test]
50+
public void GivenARemoteGitRepositoryWhenCheckingOutDetachedhead_UsingTrackingBranchOnlyBehaviourShouldReturnVersion_0_1_4plus5()
51+
{
52+
53+
using (var fixture = new RemoteRepositoryFixture(new Config()))
54+
{
55+
56+
fixture.LocalRepository.Checkout(fixture.LocalRepository.Head.Tip);
57+
58+
fixture.AssertFullSemver("0.1.0+4", fixture.LocalRepository);
59+
}
60+
}
61+
}
62+
63+
}

GitVersionCore/GitVersionContext.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
/// </summary>
1010
public class GitVersionContext
1111
{
12-
public GitVersionContext(IRepository repository, Config configuration)
13-
: this(repository, repository.Head, configuration)
12+
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true)
13+
: this(repository, repository.Head, configuration, isForTrackingBranchOnly)
1414
{
1515
Configuration = configuration;
1616
}
1717

18-
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration)
18+
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool isForTrackingBranchOnly = true)
1919
{
2020
Repository = repository;
2121
Configuration = configuration;
22+
IsContextForTrackedBranchesOnly = isForTrackingBranchOnly;
2223

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

44+
readonly bool IsContextForTrackedBranchesOnly = true;
45+
46+
4347
IEnumerable<Branch> GetBranchesContainingCommit(string commitSha)
4448
{
4549
var directBranchHasBeenFound = false;
4650
foreach (var branch in Repository.Branches)
4751
{
48-
if (branch.Tip.Sha != commitSha)
52+
if (branch.Tip.Sha != commitSha || (IsContextForTrackedBranchesOnly && !branch.IsTracking))
4953
{
5054
continue;
5155
}

0 commit comments

Comments
 (0)