Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit a118bee

Browse files
committed
Merge branch 'jl/commit-v-strip-marker' into maint
"git commit -v" appends the patch to the log message before editing, and then removes the patch when the editor returned control. However, the patch was not stripped correctly when the first modified path was a submodule. * jl/commit-v-strip-marker: commit -v: strip diffs and submodule shortlogs from the commit message
2 parents ac0835f + 1a72cfd commit a118bee

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

builtin/commit.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15051505
struct strbuf sb = STRBUF_INIT;
15061506
struct strbuf author_ident = STRBUF_INIT;
15071507
const char *index_file, *reflog_msg;
1508-
char *nl, *p;
1508+
char *nl;
15091509
unsigned char sha1[20];
15101510
struct ref_lock *ref_lock;
15111511
struct commit_list *parents = NULL, **pptr = &parents;
@@ -1601,11 +1601,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16011601
}
16021602

16031603
/* Truncate the message just before the diff, if any. */
1604-
if (verbose) {
1605-
p = strstr(sb.buf, "\ndiff --git ");
1606-
if (p != NULL)
1607-
strbuf_setlen(&sb, p - sb.buf + 1);
1608-
}
1604+
if (verbose)
1605+
wt_status_truncate_message_at_cut_line(&sb);
16091606

16101607
if (cleanup_mode != CLEANUP_NONE)
16111608
stripspace(&sb, cleanup_mode == CLEANUP_ALL);

t/t7507-commit-verbose.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,35 @@ test_expect_success 'diff in message is retained without -v' '
6565
check_message diff
6666
'
6767

68-
test_expect_failure 'diff in message is retained with -v' '
68+
test_expect_success 'diff in message is retained with -v' '
6969
git commit --amend -F diff -v &&
7070
check_message diff
7171
'
7272

73+
test_expect_success 'submodule log is stripped out too with -v' '
74+
git config diff.submodule log &&
75+
git submodule add ./. sub &&
76+
git commit -m "sub added" &&
77+
(
78+
cd sub &&
79+
echo "more" >>file &&
80+
git commit -a -m "submodule commit"
81+
) &&
82+
(
83+
GIT_EDITOR=cat &&
84+
export GIT_EDITOR &&
85+
test_must_fail git commit -a -v 2>err
86+
) &&
87+
test_i18ngrep "Aborting commit due to empty commit message." err
88+
'
89+
90+
test_expect_success 'verbose diff is stripped out with set core.commentChar' '
91+
(
92+
GIT_EDITOR=cat &&
93+
export GIT_EDITOR &&
94+
test_must_fail git -c core.commentchar=";" commit -a -v 2>err
95+
) &&
96+
test_i18ngrep "Aborting commit due to empty commit message." err
97+
'
98+
7399
test_done

wt-status.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include "column.h"
1717
#include "strbuf.h"
1818

19+
static char cut_line[] =
20+
"------------------------ >8 ------------------------\n";
21+
1922
static char default_wt_status_colors[][COLOR_MAXLEN] = {
2023
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
2124
GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
@@ -767,6 +770,18 @@ static void wt_status_print_other(struct wt_status *s,
767770
status_printf_ln(s, GIT_COLOR_NORMAL, "");
768771
}
769772

773+
void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
774+
{
775+
const char *p;
776+
struct strbuf pattern = STRBUF_INIT;
777+
778+
strbuf_addf(&pattern, "%c %s", comment_line_char, cut_line);
779+
p = strstr(buf->buf, pattern.buf);
780+
if (p && (p == buf->buf || p[-1] == '\n'))
781+
strbuf_setlen(buf, p - buf->buf);
782+
strbuf_release(&pattern);
783+
}
784+
770785
static void wt_status_print_verbose(struct wt_status *s)
771786
{
772787
struct rev_info rev;
@@ -787,10 +802,20 @@ static void wt_status_print_verbose(struct wt_status *s)
787802
* If we're not going to stdout, then we definitely don't
788803
* want color, since we are going to the commit message
789804
* file (and even the "auto" setting won't work, since it
790-
* will have checked isatty on stdout).
805+
* will have checked isatty on stdout). But we then do want
806+
* to insert the scissor line here to reliably remove the
807+
* diff before committing.
791808
*/
792-
if (s->fp != stdout)
809+
if (s->fp != stdout) {
810+
const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
811+
struct strbuf buf = STRBUF_INIT;
812+
793813
rev.diffopt.use_color = 0;
814+
fprintf(s->fp, "%c %s", comment_line_char, cut_line);
815+
strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
816+
fputs(buf.buf, s->fp);
817+
strbuf_release(&buf);
818+
}
794819
run_diff_index(&rev, 1);
795820
}
796821

wt-status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct wt_status_state {
9191
unsigned char cherry_pick_head_sha1[20];
9292
};
9393

94+
void wt_status_truncate_message_at_cut_line(struct strbuf *);
9495
void wt_status_prepare(struct wt_status *s);
9596
void wt_status_print(struct wt_status *s);
9697
void wt_status_collect(struct wt_status *s);

0 commit comments

Comments
 (0)