Skip to content

Commit 6ba65f4

Browse files
committed
Merge branch 'es/pretty-describe-more'
Extend "git log --format=%(describe)" placeholder to allow passing selected command-line options to the underlying "git describe" command. * es/pretty-describe-more: pretty: add abbrev option to %(describe) pretty: add tag option to %(describe) pretty.c: rework describe options parsing for better extensibility
2 parents 832ec72 + eccd97d commit 6ba65f4

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

Documentation/pretty-formats.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ The placeholders are:
220220
inconsistent when tags are added or removed at
221221
the same time.
222222
+
223+
** 'tags[=<bool-value>]': Instead of only considering annotated tags,
224+
consider lightweight tags as well.
225+
** 'abbrev=<number>': Instead of using the default number of hexadecimal digits
226+
(which will vary according to the number of objects in the repository with a
227+
default of 7) of the abbreviated object name, use <number> digits, or as many
228+
digits as needed to form a unique object name.
223229
** 'match=<pattern>': Only consider tags matching the given
224230
`glob(7)` pattern, excluding the "refs/tags/" prefix.
225231
** 'exclude=<pattern>': Do not consider tags matching the given
@@ -273,11 +279,6 @@ endif::git-rev-list[]
273279
If any option is provided multiple times the
274280
last occurrence wins.
275281
+
276-
The boolean options accept an optional value `[=<value>]`. The values
277-
`true`, `false`, `on`, `off` etc. are all accepted. See the "boolean"
278-
sub-section in "EXAMPLES" in linkgit:git-config[1]. If a boolean
279-
option is given with no value, it's enabled.
280-
+
281282
** 'key=<key>': only show trailers with specified <key>. Matching is done
282283
case-insensitively and trailing colon is optional. If option is
283284
given multiple times trailer lines matching any of the keys are
@@ -313,6 +314,11 @@ insert an empty string unless we are traversing reflog entries (e.g., by
313314
decoration format if `--decorate` was not already provided on the command
314315
line.
315316

317+
The boolean options accept an optional value `[=<bool-value>]`. The values
318+
`true`, `false`, `on`, `off` etc. are all accepted. See the "boolean"
319+
sub-section in "EXAMPLES" in linkgit:git-config[1]. If a boolean
320+
option is given with no value, it's enabled.
321+
316322
If you add a `+` (plus sign) after '%' of a placeholder, a line-feed
317323
is inserted immediately before the expansion if and only if the
318324
placeholder expands to a non-empty string.

pretty.c

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,28 +1275,66 @@ int format_set_trailers_options(struct process_trailer_options *opts,
12751275

12761276
static size_t parse_describe_args(const char *start, struct strvec *args)
12771277
{
1278-
const char *options[] = { "match", "exclude" };
1278+
struct {
1279+
char *name;
1280+
enum {
1281+
DESCRIBE_ARG_BOOL,
1282+
DESCRIBE_ARG_INTEGER,
1283+
DESCRIBE_ARG_STRING,
1284+
} type;
1285+
} option[] = {
1286+
{ "tags", DESCRIBE_ARG_BOOL},
1287+
{ "abbrev", DESCRIBE_ARG_INTEGER },
1288+
{ "exclude", DESCRIBE_ARG_STRING },
1289+
{ "match", DESCRIBE_ARG_STRING },
1290+
};
12791291
const char *arg = start;
12801292

12811293
for (;;) {
1282-
const char *matched = NULL;
1294+
int found = 0;
12831295
const char *argval;
12841296
size_t arglen = 0;
1297+
int optval = 0;
12851298
int i;
12861299

1287-
for (i = 0; i < ARRAY_SIZE(options); i++) {
1288-
if (match_placeholder_arg_value(arg, options[i], &arg,
1289-
&argval, &arglen)) {
1290-
matched = options[i];
1300+
for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
1301+
switch (option[i].type) {
1302+
case DESCRIBE_ARG_BOOL:
1303+
if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) {
1304+
if (optval)
1305+
strvec_pushf(args, "--%s", option[i].name);
1306+
else
1307+
strvec_pushf(args, "--no-%s", option[i].name);
1308+
found = 1;
1309+
}
1310+
break;
1311+
case DESCRIBE_ARG_INTEGER:
1312+
if (match_placeholder_arg_value(arg, option[i].name, &arg,
1313+
&argval, &arglen)) {
1314+
char *endptr;
1315+
if (!arglen)
1316+
return 0;
1317+
strtol(argval, &endptr, 10);
1318+
if (endptr - argval != arglen)
1319+
return 0;
1320+
strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1321+
found = 1;
1322+
}
1323+
break;
1324+
case DESCRIBE_ARG_STRING:
1325+
if (match_placeholder_arg_value(arg, option[i].name, &arg,
1326+
&argval, &arglen)) {
1327+
if (!arglen)
1328+
return 0;
1329+
strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1330+
found = 1;
1331+
}
12911332
break;
12921333
}
12931334
}
1294-
if (!matched)
1335+
if (!found)
12951336
break;
12961337

1297-
if (!arglen)
1298-
return 0;
1299-
strvec_pushf(args, "--%s=%.*s", matched, (int)arglen, argval);
13001338
}
13011339
return arg - start;
13021340
}

t/t4205-log-pretty-formats.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,4 +1002,20 @@ test_expect_success '%(describe:exclude=...) vs git describe --exclude ...' '
10021002
test_cmp expect actual
10031003
'
10041004

1005+
test_expect_success '%(describe:tags) vs git describe --tags' '
1006+
test_when_finished "git tag -d tagname" &&
1007+
git tag tagname &&
1008+
git describe --tags >expect &&
1009+
git log -1 --format="%(describe:tags)" >actual &&
1010+
test_cmp expect actual
1011+
'
1012+
1013+
test_expect_success '%(describe:abbrev=...) vs git describe --abbrev=...' '
1014+
test_when_finished "git tag -d tagname" &&
1015+
git tag -a -m tagged tagname &&
1016+
git describe --abbrev=15 >expect &&
1017+
git log -1 --format="%(describe:abbrev=15)" >actual &&
1018+
test_cmp expect actual
1019+
'
1020+
10051021
test_done

0 commit comments

Comments
 (0)