pretty: add %D format specifier
PATCH v3 1/1 pretty: add %D format specifier
Add a new format specifier, '%D' that is identical in behaviour to '%d', except that it does not include the ' (' prefix or ')' suffix provided by '%d'.
--- Documentation/pretty-formats.txt | 6 ++++-- log-tree.c | 24 +++++++++++++----------- log-tree.h | 8 +++++++- pretty.c | 4 ++++ t/t4205-log-pretty-formats.sh | 11 +++++++++++ 5 files changed, 39 insertions(+), 14 deletions(-)
Documentation/pretty-formats.txt +4 -2
@@ -128,6 +128,7 @@ The placeholders are: - '%ct': committer date, UNIX timestamp - '%ci': committer date, 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
@@ -182,8 +183,9 @@ The placeholders are: 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
log-tree.c +13 -11
@@ -179,14 +179,16 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre }/* -* 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 char *color_commit =diff_get_color (use_color,DIFF_COMMIT );
@@ -196,20 +198,20 @@ void format_decorations(struct strbuf *sb, decoration =lookup_decoration (&name_decoration, &commit->object);if (!decoration)return ; - prefix =" (" ; +strbuf_addstr (sb, color_commit); +strbuf_addstr (sb, prefix);while (decoration) { -strbuf_addstr (sb, color_commit); -strbuf_addstr (sb, prefix);strbuf_addstr (sb,decorate_get_color (use_color, decoration->type));if (decoration->type ==DECORATION_REF_TAG )strbuf_addstr (sb,"tag: " );strbuf_addstr (sb, decoration->name);strbuf_addstr (sb, color_reset); - prefix =", " ; +strbuf_addstr (sb, color_commit); +if (decoration->next) +strbuf_addstr (sb, separator); decoration = decoration->next; } -strbuf_addstr (sb, color_commit); -strbuf_addch (sb,')' ); +strbuf_addstr (sb, suffix);strbuf_addstr (sb, color_reset); }
log-tree.h +7 -1
@@ -13,7 +13,13 @@ int log_tree_diff_flush(struct rev_info *);int log_tree_commit (struct rev_info *,struct commit *);int log_tree_opt_parse (struct rev_info *,const char **,int );void show_log (struct rev_info *opt); -void format_decorations (struct strbuf *sb,const struct commit *commit,int use_color); +void format_decorations_extended (struct strbuf *sb,const struct commit *commit, +int use_color, +const char *prefix, +const char *separator, +const char *suffix); +#define format_decorations (strbuf, commit, color) \ + format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")void show_decorations (struct rev_info *opt,struct commit *commit); void log_write_email_headers(struct rev_info *opt, struct commit *commit, const char **subject_p,
pretty.c +4 -0
@@ -1197,6 +1197,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */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 */
t/t4205-log-pretty-formats.sh +11 -0
@@ -457,4 +457,15 @@ EOF test_cmp expected actual1 ' +test_expect_success 'clean log decoration' ' + git log --no-walk --tags --pretty="%H %D" --decorate=full >actual && + cat <<EOF >expected && +$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
Signature · 1 line
2.1.0
Quoted 5 lines · Harry Jeffery
Add a new format specifier, '%D' that is identical in behaviour to '%d', except that it does not include the ' (' prefix or ')' suffix provided by '%d'.
Signed-off-by Harry Jeffery <[email protected]>
Thanks.
Quoted 11 lines
log-tree.c +2
-3@@ -196,20 +198,20 @@ void format_decorations(struct strbuf *sb, decoration =lookup_decoration (&name_decoration, &commit->object);if (!decoration)return ; - prefix =" (" ; +strbuf_addstr (sb, color_commit); +strbuf_addstr (sb, prefix);while (decoration) { -strbuf_addstr (sb, color_commit); -strbuf_addstr (sb, prefix);strbuf_addstr (sb,decorate_get_color (use_color, decoration->type));
log-tree.c void format_decorations(struct strbuf *sb, · line 213
Quoted 10 lines
log-tree.c +3
-1if (decoration->type ==DECORATION_REF_TAG )strbuf_addstr (sb,"tag: " );strbuf_addstr (sb, decoration->name);strbuf_addstr (sb, color_reset); - prefix =", " ; +strbuf_addstr (sb, color_commit); +if (decoration->next) +strbuf_addstr (sb, separator); decoration = decoration->next; }
I was kind of found of the nice trick to use a punctuation, which first points at the prefix " (" and then later points at the separator ", ", to allow the code that prefixes the punctuation before showing a new item. It is now lost.
We can restore it by doing something like this, though:
if (!decoration)
return;
while (decoration) {
strbuf_addstr(sb, prefix);
strbuf_addstr(sb, decoration->name);
prefix = separator;
decoration = decoration->next;
}Quoted 5 lines
t/t4205-log-pretty-formats.sh +0
-0
t/t4205-log-pretty-formats.sh EOF · line 466
Quoted 10 lines
t/t4205-log-pretty-formats.sh +7
-0@@ -457,4 +457,15 @@ EOF test_cmp expected actual1 ' +test_expect_success 'clean log decoration' ' + git log --no-walk --tags --pretty="%H %D" --decorate=full >actual && + cat <<EOF >expected && +$head1 tag: refs/tags/tag2 +$head2 tag: refs/tags/message-one +$old_head1 tag: refs/tags/message-two +EOF
You could indent the here-doc if you wanted to, like this:
cat >expected <<-EOF &&
$head1 tag: ...
...
EOFand the end result may look easier on the eyes.
t/t4205-log-pretty-formats.sh EOF · line 467
Quoted 1 line
t/t4205-log-pretty-formats.sh +1
-0+ sort actual >actual1 &&
Hmph. I actually think the part that prepares the history makes sure that the output order of the commits is predictable by using test_commit and test_tick. I see existing tests at the end (which is a sign that they were added more recently than the rest of the test script, and can indicate a careless addition) already has "sort", but we shouldn't have to sort.
t/t4205-log-pretty-formats.sh EOF · line 469
Quoted 2 lines
t/t4205-log-pretty-formats.sh +2
-0+ test_cmp expected actual1 +'
Thanks.
Quoted 6 lines · Junio C Hamano
Quoted 6 lines
t/t4205-log-pretty-formats.sh +6
-0+test_expect_success 'clean log decoration' ' + git log --no-walk --tags --pretty="%H %D" --decorate=full >actual && + cat <<EOF >expected && +$head1 tag: refs/tags/tag2 +$head2 tag: refs/tags/message-one +$old_head1 tag: refs/tags/message-two
Quoted 8 lines
Quoted 1 line
+EOF
...
Hmph. I actually think the part that prepares the history makes sure that the output order of the commits is predictable by usingtest_commitandtest_tick. I see existing tests at the end (which is a sign that they were added more recently than the rest of the test script, and can indicate a careless addition) already has "sort", but we shouldn't have to sort.
Actually --tags may feed the tips in unspecified order and --no-walk ensures the commits will be shown in that unspecified order, so for a test that runs "log --no-walk --tags", sorting is unavoidable.