Skip to content

Commit 7c32834

Browse files
KarthikNayakgitster
authored andcommitted
for-each-ref: add '--merged' and '--no-merged' options
Add the '--merged' and '--no-merged' options provided by 'ref-filter'. The '--merged' option lets the user to only list refs merged into the named commit. The '--no-merged' option lets the user to only list refs not merged into the named commit. 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 35257aa commit 7c32834

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +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>]
13+
[--points-at <object>] [(--merged | --no-merged) [<object>]]
1414

1515
DESCRIPTION
1616
-----------
@@ -66,6 +66,14 @@ OPTIONS
6666
--points-at <object>::
6767
Only list refs which points at the given object.
6868

69+
--merged [<object>]::
70+
Only list refs whose tips are reachable from the
71+
specified commit (HEAD if not specified).
72+
73+
--no-merged [<object>]::
74+
Only list refs whose tips are not reachable from the
75+
specified commit (HEAD if not specified).
76+
6977
FIELD NAMES
7078
-----------
7179

builtin/for-each-ref.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
static char const * const for_each_ref_usage[] = {
99
N_("git for-each-ref [<options>] [<pattern>]"),
1010
N_("git for-each-ref [--points-at <object>]"),
11+
N_("git for-each-ref [(--merged | --no-merged) [<object>]]"),
1112
NULL
1213
};
1314

@@ -38,6 +39,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
3839
OPT_CALLBACK(0, "points-at", &filter.points_at,
3940
N_("object"), N_("print only refs which points at the given object"),
4041
parse_opt_object_name),
42+
OPT_MERGED(&filter, N_("print only refs that are merged")),
43+
OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
4144
OPT_END(),
4245
};
4346

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,27 @@ test_expect_success 'check signed tags with --points-at' '
4343
test_cmp expect actual
4444
'
4545

46+
test_expect_success 'filtering with --merged' '
47+
cat >expect <<-\EOF &&
48+
refs/heads/master
49+
refs/odd/spot
50+
refs/tags/one
51+
refs/tags/three
52+
refs/tags/two
53+
EOF
54+
git for-each-ref --format="%(refname)" --merged=master >actual &&
55+
test_cmp expect actual
56+
'
57+
58+
test_expect_success 'filtering with --no-merged' '
59+
cat >expect <<-\EOF &&
60+
refs/heads/side
61+
refs/tags/double-tag
62+
refs/tags/four
63+
refs/tags/signed-tag
64+
EOF
65+
git for-each-ref --format="%(refname)" --no-merged=master >actual &&
66+
test_cmp expect actual
67+
'
68+
4669
test_done

0 commit comments

Comments
 (0)