From: Junio C Hamano Date: Mon, 30 Nov 2009 22:44:22 +0000 (-0800) Subject: Merge branch 'jc/pretty-lf' X-Git-Tag: v1.6.6-rc1~11 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=684d0d8dcfa2288744a553fc7294c5f9f5c12026;p=git.git Merge branch 'jc/pretty-lf' Conflicts: pretty.c t/t6006-rev-list-format.sh --- 684d0d8dcfa2288744a553fc7294c5f9f5c12026 diff --cc Documentation/pretty-formats.txt index 0683fb3a3,ca9c6d1f8..53a9168ba --- a/Documentation/pretty-formats.txt +++ b/Documentation/pretty-formats.txt @@@ -135,15 -131,15 +135,23 @@@ The placeholders are - '%m': left, right or boundary mark - '%n': newline - '%x00': print a byte from a hex code +- '%w([[,[,]]])': switch line wrapping, like the -w option of + linkgit:git-shortlog[1]. + +NOTE: Some placeholders may depend on other options given to the +revision traversal engine. For example, the `%g*` reflog options will +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 diff --cc pretty.c index 5661cba59,081feb660..8f5bd1ab7 --- a/pretty.c +++ b/pretty.c @@@ -599,37 -595,8 +599,37 @@@ static void format_decoration(struct st strbuf_addch(sb, ')'); } +static void strbuf_wrap(struct strbuf *sb, size_t pos, + size_t width, size_t indent1, size_t indent2) +{ + struct strbuf tmp = STRBUF_INIT; + + if (pos) + strbuf_add(&tmp, sb->buf, pos); + strbuf_add_wrapped_text(&tmp, sb->buf + pos, + (int) indent1, (int) indent2, (int) width); + strbuf_swap(&tmp, sb); + strbuf_release(&tmp); +} + +static void rewrap_message_tail(struct strbuf *sb, + struct format_commit_context *c, + size_t new_width, size_t new_indent1, + size_t new_indent2) +{ + if (c->width == new_width && c->indent1 == new_indent1 && + c->indent2 == new_indent2) + return; + if (c->wrap_start < sb->len) + strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2); + c->wrap_start = sb->len; + c->width = new_width; + c->indent1 = new_indent1; + 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; @@@ -816,9 -739,47 +816,47 @@@ 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 void *format, struct strbuf *sb, - enum date_mode dmode) + const char *format, struct strbuf *sb, + const struct pretty_print_context *pretty_ctx) { struct format_commit_context context; diff --cc t/t6006-rev-list-format.sh index 7f61ab0e5,18a77a739..571931588 --- a/t/t6006-rev-list-format.sh +++ b/t/t6006-rev-list-format.sh @@@ -162,22 -162,26 +162,44 @@@ 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 && + test $(wc -l actual && + test $(wc -l actual && + test $(wc -l expect && + git log -g --format="%h %gD: %gs" >actual && + test_cmp expect actual +' + +test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' ' + git reflog --date=raw >expect && + git log -g --format="%h %gD: %gs" --date=raw >actual && + test_cmp expect actual +' + +test_expect_success '%gd shortens ref name' ' + echo "master@{0}" >expect.gd-short && + git log -g -1 --format=%gd refs/heads/master >actual.gd-short && + test_cmp expect.gd-short actual.gd-short +' + test_done