Skip to content

Commit b37f26d

Browse files
committed
Merge branch 'jg/tag-contains'
* jg/tag-contains: git-tag: Add --contains option Make has_commit() non-static Make opt_parse_with_commit() non-static
2 parents 2925414 + 32c35cf commit b37f26d

File tree

8 files changed

+185
-38
lines changed

8 files changed

+185
-38
lines changed

Documentation/git-tag.txt

Lines changed: 4 additions & 1 deletion
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
<name> [<commit> | <object>]
1414
'git tag' -d <name>...
15-
'git tag' [-n[<num>]] -l [<pattern>]
15+
'git tag' [-n[<num>]] -l [--contains <commit>] [<pattern>]
1616
'git tag' -v <name>...
1717

1818
DESCRIPTION
@@ -68,6 +68,9 @@ OPTIONS
6868
List tags with names that match the given pattern (or all if no pattern is given).
6969
Typing "git tag" without arguments, also lists all tags.
7070

71+
--contains <commit>::
72+
Only list tags which contain the specified commit.
73+
7174
-m <msg>::
7275
Use the given tag message (instead of prompting).
7376
If multiple `-m` options are given, their values are

builtin-branch.c

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,6 @@ struct ref_list {
193193
int kinds;
194194
};
195195

196-
static int has_commit(struct commit *commit, struct commit_list *with_commit)
197-
{
198-
if (!with_commit)
199-
return 1;
200-
while (with_commit) {
201-
struct commit *other;
202-
203-
other = with_commit->item;
204-
with_commit = with_commit->next;
205-
if (in_merge_bases(other, &commit, 1))
206-
return 1;
207-
}
208-
return 0;
209-
}
210-
211196
static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
212197
{
213198
struct ref_list *ref_list = (struct ref_list*)(cb_data);
@@ -231,7 +216,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
231216
return error("branch '%s' does not point at a commit", refname);
232217

233218
/* Filter with with_commit if specified */
234-
if (!has_commit(commit, ref_list->with_commit))
219+
if (!is_descendant_of(commit, ref_list->with_commit))
235220
return 0;
236221

237222
/* Don't add types the caller doesn't want */
@@ -401,7 +386,8 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
401386
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
402387

403388
detached = (detached && (kinds & REF_LOCAL_BRANCH));
404-
if (detached && head_commit && has_commit(head_commit, with_commit)) {
389+
if (detached && head_commit &&
390+
is_descendant_of(head_commit, with_commit)) {
405391
struct ref_item item;
406392
item.name = xstrdup("(no branch)");
407393
item.kind = REF_LOCAL_BRANCH;
@@ -466,22 +452,6 @@ static void rename_branch(const char *oldname, const char *newname, int force)
466452
strbuf_release(&newsection);
467453
}
468454

469-
static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
470-
{
471-
unsigned char sha1[20];
472-
struct commit *commit;
473-
474-
if (!arg)
475-
return -1;
476-
if (get_sha1(arg, sha1))
477-
die("malformed object name %s", arg);
478-
commit = lookup_commit_reference(sha1);
479-
if (!commit)
480-
die("no such commit %s", arg);
481-
commit_list_insert(commit, opt->value);
482-
return 0;
483-
}
484-
485455
static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset)
486456
{
487457
merge_filter = ((opt->long_name[0] == 'n')
@@ -517,13 +487,13 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
517487
OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
518488
"print only branches that contain the commit",
519489
PARSE_OPT_LASTARG_DEFAULT,
520-
opt_parse_with_commit, (intptr_t)"HEAD",
490+
parse_opt_with_commit, (intptr_t)"HEAD",
521491
},
522492
{
523493
OPTION_CALLBACK, 0, "with", &with_commit, "commit",
524494
"print only branches that contain the commit",
525495
PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
526-
opt_parse_with_commit, (intptr_t) "HEAD",
496+
parse_opt_with_commit, (intptr_t) "HEAD",
527497
},
528498
OPT__ABBREV(&abbrev),
529499

builtin-tag.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static char signingkey[1000];
2626
struct tag_filter {
2727
const char *pattern;
2828
int lines;
29+
struct commit_list *with_commit;
2930
};
3031

3132
#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
@@ -42,6 +43,16 @@ static int show_reference(const char *refname, const unsigned char *sha1,
4243
char *buf, *sp, *eol;
4344
size_t len;
4445

46+
if (filter->with_commit) {
47+
struct commit *commit;
48+
49+
commit = lookup_commit_reference_gently(sha1, 1);
50+
if (!commit)
51+
return 0;
52+
if (!is_descendant_of(commit, filter->with_commit))
53+
return 0;
54+
}
55+
4556
if (!filter->lines) {
4657
printf("%s\n", refname);
4758
return 0;
@@ -79,7 +90,8 @@ static int show_reference(const char *refname, const unsigned char *sha1,
7990
return 0;
8091
}
8192

82-
static int list_tags(const char *pattern, int lines)
93+
static int list_tags(const char *pattern, int lines,
94+
struct commit_list *with_commit)
8395
{
8496
struct tag_filter filter;
8597

@@ -88,6 +100,7 @@ static int list_tags(const char *pattern, int lines)
88100

89101
filter.pattern = pattern;
90102
filter.lines = lines;
103+
filter.with_commit = with_commit;
91104

92105
for_each_tag_ref(show_reference, (void *) &filter);
93106

@@ -360,6 +373,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
360373
list = 0, delete = 0, verify = 0;
361374
const char *msgfile = NULL, *keyid = NULL;
362375
struct msg_arg msg = { 0, STRBUF_INIT };
376+
struct commit_list *with_commit = NULL;
363377
struct option options[] = {
364378
OPT_BOOLEAN('l', NULL, &list, "list tag names"),
365379
{ OPTION_INTEGER, 'n', NULL, &lines, NULL,
@@ -378,6 +392,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
378392
OPT_STRING('u', NULL, &keyid, "key-id",
379393
"use another key to sign the tag"),
380394
OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
395+
396+
OPT_GROUP("Tag listing options"),
397+
{
398+
OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
399+
"print only tags that contain the commit",
400+
PARSE_OPT_LASTARG_DEFAULT,
401+
parse_opt_with_commit, (intptr_t)"HEAD",
402+
},
381403
OPT_END()
382404
};
383405

@@ -402,9 +424,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
402424
if (list + delete + verify > 1)
403425
usage_with_options(git_tag_usage, options);
404426
if (list)
405-
return list_tags(argv[0], lines == -1 ? 0 : lines);
427+
return list_tags(argv[0], lines == -1 ? 0 : lines,
428+
with_commit);
406429
if (lines != -1)
407430
die("-n option is only allowed with -l.");
431+
if (with_commit)
432+
die("--contains option is only allowed with -l.");
408433
if (delete)
409434
return for_each_tag_name(argv, delete_tag);
410435
if (verify)

commit.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,21 @@ struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
705705
return get_merge_bases_many(one, 1, &two, cleanup);
706706
}
707707

708+
int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
709+
{
710+
if (!with_commit)
711+
return 1;
712+
while (with_commit) {
713+
struct commit *other;
714+
715+
other = with_commit->item;
716+
with_commit = with_commit->next;
717+
if (in_merge_bases(other, &commit, 1))
718+
return 1;
719+
}
720+
return 0;
721+
}
722+
708723
int in_merge_bases(struct commit *commit, struct commit **reference, int num)
709724
{
710725
struct commit_list *bases, *b;

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ extern int is_repository_shallow(void);
133133
extern struct commit_list *get_shallow_commits(struct object_array *heads,
134134
int depth, int shallow_flag, int not_shallow_flag);
135135

136+
int is_descendant_of(struct commit *, struct commit_list *);
136137
int in_merge_bases(struct commit *, struct commit **, int);
137138

138139
extern int interactive_add(int argc, const char **argv, const char *prefix);

parse-options.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "git-compat-util.h"
22
#include "parse-options.h"
33
#include "cache.h"
4+
#include "commit.h"
45

56
#define OPT_SHORT 1
67
#define OPT_UNSET 2
@@ -506,6 +507,22 @@ int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
506507
return 0;
507508
}
508509

510+
int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
511+
{
512+
unsigned char sha1[20];
513+
struct commit *commit;
514+
515+
if (!arg)
516+
return -1;
517+
if (get_sha1(arg, sha1))
518+
return error("malformed object name %s", arg);
519+
commit = lookup_commit_reference(sha1);
520+
if (!commit)
521+
return error("no such commit %s", arg);
522+
commit_list_insert(commit, opt->value);
523+
return 0;
524+
}
525+
509526
/*
510527
* This should really be OPTION_FILENAME type as a part of
511528
* parse_options that take prefix to do this while parsing.

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ extern int parse_options_end(struct parse_opt_ctx_t *ctx);
151151
extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
152152
extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
153153
extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
154+
extern int parse_opt_with_commit(const struct option *, const char *, int);
154155

155156
#define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose")
156157
#define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet")

t/t7004-tag.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,121 @@ test_expect_success 'filename for the message is relative to cwd' '
10901090
git cat-file tag tag-from-subdir-2 | grep "in sub directory"
10911091
'
10921092
1093+
# create a few more commits to test --contains
1094+
1095+
hash1=$(git rev-parse HEAD)
1096+
1097+
test_expect_success 'creating second commit and tag' '
1098+
echo foo-2.0 >foo &&
1099+
git add foo &&
1100+
git commit -m second
1101+
git tag v2.0
1102+
'
1103+
1104+
hash2=$(git rev-parse HEAD)
1105+
1106+
test_expect_success 'creating third commit without tag' '
1107+
echo foo-dev >foo &&
1108+
git add foo &&
1109+
git commit -m third
1110+
'
1111+
1112+
hash3=$(git rev-parse HEAD)
1113+
1114+
# simple linear checks of --continue
1115+
1116+
cat > expected <<EOF
1117+
v0.2.1
1118+
v1.0
1119+
v1.0.1
1120+
v1.1.3
1121+
v2.0
1122+
EOF
1123+
1124+
test_expect_success 'checking that first commit is in all tags (hash)' "
1125+
git tag -l --contains $hash1 v* >actual
1126+
test_cmp expected actual
1127+
"
1128+
1129+
# other ways of specifying the commit
1130+
test_expect_success 'checking that first commit is in all tags (tag)' "
1131+
git tag -l --contains v1.0 v* >actual
1132+
test_cmp expected actual
1133+
"
1134+
1135+
test_expect_success 'checking that first commit is in all tags (relative)' "
1136+
git tag -l --contains HEAD~2 v* >actual
1137+
test_cmp expected actual
1138+
"
1139+
1140+
cat > expected <<EOF
1141+
v2.0
1142+
EOF
1143+
1144+
test_expect_success 'checking that second commit only has one tag' "
1145+
git tag -l --contains $hash2 v* >actual
1146+
test_cmp expected actual
1147+
"
1148+
1149+
1150+
cat > expected <<EOF
1151+
EOF
1152+
1153+
test_expect_success 'checking that third commit has no tags' "
1154+
git tag -l --contains $hash3 v* >actual
1155+
test_cmp expected actual
1156+
"
1157+
1158+
# how about a simple merge?
1159+
1160+
test_expect_success 'creating simple branch' '
1161+
git branch stable v2.0 &&
1162+
git checkout stable &&
1163+
echo foo-3.0 > foo &&
1164+
git commit foo -m fourth
1165+
git tag v3.0
1166+
'
1167+
1168+
hash4=$(git rev-parse HEAD)
1169+
1170+
cat > expected <<EOF
1171+
v3.0
1172+
EOF
1173+
1174+
test_expect_success 'checking that branch head only has one tag' "
1175+
git tag -l --contains $hash4 v* >actual
1176+
test_cmp expected actual
1177+
"
1178+
1179+
test_expect_success 'merging original branch into this branch' '
1180+
git merge --strategy=ours master &&
1181+
git tag v4.0
1182+
'
1183+
1184+
cat > expected <<EOF
1185+
v4.0
1186+
EOF
1187+
1188+
test_expect_success 'checking that original branch head has one tag now' "
1189+
git tag -l --contains $hash3 v* >actual
1190+
test_cmp expected actual
1191+
"
1192+
1193+
cat > expected <<EOF
1194+
v0.2.1
1195+
v1.0
1196+
v1.0.1
1197+
v1.1.3
1198+
v2.0
1199+
v3.0
1200+
v4.0
1201+
EOF
1202+
1203+
test_expect_success 'checking that initial commit is in all tags' "
1204+
git tag -l --contains $hash1 v* >actual
1205+
test_cmp expected actual
1206+
"
1207+
10931208
# mixing modes and options:
10941209
10951210
test_expect_success 'mixing incompatibles modes and options is forbidden' '

0 commit comments

Comments
 (0)