git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git List <git@vger.kernel.org>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Jiang Xin <worldhello.net@gmail.com>
Subject: [PATCH] builtin-commit.c: Not add duplicate S-o-b when commit
Date: Thu, 26 Jul 2012 14:01:19 +0800	[thread overview]
Message-ID: <ee08efceaa3f015732a19d49eb96bdeeaaf3d609.1343282283.git.worldhello.net@gmail.com> (raw)

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@example.com>
    Reported-by: R E Porter <reporter@example.com>

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@example.com>
    Reported-by: R E Porter <reporter@example.com>
    Signed-off-by: C O Mitter <committer@example.com>

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@gmail.com>
---
 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@example.com>
+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@example.com>" >> 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

             reply	other threads:[~2012-07-26  6:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-26  6:01 Jiang Xin [this message]
2012-07-26  6:44 ` [PATCH] builtin-commit.c: Not add duplicate S-o-b when commit Junio C Hamano
2012-07-26  7:15   ` Jiang Xin
2012-07-26 16:44     ` Junio C Hamano
2012-07-27  1:07       ` Jiang Xin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ee08efceaa3f015732a19d49eb96bdeeaaf3d609.1343282283.git.worldhello.net@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).