Skip to content

Commit 9271095

Browse files
eXeC64gitster
authored andcommitted
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'. Signed-off-by: Harry Jeffery <harry@exec64.co.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 96db324 commit 9271095

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

Documentation/pretty-formats.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ The placeholders are:
128128
- '%ct': committer date, UNIX timestamp
129129
- '%ci': committer date, ISO 8601 format
130130
- '%d': ref names, like the --decorate option of linkgit:git-log[1]
131+
- '%D': ref names without the " (", ")" wrapping.
131132
- '%e': encoding
132133
- '%s': subject
133134
- '%f': sanitized subject line, suitable for a filename
@@ -182,8 +183,9 @@ The placeholders are:
182183
NOTE: Some placeholders may depend on other options given to the
183184
revision traversal engine. For example, the `%g*` reflog options will
184185
insert an empty string unless we are traversing reflog entries (e.g., by
185-
`git log -g`). The `%d` placeholder will use the "short" decoration
186-
format if `--decorate` was not already provided on the command line.
186+
`git log -g`). The `%d` and `%D` placeholders will use the "short"
187+
decoration format if `--decorate` was not already provided on the command
188+
line.
187189

188190
If you add a `+` (plus sign) after '%' of a placeholder, a line-feed
189191
is inserted immediately before the expansion if and only if the

log-tree.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,16 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
179179
}
180180

181181
/*
182-
* The caller makes sure there is no funny color before
183-
* calling. format_decorations makes sure the same after return.
182+
* The caller makes sure there is no funny color before calling.
183+
* format_decorations_extended makes sure the same after return.
184184
*/
185-
void format_decorations(struct strbuf *sb,
185+
void format_decorations_extended(struct strbuf *sb,
186186
const struct commit *commit,
187-
int use_color)
187+
int use_color,
188+
const char *prefix,
189+
const char *separator,
190+
const char *suffix)
188191
{
189-
const char *prefix;
190192
struct name_decoration *decoration;
191193
const char *color_commit =
192194
diff_get_color(use_color, DIFF_COMMIT);
@@ -196,7 +198,6 @@ void format_decorations(struct strbuf *sb,
196198
decoration = lookup_decoration(&name_decoration, &commit->object);
197199
if (!decoration)
198200
return;
199-
prefix = " (";
200201
while (decoration) {
201202
strbuf_addstr(sb, color_commit);
202203
strbuf_addstr(sb, prefix);
@@ -205,11 +206,11 @@ void format_decorations(struct strbuf *sb,
205206
strbuf_addstr(sb, "tag: ");
206207
strbuf_addstr(sb, decoration->name);
207208
strbuf_addstr(sb, color_reset);
208-
prefix = ", ";
209+
prefix = separator;
209210
decoration = decoration->next;
210211
}
211212
strbuf_addstr(sb, color_commit);
212-
strbuf_addch(sb, ')');
213+
strbuf_addstr(sb, suffix);
213214
strbuf_addstr(sb, color_reset);
214215
}
215216

log-tree.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ int log_tree_diff_flush(struct rev_info *);
1313
int log_tree_commit(struct rev_info *, struct commit *);
1414
int log_tree_opt_parse(struct rev_info *, const char **, int);
1515
void show_log(struct rev_info *opt);
16-
void format_decorations(struct strbuf *sb, const struct commit *commit, int use_color);
16+
void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
17+
int use_color,
18+
const char *prefix,
19+
const char *separator,
20+
const char *suffix);
21+
#define format_decorations(strbuf, commit, color) \
22+
format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
1723
void show_decorations(struct rev_info *opt, struct commit *commit);
1824
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
1925
const char **subject_p,

pretty.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
11901190
load_ref_decorations(DECORATE_SHORT_REFS);
11911191
format_decorations(sb, commit, c->auto_color);
11921192
return 1;
1193+
case 'D':
1194+
load_ref_decorations(DECORATE_SHORT_REFS);
1195+
format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1196+
return 1;
11931197
case 'g': /* reflog info */
11941198
switch(placeholder[1]) {
11951199
case 'd': /* reflog selector */

t/t4205-log-pretty-formats.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,15 @@ EOF
450450
test_cmp expected actual1
451451
'
452452

453+
test_expect_success 'clean log decoration' '
454+
git log --no-walk --tags --pretty="%H %D" --decorate=full >actual &&
455+
cat >expected <<EOF &&
456+
$head1 tag: refs/tags/tag2
457+
$head2 tag: refs/tags/message-one
458+
$old_head1 tag: refs/tags/message-two
459+
EOF
460+
sort actual >actual1 &&
461+
test_cmp expected actual1
462+
'
463+
453464
test_done

0 commit comments

Comments
 (0)