Skip to contentSkip to conversation

unify appending of sob

PATCH Superseded Brandon Casey 3 participants 36 messages Jun 15, 2016

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover unify appending of sob

Round 3.

-Brandon

Brandon Casey (9):
sequencer.c: rework search for start of footer to improve clarity commit, cherry-pick -s: remove broken support for multiline rfc2822
fields
t/test-lib-functions.sh: allow to specify the tag name to test_commit t/t3511: add some tests of 'cherry-pick -s' functionality sequencer.c: recognize "(cherry picked from ..." as part of s-o-b
footer
sequencer.c: always separate "(cherry picked from" from commit body sequencer.c: teach append_signoff how to detect duplicate s-o-b sequencer.c: teach append_signoff to avoid adding a duplicate newline Unify appending signoff in format-patch, commit and sequencer

Nguyễn Thái Ngọc Duy (2):
t4014: more tests about appending s-o-b lines
format-patch: update append_signoff prototype

 builtin/commit.c         |   2 +-
 builtin/log.c            |  13 +--
 log-tree.c               |  92 ++---------------
 revision.h               |   2 +-
 sequencer.c              | 150 ++++++++++++++++++---------
 sequencer.h              |   4 +-
 t/t3511-cherry-pick-x.sh | 219 +++++++++++++++++++++++++++++++++++++++
 t/t4014-format-patch.sh  | 263 +++++++++++++++++++++++++++++++++++++++++++++++
 t/test-lib-functions.sh  |   8 +-
 9 files changed, 600 insertions(+), 153 deletions(-)
 create mode 100755 t/t3511-cherry-pick-x.sh
Signature · 1 line
1.8.1.1.450.g0327af3
#1 lore raw

PATCH v3 1/11 sequencer.c: rework search for start of footer to improve clarity

This code sequence is somewhat difficult to read. Let's rewrite it using more descriptive variable names to try to make it easier to understand.

---
 sequencer.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
sequencer.c +6 -3
index aef5e8a..cd211b2 100644
@@ -1024,16 +1024,19 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
 {
 	int ch;
-	int hit = 0;
+	int last_char_was_nl, this_char_is_nl;
 	int i, j, k;
 	int len = sb->len - ignore_footer;
 	int first = 1;
 	const char *buf = sb->buf;
 
+	/* find start of last paragraph */
+	last_char_was_nl = 0;
 	for (i = len - 1; i > 0; i--) {
-		if (hit && buf[i] == '\n')
+		this_char_is_nl = (buf[i] == '\n');
+		if (last_char_was_nl && this_char_is_nl)
 			break;
-		hit = (buf[i] == '\n');
+		last_char_was_nl = this_char_is_nl;
 	}
 
 	while (i < len - 1 && buf[i] == '\n')
Signature · 1 line
1.8.1.1.450.g0327af3
Reviewed-by on v3 1/11
#2 lore raw

Re: [PATCH v3 01/11] sequencer.c: rework search for start of footer to improve clarity

Quoted 12 lines · Brandon Casey
sequencer.c +1 -1
@@ -1024,16 +1024,19 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
 {
 	int ch;
-	int hit = 0;
+	int last_char_was_nl, this_char_is_nl;
 	int i, j, k;
 	int len = sb->len - ignore_footer;
 	int first = 1;
 	const char *buf = sb->buf;

sequencer.c int sequencer_pick_revisions(struct replay_opts *opts) · line 1039

Quoted 10 lines
sequencer.c +5 -2
 
+	/* find start of last paragraph */
+	last_char_was_nl = 0;
 	for (i = len - 1; i > 0; i--) {
-		if (hit && buf[i] == '\n')
+		this_char_is_nl = (buf[i] == '\n');
+		if (last_char_was_nl && this_char_is_nl)
 			break;
-		hit = (buf[i] == '\n');
+		last_char_was_nl = this_char_is_nl;

I would have been tempted to write

	char prev;

	prev = 0;
	for (i = len - 1; i > 0; i--) {
		char ch = buf[i];
		if (prev == '\n' && ch == '\n')	/* paragraph break */
			break;
		prev = ch;
	}

but your rewrite is just as clear. For what it's worth,

#3 lore raw

PATCH v3 2/11 commit, cherry-pick -s: remove broken support for multiline rfc2822 fields

In reply to Brandon Casey

Starting with c1e01b0c (commit: More generous accepting of RFC-2822 footer lines, 2009-10-28), "git commit -s" carefully parses the last paragraph of each commit message to check if it consists only of RFC2822-style headers, in which case the signoff will be added as a new line in the same list:

It even included support for accepting indented continuation lines for multiline fields. Unfortunately the multiline field support is broken because it checks whether buf[k] (the first character of the *next* line) instead of buf[i] is a whitespace character. The result is that any footer with a continuation line is not accepted, since the last continuation line neither starts with an RFC2822 field name nor is followed by a continuation line.

That this has remained broken for so long is good evidence that nobody actually needed multiline fields. Rip out the broken continuation support.

There should be no functional change.

[Thanks to Jonathan Nieder for the excellent commit message]

---
 sequencer.c | 6 ------
 1 file changed, 6 deletions(-)
sequencer.c +0 -6
index cd211b2..39a752b 100644
@@ -1027,7 +1027,6 @@ static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
 	int last_char_was_nl, this_char_is_nl;
 	int i, j, k;
 	int len = sb->len - ignore_footer;
-	int first = 1;
 	const char *buf = sb->buf;
 
 	/* find start of last paragraph */
@@ -1047,11 +1046,6 @@ static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
 			; /* 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 == ':')
Signature · 1 line
1.8.1.1.450.g0327af3
Reviewed-by on v3 2/11
#4 lore raw

Re: [PATCH v3 02/11] commit, cherry-pick -s: remove broken support for multiline rfc2822 fields

Quoted 2 lines · Brandon Casey
 sequencer.c | 6 ------
 1 file changed, 6 deletions(-)

Nice simplification.

#5 lore raw

PATCH v3 3/11 t/test-lib-functions.sh: allow to specify the tag name to test_commit

In reply to Brandon Casey

The <message> part of test_commit() may not be appropriate for a tag name. So let's allow test_commit to accept a fourth argument to specify the tag name.

---
 t/test-lib-functions.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
t/test-lib-functions.sh +4 -4
index fa62d01..61d0804 100644
@@ -135,12 +135,12 @@ test_pause () {
 	fi
 }
 
-# Call test_commit with the arguments "<message> [<file> [<contents>]]"
+# Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]"
 #
 # This will commit a file with the given contents and the given commit
-# message.  It will also add a tag with <message> as name.
+# message, and tag the resulting commit with the given tag name.
 #
-# Both <file> and <contents> default to <message>.
+# <file>, <contents>, and <tag> all default to <message>.
 
 test_commit () {
 	notick= &&
@@ -168,7 +168,7 @@ test_commit () {
 		test_tick
 	fi &&
 	git commit $signoff -m "$1" &&
-	git tag "$1"
+	git tag "${4:-$1}"
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
1.8.1.1.450.g0327af3
#6 lore raw

PATCH v3 4/11 t/t3511: add some tests of 'cherry-pick -s' functionality

In reply to Brandon Casey

Add some tests to ensure that 'cherry-pick -s' operates in the following manner:

* Inserts a blank line before appending a s-o-b to a commit message that does not contain a s-o-b footer

* Does not mistake first line "subject: description" as a s-o-b footer

* Does not mistake single word message body as conforming to rfc2822

* Appends a s-o-b when last s-o-b in footer does not match committer s-o-b, even when committer's s-o-b exists elsewhere in footer.

* Does not append a s-o-b when last s-o-b matches committer s-o-b

* Correctly detects a non-conforming footer containing a mix of s-o-b like elements and s-o-b elements. (marked "expect failure")

---
 t/t3511-cherry-pick-x.sh | 111 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100755 t/t3511-cherry-pick-x.sh
t/t3511-cherry-pick-x.sh new file +111 -0
new file mode 100755
index 0000000..2a040b7
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='Test cherry-pick -x and -s'
+
+. ./test-lib.sh
+
+pristine_detach () {
+	git cherry-pick --quit &&
+	git checkout -f "$1^0" &&
+	git read-tree -u --reset HEAD &&
+	git clean -d -f -f -q -x
+}
+
+mesg_one_line='base: commit message'
+
+mesg_no_footer="$mesg_one_line
+
+OneWordBodyThatsNotA-S-o-B"
+
+mesg_with_footer="$mesg_no_footer
+
+Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+Signed-off-by: A.U. Thor <[email protected]>
+Signed-off-by: B.U. Thor <[email protected]>"
+
+mesg_broken_footer="$mesg_no_footer
+
+The signed-off-by string should begin with the words Signed-off-by followed
+by a colon and space, and then the signers name and email address. e.g.
+Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+
+mesg_with_footer_sob="$mesg_with_footer
+Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+
+
+test_expect_success setup '
+	git config advice.detachedhead false &&
+	echo unrelated >unrelated &&
+	git add unrelated &&
+	test_commit initial foo a &&
+	test_commit "$mesg_one_line" foo b mesg-one-line &&
+	git reset --hard initial &&
+	test_commit "$mesg_no_footer" foo b mesg-no-footer &&
+	git reset --hard initial &&
+	test_commit "$mesg_broken_footer" foo b mesg-broken-footer &&
+	git reset --hard initial &&
+	test_commit "$mesg_with_footer" foo b mesg-with-footer &&
+	git reset --hard initial &&
+	test_commit "$mesg_with_footer_sob" foo b mesg-with-footer-sob &&
+	pristine_detach initial &&
+	test_commit conflicting unrelated
+'
+
+test_expect_success 'cherry-pick -s inserts blank line after one line subject' '
+	pristine_detach initial &&
+	git cherry-pick -s mesg-one-line &&
+	cat <<-EOF >expect &&
+		$mesg_one_line
+
+		Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+	EOF
+	git log -1 --pretty=format:%B >actual &&
+	test_cmp expect actual
+'
+
+test_expect_failure 'cherry-pick -s inserts blank line after non-conforming footer' '
+	pristine_detach initial &&
+	git cherry-pick -s mesg-broken-footer &&
+	cat <<-EOF >expect &&
+		$mesg_broken_footer
+
+		Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+	EOF
+	git log -1 --pretty=format:%B >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s inserts blank line when conforming footer not found' '
+	pristine_detach initial &&
+	git cherry-pick -s mesg-no-footer &&
+	cat <<-EOF >expect &&
+		$mesg_no_footer
+
+		Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+	EOF
+	git log -1 --pretty=format:%B >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s adds sob when last sob doesnt match committer' '
+	pristine_detach initial &&
+	git cherry-pick -s mesg-with-footer &&
+	cat <<-EOF >expect &&
+		$mesg_with_footer
+		Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+	EOF
+	git log -1 --pretty=format:%B >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s refrains from adding duplicate trailing sob' '
+	pristine_detach initial &&
+	git cherry-pick -s mesg-with-footer-sob &&
+	cat <<-EOF >expect &&
+		$mesg_with_footer_sob
+	EOF
+	git log -1 --pretty=format:%B >actual &&
+	test_cmp expect actual
+'
+
+test_done
Signature · 1 line
1.8.1.1.450.g0327af3
Reviewed-by on v3 4/11
#7 lore raw

Re: [PATCH v3 04/11] t/t3511: add some tests of 'cherry-pick -s' functionality

Quoted 5 lines · Brandon Casey
t/t3511-cherry-pick-x.sh new file +2 -0
@@ -0,0 +1,111 @@
+#!/bin/sh
+

t/t3511-cherry-pick-x.sh line 12

Quoted 10 lines
t/t3511-cherry-pick-x.sh new file +10 -0
+test_description='Test cherry-pick -x and -s'
+
+. ./test-lib.sh
+
+pristine_detach () {
+	git cherry-pick --quit &&
+	git checkout -f "$1^0" &&
+	git read-tree -u --reset HEAD &&
+	git clean -d -f -f -q -x
+}

Some day this should move to test-lib-functions.sh. Not relevant for this patch, though.

[...]

t/t3511-cherry-pick-x.sh line 72

Quoted 7 lines
t/t3511-cherry-pick-x.sh new file +7 -0
+test_expect_failure 'cherry-pick -s inserts blank line after non-conforming footer' '
+	pristine_detach initial &&
+	git cherry-pick -s mesg-broken-footer &&
+	cat <<-EOF >expect &&
+		$mesg_broken_footer
+
+		Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>

This bug is old. When c1e01b0c (commit: More generous accepting of RFC-2822 footer lines, 2009-10-28) added more precise parsing of the RFC2822 footer when deciding whether to add a newline before a new signoff, it forgot to change the "sign-off already present" case to match.

Thanks for the test. The rest of the tests in this file also look good, of course.

#8 lore raw

PATCH v3 5/11 sequencer.c: recognize "(cherry picked from ..." as part of s-o-b footer

In reply to Brandon Casey

When 'cherry-pick -s' is used to append a signed-off-by line to a cherry picked commit, it does not currently detect the "(cherry picked from..." that may have been appended by a previous 'cherry-pick -x' as part of the s-o-b footer and it will insert a blank line before appending a new s-o-b.

Let's detect "(cherry picked from...)" as part of the footer so that we will produce this:

(cherry picked from da39a3ee5e6b4b0d3255bfef95601890afd80709)

instead of this:

(cherry picked from da39a3ee5e6b4b0d3255bfef95601890afd80709)

---
 sequencer.c              | 46 ++++++++++++++++++++++++++++------------
 t/t3511-cherry-pick-x.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 13 deletions(-)
sequencer.c +33 -13
index 39a752b..0b5cd18 100644
@@ -18,6 +18,7 @@
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
 const char sign_off_header[] = "Signed-off-by: ";
+static const char cherry_picked_prefix[] = "(cherry picked from commit ";
 
 static void remove_sequencer_state(void)
 {
@@ -496,7 +497,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 
 		if (opts->record_origin) {
-			strbuf_addstr(&msgbuf, "(cherry picked from commit ");
+			strbuf_addstr(&msgbuf, cherry_picked_prefix);
 			strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
 			strbuf_addstr(&msgbuf, ")\n");
 		}
@@ -1021,11 +1022,36 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	return pick_commits(todo_list, opts);
 }
 
-static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
+static int is_rfc2822_line(const char *buf, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++) {
+		int ch = buf[i];
+		if (ch == ':')
+			break;
+		if (isalnum(ch) || (ch == '-'))
+			continue;
+		return 0;
+	}
+
+	return 1;
+}
+
+static int is_cherry_picked_from_line(const char *buf, int len)
+{
+	/*
+	 * We only care that it looks roughly like (cherry picked from ...)
+	 */
+	return !prefixcmp(buf, cherry_picked_prefix) &&
+		(buf[len - 1] == ')' ||
+		 (buf[len - 1] == '\n' && buf[len - 2] == ')'));
+}
+
+static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
 {
-	int ch;
 	int last_char_was_nl, this_char_is_nl;
-	int i, j, k;
+	int i, k;
 	int len = sb->len - ignore_footer;
 	const char *buf = sb->buf;
 
@@ -1046,15 +1072,9 @@ static int ends_rfc2822_footer(struct strbuf *sb, int ignore_footer)
 			; /* do nothing */
 		k++;
 
-		for (j = 0; i + j < len; j++) {
-			ch = buf[i + j];
-			if (ch == ':')
-				break;
-			if (isalnum(ch) ||
-			    (ch == '-'))
-				continue;
+		if (!(is_rfc2822_line(buf + i, k - i) ||
+			is_cherry_picked_from_line(buf + i, k - i)))
 			return 0;
-		}
 	}
 	return 1;
 }
@@ -1071,7 +1091,7 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer)
 	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))
+		if (!i || !has_conforming_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);
 	}
t/t3511-cherry-pick-x.sh +55 -0

77 lines not on this page · raw message

Signature · 1 line
1.8.1.1.450.g0327af3
#9 lore raw

Re: [PATCH v3 05/11] sequencer.c: recognize "(cherry picked from ..." as part of s-o-b footer

Quoted 5 lines · Brandon Casey

Let's detect "(cherry picked from...)" as part of the footer so that we will produce this:

(cherry picked from da39a3ee5e6b4b0d3255bfef95601890afd80709)

Quoted 8 lines

instead of this:

(cherry picked from da39a3ee5e6b4b0d3255bfef95601890afd80709)

Here's the tweak I suggested last time. I think its behavior is slightly better in the "ends with incomplete line" case because it limits the characters examined by is_rfc2822_line() and is_cherry_picked_from_line() not to include buf[len] (which would presumably sometimes be '\0').

sequencer.c +3 -5
index 0b5cd18c..fa29c7c5 100644
@@ -1043,9 +1043,7 @@ static int is_cherry_picked_from_line(const char *buf, int len)
 	/*
 	 * We only care that it looks roughly like (cherry picked from ...)
 	 */
-	return !prefixcmp(buf, cherry_picked_prefix) &&
-		(buf[len - 1] == ')' ||
-		 (buf[len - 1] == '\n' && buf[len - 2] == ')'));
+	return !prefixcmp(buf, cherry_picked_prefix) && buf[len - 1] == ')';
 }
 
 static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
@@ -1072,8 +1070,8 @@ static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
 			; /* do nothing */
 		k++;
 
-		if (!(is_rfc2822_line(buf + i, k - i) ||
-			is_cherry_picked_from_line(buf + i, k - i)))
+		if (!is_rfc2822_line(buf + i, k - i - 1) &&
+		    !is_cherry_picked_from_line(buf + i, k - i - 1))
 			return 0;
 	}
 	return 1;
#10 lore raw

Re: [PATCH v3 05/11] sequencer.c: recognize "(cherry picked from ..." as part of s-o-b footer

Quoted 5 lines · Jonathan Nieder

Here's the tweak I suggested last time. I think its behavior is slightly better in the "ends with incomplete line" case because it limits the characters examined by is_rfc2822_line() and is_cherry_picked_from_line() not to include buf[len] (which would presumably sometimes be '\0').

Whoops, that revealed a subtlety --- the '\n' or '\0' is what prevents exiting the loop in is_rfc2822_line when the line does not contain a colon. Here's a corrected version of the tweak, that should actually work. :)

sequencer.c +6 -10
index 0b5cd18c..108ea27b 100644
@@ -1029,13 +1029,11 @@ static int is_rfc2822_line(const char *buf, int len)
 	for (i = 0; i < len; i++) {
 		int ch = buf[i];
 		if (ch == ':')
+			return 1;
+		if (!isalnum(ch) && ch != '-')
 			break;
-		if (isalnum(ch) || (ch == '-'))
-			continue;
-		return 0;
 	}
-
-	return 1;
+	return 0;
 }
 
 static int is_cherry_picked_from_line(const char *buf, int len)
@@ -1043,9 +1041,7 @@ static int is_cherry_picked_from_line(const char *buf, int len)
 	/*
 	 * We only care that it looks roughly like (cherry picked from ...)
 	 */
-	return !prefixcmp(buf, cherry_picked_prefix) &&
-		(buf[len - 1] == ')' ||
-		 (buf[len - 1] == '\n' && buf[len - 2] == ')'));
+	return !prefixcmp(buf, cherry_picked_prefix) && buf[len - 1] == ')';
 }
 
 static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
@@ -1072,8 +1068,8 @@ static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
 			; /* do nothing */
 		k++;
 
-		if (!(is_rfc2822_line(buf + i, k - i) ||
-			is_cherry_picked_from_line(buf + i, k - i)))
+		if (!is_rfc2822_line(buf + i, k - i - 1) &&
+		    !is_cherry_picked_from_line(buf + i, k - i - 1))
 			return 0;
 	}
 	return 1;
#11 lore raw

Re: [PATCH v3 05/11] sequencer.c: recognize "(cherry picked from ..." as part of s-o-b footer

Quoted 46 lines · Jonathan Nieder
Quoted 5 lines · Jonathan Nieder

Here's the tweak I suggested last time. I think its behavior is slightly better in the "ends with incomplete line" case because it limits the characters examined by is_rfc2822_line() and is_cherry_picked_from_line() not to include buf[len] (which would presumably sometimes be '\0').

Whoops, that revealed a subtlety --- the '\n' or '\0' is what prevents exiting the loop in is_rfc2822_line when the line does not contain a colon. Here's a corrected version of the tweak, that should actually work. :)

sequencer.c +4 -8
index 0b5cd18c..108ea27b 100644
@@ -1029,13 +1029,11 @@ static int is_rfc2822_line(const char *buf, int len)
        for (i = 0; i < len; i++) {
                int ch = buf[i];
                if (ch == ':')
+                       return 1;
+               if (!isalnum(ch) && ch != '-')
                        break;
-               if (isalnum(ch) || (ch == '-'))
-                       continue;
-               return 0;
        }
-
-       return 1;
+       return 0;
 }
 
 static int is_cherry_picked_from_line(const char *buf, int len)
@@ -1043,9 +1041,7 @@ static int is_cherry_picked_from_line(const char *buf, int len)
        /*
         * We only care that it looks roughly like (cherry picked from ...)
         */
-       return !prefixcmp(buf, cherry_picked_prefix) &&
-               (buf[len - 1] == ')' ||
-                (buf[len - 1] == '\n' && buf[len - 2] == ')'));
+       return !prefixcmp(buf, cherry_picked_prefix) && buf[len - 1] == ')';
 }
 
 static int has_conforming_footer(struct strbuf *sb, int ignore_footer)

sequencer.c static int has_conforming_footer(struct strbuf *sb, int ignore_footer) · line 1075

Quoted 10 lines
sequencer.c +2 -2
@@ -1072,8 +1068,8 @@ static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
                        ; /* do nothing */
                k++;
 
-               if (!(is_rfc2822_line(buf + i, k - i) ||
-                       is_cherry_picked_from_line(buf + i, k - i)))
+               if (!is_rfc2822_line(buf + i, k - i - 1) &&
+                   !is_cherry_picked_from_line(buf + i, k - i - 1))
                        return 0;
        }
        return 1;

Looks good to me.

-Brandon

#12 lore raw

PATCH v3 6/11 sequencer.c: always separate "(cherry picked from" from commit body

In reply to Brandon Casey

Start treating the "(cherry picked from" line added by cherry-pick -x the same way that the s-o-b lines are treated. Namely, separate them from the main commit message body with an empty line.

This commit is mostly a code movement, but notice that has_conforming_footer() was modified, in addition to being moved.

Introduce tests to test this functionality.

---
 sequencer.c              | 120 +++++++++++++++++++++++++----------------------
 t/t3511-cherry-pick-x.sh |  53 +++++++++++++++++++++
 2 files changed, 116 insertions(+), 57 deletions(-)
sequencer.c +63 -57
index 0b5cd18..46d51b2 100644
@@ -20,6 +20,67 @@
 const char sign_off_header[] = "Signed-off-by: ";
 static const char cherry_picked_prefix[] = "(cherry picked from commit ";
 
+static int is_rfc2822_line(const char *buf, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++) {
+		int ch = buf[i];
+		if (ch == ':')
+			break;
+		if (isalnum(ch) || (ch == '-'))
+			continue;
+		return 0;
+	}
+
+	return 1;
+}
+
+static int is_cherry_picked_from_line(const char *buf, int len)
+{
+	/*
+	 * We only care that it looks roughly like (cherry picked from ...)
+	 */
+	return !prefixcmp(buf, cherry_picked_prefix) &&
+		(buf[len - 1] == ')' ||
+		 (buf[len - 1] == '\n' && buf[len - 2] == ')'));
+}
+
+static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
+{
+	int last_char_was_nl, this_char_is_nl;
+	int i, k;
+	int len = sb->len - ignore_footer;
+	const char *buf = sb->buf;
+
+	/* find start of last paragraph */
+	last_char_was_nl = 0;
+	for (i = len - 1; i > 0; i--) {
+		this_char_is_nl = (buf[i] == '\n');
+		if (last_char_was_nl && this_char_is_nl)
+			break;
+		last_char_was_nl = this_char_is_nl;
+	}
+
+	/* require at least one blank line */
+	if (!last_char_was_nl || buf[i] != '\n')
+		return 0;
+
+	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 (!(is_rfc2822_line(buf + i, k - i) ||
+			is_cherry_picked_from_line(buf + i, k - i)))
+			return 0;
+	}
+	return 1;
+}
+
 static void remove_sequencer_state(void)
 {
 	struct strbuf seq_dir = STRBUF_INIT;
@@ -497,6 +558,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 
 		if (opts->record_origin) {
+			if (!has_conforming_footer(&msgbuf, 0))
+				strbuf_addch(&msgbuf, '\n');
 			strbuf_addstr(&msgbuf, cherry_picked_prefix);
 			strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
 			strbuf_addstr(&msgbuf, ")\n");
@@ -1022,63 +1085,6 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	return pick_commits(todo_list, opts);
 }
 
-static int is_rfc2822_line(const char *buf, int len)
-{
-	int i;
-
-	for (i = 0; i < len; i++) {
-		int ch = buf[i];
-		if (ch == ':')
-			break;
-		if (isalnum(ch) || (ch == '-'))
-			continue;
-		return 0;
-	}
-
-	return 1;
-}
-
-static int is_cherry_picked_from_line(const char *buf, int len)
-{
-	/*
-	 * We only care that it looks roughly like (cherry picked from ...)
-	 */
-	return !prefixcmp(buf, cherry_picked_prefix) &&
-		(buf[len - 1] == ')' ||
-		 (buf[len - 1] == '\n' && buf[len - 2] == ')'));
-}
-
-static int has_conforming_footer(struct strbuf *sb, int ignore_footer)
-{
-	int last_char_was_nl, this_char_is_nl;
-	int i, k;
-	int len = sb->len - ignore_footer;
-	const char *buf = sb->buf;
-
-	/* find start of last paragraph */
-	last_char_was_nl = 0;
-	for (i = len - 1; i > 0; i--) {
-		this_char_is_nl = (buf[i] == '\n');
-		if (last_char_was_nl && this_char_is_nl)
-			break;
-		last_char_was_nl = this_char_is_nl;
-	}
-
-	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 (!(is_rfc2822_line(buf + i, k - i) ||
-			is_cherry_picked_from_line(buf + i, k - i)))
-			return 0;
-	}
-	return 1;
-}
-
 void append_signoff(struct strbuf *msgbuf, int ignore_footer)
 {
 	struct strbuf sob = STRBUF_INIT;
t/t3511-cherry-pick-x.sh +53 -0

75 lines not on this page · raw message

Signature · 1 line
1.8.1.1.450.g0327af3
Reviewed-by on v3 6/11
#13 lore raw

Re: [PATCH v3 06/11] sequencer.c: always separate "(cherry picked from" from commit body

Quoted 3 lines · Brandon Casey
sequencer.c +0 -0
@@ -20,6 +20,67 @@

[...]

Quoted 1 line

static int has_conforming_footer(struct strbuf *sb, int ignore_footer)

[...]

sequencer.c line 67

Quoted 3 lines
sequencer.c +3 -0
+	/* require at least one blank line */
+	if (!last_char_was_nl || buf[i] != '\n')
+		return 0;

Makes sense. append_signoff already added a blank line after a one-line message

foo: bar

because of e5138436 (builtin-commit.c: fix logic to omit empty line before existing footers, 2009-11-06) but the logic to do so was in the wrong place and it didn't handle its ill-formatted cousin

foo: bar
baz: qux

[...]

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts) · line 562

Quoted 6 lines
sequencer.c +2 -0
@@ -497,6 +558,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 
 		if (opts->record_origin) {
+			if (!has_conforming_footer(&msgbuf, 0))
+				strbuf_addch(&msgbuf, '\n');

What should this do in the case of an entirely blank message? (Maybe that's too insane a case to worry about.)

[...]

t/t3511-cherry-pick-x.sh test_expect_failure 'cherry-pick -s inserts blank line after non-conforming foot · line 97

Quoted 7 lines
t/t3511-cherry-pick-x.sh +1 -0
@@ -81,6 +94,19 @@ test_expect_failure 'cherry-pick -s inserts blank line after non-conforming foot
 	test_cmp expect actual
 '
 
+test_expect_success 'cherry-pick -x inserts blank line when conforming footer not found' '

Yay. :)

Thanks for a clear and well thought-out patch with thorough tests.

#14 lore raw

Re: [PATCH v3 06/11] sequencer.c: always separate "(cherry picked from" from commit body

Quoted 8 lines · Jonathan Nieder
Quoted 6 lines · Brandon Casey
sequencer.c +0 -0
@@ -497,6 +558,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
              }
 
Quoted 6 lines

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts) · line 562

Quoted 3 lines
sequencer.c +2 -0
              if (opts->record_origin) {
+                     if (!has_conforming_footer(&msgbuf, 0))
+                             strbuf_addch(&msgbuf, '\n');

What should this do in the case of an entirely blank message? (Maybe that's too insane a case to worry about.)

Yeah, I think there are a number of insane cases that I think we should just say "behavior is undefined".

- Entirely blank message
- Message lacking trailing newline.
- Oneline message that is actually a signed-off-by (equal or not to
the committer's sob).
- Message body that is entirely 1 or more newlines, or one which has
more than one trailing newline.

It's probably not worth going through contortions to handle these cases.

-Brandon

#15 lore raw

PATCH v3 7/11 sequencer.c: teach append_signoff how to detect duplicate s-o-b

In reply to Brandon Casey

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.

---
 builtin/commit.c         |  2 +-
 sequencer.c              | 47 +++++++++++++++++++++++++++++++++++++----------
 sequencer.h              |  4 +++-
 t/t3511-cherry-pick-x.sh |  2 +-
 4 files changed, 42 insertions(+), 13 deletions(-)
builtin/commit.c +1 -1
index 38b9a9c..67ea9e7 100644
@@ -700,7 +700,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 			previous = eol;
 		}
 
-		append_signoff(&sb, ignore_footer);
+		append_signoff(&sb, ignore_footer, 0);
 	}
 
 	if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
sequencer.c +37 -10

96 lines not on this page · raw message

sequencer.h +3 -1
index 9d57d57..1fc22dc 100644
@@ -6,6 +6,8 @@
 #define SEQ_TODO_FILE	"sequencer/todo"
 #define SEQ_OPTS_FILE	"sequencer/opts"
 
+#define APPEND_SIGNOFF_DEDUP (1u << 0)
+
 enum replay_action {
 	REPLAY_REVERT,
 	REPLAY_PICK
@@ -48,6 +50,6 @@ int sequencer_pick_revisions(struct replay_opts *opts);
 
 extern const char sign_off_header[];
 
-void append_signoff(struct strbuf *msgbuf, int ignore_footer);
+void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);
 
 #endif
t/t3511-cherry-pick-x.sh +1 -1
index a845e45..f977279 100755
@@ -82,7 +82,7 @@ test_expect_success 'cherry-pick -s inserts blank line after one line subject' '
 	test_cmp expect actual
 '
 
-test_expect_failure 'cherry-pick -s inserts blank line after non-conforming footer' '
+test_expect_success 'cherry-pick -s inserts blank line after non-conforming footer' '
 	pristine_detach initial &&
 	git cherry-pick -s mesg-broken-footer &&
 	cat <<-EOF >expect &&
Signature · 1 line
1.8.1.1.450.g0327af3
Reviewed-by on v3 7/11
#16 lore raw

Re: [PATCH v3 07/11] sequencer.c: teach append_signoff how to detect duplicate s-o-b

Quoted 1 line · Brandon Casey

Teach append_signoff how to detect a duplicate s-o-b in the commit footer.

This replaces the previous (slightly broken) logic that checked whether the sign-off to be appended would be redundant and puts the fixed logic further down the call-chain next to the rest of footer parsing.

I am not thrilled with the

0 = no rfc-style footer
1 = rfc-style footer, no sign-off found
2 = rfc-style footer, sign-off found but not as last field 3 = rfc-style footer, sign-off found as last field

convention but since it's local to sequencer.c and this logic to scan all lines for the sign-off can probably be ripped out soon I don't mind.

The general direction is good and the execution looks obviously correct.

#17 lore raw

Re: [PATCH v3 07/11] sequencer.c: teach append_signoff how to detect duplicate s-o-b

In reply to Brandon Casey
Quoted 2 lines · Brandon Casey
sequencer.c +0 -0

[...]

sequencer.c void append_signoff(struct strbuf *msgbuf, int ignore_footer) · line 1122

Quoted 12 lines
sequencer.c +3 -5
@@ -1096,10 +1117,16 @@ 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 || !has_conforming_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 = has_conforming_footer(msgbuf, &sob, ignore_footer);

Is this "if (i)" test needed? I'd expect that if the message has no newlines, has_conforming_footer() would notice that and return 0.

#18 lore raw

Re: [PATCH v3 07/11] sequencer.c: teach append_signoff how to detect duplicate s-o-b

Quoted 5 lines · Jonathan Nieder
Quoted 2 lines · Brandon Casey
sequencer.c +0 -0

[...]

Quoted 15 lines

sequencer.c void append_signoff(struct strbuf *msgbuf, int ignore_footer) · line 1122

Quoted 12 lines
sequencer.c +3 -5
@@ -1096,10 +1117,16 @@ 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 || !has_conforming_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 = has_conforming_footer(msgbuf, &sob, ignore_footer);

Is this "if (i)" test needed? I'd expect that if the message has no newlines, has_conforming_footer() would notice that and return 0.

Thanks for pointing out this awkwardness. It should be pushed into has_conforming_footer().

-Brandon

#19 lore raw

PATCH v3 8/11 sequencer.c: teach append_signoff to avoid adding a duplicate newline

In reply to Brandon Casey

Teach append_signoff to detect whether a blank line exists at the position that the signed-off-by line will be added, and avoid adding an additional one if one already exists. This is necessary to allow format-patch to add a s-o-b to a patch with no commit message without adding an extra newline. A following patch will make format-patch use this function rather than the append_signoff implementation inside log-tree.c.

---
 sequencer.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
sequencer.c +9 -5
index 015cb58..404b786 100644
@@ -1118,11 +1118,15 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
 	for (i = msgbuf->len - 1 - ignore_footer; i > 0 && msgbuf->buf[i - 1] != '\n'; i--)
 		; /* do nothing */
 
-	if (i)
-		has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer);
-
-	if (!has_footer)
-		strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, "\n", 1);
+	if (msgbuf->buf[i] != '\n') {
+		if (i)
+			has_footer = has_conforming_footer(msgbuf, &sob,
+					ignore_footer);
+
+		if (!has_footer)
+			strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
+					"\n", 1);
+	}
 
 	if (has_footer != 3 && (!no_dup_sob || has_footer != 2))
 		strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
Signature · 1 line
1.8.1.1.450.g0327af3
#20 lore raw

Re: [PATCH v3 08/11] sequencer.c: teach append_signoff to avoid adding a duplicate newline

Quoted 4 lines · Brandon Casey

Teach append_signoff to detect whether a blank line exists at the position that the signed-off-by line will be added, and avoid adding an additional one if one already exists. This is necessary to allow format-patch to add a s-o-b to a patch with no commit message without adding an extra newline.

I assume this means you're preserving historical behavior when adding a sign-off to a commit with empty description (which is a good thing), but what is that behavior? Is it a deliberate choice or something that developed by default?

[...]

Quoted 12 lines
sequencer.c +1 -5
@@ -1118,11 +1118,15 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
 	for (i = msgbuf->len - 1 - ignore_footer; i > 0 && msgbuf->buf[i - 1] != '\n'; i--)
 		; /* do nothing */
 
-	if (i)
-		has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer);
-
-	if (!has_footer)
-		strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, "\n", 1);
+	if (msgbuf->buf[i] != '\n') {

sequencer.c void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) · line 1131

Quoted 10 lines
sequencer.c +8 -0
+		if (i)
+			has_footer = has_conforming_footer(msgbuf, &sob,
+					ignore_footer);
+
+		if (!has_footer)
+			strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
+					"\n", 1);
+	}
 
 	if (has_footer != 3 && (!no_dup_sob || has_footer != 2))

This is too much nesting for my small mind to keep track of. Am I correct in imagining the effect is the same as the following?

	has_footer = has_conforming_footer(...)

	if (!has_footer && msgbuf->buf[i] != '\n')
		strbuf_splice(...);	/* add blank line */

	if (has_footer != 3 && ...
#21 lore raw

PATCH v3 9/11 t4014: more tests about appending s-o-b lines

In reply to Brandon Casey

From: Nguyễn Thái Ngọc Duy <[email protected]>

[bc: Squash the tests from Duy's original unify-appending-sob series.

Fix test 90 "signoff: some random signoff-alike" and mark as failing. Correct behavior should insert a blank line after message body and signed-off-by.

Add two additional tests:

1. failure to detect non-conforming elements in the footer when last line matches committer's s-o-b.
2. ensure various s-o-b -like elements in the footer are handled as conforming. e.g. "Change-id: IXXXX or Bug: 1234"
]

---
 t/t4014-format-patch.sh | 242 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 242 insertions(+)
t/t4014-format-patch.sh +242 -0
index 7fa3647..3868cef 100755
@@ -1021,4 +1021,246 @@ test_expect_success 'cover letter using branch description (6)' '
 	grep hello actual >/dev/null
 '
 
+append_signoff()
+{
+	C=`git commit-tree HEAD^^{tree} -p HEAD` &&
+	git format-patch --stdout --signoff ${C}^..${C} |
+		tee append_signoff.patch |
+		sed -n "1,/^---$/p" |
+		grep -n -E "^Subject|Sign|^$"
+}
+
+test_expect_success 'signoff: commit with no body' '
+	append_signoff </dev/null >actual &&
+	cat <<\EOF | sed "s/EOL$//" >expected &&
+4:Subject: [PATCH] EOL
+8:
+9:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: commit with only subject' '
+	echo subject | append_signoff >actual &&
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+9:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: commit with only subject that does not end with NL' '
+	printf subject | append_signoff >actual &&
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+9:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: no existing signoffs' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+11:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: no existing signoffs and no trailing NL' '
+	printf "subject\n\nbody" | append_signoff >actual &&
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+11:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: some random signoff' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+
+Signed-off-by: my@house
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+11:Signed-off-by: my@house
+12:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_failure 'signoff: some random signoff-alike' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+Fooled-by-me: my@house
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+11:
+12:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_failure 'signoff: not really a signoff' '
+	append_signoff <<\EOF >actual &&
+subject
+
+I want to mention about Signed-off-by: here.
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+9:I want to mention about Signed-off-by: here.
+10:
+11:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_failure 'signoff: not really a signoff (2)' '
+	append_signoff <<\EOF >actual &&
+subject
+
+My unfortunate
+Signed-off-by: example happens to be wrapped here.
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:Signed-off-by: example happens to be wrapped here.
+11:
+12:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_failure 'signoff: valid S-o-b paragraph in the middle' '
+	append_signoff <<\EOF >actual &&
+subject
+
+Signed-off-by: my@house
+Signed-off-by: your@house
+
+A lot of houses.
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+9:Signed-off-by: my@house
+10:Signed-off-by: your@house
+11:
+13:
+14:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: the same signoff at the end' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+
+Signed-off-by: C O Mitter <[email protected]>
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+11:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: the same signoff at the end, no trailing NL' '
+	printf "subject\n\nSigned-off-by: C O Mitter <[email protected]>" |
+		append_signoff >actual &&
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+9:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: the same signoff NOT at the end' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+
+Signed-off-by: C O Mitter <[email protected]>
+Signed-off-by: my@house
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+11:Signed-off-by: C O Mitter <[email protected]>
+12:Signed-off-by: my@house
+EOF
+	test_cmp expected actual
+'
+
+test_expect_failure 'signoff: detect garbage in non-conforming footer' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+
+Tested-by: my@house
+Some Trash
+Signed-off-by: C O Mitter <[email protected]>
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+13:Signed-off-by: C O Mitter <[email protected]>
+14:
+15:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: footer begins with non-signoff without @ sign' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+
+Reviewed-id: Noone
+Tested-by: my@house
+Change-id: Ideadbeef
+Signed-off-by: C O Mitter <[email protected]>
+Bug: 1234
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+14:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
 test_done
Signature · 1 line
1.8.1.1.450.g0327af3
#22 lore raw

Re: [PATCH v3 09/11] t4014: more tests about appending s-o-b lines

Brandon Casey wrote:

[...]

Quoted 4 lines
t/t4014-format-patch.sh +0 -0
@@ -1021,4 +1021,246 @@ test_expect_success 'cover letter using branch description (6)' '
 	grep hello actual >/dev/null

t/t4014-format-patch.sh test_expect_success 'cover letter using branch description (6)' ' · line 1031

Quoted 10 lines
t/t4014-format-patch.sh +8 -0
 '
 
+append_signoff()
+{
+	C=`git commit-tree HEAD^^{tree} -p HEAD` &&
+	git format-patch --stdout --signoff ${C}^..${C} |
+		tee append_signoff.patch |
+		sed -n "1,/^---$/p" |
+		grep -n -E "^Subject|Sign|^$"
+}

Is "grep -n" portable? I didn't find any uses of it elsewhere in the testsuite.

Style: checking exit status from format-patch, avoiding sed|grep pipeline:

	C=$(git commit-tree HEAD^ -p HEAD) &&
	git format-patch --stdout --signoff $C^..$C >append_signoff.patch &&
	awk '
		/^---$/ { exit; }
		/^Subject/ || /^Sign/ || /^$/ { print NR ":" $0 }
	' <append_signoff.patch >actual
#23 lore raw

Re: [PATCH v3 09/11] t4014: more tests about appending s-o-b lines

Quoted 9 lines · Jonathan Nieder

Brandon Casey wrote:

[...]

Quoted 6 lines
t/t4014-format-patch.sh +0 -0
@@ -1021,4 +1021,246 @@ test_expect_success 'cover letter using branch description (6)' '
 	grep hello actual >/dev/null
 '
 
Quoted 11 lines

t/t4014-format-patch.sh test_expect_success 'cover letter using branch description (6)' ' · line 1031

Quoted 8 lines
t/t4014-format-patch.sh +8 -0
+append_signoff()
+{
+	C=`git commit-tree HEAD^^{tree} -p HEAD` &&
+	git format-patch --stdout --signoff ${C}^..${C} |
+		tee append_signoff.patch |
+		sed -n "1,/^---$/p" |
+		grep -n -E "^Subject|Sign|^$"
+}

Is "grep -n" portable? I didn't find any uses of it elsewhere in the testsuite.

Yes, "-n" is in POSIX. Even though we use it ourselves, "git grep" supports it, too.

Any Emacs user would scream if their platform "grep" does not support it, as it will make M-x grep (or grep-find) useless.

Quoted 8 lines

Style: checking exit status from format-patch, avoiding sed|grep pipeline:

	C=$(git commit-tree HEAD^ -p HEAD) &&
	git format-patch --stdout --signoff $C^..$C >append_signoff.patch &&
	awk '
		/^---$/ { exit; }
		/^Subject/ || /^Sign/ || /^$/ { print NR ":" $0 }
	' <append_signoff.patch >actual

Yeah, awk/perl would be fine, too, and it is good that you pointed out that the original was losing the exit status from format-patch.

Thanks.

#24 lore raw

Re: [PATCH v3 09/11] t4014: more tests about appending s-o-b lines

Quoted 5 lines · Junio C Hamano
Quoted 2 lines

Is "grep -n" portable? I didn't find any uses of it elsewhere in the testsuite.

Yes, "-n" is in POSIX. Even though we use it ourselves, "git grep" supports it, too.

Ehh even though we *DONT* use it ourselves, ... that is.

I do not think we mind, if its use helps our test.

#25 lore raw

PATCH v3 10/11 format-patch: update append_signoff prototype

In reply to Brandon Casey

From: Nguyễn Thái Ngọc Duy <[email protected]>

This is a preparation step for merging with append_signoff from sequencer.c

---
 builtin/log.c | 13 +------------
 log-tree.c    | 17 +++++++++++++----
 revision.h    |  2 +-
 3 files changed, 15 insertions(+), 17 deletions(-)
builtin/log.c +1 -12
index 8f0b2e8..59de484 100644
@@ -1086,7 +1086,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	struct commit *origin = NULL, *head = NULL;
 	const char *in_reply_to = NULL;
 	struct patch_ids ids;
-	char *add_signoff = NULL;
 	struct strbuf buf = STRBUF_INIT;
 	int use_patch_format = 0;
 	int quiet = 0;
@@ -1193,16 +1192,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		rev.subject_prefix = strbuf_detach(&sprefix, NULL);
 	}
 
-	if (do_signoff) {
-		const char *committer;
-		const char *endpos;
-		committer = git_committer_info(IDENT_STRICT);
-		endpos = strchr(committer, '>');
-		if (!endpos)
-			die(_("bogus committer info %s"), committer);
-		add_signoff = xmemdupz(committer, endpos - committer + 1);
-	}
-
 	for (i = 0; i < extra_hdr.nr; i++) {
 		strbuf_addstr(&buf, extra_hdr.items[i].string);
 		strbuf_addch(&buf, '\n');
@@ -1393,7 +1382,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		total++;
 		start_number--;
 	}
-	rev.add_signoff = add_signoff;
+	rev.add_signoff = do_signoff;
 	while (0 <= --nr) {
 		int shown;
 		commit = list[nr];
log-tree.c +13 -4
index 5dc45c4..ac1cd68 100644
@@ -10,6 +10,8 @@
 #include "color.h"
 #include "gpg-interface.h"
 
+#define APPEND_SIGNOFF_DEDUP (1u <<0)
+
 struct decoration name_decoration = { "object names" };
 
 enum decoration_type {
@@ -253,9 +255,12 @@ static int detect_any_signoff(char *letter, int size)
 	return seen_head && seen_name;
 }
 
-static void append_signoff(struct strbuf *sb, const char *signoff)
+static void append_signoff(struct strbuf *sb, int ignore_footer, unsigned flag)
 {
+	unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
 	static const char signed_off_by[] = "Signed-off-by: ";
+	char *signoff = xstrdup(fmt_name(getenv("GIT_COMMITTER_NAME"),
+					       getenv("GIT_COMMITTER_EMAIL")));
 	size_t signoff_len = strlen(signoff);
 	int has_signoff = 0;
 	char *cp;
@@ -275,6 +280,7 @@ static void append_signoff(struct strbuf *sb, const char *signoff)
 		if (!isspace(cp[signoff_len]))
 			continue;
 		/* we already have him */
+		free(signoff);
 		return;
 	}
 
@@ -287,6 +293,7 @@ static void append_signoff(struct strbuf *sb, const char *signoff)
 	strbuf_addstr(sb, signed_off_by);
 	strbuf_add(sb, signoff, signoff_len);
 	strbuf_addch(sb, '\n');
+	free(signoff);
 }
 
 static unsigned int digits_in_number(unsigned int number)
@@ -672,8 +679,10 @@ void show_log(struct rev_info *opt)
 	/*
 	 * And then the pretty-printed message itself
 	 */
-	if (ctx.need_8bit_cte >= 0)
-		ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
+	if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
+		ctx.need_8bit_cte =
+			has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
+					       getenv("GIT_COMMITTER_EMAIL")));
 	ctx.date_mode = opt->date_mode;
 	ctx.date_mode_explicit = opt->date_mode_explicit;
 	ctx.abbrev = opt->diffopt.abbrev;
@@ -686,7 +695,7 @@ void show_log(struct rev_info *opt)
 	pretty_print_commit(&ctx, commit, &msgbuf);
 
 	if (opt->add_signoff)
-		append_signoff(&msgbuf, opt->add_signoff);
+		append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
 
 	if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
 	    ctx.notes_message && *ctx.notes_message) {
revision.h +1 -1
index 5da09ee..01bd2b7 100644
@@ -138,7 +138,7 @@ struct rev_info {
 	int		reroll_count;
 	char		*message_id;
 	struct string_list *ref_message_ids;
-	const char	*add_signoff;
+	int		add_signoff;
 	const char	*extra_headers;
 	const char	*log_reencode;
 	const char	*subject_prefix;
Signature · 1 line
1.8.1.1.450.g0327af3
Reviewed-by on v3 10/11
#26 lore raw

Re: [PATCH v3 10/11] format-patch: update append_signoff prototype

Quoted 4 lines · Brandon Casey

From: Nguyễn Thái Ngọc Duy <[email protected]>

This is a preparation step for merging with append_signoff from sequencer.c

Avoids a small unfreed allocation, too. Neat.

(On the other hand, this means more malloc churn when running "format-patch -s" on a long series of patches, but I don't think anyone will mind.)

[...]

builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1199

Quoted 10 lines
builtin/log.c +0 -4
@@ -1193,16 +1192,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		rev.subject_prefix = strbuf_detach(&sprefix, NULL);
 	}
 
-	if (do_signoff) {
-		const char *committer;
-		const char *endpos;
-		committer = git_committer_info(IDENT_STRICT);

sequencer.c uses fmt_name() which uses IDENT_STRICT, too. Phew.

#27 lore raw

PATCH v3 11/11 Unify appending signoff in format-patch, commit and sequencer

In reply to Brandon Casey

There are two implementations of append_signoff in log-tree.c and sequencer.c, which do more or less the same thing. Unify on top of the sequencer.c implementation.

Add a test in t4014 to demonstrate support for non-s-o-b elements in the commit footer provided by sequence.c:append_sob. Mark tests fixed as appropriate.

[Commit message mostly stolen from Nguyễn Thái Ngọc Duy's original
unification patch]

---
 log-tree.c              | 91 +------------------------------------------------
 t/t4014-format-patch.sh | 31 ++++++++++++++---
 2 files changed, 27 insertions(+), 95 deletions(-)
log-tree.c +1 -90
index ac1cd68..c9d9a37 100644
@@ -9,8 +9,7 @@
 #include "string-list.h"
 #include "color.h"
 #include "gpg-interface.h"
-
-#define APPEND_SIGNOFF_DEDUP (1u <<0)
+#include "sequencer.h"
 
 struct decoration name_decoration = { "object names" };
 
@@ -208,94 +207,6 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
 	putchar(')');
 }
 
-/*
- * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
- * Signed-off-by: and Acked-by: lines.
- */
-static int detect_any_signoff(char *letter, int size)
-{
-	char *cp;
-	int seen_colon = 0;
-	int seen_at = 0;
-	int seen_name = 0;
-	int seen_head = 0;
-
-	cp = letter + size;
-	while (letter <= --cp && *cp == '\n')
-		continue;
-
-	while (letter <= cp) {
-		char ch = *cp--;
-		if (ch == '\n')
-			break;
-
-		if (!seen_at) {
-			if (ch == '@')
-				seen_at = 1;
-			continue;
-		}
-		if (!seen_colon) {
-			if (ch == '@')
-				return 0;
-			else if (ch == ':')
-				seen_colon = 1;
-			else
-				seen_name = 1;
-			continue;
-		}
-		if (('A' <= ch && ch <= 'Z') ||
-		    ('a' <= ch && ch <= 'z') ||
-		    ch == '-') {
-			seen_head = 1;
-			continue;
-		}
-		/* no empty last line doesn't match */
-		return 0;
-	}
-	return seen_head && seen_name;
-}
-
-static void append_signoff(struct strbuf *sb, int ignore_footer, unsigned flag)
-{
-	unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
-	static const char signed_off_by[] = "Signed-off-by: ";
-	char *signoff = xstrdup(fmt_name(getenv("GIT_COMMITTER_NAME"),
-					       getenv("GIT_COMMITTER_EMAIL")));
-	size_t signoff_len = strlen(signoff);
-	int has_signoff = 0;
-	char *cp;
-
-	cp = sb->buf;
-
-	/* First see if we already have the sign-off by the signer */
-	while ((cp = strstr(cp, signed_off_by))) {
-
-		has_signoff = 1;
-
-		cp += strlen(signed_off_by);
-		if (cp + signoff_len >= sb->buf + sb->len)
-			break;
-		if (strncmp(cp, signoff, signoff_len))
-			continue;
-		if (!isspace(cp[signoff_len]))
-			continue;
-		/* we already have him */
-		free(signoff);
-		return;
-	}
-
-	if (!has_signoff)
-		has_signoff = detect_any_signoff(sb->buf, sb->len);
-
-	if (!has_signoff)
-		strbuf_addch(sb, '\n');
-
-	strbuf_addstr(sb, signed_off_by);
-	strbuf_add(sb, signoff, signoff_len);
-	strbuf_addch(sb, '\n');
-	free(signoff);
-}
-
 static unsigned int digits_in_number(unsigned int number)
 {
 	unsigned int i = 10, result = 1;
t/t4014-format-patch.sh +26 -5
index 3868cef..d0ec097 100755
@@ -1104,7 +1104,28 @@ EOF
 	test_cmp expected actual
 '
 
-test_expect_failure 'signoff: some random signoff-alike' '
+test_expect_success 'signoff: misc conforming footer elements' '
+	append_signoff <<\EOF >actual &&
+subject
+
+body
+
+Signed-off-by: my@house
+(cherry picked from commit da39a3ee5e6b4b0d3255bfef95601890afd80709)
+Tested-by: Some One <[email protected]>
+Bug: 1234
+EOF
+	cat >expected <<\EOF &&
+4:Subject: [PATCH] subject
+8:
+10:
+11:Signed-off-by: my@house
+15:Signed-off-by: C O Mitter <[email protected]>
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'signoff: some random signoff-alike' '
 	append_signoff <<\EOF >actual &&
 subject
 
@@ -1120,7 +1141,7 @@ EOF
 	test_cmp expected actual
 '
 
-test_expect_failure 'signoff: not really a signoff' '
+test_expect_success 'signoff: not really a signoff' '
 	append_signoff <<\EOF >actual &&
 subject
 
@@ -1136,7 +1157,7 @@ EOF
 	test_cmp expected actual
 '
 
-test_expect_failure 'signoff: not really a signoff (2)' '
+test_expect_success 'signoff: not really a signoff (2)' '
 	append_signoff <<\EOF >actual &&
 subject
 
@@ -1153,7 +1174,7 @@ EOF
 	test_cmp expected actual
 '
 
-test_expect_failure 'signoff: valid S-o-b paragraph in the middle' '
+test_expect_success 'signoff: valid S-o-b paragraph in the middle' '
 	append_signoff <<\EOF >actual &&
 subject
 
@@ -1221,7 +1242,7 @@ EOF
 	test_cmp expected actual
 '
 
-test_expect_failure 'signoff: detect garbage in non-conforming footer' '
+test_expect_success 'signoff: detect garbage in non-conforming footer' '
 	append_signoff <<\EOF >actual &&
 subject
 
Signature · 1 line
1.8.1.1.450.g0327af3
#28 lore raw

Re: [PATCH v3 11/11] Unify appending signoff in format-patch, commit and sequencer

Quoted 2 lines · Brandon Casey
log-tree.c +0 -0

[...]

log-tree.c void show_decorations(struct rev_info *opt, struct commit *commit) · line 214

Quoted 8 lines
log-tree.c +0 -4
@@ -208,94 +207,6 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
 	putchar(')');
 }
 
-/*
- * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
- * Signed-off-by: and Acked-by: lines.
- */

That's stricter than the test from sequencer.c. Maybe it's worth stealing to avoid false positives?

#29 lore raw

Re: [PATCH v3 11/11] Unify appending signoff in format-patch, commit and sequencer

Quoted 9 lines · Jonathan Nieder
Quoted 2 lines · Brandon Casey
log-tree.c +0 -0

[...]

Quoted 4 lines
log-tree.c +0 -0
@@ -208,94 +207,6 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
      putchar(')');
 }
 
Quoted 7 lines

log-tree.c void show_decorations(struct rev_info *opt, struct commit *commit) · line 214

Quoted 4 lines
log-tree.c +0 -4
-/*
- * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
- * Signed-off-by: and Acked-by: lines.
- */

That's stricter than the test from sequencer.c. Maybe it's worth stealing to avoid false positives?

No, we don't want this stricter test because it assumes that the right-hand side of "[-A-Za-z]+:" will be an email address, so it requires an '@' to exist. We want to be able to support lines that do not have email addresses on the right-hand side like:

Bug: XXX
Change-Id: XXX

and perhaps eventually

Cherry-picked-from: XXX

The current series has retained the same basic test for an rfc2822-like line that existed in sequencer.c and would interpret a line that contains only a colon as conforming. A follow-on patch could require that at least one character precede the colon, but that would be a change in behavior that is not the goal of this series.

-Brandon

Reviewed-by someone
#30 lore raw

Re: [PATCH v3 11/11] Unify appending signoff in format-patch, commit and sequencer

Quoted 5 lines · Brandon Casey

We want to be able to support lines that do
not have email addresses on the right-hand side like:

Bug: XXX
Change-Id: XXX

Good call.

By the way, regarding what the right "--signoff" behavior is for commit, cherry-pick, am, and format-patch:

I think the best behavior would be to check if the last signed-off-by line (ignoring acked-by, bug, change-id, and so on lines that follow it) matches the one to be added, and if it doesn't, add a new sign-off. That way, the sign-off list still would accurately describe the path of the patch, without silliness like

Signed-off-by me
Reviewed-by someone
Signed-off-by me

that you mentioned.

I agree that that's orthogonal to this series and just mostly preserving behavior (as you already do) is the right thing to do.

Thanks for noticing the edge cases.

Jonathan

#31 lore raw

Re: [PATCH v3 11/11] Unify appending signoff in format-patch, commit and sequencer

Quoted 7 lines · Jonathan Nieder

By the way, regarding what the right "--signoff" behavior is for commit, cherry-pick, am, and format-patch:

I think the best behavior would be to check if the last signed-off-by line (ignoring acked-by, bug, change-id, and so on lines that follow it) matches the one to be added, and if it doesn't, add a new sign-off.

This makes a lot of sense.

-Brandon

#32 lore raw
In reply to Brandon Casey
Quoted 1 line · Brandon Casey

Round 3.

Thanks for a pleasant read. My only remaining observations are cosmetic, except for a portability question in Duy's test script, a small behavior change when the commit message ends with an RFC2822-style header with no trailing newline and the possibility of tightening the pattern in sequencer.c to match the strictness of format-patch (which could easily wait for a later patch).

Jonathan

#33 lore raw
Quoted 4 lines · Jonathan Nieder
Quoted 1 line · Brandon Casey

Round 3.

Quoted 8 lines

Thanks for a pleasant read. My only remaining observations are cosmetic, except for a portability question in Duy's test script, a small behavior change when the commit message ends with an RFC2822-style header with no trailing newline and the possibility of tightening the pattern in sequencer.c to match the strictness of format-patch (which could easily wait for a later patch).

Jonathan

Thanks for a quick review. I agree that this series is getting very close with your help.

#34 lore raw
Quoted 13 lines · Junio C Hamano
Quoted 10 lines · Jonathan Nieder
Quoted 1 line · Brandon Casey

Round 3.

Thanks for a pleasant read. My only remaining observations are cosmetic, except for a portability question in Duy's test script, a small behavior change when the commit message ends with an RFC2822-style header with no trailing newline and the possibility of tightening the pattern in sequencer.c to match the strictness of format-patch (which could easily wait for a later patch).

Quoted 2 lines

Thanks for a quick review. I agree that this series is getting very close with your help.

Unless Brandon and/or Jonathan wants to have another chance to excise warts from the recorded history by rerolling the entire series one more time, I think what we have queued is in a good enough shape to merge to 'next' and any further improvement and fix can be done incrementally.

OK? Or "stop, I want to reroll"?

I'll wait for a day or two.

#35 lore raw
Quoted 24 lines · Junio C Hamano
Quoted 13 lines · Junio C Hamano
Quoted 10 lines · Jonathan Nieder
Quoted 1 line · Brandon Casey

Round 3.

Thanks for a pleasant read. My only remaining observations are cosmetic, except for a portability question in Duy's test script, a small behavior change when the commit message ends with an RFC2822-style header with no trailing newline and the possibility of tightening the pattern in sequencer.c to match the strictness of format-patch (which could easily wait for a later patch).

Quoted 2 lines

Thanks for a quick review. I agree that this series is getting very close with your help.

Unless Brandon and/or Jonathan wants to have another chance to excise warts from the recorded history by rerolling the entire series one more time, I think what we have queued is in a good enough shape to merge to 'next' and any further improvement and fix can be done incrementally.

Quoted 3 lines

OK? Or "stop, I want to reroll"?

I'll wait for a day or two.

Let's hold off so I can do another round. I worked on this last night and was able to simplify some things nicely. I'll try to finish up tonight and resubmit.

-Brandon