Skip to content

Commit 60b8762

Browse files
Kyle WascherKyle Wascher
authored andcommitted
Fix inmemory odb backends. ObjectDatabase now has to create a new odb and set it on the repository when it is bare.
1 parent 49542c1 commit 60b8762

6 files changed

Lines changed: 51 additions & 4 deletions

File tree

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ public void ADisposableOdbBackendGetsDisposedUponRepositoryDisposal()
251251
Assert.Equal(1, nbOfDisposeCalls);
252252
}
253253

254+
[Fact]
255+
public void CanCreateInMemoryRepositoryWithBackend()
256+
{
257+
using (var repo = new Repository())
258+
{
259+
repo.ObjectDatabase.AddBackend(new MockOdbBackend(), int.MaxValue);
260+
261+
Assert.True(repo.Info.IsBare);
262+
Assert.Null(repo.Info.Path);
263+
Assert.Null(repo.Info.WorkingDirectory);
264+
265+
Assert.Throws<BareRepositoryException>(() => { var idx = repo.Index; });
266+
}
267+
}
268+
254269
#region MockOdbBackend
255270

256271
private class MockOdbBackend : OdbBackend, IDisposable

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7-
using Xunit.Extensions;
87

98
namespace LibGit2Sharp.Tests
109
{
@@ -685,6 +684,8 @@ public void CanCreateInMemoryRepository()
685684
{
686685
using (var repo = new Repository())
687686
{
687+
Assert.NotNull(repo.ObjectDatabase);
688+
688689
Assert.True(repo.Info.IsBare);
689690
Assert.Null(repo.Info.Path);
690691
Assert.Null(repo.Info.WorkingDirectory);

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,12 @@ internal static extern unsafe int git_repository_message(
13941394
internal static extern unsafe int git_repository_new(
13951395
out git_repository* repo);
13961396

1397+
[DllImport(libgit2)]
1398+
internal static extern unsafe void git_repository_set_odb(git_repository* repo, IntPtr odb);
1399+
1400+
[DllImport(libgit2)]
1401+
internal static extern unsafe int git_odb_new(out git_odb* odb);
1402+
13971403
[DllImport(libgit2)]
13981404
internal static extern unsafe int git_repository_odb(out git_odb* odb, git_repository* repo);
13991405

LibGit2Sharp/Core/Proxy.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,20 @@ public static unsafe string git_repository_message(RepositoryHandle repo)
25272527
}
25282528
}
25292529

2530+
public static unsafe void git_repository_set_odb(RepositoryHandle repo, IntPtr gitOdbBackendPointer)
2531+
{
2532+
NativeMethods.git_repository_set_odb(repo, gitOdbBackendPointer);
2533+
}
2534+
2535+
public static unsafe ObjectDatabaseHandle git_odb_new()
2536+
{
2537+
git_odb* handle;
2538+
var res = NativeMethods.git_odb_new(out handle);
2539+
Ensure.ZeroResult(res);
2540+
2541+
return new ObjectDatabaseHandle(handle, true);
2542+
}
2543+
25302544
public static unsafe ObjectDatabaseHandle git_repository_odb(RepositoryHandle repo)
25312545
{
25322546
git_odb* handle;

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@ public class ObjectDatabase : IEnumerable<GitObject>
2525
protected ObjectDatabase()
2626
{ }
2727

28-
internal ObjectDatabase(Repository repo)
28+
internal ObjectDatabase(Repository repo, bool isBare)
2929
{
3030
this.repo = repo;
31-
handle = Proxy.git_repository_odb(repo.Handle);
31+
32+
if (isBare)
33+
{
34+
handle = Proxy.git_odb_new();
35+
36+
Proxy.git_repository_set_odb(repo.Handle, handle.AsIntPtr());
37+
}
38+
else
39+
{
40+
handle = Proxy.git_repository_odb(repo.Handle);
41+
}
3242

3343
repo.RegisterForCleanup(handle);
3444
}

LibGit2Sharp/Repository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ private Repository(string path, RepositoryOptions options, RepositoryRequiredPar
172172
configurationGlobalFilePath,
173173
configurationXDGFilePath,
174174
configurationSystemFilePath)));
175-
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
175+
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this, isBare));
176+
176177
diff = new Diff(this);
177178
notes = new NoteCollection(this);
178179
ignore = new Ignore(this);

0 commit comments

Comments
 (0)