git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 06/16] builtin/for-each-ref.c: add `--exclude` option

From
Taylor Blau <me@ttaylorr.com>
Date
Jun 20, 2023, 14:21 UTC
Message-ID
<c4fd47fd750b0381838aa9fb74a665fb5f89c7f4.1687270849.git.me@ttaylorr.com>
In-Reply-To
<cover.1687270849.git.me@ttaylorr.com>

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>
---
 Documentation/git-for-each-ref.txt |  6 +++++
 builtin/for-each-ref.c             |  1 +
 ref-filter.c                       | 13 +++++++++++
 ref-filter.h                       |  6 +++++
 t/t6300-for-each-ref.sh            | 35 ++++++++++++++++++++++++++++++
 5 files changed, 61 insertions(+)
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 1e215d4e73..5743eb5def 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -14,6 +14,7 @@ SYNOPSIS
 		   [--points-at=<object>]
 		   [--merged[=<object>]] [--no-merged[=<object>]]
 		   [--contains[=<object>]] [--no-contains[=<object>]]
+		   [--exclude=<pattern> ...]
 
 DESCRIPTION
 -----------
@@ -102,6 +103,11 @@ OPTIONS
 	Do not print a newline after formatted refs where the format expands
 	to the empty string.
 
+--exclude=<pattern>::
+	If one or more patterns are given, only refs which do not match
+	any excluded pattern(s) are shown. Matching is done using the
+	same rules as `<pattern>` above.
+
 FIELD NAMES
 -----------
 
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index c01fa6fefe..3384987428 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -47,6 +47,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
 		OPT_STRING(  0 , "format", &format.format, N_("format"), N_("format to use for the output")),
 		OPT__COLOR(&format.use_color, N_("respect format colors")),
+		OPT_REF_FILTER_EXCLUDE(&filter),
 		OPT_REF_SORT(&sorting_options),
 		OPT_CALLBACK(0, "points-at", &filter.points_at,
 			     N_("object"), N_("print only refs which points at the given object"),
diff --git a/ref-filter.c b/ref-filter.c
index 6d91c7cb0d..d44418efb7 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2171,6 +2171,15 @@ static int filter_pattern_match(struct ref_filter *filter, const char *refname)
 			     filter->ignore_case);
 }
 
+static int filter_exclude_match(struct ref_filter *filter, const char *refname)
+{
+	if (!filter->exclude.nr)
+		return 0;
+	if (filter->match_as_path)
+		return match_name_as_path(filter, filter->exclude.v, refname);
+	return match_pattern(filter->exclude.v, refname, filter->ignore_case);
+}
+
 /*
  * This is the same as for_each_fullref_in(), but it tries to iterate
  * only over the patterns we'll care about. Note that it _doesn't_ do a full
@@ -2338,6 +2347,9 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 	if (!filter_pattern_match(filter, refname))
 		return 0;
 
+	if (filter_exclude_match(filter, refname))
+		return 0;
+
 	if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
 		return 0;
 
@@ -2877,6 +2889,7 @@ void ref_filter_init(struct ref_filter *filter)
 
 void ref_filter_clear(struct ref_filter *filter)
 {
+	strvec_clear(&filter->exclude);
 	oid_array_clear(&filter->points_at);
 	free_commit_list(filter->with_commit);
 	free_commit_list(filter->no_commit);
diff --git a/ref-filter.h b/ref-filter.h
index 160b807224..1524bc463a 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -6,6 +6,7 @@
 #include "refs.h"
 #include "commit.h"
 #include "string-list.h"
+#include "strvec.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -59,6 +60,7 @@ struct ref_array {
 
 struct ref_filter {
 	const char **name_patterns;
+	struct strvec exclude;
 	struct oid_array points_at;
 	struct commit_list *with_commit;
 	struct commit_list *no_commit;
@@ -94,6 +96,7 @@ struct ref_format {
 
 #define REF_FILTER_INIT { \
 	.points_at = OID_ARRAY_INIT, \
+	.exclude = STRVEC_INIT, \
 }
 #define REF_FORMAT_INIT {             \
 	.use_color = -1,              \
@@ -112,6 +115,9 @@ struct ref_format {
 #define OPT_REF_SORT(var) \
 	OPT_STRING_LIST(0, "sort", (var), \
 			N_("key"), N_("field name to sort on"))
+#define OPT_REF_FILTER_EXCLUDE(var) \
+	OPT_STRVEC(0, "exclude", &(var)->exclude, \
+		   N_("pattern"), N_("exclude refs which match pattern"))
 
 /*
  * API for filtering a set of refs. Based on the type of refs the user
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 5c00607608..7e8d578522 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -447,6 +447,41 @@ test_expect_success 'exercise glob patterns with prefixes' '
 	test_cmp expected actual
 '
 
+cat >expected <<\EOF
+refs/tags/bar
+refs/tags/baz
+refs/tags/testtag
+EOF
+
+test_expect_success 'exercise patterns with prefix exclusions' '
+	for tag in foo/one foo/two foo/three bar baz
+	do
+		git tag "$tag" || return 1
+	done &&
+	test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
+	git for-each-ref --format="%(refname)" \
+		refs/tags/ --exclude=refs/tags/foo >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<\EOF
+refs/tags/bar
+refs/tags/baz
+refs/tags/foo/one
+refs/tags/testtag
+EOF
+
+test_expect_success 'exercise patterns with pattern exclusions' '
+	for tag in foo/one foo/two foo/three bar baz
+	do
+		git tag "$tag" || return 1
+	done &&
+	test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
+	git for-each-ref --format="%(refname)" \
+		refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
+	test_cmp expected actual
+'
+
 cat >expected <<\EOF
 'refs/heads/main'
 'refs/remotes/origin/main'
-- 
2.41.0.44.gf2359540d2
Previous: Jeff KingNext: Taylor Blau
Message 109 of 149 in “refs: implement skip lists for packed backend”
  1. 00/15 refs: implement skip lists for packed backendTaylor Blau, May 8, 2023
  2. 01/15 refs.c: rename `ref_filter`Taylor Blau, May 8, 2023
  3. 03/15 ref-filter: clear reachable list pointers after freeingTaylor Blau, May 8, 2023
  4. 02/15 ref-filter.h: provide `REF_FILTER_INIT`Taylor Blau, May 8, 2023
  5. 04/15 ref-filter: add ref_filter_clear()Taylor Blau, May 8, 2023
  6. Junio C HamanoMay 8, 2023
  7. Taylor BlauMay 8, 2023
  8. Patrick SteinhardtMay 9, 2023
  9. Taylor BlauMay 9, 2023
  10. 05/15 ref-filter.c: parameterize match functions over patternsTaylor Blau, May 8, 2023
  11. Junio C HamanoMay 8, 2023
  12. Taylor BlauMay 9, 2023
  13. 06/15 builtin/for-each-ref.c: add `--exclude` optionTaylor Blau, May 8, 2023
  14. Junio C HamanoMay 8, 2023
  15. Taylor BlauMay 9, 2023
  16. 07/15 refs: plumb `exclude_patterns` argument throughoutTaylor Blau, May 8, 2023
  17. Patrick SteinhardtMay 9, 2023
  18. Taylor BlauMay 9, 2023
  19. 08/15 refs/packed-backend.c: refactor `find_reference_location()`Taylor Blau, May 8, 2023
  20. Junio C HamanoMay 8, 2023
  21. Taylor BlauMay 9, 2023
  22. 09/15 refs/packed-backend.c: implement skip lists to avoid excluded pattern(s)Taylor Blau, May 8, 2023
  23. Chris TorekMay 9, 2023
  24. Taylor BlauMay 9, 2023
  25. Patrick SteinhardtMay 9, 2023
  26. Taylor BlauMay 9, 2023
  27. Taylor BlauMay 9, 2023
  28. Patrick SteinhardtMay 10, 2023
  29. Junio C HamanoMay 9, 2023
  30. Taylor BlauMay 10, 2023
  31. 10/15 refs/packed-backend.c: add trace2 counters for skip listTaylor Blau, May 8, 2023
  32. 12/15 refs/packed-backend.c: ignore complicated hidden refs rulesTaylor Blau, May 8, 2023
  33. 11/15 revision.h: store hidden refs in a `strvec`Taylor Blau, May 8, 2023
  34. 13/15 refs.h: let `for_each_namespaced_ref()` take excluded patternsTaylor Blau, May 8, 2023
  35. 14/15 upload-pack.c: avoid enumerating hidden refs where possibleTaylor Blau, May 8, 2023
  36. Patrick SteinhardtMay 9, 2023
  37. Taylor BlauMay 9, 2023
  38. 15/15 builtin/receive-pack.c: avoid enumerating hidden referencesTaylor Blau, May 8, 2023
  39. 00/16 refs: implement jump lists for packed backendTaylor Blau, May 15, 2023
  40. 01/16 refs.c: rename `ref_filter`Taylor Blau, May 15, 2023
  41. 02/16 ref-filter.h: provide `REF_FILTER_INIT`Taylor Blau, May 15, 2023
  42. 03/16 ref-filter: clear reachable list pointers after freeingTaylor Blau, May 15, 2023
  43. 04/16 ref-filter: add `ref_filter_clear()`Taylor Blau, May 15, 2023
  44. 05/16 ref-filter.c: parameterize match functions over patternsTaylor Blau, May 15, 2023
  45. 06/16 builtin/for-each-ref.c: add `--exclude` optionTaylor Blau, May 15, 2023
  46. 07/16 refs: plumb `exclude_patterns` argument throughoutTaylor Blau, May 15, 2023
  47. 08/16 refs/packed-backend.c: refactor `find_reference_location()`Taylor Blau, May 15, 2023
  48. 09/16 refs/packed-backend.c: implement jump lists to avoid excluded pattern(s)Taylor Blau, May 15, 2023
  49. Patrick SteinhardtJun 6, 2023
  50. Taylor BlauJun 20, 2023
  51. 10/16 refs/packed-backend.c: add trace2 counters for jump listTaylor Blau, May 15, 2023
  52. 11/16 revision.h: store hidden refs in a `strvec`Taylor Blau, May 15, 2023
  53. Patrick SteinhardtJun 6, 2023
  54. Taylor BlauJun 20, 2023
  55. 12/16 refs/packed-backend.c: ignore complicated hidden refs rulesTaylor Blau, May 15, 2023
  56. 13/16 refs.h: let `for_each_namespaced_ref()` take excluded patternsTaylor Blau, May 15, 2023
  57. Patrick SteinhardtJun 6, 2023
  58. Taylor BlauJun 20, 2023
  59. 14/16 builtin/receive-pack.c: avoid enumerating hidden referencesTaylor Blau, May 15, 2023
  60. 15/16 upload-pack.c: avoid enumerating hidden refs where possibleTaylor Blau, May 15, 2023
  61. 16/16 ls-refs.c: avoid enumerating hidden refs where possibleTaylor Blau, May 15, 2023
  62. Patrick SteinhardtJun 6, 2023
  63. Taylor BlauJun 20, 2023
  64. 00/16 refs: implement jump lists for packed backendTaylor Blau, Jun 7, 2023
  65. 01/16 refs.c: rename `ref_filter`Taylor Blau, Jun 7, 2023
  66. Junio C HamanoJun 13, 2023
  67. 02/16 ref-filter.h: provide `REF_FILTER_INIT`Taylor Blau, Jun 7, 2023
  68. 03/16 ref-filter: clear reachable list pointers after freeingTaylor Blau, Jun 7, 2023
  69. 04/16 ref-filter: add `ref_filter_clear()`Taylor Blau, Jun 7, 2023
  70. 05/16 ref-filter.c: parameterize match functions over patternsTaylor Blau, Jun 7, 2023
  71. Junio C HamanoJun 13, 2023
  72. 06/16 builtin/for-each-ref.c: add `--exclude` optionTaylor Blau, Jun 7, 2023
  73. 07/16 refs: plumb `exclude_patterns` argument throughoutTaylor Blau, Jun 7, 2023
  74. Junio C HamanoJun 13, 2023
  75. Taylor BlauJun 20, 2023
  76. 08/16 refs/packed-backend.c: refactor `find_reference_location()`Taylor Blau, Jun 7, 2023
  77. 09/16 refs/packed-backend.c: implement jump lists to avoid excluded pattern(s)Taylor Blau, Jun 7, 2023
  78. Junio C HamanoJun 14, 2023
  79. Taylor BlauJun 20, 2023
  80. Junio C HamanoJun 20, 2023
  81. 10/16 refs/packed-backend.c: add trace2 counters for jump listTaylor Blau, Jun 7, 2023
  82. Junio C HamanoJun 14, 2023
  83. Taylor BlauJun 20, 2023
  84. 11/16 revision.h: store hidden refs in a `strvec`Taylor Blau, Jun 7, 2023
  85. 12/16 refs/packed-backend.c: ignore complicated hidden refs rulesTaylor Blau, Jun 7, 2023
  86. Junio C HamanoJun 14, 2023
  87. 13/16 refs.h: let `for_each_namespaced_ref()` take excluded patternsTaylor Blau, Jun 7, 2023
  88. 14/16 builtin/receive-pack.c: avoid enumerating hidden referencesTaylor Blau, Jun 7, 2023
  89. 15/16 upload-pack.c: avoid enumerating hidden refs where possibleTaylor Blau, Jun 7, 2023
  90. 16/16 ls-refs.c: avoid enumerating hidden refs where possibleTaylor Blau, Jun 7, 2023
  91. Junio C HamanoJun 12, 2023
  92. 00/16 refs: implement jump lists for packed backendTaylor Blau, Jun 20, 2023
  93. 01/16 refs.c: rename `ref_filter`Taylor Blau, Jun 20, 2023
  94. Jeff KingJul 3, 2023
  95. 02/16 ref-filter.h: provide `REF_FILTER_INIT`Taylor Blau, Jun 20, 2023
  96. Jeff KingJul 3, 2023
  97. Taylor BlauJul 3, 2023
  98. 03/16 ref-filter: clear reachable list pointers after freeingTaylor Blau, Jun 20, 2023
  99. Jeff KingJul 3, 2023
  100. 04/16 ref-filter: add `ref_filter_clear()`Taylor Blau, Jun 20, 2023
  101. Jeff KingJul 3, 2023
  102. Taylor BlauJul 3, 2023
  103. Jeff KingJul 3, 2023
  104. 05/16 ref-filter.c: parameterize match functions over patternsTaylor Blau, Jun 20, 2023
  105. Jeff KingJul 3, 2023
  106. Taylor BlauJul 3, 2023
  107. Taylor BlauJul 3, 2023
  108. Jeff KingJul 3, 2023
  109. 06/16 builtin/for-each-ref.c: add `--exclude` optionTaylor Blau, Jun 20, 2023
  110. 07/16 refs: plumb `exclude_patterns` argument throughoutTaylor Blau, Jun 20, 2023
  111. 08/16 refs/packed-backend.c: refactor `find_reference_location()`Taylor Blau, Jun 20, 2023
  112. 09/16 refs/packed-backend.c: implement jump lists to avoid excluded pattern(s)Taylor Blau, Jun 20, 2023
  113. Jeff KingJul 3, 2023
  114. Taylor BlauJul 3, 2023
  115. 10/16 refs/packed-backend.c: add trace2 counters for jump listTaylor Blau, Jun 20, 2023
  116. 11/16 revision.h: store hidden refs in a `strvec`Taylor Blau, Jun 20, 2023
  117. Jeff KingJul 3, 2023
  118. 12/16 refs/packed-backend.c: ignore complicated hidden refs rulesTaylor Blau, Jun 20, 2023
  119. Jeff KingJul 3, 2023
  120. Taylor BlauJul 4, 2023
  121. 13/16 refs.h: let `for_each_namespaced_ref()` take excluded patternsTaylor Blau, Jun 20, 2023
  122. 14/16 builtin/receive-pack.c: avoid enumerating hidden referencesTaylor Blau, Jun 20, 2023
  123. 15/16 upload-pack.c: avoid enumerating hidden refs where possibleTaylor Blau, Jun 20, 2023
  124. Jeff KingJul 3, 2023
  125. Taylor BlauJul 4, 2023
  126. 16/16 ls-refs.c: avoid enumerating hidden refs where possibleTaylor Blau, Jun 20, 2023
  127. Jeff KingJul 3, 2023
  128. Jeff KingJul 3, 2023
  129. 00/16 refs: implement jump lists for packed backendTaylor Blau, Jul 10, 2023
  130. 01/16 refs.c: rename `ref_filter`Taylor Blau, Jul 10, 2023
  131. 02/16 ref-filter.h: provide `REF_FILTER_INIT`Taylor Blau, Jul 10, 2023
  132. 03/16 ref-filter: clear reachable list pointers after freeingTaylor Blau, Jul 10, 2023
  133. 04/16 ref-filter: add `ref_filter_clear()`Taylor Blau, Jul 10, 2023
  134. 05/16 ref-filter.c: parameterize match functions over patternsTaylor Blau, Jul 10, 2023
  135. 06/16 builtin/for-each-ref.c: add `--exclude` optionTaylor Blau, Jul 10, 2023
  136. 07/16 refs: plumb `exclude_patterns` argument throughoutTaylor Blau, Jul 10, 2023
  137. 08/16 refs/packed-backend.c: refactor `find_reference_location()`Taylor Blau, Jul 10, 2023
  138. 09/16 refs/packed-backend.c: implement jump lists to avoid excluded pattern(s)Taylor Blau, Jul 10, 2023
  139. 10/16 refs/packed-backend.c: add trace2 counters for jump listTaylor Blau, Jul 10, 2023
  140. 11/16 revision.h: store hidden refs in a `strvec`Taylor Blau, Jul 10, 2023
  141. 12/16 refs.h: let `for_each_namespaced_ref()` take excluded patternsTaylor Blau, Jul 10, 2023
  142. 14/16 builtin/receive-pack.c: avoid enumerating hidden referencesTaylor Blau, Jul 10, 2023
  143. 13/16 refs.h: implement `hidden_refs_to_excludes()`Taylor Blau, Jul 10, 2023
  144. 15/16 upload-pack.c: avoid enumerating hidden refs where possibleTaylor Blau, Jul 10, 2023
  145. 16/16 ls-refs.c: avoid enumerating hidden refs where possibleTaylor Blau, Jul 10, 2023
  146. Junio C HamanoJul 10, 2023
  147. Patrick SteinhardtJul 11, 2023
  148. Junio C HamanoJul 11, 2023
  149. Taylor BlauJul 11, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.