8000 Merge branch 'pr/1278' · libgit2/libgit2sharp@9cb1cb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cb1cb1

Browse files
committed
Merge branch 'pr/1278'
2 parents 6e58eb6 + c876006 commit 9cb1cb1

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

LibGit2Sharp/Commands/Checkout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static Branch Checkout(IRepository repository, string committishOrBranchS
4747
return Checkout(repository, branch, options);
4848
}
4949

50-
Commit commit = obj.DereferenceToCommit(true);
50+
Commit commit = obj.Peel<Commit>(true);
5151
Checkout(repository, commit.Tree, options, committishOrBranchSpec);
5252

5353
return repository.Head;

LibGit2Sharp/GitObject.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public abstract class GitObject : IEquatable<GitObject>, IBelongToARepository
2121
{ typeof(Blob), ObjectType.Blob },
2222
{ typeof(TagAnnotation), ObjectType.Tag },
2323
};
24+
internal static IDictionary<Type, GitObjectType> TypeToGitKindMap =
25+
new Dictionary<Type, GitObjectType>
26+
{
27+
{ typeof(Commit), GitObjectType.Commit },
28+
{ typeof(Tree), GitObjectType.Tree },
29+
{ typeof(Blob), GitObjectType.Blob },
30+
{ typeof(TagAnnotation), GitObjectType.Tag },
31+
};
2432

2533
private static readonly LambdaEqualityHelper<GitObject> equalityHelper =
2634
new LambdaEqualityHelper<GitObject>(x => x.Id);
@@ -83,19 +91,32 @@ internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType
8391
}
8492
}
8593

86-
internal Commit DereferenceToCommit(bool throwsIfCanNotBeDereferencedToACommit)
94+
internal T Peel<T>(bool throwOnError) where T : GitObject
8795
{
88-
using (ObjectHandle peeledHandle = Proxy.git_object_peel(repo.Handle, Id, GitObjectType.Commit, throwsIfCanNotBeDereferencedToACommit))
96+
GitObjectType kind;
97+
if (!TypeToGitKindMap.TryGetValue(typeof(T), out kind))
8998
{
90-
if (peeledHandle == null)
91-
{
92-
return null;
93-
}
99+
throw new ArgumentException("Invalid type passed to peel");
100+
}
94101

95-
return (Commit)BuildFrom(repo, Proxy.git_object_id(peeledHandle), GitObjectType.Commit, null);
102+
using (var handle = Proxy.git_object_peel(repo.Handle, Id, kind, throwOnError))
103+
{
104+
return (T)BuildFrom(this.repo, Proxy.git_object_id(handle), kind, null);
96105
}
97106
}
98107

108+
/// <summary>
109+
/// Peel this object to the specified type
110+
///
111+
/// It will throw if the object cannot be peeled to the type.
112+
/// </summary>
113+
/// <typeparam name="T">The kind of <see cref="GitObject"/> to peel to.</typeparam>
114+
/// <returns>The peeled object</returns>
115+
public T Peel<T>() where T : GitObject
116+
{
117+
return Peel<T>(true);
118+
}
119+
99120
/// <summary>
100121
/// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="GitObject"/>.
101122
/// </summary>

LibGit2Sharp/ReferenceCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ public virtual IEnumerable<Reference> ReachableFrom(
736736
{
737737
var peeledTargetCommit = reference
738738
.ResolveToDirectReference()
739-
.Target.DereferenceToCommit(false);
739+
.Target.Peel<Commit>(false);
740740

741741
if (peeledTargetCommit == null)
742742
{

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lo
587587

588588
if (lookUpOptions.HasFlag(LookUpOptions.DereferenceResultToCommit))
589589
{
590-
return obj.DereferenceToCommit(lookUpOptions.HasFlag(LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit));
590+
return obj.Peel<Commit>(lookUpOptions.HasFlag(LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit));
591591
}
592592

593593
return obj;

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private static Commit LookUpCommit(IRepository repository, string committish)
182182
{
183183
GitObject obj = repository.Lookup(committish);
184184
Ensure.GitObjectIsNotNull(obj, committish);
185-
return obj.DereferenceToCommit(true);
185+
return obj.Peel<Commit>(true);
186186
}
187187

188188
/// <summary>

0 commit comments

Comments
 (0)
0