Skip to content

Commit b2b3e38

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 2f99458 commit b2b3e38

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;
@@ -124,6 +124,11 @@ private string DebuggerDisplay
124124
}
125125
}
126126

127+
AnnotatedCommit IAnnotatedCommit.GetAnnotatedCommit()
128+
{
129+
return repo.LookupAnnotatedCommit(this);
130+
}
131+
127132
private class ParentsCollection : ICollection<Commit>
128133
{
129134
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
@@ -416,5 +416,23 @@ public interface IRepository : IDisposable
416416
/// <param name="options">Determines how the commit will be described.</param>
417417
/// <returns>A descriptive identifier for the commit based on the nearest annotated tag.</returns>
418418
string Describe(Commit commit, DescribeOptions options);
419+
420+
/// <summary>
421+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
422+
/// </summary>
423+
/// <param name="revspec">A string in extended SHA-1 syntax to look up the object</param>
424+
AnnotatedCommit LookupAnnotatedCommit(string revspec);
425+
426+
/// <summary>
427+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
428+
/// </summary>
429+
/// <param name="reference">A reference pointing to the commit</param>
430+
AnnotatedCommit LookupAnnotatedCommit(Reference reference);
431+
432+
/// <summary>
433+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
434+
/// </summary>
435+
/// <param name="commit">A commit</param>
436+
AnnotatedCommit LookupAnnotatedCommit(Commit commit);
419437
}
420438
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@
358358
<Compile Include="Commands\Stage.cs" />
359359
<Compile Include="Commands\Remove.cs" />
360360
<Compile Include="Core\DisposableArray.cs" />
361+
<Compile Include="IAnnotatedCommit.cs" />
362+
<Compile Include="AnnotatedCommit.cs" />
361363
</ItemGroup>
362364
<ItemGroup>
363365
<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
@@ -1923,5 +1923,32 @@ private string DebuggerDisplay
19231923
Info.IsBare ? Info.Path : Info.WorkingDirectory);
19241924
}
19251925
}
1926+
1927+
/// <summary>
1928+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
1929+
/// </summary>
1930+
/// <param name="revspec">A string in extended SHA-1 syntax to look up the object</param>
1931+
public AnnotatedCommit LookupAnnotatedCommit(string revspec)
1932+
{
1933+
return new AnnotatedCommit(this, revspec);
1934+
}
1935+
1936+
/// <summary>
1937+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
1938+
/// </summary>
1939+
/// <param name="reference">A reference pointing to the commit</param>
1940+
public AnnotatedCommit LookupAnnotatedCommit(Reference reference)
1941+
{
1942+
return new AnnotatedCommit(this, reference);
1943+
}
1944+
1945+
/// <summary>
1946+
/// Initialize a <see cref="LibGit2Sharp.AnnotatedCommit"/> from extended SHA-1 syntax
1947+
/// </summary>
1948+
/// <param name="commit">A commit</param>
1949+
public AnnotatedCommit LookupAnnotatedCommit(Commit commit)
1950+
{
1951+
return new AnnotatedCommit(this, commit);
1952+
}
19261953
}
19271954
}

0 commit comments

Comments
 (0)