pretty: add tag option to %(describe)

The %(describe) placeholder by default, like `git describe`, only supports annotated tags. However, some people do use lightweight tags for releases, and would like to describe those anyway. The command line tool has an option to support this. Teach the placeholder to support this as well. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eli Schwartz committed Oct 31, 2021 at 13:15 UTC 1d517ceab98fd5c3c1a8fb6ef575b44c31ea7a9e
3 files changed +27 -5
Documentation/pretty-formats.txt
+7 -5
@@ -220,6 +220,8 @@ The placeholders are:
220 inconsistent when tags are added or removed at
221 the same time.
222 +
223 +** 'tags[=<bool>]': Instead of only considering annotated tags,
224 + consider lightweight tags as well.
225 ** 'match=<pattern>': Only consider tags matching the given
226 `glob(7)` pattern, excluding the "refs/tags/" prefix.
227 ** 'exclude=<pattern>': Do not consider tags matching the given
@@ -273,11 +275,6 @@ endif::git-rev-list[]
275 If any option is provided multiple times the
276 last occurrence wins.
277 +
276 -The boolean options accept an optional value `[=<BOOL>]`. 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 -+
278 ** 'key=<K>': only show trailers with specified key. Matching is done
279 case-insensitively and trailing colon is optional. If option is
280 given multiple times trailer lines matching any of the keys are
@@ -313,6 +310,11 @@ insert an empty string unless we are traversing reflog entries (e.g., by
310 decoration format if `--decorate` was not already provided on the command
311 line.
312
313 +The boolean options accept an optional value `[=<bool>]`. The values
314 +`true`, `false`, `on`, `off` etc. are all accepted. See the "boolean"
315 +sub-section in "EXAMPLES" in linkgit:git-config[1]. If a boolean
316 +option is given with no value, it's enabled.
317 +
318 If you add a `+` (plus sign) after '%' of a placeholder, a line-feed
319 is inserted immediately before the expansion if and only if the
320 placeholder expands to a non-empty string.
pretty.c
+12
@@ -1219,9 +1219,11 @@ static size_t parse_describe_args(const char *start, struct strvec *args)
1219 struct {
1220 char *name;
1221 enum {
1222 + DESCRIBE_ARG_BOOL,
1223 DESCRIBE_ARG_STRING,
1224 } type;
1225 } option[] = {
1226 + { "tags", DESCRIBE_ARG_BOOL},
1227 { "exclude", DESCRIBE_ARG_STRING },
1228 { "match", DESCRIBE_ARG_STRING },
1229 };
@@ -1231,10 +1233,20 @@ static size_t parse_describe_args(const char *start, struct strvec *args)
1233 int found = 0;
1234 const char *argval;
1235 size_t arglen = 0;
1236 + int optval = 0;
1237 int i;
1238
1239 for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
1240 switch (option[i].type) {
1241 + case DESCRIBE_ARG_BOOL:
1242 + if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) {
1243 + if (optval)
1244 + strvec_pushf(args, "--%s", option[i].name);
1245 + else
1246 + strvec_pushf(args, "--no-%s", option[i].name);
1247 + found = 1;
1248 + }
1249 + break;
1250 case DESCRIBE_ARG_STRING:
1251 if (match_placeholder_arg_value(arg, option[i].name, &arg,
1252 &argval, &arglen)) {
t/t4205-log-pretty-formats.sh
+8
@@ -1002,4 +1002,12 @@ test_expect_success '%(describe:exclude=...) vs git describe --exclude ...' '
1002 test_cmp expect actual
1003 '
1004
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_done