Skip to content

Commit 2f52a40

Browse files
committed
Commit: Introduce CommentaryChar and PrettifyMessage in CommitOptions
1 parent 94f769e commit 2f52a40

11 files changed

+76
-21
lines changed

LibGit2Sharp.Tests/CommitAncestorFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ private static Commit CreateOrphanedCommit(IRepository repo)
120120
random.Author,
121121
random.Committer,
122122
"This is a test commit created by 'CommitFixture.CannotFindCommonAncestorForCommmitsWithoutCommonAncestor'",
123-
false,
124123
random.Tree,
125-
Enumerable.Empty<Commit>());
124+
Enumerable.Empty<Commit>(),
125+
false);
126126

127127
return orphanedCommit;
128128
}

LibGit2Sharp.Tests/FilterBranchFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ public void CanRewriteAuthorOfCommits()
171171
public void CanRewriteAuthorOfCommitsOnlyBeingPointedAtByTags()
172172
{
173173
var commit = repo.ObjectDatabase.CreateCommit(
174-
Constants.Signature, Constants.Signature, "I'm a lonesome commit", false,
175-
repo.Head.Tip.Tree, Enumerable.Empty<Commit>());
174+
Constants.Signature, Constants.Signature, "I'm a lonesome commit",
175+
repo.Head.Tip.Tree, Enumerable.Empty<Commit>(), false);
176176

177177
repo.Tags.Add("so-lonely", commit);
178178

LibGit2Sharp.Tests/ObjectDatabaseFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ public void CanCreateATreeContainingAGitLinkFromAnUntrackedSubmoduleInTheWorking
336336
Assert.IsType<GitLink>(te.Target);
337337
Assert.Equal(objectId, te.Target.Id);
338338

339-
var commitWithSubmodule = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Submodule!", false,
340-
tree, new[] { repo.Head.Tip });
339+
var commitWithSubmodule = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Submodule!",
340+
tree, new[] { repo.Head.Tip }, false);
341341
repo.Reset(ResetMode.Soft, commitWithSubmodule);
342342

343343
var submodule = repo.Submodules[submodulePath];
@@ -373,7 +373,7 @@ public void CanCreateACommit()
373373

374374
Tree tree = repo.ObjectDatabase.CreateTree(td);
375375

376-
Commit commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Ü message", true, tree, new[] { repo.Head.Tip });
376+
Commit commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "Ü message", tree, new[] { repo.Head.Tip }, true);
377377

378378
Branch newHead = repo.Head;
379379

@@ -454,7 +454,7 @@ public void CreatingACommitWithMessageContainingZeroByteThrows(string message)
454454
using (var repo = new Repository(BareTestRepoPath))
455455
{
456456
Assert.Throws<ArgumentException>(() => repo.ObjectDatabase.CreateCommit(
457-
Constants.Signature, Constants.Signature, message, false, repo.Head.Tip.Tree, Enumerable.Empty<Commit>()));
457+
Constants.Signature, Constants.Signature, message, repo.Head.Tip.Tree, Enumerable.Empty<Commit>(), false));
458458
}
459459
}
460460

LibGit2Sharp.Tests/ReflogFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public void AppendingToReflogDependsOnCoreLogAllRefUpdatesSetting(bool isBare, b
158158

159159
var blob = repo.ObjectDatabase.CreateBlob(Stream.Null);
160160
var tree = repo.ObjectDatabase.CreateTree(new TreeDefinition().Add("yoink", blob, Mode.NonExecutableFile));
161-
var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink", false,
162-
tree, Enumerable.Empty<Commit>());
161+
var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink",
162+
tree, Enumerable.Empty<Commit>(), false);
163163

164164
var branch = repo.CreateBranch("yoink", commit);
165165
var log = repo.Refs.Log(branch.CanonicalName);
@@ -178,8 +178,8 @@ public void UnsignedMethodsWriteCorrectlyToTheReflog()
178178

179179
var blob = repo.ObjectDatabase.CreateBlob(Stream.Null);
180180
var tree = repo.ObjectDatabase.CreateTree(new TreeDefinition().Add("yoink", blob, Mode.NonExecutableFile));
181-
var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink", false,
182-
tree, Enumerable.Empty<Commit>());
181+
var commit = repo.ObjectDatabase.CreateCommit(Constants.Signature, Constants.Signature, "yoink",
182+
tree, Enumerable.Empty<Commit>(), false);
183183

184184
var direct = repo.Refs.Add("refs/heads/direct", commit.Id);
185185
AssertRefLogEntry(repo, direct.CanonicalName, direct.ResolveToDirectReference().Target.Id, null);

LibGit2Sharp/CommitOptions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ namespace LibGit2Sharp
1313
/// </summary>
1414
public sealed class CommitOptions
1515
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="CommitOptions"/> class.
18+
/// <para>
19+
/// Default behavior:
20+
/// The message is prettified.
21+
/// No automatic removal of comments is performed.
22+
/// </para>
23+
/// </summary>
24+
public CommitOptions()
25+
{
26+
PrettifyMessage = true;
27+
}
28+
1629
/// <summary>
1730
/// True to amend the current <see cref="Commit"/> pointed at by <see cref="Repository.Head"/>, false otherwise.
1831
/// </summary>
@@ -22,5 +35,16 @@ public sealed class CommitOptions
2235
/// True to allow creation of an empty <see cref="Commit"/>, false otherwise.
2336
/// </summary>
2437
public bool AllowEmptyCommit { get; set; }
38+
39+
/// <summary>
40+
/// True to prettify the message by stripping leading and trailing empty lines, trailing whitespace, and collapsing consecutive empty lines, false otherwise.
41+
/// </summary>
42+
public bool PrettifyMessage { get; set; }
43+
44+
/// <summary>
45+
/// 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.
46+
/// This property will only be considered when PrettifyMessage is set to true.
47+
/// </summary>
48+
public char? CommentaryChar { get; set; }
2549
}
2650
}

LibGit2Sharp/Core/HistoryRewriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ private void RewriteCommit(Commit commit)
234234
var newCommit = repo.ObjectDatabase.CreateCommit(newHeader.Author,
235235
newHeader.Committer,
236236
newHeader.Message,
237-
true,
238237
newTree,
239-
mappedNewParents);
238+
mappedNewParents,
239+
true);
240240

241241
// Record the rewrite
242242
objectMap[commit] = newCommit;

LibGit2Sharp/Core/Proxy.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,17 +1070,23 @@ public static void git_merge_head_free(IntPtr handle)
10701070

10711071
#region git_message_
10721072

1073-
public static string git_message_prettify(string message)
1073+
public static string git_message_prettify(string message, char? commentChar)
10741074
{
10751075
if (string.IsNullOrEmpty(message))
10761076
{
10771077
return string.Empty;
10781078
}
10791079

1080+
int comment = commentChar.GetValueOrDefault();
1081+
if (comment > sbyte.MaxValue)
1082+
{
1083+
throw new InvalidOperationException("Only single byte characters are allowed as commentary characters in a message (eg. '#').");
1084+
}
1085+
10801086
using (ThreadAffinity())
10811087
using (var buf = new GitBuf())
10821088
{
1083-
int res= NativeMethods.git_message_prettify(buf, message, false, (sbyte)'#');
1089+
int res = NativeMethods.git_message_prettify(buf, message, false, (sbyte)comment);
10841090
Ensure.Int32Result(res);
10851091

10861092
return LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty;

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,32 @@ public virtual Tree CreateTree(TreeDefinition treeDefinition)
259259
/// <param name="tree">The <see cref="Tree"/> of the <see cref="Commit"/> to be created.</param>
260260
/// <param name="parents">The parents of the <see cref="Commit"/> to be created.</param>
261261
/// <returns>The created <see cref="Commit"/>.</returns>
262+
[Obsolete("This will be removed in future relases. Use the overload CreateCommit(Signature, Signature, string, Tree, IEnumerable<Commit>, bool, char).")]
262263
public virtual Commit CreateCommit(Signature author, Signature committer, string message, bool prettifyMessage, Tree tree, IEnumerable<Commit> parents)
264+
{
265+
return CreateCommit(author, committer, message, tree, parents, prettifyMessage);
266+
}
267+
268+
/// <summary>
269+
/// Inserts a <see cref="Commit"/> into the object database, referencing an existing <see cref="Tree"/>.
270+
/// <para>
271+
/// Prettifing the message includes:
272+
/// * Removing empty lines from the beginning and end.
273+
/// * Removing trailing spaces from every line.
274+
/// * Turning multiple consecutive empty lines between paragraphs into just one empty line.
275+
/// * Ensuring the commit message ends with a newline.
276+
/// * Removing every line starting with "#".
277+
/// </para>
278+
/// </summary>
279+
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
280+
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
281+
/// <param name="message">The description of why a change was made to the repository.</param>
282+
/// <param name="tree">The <see cref="Tree"/> of the <see cref="Commit"/> to be created.</param>
283+
/// <param name="parents">The parents of the <see cref="Commit"/> to be created.</param>
284+
/// <param name="prettifyMessage">True to prettify the message, or false to leave it as is.</param>
285+
/// <param name="commentChar">Character that lines start with to be stripped if prettifyMessage is true.</param>
286+
/// <returns>The created <see cref="Commit"/>.</returns>
287+
public virtual Commit CreateCommit(Signature author, Signature committer, string message, Tree tree, IEnumerable<Commit> parents, bool prettifyMessage, char? commentChar = null)
263288
{
264289
Ensure.ArgumentNotNull(message, "message");
265290
Ensure.ArgumentDoesNotContainZeroByte(message, "message");
@@ -270,7 +295,7 @@ public virtual Commit CreateCommit(Signature author, Signature committer, string
270295

271296
if (prettifyMessage)
272297
{
273-
message = Proxy.git_message_prettify(message);
298+
message = Proxy.git_message_prettify(message, commentChar);
274299
}
275300
GitOid[] parentIds = parents.Select(p => p.Id.Oid).ToArray();
276301

@@ -296,7 +321,7 @@ public virtual TagAnnotation CreateTagAnnotation(string name, GitObject target,
296321
Ensure.ArgumentDoesNotContainZeroByte(name, "name");
297322
Ensure.ArgumentDoesNotContainZeroByte(message, "message");
298323

299-
string prettifiedMessage = Proxy.git_message_prettify(message);
324+
string prettifiedMessage = Proxy.git_message_prettify(message, null);
300325

301326
ObjectId tagId = Proxy.git_tag_annotation_create(repo.Handle, name, target, tagger, prettifiedMessage);
302327

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ public Commit Commit(string message, Signature author, Signature committer, Comm
935935
}
936936
}
937937

938-
Commit result = ObjectDatabase.CreateCommit(author, committer, message, true, tree, parents);
938+
Commit result = ObjectDatabase.CreateCommit(author, committer, message, tree, parents, options.PrettifyMessage, options.CommentaryChar);
939939

940940
Proxy.git_repository_state_cleanup(handle);
941941

LibGit2Sharp/StashCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public virtual Stash Add(Signature stasher, string message = null, StashModifier
8787
{
8888
Ensure.ArgumentNotNull(stasher, "stasher");
8989

90-
string prettifiedMessage = Proxy.git_message_prettify(string.IsNullOrEmpty(message) ? string.Empty : message);
90+
string prettifiedMessage = Proxy.git_message_prettify(string.IsNullOrEmpty(message) ? string.Empty : message, null);
9191

9292
ObjectId oid = Proxy.git_stash_save(repo.Handle, stasher, prettifiedMessage, options);
9393

LibGit2Sharp/TagCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public virtual Tag Add(string name, GitObject target, Signature tagger, string m
8585
Ensure.ArgumentNotNull(tagger, "tagger");
8686
Ensure.ArgumentNotNull(message, "message");
8787

88-
string prettifiedMessage = Proxy.git_message_prettify(message);
88+
string prettifiedMessage = Proxy.git_message_prettify(message, null);
8989

9090
Proxy.git_tag_create(repo.Handle, name, target, tagger, prettifiedMessage, allowOverwrite);
9191

0 commit comments

Comments
 (0)