Skip to content

Commit 46c0f91

Browse files
committed
Merge branch 'nd/commit-editor-cleanup'
"git commit --cleanup=<mode>" learned a new mode, scissors. * nd/commit-editor-cleanup: commit: add --cleanup=scissors wt-status.c: move cut-line print code out to wt_status_add_cut_line wt-status.c: make cut_line[] const to shrink .data section a bit
2 parents d4c6e9f + 75df1f4 commit 46c0f91

5 files changed

Lines changed: 48 additions & 12 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;
@@ -755,7 +756,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
755756
int ident_shown = 0;
756757
int saved_color_setting;
757758
char *ai_tmp, *ci_tmp;
758-
if (whence != FROM_COMMIT)
759+
if (whence != FROM_COMMIT) {
760+
if (cleanup_mode == CLEANUP_SCISSORS)
761+
wt_status_add_cut_line(s->fp);
759762
status_printf_ln(s, GIT_COLOR_NORMAL,
760763
whence == FROM_MERGE
761764
? _("\n"
@@ -771,13 +774,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
771774
git_path(whence == FROM_MERGE
772775
? "MERGE_HEAD"
773776
: "CHERRY_PICK_HEAD"));
777+
}
774778

775779
fprintf(s->fp, "\n");
776780
if (cleanup_mode == CLEANUP_ALL)
777781
status_printf(s, GIT_COLOR_NORMAL,
778782
_("Please enter the commit message for your changes."
779783
" Lines starting\nwith '%c' will be ignored, and an empty"
780784
" message aborts the commit.\n"), comment_line_char);
785+
else if (cleanup_mode == CLEANUP_SCISSORS && whence == FROM_COMMIT)
786+
wt_status_add_cut_line(s->fp);
781787
else /* CLEANUP_SPACE, that is. */
782788
status_printf(s, GIT_COLOR_NORMAL,
783789
_("Please enter the commit message for your changes."
@@ -1133,6 +1139,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
11331139
cleanup_mode = CLEANUP_SPACE;
11341140
else if (!strcmp(cleanup_arg, "strip"))
11351141
cleanup_mode = CLEANUP_ALL;
1142+
else if (!strcmp(cleanup_arg, "scissors"))
1143+
cleanup_mode = use_editor ? CLEANUP_SCISSORS : CLEANUP_SPACE;
11361144
else
11371145
die(_("Invalid cleanup mode %s"), cleanup_arg);
11381146

@@ -1605,8 +1613,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16051613
die(_("could not read commit message: %s"), strerror(saved_errno));
16061614
}
16071615

1608-
/* Truncate the message just before the diff, if any. */
1609-
if (verbose)
1616+
if (verbose || /* Truncate the message just before the diff, if any. */
1617+
cleanup_mode == CLEANUP_SCISSORS)
16101618
wt_status_truncate_message_at_cut_line(&sb);
16111619

16121620
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 &&

wt-status.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "strbuf.h"
1818
#include "utf8.h"
1919

20-
static char cut_line[] =
20+
static const char cut_line[] =
2121
"------------------------ >8 ------------------------\n";
2222

2323
static char default_wt_status_colors[][COLOR_MAXLEN] = {
@@ -841,6 +841,17 @@ void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
841841
strbuf_release(&pattern);
842842
}
843843

844+
void wt_status_add_cut_line(FILE *fp)
845+
{
846+
const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
847+
struct strbuf buf = STRBUF_INIT;
848+
849+
fprintf(fp, "%c %s", comment_line_char, cut_line);
850+
strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
851+
fputs(buf.buf, fp);
852+
strbuf_release(&buf);
853+
}
854+
844855
static void wt_status_print_verbose(struct wt_status *s)
845856
{
846857
struct rev_info rev;
@@ -866,14 +877,8 @@ static void wt_status_print_verbose(struct wt_status *s)
866877
* diff before committing.
867878
*/
868879
if (s->fp != stdout) {
869-
const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
870-
struct strbuf buf = STRBUF_INIT;
871-
872880
rev.diffopt.use_color = 0;
873-
fprintf(s->fp, "%c %s", comment_line_char, cut_line);
874-
strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
875-
fputs(buf.buf, s->fp);
876-
strbuf_release(&buf);
881+
wt_status_add_cut_line(s->fp);
877882
}
878883
run_diff_index(&rev, 1);
879884
}

wt-status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct wt_status_state {
9292
};
9393

9494
void wt_status_truncate_message_at_cut_line(struct strbuf *);
95+
void wt_status_add_cut_line(FILE *fp);
9596
void wt_status_prepare(struct wt_status *s);
9697
void wt_status_print(struct wt_status *s);
9798
void wt_status_collect(struct wt_status *s);

0 commit comments

Comments
 (0)