Skip to content

Commit 146ba84

Browse files
committed
Added an option to choose if commit message is prettified or not
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 "#". Note that this has nothing to do with the core.autocrlf option.
1 parent 61e8add commit 146ba84

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

LibGit2Sharp/Core/HistoryRewriter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,11 @@ private void RewriteCommit(Commit commit)
222222
return;
223223
}
224224

225-
var newCommit = repo.ObjectDatabase.CreateCommit(newHeader.Message, newHeader.Author,
226-
newHeader.Committer, newTree,
225+
var newCommit = repo.ObjectDatabase.CreateCommit(newHeader.Author,
226+
newHeader.Committer,
227+
newHeader.Message,
228+
true,
229+
newTree,
227230
mappedNewParents);
228231

229232
// Record the rewrite

LibGit2Sharp/Core/Proxy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public static ObjectId git_commit_create(
316316
string referenceName,
317317
Signature author,
318318
Signature committer,
319-
string prettifiedMessage,
319+
string message,
320320
Tree tree,
321321
GitOid[] parentIds)
322322
{
@@ -331,7 +331,7 @@ public static ObjectId git_commit_create(
331331

332332
int res = NativeMethods.git_commit_create_from_oids(
333333
out commitOid, repo, referenceName, authorHandle,
334-
committerHandle, null, prettifiedMessage,
334+
committerHandle, null, message,
335335
ref treeOid, parentPtrs.Count, parentPtrs.ToArray());
336336

337337
Ensure.ZeroResult(res);

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,36 @@ public virtual Tree CreateTree(TreeDefinition treeDefinition)
191191
/// <param name="tree">The <see cref="Tree"/> of the <see cref="Commit"/> to be created.</param>
192192
/// <param name="parents">The parents of the <see cref="Commit"/> to be created.</param>
193193
/// <returns>The created <see cref="Commit"/>.</returns>
194+
[Obsolete]
194195
public virtual Commit CreateCommit(string message, Signature author, Signature committer, Tree tree, IEnumerable<Commit> parents)
195196
{
196-
return CreateCommit(message, author, committer, tree, parents, null);
197+
return CreateCommit(author, committer, message, true, tree, parents, null);
197198
}
198199

199-
internal Commit CreateCommit(string message, Signature author, Signature committer, Tree tree, IEnumerable<Commit> parents, string referenceName)
200+
/// <summary>
201+
/// Inserts a <see cref="Commit"/> into the object database, referencing an existing <see cref="Tree"/>.
202+
/// </summary>
203+
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
204+
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
205+
/// <param name="message">The description of why a change was made to the repository.</param>
206+
/// <param name="prettifyMessage">True to prettify the message, or false to leave it as is</param>
207+
/// <param name="tree">The <see cref="Tree"/> of the <see cref="Commit"/> to be created.</param>
208+
/// <param name="parents">The parents of the <see cref="Commit"/> to be created.</param>
209+
/// <returns>The created <see cref="Commit"/>.</returns>
210+
/// <para>
211+
/// Prettifing the message includes:
212+
/// * Removing empty lines from the beginning and end.
213+
/// * Removing trailing spaces from every line.
214+
/// * Turning multiple consecutive empty lines between paragraphs into just one empty line.
215+
/// * Ensuring the commit message ends with a newline.
216+
/// * Removing every line starting with "#".
217+
/// </para>
218+
public virtual Commit CreateCommit(Signature author, Signature committer, string message, bool prettifyMessage, Tree tree, IEnumerable<Commit> parents)
219+
{
220+
return CreateCommit(author, committer, message, prettifyMessage, tree, parents, null);
221+
}
222+
223+
internal Commit CreateCommit(Signature author, Signature committer, string message, bool prettifyMessage, Tree tree, IEnumerable<Commit> parents, string referenceName)
200224
{
201225
Ensure.ArgumentNotNull(message, "message");
202226
Ensure.ArgumentDoesNotContainZeroByte(message, "message");
@@ -205,10 +229,13 @@ internal Commit CreateCommit(string message, Signature author, Signature committ
205229
Ensure.ArgumentNotNull(tree, "tree");
206230
Ensure.ArgumentNotNull(parents, "parents");
207231

208-
string prettifiedMessage = Proxy.git_message_prettify(message);
232+
if (prettifyMessage)
233+
{
234+
message = Proxy.git_message_prettify(message);
235+
}
209236
GitOid[] parentIds = parents.Select(p => p.Id.Oid).ToArray();
210237

211-
ObjectId commitId = Proxy.git_commit_create(repo.Handle, referenceName, author, committer, prettifiedMessage, tree, parentIds);
238+
ObjectId commitId = Proxy.git_commit_create(repo.Handle, referenceName, author, committer, message, tree, parentIds);
212239

213240
return repo.Lookup<Commit>(commitId);
214241
}

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ public Commit Commit(string message, Signature author, Signature committer, bool
880880

881881
var parents = RetrieveParentsOfTheCommitBeingCreated(amendPreviousCommit);
882882

883-
Commit result = ObjectDatabase.CreateCommit(message, author, committer, tree, parents, "HEAD");
883+
Commit result = ObjectDatabase.CreateCommit(author, committer, message, true, tree, parents, "HEAD");
884884

885885
Proxy.git_repository_state_cleanup(handle);
886886

LibGit2Sharp/libgit2sharp_hash.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
unknown
1+
61e8add29c12bd4a56b84af6ddac318e1778ea50

0 commit comments

Comments
 (0)