Merge branch 'jc/pretty-lf'
authorJunio C Hamano <gitster@pobox.com>
Mon, 30 Nov 2009 22:44:22 +0000 (14:44 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Nov 2009 22:44:22 +0000 (14:44 -0800)
Conflicts:
pretty.c
t/t6006-rev-list-format.sh

1  2 
Documentation/pretty-formats.txt
pretty.c
t/t6006-rev-list-format.sh

index 0683fb3a3d34e89c67c6e37f2ee96adcd2821ea0,ca9c6d1f8078900f3f08b24cd47ba802c5e80ba1..53a9168ba7d8959e65c2442f6a078155d6f24c71
@@@ -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([<w>[,<i1>[,<i2>]]])': 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 5661cba5952e2e68dd067a8afebe144bc1ca63af,081feb66026a473060a7beaf40de2618b792ac0f..8f5bd1ab7f119715564fb13cb74de3937d1a3774
+++ b/pretty.c
@@@ -599,37 -595,8 +599,37 @@@ static void format_decoration(struct st
                strbuf_addch(sb, ')');
  }
  
- static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
-                                void *context)
 +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_one(struct strbuf *sb, const char *placeholder,
+                               void *context)
  {
        struct format_commit_context *c = context;
        const struct commit *commit = c->commit;
        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;
  
index 7f61ab0e522fd28c5e10594f03ed9076f9f5ce42,18a77a739ed326ab9cc17ca3838b3859becddabe..571931588eda5799efbf2b0789abd10b8770a0a7
@@@ -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) = 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 &&
 +      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