diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-04-15 23:39:43 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-04-24 12:51:43 +0400 |
commit | 49f12808e25e38e8f4493fe72ba137a5eaea2f37 (patch) | |
tree | f51b7d003e366740e2dd191f39259c497d800473 | |
parent | 58a11459cf54041eec11e3f6cde2b815fd707aa8 (diff) |
Add Repository.ObjectDatabase.Contains()
Introduces as well the ObjectDatabase namespace which provides methods to directly work against the Git object database without involving the index nor the working directory.
-rw-r--r-- | LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | 24 | ||||
-rw-r--r-- | LibGit2Sharp/Core/Handles/ObjectDatabaseSafeHandle.cs | 11 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 9 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 2 | ||||
-rw-r--r-- | LibGit2Sharp/ObjectDatabase.cs | 33 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 13 |
7 files changed, 93 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index 3fcdd12e..1116d395 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -46,6 +46,7 @@ </ItemGroup> <ItemGroup> <Compile Include="ConfigurationFixture.cs" /> + <Compile Include="ObjectDatabaseFixture.cs" /> <Compile Include="ResetFixture.cs" /> <Compile Include="LazyFixture.cs" /> <Compile Include="RemoteFixture.cs" /> diff --git a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs new file mode 100644 index 00000000..edfce01f --- /dev/null +++ b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs @@ -0,0 +1,24 @@ +using LibGit2Sharp.Tests.TestHelpers; +using Xunit; +using Xunit.Extensions; + +namespace LibGit2Sharp.Tests +{ + public class ObjectDatabaseFixture : BaseFixture + { + [Theory] + [InlineData("8496071c1b46c854b31185ea97743be6a8774479", true)] + [InlineData("1385f264afb75a56a5bec74243be9b367ba4ca08", true)] + [InlineData("ce08fe4884650f067bd5703b6a59a8b3b3c99a09", false)] + [InlineData("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", false)] + public void CanTellIfObjectsExists(string sha, bool shouldExists) + { + using (var repo = new Repository(BareTestRepoPath)) + { + var oid = new ObjectId(sha); + + Assert.Equal(shouldExists, repo.ObjectDatabase.Contains(oid)); + } + } + } +} diff --git a/LibGit2Sharp/Core/Handles/ObjectDatabaseSafeHandle.cs b/LibGit2Sharp/Core/Handles/ObjectDatabaseSafeHandle.cs new file mode 100644 index 00000000..0f0aa3e0 --- /dev/null +++ b/LibGit2Sharp/Core/Handles/ObjectDatabaseSafeHandle.cs @@ -0,0 +1,11 @@ +namespace LibGit2Sharp.Core.Handles +{ + internal class ObjectDatabaseSafeHandle : SafeHandleBase + { + protected override bool ReleaseHandle() + { + NativeMethods.git_odb_free(handle); + return true; + } + } +} diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index f31bfffe..bc65b470 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -218,6 +218,12 @@ namespace LibGit2Sharp.Core public static extern string git_lasterror(); [DllImport(libgit2)] + public static extern int git_odb_exists(ObjectDatabaseSafeHandle odb, ref GitOid id); + + [DllImport(libgit2)] + public static extern void git_odb_free(IntPtr odb); + + [DllImport(libgit2)] public static extern void git_object_free(IntPtr obj); [DllImport(libgit2)] @@ -327,6 +333,9 @@ namespace LibGit2Sharp.Core RepositorySafeHandle repo); [DllImport(libgit2)] + public static extern int git_repository_odb(out ObjectDatabaseSafeHandle odb, RepositorySafeHandle repo); + + [DllImport(libgit2)] public static extern int git_repository_discover( byte[] repository_path, // NB: This is more properly a StringBuilder, but it's UTF8 int size, diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 82f9556a..cb2195cd 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -60,6 +60,7 @@ <Compile Include="Core\DisposableEnumerable.cs" /> <Compile Include="Core\EnumExtensions.cs" /> <Compile Include="Core\GitObjectExtensions.cs" /> + <Compile Include="Core\Handles\ObjectDatabaseSafeHandle.cs" /> <Compile Include="Core\Handles\GitObjectSafeHandle.cs" /> <Compile Include="Core\Handles\IndexEntrySafeHandle.cs" /> <Compile Include="Core\Handles\NotOwnedSafeHandleBase.cs" /> @@ -106,6 +107,7 @@ <Compile Include="Filter.cs" /> <Compile Include="IQueryableCommitCollection.cs" /> <Compile Include="Core\LookUpOptions.cs" /> + <Compile Include="ObjectDatabase.cs" /> <Compile Include="ReferenceWrapper.cs" /> <Compile Include="ObjectId.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs new file mode 100644 index 00000000..76d2c0b4 --- /dev/null +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -0,0 +1,33 @@ +using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; + +namespace LibGit2Sharp +{ + /// <summary> + /// Provides methods to directly work against the Git object database + /// without involving the index nor the working directory. + /// </summary> + public class ObjectDatabase + { + private readonly ObjectDatabaseSafeHandle handle; + + internal ObjectDatabase(Repository repo) + { + Ensure.Success(NativeMethods.git_repository_odb(out handle, repo.Handle)); + + repo.RegisterForCleanup(handle); + } + + /// <summary> + /// Determines if the given object can be found in the object database. + /// </summary> + /// <param name="objectId">Identifier of the object being searched for.</param> + /// <returns>True if the object has been found; false otherwise.</returns> + public bool Contains(ObjectId objectId) + { + var oid = objectId.Oid; + + return NativeMethods.git_odb_exists(handle, ref oid) != (int)GitErrorCode.GIT_SUCCESS; + } + } +} diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index e49e8c0d..1eccf449 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -23,6 +23,7 @@ namespace LibGit2Sharp private readonly TagCollection tags; private readonly Lazy<RepositoryInformation> info; private readonly bool isBare; + private readonly Lazy<ObjectDatabase> odb; private readonly Stack<SafeHandleBase> handlesToCleanup = new Stack<SafeHandleBase>(); private static readonly Lazy<string> versionRetriever = new Lazy<string>(RetrieveVersion); @@ -57,6 +58,7 @@ namespace LibGit2Sharp info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare)); config = new Lazy<Configuration>(() => new Configuration(this)); remotes = new Lazy<RemoteCollection>(() => new RemoteCollection(this)); + odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this)); } ~Repository() @@ -113,6 +115,17 @@ namespace LibGit2Sharp } /// <summary> + /// Gets the database. + /// </summary> + public ObjectDatabase ObjectDatabase + { + get + { + return odb.Value; + } + } + + /// <summary> /// Lookup and enumerate references in the repository. /// </summary> public ReferenceCollection Refs |