Skip to content

Commit

Permalink
Fix inmemory odb backends. ObjectDatabase now has to create a new odb…
Browse files Browse the repository at this point in the history
… and set it on the repository when it is InMemory.
  • Loading branch information
Kyle Wascher authored and Kyle Wascher committed Aug 4, 2017
1 parent f8d6dac commit 481e330
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
15 changes: 15 additions & 0 deletions LibGit2Sharp.Tests/OdbBackendFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ public void ADisposableOdbBackendGetsDisposedUponRepositoryDisposal()
Assert.Equal(1, nbOfDisposeCalls);
}

[Fact]
public void CanCreateInMemoryRepositoryWithBackend()
{
using (var repo = new Repository())
{
repo.ObjectDatabase.AddBackend(new MockOdbBackend(), int.MaxValue);

Assert.True(repo.Info.IsBare);
Assert.Null(repo.Info.Path);
Assert.Null(repo.Info.WorkingDirectory);

Assert.Throws<BareRepositoryException>(() => { var idx = repo.Index; });
}
}

#region MockOdbBackend

private class MockOdbBackend : OdbBackend, IDisposable
Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp.Tests/RepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
Expand Down Expand Up @@ -685,6 +684,8 @@ public void CanCreateInMemoryRepository()
{
using (var repo = new Repository())
{
Assert.NotNull(repo.ObjectDatabase);

Assert.True(repo.Info.IsBare);
Assert.Null(repo.Info.Path);
Assert.Null(repo.Info.WorkingDirectory);
Expand Down
6 changes: 6 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,12 @@ internal static extern unsafe int git_repository_message(
internal static extern unsafe int git_repository_new(
out git_repository* repo);

[DllImport(libgit2)]
internal static extern unsafe void git_repository_set_odb(git_repository* repo, IntPtr odb);

[DllImport(libgit2)]
internal static extern unsafe int git_odb_new(out git_odb* odb);

[DllImport(libgit2)]
internal static extern unsafe int git_repository_odb(out git_odb* odb, git_repository* repo);

Expand Down
14 changes: 14 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,20 @@ public static unsafe string git_repository_message(RepositoryHandle repo)
}
}

public static unsafe void git_repository_set_odb(RepositoryHandle repo, IntPtr gitOdbBackendPointer)
{
NativeMethods.git_repository_set_odb(repo, gitOdbBackendPointer);
}

public static unsafe ObjectDatabaseHandle git_odb_new()
{
git_odb* handle;
var res = NativeMethods.git_odb_new(out handle);
Ensure.ZeroResult(res);

return new ObjectDatabaseHandle(handle, true);
}

public static unsafe ObjectDatabaseHandle git_repository_odb(RepositoryHandle repo)
{
git_odb* handle;
Expand Down
14 changes: 12 additions & 2 deletions LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ public class ObjectDatabase : IEnumerable<GitObject>
protected ObjectDatabase()
{ }

internal ObjectDatabase(Repository repo)
internal ObjectDatabase(Repository repo, bool isInMemory)
{
this.repo = repo;
handle = Proxy.git_repository_odb(repo.Handle);

if (isInMemory)
{
handle = Proxy.git_odb_new();

Proxy.git_repository_set_odb(repo.Handle, handle.AsIntPtr());
}
else
{
handle = Proxy.git_repository_odb(repo.Handle);
}

repo.RegisterForCleanup(handle);
}
Expand Down
6 changes: 4 additions & 2 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ private Repository(string path, RepositoryOptions options, RepositoryRequiredPar
/* TODO: bug in libgit2, update when fixed by
* https://github.com/libgit2/libgit2/pull/2970
*/
if (path == null)
var isInMemory = path == null;
if (isInMemory)
{
isBare = true;
}
Expand Down Expand Up @@ -172,7 +173,8 @@ private Repository(string path, RepositoryOptions options, RepositoryRequiredPar
configurationGlobalFilePath,
configurationXDGFilePath,
configurationSystemFilePath)));
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this, isInMemory));

diff = new Diff(this);
notes = new NoteCollection(this);
ignore = new Ignore(this);
Expand Down

0 comments on commit 481e330

Please sign in to comment.