commit: add --cleanup=scissors

Since 1a72cfd (commit -v: strip diffs and submodule shortlogs from the commit message - 2013-12-05) we have a less fragile way to cut out "git status" at the end of a commit message but it's only enabled for stripping submodule shortlogs. Add new cleanup option that reuses the same mechanism for the entire "git status" without accidentally removing lines starting with '#'. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Feb 17, 2014 at 19:15 UTC 75df1f434f8cfdac1c8eeacae259ab375c01385b
3 files changed +34 -4
Documentation/git-commit.txt
+7 -1
@@ -176,7 +176,7 @@ OPTIONS
176 --cleanup=<mode>::
177 This option determines how the supplied commit message should be
178 cleaned up before committing. The '<mode>' can be `strip`,
179 - `whitespace`, `verbatim`, or `default`.
179 + `whitespace`, `verbatim`, `scissors` or `default`.
180 +
181 --
182 strip::
@@ -186,6 +186,12 @@ whitespace::
186 Same as `strip` except #commentary is not removed.
187 verbatim::
188 Do not change the message at all.
189 +scissors::
190 + Same as `whitespace`, except that everything from (and
191 + including) the line
192 + "`# ------------------------ >8 ------------------------`"
193 + is truncated if the message is to be edited. "`#`" can be
194 + customized with core.commentChar.
195 default::
196 Same as `strip` if the message is to be edited.
197 Otherwise `whitespace`.
builtin/commit.c
+11 -3
@@ -113,6 +113,7 @@ static char *sign_commit;
113 static enum {
114 CLEANUP_SPACE,
115 CLEANUP_NONE,
116 + CLEANUP_SCISSORS,
117 CLEANUP_ALL
118 } cleanup_mode;
119 static const char *cleanup_arg;
@@ -754,7 +755,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
755 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
756 if (use_editor && include_status) {
757 char *ai_tmp, *ci_tmp;
757 - if (whence != FROM_COMMIT)
758 + if (whence != FROM_COMMIT) {
759 + if (cleanup_mode == CLEANUP_SCISSORS)
760 + wt_status_add_cut_line(s->fp);
761 status_printf_ln(s, GIT_COLOR_NORMAL,
762 whence == FROM_MERGE
763 ? _("\n"
@@ -770,6 +773,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
773 git_path(whence == FROM_MERGE
774 ? "MERGE_HEAD"
775 : "CHERRY_PICK_HEAD"));
776 + }
777
778 fprintf(s->fp, "\n");
779 if (cleanup_mode == CLEANUP_ALL)
@@ -777,6 +781,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
781 _("Please enter the commit message for your changes."
782 " Lines starting\nwith '%c' will be ignored, and an empty"
783 " message aborts the commit.\n"), comment_line_char);
784 + else if (cleanup_mode == CLEANUP_SCISSORS && whence == FROM_COMMIT)
785 + wt_status_add_cut_line(s->fp);
786 else /* CLEANUP_SPACE, that is. */
787 status_printf(s, GIT_COLOR_NORMAL,
788 _("Please enter the commit message for your changes."
@@ -1132,6 +1138,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
1138 cleanup_mode = CLEANUP_SPACE;
1139 else if (!strcmp(cleanup_arg, "strip"))
1140 cleanup_mode = CLEANUP_ALL;
1141 + else if (!strcmp(cleanup_arg, "scissors"))
1142 + cleanup_mode = use_editor ? CLEANUP_SCISSORS : CLEANUP_SPACE;
1143 else
1144 die(_("Invalid cleanup mode %s"), cleanup_arg);
1145
@@ -1600,8 +1608,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1608 die(_("could not read commit message: %s"), strerror(saved_errno));
1609 }
1610
1603 - /* Truncate the message just before the diff, if any. */
1604 - if (verbose)
1611 + if (verbose || /* Truncate the message just before the diff, if any. */
1612 + cleanup_mode == CLEANUP_SCISSORS)
1613 wt_status_truncate_message_at_cut_line(&sb);
1614
1615 if (cleanup_mode != CLEANUP_NONE)
t/t7502-commit.sh
+16
@@ -223,6 +223,22 @@ test_expect_success 'cleanup commit messages (whitespace option,-F)' '
223
224 '
225
226 +test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
227 +
228 + echo >>negative &&
229 + cat >text <<EOF &&
230 +
231 +# to be kept
232 +# ------------------------ >8 ------------------------
233 +to be removed
234 +EOF
235 + echo "# to be kept" >expect &&
236 + git commit --cleanup=scissors -e -F text -a &&
237 + git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
238 + test_cmp expect actual
239 +
240 +'
241 +
242 test_expect_success 'cleanup commit messages (strip option,-F)' '
243
244 echo >>negative &&