Skip to content

Commit

Permalink
Merge branch 'jc/pretty-lf'
Browse files Browse the repository at this point in the history
Conflicts:
	pretty.c
	t/t6006-rev-list-format.sh
  • Loading branch information
Junio C Hamano committed Nov 30, 2009
2 parents 261fbda + 9fa708d commit 684d0d8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Documentation/pretty-formats.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ insert an empty string unless we are traversing reflog entries (e.g., by
`git log -g`). The `%d` placeholder will use the "short" decoration
format if `--decorate` was not already provided on the command line.

If you add a `{plus}` (plus sign) after '%' of a placeholder, a line-feed
is inserted immediately before the expansion if and only if the
placeholder expands to a non-empty string.

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.

* 'tformat:'
+
The 'tformat:' format works exactly like 'format:', except that it
Expand Down
42 changes: 40 additions & 2 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ static void rewrap_message_tail(struct strbuf *sb,
c->indent2 = new_indent2;
}

static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
void *context)
static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
void *context)
{
struct format_commit_context *c = context;
const struct commit *commit = c->commit;
Expand Down Expand Up @@ -816,6 +816,44 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
return 0; /* unknown placeholder */
}

static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
void *context)
{
int consumed;
size_t orig_len;
enum {
NO_MAGIC,
ADD_LF_BEFORE_NON_EMPTY,
DEL_LF_BEFORE_EMPTY,
} magic = NO_MAGIC;

switch (placeholder[0]) {
case '-':
magic = DEL_LF_BEFORE_EMPTY;
break;
case '+':
magic = ADD_LF_BEFORE_NON_EMPTY;
break;
default:
break;
}
if (magic != NO_MAGIC)
placeholder++;

orig_len = sb->len;
consumed = format_commit_one(sb, placeholder, context);
if (magic == NO_MAGIC)
return consumed;

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);
}
return consumed + 1;
}

void format_commit_message(const struct commit *commit,
const char *format, struct strbuf *sb,
const struct pretty_print_context *pretty_ctx)
Expand Down
22 changes: 22 additions & 0 deletions t/t6006-rev-list-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ test_expect_success 'empty email' '
}
'

test_expect_success 'del LF before empty (1)' '
git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
test $(wc -l <actual) = 2
'

test_expect_success 'del LF before empty (2)' '
git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
test $(wc -l <actual) = 6 &&
grep "^$" actual
'

test_expect_success 'add LF before non-empty (1)' '
git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
test $(wc -l <actual) = 2
'

test_expect_success 'add LF before non-empty (2)' '
git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
test $(wc -l <actual) = 6 &&
grep "^$" actual
'

test_expect_success '"%h %gD: %gs" is same as git-reflog' '
git reflog >expect &&
git log -g --format="%h %gD: %gs" >actual &&
Expand Down

0 comments on commit 684d0d8

Please sign in to comment.