From: Junio C Hamano Date: Mon, 29 Sep 2014 19:36:08 +0000 (-0700) Subject: Merge branch 'hj/pretty-naked-decoration' X-Git-Tag: v2.2.0-rc0~78 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/b8e533f12acec63242c5405286fbbcfa66eecfdd?ds=inline;hp=-c Merge branch 'hj/pretty-naked-decoration' 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 --- b8e533f12acec63242c5405286fbbcfa66eecfdd diff --combined Documentation/pretty-formats.txt index 6c30723740,26904d716f..eecc39dec9 --- a/Documentation/pretty-formats.txt +++ b/Documentation/pretty-formats.txt @@@ -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 @@@ -184,8 -183,9 +185,9 @@@ 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 bcee7c5966,f120fd34d1..cff7ac1dbd --- a/log-tree.c +++ b/log-tree.c @@@ -12,7 -12,17 +12,7 @@@ #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); @@@ -200,11 -206,11 +201,11 @@@ 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 31f2edef11,811291c898..5fd9de6ce1 --- a/pretty.c +++ 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; } @@@ -162,7 -155,7 +162,7 @@@ 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'); } diff --combined t/t4205-log-pretty-formats.sh index 9b75399572,e6015e2684..7398605e7b --- a/t/t4205-log-pretty-formats.sh +++ b/t/t4205-log-pretty-formats.sh @@@ -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 <actual1 && + test_cmp expected actual1 + ' + test_done