Skip to content

Commit 6205f3c

Browse files
committed
Expose annotated commits
These are what we really use for merges and rebases, so expose them so we can accept these instead of having multiple variations for the user to guess.
1 parent 2614ae8 commit 6205f3c

7 files changed

Lines changed: 140 additions & 2 deletions

File tree

LibGit2Sharp/AnnotatedCommit.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
using LibGit2Sharp.Core.Handles;
4+
5+
namespace LibGit2Sharp
6+
{
7+
/// <summary>
8+
/// A commit with information about its source. Input for merge and rebase functions
9+
/// </summary>
10+
public class AnnotatedCommit : IDisposable, IAnnotatedCommit
11+
{
12+
internal readonly Repository repository;
13+
internal AnnotatedCommitHandle Handle { get; private set; }
14+
15+
/// <summary>
16+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
17+
/// </summary>
18+
/// <param name="repository">The repository in which the commit lives</param>
19+
/// <param name="revspec">A string in extended SHA-1 syntax to look up the object</param>
20+
public AnnotatedCommit(Repository repository, string revspec)
21+
{
22+
this.repository = repository;
23+
this.Handle = Proxy.git_annotated_commit_from_revspec(repository.Handle, revspec);
24+
}
25+
26+
/// <summary>
27+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
28+
/// </summary>
29+
/// <param name="repository">The repository in which the commit lives</param>
30+
/// <param name="reference">A reference pointing to the commit</param>
31+
public AnnotatedCommit(Repository repository, Reference reference)
32+
{
33+
this.repository = repository;
34+
using (var refHandle = Proxy.git_reference_lookup(repository.Handle, reference.CanonicalName, true))
35+
{
36+
this.Handle = Proxy.git_annotated_commit_from_ref(repository.Handle, refHandle);
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
42+
/// </summary>
43+
/// <param name="repository">The repository in which the commit lives</param>
44+
/// <param name="commit">A commit</param>
45+
public AnnotatedCommit(Repository repository, Commit commit)
46+
{
47+
this.repository = repository;
48+
this.Handle = Proxy.git_annotated_commit_lookup(repository.Handle, commit.Id.Oid);
49+
}
50+
51+
/// <summary>
52+
/// Releases all resource used by the <see cref="LibGit2Sharp.AnnotatedCommit"/> object.
53+
/// </summary>
54+
public void Dispose()
55+
{
56+
Handle.Dispose();
57+
}
58+
59+
AnnotatedCommit IAnnotatedCommit.GetAnnotatedCommit()
60+
{
61+
return this;
62+
}
63+
}
64+
}
65+

LibGit2Sharp/Commit.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace LibGit2Sharp
1313
/// A Commit
1414
/// </summary>
1515
[DebuggerDisplay("{DebuggerDisplay,nq}")]
16-
public class Commit : GitObject
16+
public class Commit : GitObject, IAnnotatedCommit
1717
{
1818
private readonly GitObjectLazyGroup group1;
1919
private readonly GitObjectLazyGroup group2;
@@ -138,6 +138,11 @@ private string DebuggerDisplay
138138
}
139139
}
140140

141+
AnnotatedCommit IAnnotatedCommit.GetAnnotatedCommit()
142+
{
143+
return repo.LookupAnnotatedCommit(this);
144+
}
145+
141146
private class ParentsCollection : ICollection<Commit>
142147
{
143148
private readonly Lazy<ICollection<Commit>> _parents;

LibGit2Sharp/IAnnotatedCommit.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Interface to retrieve an annotated commit from another type
7+
/// </summary>
8+
public interface IAnnotatedCommit
9+
{
10+
/// <summary>
11+
/// Retrieve an annotated commit from this object
12+
/// </summary>
13+
AnnotatedCommit GetAnnotatedCommit();
14+
}
15+
}
16+

LibGit2Sharp/IRepository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,23 @@ public interface IRepository : IDisposable
436436
/// <param name="reference">The reference mentioned in the revision (if any)</param>
437437
/// <param name="obj">The object which the revision resolves to</param>
438438
void RevParse(string revision, out Reference reference, out GitObject obj);
439+
440+
/// <summary>
441+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
442+
/// </summary>
443+
/// <param name="revspec">A string in extended SHA-1 syntax to look up the object</param>
444+
AnnotatedCommit LookupAnnotatedCommit(string revspec);
445+
446+
/// <summary>
447+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
448+
/// </summary>
449+
/// <param name="reference">A reference pointing to the commit</param>
450+
AnnotatedCommit LookupAnnotatedCommit(Reference reference);
451+
452+
/// <summary>
453+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
454+
/// </summary>
455+
/// <param name="commit">A commit</param>
456+
AnnotatedCommit LookupAnnotatedCommit(Commit commit);
439457
}
440458
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@
359359
<Compile Include="Commands\Remove.cs" />
360360
<Compile Include="Commands\Checkout.cs" />
361361
<Compile Include="Core\DisposableArray.cs" />
362+
<Compile Include="IAnnotatedCommit.cs" />
363+
<Compile Include="AnnotatedCommit.cs" />
362364
</ItemGroup>
363365
<ItemGroup>
364366
<CodeAnalysisDictionary Include="CustomDictionary.xml" />

LibGit2Sharp/Reference.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace LibGit2Sharp
1010
/// A Reference to another git object
1111
/// </summary>
1212
[DebuggerDisplay("{DebuggerDisplay,nq}")]
13-
public abstract class Reference : IEquatable<Reference>, IBelongToARepository
13+
public abstract class Reference : IEquatable<Reference>, IBelongToARepository, IAnnotatedCommit
1414
{
1515
private static readonly LambdaEqualityHelper<Reference> equalityHelper =
1616
new LambdaEqualityHelper<Reference>(x => x.CanonicalName, x => x.TargetIdentifier);
@@ -255,5 +255,10 @@ IRepository IBelongToARepository.Repository
255255
return repo;
256256
}
257257
}
258+
259+
AnnotatedCommit IAnnotatedCommit.GetAnnotatedCommit()
260+
{
261+
return repo.LookupAnnotatedCommit(this);
262+
}
258263
}
259264
}

LibGit2Sharp/Repository.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,5 +1925,32 @@ private string DebuggerDisplay
19251925
Info.IsBare ? Info.Path : Info.WorkingDirectory);
19261926
}
19271927
}
1928+
1929+
/// <summary>
1930+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
1931+
/// </summary>
1932+
/// <param name="revspec">A string in extended SHA-1 syntax to look up the object</param>
1933+
public AnnotatedCommit LookupAnnotatedCommit(string revspec)
1934+
{
1935+
return new AnnotatedCommit(this, revspec);
1936+
}
1937+
1938+
/// <summary>
1939+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
1940+
/// </summary>
1941+
/// <param name="reference">A reference pointing to the commit</param>
1942+
public AnnotatedCommit LookupAnnotatedCommit(Reference reference)
1943+
{
1944+
return new AnnotatedCommit(this, reference);
1945+
}
1946+
1947+
/// <summary>
1948+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
1949+
/// </summary>
1950+
/// <param name="commit">A commit</param>
1951+
public AnnotatedCommit LookupAnnotatedCommit(Commit commit)
1952+
{
1953+
return new AnnotatedCommit(this, commit);
1954+
}
19281955
}
19291956
}

0 commit comments

Comments
 (0)