From 2f52a408344fbfff551a0eccc40293a508c79559 Mon Sep 17 00:00:00 2001 From: Ungureanu Marius Date: Tue, 3 Jun 2014 18:17:34 +0300 Subject: [PATCH] Commit: Introduce CommentaryChar and PrettifyMessage in CommitOptions --- LibGit2Sharp.Tests/CommitAncestorFixture.cs | 4 +-- LibGit2Sharp.Tests/FilterBranchFixture.cs | 4 +-- LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | 8 +++--- LibGit2Sharp.Tests/ReflogFixture.cs | 8 +++--- LibGit2Sharp/CommitOptions.cs | 24 +++++++++++++++++ LibGit2Sharp/Core/HistoryRewriter.cs | 4 +-- LibGit2Sharp/Core/Proxy.cs | 10 +++++-- LibGit2Sharp/ObjectDatabase.cs | 29 +++++++++++++++++++-- LibGit2Sharp/Repository.cs | 2 +- LibGit2Sharp/StashCollection.cs | 2 +- LibGit2Sharp/TagCollection.cs | 2 +- 11 files changed, 76 insertions(+), 21 deletions(-) diff --git a/LibGit2Sharp.Tests/CommitAncestorFixture.cs b/LibGit2Sharp.Tests/CommitAncestorFixture.cs index a8d2c6caa..fe83c1117 100644 --- a/LibGit2Sharp.Tests/CommitAncestorFixture.cs +++ b/LibGit2Sharp.Tests/CommitAncestorFixture.cs @@ -120,9 +120,9 @@ private static Commit CreateOrphanedCommit(IRepository repo) random.Author, random.Committer, "This is a test commit created by 'CommitFixture.CannotFindCommonAncestorForCommmitsWithoutCommonAncestor'", - false, random.Tree, - Enumerable.Empty()); + Enumerable.Empty(), + false); return orphanedCommit; } diff --git a/LibGit2Sharp.Tests/FilterBranchFixture.cs b/LibGit2Sharp.Tests/FilterBranchFixture.cs index a9c79663b..9cd5beac7 100644 --- a/LibGit2Sharp.Tests/FilterBranchFixture.cs +++ b/LibGit2Sharp.Tests/FilterBranchFixture.cs @@ -171,8 +171,8 @@ public void CanRewriteAuthorOfCommits() public void CanRewriteAuthorOfCommitsOnlyBeingPointedAtByTags() { var commit = repo.ObjectDatabase.CreateCommit( - Constants.Signature, Constants.Signature, "I'm a lonesome commit", false, - repo.Head.Tip.Tree, Enumerable.Empty()); + Constants.Signature, Constants.Signature, "I'm a lonesome commit", + repo.Head.Tip.Tree, Enumerable.Empty(), false); repo.Tags.Add("so-lonely", commit); diff --git a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs index b0d622ee2..649cb26ec 100644 --- a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs +++ b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs @@ -336,8 +336,8 @@ public void CanCreateATreeContainingAGitLinkFromAnUntrackedSubmoduleInTheWorking Assert.IsType(te.Target); Assert.Equal(objectId, te.Target.Id); - var commitWithSubmodule = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Submodule!", false, - tree, new[] { repo.Head.Tip }); + var commitWithSubmodule = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Submodule!", + tree, new[] { repo.Head.Tip }, false); repo.Reset(ResetMode.Soft, commitWithSubmodule); var submodule = repo.Submodules[submodulePath]; @@ -373,7 +373,7 @@ public void CanCreateACommit() Tree tree = repo.ObjectDatabase.CreateTree(td); - Commit commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Ü message", true, tree, new[] { repo.Head.Tip }); + Commit commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Ü message", tree, new[] { repo.Head.Tip }, true); Branch newHead = repo.Head; @@ -454,7 +454,7 @@ public void CreatingACommitWithMessageContainingZeroByteThrows(string message) using (var repo = new Repository(BareTestRepoPath)) { Assert.Throws(() => repo.ObjectDatabase.CreateCommit( - Constants.Signature, Constants.Signature, message, false, repo.Head.Tip.Tree, Enumerable.Empty())); + Constants.Signature, Constants.Signature, message, repo.Head.Tip.Tree, Enumerable.Empty(), false)); } } diff --git a/LibGit2Sharp.Tests/ReflogFixture.cs b/LibGit2Sharp.Tests/ReflogFixture.cs index be1d35fd7..f8bc249d1 100644 --- a/LibGit2Sharp.Tests/ReflogFixture.cs +++ b/LibGit2Sharp.Tests/ReflogFixture.cs @@ -158,8 +158,8 @@ public void AppendingToReflogDependsOnCoreLogAllRefUpdatesSetting(bool isBare, b var blob = repo.ObjectDatabase.CreateBlob(Stream.Null); var tree = repo.ObjectDatabase.CreateTree(new TreeDefinition().Add("yoink", blob, Mode.NonExecutableFile)); - var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink", false, - tree, Enumerable.Empty()); + var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink", + tree, Enumerable.Empty(), false); var branch = repo.CreateBranch("yoink", commit); var log = repo.Refs.Log(branch.CanonicalName); @@ -178,8 +178,8 @@ public void UnsignedMethodsWriteCorrectlyToTheReflog() var blob = repo.ObjectDatabase.CreateBlob(Stream.Null); var tree = repo.ObjectDatabase.CreateTree(new TreeDefinition().Add("yoink", blob, Mode.NonExecutableFile)); - var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink", false, - tree, Enumerable.Empty()); + var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink", + tree, Enumerable.Empty(), false); var direct = repo.Refs.Add("refs/heads/direct", commit.Id); AssertRefLogEntry(repo, direct.CanonicalName, direct.ResolveToDirectReference().Target.Id, null); diff --git a/LibGit2Sharp/CommitOptions.cs b/LibGit2Sharp/CommitOptions.cs index ce20a50af..c36fb4334 100644 --- a/LibGit2Sharp/CommitOptions.cs +++ b/LibGit2Sharp/CommitOptions.cs @@ -13,6 +13,19 @@ namespace LibGit2Sharp /// public sealed class CommitOptions { + /// + /// Initializes a new instance of the class. + /// + /// Default behavior: + /// The message is prettified. + /// No automatic removal of comments is performed. + /// + /// + public CommitOptions() + { + PrettifyMessage = true; + } + /// /// True to amend the current pointed at by , false otherwise. /// @@ -22,5 +35,16 @@ public sealed class CommitOptions /// True to allow creation of an empty , false otherwise. /// public bool AllowEmptyCommit { get; set; } + + /// + /// True to prettify the message by stripping leading and trailing empty lines, trailing whitespace, and collapsing consecutive empty lines, false otherwise. + /// + public bool PrettifyMessage { get; set; } + + /// + /// The starting line char used to identify commentaries in the Commit message during the prettifying of the Commit message. If set (usually to '#'), all lines starting with this char will be removed from the message before the Commit is done. + /// This property will only be considered when PrettifyMessage is set to true. + /// + public char? CommentaryChar { get; set; } } } diff --git a/LibGit2Sharp/Core/HistoryRewriter.cs b/LibGit2Sharp/Core/HistoryRewriter.cs index 3931f9cba..916ef7d97 100644 --- a/LibGit2Sharp/Core/HistoryRewriter.cs +++ b/LibGit2Sharp/Core/HistoryRewriter.cs @@ -234,9 +234,9 @@ private void RewriteCommit(Commit commit) var newCommit = repo.ObjectDatabase.CreateCommit(newHeader.Author, newHeader.Committer, newHeader.Message, - true, newTree, - mappedNewParents); + mappedNewParents, + true); // Record the rewrite objectMap[commit] = newCommit; diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index ed6a39b24..e6b3940b2 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -1070,17 +1070,23 @@ public static void git_merge_head_free(IntPtr handle) #region git_message_ - public static string git_message_prettify(string message) + public static string git_message_prettify(string message, char? commentChar) { if (string.IsNullOrEmpty(message)) { return string.Empty; } + int comment = commentChar.GetValueOrDefault(); + if (comment > sbyte.MaxValue) + { + throw new InvalidOperationException("Only single byte characters are allowed as commentary characters in a message (eg. '#')."); + } + using (ThreadAffinity()) using (var buf = new GitBuf()) { - int res= NativeMethods.git_message_prettify(buf, message, false, (sbyte)'#'); + int res = NativeMethods.git_message_prettify(buf, message, false, (sbyte)comment); Ensure.Int32Result(res); return LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty; diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index b9412ddf4..3d36b9c26 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -259,7 +259,32 @@ public virtual Tree CreateTree(TreeDefinition treeDefinition) /// The of the to be created. /// The parents of the to be created. /// The created . + [Obsolete("This will be removed in future relases. Use the overload CreateCommit(Signature, Signature, string, Tree, IEnumerable, bool, char).")] public virtual Commit CreateCommit(Signature author, Signature committer, string message, bool prettifyMessage, Tree tree, IEnumerable parents) + { + return CreateCommit(author, committer, message, tree, parents, prettifyMessage); + } + + /// + /// Inserts a into the object database, referencing an existing . + /// + /// Prettifing the message includes: + /// * Removing empty lines from the beginning and end. + /// * Removing trailing spaces from every line. + /// * Turning multiple consecutive empty lines between paragraphs into just one empty line. + /// * Ensuring the commit message ends with a newline. + /// * Removing every line starting with "#". + /// + /// + /// The of who made the change. + /// The of who added the change to the repository. + /// The description of why a change was made to the repository. + /// The of the to be created. + /// The parents of the to be created. + /// True to prettify the message, or false to leave it as is. + /// Character that lines start with to be stripped if prettifyMessage is true. + /// The created . + public virtual Commit CreateCommit(Signature author, Signature committer, string message, Tree tree, IEnumerable parents, bool prettifyMessage, char? commentChar = null) { Ensure.ArgumentNotNull(message, "message"); Ensure.ArgumentDoesNotContainZeroByte(message, "message"); @@ -270,7 +295,7 @@ public virtual Commit CreateCommit(Signature author, Signature committer, string if (prettifyMessage) { - message = Proxy.git_message_prettify(message); + message = Proxy.git_message_prettify(message, commentChar); } GitOid[] parentIds = parents.Select(p => p.Id.Oid).ToArray(); @@ -296,7 +321,7 @@ public virtual TagAnnotation CreateTagAnnotation(string name, GitObject target, Ensure.ArgumentDoesNotContainZeroByte(name, "name"); Ensure.ArgumentDoesNotContainZeroByte(message, "message"); - string prettifiedMessage = Proxy.git_message_prettify(message); + string prettifiedMessage = Proxy.git_message_prettify(message, null); ObjectId tagId = Proxy.git_tag_annotation_create(repo.Handle, name, target, tagger, prettifiedMessage); diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 3e56327f1..68d87e6f2 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -935,7 +935,7 @@ public Commit Commit(string message, Signature author, Signature committer, Comm } } - Commit result = ObjectDatabase.CreateCommit(author, committer, message, true, tree, parents); + Commit result = ObjectDatabase.CreateCommit(author, committer, message, tree, parents, options.PrettifyMessage, options.CommentaryChar); Proxy.git_repository_state_cleanup(handle); diff --git a/LibGit2Sharp/StashCollection.cs b/LibGit2Sharp/StashCollection.cs index 07fc33797..ffe137a5b 100644 --- a/LibGit2Sharp/StashCollection.cs +++ b/LibGit2Sharp/StashCollection.cs @@ -87,7 +87,7 @@ public virtual Stash Add(Signature stasher, string message = null, StashModifier { Ensure.ArgumentNotNull(stasher, "stasher"); - string prettifiedMessage = Proxy.git_message_prettify(string.IsNullOrEmpty(message) ? string.Empty : message); + string prettifiedMessage = Proxy.git_message_prettify(string.IsNullOrEmpty(message) ? string.Empty : message, null); ObjectId oid = Proxy.git_stash_save(repo.Handle, stasher, prettifiedMessage, options); diff --git a/LibGit2Sharp/TagCollection.cs b/LibGit2Sharp/TagCollection.cs index e1fd5b50d..04c426095 100644 --- a/LibGit2Sharp/TagCollection.cs +++ b/LibGit2Sharp/TagCollection.cs @@ -85,7 +85,7 @@ public virtual Tag Add(string name, GitObject target, Signature tagger, string m Ensure.ArgumentNotNull(tagger, "tagger"); Ensure.ArgumentNotNull(message, "message"); - string prettifiedMessage = Proxy.git_message_prettify(message); + string prettifiedMessage = Proxy.git_message_prettify(message, null); Proxy.git_tag_create(repo.Handle, name, target, tagger, prettifiedMessage, allowOverwrite);