8000 Make `GitObject.DereferenceToCommit()` use `git_object_peel()` · ben/libgit2sharp@a9dc969 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9dc969

Browse files
yorahnulltoken
authored andcommitted
Make GitObject.DereferenceToCommit() use git_object_peel()
1 parent 32059a8 commit a9dc969

File tree

6 files changed

+61
-37
lines changed

6 files changed

+61
-37
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,30 @@ public void CanEnumerateAllCommits()
358358
}
359359

360360
[Fact]
361-
public void CanEnumerateCommitsFromATagWhichDoesNotPointAtACommit()
361+
public void CanEnumerateCommitsFromATagWhichPointsToABlob()
362362
{
363363
AssertEnumerationOfCommits(
364364
repo => new Filter { Since = repo.Tags["point_to_blob"] },
365365
new string[] { });
366366
}
367367

368+
[Fact]
369+
public void CanEnumerateCommitsFromATagWhichPointsToATree()
370+
{
371+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(BareTestRepoPath);
372+
373+
using (var repo = new Repository(path.RepositoryPath))
374+
{
375+
string headTreeSha = repo.Head.Tip.Tree.Sha;
376+
377+
Tag tag = repo.ApplyTag("point_to_tree", headTreeSha);
378+
379+
AssertEnumerationOfCommitsInRepo(repo,
380+
r => new Filter { Since = tag },
381+
new string[] { });
382+
}
383+
}
384+
368385
private static void AssertEnumerationOfCommits(Func<Repository, Filter> filterBuilder, IEnumerable<string> abbrevIds)
369386
{
370387
using (var repo = new Repository(BareTestRepoPath))

LibGit2Sharp/Core/GitObjectExtensions.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,12 @@ internal static extern int git_note_foreach(
521521
[DllImport(libgit2)]
522522
internal static extern int git_object_lookup(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, GitObjectType type);
523523

524+
[DllImport(libgit2)]
525+
internal static extern int git_object_peel(
526+
out GitObjectSafeHandle peeled,
527+
GitObjectSafeHandle obj,
528+
GitObjectType type);
529+
524530
[DllImport(libgit2)]
525531
internal static extern GitObjectType git_object_type(GitObjectSafeHandle obj);
526532

LibGit2Sharp/Core/Proxy.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,29 @@ public static GitObjectSafeHandle git_object_lookup(RepositorySafeHandle repo, O
915915
}
916916
}
917917

918+
public static GitObjectSafeHandle git_object_peel(RepositorySafeHandle repo, ObjectId id, GitObjectType type, bool throwsIfCanNotPeel)
919+
{
920+
using (ThreadAffinity())
921+
{
922+
GitObjectSafeHandle peeled;
923+
int res;
924+
925+
using (var obj = new ObjectSafeWrapper(id, repo))
926+
{
927+
res = NativeMethods.git_object_peel(out peeled, obj.ObjectPtr, type);
928+
}
929+
930+
if (!throwsIfCanNotPeel &&
931+
(res == (int)GitErrorCode.NotFound || res == (int)GitErrorCode.Ambiguous))
932+
{
933+
return null;
934+
}
935+
936+
Ensure.Success(res);
937+
return peeled;
938+
}
939+
}
940+
918941
public static GitObjectType git_object_type(GitObjectSafeHandle obj)
919942
{
920943
return NativeMethods.git_object_type(obj);

LibGit2Sharp/GitObject.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.Globalization;
44
using LibGit2Sharp.Core;
5+
using LibGit2Sharp.Core.Handles;
56

67
namespace LibGit2Sharp
78
{
@@ -76,6 +77,19 @@ internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType
7677
}
7778
}
7879

80+
internal Commit DereferenceToCommit(string identifier, bool throwsIfCanNotBeDereferencedToACommit)
81+
{
82+
using (GitObjectSafeHandle peeledHandle = Proxy.git_object_peel(repo.Handle, Id, GitObjectType.Commit, throwsIfCanNotBeDereferencedToACommit))
83+
{
8 341A 4+
if (peeledHandle == null)
85+
{
86+
return null;
87+
}
88+
89+
return (Commit) BuildFrom(repo, Proxy.git_object_id(peeledHandle), GitObjectType.Commit, null);
90+
}
91+
}
92+
7993
/// <summary>
8094
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "GitObject" />.
8195
/// </summary>

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
<Compile Include="Core\GitDirection.cs" />
105105
<Compile Include="Core\GitError.cs" />
106106
<Compile Include="Core\GitErrorCategory.cs" />
107-
<Compile Include="Core\GitObjectExtensions.cs" />
108107
<Compile Include="Core\GitOdbBackend.cs" />
109108
<Compile Include="Core\GitOdbBackendStream.cs" />
110109
<Compile Include="Core\Handles\GitErrorSafeHandle.cs" />

0 commit comments

Comments
 (0)
0