sequencer.c: teach append_signoff how to detect duplicate s-o-b

Teach append_signoff how to detect a duplicate s-o-b in the commit footer. This is in preparation to unify the append_signoff implementations in log-tree.c and sequencer.c. Fixes test in t3511. Signed-off-by: Brandon Casey <bcasey@nvidia.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Feb 12, 2013 at 02:17 UTC bab4d1097c8be7d688a53b992232063dbf300fd4
4 files changed +50 -17
builtin/commit.c
+1 -1
@@ -700,7 +700,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
700 previous = eol;
701 }
702
703 - append_signoff(&sb, ignore_footer);
703 + append_signoff(&sb, ignore_footer, 0);
704 }
705
706 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
sequencer.c
+45 -14
@@ -44,12 +44,20 @@ static int is_cherry_picked_from_line(const char *buf, int len)
44 !prefixcmp(buf, cherry_picked_prefix) && buf[len - 1] == ')';
45 }
46
47 -static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
47 +/*
48 + * Returns 0 for non-conforming footer
49 + * Returns 1 for conforming footer
50 + * Returns 2 when sob exists within conforming footer
51 + * Returns 3 when sob exists within conforming footer as last entry
52 + */
53 +static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
54 + int ignore_footer)
55 {
56 char prev;
57 int i, k;
58 int len = sb->len - ignore_footer;
59 const char *buf = sb->buf;
60 + int found_sob = 0;
61
62 /* footer must end with newline */
63 if (!len || buf[len - 1] != '\n')
@@ -72,14 +80,25 @@ static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
80 i++;
81
82 for (; i < len; i = k) {
83 + int found_rfc2822;
84 +
85 for (k = i; k < len && buf[k] != '\n'; k++)
86 ; /* do nothing */
87 k++;
88
79 - if (!is_rfc2822_line(buf + i, k - i - 1) &&
80 - !is_cherry_picked_from_line(buf + i, k - i - 1))
89 + found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1);
90 + if (found_rfc2822 && sob &&
91 + !strncmp(buf + i, sob->buf, sob->len))
92 + found_sob = k;
93 +
94 + if (!(found_rfc2822 ||
95 + is_cherry_picked_from_line(buf + i, k - i - 1)))
96 return 0;
97 }
98 + if (found_sob == i)
99 + return 3;
100 + if (found_sob)
101 + return 2;
102 return 1;
103 }
104
@@ -300,7 +319,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
319 rollback_lock_file(&index_lock);
320
321 if (opts->signoff)
303 - append_signoff(msgbuf, 0);
322 + append_signoff(msgbuf, 0, 0);
323
324 if (!clean) {
325 int i;
@@ -556,7 +575,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
575 }
576
577 if (opts->record_origin) {
559 - if (!has_conforming_footer(&msgbuf, 0))
578 + if (!has_conforming_footer(&msgbuf, NULL, 0))
579 strbuf_addch(&msgbuf, '\n');
580 strbuf_addstr(&msgbuf, cherry_picked_prefix);
581 strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
@@ -1083,21 +1102,33 @@ int sequencer_pick_revisions(struct replay_opts *opts)
1102 return pick_commits(todo_list, opts);
1103 }
1104
1086 -void append_signoff(struct strbuf *msgbuf, int ignore_footer)
1105 +void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
1106 {
1107 + unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
1108 struct strbuf sob = STRBUF_INIT;
1089 - int i;
1109 + int has_footer;
1110
1111 strbuf_addstr(&sob, sign_off_header);
1112 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
1113 getenv("GIT_COMMITTER_EMAIL")));
1114 strbuf_addch(&sob, '\n');
1095 - for (i = msgbuf->len - 1 - ignore_footer; i > 0 && msgbuf->buf[i - 1] != '\n'; i--)
1096 - ; /* do nothing */
1097 - if (prefixcmp(msgbuf->buf + i, sob.buf)) {
1098 - if (!has_conforming_footer(msgbuf, ignore_footer))
1099 - strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, "\n", 1);
1100 - strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, sob.buf, sob.len);
1101 - }
1115 +
1116 + /*
1117 + * If the whole message buffer is equal to the sob, pretend that we
1118 + * found a conforming footer with a matching sob
1119 + */
1120 + if (msgbuf->len - ignore_footer == sob.len &&
1121 + !strncmp(msgbuf->buf, sob.buf, sob.len))
1122 + has_footer = 3;
1123 + else
1124 + has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer);
1125 +
1126 + if (!has_footer)
1127 + strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, "\n", 1);
1128 +
1129 + if (has_footer != 3 && (!no_dup_sob || has_footer != 2))
1130 + strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
1131 + sob.buf, sob.len);
1132 +
1133 strbuf_release(&sob);
1134 }
sequencer.h
+3 -1
@@ -6,6 +6,8 @@
6 #define SEQ_TODO_FILE "sequencer/todo"
7 #define SEQ_OPTS_FILE "sequencer/opts"
8
9 +#define APPEND_SIGNOFF_DEDUP (1u << 0)
10 +
11 enum replay_action {
12 REPLAY_REVERT,
13 REPLAY_PICK
@@ -48,6 +50,6 @@ int sequencer_pick_revisions(struct replay_opts *opts);
50
51 extern const char sign_off_header[];
52
51 -void append_signoff(struct strbuf *msgbuf, int ignore_footer);
53 +void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);
54
55 #endif
t/t3511-cherry-pick-x.sh
+1 -1
@@ -82,7 +82,7 @@ test_expect_success 'cherry-pick -s inserts blank line after one line subject' '
82 test_cmp expect actual
83 '
84
85 -test_expect_failure 'cherry-pick -s inserts blank line after non-conforming footer' '
85 +test_expect_success 'cherry-pick -s inserts blank line after non-conforming footer' '
86 pristine_detach initial &&
87 git cherry-pick -s mesg-broken-footer &&
88 cat <<-EOF >expect &&