diff --git a/GitFlowVersion/GitDirFinder.cs b/GitFlowVersion/GitDirFinder.cs index f5d69c662a..e1f4d7d8b1 100644 --- a/GitFlowVersion/GitDirFinder.cs +++ b/GitFlowVersion/GitDirFinder.cs @@ -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; } } diff --git a/Tests/GitDirFinderTests.cs b/Tests/GitDirFinderTests.cs new file mode 100644 index 0000000000..57150d4ea1 --- /dev/null +++ b/Tests/GitDirFinderTests.cs @@ -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)); + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 52207e03c7..65fdf174b6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -86,6 +86,7 @@ +