Skip to content

Commit d325406

Browse files
KarthikNayakgitster
authored andcommitted
for-each-ref: add '--points-at' option
Add the '--points-at' option provided by 'ref-filter'. The option lets the user to list only refs which points at the given object. Add documentation and tests for the same. Based-on-patch-by: Jeff King <[email protected]> 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 6841104 commit d325406

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
1212
[(--sort=<key>)...] [--format=<format>] [<pattern>...]
13+
[--points-at <object>]
1314

1415
DESCRIPTION
1516
-----------
@@ -62,6 +63,8 @@ OPTIONS
6263
the specified host language. This is meant to produce
6364
a scriptlet that can directly be `eval`ed.
6465

66+
--points-at <object>::
67+
Only list refs which points at the given object.
6568

6669
FIELD NAMES
6770
-----------

builtin/for-each-ref.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
static char const * const for_each_ref_usage[] = {
99
N_("git for-each-ref [<options>] [<pattern>]"),
10+
N_("git for-each-ref [--points-at <object>]"),
1011
NULL
1112
};
1213

@@ -34,9 +35,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
3435
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
3536
OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
3637
N_("field name to sort on"), &parse_opt_ref_sorting),
38+
OPT_CALLBACK(0, "points-at", &filter.points_at,
39+
N_("object"), N_("print only refs which points at the given object"),
40+
parse_opt_object_name),
3741
OPT_END(),
3842
};
3943

44+
memset(&array, 0, sizeof(array));
45+
memset(&filter, 0, sizeof(filter));
46+
4047
parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
4148
if (maxcount < 0) {
4249
error("invalid --count argument: `%d'", maxcount);
@@ -55,8 +62,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
5562
/* for warn_ambiguous_refs */
5663
git_config(git_default_config, NULL);
5764

58-
memset(&array, 0, sizeof(array));
59-
memset(&filter, 0, sizeof(filter));
6065
filter.name_patterns = argv;
6166
filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
6267
ref_array_sort(sorting, &array);

t/t6302-for-each-ref-filter.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,24 @@ test_expect_success 'setup some history and refs' '
2323
git update-ref refs/odd/spot master
2424
'
2525

26+
test_expect_success 'filtering with --points-at' '
27+
cat >expect <<-\EOF &&
28+
refs/heads/master
29+
refs/odd/spot
30+
refs/tags/three
31+
EOF
32+
git for-each-ref --format="%(refname)" --points-at=master >actual &&
33+
test_cmp expect actual
34+
'
35+
36+
test_expect_success 'check signed tags with --points-at' '
37+
sed -e "s/Z$//" >expect <<-\EOF &&
38+
refs/heads/side Z
39+
refs/tags/four Z
40+
refs/tags/signed-tag four
41+
EOF
42+
git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
43+
test_cmp expect actual
44+
'
45+
2646
test_done

0 commit comments

Comments
 (0)