8000 Replace managed tree indexer functionality with native libgit2 git_tr… · Folcon/libgit2sharp@0200e5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0200e5f

Browse files
committed
Replace managed tree indexer functionality with native libgit2 git_tree_frompath() invokation
1 parent 7547618 commit 0200e5f

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public void CanReadCommitWithMultipleParents()
391391
}
392392

393393
[Test]
394-
public void CanDirectlyAccessFileOfTheCommit()
394+
public void CanDirectlyAccessABlobOfTheCommit()
395395
{
396396
using (var repo = new Repository(Constants.BareTestRepoPath))
397397
{
@@ -404,6 +404,29 @@ public void CanDirectlyAccessFileOfTheCommit()
404404
}
405405
}
406406

407+
[Test]
408+
public void CanDirectlyAccessATreeOfTheCommit()
409+
{
410+
using (var repo = new Repository(Constants.BareTestRepoPath))
411+
{
412+
var commit = repo.Lookup<Commit>("4c062a6");
413+
414+
var tree1 = commit["1"].Target as Tree;
415+
tree1.ShouldNotBeNull();
416+
}
417+
}
418+
419+
[Test]
420+
public void DirectlyAccessingAnUnknownTreeEntryOfTheCommitReturnsNull()
421+
{
422+
using (var repo = new Repository(Constants.BareTestRepoPath))
423+
{
424+
var commit = repo.Lookup<Commit>("4c062a6");
425+
426+
commit["I-am-not-here"].ShouldBeNull();
427+
}
428+
}
429+
407430
[Test]
408431
public void CanCommitWithSignatureFromConfig()
409432
{
@@ -426,7 +449,7 @@ public void CanCommitWithSignatureFromConfig()
426449

427450
repo.Head[relativeFilepath].ShouldBeNull();
428451

429-
var commit = repo.Commit("Initial egotistic commit");
452+
Commit commit = repo.Commit("Initial egotistic commit");
430453

431454
AssertBlobContent(repo.Head[relativeFilepath], "nulltoken\n");
432455
AssertBlobContent(commit[relativeFilepath], "nulltoken\n");
@@ -440,7 +463,6 @@ public void CanCommitWithSignatureFromConfig()
440463
}
441464
}
442465

443-
444466
[Test]
445467
public void CanCommitALittleBit()
446468
{

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,8 @@ public static extern IntPtr git_tree_entry_byname(
399399

400400
[DllImport(libgit2)]
401401
public static extern uint git_tree_entrycount(IntPtr tree);
402+
403+
[DllImport(libgit2)]
404+
public static extern int git_tree_frompath(out IntPtr tree, IntPtr root, string treeentry_path);
402405
}
403406
}

LibGit2Sharp/Tree.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,22 @@ public TreeEntry this[string relativePath]
3232

3333
private TreeEntry RetrieveFromPath(string relativePath)
3434
{
35-
string[] pathSegments = relativePath.Split('/');
35+
Ensure.ArgumentNotNullOrEmptyString(relativePath, "relativePath");
3636

37-
Tree tree = this;
38-
39-
for (int i = 0; i < pathSegments.Length - 1; i++)
37+
using (var obj = new ObjectSafeWrapper(Id, repo))
4038
{
41-
TreeEntry entry = tree.RetrieveFromName(pathSegments[i]);
39+
IntPtr objectPtr;
40+
41+
int res = NativeMethods.git_tree_frompath(out objectPtr, obj.ObjectPtr, relativePath);
4242

43-
if (entry == null || entry.Type != GitObjectType.Tree)
43+
if (res == (int)GitErrorCode.GIT_ENOTFOUND)
4444
{
4545
return null;
4646
}
4747

48-
tree = (Tree)entry.Target;
49-
}
48+
Ensure.Success(res);
5049

51-
return tree.RetrieveFromName(pathSegments[pathSegments.Length - 1]);
52-
}
53-
54-
private TreeEntry RetrieveFromName(string entryName)
55-
{
56-
using (var obj = new ObjectSafeWrapper(Id, repo))
57-
{
58-
IntPtr e = NativeMethods.git_tree_entry_byname(obj.ObjectPtr, entryName);
50+
IntPtr e = NativeMethods.git_tree_entry_byname(objectPtr, relativePath.Split('/').Last());
5951

6052
if (e == IntPtr.Zero)
6153
{

0 commit comments

Comments
 (0)
0