Subject: [PATCH v2 4/4] Unify appending signoff in format-patch, commit and sequencer

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 22 Nov 2012 23:38:09 +0700

There are two implementations of append_signoff in log-tree.c and
sequencer.c, which do more or less the same thing. This patch removes
the sequencer.c's in favor of the format-patch's.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/commit.c  | 10 ++++++++-
 log-tree.c        | 14 ++++++++----
 log-tree.h        |  4 ++++
 sequencer.c       | 65 ++-----------------------------------------------------
 sequencer.h       |  4 ----
 t/t7501-commit.sh |  2 +-
 6 files changed, 26 insertions(+), 73 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index a17a5df..6d323d9 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -698,7 +698,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 			previous = eol;
 		}
 
-		append_signoff(&sb, ignore_footer);
+		if (ignore_footer) {
+			struct strbuf footer = STRBUF_INIT;
+			strbuf_addstr(&footer, sb.buf + sb.len - ignore_footer);
+			strbuf_setlen(&sb, sb.len - ignore_footer);
+			append_signoff(&sb, SOB_IGNORE_SAME);
+			strbuf_addstr(&sb, footer.buf);
+			strbuf_release(&footer);
+		} else
+			append_signoff(&sb, SOB_IGNORE_SAME);
 	}
 
 	if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
diff --git a/log-tree.c b/log-tree.c
index 7e50545..e8d31d9 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -12,6 +12,8 @@
 
 struct decoration name_decoration = { "object names" };
 
+const char sign_off_header[] = "Signed-off-by: ";
+
 enum decoration_type {
 	DECORATION_NONE = 0,
 	DECORATION_REF_LOCAL,
@@ -207,7 +209,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
 }
 
 /*
- * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
+ * Search for "^[-A-Za-z0-9]+: [^@]+@" pattern. It usually matches
  * Signed-off-by: and Acked-by: lines.
  */
 static int detect_any_signoff(char *letter, int size)
@@ -243,6 +245,7 @@ static int detect_any_signoff(char *letter, int size)
 		}
 		if (('A' <= ch && ch <= 'Z') ||
 		    ('a' <= ch && ch <= 'z') ||
+		    ('0' <= ch && ch <= '9') ||
 		    ch == '-') {
 			seen_head = 1;
 			continue;
@@ -253,11 +256,10 @@ static int detect_any_signoff(char *letter, int size)
 	return seen_head && seen_name;
 }
 
-static void append_signoff(struct strbuf *sb, int flags)
+void append_signoff(struct strbuf *sb, int flags)
 {
 	char* signoff = xstrdup(fmt_name(getenv("GIT_COMMITTER_NAME"),
 					 getenv("GIT_COMMITTER_EMAIL")));
-	static const char sign_off_header[] = "Signed-off-by: ";
 	size_t signoff_len = strlen(signoff);
 	int has_signoff = 0;
 	char *cp;
@@ -310,7 +312,11 @@ static void append_signoff(struct strbuf *sb, int flags)
 		if (!isspace(cp[signoff_len]))
 			continue;
 		/* we already have him */
-		return;
+		if (flags & SOB_IGNORE_SAME) {
+			if (cp[signoff_len + 1] == '\0')
+				return;
+		} else
+			return;
 	}
 
 	if (!has_signoff)
diff --git a/log-tree.h b/log-tree.h
index f5ac238..739f729 100644
--- a/log-tree.h
+++ b/log-tree.h
@@ -24,4 +24,8 @@ void load_ref_decorations(int flags);
 void get_patch_filename(struct commit *commit, const char *subject, int nr,
 			const char *suffix, struct strbuf *buf);
 
+#define SOB_IGNORE_SAME   1
+extern const char sign_off_header[];
+void append_signoff(struct strbuf *msgbuf, int flags);
+
 #endif
diff --git a/sequencer.c b/sequencer.c
index e3723d2..bc02a66 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -14,11 +14,10 @@
 #include "merge-recursive.h"
 #include "refs.h"
 #include "argv-array.h"
+#include "log-tree.h"
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
-const char sign_off_header[] = "Signed-off-by: ";
-
 static void remove_sequencer_state(void)
 {
 	struct strbuf seq_dir = STRBUF_INIT;
@@ -236,7 +235,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
 	rollback_lock_file(&index_lock);
 
 	if (opts->signoff)
-		append_signoff(msgbuf, 0);
+		append_signoff(msgbuf, SOB_IGNORE_SAME);
 
 	if (!clean) {
 		int i;
@@ -1016,63 +1015,3 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	save_opts(opts);
 	return pick_commits(todo_list, opts);
 }
-
-static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
-{
-	int ch;
-	int hit = 0;
-	int i, j, k;
-	int len = sb->len - ignore_footer;
-	int first = 1;
-	const char *buf = sb->buf;
-
-	for (i = len - 1; i > 0; i--) {
-		if (hit && buf[i] == '\n')
-			break;
-		hit = (buf[i] == '\n');
-	}
-
-	while (i < len - 1 && buf[i] == '\n')
-		i++;
-
-	for (; i < len; i = k) {
-		for (k = i; k < len && buf[k] != '\n'; k++)
-			; /* do nothing */
-		k++;
-
-		if ((buf[k] == ' ' || buf[k] == '\t') && !first)
-			continue;
-
-		first = 0;
-
-		for (j = 0; i + j < len; j++) {
-			ch = buf[i + j];
-			if (ch == ':')
-				break;
-			if (isalnum(ch) ||
-			    (ch == '-'))
-				continue;
-			return 0;
-		}
-	}
-	return 1;
-}
-
-void append_signoff(struct strbuf *msgbuf, int ignore_footer)
-{
-	struct strbuf sob = STRBUF_INIT;
-	int i;
-
-	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 = 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);
-	}
-	strbuf_release(&sob);
-}
diff --git a/sequencer.h b/sequencer.h
index 9d57d57..99eb7fa 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -46,8 +46,4 @@ struct replay_opts {
 
 int sequencer_pick_revisions(struct replay_opts *opts);
 
-extern const char sign_off_header[];
-
-void append_signoff(struct strbuf *msgbuf, int ignore_footer);
-
 #endif
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 195e747..6dd4580 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -353,7 +353,7 @@ test_expect_success 'signoff gap' '
 
 	echo 3 >positive &&
 	git add positive &&
-	alt="Alt-RFC-822-Header: Value" &&
+	alt="Alt-RFC-822-Header: Va@lue" &&
 	git commit -s -m "welcome
 
 $alt" &&
-- 
1.8.0.4.g5d0415a