git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v10 12/12] trailer: add blank line before the trailers if needed

From
Christian Couder <chriscool@tuxfamily.org>
Date
Apr 6, 2014, 17:02 UTC
Message-ID
<20140406170204.15116.15100.chriscool@tuxfamily.org>
In-Reply-To
<20140406163214.15116.91484.chriscool@tuxfamily.org>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t7513-interpret-trailers.sh | 12 +++++++++++-
 trailer.c                     | 26 ++++++++++++++++++--------
 2 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
index 262f7bf..44a7131 100755
--- a/t/t7513-interpret-trailers.sh
+++ b/t/t7513-interpret-trailers.sh
@@ -34,6 +34,7 @@ test_expect_success 'setup' '
 
 test_expect_success 'without config' '
 	sed -e "s/ Z\$/ /" >expected <<-\EOF &&
+
 		ack: Peff
 		Reviewed-by: Z
 		Acked-by: Johan
@@ -44,6 +45,7 @@ test_expect_success 'without config' '
 
 test_expect_success '--trim-empty without config' '
 	cat >expected <<-\EOF &&
+
 		ack: Peff
 		Acked-by: Johan
 	EOF
@@ -55,6 +57,7 @@ test_expect_success '--trim-empty without config' '
 test_expect_success 'with config setup' '
 	git config trailer.ack.key "Acked-by: " &&
 	cat >expected <<-\EOF &&
+
 		Acked-by: Peff
 	EOF
 	git interpret-trailers --trim-empty "ack = Peff" >actual &&
@@ -68,6 +71,7 @@ test_expect_success 'with config setup' '
 test_expect_success 'with config setup and = sign' '
 	git config trailer.ack.key "Acked-by= " &&
 	cat >expected <<-\EOF &&
+
 		Acked-by= Peff
 	EOF
 	git interpret-trailers --trim-empty "ack = Peff" >actual &&
@@ -81,6 +85,7 @@ test_expect_success 'with config setup and = sign' '
 test_expect_success 'with config setup and # sign' '
 	git config trailer.bug.key "Bug #" &&
 	cat >expected <<-\EOF &&
+
 		Bug #42
 	EOF
 	git interpret-trailers --trim-empty "bug = 42" >actual &&
@@ -88,8 +93,10 @@ test_expect_success 'with config setup and # sign' '
 '
 
 test_expect_success 'with commit basic message' '
+	cat basic_message >expected &&
+	echo >>expected &&
 	git interpret-trailers <basic_message >actual &&
-	test_cmp basic_message actual
+	test_cmp expected actual
 '
 
 test_expect_success 'with commit complex message' '
@@ -436,6 +443,7 @@ test_expect_success 'with failing command using $ARG' '
 
 test_expect_success 'with empty tokens' '
 	cat >expected <<-EOF &&
+
 		Signed-off-by: A U Thor <author@example.com>
 	EOF
 	git interpret-trailers ":" ":test" >actual <<-EOF &&
@@ -446,6 +454,7 @@ test_expect_success 'with empty tokens' '
 test_expect_success 'with command but no key' '
 	git config --unset trailer.sign.key &&
 	cat >expected <<-EOF &&
+
 		sign: A U Thor <author@example.com>
 	EOF
 	git interpret-trailers >actual <<-EOF &&
@@ -456,6 +465,7 @@ test_expect_success 'with command but no key' '
 test_expect_success 'with no command and no key' '
 	git config --unset trailer.review.key &&
 	cat >expected <<-EOF &&
+
 		review: Junio
 		sign: A U Thor <author@example.com>
 	EOF
diff --git a/trailer.c b/trailer.c
index 09db2c2..639f657 100644
--- a/trailer.c
+++ b/trailer.c
@@ -618,12 +618,14 @@ static struct strbuf **read_stdin(void)
 }
 
 /*
- * Return the the (0 based) index of the first trailer line
+ * Return the (0 based) index of the first trailer line
  * or the line count if there are no trailers.
+ * The has_blank_line parameter tells if there is a blank
+ * line before the trailers.
  */
-static int find_trailer_start(struct strbuf **lines)
+static int find_trailer_start(struct strbuf **lines, int *has_blank_line)
 {
-	int start, empty = 1, count = 0;
+	int start, only_spaces = 1, count = 0;
 
 	/* Get the line count */
 	while (lines[count])
@@ -635,32 +637,40 @@ static int find_trailer_start(struct strbuf **lines)
 	 */
 	for (start = count - 1; start >= 0; start--) {
 		if (contains_only_spaces(lines[start]->buf)) {
-			if (empty)
+			if (only_spaces)
 				continue;
+			*has_blank_line = 1;
 			return start + 1;
 		}
 		if (strchr(lines[start]->buf, ':')) {
-			if (empty)
-				empty = 0;
+			if (only_spaces)
+				only_spaces = 0;
 			continue;
 		}
+		*has_blank_line = start == count - 1 ?
+		  0 : contains_only_spaces(lines[start + 1]->buf);
 		return count;
 	}
 
-	return empty ? count : start + 1;
+	*has_blank_line = only_spaces ? count > 0 : 0;
+	return only_spaces ? count : start + 1;
 }
 
 static void process_stdin(struct trailer_item **in_tok_first,
 			  struct trailer_item **in_tok_last)
 {
 	struct strbuf **lines = read_stdin();
-	int start = find_trailer_start(lines);
+	int has_blank_line;
+	int start = find_trailer_start(lines, &has_blank_line);
 	int i;
 
 	/* Print non trailer lines as is */
 	for (i = 0; lines[i] && i < start; i++)
 		printf("%s", lines[i]->buf);
 
+	if (!has_blank_line)
+		printf("\n");
+
 	/* Parse trailer lines */
 	for (i = start; lines[i]; i++) {
 		struct trailer_item *new = create_trailer_item(lines[i]->buf);
-- 
1.9.0.163.g8ca203c
Previous: Jeremy MortonNext: Junio C Hamano
Message 31 of 33 in “Add interpret-trailers builtin”
  1. 00/12 Add interpret-trailers builtinChristian Couder, Apr 6, 2014
  2. 01/12 trailer: add data structures and basic functionsChristian Couder, Apr 6, 2014
  3. 02/12 trailer: process trailers from stdin and argumentsChristian Couder, Apr 6, 2014
  4. 03/12 trailer: read and process config informationChristian Couder, Apr 6, 2014
  5. 04/12 trailer: process command line trailer argumentsChristian Couder, Apr 6, 2014
  6. 05/12 trailer: parse trailers from stdinChristian Couder, Apr 6, 2014
  7. 06/12 trailer: put all the processing together and printChristian Couder, Apr 6, 2014
  8. 07/12 trailer: add interpret-trailers commandChristian Couder, Apr 6, 2014
  9. 08/12 trailer: add tests for "git interpret-trailers"Christian Couder, Apr 6, 2014
  10. 09/12 trailer: execute command from 'trailer.<name>.command'Christian Couder, Apr 6, 2014
  11. 10/12 trailer: add tests for commands in config fileChristian Couder, Apr 6, 2014
  12. 11/12 Documentation: add documentation for 'git interpret-trailers'Christian Couder, Apr 6, 2014
  13. Michael HaggertyApr 8, 2014
  14. Christian CouderApr 8, 2014
  15. Michael HaggertyApr 8, 2014
  16. Christian CouderApr 25, 2014
  17. Michael HaggertyApr 28, 2014
  18. Christian CouderMay 25, 2014
  19. Michael HaggertyMay 27, 2014
  20. Johan HerlandMay 27, 2014
  21. Junio C HamanoMay 27, 2014
  22. Junio C HamanoApr 8, 2014
  23. Junio C HamanoApr 8, 2014
  24. Christian CouderApr 25, 2014
  25. Junio C HamanoApr 28, 2014
  26. Jeremy MortonApr 29, 2014
  27. Christian CouderApr 29, 2014
  28. Jeremy MortonApr 29, 2014
  29. Christian CouderMay 1, 2014
  30. Jeremy MortonApr 29, 2014
  31. 12/12 trailer: add blank line before the trailers if neededChristian Couder, Apr 6, 2014
  32. Junio C HamanoApr 7, 2014
  33. Christian CouderApr 8, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.