Skip to content

Commit df09474

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7cc53e commit df09474

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

Documentation/git-tag.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ SYNOPSIS
1313
<tagname> [<commit> | <object>]
1414
'git tag' -d <tagname>...
1515
'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>...]
1718
'git tag' -v <tagname>...
1819

1920
DESCRIPTION
@@ -158,6 +159,11 @@ This option is only applicable when listing tags without annotation lines.
158159
The object that the new tag will refer to, usually a commit.
159160
Defaults to HEAD.
160161

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)`.
161167

162168
CONFIGURATION
163169
-------------

builtin/tag.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,33 @@ static const char * const git_tag_usage[] = {
2323
N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
2424
N_("git tag -d <tagname>..."),
2525
N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
26-
"\n\t\t[<pattern>...]"),
26+
"\n\t\t[--format=<format>] [<pattern>...]"),
2727
N_("git tag -v <tagname>..."),
2828
NULL
2929
};
3030

3131
static unsigned int colopts;
3232

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)
3434
{
3535
struct ref_array array;
36-
char *format, *to_free = NULL;
36+
char *to_free = NULL;
3737
int i;
3838

3939
memset(&array, 0, sizeof(array));
4040

4141
if (filter->lines == -1)
4242
filter->lines = 0;
4343

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+
}
5053

5154
verify_ref_format(format);
5255
filter_refs(&array, filter, FILTER_REFS_TAGS);
@@ -330,6 +333,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
330333
struct strbuf err = STRBUF_INIT;
331334
struct ref_filter filter;
332335
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
336+
const char *format = NULL;
333337
struct option options[] = {
334338
OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
335339
{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -362,6 +366,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
362366
OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
363367
N_("print only tags of the object"), 0, parse_opt_object_name
364368
},
369+
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
365370
OPT_END()
366371
};
367372

@@ -402,7 +407,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
402407
run_column_filter(colopts, &copts);
403408
}
404409
filter.name_patterns = argv;
405-
ret = list_tags(&filter, sorting);
410+
ret = list_tags(&filter, sorting, format);
406411
if (column_active(colopts))
407412
stop_column_filter();
408413
return ret;

t/t7004-tag.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,4 +1519,16 @@ EOF"
15191519
test_cmp expect actual
15201520
'
15211521

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+
15221534
test_done

0 commit comments

Comments
 (0)