Skip to content

Commit b081547

Browse files
rscharfegitster
authored andcommitted
pretty: add merge and exclude options to %(describe)
Allow restricting the tags used by the placeholder %(describe) with the options match and exclude. E.g. the following command describes the current commit using official version tags, without those for release candidates: $ git log -1 --format='%(describe:match=v[0-9]*,exclude=*rc*)' Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15ae82d commit b081547

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

Documentation/pretty-formats.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,17 @@ The placeholders are:
208208
'%cs':: committer date, short format (`YYYY-MM-DD`)
209209
'%d':: ref names, like the --decorate option of linkgit:git-log[1]
210210
'%D':: ref names without the " (", ")" wrapping.
211-
'%(describe)':: human-readable name, like linkgit:git-describe[1];
212-
empty string for undescribable commits
211+
'%(describe[:options])':: human-readable name, like
212+
linkgit:git-describe[1]; empty string for
213+
undescribable commits. The `describe` string
214+
may be followed by a colon and zero or more
215+
comma-separated options.
216+
+
217+
** 'match=<pattern>': Only consider tags matching the given
218+
`glob(7)` pattern, excluding the "refs/tags/" prefix.
219+
** 'exclude=<pattern>': Do not consider tags matching the given
220+
`glob(7)` pattern, excluding the "refs/tags/" prefix.
221+
213222
'%S':: ref name given on the command line by which the commit was reached
214223
(like `git log --source`), only works with `git log`
215224
'%e':: encoding

pretty.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,34 @@ static int format_trailer_match_cb(const struct strbuf *key, void *ud)
11501150
return 0;
11511151
}
11521152

1153+
static size_t parse_describe_args(const char *start, struct strvec *args)
1154+
{
1155+
const char *options[] = { "match", "exclude" };
1156+
const char *arg = start;
1157+
1158+
for (;;) {
1159+
const char *matched = NULL;
1160+
const char *argval;
1161+
size_t arglen = 0;
1162+
int i;
1163+
1164+
for (i = 0; i < ARRAY_SIZE(options); i++) {
1165+
if (match_placeholder_arg_value(arg, options[i], &arg,
1166+
&argval, &arglen)) {
1167+
matched = options[i];
1168+
break;
1169+
}
1170+
}
1171+
if (!matched)
1172+
break;
1173+
1174+
if (!arglen)
1175+
return 0;
1176+
strvec_pushf(args, "--%s=%.*s", matched, (int)arglen, argval);
1177+
}
1178+
return arg - start;
1179+
}
1180+
11531181
static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
11541182
const char *placeholder,
11551183
void *context)
@@ -1215,20 +1243,31 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12151243
return parse_padding_placeholder(placeholder, c);
12161244
}
12171245

1218-
if (skip_prefix(placeholder, "(describe)", &arg)) {
1246+
if (skip_prefix(placeholder, "(describe", &arg)) {
12191247
struct child_process cmd = CHILD_PROCESS_INIT;
12201248
struct strbuf out = STRBUF_INIT;
12211249
struct strbuf err = STRBUF_INIT;
12221250

12231251
cmd.git_cmd = 1;
12241252
strvec_push(&cmd.args, "describe");
1253+
1254+
if (*arg == ':') {
1255+
arg++;
1256+
arg += parse_describe_args(arg, &cmd.args);
1257+
}
1258+
1259+
if (*arg != ')') {
1260+
child_process_clear(&cmd);
1261+
return 0;
1262+
}
1263+
12251264
strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
12261265
pipe_command(&cmd, NULL, 0, &out, 0, &err, 0);
12271266
strbuf_rtrim(&out);
12281267
strbuf_addbuf(sb, &out);
12291268
strbuf_release(&out);
12301269
strbuf_release(&err);
1231-
return arg - placeholder;
1270+
return arg - placeholder + 1;
12321271
}
12331272

12341273
/* these depend on the commit */

t/t4205-log-pretty-formats.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,4 +972,20 @@ test_expect_success '%(describe) vs git describe' '
972972
test_must_be_empty err
973973
'
974974

975+
test_expect_success '%(describe:match=...) vs git describe --match ...' '
976+
test_when_finished "git tag -d tag-match" &&
977+
git tag -a -m tagged tag-match&&
978+
git describe --match "*-match" >expect &&
979+
git log -1 --format="%(describe:match=*-match)" >actual &&
980+
test_cmp expect actual
981+
'
982+
983+
test_expect_success '%(describe:exclude=...) vs git describe --exclude ...' '
984+
test_when_finished "git tag -d tag-exclude" &&
985+
git tag -a -m tagged tag-exclude &&
986+
git describe --exclude "*-exclude" >expect &&
987+
git log -1 --format="%(describe:exclude=*-exclude)" >actual &&
988+
test_cmp expect actual
989+
'
990+
975991
test_done

0 commit comments

Comments
 (0)