Skip to content

odb_foreach() issue #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
85 changes: 77 additions & 8 deletions LibGit2Sharp.Tests/ObjectDatabaseFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
Expand Down Expand Up @@ -401,21 +403,88 @@ public void CanCreateATagAnnotationPointingToAGitObject()
}
}

private static void AddCommitToRepo(Repository repo, int howMany)
{
var author = new Signature("nulltoken", "[email protected]", DateTimeOffset.Parse("Wed, Dec 14 2011 08:29:03 +0100"));

for (int i = 0; i < howMany; i++)
{
string relativeFilepath = string.Format("{0}.txt", i);
Touch(repo.Info.WorkingDirectory, relativeFilepath, string.Format("{0}\n", i));
repo.Index.Stage(relativeFilepath);

repo.Commit("Initial commit", author, author);
}
}

[Fact]
public void CanEnumerateGeneratedGitObjects()
{
string path = InitNewRepository();

using (var repo = new Repository(path))
{
AddCommitToRepo(repo, 200);

Console.WriteLine("Enumerate_Works");
repo.ObjectDatabase.Enumerate_Works();
Console.WriteLine();

Console.WriteLine("Enumerate_DoesNotWork");
repo.ObjectDatabase.Enumerate_DoesNotWork();
Console.WriteLine();
}
}

/*
$ git count-objects --verbose
count: 43
size: 4
in-pack: 1640
packs: 3
size-pack: 425
prune-packable: 0
garbage: 0
*/
[Fact]
public void CanEnumerateTheGitObjectsFromBareRepository()
{
using (var repo = new Repository(BareTestRepoPath))
{
int count = 0;

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

Assert.True(count >= 1683);
Console.WriteLine("Enumerate_DoesNotWork");
repo.ObjectDatabase.Enumerate_DoesNotWork();
Console.WriteLine();
}

/*
------ Test started: Assembly: LibGit2Sharp.Tests.dll ------

Test 'LibGit2Sharp.Tests.ObjectDatabaseFixture.CanEnumerateTheGitObjectsFromBareRepository' failed: System.OutOfMemoryException : Insufficient memory to continue the execution of the program.
at LibGit2Sharp.Core.NativeMethods.git_odb_foreach(ObjectDatabaseSafeHandle odb, git_odb_foreach_cb cb, IntPtr payload)
Core\Proxy.cs(1063,0): at LibGit2Sharp.Core.Proxy.git_odb_foreach2(ObjectDatabaseSafeHandle odb)
ObjectDatabase.cs(37,0): at LibGit2Sharp.ObjectDatabase.Enumerate()
ObjectDatabaseFixture.cs(448,0): at LibGit2Sharp.Tests.ObjectDatabaseFixture.CanEnumerateTheGitObjectsFromBareRepository()

1 : 1385f264afb75a56a5bec74243be9b367ba4ca08
2 : 181037049a54a1eb5fab404658a3a250b44335d7
3 : 1810dff58d8a660512d4832e740f692884338ccd
4 : 1a550e416326cdb4a8e127a04dd69d7a01b11cf4

...

40 : e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
41 : f60079018b664e4e79329a7ef9559c8d9e0378d1
42 : fa49b077972391ad58037050f2a75f74e3671e92
43 : fd093bff70906175335656e6ce6ae05783708765
44 : fb20a5a4b6185d9188d82c874db3d9729ef31f3b

0 passed, 1 failed, 0 skipped, took 5,46 seconds (xUnit.net 1.9.0 build 1566).

*/
}
}
}
31 changes: 29 additions & 2 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,33 @@ public static ICollection<TResult> git_odb_foreach<TResult>(
IntPtr.Zero));
}

public static void git_odb_foreach_doesnt_work(
ObjectDatabaseSafeHandle odb)
{
int count = 0;
int res = UnSafeNativeMethods.git_odb_foreach(odb, (ref GitOid id, IntPtr payload) =>
{
Console.WriteLine("{0} - {1}", ++count, new ObjectId(id));
return 0;
}, IntPtr.Zero);

Ensure.ZeroResult(res);
}

public static void git_odb_foreach_works(
ObjectDatabaseSafeHandle odb)
{
int count = 0;
int res = NativeMethods.git_odb_foreach(odb, (IntPtr id, IntPtr payload) =>
{
var t = (GitOid)Marshal.PtrToStructure(id, typeof(GitOid));
Console.WriteLine("{0} - {1}", ++count, new ObjectId(t));
return 0;
}, IntPtr.Zero);

Ensure.ZeroResult(res);
}

public static void git_odb_free(IntPtr odb)
{
NativeMethods.git_odb_free(odb);
Expand Down Expand Up @@ -1388,7 +1415,7 @@ public static string git_refspec_rtransform(GitRefSpecHandle refSpecPtr, string

public static TagFetchMode git_remote_autotag(RemoteSafeHandle remote)
{
return (TagFetchMode) NativeMethods.git_remote_autotag(remote);
return (TagFetchMode)NativeMethods.git_remote_autotag(remote);
}

public static RemoteSafeHandle git_remote_create(RepositorySafeHandle repo, string name, string url)
Expand Down Expand Up @@ -1951,7 +1978,7 @@ public static void git_stash_drop(RepositorySafeHandle repo, int index)
{
using (ThreadAffinity())
{
int res = NativeMethods.git_stash_drop(repo, (UIntPtr) index);
int res = NativeMethods.git_stash_drop(repo, (UIntPtr)index);
Ensure.BooleanResult(res);
}
}
Expand Down
10 changes: 10 additions & 0 deletions LibGit2Sharp/Core/UnSafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ internal static unsafe class UnSafeNativeMethods
[DllImport(libgit2)]
internal static extern void git_strarray_free(ref git_strarray array);

internal delegate int git_odb_foreach_cb(
[In] ref GitOid id,
IntPtr payload);

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

#region Nested type: git_strarray

internal struct git_strarray
Expand Down
10 changes: 10 additions & 0 deletions LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ internal ObjectDatabase(Repository repo)
repo.RegisterForCleanup(handle);
}

public virtual void Enumerate_Works()
{
Proxy.git_odb_foreach_works(handle);
}

public virtual void Enumerate_DoesNotWork()
{
Proxy.git_odb_foreach_doesnt_work(handle);
}

#region Implementation of IEnumerable

/// <summary>
Expand Down