rev-parse: introduce --exclude=<glob> to tame wildcards

Teach "rev-parse" the same "I'm going to glob, but omit the ones that match these patterns" feature as "rev-list". Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 1, 2013 at 12:13 UTC 9dc01bf0631b51dfe497d57942a9085e0808e205
3 files changed +43
Documentation/git-rev-parse.txt
+14
@@ -155,6 +155,20 @@ shown. If the pattern does not contain a globbing character (`?`,
155 character (`?`, `*`, or `[`), it is turned into a prefix
156 match by appending `/*`.
157
158 +--exclude=<glob-pattern>::
159 + Do not include refs matching '<glob-pattern>' that the next `--all`,
160 + `--branches`, `--tags`, `--remotes`, or `--glob` would otherwise
161 + consider. Repetitions of this option accumulate exclusion patterns
162 + up to the next `--all`, `--branches`, `--tags`, `--remotes`, or
163 + `--glob` option (other options or arguments do not clear
164 + accumlated patterns).
165 ++
166 +The patterns given should not begin with `refs/heads`, `refs/tags`, or
167 +`refs/remotes` when applied to `--branches`, `--tags`, or `--remotes`,
168 +respectively, and they must begin with `refs/` when applied to `--glob`
169 +or `--all`. If a trailing '/{asterisk}' is intended, it must be given
170 +explicitly.
171 +
172 --show-toplevel::
173 Show the absolute path of the top-level directory.
174
builtin/rev-parse.c
+17
@@ -9,6 +9,8 @@
9 #include "quote.h"
10 #include "builtin.h"
11 #include "parse-options.h"
12 +#include "diff.h"
13 +#include "revision.h"
14
15 #define DO_REVS 1
16 #define DO_NOREV 2
@@ -30,6 +32,8 @@ static int abbrev_ref;
32 static int abbrev_ref_strict;
33 static int output_sq;
34
35 +static struct string_list *ref_excludes;
36 +
37 /*
38 * Some arguments are relevant "revision" arguments,
39 * others are about output format or other details.
@@ -185,6 +189,8 @@ static int show_default(void)
189
190 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
191 {
192 + if (ref_excluded(ref_excludes, refname))
193 + return 0;
194 show_rev(NORMAL, sha1, refname);
195 return 0;
196 }
@@ -633,32 +639,43 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
639 if (!prefixcmp(arg, "--branches=")) {
640 for_each_glob_ref_in(show_reference, arg + 11,
641 "refs/heads/", NULL);
642 + clear_ref_exclusion(&ref_excludes);
643 continue;
644 }
645 if (!strcmp(arg, "--branches")) {
646 for_each_branch_ref(show_reference, NULL);
647 + clear_ref_exclusion(&ref_excludes);
648 continue;
649 }
650 if (!prefixcmp(arg, "--tags=")) {
651 for_each_glob_ref_in(show_reference, arg + 7,
652 "refs/tags/", NULL);
653 + clear_ref_exclusion(&ref_excludes);
654 continue;
655 }
656 if (!strcmp(arg, "--tags")) {
657 for_each_tag_ref(show_reference, NULL);
658 + clear_ref_exclusion(&ref_excludes);
659 continue;
660 }
661 if (!prefixcmp(arg, "--glob=")) {
662 for_each_glob_ref(show_reference, arg + 7, NULL);
663 + clear_ref_exclusion(&ref_excludes);
664 continue;
665 }
666 if (!prefixcmp(arg, "--remotes=")) {
667 for_each_glob_ref_in(show_reference, arg + 10,
668 "refs/remotes/", NULL);
669 + clear_ref_exclusion(&ref_excludes);
670 continue;
671 }
672 if (!strcmp(arg, "--remotes")) {
673 for_each_remote_ref(show_reference, NULL);
674 + clear_ref_exclusion(&ref_excludes);
675 + continue;
676 + }
677 + if (!prefixcmp(arg, "--exclude=")) {
678 + add_ref_exclusion(&ref_excludes, arg + 10);
679 continue;
680 }
681 if (!strcmp(arg, "--show-toplevel")) {
t/t6018-rev-list-glob.sh
+12
@@ -129,6 +129,18 @@ test_expect_success 'rev-parse --remotes=foo' '
129
130 '
131
132 +test_expect_success 'rev-parse --exclude with --branches' '
133 + compare rev-parse "--exclude=*/* --branches" "master someref subspace-x"
134 +'
135 +
136 +test_expect_success 'rev-parse --exclude with --all' '
137 + compare rev-parse "--exclude=refs/remotes/* --all" "--branches --tags"
138 +'
139 +
140 +test_expect_success 'rev-parse accumulates multiple --exclude' '
141 + compare rev-parse "--exclude=refs/remotes/* --exclude=refs/tags/* --all" --branches
142 +'
143 +
144 test_expect_success 'rev-list --glob=refs/heads/subspace/*' '
145
146 compare rev-list "subspace/one subspace/two" "--glob=refs/heads/subspace/*"