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 <bmalehorn@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brian Malehorn committed May 15, 2017 at 23:06 UTC d76650b8d16c9e5e7b6ee94e6922a3b99be74746
5 files changed +32 -13
builtin/commit.c
+1 -1
@@ -1735,7 +1735,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1735
1736 if (verbose || /* Truncate the message just before the diff, if any. */
1737 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));
1739
1740 if (cleanup_mode != CLEANUP_NONE)
1741 strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
commit.c
+7 -6
@@ -11,6 +11,7 @@
11 #include "commit-slab.h"
12 #include "prio-queue.h"
13 #include "sha1-lookup.h"
14 +#include "wt-status.h"
15
16 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
17
@@ -1648,10 +1649,9 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
1649 /*
1650 * Inspect the given string and determine the true "end" of the log message, in
1651 * 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.
1655 *
1656 * Returns the number of bytes from the tail to ignore, to be fed as
1657 * the second parameter to append_signoff().
@@ -1661,8 +1661,9 @@ int ignore_non_trailer(const char *buf, size_t len)
1661 int boc = 0;
1662 int bol = 0;
1663 int in_old_conflicts_block = 0;
1664 + size_t cutoff = wt_status_locate_end(buf, len);
1665
1665 - while (bol < len) {
1666 + while (bol < cutoff) {
1667 const char *next_line = memchr(buf + bol, '\n', len - bol);
1668
1669 if (!next_line)
@@ -1688,5 +1689,5 @@ int ignore_non_trailer(const char *buf, size_t len)
1689 }
1690 bol = next_line - buf;
1691 }
1691 - return boc ? len - boc : 0;
1692 + return boc ? len - boc : len - cutoff;
1693 }
t/t7513-interpret-trailers.sh
+17
@@ -1258,4 +1258,21 @@ test_expect_success 'with no command and no key' '
1258 test_cmp expected actual
1259 '
1260
1261 +test_expect_success 'with cut line' '
1262 + cat >expected <<-\EOF &&
1263 + my subject
1264 +
1265 + review: Brian
1266 + sign: A U Thor <author@example.com>
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 +
1278 test_done
wt-status.c
+6 -5
@@ -896,17 +896,18 @@ conclude:
896 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
897 }
898
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)
900 {
901 const char *p;
902 struct strbuf pattern = STRBUF_INIT;
903
904 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;
909 strbuf_release(&pattern);
910 + return len;
911 }
912
913 void wt_status_add_cut_line(FILE *fp)
wt-status.h
+1 -1
@@ -112,7 +112,7 @@ struct wt_status_state {
112 unsigned char cherry_pick_head_sha1[20];
113 };
114
115 -void wt_status_truncate_message_at_cut_line(struct strbuf *);
115 +size_t wt_status_locate_end(const char *s, size_t len);
116 void wt_status_add_cut_line(FILE *fp);
117 void wt_status_prepare(struct wt_status *s);
118 void wt_status_print(struct wt_status *s);