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

[PATCH] pretty: Introduce ' ' modifier to add space if non-empty

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Jun 14, 2010, 16:12 UTC
Message-ID
<4bb622edfd80b83fc5bd68d383887aec260a0b88.1276531906.git.git@drmicha.warpmail.net>

We have the '+' modifiier which helps combine format specifiers which may possibly be empty, e.g. '%s%+b%n'.

Introduce an analogous ' ' (space) modifier which adds a space before non-empty items. This helps assemble "one line type" format specifiers.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 Documentation/pretty-formats.txt |    4 ++++
 pretty.c                         |   13 ++++++++++---
 t/t6006-rev-list-format.sh       |   10 ++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 8c68ce9..561cc9f 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -159,6 +159,10 @@ If you add a `-` (minus sign) after '%' of a placeholder, line-feeds that
 immediately precede the expansion are deleted if and only if the
 placeholder expands to an empty string.
 
+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.
+
 * 'tformat:'
 +
 The 'tformat:' format works exactly like 'format:', except that it
diff --git a/pretty.c b/pretty.c
index 146320f..8b2fc71 100644
--- a/pretty.c
+++ b/pretty.c
@@ -946,6 +946,7 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 		NO_MAGIC,
 		ADD_LF_BEFORE_NON_EMPTY,
 		DEL_LF_BEFORE_EMPTY,
+		ADD_SP_BEFORE_NON_EMPTY,
 	} magic = NO_MAGIC;
 
 	switch (placeholder[0]) {
@@ -955,6 +956,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 	case '+':
 		magic = ADD_LF_BEFORE_NON_EMPTY;
 		break;
+	case ' ':
+		magic = ADD_SP_BEFORE_NON_EMPTY;
+		break;
 	default:
 		break;
 	}
@@ -969,8 +973,11 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 	if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
 		while (sb->len && sb->buf[sb->len - 1] == '\n')
 			strbuf_setlen(sb, sb->len - 1);
-	} else if ((orig_len != sb->len) && magic == ADD_LF_BEFORE_NON_EMPTY) {
-		strbuf_insert(sb, orig_len, "\n", 1);
+	} else if (orig_len != sb->len) {
+		if (magic == ADD_LF_BEFORE_NON_EMPTY)
+			strbuf_insert(sb, orig_len, "\n", 1);
+		else if (magic == ADD_SP_BEFORE_NON_EMPTY)
+			strbuf_insert(sb, orig_len, " ", 1);
 	}
 	return consumed + 1;
 }
@@ -980,7 +987,7 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
 {
 	struct userformat_want *w = context;
 
-	if (*placeholder == '+' || *placeholder == '-')
+	if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
 		placeholder++;
 
 	switch (*placeholder) {
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
index 9b77073..cccacd4 100755
--- a/t/t6006-rev-list-format.sh
+++ b/t/t6006-rev-list-format.sh
@@ -200,6 +200,16 @@ test_expect_success 'add LF before non-empty (2)' '
 	grep "^$" actual
 '
 
+test_expect_success 'add SP before non-empty (1)' '
+	git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
+	test $(wc -w <actual) = 2
+'
+
+test_expect_success 'add SP before non-empty (2)' '
+	git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
+	test $(wc -w <actual) = 4
+'
+
 test_expect_success '--abbrev' '
 	echo SHORT SHORT SHORT >expect2 &&
 	echo LONG LONG LONG >expect3 &&
-- 
1.7.1.511.gfbed4
Message 1 of 1 in “pretty: Introduce ' ' modifier to add space if non-empty”
  1. pretty: Introduce ' ' modifier to add space if non-emptyMichael J Gruber, Jun 14, 2010

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.