@matheusbsbs / matheus-bagliano / commits / 7b88176e9b
Fork 0

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

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael J Gruber committed Jun 14, 2010 at 18:12 UTC 7b88176e9bce658fd44c2192c1b7aa6e612ee0c2
3 files changed +24 -3
Documentation/pretty-formats.txt
+4
@@ -159,6 +159,10 @@ If you add a `-` (minus sign) after '%' of a placeholder, line-feeds that
159 immediately precede the expansion are deleted if and only if the
160 placeholder expands to an empty string.
161
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 +
166 * 'tformat:'
167 +
168 The 'tformat:' format works exactly like 'format:', except that it
pretty.c
+10 -3
@@ -942,6 +942,7 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
942 NO_MAGIC,
943 ADD_LF_BEFORE_NON_EMPTY,
944 DEL_LF_BEFORE_EMPTY,
945 + ADD_SP_BEFORE_NON_EMPTY,
946 } magic = NO_MAGIC;
947
948 switch (placeholder[0]) {
@@ -951,6 +952,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
952 case '+':
953 magic = ADD_LF_BEFORE_NON_EMPTY;
954 break;
955 + case ' ':
956 + magic = ADD_SP_BEFORE_NON_EMPTY;
957 + break;
958 default:
959 break;
960 }
@@ -965,8 +969,11 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
969 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
970 while (sb->len && sb->buf[sb->len - 1] == '\n')
971 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);
977 }
978 return consumed + 1;
979 }
@@ -976,7 +983,7 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
983 {
984 struct userformat_want *w = context;
985
979 - if (*placeholder == '+' || *placeholder == '-')
986 + if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
987 placeholder++;
988
989 switch (*placeholder) {
t/t6006-rev-list-format.sh
+10
@@ -200,6 +200,16 @@ test_expect_success 'add LF before non-empty (2)' '
200 grep "^$" actual
201 '
202
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 +
213 test_expect_success '--abbrev' '
214 echo SHORT SHORT SHORT >expect2 &&
215 echo LONG LONG LONG >expect3 &&