Skip to content

Walk parents for .git dir in Task #469

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
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
1 change: 1 addition & 0 deletions GitVersionTask.Tests/GitVersionTask.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="ApprovalTestsConfig.cs" />
<Compile Include="AssemblyInfoBuilderTests.cs" />
<Compile Include="AssemblyLocation.cs" />
<Compile Include="GitVersionTaskDirectoryTests.cs" />
<Compile Include="Mocks\MockBuildEngine.cs" />
<Compile Include="GetVersionTaskTests.cs" />
<Compile Include="Helpers\IPostTestDirectoryRemover.cs" />
Expand Down
61 changes: 61 additions & 0 deletions GitVersionTask.Tests/GitVersionTaskDirectoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.IO;
using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class GitVersionTaskDirectoryTests
{
string workDirectory;
string gitDirectory;

[SetUp]
public void CreateTemporaryRepository()
{
workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

gitDirectory = Repository.Init(workDirectory)
.TrimEnd(new[] { Path.DirectorySeparatorChar });

Assert.NotNull(gitDirectory);
}

[TearDown]
public void Cleanup()
{
Directory.Delete(workDirectory, true);
}

[Test]
public void Finds_GitDirectory()
{
try
{
VersionAndBranchFinder.GetVersion(workDirectory, null, true, null);
}
catch (Exception ex)
{
// `RepositoryNotFoundException` means that it couldn't find the .git directory,
// any other exception means that the .git was found but there was some other issue that this test doesn't care about.
Assert.IsNotAssignableFrom<RepositoryNotFoundException>(ex);
}
}

[Test]
public void Finds_GitDirectory_In_Parent()
{
var childDir = Path.Combine(workDirectory, "child");
Directory.CreateDirectory(childDir);

try
{
VersionAndBranchFinder.GetVersion(childDir, null, true, null);
}
catch (Exception ex)
{
// `RepositoryNotFoundException` means that it couldn't find the .git directory,
// any other exception means that the .git was found but there was some other issue that this test doesn't care about.
Assert.IsNotAssignableFrom<RepositoryNotFoundException>(ex);
}
}
}
3 changes: 2 additions & 1 deletion GitVersionTask/VersionAndBranchFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static bool TryGetVersion(string directory, out VersionVariables versionV

public static VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem)
{
using (var repo = RepositoryLoader.GetRepo(directory))
var gitDir = GitDirFinder.TreeWalkForDotGitDir(directory);
using (var repo = RepositoryLoader.GetRepo(gitDir))
{
var ticks = DirectoryDateFinder.GetLastDirectoryWrite(directory);
var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
Expand Down