@matheusbsbs / matheus-bagliano / commits / 5f06573743
Fork 0

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>

Alex Riesen committed Dec 22, 2007 at 19:46 UTC 5f0657374344a5d8cf5ce5a9920a08c0be422194
3 files changed +114 -5
Documentation/git-commit.txt
+11 -1
@@ -11,7 +11,7 @@ SYNOPSIS
11 'git-commit' [-a | --interactive] [-s] [-v] [-u]
12 [(-c | -C) <commit> | -F <file> | -m <msg> | --amend]
13 [--allow-empty] [--no-verify] [-e] [--author <author>]
14 - [--] [[-i | -o ]<file>...]
14 + [--cleanup=<mode>] [--] [[-i | -o ]<file>...]
15
16 DESCRIPTION
17 -----------
@@ -95,6 +95,16 @@ OPTIONS
95 from making such a commit. This option bypasses the safety, and
96 is primarily for use by foreign scm interface scripts.
97
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 +
108 -e|--edit::
109 The message taken from file with `-F`, command line with
110 `-m`, and from file with `-C` are usually used as the
builtin-commit.c
+38 -4
@@ -47,6 +47,19 @@ static char *logfile, *force_author, *template_file;
47 static char *edit_message, *use_message;
48 static int all, edit_flag, also, interactive, only, amend, signoff;
49 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;
63
64 static int use_editor = 1, initial_commit, in_merge;
65 const char *only_include_assumed;
@@ -88,6 +101,7 @@ static struct option builtin_commit_options[] = {
101 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
102 OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
103 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"),
105
106 OPT_END()
107 };
@@ -346,7 +360,8 @@ static int prepare_log_message(const char *index_file, const char *prefix)
360 if (fp == NULL)
361 die("could not open %s", git_path(commit_editmsg));
362
349 - stripspace(&sb, 0);
363 + if (cleanup_mode != CLEANUP_NONE)
364 + stripspace(&sb, 0);
365
366 if (signoff) {
367 struct strbuf sob;
@@ -411,7 +426,12 @@ static int prepare_log_message(const char *index_file, const char *prefix)
426 fprintf(fp,
427 "\n"
428 "# 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");
435 if (only_include_assumed)
436 fprintf(fp, "# %s\n", only_include_assumed);
437
@@ -435,10 +455,13 @@ static int message_is_empty(struct strbuf *sb, int start)
455 const char *nl;
456 int eol, i;
457
458 + if (cleanup_mode == CLEANUP_NONE && sb->len)
459 + return 0;
460 +
461 /* See if the template is just a prefix of the message. */
462 strbuf_init(&tmpl, 0);
463 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
441 - stripspace(&tmpl, 1);
464 + stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
465 if (start + tmpl.len <= sb->len &&
466 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
467 start += tmpl.len;
@@ -591,6 +614,16 @@ static int parse_and_validate_options(int argc, const char *argv[],
614 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
615 also = 0;
616 }
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);
627
628 if (all && argc > 0)
629 die("Paths with -a does not make sense.");
@@ -817,7 +850,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
850 if (p != NULL)
851 strbuf_setlen(&sb, p - sb.buf + 1);
852
820 - stripspace(&sb, 1);
853 + if (cleanup_mode != CLEANUP_NONE)
854 + stripspace(&sb, cleanup_mode == CLEANUP_ALL);
855 if (sb.len < header_len || message_is_empty(&sb, header_len)) {
856 rollback_index_files();
857 die("no commit message? aborting commit.");
t/t7502-commit.sh
+65
@@ -89,4 +89,69 @@ test_expect_success 'verbose' '
89
90 '
91
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 +
157 test_done