Skip to content

Commit 3af9283

Browse files
committed
Merge remote-tracking branch 'elego/plastic' into plastic
These commits were forgotten before merging pack-protocol.
2 parents a78d06f + e0b2344 commit 3af9283

File tree

4 files changed

+42
-50
lines changed

4 files changed

+42
-50
lines changed

LibGit2Sharp.Tests/ObjectDatabaseFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public void CanListAllObjectsFromDatabase(string id)
270270
objects.Contains(new ObjectId(id));
271271
}
272272

273+
[Fact]
273274
public void CanListAllCommitsFromDatabase()
274275
{
275276
TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo();

LibGit2Sharp/Indexer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ public void IndexStream(Stream stream)
4141
{
4242
byte[] buffer = new byte[4096];
4343
int read;
44-
var stats = Marshal.AllocHGlobal(Marshal.SizeOf(Stats));
45-
do {
46-
read = stream.Read(buffer, 0, buffer.Length);
47-
Ensure.Success(NativeMethods.git_indexer_stream_add(handle, buffer, new UIntPtr((ulong)read), stats));
48-
Stats = (IndexerStats)Marshal.PtrToStructure(stats, typeof(IndexerStats));
49-
} while(read > 0);
50-
Ensure.Success(NativeMethods.git_indexer_stream_finalize(handle, stats));
51-
Stats = (IndexerStats)Marshal.PtrToStructure(stats, typeof(IndexerStats));
52-
53-
Marshal.FreeHGlobal(stats);
44+
unsafe {
45+
var stats = stackalloc byte[Marshal.SizeOf(Stats)];
46+
var statsptr = new IntPtr(stats);
47+
do {
48+
read = stream.Read(buffer, 0, buffer.Length);
49+
Ensure.Success(NativeMethods.git_indexer_stream_add(handle, buffer, new UIntPtr((ulong)read), statsptr));
50+
Stats = (IndexerStats)Marshal.PtrToStructure(statsptr, typeof(IndexerStats));
51+
} while(read > 0);
52+
Ensure.Success(NativeMethods.git_indexer_stream_finalize(handle, statsptr));
53+
Stats = (IndexerStats)Marshal.PtrToStructure(statsptr, typeof(IndexerStats));
54+
}
5455
}
5556

5657
public ObjectId Hash()

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ObjectDatabase
1515
{
1616
private readonly Repository repo;
1717
private readonly ObjectDatabaseSafeHandle handle;
18-
private List<ObjectId> objects = new List<ObjectId>();
18+
private List<ObjectId> objects = null;
1919

2020
public Diff Diff
2121
{
@@ -44,6 +44,8 @@ internal ObjectDatabase(Repository repo)
4444
internal ObjectDatabase(ObjectDatabaseSafeHandle handle)
4545
{
4646
this.handle = handle;
47+
this.repo = new Repository(this);
48+
repo.RegisterForCleanup(handle);
4749
}
4850

4951
internal ObjectDatabaseSafeHandle Handle
@@ -65,6 +67,10 @@ private int ForeachCallback(ref GitOid oid, IntPtr data)
6567
/// </returns>
6668
public List<ObjectId> AllObjects()
6769
{
70+
if (objects != null)
71+
return objects;
72+
73+
objects = new List<ObjectId>();
6874
Ensure.Success(NativeMethods.git_odb_foreach(handle, ForeachCallback, IntPtr.Zero));
6975
return objects;
7076
}
@@ -168,43 +174,15 @@ internal Commit CreateCommit(string message, Signature author, Signature committ
168174
return repo.Lookup<Commit>(new ObjectId(commitOid));
169175
}
170176

171-
internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath knownPath)
177+
/// <summary>
178+
/// Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
179+
/// </summary>
180+
/// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
181+
/// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
182+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
183+
public GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any)
172184
{
173-
Ensure.ArgumentNotNull(id, "id");
174-
175-
GitOid oid = id.Oid;
176-
GitObjectSafeHandle obj = null;
177-
178-
try
179-
{
180-
int res;
181-
if (id is AbbreviatedObjectId)
182-
{
183-
res = NativeMethods.git_object_lookup_prefix_odb(out obj, handle, ref oid, (uint)((AbbreviatedObjectId)id).Length, type);
184-
}
185-
else
186-
{
187-
res = NativeMethods.git_object_lookup_odb(out obj, handle, ref oid, type);
188-
}
189-
190-
if (res == (int)GitErrorCode.GIT_ENOTFOUND)
191-
{
192-
return null;
193-
}
194-
195-
Ensure.Success(res);
196-
197-
if (id is AbbreviatedObjectId)
198-
{
199-
id = GitObject.ObjectIdOf(obj);
200-
}
201-
202-
return GitObject.CreateFromPtr(obj, id, this, knownPath);
203-
}
204-
finally
205-
{
206-
obj.SafeDispose();
207-
}
185+
return repo.Lookup(shaOrReferenceName, type, LookUpOptions.None);
208186
}
209187

210188
/// <summary>
@@ -215,12 +193,12 @@ internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath know
215193
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
216194
public GitObject Lookup(ObjectId id, GitObjectType type = GitObjectType.Any)
217195
{
218-
return LookupInternal(id, type, null);
196+
return repo.Lookup(id, type);
219197
}
220198

221199
internal GitObject LookupTreeEntryTarget(ObjectId id, FilePath path)
222200
{
223-
return LookupInternal(id, GitObjectType.Any, path);
201+
return repo.LookupTreeEntryTarget(id, path);
224202
}
225203

226204
}

LibGit2Sharp/ObjectDatabaseExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@ namespace LibGit2Sharp
33
{
44
public static class ObjectDatabaseExtensions
55
{
6+
/// <summary>
7+
/// Try to lookup an object by its sha or a reference name.
8+
/// </summary>
9+
/// <typeparam name = "T"></typeparam>
10+
/// <param name = "odb">The <see cref = "ObjectDatabase" /> being looked up.</param>
11+
/// <param name = "shaOrRef">The shaOrRef to lookup.</param>
12+
/// <returns></returns>
13+
public static T Lookup<T>(this ObjectDatabase odb, string shaOrRef) where T : GitObject
14+
{
15+
return (T)odb.Lookup(shaOrRef, GitObject.TypeToTypeMap[typeof(T)]);
16+
}
17+
618
/// <summary>
719
/// Try to lookup an object by its <see cref = "ObjectId" />.
820
/// </summary>
921
/// <typeparam name = "T"></typeparam>
10-
/// <param name = "repository">The <see cref = "Repository" /> being looked up.</param>
22+
/// <param name = "odb">The <see cref = "ObjectDatabase" /> being looked up.</param>
1123
/// <param name = "id">The id.</param>
1224
/// <returns></returns>
1325
public static T Lookup<T>(this ObjectDatabase odb, ObjectId id) where T : GitObject

0 commit comments

Comments
 (0)