[PATCH] builtin-commit.c: Not add duplicate S-o-b when commit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Scan the whole rfc2822 footer for duplicate S-o-b, not just the last
line of the commit message.

A commit may have multiple S-o-bs, or other tags, such as:

    some commit log...

    Signed-off-by: C O Mitter <committer@xxxxxxxxxxx>
    Reported-by: R E Porter <reporter@xxxxxxxxxxx>

Because the S-o-b is not located at the last line in the above commit,
when the above commit is amended by the original committer, a
duplicated S-o-b may appended by accident. New commit log may looks
like:

    some commit log...

    Signed-off-by: C O Mitter <committer@xxxxxxxxxxx>
    Reported-by: R E Porter <reporter@xxxxxxxxxxx>
    Signed-off-by: C O Mitter <committer@xxxxxxxxxxx>

This hack scans the whole rfc2822 footer for duplicate S-o-b, and only
append a S-o-b when necessary. Also add testcases in 't/t7502-commit.sh'
for this.

Signed-off-by: Jiang Xin <worldhello.net@xxxxxxxxx>
---
 builtin/commit.c  | 28 ++++++++++++++++++++++++----
 t/t7502-commit.sh | 19 +++++++++++++++++++
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 20cef..1a3da 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -704,15 +704,35 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	if (signoff) {
 		struct strbuf sob = STRBUF_INIT;
 		int i;
+		int hit_footer = 0;
+		int hit_sob = 0;
 
 		strbuf_addstr(&sob, sign_off_header);
 		strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
 					     getenv("GIT_COMMITTER_EMAIL")));
 		strbuf_addch(&sob, '\n');
-		for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
-			; /* do nothing */
-		if (prefixcmp(sb.buf + i, sob.buf)) {
-			if (!i || !ends_rfc2822_footer(&sb))
+		for (i = sb.len - 1; i > 0; i--) {
+			if (hit_footer && sb.buf[i] == '\n') {
+				hit_footer = 2;
+				i += 2;
+				break;
+			}
+			hit_footer = (sb.buf[i] == '\n');
+		}
+		hit_footer = (2 == hit_footer);
+		if (hit_footer) {
+			while (i < sb.len)
+			{
+				if (!prefixcmp(sb.buf + i, sob.buf)) {
+					hit_sob = 1;
+					break;
+				}
+				while (i < sb.len && sb.buf[i++] != '\n')
+					; /* do nothing */
+			}
+		}
+		if (!hit_sob) {
+			if (!hit_footer || !ends_rfc2822_footer(&sb))
 				strbuf_addch(&sb, '\n');
 			strbuf_addbuf(&sb, &sob);
 		}
diff --git a/t/t7502-commit.sh b/t/t7502-commit.sh
index 18145..8198f 100755
--- a/t/t7502-commit.sh
+++ b/t/t7502-commit.sh
@@ -336,7 +336,26 @@ test_expect_success 'A single-liner subject with a token plus colon is not a foo
 	git commit -s -m "hello: kitty" --allow-empty &&
 	git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
 	test_line_count = 3 actual
+'
+
+cat > expect << EOF
+Footer-like: commit log
+
+Signed-off-by: C O Mitter <committer@xxxxxxxxxxx>
+EOF
+
+test_expect_success 'S-o-b after footer-like commit message' '
+	head -1 expect | git commit -s --allow-empty -F - &&
+	git cat-file commit HEAD | sed "1,/^\$/d" > output &&
+	test_cmp expect output
+'
+
+echo "Reported-by: R E Porter <reporter@xxxxxxxxxxx>" >> expect
 
+test_expect_success 'no duplicate S-o-b when signoff' '
+	cat expect | git commit -s --allow-empty -F - &&
+	git cat-file commit HEAD | sed "1,/^\$/d" > output &&
+	test_cmp expect output
 '
 
 cat >.git/FAKE_EDITOR <<EOF
-- 
1.7.12.rc0.28.g8ecd8a5.dirty

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


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]