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 <peff@peff.net> 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 Jul 7, 2015 at 21:36 UTC d325406ef2f3c819d02ac838fb2a3f8e021d08ae
3 files changed +30 -2
Documentation/git-for-each-ref.txt
+3
@@ -10,6 +10,7 @@ SYNOPSIS
10 [verse]
11 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
12 [(--sort=<key>)...] [--format=<format>] [<pattern>...]
13 + [--points-at <object>]
14
15 DESCRIPTION
16 -----------
@@ -62,6 +63,8 @@ OPTIONS
63 the specified host language. This is meant to produce
64 a scriptlet that can directly be `eval`ed.
65
66 +--points-at <object>::
67 + Only list refs which points at the given object.
68
69 FIELD NAMES
70 -----------
builtin/for-each-ref.c
+7 -2
@@ -7,6 +7,7 @@
7
8 static char const * const for_each_ref_usage[] = {
9 N_("git for-each-ref [<options>] [<pattern>]"),
10 + N_("git for-each-ref [--points-at <object>]"),
11 NULL
12 };
13
@@ -34,9 +35,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
35 OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
36 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
37 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),
41 OPT_END(),
42 };
43
44 + memset(&array, 0, sizeof(array));
45 + memset(&filter, 0, sizeof(filter));
46 +
47 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
48 if (maxcount < 0) {
49 error("invalid --count argument: `%d'", maxcount);
@@ -55,8 +62,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
62 /* for warn_ambiguous_refs */
63 git_config(git_default_config, NULL);
64
58 - memset(&array, 0, sizeof(array));
59 - memset(&filter, 0, sizeof(filter));
65 filter.name_patterns = argv;
66 filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
67 ref_array_sort(sorting, &array);
t/t6302-for-each-ref-filter.sh
+20
@@ -23,4 +23,24 @@ test_expect_success 'setup some history and refs' '
23 git update-ref refs/odd/spot master
24 '
25
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 +
46 test_done