interpret-trailers: honor the cut line
authorBrian Malehorn <bmalehorn@gmail.com>
Tue, 16 May 2017 06:06:49 +0000 (23:06 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 May 2017 06:00:48 +0000 (15:00 +0900)
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>
builtin/commit.c
commit.c
t/t7513-interpret-trailers.sh
wt-status.c
wt-status.h
index 1d805f5da8abd1cfafc41c5c47fd048b2549fbf4..8d1cac0629e7079daeef57ac049c0c7d7549e6d0 100644 (file)
@@ -1735,7 +1735,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
        if (verbose || /* Truncate the message just before the diff, if any. */
            cleanup_mode == CLEANUP_SCISSORS)
-               wt_status_truncate_message_at_cut_line(&sb);
+               strbuf_setlen(&sb, wt_status_locate_end(sb.buf, sb.len));
 
        if (cleanup_mode != CLEANUP_NONE)
                strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
index 73c78c2b80c1a21e83f1942347a76fb76511a0d3..c14ddf191fbf047607699e562fd85c8046932efb 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -11,6 +11,7 @@
 #include "commit-slab.h"
 #include "prio-queue.h"
 #include "sha1-lookup.h"
+#include "wt-status.h"
 
 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
 
@@ -1648,10 +1649,9 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
 /*
  * Inspect the given string and determine the true "end" of the log message, in
  * order to find where to put a new Signed-off-by: line.  Ignored are
- * trailing comment lines and blank lines, and also the traditional
- * "Conflicts:" block that is not commented out, so that we can use
- * "git commit -s --amend" on an existing commit that forgot to remove
- * it.
+ * trailing comment lines and blank lines.  To support "git commit -s
+ * --amend" on an existing commit, we also ignore "Conflicts:".  To
+ * support "git commit -v", we truncate at cut lines.
  *
  * Returns the number of bytes from the tail to ignore, to be fed as
  * the second parameter to append_signoff().
@@ -1661,8 +1661,9 @@ int ignore_non_trailer(const char *buf, size_t len)
        int boc = 0;
        int bol = 0;
        int in_old_conflicts_block = 0;
+       size_t cutoff = wt_status_locate_end(buf, len);
 
-       while (bol < len) {
+       while (bol < cutoff) {
                const char *next_line = memchr(buf + bol, '\n', len - bol);
 
                if (!next_line)
@@ -1688,5 +1689,5 @@ int ignore_non_trailer(const char *buf, size_t len)
                }
                bol = next_line - buf;
        }
-       return boc ? len - boc : 0;
+       return boc ? len - boc : len - cutoff;
 }
index 4dd1d7c52085d25546f64692acd0b351a7dddd72..0c6f91c4338cce131b3d6066e590f177f7e739d1 100755 (executable)
@@ -1258,4 +1258,21 @@ test_expect_success 'with no command and no key' '
        test_cmp expected actual
 '
 
+test_expect_success 'with cut line' '
+       cat >expected <<-\EOF &&
+               my subject
+
+               review: Brian
+               sign: A U Thor <author@example.com>
+               # ------------------------ >8 ------------------------
+               ignore this
+       EOF
+       git interpret-trailers --trailer review:Brian >actual <<-\EOF &&
+               my subject
+               # ------------------------ >8 ------------------------
+               ignore this
+       EOF
+       test_cmp expected actual
+'
+
 test_done
index 03754849626d1bf117e3821fb7cdffcd594f694f..f4e198e5ca0c99ab97f859d99bda223ddaab68ba 100644 (file)
@@ -896,17 +896,18 @@ static void wt_longstatus_print_other(struct wt_status *s,
        status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
 }
 
-void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
+size_t wt_status_locate_end(const char *s, size_t len)
 {
        const char *p;
        struct strbuf pattern = STRBUF_INIT;
 
        strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
-       if (starts_with(buf->buf, pattern.buf + 1))
-               strbuf_setlen(buf, 0);
-       else if ((p = strstr(buf->buf, pattern.buf)))
-               strbuf_setlen(buf, p - buf->buf + 1);
+       if (starts_with(s, pattern.buf + 1))
+               len = 0;
+       else if ((p = strstr(s, pattern.buf)))
+               len = p - s + 1;
        strbuf_release(&pattern);
+       return len;
 }
 
 void wt_status_add_cut_line(FILE *fp)
index 6018c627b1e40e5c36d0c0c12eff1f262cb439b4..8a3864783b039d92454cfda1c2ef2457e737cddd 100644 (file)
@@ -112,7 +112,7 @@ struct wt_status_state {
        unsigned char cherry_pick_head_sha1[20];
 };
 
-void wt_status_truncate_message_at_cut_line(struct strbuf *);
+size_t wt_status_locate_end(const char *s, size_t len);
 void wt_status_add_cut_line(FILE *fp);
 void wt_status_prepare(struct wt_status *s);
 void wt_status_print(struct wt_status *s);