Skip to content

Commit 1f2705e

Browse files
committed
Merge branch 'jk/tag-list-multiple-patterns' into maint
* jk/tag-list-multiple-patterns: tag: accept multiple patterns for --list
2 parents 1320352 + 588d0e8 commit 1f2705e

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

Documentation/git-tag.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SYNOPSIS
1212
'git tag' [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]
1313
<tagname> [<commit> | <object>]
1414
'git tag' -d <tagname>...
15-
'git tag' [-n[<num>]] -l [--contains <commit>] [<pattern>]
15+
'git tag' [-n[<num>]] -l [--contains <commit>] [<pattern>...]
1616
'git tag' -v <tagname>...
1717

1818
DESCRIPTION
@@ -69,8 +69,11 @@ OPTIONS
6969
If the tag is not annotated, the commit message is displayed instead.
7070

7171
-l <pattern>::
72-
List tags with names that match the given pattern (or all if no pattern is given).
73-
Typing "git tag" without arguments, also lists all tags.
72+
List tags with names that match the given pattern (or all if no
73+
pattern is given). Running "git tag" without arguments also
74+
lists all tags. The pattern is a shell wildcard (i.e., matched
75+
using fnmatch(3)). Multiple patterns may be given; if any of
76+
them matches, the tag is shown.
7477

7578
--contains <commit>::
7679
Only list tags which contain the specified commit.

builtin/tag.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,36 @@
1616
static const char * const git_tag_usage[] = {
1717
"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
1818
"git tag -d <tagname>...",
19-
"git tag -l [-n[<num>]] [<pattern>]",
19+
"git tag -l [-n[<num>]] [<pattern>...]",
2020
"git tag -v <tagname>...",
2121
NULL
2222
};
2323

2424
static char signingkey[1000];
2525

2626
struct tag_filter {
27-
const char *pattern;
27+
const char **patterns;
2828
int lines;
2929
struct commit_list *with_commit;
3030
};
3131

32+
static int match_pattern(const char **patterns, const char *ref)
33+
{
34+
/* no pattern means match everything */
35+
if (!*patterns)
36+
return 1;
37+
for (; *patterns; patterns++)
38+
if (!fnmatch(*patterns, ref, 0))
39+
return 1;
40+
return 0;
41+
}
42+
3243
static int show_reference(const char *refname, const unsigned char *sha1,
3344
int flag, void *cb_data)
3445
{
3546
struct tag_filter *filter = cb_data;
3647

37-
if (!fnmatch(filter->pattern, refname, 0)) {
48+
if (match_pattern(filter->patterns, refname)) {
3849
int i;
3950
unsigned long size;
4051
enum object_type type;
@@ -88,15 +99,12 @@ static int show_reference(const char *refname, const unsigned char *sha1,
8899
return 0;
89100
}
90101

91-
static int list_tags(const char *pattern, int lines,
102+
static int list_tags(const char **patterns, int lines,
92103
struct commit_list *with_commit)
93104
{
94105
struct tag_filter filter;
95106

96-
if (pattern == NULL)
97-
pattern = "*";
98-
99-
filter.pattern = pattern;
107+
filter.patterns = patterns;
100108
filter.lines = lines;
101109
filter.with_commit = with_commit;
102110

@@ -425,7 +433,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
425433
if (list + delete + verify > 1)
426434
usage_with_options(git_tag_usage, options);
427435
if (list)
428-
return list_tags(argv[0], lines == -1 ? 0 : lines,
436+
return list_tags(argv, lines == -1 ? 0 : lines,
429437
with_commit);
430438
if (lines != -1)
431439
die(_("-n option is only allowed with -l."));

t/t7004-tag.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ test_expect_success \
257257
test_cmp expect actual
258258
'
259259

260+
test_expect_success 'tag -l can accept multiple patterns' '
261+
git tag -l "v1*" "v0*" >actual &&
262+
test_cmp expect actual
263+
'
264+
260265
# creating and verifying lightweight tags:
261266

262267
test_expect_success \

0 commit comments

Comments
 (0)