Skip to content

Commit d76650b

Browse files
bmalehorngitster
authored andcommitted
interpret-trailers: honor the cut line
If a commit message is edited with the "verbose" option, the buffer will have a cut line and diff after the log message, like so: my subject # ------------------------ >8 ------------------------ # Do not touch the line above. # Everything below will be removed. diff --git a/foo.txt b/foo.txt index 5716ca5..7601807 100644 --- a/foo.txt +++ b/foo.txt @@ -1 +1 @@ -bar +baz "git interpret-trailers" is unaware of the cut line, and assumes the trailer block would be at the end of the whole thing. This can easily be seen with: $ GIT_EDITOR='git interpret-trailers --in-place --trailer Acked-by:me' \ git commit --amend -v Teach "git interpret-trailers" to notice the cut-line and ignore the remainder of the input when looking for a place to add new trailer block. This makes it consistent with how "git commit -v -s" inserts a new Signed-off-by: line. This can be done by the same logic as the existing helper function, wt_status_truncate_message_at_cut_line(), uses, but it wants the caller to pass a strbuf to it. Because the function ignore_non_trailer() used by the command takes a <pointer, length> pair, not a strbuf, steal the logic from wt_status_truncate_message_at_cut_line() to create a new wt_status_locate_end() helper function that takes <pointer, length> pair, and make ignore_non_trailer() call it to help "interpret-trailers". Since there is only one caller of wt_status_truncate_message_at_cut_line() in cmd_commit(), rewrite it to call wt_status_locate_end() helper instead and remove the old helper that no longer has any caller. Signed-off-by: Brian Malehorn <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b06d364 commit d76650b

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17351735

17361736
if (verbose || /* Truncate the message just before the diff, if any. */
17371737
cleanup_mode == CLEANUP_SCISSORS)
1738-
wt_status_truncate_message_at_cut_line(&sb);
1738+
strbuf_setlen(&sb, wt_status_locate_end(sb.buf, sb.len));
17391739

17401740
if (cleanup_mode != CLEANUP_NONE)
17411741
strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);

commit.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "commit-slab.h"
1212
#include "prio-queue.h"
1313
#include "sha1-lookup.h"
14+
#include "wt-status.h"
1415

1516
static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
1617

@@ -1648,10 +1649,9 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
16481649
/*
16491650
* Inspect the given string and determine the true "end" of the log message, in
16501651
* order to find where to put a new Signed-off-by: line. Ignored are
1651-
* trailing comment lines and blank lines, and also the traditional
1652-
* "Conflicts:" block that is not commented out, so that we can use
1653-
* "git commit -s --amend" on an existing commit that forgot to remove
1654-
* it.
1652+
* trailing comment lines and blank lines. To support "git commit -s
1653+
* --amend" on an existing commit, we also ignore "Conflicts:". To
1654+
* support "git commit -v", we truncate at cut lines.
16551655
*
16561656
* Returns the number of bytes from the tail to ignore, to be fed as
16571657
* the second parameter to append_signoff().
@@ -1661,8 +1661,9 @@ int ignore_non_trailer(const char *buf, size_t len)
16611661
int boc = 0;
16621662
int bol = 0;
16631663
int in_old_conflicts_block = 0;
1664+
size_t cutoff = wt_status_locate_end(buf, len);
16641665

1665-
while (bol < len) {
1666+
while (bol < cutoff) {
16661667
const char *next_line = memchr(buf + bol, '\n', len - bol);
16671668

16681669
if (!next_line)
@@ -1688,5 +1689,5 @@ int ignore_non_trailer(const char *buf, size_t len)
16881689
}
16891690
bol = next_line - buf;
16901691
}
1691-
return boc ? len - boc : 0;
1692+
return boc ? len - boc : len - cutoff;
16921693
}

t/t7513-interpret-trailers.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,4 +1258,21 @@ test_expect_success 'with no command and no key' '
12581258
test_cmp expected actual
12591259
'
12601260

1261+
test_expect_success 'with cut line' '
1262+
cat >expected <<-\EOF &&
1263+
my subject
1264+
1265+
review: Brian
1266+
sign: A U Thor <[email protected]>
1267+
# ------------------------ >8 ------------------------
1268+
ignore this
1269+
EOF
1270+
git interpret-trailers --trailer review:Brian >actual <<-\EOF &&
1271+
my subject
1272+
# ------------------------ >8 ------------------------
1273+
ignore this
1274+
EOF
1275+
test_cmp expected actual
1276+
'
1277+
12611278
test_done

wt-status.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,18 @@ static void wt_longstatus_print_other(struct wt_status *s,
896896
status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
897897
}
898898

899-
void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
899+
size_t wt_status_locate_end(const char *s, size_t len)
900900
{
901901
const char *p;
902902
struct strbuf pattern = STRBUF_INIT;
903903

904904
strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
905-
if (starts_with(buf->buf, pattern.buf + 1))
906-
strbuf_setlen(buf, 0);
907-
else if ((p = strstr(buf->buf, pattern.buf)))
908-
strbuf_setlen(buf, p - buf->buf + 1);
905+
if (starts_with(s, pattern.buf + 1))
906+
len = 0;
907+
else if ((p = strstr(s, pattern.buf)))
908+
len = p - s + 1;
909909
strbuf_release(&pattern);
910+
return len;
910911
}
911912

912913
void wt_status_add_cut_line(FILE *fp)

wt-status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct wt_status_state {
112112
unsigned char cherry_pick_head_sha1[20];
113113
};
114114

115-
void wt_status_truncate_message_at_cut_line(struct strbuf *);
115+
size_t wt_status_locate_end(const char *s, size_t len);
116116
void wt_status_add_cut_line(FILE *fp);
117117
void wt_status_prepare(struct wt_status *s);
118118
void wt_status_print(struct wt_status *s);

0 commit comments

Comments
 (0)