Skip to content

Commit 3d25e8c

Browse files
committed
Merge pull request #469 from Sacrelicious/fix-task-not-walking-for-git-dir-#460
Walk parents for .git dir in Task
2 parents fe26bb1 + 268b301 commit 3d25e8c

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

GitVersionTask.Tests/GitVersionTask.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Compile Include="ApprovalTestsConfig.cs" />
102102
<Compile Include="AssemblyInfoBuilderTests.cs" />
103103
<Compile Include="AssemblyLocation.cs" />
104+
<Compile Include="GitVersionTaskDirectoryTests.cs" />
104105
<Compile Include="Mocks\MockBuildEngine.cs" />
105106
<Compile Include="GetVersionTaskTests.cs" />
106107
<Compile Include="Helpers\IPostTestDirectoryRemover.cs" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.IO;
3+
using LibGit2Sharp;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
public class GitVersionTaskDirectoryTests
8+
{
9+
string workDirectory;
10+
string gitDirectory;
11+
12+
[SetUp]
13+
public void CreateTemporaryRepository()
14+
{
15+
workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
16+
17+
gitDirectory = Repository.Init(workDirectory)
18+
.TrimEnd(new[] { Path.DirectorySeparatorChar });
19+
20+
Assert.NotNull(gitDirectory);
21+
}
22+
23+
[TearDown]
24+
public void Cleanup()
25+
{
26+
Directory.Delete(workDirectory, true);
27+
}
28+
29+
[Test]
30+
public void Finds_GitDirectory()
31+
{
32+
try
33+
{
34+
VersionAndBranchFinder.GetVersion(workDirectory, null, true, null);
35+
}
36+
catch (Exception ex)
37+
{
38+
// `RepositoryNotFoundException` means that it couldn't find the .git directory,
39+
// any other exception means that the .git was found but there was some other issue that this test doesn't care about.
40+
Assert.IsNotAssignableFrom<RepositoryNotFoundException>(ex);
41+
}
42+
}
43+
44+
[Test]
45+
public void Finds_GitDirectory_In_Parent()
46+
{
47+
var childDir = Path.Combine(workDirectory, "child");
48+
Directory.CreateDirectory(childDir);
49+
50+
try
51+
{
52+
VersionAndBranchFinder.GetVersion(childDir, null, true, null);
53+
}
54+
catch (Exception ex)
55+
{
56+
// `RepositoryNotFoundException` means that it couldn't find the .git directory,
57+
// any other exception means that the .git was found but there was some other issue that this test doesn't care about.
58+
Assert.IsNotAssignableFrom<RepositoryNotFoundException>(ex);
59+
}
60+
}
61+
}

GitVersionTask/VersionAndBranchFinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public static bool TryGetVersion(string directory, out VersionVariables versionV
2424

2525
public static VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem)
2626
{
27-
using (var repo = RepositoryLoader.GetRepo(directory))
27+
var gitDir = GitDirFinder.TreeWalkForDotGitDir(directory);
28+
using (var repo = RepositoryLoader.GetRepo(gitDir))
2829
{
2930
var ticks = DirectoryDateFinder.GetLastDirectoryWrite(directory);
3031
var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);

0 commit comments

Comments
 (0)