tag: add --points-at list option

This filters the list for tags of the given object. Example, john$ git tag v1.0-john v1.0 john$ git tag -l --points-at v1.0 v1.0-john v1.0 Signed-off-by: Tom Grennan <tmgrennan@gmail.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tom Grennan committed Feb 8, 2012 at 15:03 UTC ae7706b9ac98e3c412d799c910de604347ffce5d
3 files changed +93 -2
Documentation/git-tag.txt
+5 -1
@@ -12,7 +12,8 @@ SYNOPSIS
12 'git tag' [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]
13 <tagname> [<commit> | <object>]
14 'git tag' -d <tagname>...
15 -'git tag' [-n[<num>]] -l [--contains <commit>] [<pattern>...]
15 +'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
16 + [<pattern>...]
17 'git tag' -v <tagname>...
18
19 DESCRIPTION
@@ -86,6 +87,9 @@ OPTIONS
87 --contains <commit>::
88 Only list tags which contain the specified commit.
89
90 +--points-at <object>::
91 + Only list tags of the given object.
92 +
93 -m <msg>::
94 --message=<msg>::
95 Use the given tag message (instead of prompting).
builtin/tag.c
+49 -1
@@ -15,11 +15,13 @@
15 #include "diff.h"
16 #include "revision.h"
17 #include "gpg-interface.h"
18 +#include "sha1-array.h"
19
20 static const char * const git_tag_usage[] = {
21 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
22 "git tag -d <tagname>...",
22 - "git tag -l [-n[<num>]] [<pattern>...]",
23 + "git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] "
24 + "\n\t\t[<pattern>...]",
25 "git tag -v <tagname>...",
26 NULL
27 };
@@ -30,6 +32,8 @@ struct tag_filter {
32 struct commit_list *with_commit;
33 };
34
35 +static struct sha1_array points_at;
36 +
37 static int match_pattern(const char **patterns, const char *ref)
38 {
39 /* no pattern means match everything */
@@ -41,6 +45,24 @@ static int match_pattern(const char **patterns, const char *ref)
45 return 0;
46 }
47
48 +static const unsigned char *match_points_at(const char *refname,
49 + const unsigned char *sha1)
50 +{
51 + const unsigned char *tagged_sha1 = NULL;
52 + struct object *obj;
53 +
54 + if (sha1_array_lookup(&points_at, sha1) >= 0)
55 + return sha1;
56 + obj = parse_object(sha1);
57 + if (!obj)
58 + die(_("malformed object at '%s'"), refname);
59 + if (obj->type == OBJ_TAG)
60 + tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
61 + if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
62 + return tagged_sha1;
63 + return NULL;
64 +}
65 +
66 static int in_commit_list(const struct commit_list *want, struct commit *c)
67 {
68 for (; want; want = want->next)
@@ -105,6 +127,9 @@ static int show_reference(const char *refname, const unsigned char *sha1,
127 return 0;
128 }
129
130 + if (points_at.nr && !match_points_at(refname, sha1))
131 + return 0;
132 +
133 if (!filter->lines) {
134 printf("%s\n", refname);
135 return 0;
@@ -375,6 +400,23 @@ static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
400 return check_refname_format(sb->buf, 0);
401 }
402
403 +int parse_opt_points_at(const struct option *opt __attribute__ ((unused)),
404 + const char *arg, int unset)
405 +{
406 + unsigned char sha1[20];
407 +
408 + if (unset) {
409 + sha1_array_clear(&points_at);
410 + return 0;
411 + }
412 + if (!arg)
413 + return error(_("switch 'points-at' requires an object"));
414 + if (get_sha1(arg, sha1))
415 + return error(_("malformed object name '%s'"), arg);
416 + sha1_array_append(&points_at, sha1);
417 + return 0;
418 +}
419 +
420 int cmd_tag(int argc, const char **argv, const char *prefix)
421 {
422 struct strbuf buf = STRBUF_INIT;
@@ -417,6 +459,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
459 PARSE_OPT_LASTARG_DEFAULT,
460 parse_opt_with_commit, (intptr_t)"HEAD",
461 },
462 + {
463 + OPTION_CALLBACK, 0, "points-at", NULL, "object",
464 + "print only tags of the object", 0, parse_opt_points_at
465 + },
466 OPT_END()
467 };
468
@@ -448,6 +494,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
494 die(_("-n option is only allowed with -l."));
495 if (with_commit)
496 die(_("--contains option is only allowed with -l."));
497 + if (points_at.nr)
498 + die(_("--points-at option is only allowed with -l."));
499 if (delete)
500 return for_each_tag_name(argv, delete_tag);
501 if (verify)
t/t7004-tag.sh
+39
@@ -1269,4 +1269,43 @@ test_expect_success 'mixing incompatibles modes and options is forbidden' '
1269 test_must_fail git tag -v -s
1270 '
1271
1272 +# check points-at
1273 +
1274 +test_expect_success '--points-at cannot be used in non-list mode' '
1275 + test_must_fail git tag --points-at=v4.0 foo
1276 +'
1277 +
1278 +test_expect_success '--points-at finds lightweight tags' '
1279 + echo v4.0 >expect &&
1280 + git tag --points-at v4.0 >actual &&
1281 + test_cmp expect actual
1282 +'
1283 +
1284 +test_expect_success '--points-at finds annotated tags of commits' '
1285 + git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
1286 + echo annotated-v4.0 >expect &&
1287 + git tag -l --points-at v4.0 "annotated*" >actual &&
1288 + test_cmp expect actual
1289 +'
1290 +
1291 +test_expect_success '--points-at finds annotated tags of tags' '
1292 + git tag -m "describing the v4.0 tag object" \
1293 + annotated-again-v4.0 annotated-v4.0 &&
1294 + cat >expect <<-\EOF &&
1295 + annotated-again-v4.0
1296 + annotated-v4.0
1297 + EOF
1298 + git tag --points-at=annotated-v4.0 >actual &&
1299 + test_cmp expect actual
1300 +'
1301 +
1302 +test_expect_success 'multiple --points-at are OR-ed together' '
1303 + cat >expect <<-\EOF &&
1304 + v2.0
1305 + v3.0
1306 + EOF
1307 + git tag --points-at=v2.0 --points-at=v3.0 >actual &&
1308 + test_cmp expect actual
1309 +'
1310 +
1311 test_done