Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@microsoft.com>2014-04-09 19:19:06 +0400
committerEdward Thomson <ethomson@microsoft.com>2014-04-09 21:00:52 +0400
commitbd85a5b8557e27105d6476c83486516c73b95cd9 (patch)
treebb996ab1277b2cd13857a91d241ad58930c4022c
parent8a303f2b24a053816d8d88c3733447ae5565e662 (diff)
Introduce CommitOptions
Repository#Commit (and its extension methods) now take CommitOptions. All prior methods are deprecated.
-rw-r--r--LibGit2Sharp.Tests/CommitFixture.cs27
-rw-r--r--LibGit2Sharp/CommitOptions.cs26
-rw-r--r--LibGit2Sharp/IRepository.cs16
-rw-r--r--LibGit2Sharp/LibGit2Sharp.csproj1
-rw-r--r--LibGit2Sharp/Repository.cs36
-rw-r--r--LibGit2Sharp/RepositoryExtensions.cs45
6 files changed, 127 insertions, 24 deletions
diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs
index c34bd4bb..e372fd7d 100644
--- a/LibGit2Sharp.Tests/CommitFixture.cs
+++ b/LibGit2Sharp.Tests/CommitFixture.cs
@@ -752,7 +752,8 @@ namespace LibGit2Sharp.Tests
CreateAndStageANewFile(repo);
- Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true);
+ Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AmendPreviousCommit = true });
Assert.Equal(1, repo.Head.Commits.Count());
@@ -775,7 +776,8 @@ namespace LibGit2Sharp.Tests
CreateAndStageANewFile(repo);
const string commitMessage = "I'm rewriting the history!";
- Commit amendedCommit = repo.Commit(commitMessage, Constants.Signature, Constants.Signature, true);
+ Commit amendedCommit = repo.Commit(commitMessage, Constants.Signature, Constants.Signature,
+ new CommitOptions { AmendPreviousCommit = true });
AssertCommitHasBeenAmended(repo, amendedCommit, mergedCommit);
@@ -810,7 +812,8 @@ namespace LibGit2Sharp.Tests
using (var repo = new Repository(repoPath))
{
- Assert.Throws<UnbornBranchException>(() => repo.Commit("I can not amend anything !:(", Constants.Signature, Constants.Signature, true));
+ Assert.Throws<UnbornBranchException>(() =>
+ repo.Commit("I can not amend anything !:(", Constants.Signature, Constants.Signature, new CommitOptions { AmendPreviousCommit = true }));
}
}
@@ -908,7 +911,8 @@ namespace LibGit2Sharp.Tests
repo.Reset(ResetMode.Hard);
repo.RemoveUntrackedFiles();
- repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true);
+ repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AllowEmptyCommit = true });
}
}
@@ -921,9 +925,11 @@ namespace LibGit2Sharp.Tests
repo.Reset(ResetMode.Hard);
repo.RemoveUntrackedFiles();
- repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true);
+ repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AllowEmptyCommit = true });
- Assert.Throws<EmptyCommitException>(() => repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, true, false));
+ Assert.Throws<EmptyCommitException>(() => repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AmendPreviousCommit = true }));
}
}
@@ -936,9 +942,11 @@ namespace LibGit2Sharp.Tests
repo.Reset(ResetMode.Hard);
repo.RemoveUntrackedFiles();
- Commit emptyCommit = repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true);
+ Commit emptyCommit = repo.Commit("Empty commit!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AllowEmptyCommit = true });
- Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true, true);
+ Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AmendPreviousCommit = true, AllowEmptyCommit = true });
AssertCommitHasBeenAmended(repo, amendedCommit, emptyCommit);
}
}
@@ -976,7 +984,8 @@ namespace LibGit2Sharp.Tests
Touch(repo.Info.Path, "MERGE_HEAD", "f705abffe7015f2beacf2abe7a36583ebee3487e\n");
Commit newMergedCommit = repo.Commit("Merge commit", Constants.Signature, Constants.Signature);
- Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true);
+ Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature,
+ new CommitOptions { AmendPreviousCommit = true });
AssertCommitHasBeenAmended(repo, amendedCommit, newMergedCommit);
}
}
diff --git a/LibGit2Sharp/CommitOptions.cs b/LibGit2Sharp/CommitOptions.cs
new file mode 100644
index 00000000..ce20a50a
--- /dev/null
+++ b/LibGit2Sharp/CommitOptions.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace LibGit2Sharp
+{
+ /// <summary>
+ /// Provides optional additional information to commit creation.
+ /// By default, a new commit will be created (instead of amending the
+ /// HEAD commit) and an empty commit which is unchanged from the current
+ /// HEAD is disallowed.
+ /// </summary>
+ public sealed class CommitOptions
+ {
+ /// <summary>
+ /// True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.
+ /// </summary>
+ public bool AmendPreviousCommit { get; set; }
+
+ /// <summary>
+ /// True to allow creation of an empty <see cref="Commit"/>, false otherwise.
+ /// </summary>
+ public bool AllowEmptyCommit { get; set; }
+ }
+}
diff --git a/LibGit2Sharp/IRepository.cs b/LibGit2Sharp/IRepository.cs
index 75e87b8b..61c96930 100644
--- a/LibGit2Sharp/IRepository.cs
+++ b/LibGit2Sharp/IRepository.cs
@@ -163,10 +163,22 @@ namespace LibGit2Sharp
/// <param name="message">The description of why a change was made to the repository.</param>
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
+ /// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
+ /// <returns>The generated <see cref="Commit"/>.</returns>
+ Commit Commit(string message, Signature author, Signature committer, CommitOptions options = null);
+
+ /// <summary>
+ /// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="Commit"/> into the repository.
+ /// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
+ /// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
+ /// </summary>
+ /// <param name="message">The description of why a change was made to the repository.</param>
+ /// <param name="author">The <see cref="Signature"/> of who made the change.</param>
+ /// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
/// <param name="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
- /// <param name="allowEmptyCommit">True to allow creation of an empty <see cref="Commit"/>, false otherwise.</param>
/// <returns>The generated <see cref="Commit"/>.</returns>
- Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false, bool allowEmptyCommit = false);
+ [Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
+ Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit);
/// <summary>
/// Sets the current <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and
diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj
index 08311890..47153bc9 100644
--- a/LibGit2Sharp/LibGit2Sharp.csproj
+++ b/LibGit2Sharp/LibGit2Sharp.csproj
@@ -74,6 +74,7 @@
<Compile Include="CheckoutOptions.cs" />
<Compile Include="CloneOptions.cs" />
<Compile Include="CommitFilter.cs" />
+ <Compile Include="CommitOptions.cs" />
<Compile Include="CommitSortStrategies.cs" />
<Compile Include="CompareOptions.cs" />
<Compile Include="ContentChangeStats.cs" />
diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs
index 38cc0adb..019ee92e 100644
--- a/LibGit2Sharp/Repository.cs
+++ b/LibGit2Sharp/Repository.cs
@@ -893,14 +893,18 @@ namespace LibGit2Sharp
/// <param name="message">The description of why a change was made to the repository.</param>
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
- /// <param name="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
- /// <param name="allowEmptyCommit">True to allow creation of an empty <see cref="Commit"/>, false otherwise.</param>
+ /// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
/// <returns>The generated <see cref="Commit"/>.</returns>
- public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false, bool allowEmptyCommit = false)
+ public Commit Commit(string message, Signature author, Signature committer, CommitOptions options = null)
{
+ if (options == null)
+ {
+ options = new CommitOptions();
+ }
+
bool isHeadOrphaned = Info.IsHeadUnborn;
- if (amendPreviousCommit && isHeadOrphaned)
+ if (options.AmendPreviousCommit && isHeadOrphaned)
{
throw new UnbornBranchException("Can not amend anything. The Head doesn't point at any commit.");
}
@@ -908,12 +912,12 @@ namespace LibGit2Sharp
var treeId = Proxy.git_tree_create_fromindex(Index);
var tree = this.Lookup<Tree>(treeId);
- var parents = RetrieveParentsOfTheCommitBeingCreated(amendPreviousCommit).ToList();
+ var parents = RetrieveParentsOfTheCommitBeingCreated(options.AmendPreviousCommit).ToList();
- if (parents.Count == 1 && !allowEmptyCommit)
+ if (parents.Count == 1 && !options.AllowEmptyCommit)
{
var treesame = parents[0].Tree.Id.Equals(treeId);
- var amendMergeCommit = amendPreviousCommit && !isHeadOrphaned && Head.Tip.Parents.Count() > 1;
+ var amendMergeCommit = options.AmendPreviousCommit && !isHeadOrphaned && Head.Tip.Parents.Count() > 1;
if (treesame && !amendMergeCommit)
{
@@ -934,12 +938,28 @@ namespace LibGit2Sharp
return result;
}
- var logMessage = BuildCommitLogMessage(result, amendPreviousCommit, isHeadOrphaned, parents.Count > 1);
+ var logMessage = BuildCommitLogMessage(result, options.AmendPreviousCommit, isHeadOrphaned, parents.Count > 1);
LogCommit(result, logMessage);
return result;
}
+ /// <summary>
+ /// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="Commit"/> into the repository.
+ /// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
+ /// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
+ /// </summary>
+ /// <param name="message">The description of why a change was made to the repository.</param>
+ /// <param name="author">The <see cref="Signature"/> of who made the change.</param>
+ /// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
+ /// <param name="amendPreviousCommit">True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
+ /// <returns>The generated <see cref="Commit"/>.</returns>
+ [Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
+ public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit)
+ {
+ return Commit(message, author, committer, new CommitOptions { AmendPreviousCommit = amendPreviousCommit });
+ }
+
private string BuildCommitLogMessage(Commit commit, bool amendPreviousCommit, bool isHeadOrphaned, bool isMergeCommit)
{
string kind = string.Empty;
diff --git a/LibGit2Sharp/RepositoryExtensions.cs b/LibGit2Sharp/RepositoryExtensions.cs
index 82742350..d6e751b2 100644
--- a/LibGit2Sharp/RepositoryExtensions.cs
+++ b/LibGit2Sharp/RepositoryExtensions.cs
@@ -204,13 +204,31 @@ namespace LibGit2Sharp
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="message">The description of why a change was made to the repository.</param>
+ /// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
+ /// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
+ public static Commit Commit(this IRepository repository, string message, CommitOptions options = null)
+ {
+ Signature author = repository.Config.BuildSignature(DateTimeOffset.Now, true);
+
+ return repository.Commit(message, author, options);
+ }
+
+ /// <summary>
+ /// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository.
+ /// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
+ /// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
+ /// <para>Both the Author and Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para>
+ /// </summary>
+ /// <param name="repository">The <see cref="Repository"/> being worked with.</param>
+ /// <param name="message">The description of why a change was made to the repository.</param>
/// <param name="amendPreviousCommit">True to amend the current <see cref="LibGit2Sharp.Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
- public static Commit Commit(this IRepository repository, string message, bool amendPreviousCommit = false)
+ [Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
+ public static Commit Commit(this IRepository repository, string message, bool amendPreviousCommit)
{
Signature author = repository.Config.BuildSignature(DateTimeOffset.Now, true);
- return repository.Commit(message, author, amendPreviousCommit);
+ return repository.Commit(message, author, new CommitOptions { AmendPreviousCommit = amendPreviousCommit });
}
/// <summary>
@@ -222,13 +240,30 @@ namespace LibGit2Sharp
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
/// <param name="message">The description of why a change was made to the repository.</param>
- /// <param name="amendPreviousCommit">True to amend the current <see cref="LibGit2Sharp.Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
+ /// <param name="options">The <see cref="CommitOptions"/> that specify the commit behavior.</param>
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
- public static Commit Commit(this IRepository repository, string message, Signature author, bool amendPreviousCommit = false)
+ public static Commit Commit(this IRepository repository, string message, Signature author, CommitOptions options = null)
{
Signature committer = repository.Config.BuildSignature(DateTimeOffset.Now, true);
- return repository.Commit(message, author, committer, amendPreviousCommit);
+ return repository.Commit(message, author, committer, options);
+ }
+
+ /// <summary>
+ /// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository.
+ /// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
+ /// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
+ /// <para>The Committer will be guessed from the Git configuration. An exception will be raised if no configuration is reachable.</para>
+ /// </summary>
+ /// <param name="repository">The <see cref="Repository"/> being worked with.</param>
+ /// <param name="author">The <see cref="Signature"/> of who made the change.</param>
+ /// <param name="message">The description of why a change was made to the repository.</param>
+ /// <param name="amendPreviousCommit">True to amend the current <see cref="LibGit2Sharp.Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.</param>
+ /// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
+ [Obsolete("This method will be removed in the next release. Please use a Commit overload that accepts a CommitOptions instead.")]
+ public static Commit Commit(this IRepository repository, string message, Signature author, bool amendPreviousCommit)
+ {
+ return repository.Commit(message, author, new CommitOptions { AmendPreviousCommit = amendPreviousCommit });
}
/// <summary>