Skip to content

Commit

Permalink
Teach repo.ObjectDatabase to enumerate GitObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Aug 15, 2013
1 parent 7ea504a commit aac17c7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
17 changes: 17 additions & 0 deletions LibGit2Sharp.Tests/ObjectDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,22 @@ public void CanCreateATagAnnotationPointingToAGitObject()
Assert.Equal(tag, fetched);
}
}

[Fact]
public void CanEnumerateTheGitObjectsFromBareRepository()
{
using (var repo = new Repository(BareTestRepoPath))
{
int count = 0;

foreach (var obj in repo.ObjectDatabase)
{
Assert.NotNull(obj);
count++;
}

Assert.True(count >= 1683);
}
}
}
}
10 changes: 10 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,16 @@ internal static extern int git_note_foreach(
[DllImport(libgit2)]
internal static extern int git_odb_exists(ObjectDatabaseSafeHandle odb, ref GitOid id);

internal delegate int git_odb_foreach_cb(
IntPtr id,
IntPtr payload);

[DllImport(libgit2)]
internal static extern int git_odb_foreach(
ObjectDatabaseSafeHandle odb,
git_odb_foreach_cb cb,
IntPtr payload);

[DllImport(libgit2)]
internal static extern void git_odb_free(IntPtr odb);

Expand Down
13 changes: 13 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -1047,6 +1048,18 @@ public static bool git_odb_exists(ObjectDatabaseSafeHandle odb, ObjectId id)
return (res == 1);
}

public static ICollection<TResult> git_odb_foreach<TResult>(
ObjectDatabaseSafeHandle odb,
Func<IntPtr, TResult> resultSelector)
{
return git_foreach(
resultSelector,
c => NativeMethods.git_odb_foreach(
odb,
(x, p) => c(x, p),
IntPtr.Zero));
}

public static void git_odb_free(IntPtr odb)
{
NativeMethods.git_odb_free(odb);
Expand Down
30 changes: 29 additions & 1 deletion LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -13,7 +14,7 @@ namespace LibGit2Sharp
/// Provides methods to directly work against the Git object database
/// without involving the index nor the working directory.
/// </summary>
public class ObjectDatabase
public class ObjectDatabase : IEnumerable<GitObject>
{
private readonly Repository repo;
private readonly ObjectDatabaseSafeHandle handle;
Expand All @@ -32,6 +33,33 @@ internal ObjectDatabase(Repository repo)
repo.RegisterForCleanup(handle);
}

#region Implementation of IEnumerable

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<GitObject> GetEnumerator()
{
ICollection<GitOid> oids = Proxy.git_odb_foreach(handle,
ptr => (GitOid) Marshal.PtrToStructure(ptr, typeof (GitOid)));

return oids
.Select(gitOid => repo.Lookup<GitObject>(new ObjectId(gitOid)))
.GetEnumerator();
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion

/// <summary>
/// Determines if the given object can be found in the object database.
/// </summary>
Expand Down

0 comments on commit aac17c7

Please sign in to comment.