[PATCHv3 5/5] for-each-ref --exclude option
Example,
$ git for-each-ref --format="%(refname)" refs/remotes/origin
refs/remotes/origin/HEAD
refs/remotes/origin/maint
refs/remotes/origin/master
refs/remotes/origin/next
refs/remotes/origin/pu
refs/remotes/origin/todo
$ ./git-for-each-ref --format="%(refname)" --exclude "*/HEAD" refs/remotes/origin
refs/remotes/origin/maint
refs/remotes/origin/master
refs/remotes/origin/next
refs/remotes/origin/pu
refs/remotes/origin/todo
Signed-off-by: Tom Grennan <tmgrennan@gmail.com>
---
Documentation/git-for-each-ref.txt | 7 ++++++-
builtin/for-each-ref.c | 8 +++++++-
t/t6300-for-each-ref.sh | 11 +++++++++++
3 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index c872b88..5f19a8b 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -9,7 +9,8 @@ SYNOPSIS
--------
[verse]
'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
- [(--sort=<key>)...] [--format=<format>] [<pattern>...]
+ [(--sort=<key>)...] [--format=<format>]
+ [--exclude=<pattern>] [<pattern>...]
DESCRIPTION
-----------
@@ -47,6 +48,10 @@ OPTIONS
`xx`; for example `%00` interpolates to `\0` (NUL),
`%09` to `\t` (TAB) and `%0a` to `\n` (LF).
+--exclude <pattern>::
+ Ignore refs matching the given pattern. This has precedence
+ over any other pattern match arguments.
+
<pattern>...::
If one or more patterns are given, only refs are shown that
match against at least one pattern, either using fnmatch(3) or
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index bd6a114..783f59f 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -91,6 +91,9 @@ static const char **used_atom;
static cmp_type *used_atom_type;
static int used_atom_cnt, sort_atom_limit, need_tagged, need_symref;
+/* list of ref patterns and ref groups (i.e. foo/) to ignore */
+static struct string_list exclude = STRING_LIST_INIT_NODUP;
+
/*
* Used to parse format string and sort specifiers
*/
@@ -781,7 +784,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
struct refinfo *ref;
int cnt;
- if (!match_pattern(refname, cb->grab_pattern, NULL, FNM_PATHNAME))
+ if (!match_pattern(refname, cb->grab_pattern, &exclude, FNM_PATHNAME))
return 0;
/*
@@ -985,6 +988,9 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
OPT_STRING( 0 , "format", &format, "format", "format to use for the output"),
OPT_CALLBACK(0 , "sort", sort_tail, "key",
"field name to sort on", &opt_parse_sort),
+ OPT_CALLBACK(0, "exclude", &exclude, "pattern",
+ "ignore pattern matching refs",
+ parse_opt_string_list),
OPT_END(),
};
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 1721784..26df442 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -243,6 +243,17 @@ test_expect_success 'Verify descending sort' '
'
cat >expected <<\EOF
+refs/tags/testtag
+refs/heads/master
+EOF
+
+test_expect_success 'Verify exclusion with sort' '
+ git for-each-ref --format="%(refname)" --sort=-refname \
+ --exclude "*origin*" >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
'refs/heads/master'
'refs/remotes/origin/master'
'refs/tags/testtag'
--
1.7.8