{"thread":{"id":"24108","subject":"[PATCH] pretty: Introduce ' ' modifier to add space if non-empty","startedAt":"2010-06-14T16:12:29Z","lastAt":"2010-06-14T16:12:29Z","messageCount":1,"participants":["Michael J Gruber"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"143680","messageId":"4bb622edfd80b83fc5bd68d383887aec260a0b88.1276531906.git.git@drmicha.warpmail.net","threadId":"24108","inReplyTo":null,"subject":"[PATCH] pretty: Introduce ' ' modifier to add space if non-empty","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2010-06-14T16:12:29Z","receivedAt":"2010-06-14T16:12:29Z","isPatch":true,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"We have the '+' modifiier which helps combine format specifiers which\nmay possibly be empty, e.g. '%s%+b%n'.\n\nIntroduce an analogous ' ' (space) modifier which adds a space before\nnon-empty items. This helps assemble \"one line type\" format specifiers.\n\nSigned-off-by: Michael J Gruber <git@drmicha.warpmail.net>\n---\n Documentation/pretty-formats.txt |    4 ++++\n pretty.c                         |   13 ++++++++++---\n t/t6006-rev-list-format.sh       |   10 ++++++++++\n 3 files changed, 24 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt\nindex 8c68ce9..561cc9f 100644\n--- a/Documentation/pretty-formats.txt\n+++ b/Documentation/pretty-formats.txt\n@@ -159,6 +159,10 @@ If you add a `-` (minus sign) after '%' of a placeholder, line-feeds that\n immediately precede the expansion are deleted if and only if the\n placeholder expands to an empty string.\n \n+If you add a ` ` (space) after '%' of a placeholder, a space\n+is inserted immediately before the expansion if and only if the\n+placeholder expands to a non-empty string.\n+\n * 'tformat:'\n +\n The 'tformat:' format works exactly like 'format:', except that it\ndiff --git a/pretty.c b/pretty.c\nindex 146320f..8b2fc71 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -946,6 +946,7 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,\n \t\tNO_MAGIC,\n \t\tADD_LF_BEFORE_NON_EMPTY,\n \t\tDEL_LF_BEFORE_EMPTY,\n+\t\tADD_SP_BEFORE_NON_EMPTY,\n \t} magic = NO_MAGIC;\n \n \tswitch (placeholder[0]) {\n@@ -955,6 +956,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,\n \tcase '+':\n \t\tmagic = ADD_LF_BEFORE_NON_EMPTY;\n \t\tbreak;\n+\tcase ' ':\n+\t\tmagic = ADD_SP_BEFORE_NON_EMPTY;\n+\t\tbreak;\n \tdefault:\n \t\tbreak;\n \t}\n@@ -969,8 +973,11 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,\n \tif ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {\n \t\twhile (sb->len && sb->buf[sb->len - 1] == '\\n')\n \t\t\tstrbuf_setlen(sb, sb->len - 1);\n-\t} else if ((orig_len != sb->len) && magic == ADD_LF_BEFORE_NON_EMPTY) {\n-\t\tstrbuf_insert(sb, orig_len, \"\\n\", 1);\n+\t} else if (orig_len != sb->len) {\n+\t\tif (magic == ADD_LF_BEFORE_NON_EMPTY)\n+\t\t\tstrbuf_insert(sb, orig_len, \"\\n\", 1);\n+\t\telse if (magic == ADD_SP_BEFORE_NON_EMPTY)\n+\t\t\tstrbuf_insert(sb, orig_len, \" \", 1);\n \t}\n \treturn consumed + 1;\n }\n@@ -980,7 +987,7 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,\n {\n \tstruct userformat_want *w = context;\n \n-\tif (*placeholder == '+' || *placeholder == '-')\n+\tif (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')\n \t\tplaceholder++;\n \n \tswitch (*placeholder) {\ndiff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh\nindex 9b77073..cccacd4 100755\n--- a/t/t6006-rev-list-format.sh\n+++ b/t/t6006-rev-list-format.sh\n@@ -200,6 +200,16 @@ test_expect_success 'add LF before non-empty (2)' '\n \tgrep \"^$\" actual\n '\n \n+test_expect_success 'add SP before non-empty (1)' '\n+\tgit show -s --pretty=format:\"%s% bThanks\" HEAD^^ >actual &&\n+\ttest $(wc -w <actual) = 2\n+'\n+\n+test_expect_success 'add SP before non-empty (2)' '\n+\tgit show -s --pretty=format:\"%s% sThanks\" HEAD^^ >actual &&\n+\ttest $(wc -w <actual) = 4\n+'\n+\n test_expect_success '--abbrev' '\n \techo SHORT SHORT SHORT >expect2 &&\n \techo LONG LONG LONG >expect3 &&\n-- \n1.7.1.511.gfbed4\n"}]}