diff options
author | nulltoken <emeric.fermas@gmail.com> | 2011-06-18 16:50:03 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2011-06-18 17:00:55 +0400 |
commit | 6f910541adb90f93392d53045894b83a30b045ef (patch) | |
tree | 435f0141c61369595a32b72f0d5117859bf4b0c2 | |
parent | a29c89651fb444efbdbaaae735d8a4f15a394d6c (diff) |
Add Repository.Discover() method
Given a path, this method will probe for a valid git repository, either bare or standard.
Should fix issue #25.
-rw-r--r-- | LibGit2Sharp.Tests/RepositoryFixture.cs | 35 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 6 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 19 |
3 files changed, 59 insertions, 1 deletions
diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs index 4c137b73..06146a06 100644 --- a/LibGit2Sharp.Tests/RepositoryFixture.cs +++ b/LibGit2Sharp.Tests/RepositoryFixture.cs @@ -250,5 +250,40 @@ namespace LibGit2Sharp.Tests Assert.Throws<ArgumentNullException>(() => repo.HasObject(null)); } } + + [Test] + public void CanDiscoverABareRepoGivenTheRepoPath() + { + string path = Repository.Discover(Constants.TestRepoPath); + path.ShouldEqual(Path.GetFullPath(Constants.TestRepoPath)); + } + + [Test] + public void CanDiscoverABareRepoGivenASubDirectoryOfTheRepoPath() + { + string path = Repository.Discover(Path.Combine(Constants.TestRepoPath, "objects/4a")); + path.ShouldEqual(Path.GetFullPath(Constants.TestRepoPath)); + } + + [Test] + public void CanDiscoverAStandardRepoGivenTheRepoPath() + { + string path = Repository.Discover(Constants.TestRepoWithWorkingDirPath); + path.ShouldEqual(Path.GetFullPath(Constants.TestRepoWithWorkingDirPath)); + } + + [Test] + public void CanDiscoverAStandardRepoGivenASubDirectoryOfTheRepoPath() + { + string path = Repository.Discover(Path.Combine(Constants.TestRepoWithWorkingDirPath, "objects/4a")); + path.ShouldEqual(Path.GetFullPath(Constants.TestRepoWithWorkingDirPath)); + } + + [Test] + public void CanDiscoverAStandardRepoGivenTheWorkingDirPath() + { + string path = Repository.Discover(Constants.TestRepoWithWorkingDirRootPath); + path.ShouldEqual(Path.GetFullPath(Constants.TestRepoWithWorkingDirPath)); + } } }
\ No newline at end of file diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index b000458c..10b2594a 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using System.Text; namespace LibGit2Sharp.Core { @@ -144,6 +145,11 @@ namespace LibGit2Sharp.Core public static extern IntPtr git_repository_database(RepositorySafeHandle repository); [DllImport(libgit2)] + public static extern int git_repository_discover(StringBuilder repository_path, int size, string start_path, + [MarshalAs(UnmanagedType.Bool)] bool across_fs, + string ceiling_dirs); + + [DllImport(libgit2)] public static extern void git_repository_free(IntPtr repository); [DllImport(libgit2)] diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 4f60318c..bc3a4bf1 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -153,7 +154,7 @@ namespace LibGit2Sharp /// </summary> /// <param name = "sha">The sha.</param> /// <returns></returns> - public bool HasObject(string sha) + public bool HasObject(string sha) //TODO: To be removed from front facing API (maybe should we create an Repository.Advanced to hold those kind of functions)? { Ensure.ArgumentNotNullOrEmptyString(sha, "sha"); @@ -235,5 +236,21 @@ namespace LibGit2Sharp { return reference != null && ((reference is DirectReference) || (reference is SymbolicReference && ((SymbolicReference)reference).Target != null)); } + + /// <summary> + /// Probe for a git repository. + /// <para>The lookup start from <paramref name="startingPath"/> and walk upward parent directories if nothing has been found.</para> + /// </summary> + /// <param name="startingPath">The base path where the lookup starts.</param> + /// <returns>The path to the git repository.</returns> + public static string Discover(string startingPath) + { + var buffer = new StringBuilder(4096); + + int result = NativeMethods.git_repository_discover(buffer, buffer.Capacity, PosixPathHelper.ToPosix(startingPath), false, null); + Ensure.Success(result); + + return PosixPathHelper.ToNative(buffer.ToString()); + } } }
\ No newline at end of file |