tag.c: implement '--format' option

Implement the '--format' option provided by 'ref-filter'. This lets the user list tags as per desired format similar to the implementation in 'git for-each-ref'. Add tests and documentation for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Sep 11, 2015 at 20:36 UTC df0947417acd1058c17e2bf20374497f33084549
3 files changed +34 -11
Documentation/git-tag.txt
+7 -1
@@ -13,7 +13,8 @@ SYNOPSIS
13 <tagname> [<commit> | <object>]
14 'git tag' -d <tagname>...
15 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
16 - [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]
16 + [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
17 + [--format=<format>] [<pattern>...]
18 'git tag' -v <tagname>...
19
20 DESCRIPTION
@@ -158,6 +159,11 @@ This option is only applicable when listing tags without annotation lines.
159 The object that the new tag will refer to, usually a commit.
160 Defaults to HEAD.
161
162 +<format>::
163 + A string that interpolates `%(fieldname)` from the object
164 + pointed at by a ref being shown. The format is the same as
165 + that of linkgit:git-for-each-ref[1]. When unspecified,
166 + defaults to `%(refname:short)`.
167
168 CONFIGURATION
169 -------------
builtin/tag.c
+15 -10
@@ -23,17 +23,17 @@ static const char * const git_tag_usage[] = {
23 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
24 N_("git tag -d <tagname>..."),
25 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
26 - "\n\t\t[<pattern>...]"),
26 + "\n\t\t[--format=<format>] [<pattern>...]"),
27 N_("git tag -v <tagname>..."),
28 NULL
29 };
30
31 static unsigned int colopts;
32
33 -static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
33 +static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
34 {
35 struct ref_array array;
36 - char *format, *to_free = NULL;
36 + char *to_free = NULL;
37 int i;
38
39 memset(&array, 0, sizeof(array));
@@ -41,12 +41,15 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
41 if (filter->lines == -1)
42 filter->lines = 0;
43
44 - if (filter->lines) {
45 - to_free = xstrfmt("%s %%(contents:lines=%d)",
46 - "%(align:15)%(refname:short)%(end)", filter->lines);
47 - format = to_free;
48 - } else
49 - format = "%(refname:short)";
44 + if (!format) {
45 + if (filter->lines) {
46 + to_free = xstrfmt("%s %%(contents:lines=%d)",
47 + "%(align:15)%(refname:short)%(end)",
48 + filter->lines);
49 + format = to_free;
50 + } else
51 + format = "%(refname:short)";
52 + }
53
54 verify_ref_format(format);
55 filter_refs(&array, filter, FILTER_REFS_TAGS);
@@ -330,6 +333,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
333 struct strbuf err = STRBUF_INIT;
334 struct ref_filter filter;
335 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
336 + const char *format = NULL;
337 struct option options[] = {
338 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
339 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -362,6 +366,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
366 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
367 N_("print only tags of the object"), 0, parse_opt_object_name
368 },
369 + OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
370 OPT_END()
371 };
372
@@ -402,7 +407,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
407 run_column_filter(colopts, &copts);
408 }
409 filter.name_patterns = argv;
405 - ret = list_tags(&filter, sorting);
410 + ret = list_tags(&filter, sorting, format);
411 if (column_active(colopts))
412 stop_column_filter();
413 return ret;
t/t7004-tag.sh
+12
@@ -1519,4 +1519,16 @@ EOF"
1519 test_cmp expect actual
1520 '
1521
1522 +test_expect_success '--format should list tags as per format given' '
1523 + cat >expect <<-\EOF &&
1524 + refname : refs/tags/foo1.10
1525 + refname : refs/tags/foo1.3
1526 + refname : refs/tags/foo1.6
1527 + refname : refs/tags/foo1.6-rc1
1528 + refname : refs/tags/foo1.6-rc2
1529 + EOF
1530 + git tag -l --format="refname : %(refname)" "foo*" >actual &&
1531 + test_cmp expect actual
1532 +'
1533 +
1534 test_done