Detect whether the s-o-b already exists in the commit footer and refrain
from adding a duplicate.

Update t3511 to test new behavior.

Signed-off-by: Brandon Casey <bca...@nvidia.com>
---


Hi Duy,

How about this patch on top of my series as a base for your patch to
unify the code paths that append signoff in format-patch, commit, and
sequencer?

-Brandon


 sequencer.c              | 28 ++++++++++++++++++----------
 t/t3511-cherry-pick-x.sh | 20 ++++++++++++++++++--
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index 7ad1163..546dacb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -42,13 +42,15 @@ static int is_cherry_pick_from_line(const char *buf, int 
len)
                !prefixcmp(buf, cherry_picked_prefix);
 }
 
-static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
+static int ends_rfc2822_footer(struct strbuf *sb, struct strbuf *sob,
+       int ignore_footer)
 {
        int hit = 0;
        int i, k;
        int len = sb->len - ignore_footer;
        int last_was_rfc2822 = 0;
        const char *buf = sb->buf;
+       int found_sob = 0;
 
        for (i = len - 1; i > 0; i--) {
                if (hit && buf[i] == '\n')
@@ -66,12 +68,15 @@ static int ends_rfc2822_footer(struct strbuf *sb, int 
ignore_footer)
 
                if (last_was_rfc2822 && (buf[i] == ' ' || buf[i] == '\t'))
                        continue;
+               if ((last_was_rfc2822 = is_rfc2822_line(buf+i, k-i)) &&
+                       sob && !found_sob &&
+                       !strncasecmp(buf+i, sob->buf, sob->len))
+                       found_sob = 1;
 
-               if (!((last_was_rfc2822 = is_rfc2822_line(buf+i, k-i)) ||
-                       is_cherry_pick_from_line(buf+i, k-i)))
+               if (!(last_was_rfc2822 || is_cherry_pick_from_line(buf+i, k-i)))
                        return 0;
        }
-       return 1;
+       return 1 + found_sob;
 }
 
 static void remove_sequencer_state(void)
@@ -547,7 +552,7 @@ static int do_pick_commit(struct commit *commit, struct 
replay_opts *opts)
                }
 
                if (opts->record_origin) {
-                       if (!ends_rfc2822_footer(&msgbuf, 0))
+                       if (!ends_rfc2822_footer(&msgbuf, NULL, 0))
                                strbuf_addch(&msgbuf, '\n');
                        strbuf_addstr(&msgbuf, cherry_picked_prefix);
                        strbuf_addstr(&msgbuf, 
sha1_to_hex(commit->object.sha1));
@@ -1077,6 +1082,7 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 void append_signoff(struct strbuf *msgbuf, int ignore_footer)
 {
        struct strbuf sob = STRBUF_INIT;
+       int has_footer = 0;
        int i;
 
        strbuf_addstr(&sob, sign_off_header);
@@ -1085,10 +1091,12 @@ void append_signoff(struct strbuf *msgbuf, int 
ignore_footer)
        strbuf_addch(&sob, '\n');
        for (i = msgbuf->len - 1 - ignore_footer; i > 0 && msgbuf->buf[i - 1] 
!= '\n'; i--)
                ; /* do nothing */
-       if (prefixcmp(msgbuf->buf + i, sob.buf)) {
-               if (!i || !ends_rfc2822_footer(msgbuf, ignore_footer))
-                       strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, 
"\n", 1);
-               strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, sob.buf, 
sob.len);
-       }
+       if (!i || !(has_footer =
+               ends_rfc2822_footer(msgbuf, &sob, ignore_footer)))
+                       strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
+                               "\n", 1);
+       if (has_footer != 2)
+               strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, sob.buf,
+                       sob.len);
        strbuf_release(&sob);
 }
diff --git a/t/t3511-cherry-pick-x.sh b/t/t3511-cherry-pick-x.sh
index af7a87c..a15b199 100755
--- a/t/t3511-cherry-pick-x.sh
+++ b/t/t3511-cherry-pick-x.sh
@@ -11,9 +11,10 @@ pristine_detach () {
        git clean -d -f -f -q -x
 }
 
-non_rfc2822_mesg='base with footer
+non_rfc2822_mesg="base with footer
 
-Commit message body is here.'
+Commit message body is here.
+Not an s-o-b Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
 
 rfc2822_mesg="$non_rfc2822_mesg
 
@@ -25,6 +26,9 @@ rfc2822_cherry_mesg="$rfc2822_mesg
 (cherry picked from commit da39a3ee5e6b4b0d3255bfef95601890afd80709)
 Tested-by: C.U. Thor <cut...@example.com>"
 
+rfc2822_cherry_sob_mesg="$rfc2822_cherry_mesg
+Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+Signed-off-by: C.U. Thor <cut...@example.com>"
 
 test_expect_success setup '
        git config advice.detachedhead false &&
@@ -36,6 +40,8 @@ test_expect_success setup '
        test_commit "$rfc2822_mesg" foo b rfc2822-base &&
        git reset --hard initial &&
        test_commit "$rfc2822_cherry_mesg" foo b rfc2822-cherry-base &&
+       git reset --hard initial &&
+       test_commit "$rfc2822_cherry_sob_mesg" foo b rfc2822-cherry-sob-base &&
        pristine_detach initial &&
        test_commit conflicting unrelated
 '
@@ -151,4 +157,14 @@ test_expect_success 'cherry-pick treats -x -s "(cherry 
picked from..." line as p
        test_cmp expect actual
 '
 
+test_expect_success 'cherry-pick -s detects committer s-o-b already exists' '
+       pristine_detach initial &&
+       git cherry-pick -s rfc2822-cherry-sob-base &&
+       cat <<-EOF >expect &&
+               $rfc2822_cherry_sob_mesg
+       EOF
+       git log -1 --pretty=format:%B >actual &&
+       test_cmp expect actual
+'
+
 test_done
-- 
1.8.0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to