Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit bd85a5b

Browse files
author
Edward Thomson
committed
Introduce CommitOptions
Repository#Commit (and its extension methods) now take CommitOptions. All prior methods are deprecated.
1 parent 8a303f2 commit bd85a5b

File tree

6 files changed

+127
-24
lines changed

6 files changed

+127
-24
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ public void CanAmendARootCommit()
752752

753753
CreateAndStageANewFile(repo);
754754

755-
Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true);
755+
Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature,
756+
new CommitOptions { AmendPreviousCommit = true });
756757

757758
Assert.Equal(1, repo.Head.Commits.Count());
758759

@@ -775,7 +776,8 @@ public void CanAmendACommitWithMoreThanOneParent()
775776
CreateAndStageANewFile(repo);
776777
const string commitMessage = "I'm rewriting the history!";
777778

778-
Commit amendedCommit = repo.Commit(commitMessage, Constants.Signature, Constants.Signature, true);
779+
Commit amendedCommit = repo.Commit(commitMessage, Constants.Signature, Constants.Signature,
780+
new CommitOptions { AmendPreviousCommit = true });
779781

780782
AssertCommitHasBeenAmended(repo, amendedCommit, mergedCommit);
781783

@@ -810,7 +812,8 @@ public void CanNotAmendAnEmptyRepository()
810812

811813
using (var repo = new Repository(repoPath))
812814
{
813-
Assert.Throws<UnbornBranchException>(() => repo.Commit("I can not amend anything !:(", Constants.Signature, Constants.Signature, true));
815+
Assert.Throws<UnbornBranchException>(() =>
816+
repo.Commit("I can not amend anything !:(", Constants.Signature, Constants.Signature, new CommitOptions { AmendPreviousCommit = true }));
814817
}
815818
}
816819

@@ -908,7 +911,8 @@ public void CanCommitAnEmptyCommitWhenForced()
908911
repo.Reset(ResetMode.Hard);
909912
repo.RemoveUntrackedFiles();
910913

911-
repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true);
914+
repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
915+
new CommitOptions { AllowEmptyCommit = true });
912916
}
913917
}
914918

@@ -921,9 +925,11 @@ public void CanNotAmendAnEmptyCommit()
921925
repo.Reset(ResetMode.Hard);
922926
repo.RemoveUntrackedFiles();
923927

924-
repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true);
928+
repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
929+
new CommitOptions { AllowEmptyCommit = true });
925930

926-
Assert.Throws<EmptyCommitException>(() => repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, true, false));
931+
Assert.Throws<EmptyCommitException>(() => repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
932+
new CommitOptions { AmendPreviousCommit = true }));
927933
}
928934
}
929935

@@ -936,9 +942,11 @@ public void CanAmendAnEmptyCommitWhenForced()
936942
repo.Reset(ResetMode.Hard);
937943
repo.RemoveUntrackedFiles();
938944

939-
Commit emptyCommit = repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true);
945+
Commit emptyCommit = repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
946+
new CommitOptions { AllowEmptyCommit = true });
940947

941-
Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true, true);
948+
Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature,
949+
new CommitOptions { AmendPreviousCommit = true, AllowEmptyCommit = true });
942950
AssertCommitHasBeenAmended(repo, amendedCommit, emptyCommit);
943951
}
944952
}
@@ -976,7 +984,8 @@ public void CanAmendAnEmptyMergeCommit()
976984
Touch(repo.Info.Path, "MERGE_HEAD", "f705abffe7015f2beacf2abe7a36583ebee3487e\n");
977985
Commit newMergedCommit = repo.Commit("Merge commit", Constants.Signature, Constants.Signature);
978986

979-
Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true);
987+
Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature,
988+
new CommitOptions { AmendPreviousCommit = true });
980989
AssertCommitHasBeenAmended(repo, amendedCommit, newMergedCommit);
981990
}
982991
}

LibGit2Sharp/CommitOptions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp
7+
{
8+
/// <summary>
9+
/// Provides optional additional information to commit creation.
10+
/// By default, a new commit will be created (instead of amending the
11+
/// HEAD commit) and an empty commit which is unchanged from the current
12+
/// HEAD is disallowed.
13+
/// </summary>
14+
public sealed class CommitOptions
15+
{
16+
/// <summary>
17+
/// True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.
18+
/// </summary>
19+
public bool AmendPreviousCommit { get; set; }
20+
21+
/// <summary>
22+
/// True to allow creation of an empty <see cref="Commit"/>, false otherwise.
23+
/// </summary>
24+
public bool AllowEmptyCommit { get; set; }
25+
}
26+
}

LibGit2Sharp/IRepository.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ public interface IRepository : IDisposable
155155
/// <returns>The <see cref="GitObject"/> or null if it was not found.</returns>
156156
GitObject Lookup(string objectish, ObjectType type);
157157

158+
/// <summary>
159+
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="Commit"/> into the repository.
160+
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
161+
/// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
162+
/// </summary>
163+
/// <param name="message">The description of why a change was made to the repository.</param>
164+
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
165+
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
166+
/// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
167+
/// <returns>The generated <see cref="Commit"/>.</returns>
168+
Commit Commit(string message, Signature author, Signature committer, CommitOptions options = null);
169+
158170
/// <summary>
159171
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="Commit"/> into the repository.
160172
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
@@ -164,9 +176,9 @@ public interface IRepository : IDisposable
164176
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
165177
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
166178
/// <param name="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
167-
/// <param name="allowEmptyCommit">True to allow creation of an empty <see cref="Commit"/>, false otherwise.</param>
168179
/// <returns>The generated <see cref="Commit"/>.</returns>
169-
Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false, bool allowEmptyCommit = false);
180+
[Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
181+
Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit);
170182

171183
/// <summary>
172184
/// Sets the current <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="CheckoutOptions.cs" />
7575
<Compile Include="CloneOptions.cs" />
7676
<Compile Include="CommitFilter.cs" />
77+
<Compile Include="CommitOptions.cs" />
7778
<Compile Include="CommitSortStrategies.cs" />
7879
<Compile Include="CompareOptions.cs" />
7980
<Compile Include="ContentChangeStats.cs" />

LibGit2Sharp/Repository.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -893,27 +893,31 @@ public void Reset(Commit commit, IEnumerable<string> paths = null, ExplicitPaths
893893
/// <param name="message">The description of why a change was made to the repository.</param>
894894
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
895895
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
896-
/// <param name="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
897-
/// <param name="allowEmptyCommit">True to allow creation of an empty <see cref="Commit"/>, false otherwise.</param>
896+
/// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
898897
/// <returns>The generated <see cref="Commit"/>.</returns>
899-
public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false, bool allowEmptyCommit = false)
898+
public Commit Commit(string message, Signature author, Signature committer, CommitOptions options = null)
900899
{
900+
if (options == null)
901+
{
902+
options = new CommitOptions();
903+
}
904+
901905
bool isHeadOrphaned = Info.IsHeadUnborn;
902906

903-
if (amendPreviousCommit && isHeadOrphaned)
907+
if (options.AmendPreviousCommit && isHeadOrphaned)
904908
{
905909
throw new UnbornBranchException("Can not amend anything. The Head doesn't point at any commit.");
906910
}
907911

908912
var treeId = Proxy.git_tree_create_fromindex(Index);
909913
var tree = this.Lookup<Tree>(treeId);
910914

911-
var parents = RetrieveParentsOfTheCommitBeingCreated(amendPreviousCommit).ToList();
915+
var parents = RetrieveParentsOfTheCommitBeingCreated(options.AmendPreviousCommit).ToList();
912916

913-
if (parents.Count == 1 && !allowEmptyCommit)
917+
if (parents.Count == 1 && !options.AllowEmptyCommit)
914918
{
915919
var treesame = parents[0].Tree.Id.Equals(treeId);
916-
var amendMergeCommit = amendPreviousCommit && !isHeadOrphaned && Head.Tip.Parents.Count() > 1;
920+
var amendMergeCommit = options.AmendPreviousCommit && !isHeadOrphaned && Head.Tip.Parents.Count() > 1;
917921

918922
if (treesame && !amendMergeCommit)
919923
{
@@ -934,12 +938,28 @@ public Commit Commit(string message, Signature author, Signature committer, bool
934938
return result;
935939
}
936940

937-
var logMessage = BuildCommitLogMessage(result, amendPreviousCommit, isHeadOrphaned, parents.Count > 1);
941+
var logMessage = BuildCommitLogMessage(result, options.AmendPreviousCommit, isHeadOrphaned, parents.Count > 1);
938942
LogCommit(result, logMessage);
939943

940944
return result;
941945
}
942946

947+
/// <summary>
948+
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="Commit"/> into the repository.
949+
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
950+
/// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
951+
/// </summary>
952+
/// <param name="message">The description of why a change was made to the repository.</param>
953+
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
954+
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
955+
/// <param name="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
956+
/// <returns>The generated <see cref="Commit"/>.</returns>
957+
[Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
958+
public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit)
959+
{
960+
return Commit(message, author, committer, new CommitOptions { AmendPreviousCommit = amendPreviousCommit });
961+
}
962+
943963
private string BuildCommitLogMessage(Commit commit, bool amendPreviousCommit, bool isHeadOrphaned, bool isMergeCommit)
944964
{
945965
string kind = string.Empty;

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ private static Commit LookUpCommit(IRepository repository, string committish)
196196
return obj.DereferenceToCommit(true);
197197
}
198198

199+
/// <summary>
200+
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository.
201+
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
202+
/// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
203+
/// <para>Both the Author and Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para>
204+
/// </summary>
205+
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
206+
/// <param name="message">The description of why a change was made to the repository.</param>
207+
/// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
208+
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
209+
public static Commit Commit(this IRepository repository, string message, CommitOptions options = null)
210+
{
211+
Signature author = repository.Config.BuildSignature(DateTimeOffset.Now, true);
212+
213+
return repository.Commit(message, author, options);
214+
}
215+
199216
/// <summary>
200217
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository.
201218
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
@@ -206,11 +223,12 @@ private static Commit LookUpCommit(IRepository repository, string committish)
206223
/// <param name="message">The description of why a change was made to the repository.</param>
207224
/// <param name="amendPreviousCommit">True to amend the current <see cref="LibGit2Sharp.Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
208225
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
209-
public static Commit Commit(this IRepository repository, string message, bool amendPreviousCommit = false)
226+
[Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
227+
public static Commit Commit(this IRepository repository, string message, bool amendPreviousCommit)
210228
{
211229
Signature author = repository.Config.BuildSignature(DateTimeOffset.Now, true);
212230

213-
return repository.Commit(message, author, amendPreviousCommit);
231+
return repository.Commit(message, author, new CommitOptions { AmendPreviousCommit = amendPreviousCommit });
214232
}
215233

216234
/// <summary>
@@ -222,13 +240,30 @@ public static Commit Commit(this IRepository repository, string message, bool am
222240
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
223241
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
224242
/// <param name="message">The description of why a change was made to the repository.</param>
225-
/// <param name="amendPreviousCommit">True to amend the current <see cref="LibGit2Sharp.Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
243+
/// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
226244
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
227-
public static Commit Commit(this IRepository repository, string message, Signature author, bool amendPreviousCommit = false)
245+
public static Commit Commit(this IRepository repository, string message, Signature author, CommitOptions options = null)
228246
{
229247
Signature committer = repository.Config.BuildSignature(DateTimeOffset.Now, true);
230248

231-
return repository.Commit(message, author, committer, amendPreviousCommit);
249+
return repository.Commit(message, author, committer, options);
250+
}
251+
252+
/// <summary>
253+
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository.
254+
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
255+
/// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
256+
/// <para>The Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para>
257+
/// </summary>
258+
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
259+
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
260+
/// <param name="message">The description of why a change was made to the repository.</param>
261+
/// <param name="amendPreviousCommit">True to amend the current <see cref="LibGit2Sharp.Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
262+
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
263+
[Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
264+
public static Commit Commit(this IRepository repository, string message, Signature author, bool amendPreviousCommit)
265+
{
266+
return repository.Commit(message, author, new CommitOptions { AmendPreviousCommit = amendPreviousCommit });
232267
}
233268

234269
/// <summary>

0 commit comments

Comments
 (0)