]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'hj/pretty-naked-decoration'
authorJunio C Hamano <gitster@pobox.com>
Mon, 29 Sep 2014 19:36:08 +0000 (12:36 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 29 Sep 2014 19:36:09 +0000 (12:36 -0700)
The pretty-format specifier "%d", which expanded to " (tagname)"
for a tagged commit, gained a cousin "%D" that just gives the
"tagname" without frills.

* hj/pretty-naked-decoration:
  pretty: add %D format specifier

1  2 
Documentation/pretty-formats.txt
log-tree.c
pretty.c
t/t4205-log-pretty-formats.sh

index 6c30723740cff5d88d3696f45e2325bc565e5f4a,26904d716f5fdf0e928375116bf038e517dd6d49..eecc39dec99b3cab3da3e36440653ea46ac762b9
@@@ -115,21 -115,20 +115,22 @@@ The placeholders are
  - '%aD': author date, RFC2822 style
  - '%ar': author date, relative
  - '%at': author date, UNIX timestamp
 -- '%ai': author date, ISO 8601 format
 +- '%ai': author date, ISO 8601-like format
 +- '%aI': author date, strict ISO 8601 format
  - '%cn': committer name
  - '%cN': committer name (respecting .mailmap, see
    linkgit:git-shortlog[1] or linkgit:git-blame[1])
  - '%ce': committer email
  - '%cE': committer email (respecting .mailmap, see
    linkgit:git-shortlog[1] or linkgit:git-blame[1])
 -- '%cd': committer date
 +- '%cd': committer date (format respects --date= option)
  - '%cD': committer date, RFC2822 style
  - '%cr': committer date, relative
  - '%ct': committer date, UNIX timestamp
 -- '%ci': committer date, ISO 8601 format
 +- '%ci': committer date, ISO 8601-like format
 +- '%cI': committer date, strict ISO 8601 format
  - '%d': ref names, like the --decorate option of linkgit:git-log[1]
+ - '%D': ref names without the " (", ")" wrapping.
  - '%e': encoding
  - '%s': subject
  - '%f': sanitized subject line, suitable for a filename
  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.
+ `git log -g`). The `%d` and `%D` placeholders will use the "short"
+ decoration format if `--decorate` was not already provided on the command
+ line.
  
  If you add a `+` (plus sign) after '%' of a placeholder, a line-feed
  is inserted immediately before the expansion if and only if the
diff --combined log-tree.c
index bcee7c596696eb1da109b3ff375e09453045a60f,f120fd34d1f811b6114ab0d1ace7b1ca3fbe2dcf..cff7ac1dbd8942f3013e6fa91d30d2ea33495854
  #include "sequencer.h"
  #include "line-log.h"
  
 -struct decoration name_decoration = { "object names" };
 -
 -enum decoration_type {
 -      DECORATION_NONE = 0,
 -      DECORATION_REF_LOCAL,
 -      DECORATION_REF_REMOTE,
 -      DECORATION_REF_TAG,
 -      DECORATION_REF_STASH,
 -      DECORATION_REF_HEAD,
 -      DECORATION_GRAFTED,
 -};
 +static struct decoration name_decoration = { "object names" };
  
  static char decoration_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
@@@ -74,20 -84,15 +74,20 @@@ int parse_decorate_color_config(const c
  #define decorate_get_color_opt(o, ix) \
        decorate_get_color((o)->use_color, ix)
  
 -static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
 +void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
  {
        int nlen = strlen(name);
 -      struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
 +      struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
        memcpy(res->name, name, nlen + 1);
        res->type = type;
        res->next = add_decoration(&name_decoration, obj, res);
  }
  
 +const struct name_decoration *get_name_decoration(const struct object *obj)
 +{
 +      return lookup_decoration(&name_decoration, obj);
 +}
 +
  static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
  {
        struct object *obj;
@@@ -174,24 -179,25 +174,25 @@@ static void show_children(struct rev_in
  }
  
  /*
-  * The caller makes sure there is no funny color before
-  * calling. format_decorations makes sure the same after return.
+  * The caller makes sure there is no funny color before calling.
+  * format_decorations_extended makes sure the same after return.
   */
- void format_decorations(struct strbuf *sb,
+ void format_decorations_extended(struct strbuf *sb,
                        const struct commit *commit,
-                       int use_color)
+                       int use_color,
+                       const char *prefix,
+                       const char *separator,
+                       const char *suffix)
  {
-       const char *prefix;
 -      struct name_decoration *decoration;
 +      const struct name_decoration *decoration;
        const char *color_commit =
                diff_get_color(use_color, DIFF_COMMIT);
        const char *color_reset =
                decorate_get_color(use_color, DECORATION_NONE);
  
 -      decoration = lookup_decoration(&name_decoration, &commit->object);
 +      decoration = get_name_decoration(&commit->object);
        if (!decoration)
                return;
-       prefix = " (";
        while (decoration) {
                strbuf_addstr(sb, color_commit);
                strbuf_addstr(sb, prefix);
                        strbuf_addstr(sb, "tag: ");
                strbuf_addstr(sb, decoration->name);
                strbuf_addstr(sb, color_reset);
-               prefix = ", ";
+               prefix = separator;
                decoration = decoration->next;
        }
        strbuf_addstr(sb, color_commit);
-       strbuf_addch(sb, ')');
+       strbuf_addstr(sb, suffix);
        strbuf_addstr(sb, color_reset);
  }
  
@@@ -644,7 -650,7 +645,7 @@@ void show_log(struct rev_info *opt
                graph_show_commit_msg(opt->graph, &msgbuf);
        else
                fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
 -      if (opt->use_terminator) {
 +      if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
                if (!opt->missing_newline)
                        graph_show_padding(opt->graph);
                putchar(opt->diffopt.line_termination);
@@@ -671,8 -677,7 +672,8 @@@ int log_tree_diff_flush(struct rev_inf
                show_log(opt);
                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
                    opt->verbose_header &&
 -                  opt->commit_format != CMIT_FMT_ONELINE) {
 +                  opt->commit_format != CMIT_FMT_ONELINE &&
 +                  !commit_format_is_empty(opt->commit_format)) {
                        /*
                         * When showing a verbose header (i.e. log message),
                         * and not in --pretty=oneline format, we would want
diff --combined pretty.c
index 31f2edef1106200761aff49758545972000d9c28,811291c898a65de7a41463eba55d8115279aa578..5fd9de6ce11da25b8ddf8ece9f718023394624e6
+++ b/pretty.c
@@@ -24,11 -24,6 +24,11 @@@ static size_t commit_formats_len
  static size_t commit_formats_alloc;
  static struct cmt_fmt_map *find_commit_format(const char *sought);
  
 +int commit_format_is_empty(enum cmit_fmt fmt)
 +{
 +      return fmt == CMIT_FMT_USERFORMAT && !*user_format;
 +}
 +
  static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
  {
        free(user_format);
@@@ -70,9 -65,7 +70,9 @@@ static int git_pretty_formats_config(co
  
        commit_format->name = xstrdup(name);
        commit_format->format = CMIT_FMT_USERFORMAT;
 -      git_config_string(&fmt, var, value);
 +      if (git_config_string(&fmt, var, value))
 +              return -1;
 +
        if (starts_with(fmt, "format:") || starts_with(fmt, "tformat:")) {
                commit_format->is_tformat = fmt[0] == 't';
                fmt = strchr(fmt, ':') + 1;
@@@ -153,7 -146,7 +153,7 @@@ void get_commit_format(const char *arg
        struct cmt_fmt_map *commit_format;
  
        rev->use_terminator = 0;
 -      if (!arg || !*arg) {
 +      if (!arg) {
                rev->commit_format = CMIT_FMT_DEFAULT;
                return;
        }
                return;
        }
  
 -      if (strchr(arg, '%')) {
 +      if (!*arg || strchr(arg, '%')) {
                save_user_format(rev, arg, 1);
                return;
        }
@@@ -554,11 -547,31 +554,11 @@@ static void add_merge_info(const struc
        strbuf_addch(sb, '\n');
  }
  
 -static char *get_header(const struct commit *commit, const char *msg,
 -                      const char *key)
 +static char *get_header(const char *msg, const char *key)
  {
 -      int key_len = strlen(key);
 -      const char *line = msg;
 -
 -      while (line) {
 -              const char *eol = strchrnul(line, '\n'), *next;
 -
 -              if (line == eol)
 -                      return NULL;
 -              if (!*eol) {
 -                      warning("malformed commit (header is missing newline): %s",
 -                              sha1_to_hex(commit->object.sha1));
 -                      next = NULL;
 -              } else
 -                      next = eol + 1;
 -              if (eol - line > key_len &&
 -                  !strncmp(line, key, key_len) &&
 -                  line[key_len] == ' ') {
 -                      return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
 -              }
 -              line = next;
 -      }
 -      return NULL;
 +      size_t len;
 +      const char *v = find_commit_header(msg, key, &len);
 +      return v ? xmemdupz(v, len) : NULL;
  }
  
  static char *replace_encoding_header(char *buf, const char *encoding)
@@@ -604,10 -617,11 +604,10 @@@ const char *logmsg_reencode(const struc
  
        if (!output_encoding || !*output_encoding) {
                if (commit_encoding)
 -                      *commit_encoding =
 -                              get_header(commit, msg, "encoding");
 +                      *commit_encoding = get_header(msg, "encoding");
                return msg;
        }
 -      encoding = get_header(commit, msg, "encoding");
 +      encoding = get_header(msg, "encoding");
        if (commit_encoding)
                *commit_encoding = encoding;
        use_encoding = encoding ? encoding : utf8;
@@@ -717,12 -731,9 +717,12 @@@ static size_t format_person_part(struc
        case 'r':       /* date, relative */
                strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
                return placeholder_len;
 -      case 'i':       /* date, ISO 8601 */
 +      case 'i':       /* date, ISO 8601-like */
                strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601));
                return placeholder_len;
 +      case 'I':       /* date, ISO 8601 strict */
 +              strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601_STRICT));
 +              return placeholder_len;
        }
  
  skip:
@@@ -1179,6 -1190,10 +1179,10 @@@ static size_t format_commit_one(struct 
                load_ref_decorations(DECORATE_SHORT_REFS);
                format_decorations(sb, commit, c->auto_color);
                return 1;
+       case 'D':
+               load_ref_decorations(DECORATE_SHORT_REFS);
+               format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
+               return 1;
        case 'g':               /* reflog info */
                switch(placeholder[1]) {
                case 'd':       /* reflog selector */
@@@ -1377,7 -1392,9 +1381,7 @@@ static size_t format_and_pad_commit(str
                 * convert it back to chars
                 */
                padding = padding - len + local_sb.len;
 -              strbuf_grow(sb, padding);
 -              strbuf_setlen(sb, sb_len + padding);
 -              memset(sb->buf + sb_len, ' ', sb->len - sb_len);
 +              strbuf_addchars(sb, ' ', padding);
                memcpy(sb->buf + sb_len + offset, local_sb.buf,
                       local_sb.len);
        }
@@@ -1652,8 -1669,10 +1656,8 @@@ void pp_remainder(struct pretty_print_c
                first = 0;
  
                strbuf_grow(sb, linelen + indent + 20);
 -              if (indent) {
 -                      memset(sb->buf + sb->len, ' ', indent);
 -                      strbuf_setlen(sb, sb->len + indent);
 -              }
 +              if (indent)
 +                      strbuf_addchars(sb, ' ', indent);
                strbuf_add(sb, line, linelen);
                strbuf_addch(sb, '\n');
        }
index 9b75399572b1cbe1bfcb00995e1660a93fe6a4c6,e6015e2684ebf7ed1bdb3dad1a43b8309248ef3b..7398605e7b894259548f257aaf3e92d9481534eb
@@@ -431,21 -431,6 +431,21 @@@ EO
        test_cmp expected actual
  '
  
 +test_expect_success 'strbuf_utf8_replace() not producing NUL' '
 +      git log --color --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)%C(auto)%d" |
 +              test_decode_color |
 +              nul_to_q >actual &&
 +      ! grep Q actual
 +'
 +
 +# ISO strict date format
 +test_expect_success 'ISO and ISO-strict date formats display the same values' '
 +      git log --format=%ai%n%ci |
 +      sed -e "s/ /T/; s/ //; s/..\$/:&/" >expected &&
 +      git log --format=%aI%n%cI >actual &&
 +      test_cmp expected actual
 +'
 +
  # get new digests (with no abbreviations)
  head1=$(git rev-parse --verify HEAD~0) &&
  head2=$(git rev-parse --verify HEAD~1) &&
@@@ -465,4 -450,15 +465,15 @@@ EO
        test_cmp expected actual1
  '
  
+ test_expect_success 'clean log decoration' '
+       git log --no-walk --tags --pretty="%H %D" --decorate=full >actual &&
+       cat >expected <<EOF &&
+ $head1 tag: refs/tags/tag2
+ $head2 tag: refs/tags/message-one
+ $old_head1 tag: refs/tags/message-two
+ EOF
+       sort actual >actual1 &&
+       test_cmp expected actual1
+ '
  test_done