Skip to content

Commit 684d0d8

Browse files
committed
Merge branch 'jc/pretty-lf'
Conflicts: pretty.c t/t6006-rev-list-format.sh
2 parents 261fbda + 9fa708d commit 684d0d8

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

Documentation/pretty-formats.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ insert an empty string unless we are traversing reflog entries (e.g., by
144144
`git log -g`). The `%d` placeholder will use the "short" decoration
145145
format if `--decorate` was not already provided on the command line.
146146

147+
If you add a `{plus}` (plus sign) after '%' of a placeholder, a line-feed
148+
is inserted immediately before the expansion if and only if the
149+
placeholder expands to a non-empty string.
150+
151+
If you add a `-` (minus sign) after '%' of a placeholder, line-feeds that
152+
immediately precede the expansion are deleted if and only if the
153+
placeholder expands to an empty string.
154+
147155
* 'tformat:'
148156
+
149157
The 'tformat:' format works exactly like 'format:', except that it

pretty.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ static void rewrap_message_tail(struct strbuf *sb,
628628
c->indent2 = new_indent2;
629629
}
630630

631-
static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
632-
void *context)
631+
static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
632+
void *context)
633633
{
634634
struct format_commit_context *c = context;
635635
const struct commit *commit = c->commit;
@@ -816,6 +816,44 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
816816
return 0; /* unknown placeholder */
817817
}
818818

819+
static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
820+
void *context)
821+
{
822+
int consumed;
823+
size_t orig_len;
824+
enum {
825+
NO_MAGIC,
826+
ADD_LF_BEFORE_NON_EMPTY,
827+
DEL_LF_BEFORE_EMPTY,
828+
} magic = NO_MAGIC;
829+
830+
switch (placeholder[0]) {
831+
case '-':
832+
magic = DEL_LF_BEFORE_EMPTY;
833+
break;
834+
case '+':
835+
magic = ADD_LF_BEFORE_NON_EMPTY;
836+
break;
837+
default:
838+
break;
839+
}
840+
if (magic != NO_MAGIC)
841+
placeholder++;
842+
843+
orig_len = sb->len;
844+
consumed = format_commit_one(sb, placeholder, context);
845+
if (magic == NO_MAGIC)
846+
return consumed;
847+
848+
if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
849+
while (sb->len && sb->buf[sb->len - 1] == '\n')
850+
strbuf_setlen(sb, sb->len - 1);
851+
} else if ((orig_len != sb->len) && magic == ADD_LF_BEFORE_NON_EMPTY) {
852+
strbuf_insert(sb, orig_len, "\n", 1);
853+
}
854+
return consumed + 1;
855+
}
856+
819857
void format_commit_message(const struct commit *commit,
820858
const char *format, struct strbuf *sb,
821859
const struct pretty_print_context *pretty_ctx)

t/t6006-rev-list-format.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,28 @@ test_expect_success 'empty email' '
162162
}
163163
'
164164

165+
test_expect_success 'del LF before empty (1)' '
166+
git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
167+
test $(wc -l <actual) = 2
168+
'
169+
170+
test_expect_success 'del LF before empty (2)' '
171+
git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
172+
test $(wc -l <actual) = 6 &&
173+
grep "^$" actual
174+
'
175+
176+
test_expect_success 'add LF before non-empty (1)' '
177+
git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
178+
test $(wc -l <actual) = 2
179+
'
180+
181+
test_expect_success 'add LF before non-empty (2)' '
182+
git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
183+
test $(wc -l <actual) = 6 &&
184+
grep "^$" actual
185+
'
186+
165187
test_expect_success '"%h %gD: %gs" is same as git-reflog' '
166188
git reflog >expect &&
167189
git log -g --format="%h %gD: %gs" >actual &&

0 commit comments

Comments
 (0)