Skip to content

Commit ae7706b

Browse files
tgrennangitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 828ea97 commit ae7706b

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

Documentation/git-tag.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ 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>] [--points-at <object>]
16+
[<pattern>...]
1617
'git tag' -v <tagname>...
1718

1819
DESCRIPTION
@@ -86,6 +87,9 @@ OPTIONS
8687
--contains <commit>::
8788
Only list tags which contain the specified commit.
8889

90+
--points-at <object>::
91+
Only list tags of the given object.
92+
8993
-m <msg>::
9094
--message=<msg>::
9195
Use the given tag message (instead of prompting).

builtin/tag.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include "diff.h"
1616
#include "revision.h"
1717
#include "gpg-interface.h"
18+
#include "sha1-array.h"
1819

1920
static const char * const git_tag_usage[] = {
2021
"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
2122
"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>...]",
2325
"git tag -v <tagname>...",
2426
NULL
2527
};
@@ -30,6 +32,8 @@ struct tag_filter {
3032
struct commit_list *with_commit;
3133
};
3234

35+
static struct sha1_array points_at;
36+
3337
static int match_pattern(const char **patterns, const char *ref)
3438
{
3539
/* no pattern means match everything */
@@ -41,6 +45,24 @@ static int match_pattern(const char **patterns, const char *ref)
4145
return 0;
4246
}
4347

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+
4466
static int in_commit_list(const struct commit_list *want, struct commit *c)
4567
{
4668
for (; want; want = want->next)
@@ -105,6 +127,9 @@ static int show_reference(const char *refname, const unsigned char *sha1,
105127
return 0;
106128
}
107129

130+
if (points_at.nr && !match_points_at(refname, sha1))
131+
return 0;
132+
108133
if (!filter->lines) {
109134
printf("%s\n", refname);
110135
return 0;
@@ -375,6 +400,23 @@ static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
375400
return check_refname_format(sb->buf, 0);
376401
}
377402

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+
378420
int cmd_tag(int argc, const char **argv, const char *prefix)
379421
{
380422
struct strbuf buf = STRBUF_INIT;
@@ -417,6 +459,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
417459
PARSE_OPT_LASTARG_DEFAULT,
418460
parse_opt_with_commit, (intptr_t)"HEAD",
419461
},
462+
{
463+
OPTION_CALLBACK, 0, "points-at", NULL, "object",
464+
"print only tags of the object", 0, parse_opt_points_at
465+
},
420466
OPT_END()
421467
};
422468

@@ -448,6 +494,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
448494
die(_("-n option is only allowed with -l."));
449495
if (with_commit)
450496
die(_("--contains option is only allowed with -l."));
497+
if (points_at.nr)
498+
die(_("--points-at option is only allowed with -l."));
451499
if (delete)
452500
return for_each_tag_name(argv, delete_tag);
453501
if (verify)

t/t7004-tag.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,4 +1269,43 @@ test_expect_success 'mixing incompatibles modes and options is forbidden' '
12691269
test_must_fail git tag -v -s
12701270
'
12711271
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+
12721311
test_done

0 commit comments

Comments
 (0)