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
18 changes: 6 additions & 12 deletions GitFlowVersion/GitDirFinder.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
namespace GitFlowVersion
{
using System.IO;
using LibGit2Sharp;

public class GitDirFinder
{

public static string TreeWalkForGitDir(string currentDirectory)
{
while (true)
string gitDir = Repository.Discover(currentDirectory);

if (gitDir != null)
{
var gitDir = Path.Combine(currentDirectory, @".git");
if (Directory.Exists(gitDir))
{
return gitDir;
}
var parent = Directory.GetParent(currentDirectory);
if (parent == null)
{
break;
}
currentDirectory = parent.FullName;
return gitDir.TrimEnd(new []{ Path.DirectorySeparatorChar });
}

return null;
}
}
Expand Down
55 changes: 55 additions & 0 deletions Tests/GitDirFinderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;
using GitFlowVersion;
using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class GitDirFinderTests
{
private string workDir;
private string gitDir;

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

gitDir = Repository.Init(workDir)
.TrimEnd(new[] { Path.DirectorySeparatorChar });

Assert.NotNull(gitDir);
}

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

[Test]
public void From_Workdir()
{
Assert.AreEqual(gitDir, GitDirFinder.TreeWalkForGitDir(workDir));
}

[Test]
public void From_Workdir_Parent()
{
string parentDir = Directory.GetParent(workDir).FullName;
Assert.Null(GitDirFinder.TreeWalkForGitDir(parentDir));
}

[Test]
public void From_GitDir()
{
Assert.AreEqual(gitDir, GitDirFinder.TreeWalkForGitDir(gitDir));
}

[Test]
public void From_RefsDir()
{
string refsDir = Path.Combine(gitDir, "refs");
Assert.AreEqual(gitDir, GitDirFinder.TreeWalkForGitDir(refsDir));
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="AssemblyLocation.cs" />
<Compile Include="BranchClassifierTests.cs" />
<Compile Include="BranchFinders\DevelopTests.cs" />
<Compile Include="GitDirFinderTests.cs" />
<Compile Include="NugetVersionBuilderTests.cs" />
<Compile Include="SemanticVersionParserTests.cs" />
<Compile Include="ShortVersionParserTests.cs" />
Expand Down