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 <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 10, 2015 at 21:52 UTC 5242860f548d1869ac2779726ad496f0ae8ab5ca
3 files changed +38 -2
Documentation/git-tag.txt
+6 -1
@@ -14,7 +14,7 @@ SYNOPSIS
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>]
17 - [--format=<format>] [<pattern>...]
17 + [--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]
18 'git tag' -v <tagname>...
19
20 DESCRIPTION
@@ -165,6 +165,11 @@ This option is only applicable when listing tags without annotation lines.
165 that of linkgit:git-for-each-ref[1]. When unspecified,
166 defaults to `%(refname:short)`.
167
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 +
173 CONFIGURATION
174 -------------
175 By default, 'git tag' in sign-with-default mode (-s) will use your
builtin/tag.c
+5 -1
@@ -23,7 +23,7 @@ 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[--format=<format>] [<pattern>...]"),
26 + "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
27 N_("git tag -v <tagname>..."),
28 NULL
29 };
@@ -360,6 +360,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
360 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
361 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
362 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")),
365 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
366 N_("field name to sort on"), &parse_opt_ref_sorting),
367 {
@@ -418,6 +420,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
420 die(_("--contains option is only allowed with -l."));
421 if (filter.points_at.nr)
422 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"));
425 if (cmdmode == 'd')
426 return for_each_tag_name(argv, delete_tag);
427 if (cmdmode == 'v')
t/t7004-tag.sh
+27
@@ -1531,4 +1531,31 @@ test_expect_success '--format should list tags as per format given' '
1531 test_cmp expect actual
1532 '
1533
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 +
1561 test_done