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

[PATCH] Pretty-format: Ability to add newline after non-empty string

From
Mmatni403@gmail.com <matni403@gmail.com>
Date
Feb 7, 2019, 06:39 UTC
Message-ID
<20190207063957.11052-1-matni403@gmail.com>
From: Mats Nilsson <matni403@gmail.com>

This allows for expansion of %*x to %x followed by a LF if and only if %x is non-empty.

Signed-off-by: Mats Nilsson <matni403@gmail.com>
---
 Documentation/pretty-formats.txt |  4 ++++
 pretty.c                         | 11 +++++++++--
 t/t6006-rev-list-format.sh       |  5 +++++
 3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index de6953108c..cddd21e27e 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -236,6 +236,10 @@ If you add a ` ` (space) after '%' of a placeholder, a space
 is inserted immediately before the expansion if and only if the
 placeholder expands to a non-empty string.
 
+If you add a '*' (star) after '%' of a placeholder, a line-feed
+is added immediately after the expansion if and only if the
+placeholder expands to a non-empty string.
+
 * 'tformat:'
 +
 The 'tformat:' format works exactly like 'format:', except that it
diff --git a/pretty.c b/pretty.c
index 0ab45d10d7..fedea05acc 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1457,7 +1457,8 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
 		NO_MAGIC,
 		ADD_LF_BEFORE_NON_EMPTY,
 		DEL_LF_BEFORE_EMPTY,
-		ADD_SP_BEFORE_NON_EMPTY
+		ADD_SP_BEFORE_NON_EMPTY,
+		ADD_LF_AFTER_NON_EMPTY
 	} magic = NO_MAGIC;
 
 	switch (placeholder[0]) {
@@ -1470,6 +1471,9 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
 	case ' ':
 		magic = ADD_SP_BEFORE_NON_EMPTY;
 		break;
+	case '*':
+		magic = ADD_LF_AFTER_NON_EMPTY;
+		break;
 	default:
 		break;
 	}
@@ -1492,6 +1496,8 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
 			strbuf_insert(sb, orig_len, "\n", 1);
 		else if (magic == ADD_SP_BEFORE_NON_EMPTY)
 			strbuf_insert(sb, orig_len, " ", 1);
+		else if (magic == ADD_LF_AFTER_NON_EMPTY)
+			strbuf_addstr(sb, "\n");
 	}
 	return consumed + 1;
 }
@@ -1501,7 +1507,8 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
 {
 	struct userformat_want *w = context;
 
-	if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
+	if (*placeholder == '+' || *placeholder == '-' ||
+		*placeholder == ' ' || *placeholder == '*')
 		placeholder++;
 
 	switch (*placeholder) {
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
index da113d975b..e333ed91d3 100755
--- a/t/t6006-rev-list-format.sh
+++ b/t/t6006-rev-list-format.sh
@@ -445,6 +445,11 @@ test_expect_success 'add SP before non-empty (2)' '
 	test $(wc -w <actual) = 6
 '
 
+test_expect_success 'add LF after non-empty' '
+	git show -s --pretty=format:"%s%*sThanks" HEAD^^ >actual &&
+	test_line_count = 2 actual
+'
+
 test_expect_success '--abbrev' '
 	echo SHORT SHORT SHORT >expect2 &&
 	echo LONG LONG LONG >expect3 &&
-- 
2.14.1.windows.1
Next: Junio C Hamano
Message 1 of 3 in “Pretty-format: Ability to add newline after non-empty string”
  1. Pretty-format: Ability to add newline after non-empty stringmatni403@gmail.com, Feb 7, 2019
  2. Junio C HamanoFeb 7, 2019
  3. Mats NilssonFeb 13, 2019

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.