Skip to content

Commit 5f06573

Browse files
raalkmlgitster
authored andcommitted
Allow selection of different cleanup modes for commit messages
Although we traditionally stripped away excess blank lines, trailing whitespaces and lines that begin with "#" from the commit log message, sometimes the message just has to be the way user wants it. For instance, a commit message template can contain lines that begin with "#", the message must be kept as close to its original source as possible if you are converting from a foreign SCM, or maybe the message has a shell script including its comments for future reference. The cleanup modes are default, verbatim, whitespace and strip. The default mode depends on if the message is being edited and will either strip whitespace and comments (if editor active) or just strip the whitespace (for where the message is given explicitely). Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4803466 commit 5f06573

File tree

3 files changed

+114
-5
lines changed

3 files changed

+114
-5
lines changed

Documentation/git-commit.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SYNOPSIS
1111
'git-commit' [-a | --interactive] [-s] [-v] [-u]
1212
[(-c | -C) <commit> | -F <file> | -m <msg> | --amend]
1313
[--allow-empty] [--no-verify] [-e] [--author <author>]
14-
[--] [[-i | -o ]<file>...]
14+
[--cleanup=<mode>] [--] [[-i | -o ]<file>...]
1515

1616
DESCRIPTION
1717
-----------
@@ -95,6 +95,16 @@ OPTIONS
9595
from making such a commit. This option bypasses the safety, and
9696
is primarily for use by foreign scm interface scripts.
9797

98+
--cleanup=<mode>::
99+
This option sets how the commit message is cleaned up.
100+
The '<mode>' can be one of 'verbatim', 'whitespace', 'strip',
101+
and 'default'. The 'default' mode will strip leading and
102+
trailing empty lines and #commentary from the commit message
103+
only if the message is to be edited. Otherwise only whitespace
104+
removed. The 'verbatim' mode does not change message at all,
105+
'whitespace' removes just leading/trailing whitespace lines
106+
and 'strip' removes both whitespace and commentary.
107+
98108
-e|--edit::
99109
The message taken from file with `-F`, command line with
100110
`-m`, and from file with `-C` are usually used as the

builtin-commit.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ static char *logfile, *force_author, *template_file;
4747
static char *edit_message, *use_message;
4848
static int all, edit_flag, also, interactive, only, amend, signoff;
4949
static int quiet, verbose, untracked_files, no_verify, allow_empty;
50+
/*
51+
* The default commit message cleanup mode will remove the lines
52+
* beginning with # (shell comments) and leading and trailing
53+
* whitespaces (empty lines or containing only whitespaces)
54+
* if editor is used, and only the whitespaces if the message
55+
* is specified explicitly.
56+
*/
57+
static enum {
58+
CLEANUP_SPACE,
59+
CLEANUP_NONE,
60+
CLEANUP_ALL,
61+
} cleanup_mode;
62+
static char *cleanup_arg;
5063

5164
static int use_editor = 1, initial_commit, in_merge;
5265
const char *only_include_assumed;
@@ -88,6 +101,7 @@ static struct option builtin_commit_options[] = {
88101
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
89102
OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
90103
OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
104+
OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
91105

92106
OPT_END()
93107
};
@@ -346,7 +360,8 @@ static int prepare_log_message(const char *index_file, const char *prefix)
346360
if (fp == NULL)
347361
die("could not open %s", git_path(commit_editmsg));
348362

349-
stripspace(&sb, 0);
363+
if (cleanup_mode != CLEANUP_NONE)
364+
stripspace(&sb, 0);
350365

351366
if (signoff) {
352367
struct strbuf sob;
@@ -411,7 +426,12 @@ static int prepare_log_message(const char *index_file, const char *prefix)
411426
fprintf(fp,
412427
"\n"
413428
"# Please enter the commit message for your changes.\n"
414-
"# (Comment lines starting with '#' will not be included)\n");
429+
"# (Comment lines starting with '#' will ");
430+
if (cleanup_mode == CLEANUP_ALL)
431+
fprintf(fp, "not be included)\n");
432+
else /* CLEANUP_SPACE, that is. */
433+
fprintf(fp, "be kept.\n"
434+
"# You can remove them yourself if you want to)\n");
415435
if (only_include_assumed)
416436
fprintf(fp, "# %s\n", only_include_assumed);
417437

@@ -435,10 +455,13 @@ static int message_is_empty(struct strbuf *sb, int start)
435455
const char *nl;
436456
int eol, i;
437457

458+
if (cleanup_mode == CLEANUP_NONE && sb->len)
459+
return 0;
460+
438461
/* See if the template is just a prefix of the message. */
439462
strbuf_init(&tmpl, 0);
440463
if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
441-
stripspace(&tmpl, 1);
464+
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
442465
if (start + tmpl.len <= sb->len &&
443466
memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
444467
start += tmpl.len;
@@ -591,6 +614,16 @@ static int parse_and_validate_options(int argc, const char *argv[],
591614
only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
592615
also = 0;
593616
}
617+
if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
618+
cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
619+
else if (!strcmp(cleanup_arg, "verbatim"))
620+
cleanup_mode = CLEANUP_NONE;
621+
else if (!strcmp(cleanup_arg, "whitespace"))
622+
cleanup_mode = CLEANUP_SPACE;
623+
else if (!strcmp(cleanup_arg, "strip"))
624+
cleanup_mode = CLEANUP_ALL;
625+
else
626+
die("Invalid cleanup mode %s", cleanup_arg);
594627

595628
if (all && argc > 0)
596629
die("Paths with -a does not make sense.");
@@ -817,7 +850,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
817850
if (p != NULL)
818851
strbuf_setlen(&sb, p - sb.buf + 1);
819852

820-
stripspace(&sb, 1);
853+
if (cleanup_mode != CLEANUP_NONE)
854+
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
821855
if (sb.len < header_len || message_is_empty(&sb, header_len)) {
822856
rollback_index_files();
823857
die("no commit message? aborting commit.");

t/t7502-commit.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,69 @@ test_expect_success 'verbose' '
8989
9090
'
9191

92+
test_expect_success 'cleanup commit messages (verbatim,-t)' '
93+
94+
echo >>negative &&
95+
{ echo;echo "# text";echo; } >expect &&
96+
git commit --cleanup=verbatim -t expect -a &&
97+
git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
98+
diff -u expect actual
99+
100+
'
101+
102+
test_expect_success 'cleanup commit messages (verbatim,-F)' '
103+
104+
echo >>negative &&
105+
git commit --cleanup=verbatim -F expect -a &&
106+
git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
107+
diff -u expect actual
108+
109+
'
110+
111+
test_expect_success 'cleanup commit messages (verbatim,-m)' '
112+
113+
echo >>negative &&
114+
git commit --cleanup=verbatim -m "$(cat expect)" -a &&
115+
git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
116+
diff -u expect actual
117+
118+
'
119+
120+
test_expect_success 'cleanup commit messages (whitespace,-F)' '
121+
122+
echo >>negative &&
123+
{ echo;echo "# text";echo; } >text &&
124+
echo "# text" >expect &&
125+
git commit --cleanup=whitespace -F text -a &&
126+
git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
127+
diff -u expect actual
128+
129+
'
130+
131+
test_expect_success 'cleanup commit messages (strip,-F)' '
132+
133+
echo >>negative &&
134+
{ echo;echo "# text";echo sample;echo; } >text &&
135+
echo sample >expect &&
136+
git commit --cleanup=strip -F text -a &&
137+
git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
138+
diff -u expect actual
139+
140+
'
141+
142+
echo "sample
143+
144+
# Please enter the commit message for your changes.
145+
# (Comment lines starting with '#' will not be included)" >expect
146+
147+
test_expect_success 'cleanup commit messages (strip,-F,-e)' '
148+
149+
echo >>negative &&
150+
{ echo;echo sample;echo; } >text &&
151+
git commit -e -F text -a &&
152+
head -n 4 .git/COMMIT_EDITMSG >actual &&
153+
diff -u expect actual
154+
155+
'
156+
92157
test_done

0 commit comments

Comments
 (0)