Skip to content

Commit 5242860

Browse files
KarthikNayakgitster
authored andcommitted
tag.c: implement '--merged' and '--no-merged' options
Use 'ref-filter' APIs to implement the '--merged' and '--no-merged' options into 'tag.c'. The '--merged' option lets the user to only list tags merged into the named commit. The '--no-merged' option lets the user to only list tags not merged into the named commit. If no object is provided it assumes HEAD as the object. Add documentation and tests 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 df09474 commit 5242860

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Documentation/git-tag.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SYNOPSIS
1414
'git tag' -d <tagname>...
1515
'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
1616
[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
17-
[--format=<format>] [<pattern>...]
17+
[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]
1818
'git tag' -v <tagname>...
1919

2020
DESCRIPTION
@@ -165,6 +165,11 @@ This option is only applicable when listing tags without annotation lines.
165165
that of linkgit:git-for-each-ref[1]. When unspecified,
166166
defaults to `%(refname:short)`.
167167

168+
--[no-]merged [<commit>]::
169+
Only list tags whose tips are reachable, or not reachable
170+
if '--no-merged' is used, from the specified commit ('HEAD'
171+
if not specified).
172+
168173
CONFIGURATION
169174
-------------
170175
By default, 'git tag' in sign-with-default mode (-s) will use your

builtin/tag.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ 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[--format=<format>] [<pattern>...]"),
26+
"\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
2727
N_("git tag -v <tagname>..."),
2828
NULL
2929
};
@@ -360,6 +360,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
360360
OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
361361
OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
362362
OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
363+
OPT_MERGED(&filter, N_("print only tags that are merged")),
364+
OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
363365
OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
364366
N_("field name to sort on"), &parse_opt_ref_sorting),
365367
{
@@ -418,6 +420,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
418420
die(_("--contains option is only allowed with -l."));
419421
if (filter.points_at.nr)
420422
die(_("--points-at option is only allowed with -l."));
423+
if (filter.merge_commit)
424+
die(_("--merged and --no-merged option are only allowed with -l"));
421425
if (cmdmode == 'd')
422426
return for_each_tag_name(argv, delete_tag);
423427
if (cmdmode == 'v')

t/t7004-tag.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,4 +1531,31 @@ test_expect_success '--format should list tags as per format given' '
15311531
test_cmp expect actual
15321532
'
15331533

1534+
test_expect_success 'setup --merged test tags' '
1535+
git tag mergetest-1 HEAD~2 &&
1536+
git tag mergetest-2 HEAD~1 &&
1537+
git tag mergetest-3 HEAD
1538+
'
1539+
1540+
test_expect_success '--merged cannot be used in non-list mode' '
1541+
test_must_fail git tag --merged=mergetest-2 foo
1542+
'
1543+
1544+
test_expect_success '--merged shows merged tags' '
1545+
cat >expect <<-\EOF &&
1546+
mergetest-1
1547+
mergetest-2
1548+
EOF
1549+
git tag -l --merged=mergetest-2 mergetest-* >actual &&
1550+
test_cmp expect actual
1551+
'
1552+
1553+
test_expect_success '--no-merged show unmerged tags' '
1554+
cat >expect <<-\EOF &&
1555+
mergetest-3
1556+
EOF
1557+
git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
1558+
test_cmp expect actual
1559+
'
1560+
15341561
test_done

0 commit comments

Comments
 (0)