Skip to content

Commit 223a923

Browse files
committed
Merge branch 'mg/pretty-magic-space'
* mg/pretty-magic-space: pretty: Introduce ' ' modifier to add space if non-empty Conflicts: pretty.c
2 parents 5bfd536 + 7b88176 commit 223a923

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Documentation/pretty-formats.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ If you add a `-` (minus sign) after '%' of a placeholder, line-feeds that
159159
immediately precede the expansion are deleted if and only if the
160160
placeholder expands to an empty string.
161161

162+
If you add a ` ` (space) after '%' of a placeholder, a space
163+
is inserted immediately before the expansion if and only if the
164+
placeholder expands to a non-empty string.
165+
162166
* 'tformat:'
163167
+
164168
The 'tformat:' format works exactly like 'format:', except that it

pretty.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,8 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
941941
enum {
942942
NO_MAGIC,
943943
ADD_LF_BEFORE_NON_EMPTY,
944-
DEL_LF_BEFORE_EMPTY
944+
DEL_LF_BEFORE_EMPTY,
945+
ADD_SP_BEFORE_NON_EMPTY
945946
} magic = NO_MAGIC;
946947

947948
switch (placeholder[0]) {
@@ -951,6 +952,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
951952
case '+':
952953
magic = ADD_LF_BEFORE_NON_EMPTY;
953954
break;
955+
case ' ':
956+
magic = ADD_SP_BEFORE_NON_EMPTY;
957+
break;
954958
default:
955959
break;
956960
}
@@ -965,8 +969,11 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
965969
if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
966970
while (sb->len && sb->buf[sb->len - 1] == '\n')
967971
strbuf_setlen(sb, sb->len - 1);
968-
} else if ((orig_len != sb->len) && magic == ADD_LF_BEFORE_NON_EMPTY) {
969-
strbuf_insert(sb, orig_len, "\n", 1);
972+
} else if (orig_len != sb->len) {
973+
if (magic == ADD_LF_BEFORE_NON_EMPTY)
974+
strbuf_insert(sb, orig_len, "\n", 1);
975+
else if (magic == ADD_SP_BEFORE_NON_EMPTY)
976+
strbuf_insert(sb, orig_len, " ", 1);
970977
}
971978
return consumed + 1;
972979
}
@@ -976,7 +983,7 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
976983
{
977984
struct userformat_want *w = context;
978985

979-
if (*placeholder == '+' || *placeholder == '-')
986+
if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
980987
placeholder++;
981988

982989
switch (*placeholder) {

t/t6006-rev-list-format.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ test_expect_success 'add LF before non-empty (2)' '
200200
grep "^$" actual
201201
'
202202

203+
test_expect_success 'add SP before non-empty (1)' '
204+
git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
205+
test $(wc -w <actual) = 2
206+
'
207+
208+
test_expect_success 'add SP before non-empty (2)' '
209+
git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
210+
test $(wc -w <actual) = 4
211+
'
212+
203213
test_expect_success '--abbrev' '
204214
echo SHORT SHORT SHORT >expect2 &&
205215
echo LONG LONG LONG >expect3 &&

0 commit comments

Comments
 (0)