Skip to content

Commit 97d0c52

Browse files
committed
Merge branch 'ar/commit-cleanup'
* ar/commit-cleanup: Allow selection of different cleanup modes for commit messages builtin-commit: avoid double-negation in the code. builtin-commit: fix amending of the initial commit t7005: do not exit inside test.
2 parents d562509 + 5f06573 commit 97d0c52

File tree

4 files changed

+125
-16
lines changed

4 files changed

+125
-16
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: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@ 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

51-
static int no_edit, initial_commit, in_merge;
64+
static int use_editor = 1, initial_commit, in_merge;
5265
const char *only_include_assumed;
5366
struct strbuf message;
5467

@@ -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;
@@ -372,22 +387,22 @@ static int prepare_log_message(const char *index_file, const char *prefix)
372387

373388
strbuf_release(&sb);
374389

375-
if (no_edit) {
390+
if (!use_editor) {
376391
struct rev_info rev;
377-
unsigned char sha1[40];
392+
unsigned char sha1[20];
378393
const char *parent = "HEAD";
379394

380395
fclose(fp);
381396

382397
if (!active_nr && read_cache() < 0)
383398
die("Cannot read index");
384399

385-
if (get_sha1("HEAD", sha1) != 0)
386-
return !!active_nr;
387-
388400
if (amend)
389401
parent = "HEAD^1";
390402

403+
if (get_sha1(parent, sha1))
404+
return !!active_nr;
405+
391406
init_revisions(&rev, "");
392407
rev.abbrev = 0;
393408
setup_revisions(0, NULL, &rev, parent);
@@ -398,7 +413,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
398413
return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
399414
}
400415

401-
if (in_merge && !no_edit)
416+
if (in_merge)
402417
fprintf(fp,
403418
"#\n"
404419
"# It looks like you may be committing a MERGE.\n"
@@ -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;
@@ -513,9 +536,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
513536
argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
514537

515538
if (logfile || message.len || use_message)
516-
no_edit = 1;
539+
use_editor = 0;
517540
if (edit_flag)
518-
no_edit = 0;
541+
use_editor = 1;
519542

520543
if (get_sha1("HEAD", head_sha1))
521544
initial_commit = 1;
@@ -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.");
@@ -796,7 +829,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
796829

797830
/* Get the commit message and validate it */
798831
header_len = sb.len;
799-
if (!no_edit) {
832+
if (use_editor) {
800833
char index[PATH_MAX];
801834
const char *env[2] = { index, NULL };
802835
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
@@ -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/t7005-editor.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test_expect_success 'dumb should error out when falling back on vi' '
3737
if git commit --amend
3838
then
3939
echo "Oops?"
40-
exit 1
40+
false
4141
else
4242
: happy
4343
fi

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)