Skip to content

Commit 75df1f4

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent fcef931 commit 75df1f4

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

Documentation/git-commit.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ OPTIONS
176176
--cleanup=<mode>::
177177
This option determines how the supplied commit message should be
178178
cleaned up before committing. The '<mode>' can be `strip`,
179-
`whitespace`, `verbatim`, or `default`.
179+
`whitespace`, `verbatim`, `scissors` or `default`.
180180
+
181181
--
182182
strip::
@@ -186,6 +186,12 @@ whitespace::
186186
Same as `strip` except #commentary is not removed.
187187
verbatim::
188188
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.
189195
default::
190196
Same as `strip` if the message is to be edited.
191197
Otherwise `whitespace`.

builtin/commit.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static char *sign_commit;
113113
static enum {
114114
CLEANUP_SPACE,
115115
CLEANUP_NONE,
116+
CLEANUP_SCISSORS,
116117
CLEANUP_ALL
117118
} cleanup_mode;
118119
static const char *cleanup_arg;
@@ -754,7 +755,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
754755
strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
755756
if (use_editor && include_status) {
756757
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);
758761
status_printf_ln(s, GIT_COLOR_NORMAL,
759762
whence == FROM_MERGE
760763
? _("\n"
@@ -770,13 +773,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
770773
git_path(whence == FROM_MERGE
771774
? "MERGE_HEAD"
772775
: "CHERRY_PICK_HEAD"));
776+
}
773777

774778
fprintf(s->fp, "\n");
775779
if (cleanup_mode == CLEANUP_ALL)
776780
status_printf(s, GIT_COLOR_NORMAL,
777781
_("Please enter the commit message for your changes."
778782
" Lines starting\nwith '%c' will be ignored, and an empty"
779783
" 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);
780786
else /* CLEANUP_SPACE, that is. */
781787
status_printf(s, GIT_COLOR_NORMAL,
782788
_("Please enter the commit message for your changes."
@@ -1132,6 +1138,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
11321138
cleanup_mode = CLEANUP_SPACE;
11331139
else if (!strcmp(cleanup_arg, "strip"))
11341140
cleanup_mode = CLEANUP_ALL;
1141+
else if (!strcmp(cleanup_arg, "scissors"))
1142+
cleanup_mode = use_editor ? CLEANUP_SCISSORS : CLEANUP_SPACE;
11351143
else
11361144
die(_("Invalid cleanup mode %s"), cleanup_arg);
11371145

@@ -1600,8 +1608,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16001608
die(_("could not read commit message: %s"), strerror(saved_errno));
16011609
}
16021610

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)
16051613
wt_status_truncate_message_at_cut_line(&sb);
16061614

16071615
if (cleanup_mode != CLEANUP_NONE)

t/t7502-commit.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ test_expect_success 'cleanup commit messages (whitespace option,-F)' '
223223
224224
'
225225

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+
226242
test_expect_success 'cleanup commit messages (strip option,-F)' '
227243
228244
echo >>negative &&

0 commit comments

Comments
 (0)