builtin/for-each-ref.c: add `--exclude` option

When using `for-each-ref`, it is sometimes convenient for the caller to be able to exclude certain parts of the references. For example, if there are many `refs/__hidden__/*` references, the caller may want to emit all references *except* the hidden ones. Currently, the only way to do this is to post-process the output, like: $ git for-each-ref --format='%(refname)' | grep -v '^refs/hidden/' Which is do-able, but requires processing a potentially large quantity of references. Teach `git for-each-ref` a new `--exclude=<pattern>` option, which excludes references from the results if they match one or more excluded patterns. This patch provides a naive implementation where the `ref_filter` still sees all references (including ones that it will discard) and is left to check whether each reference matches any excluded pattern(s) before emitting them. By culling out references we know the caller doesn't care about, we can avoid allocating memory for their storage, as well as spending time sorting the output (among other things). Even the naive implementation provides a significant speed-up on a modified copy of linux.git (that has a hidden ref pointing at each commit): $ hyperfine \ 'git.compile for-each-ref --format="%(objectname) %(refname)" | grep -vE "[0-9a-f]{40} refs/pull/"' \ 'git.compile for-each-ref --format="%(objectname) %(refname)" --exclude refs/pull/' Benchmark 1: git.compile for-each-ref --format="%(objectname) %(refname)" | grep -vE "[0-9a-f]{40} refs/pull/" Time (mean ± σ): 820.1 ms ± 2.0 ms [User: 703.7 ms, System: 152.0 ms] Range (min … max): 817.7 ms … 823.3 ms 10 runs Benchmark 2: git.compile for-each-ref --format="%(objectname) %(refname)" --exclude refs/pull/ Time (mean ± σ): 106.6 ms ± 1.1 ms [User: 99.4 ms, System: 7.1 ms] Range (min … max): 104.7 ms … 109.1 ms 27 runs Summary 'git.compile for-each-ref --format="%(objectname) %(refname)" --exclude refs/pull/' ran 7.69 ± 0.08 times faster than 'git.compile for-each-ref --format="%(objectname) %(refname)" | grep -vE "[0-9a-f]{40} refs/pull/"' Subsequent patches will improve on this by avoiding visiting excluded sections of the `packed-refs` file in certain cases. Co-authored-by: Jeff King <peff@peff.net> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jul 10, 2023 at 17:12 UTC 8255dd8a3dce094129690d17595dc822171d1e61
5 files changed +62
Documentation/git-for-each-ref.txt
+6
@@ -14,6 +14,7 @@ SYNOPSIS
14 [--points-at=<object>]
15 [--merged[=<object>]] [--no-merged[=<object>]]
16 [--contains[=<object>]] [--no-contains[=<object>]]
17 + [--exclude=<pattern> ...]
18
19 DESCRIPTION
20 -----------
@@ -102,6 +103,11 @@ OPTIONS
103 Do not print a newline after formatted refs where the format expands
104 to the empty string.
105
106 +--exclude=<pattern>::
107 + If one or more patterns are given, only refs which do not match
108 + any excluded pattern(s) are shown. Matching is done using the
109 + same rules as `<pattern>` above.
110 +
111 FIELD NAMES
112 -----------
113
builtin/for-each-ref.c
+1
@@ -47,6 +47,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
47 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
48 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
49 OPT__COLOR(&format.use_color, N_("respect format colors")),
50 + OPT_REF_FILTER_EXCLUDE(&filter),
51 OPT_REF_SORT(&sorting_options),
52 OPT_CALLBACK(0, "points-at", &filter.points_at,
53 N_("object"), N_("print only refs which points at the given object"),
ref-filter.c
+14
@@ -2171,6 +2171,16 @@ static int filter_pattern_match(struct ref_filter *filter, const char *refname)
2171 filter->ignore_case);
2172 }
2173
2174 +static int filter_exclude_match(struct ref_filter *filter, const char *refname)
2175 +{
2176 + if (!filter->exclude.nr)
2177 + return 0;
2178 + if (filter->match_as_path)
2179 + return match_name_as_path(filter->exclude.v, refname,
2180 + filter->ignore_case);
2181 + return match_pattern(filter->exclude.v, refname, filter->ignore_case);
2182 +}
2183 +
2184 /*
2185 * This is the same as for_each_fullref_in(), but it tries to iterate
2186 * only over the patterns we'll care about. Note that it _doesn't_ do a full
@@ -2338,6 +2348,9 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
2348 if (!filter_pattern_match(filter, refname))
2349 return 0;
2350
2351 + if (filter_exclude_match(filter, refname))
2352 + return 0;
2353 +
2354 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
2355 return 0;
2356
@@ -2877,6 +2890,7 @@ void ref_filter_init(struct ref_filter *filter)
2890
2891 void ref_filter_clear(struct ref_filter *filter)
2892 {
2893 + strvec_clear(&filter->exclude);
2894 oid_array_clear(&filter->points_at);
2895 free_commit_list(filter->with_commit);
2896 free_commit_list(filter->no_commit);
ref-filter.h
+6
@@ -6,6 +6,7 @@
6 #include "refs.h"
7 #include "commit.h"
8 #include "string-list.h"
9 +#include "strvec.h"
10
11 /* Quoting styles */
12 #define QUOTE_NONE 0
@@ -59,6 +60,7 @@ struct ref_array {
60
61 struct ref_filter {
62 const char **name_patterns;
63 + struct strvec exclude;
64 struct oid_array points_at;
65 struct commit_list *with_commit;
66 struct commit_list *no_commit;
@@ -94,6 +96,7 @@ struct ref_format {
96
97 #define REF_FILTER_INIT { \
98 .points_at = OID_ARRAY_INIT, \
99 + .exclude = STRVEC_INIT, \
100 }
101 #define REF_FORMAT_INIT { \
102 .use_color = -1, \
@@ -112,6 +115,9 @@ struct ref_format {
115 #define OPT_REF_SORT(var) \
116 OPT_STRING_LIST(0, "sort", (var), \
117 N_("key"), N_("field name to sort on"))
118 +#define OPT_REF_FILTER_EXCLUDE(var) \
119 + OPT_STRVEC(0, "exclude", &(var)->exclude, \
120 + N_("pattern"), N_("exclude refs which match pattern"))
121
122 /*
123 * API for filtering a set of refs. Based on the type of refs the user
t/t6300-for-each-ref.sh
+35
@@ -447,6 +447,41 @@ test_expect_success 'exercise glob patterns with prefixes' '
447 test_cmp expected actual
448 '
449
450 +cat >expected <<\EOF
451 +refs/tags/bar
452 +refs/tags/baz
453 +refs/tags/testtag
454 +EOF
455 +
456 +test_expect_success 'exercise patterns with prefix exclusions' '
457 + for tag in foo/one foo/two foo/three bar baz
458 + do
459 + git tag "$tag" || return 1
460 + done &&
461 + test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
462 + git for-each-ref --format="%(refname)" \
463 + refs/tags/ --exclude=refs/tags/foo >actual &&
464 + test_cmp expected actual
465 +'
466 +
467 +cat >expected <<\EOF
468 +refs/tags/bar
469 +refs/tags/baz
470 +refs/tags/foo/one
471 +refs/tags/testtag
472 +EOF
473 +
474 +test_expect_success 'exercise patterns with pattern exclusions' '
475 + for tag in foo/one foo/two foo/three bar baz
476 + do
477 + git tag "$tag" || return 1
478 + done &&
479 + test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
480 + git for-each-ref --format="%(refname)" \
481 + refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
482 + test_cmp expected actual
483 +'
484 +
485 cat >expected <<\EOF
486 'refs/heads/main'
487 'refs/remotes/origin/main'