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

[PATCH v5 0/5] for-each-ref: introduce seeking functionality via '--start-after'

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jul 15, 2025, 11:28 UTC
Message-ID
<20250715-306-git-for-each-ref-pagination-v5-0-852d5a2f56e1@gmail.com>
In-Reply-To
<20250701-306-git-for-each-ref-pagination-v1-0-4f0ae7c0688f@gmail.com>

The `git-for-each-ref(1)` command is used to iterate over references present in a repository. In large repositories with millions of references, it would be optimal to paginate this output such that we can start iteration from a given reference. This would avoid having to iterate over all references from the beginning each time when paginating through results.

This series adds a '--start-after' option in 'git-for-each-ref(1)'. When used, the reference iteration seeks to first reference following the marker alphabetically. When paging, it should be noted that references may be deleted, modified or added between invocations. Output will only yield those references which follow the marker lexicographically. If the marker does not exist, output begins from the first reference that would come after it alphabetically.

This enables efficient pagination workflows like:
    git for-each-ref --count=100
    git for-each-ref --count=100 --start-after=refs/heads/branch-100
    git for-each-ref --count=100 --start-after=refs/heads/branch-200

To add this functionality, we expose the `ref_iterator` outside the 'refs/' namespace and modify the `ref_iterator_seek()` to actually seek to a given reference and only set the prefix when the `set_prefix` field is set.

On the reftable and packed backend, the changes are simple. But since the files backend uses 'ref-cache' for reference handling, the changes there are a little more involved, since we need to setup the right levels and the indexing.

Initially I was also planning to cleanup all the `refs_for_each...()` functions in 'refs.h' by simply using the iterator, but this bloated the series. So I've left that for another day.

Changes in v5:
- Changes to the comments to refer to the flag
  'REF_ITERATOR_SEEK_SET_PREFIX' instead of a variable used in older
  versions. Also other small grammar fixes.
- Added a commit to remove an unnecessary else clause.
- Move seeking functionality within `for_each_fullref_in_pattern` to its
  own function.
- Fix incorrect naming in the tests.
- Link to v4: https://lore.kernel.org/r/20250711-306-git-for-each-ref-pagination-v4-0-ed3303ad5b89@gmail.com
Changes in v4:
- Patch 3/4: Move around the documentation for the flag and rename the
  seek variable to refname.
- Patch 4/4: Cleanup the commit message and also the documentation.
- Link to v3: https://lore.kernel.org/r/20250708-306-git-for-each-ref-pagination-v3-0-8cfba1080be4@gmail.com
Changes in v3:
- Change the working of the command to exclude the marker provided. With
  this rename the flag to '--start-after'.
- Extend the documentation to add a note about concurrent modifications
  to the reference database.
- Link to v2: https://lore.kernel.org/r/20250704-306-git-for-each-ref-pagination-v2-0-bcde14acdd81@gmail.com
Changes in v2:
- Modify 'ref_iterator_seek()' to take in flags instead of a
  'set_prefix' variable. This improves readability, where users would
  use the 'REF_ITERATOR_SEEK_SET_PREFIX' instead of simply passing '1'.
- When the set prefix flag isn't usage, reset any previously set prefix.
  This ensures that the internal prefix state is always reset whenever
  we seek and unifies the behavior between 'ref_iterator_seek' and
  'ref_iterator_begin'.
- Don't allow '--skip-until' to be run with '--sort', since the seeking
  always takes place before any sorting and this can be confusing.
- Some styling fixes:
  - Remove extra newline
  - Skip braces around single lined if...else clause
  - Add braces around 'if' clause
  - Fix indentation
- Link to v1: https://lore.kernel.org/git/20250701-306-git-for-each-ref-pagination-v1-0-4f0ae7c0688f@gmail.com/
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 Documentation/git-for-each-ref.adoc |  10 +-
 builtin/for-each-ref.c              |   8 ++
 ref-filter.c                        | 116 ++++++++++++++-------
 ref-filter.h                        |   1 +
 refs.c                              |   6 +-
 refs.h                              | 155 ++++++++++++++++++++++++++++
 refs/debug.c                        |   7 +-
 refs/files-backend.c                |   7 +-
 refs/iterator.c                     |  26 +++--
 refs/packed-backend.c               |  17 ++--
 refs/ref-cache.c                    |  99 ++++++++++++++----
 refs/ref-cache.h                    |   7 --
 refs/refs-internal.h                | 152 ++--------------------------
 refs/reftable-backend.c             |  21 ++--
 t/t6302-for-each-ref-filter.sh      | 194 ++++++++++++++++++++++++++++++++++++
 15 files changed, 583 insertions(+), 243 deletions(-)
Karthik Nayak (5):
      refs: expose `ref_iterator` via 'refs.h'
      ref-cache: remove unused function 'find_ref_entry()'
      refs: selectively set prefix in the seek functions
      ref-filter: remove unnecessary else clause
      for-each-ref: introduce a '--start-after' option
Range-diff versus v4:
1:  dde167f421 = 1:  f9c9a7fdd9 refs: expose `ref_iterator` via 'refs.h'
2:  e392e93520 = 2:  83bee35517 ref-cache: remove unused function 'find_ref_entry()'
3:  711ffcac00 ! 3:  3b6019a1e7 refs: selectively set prefix in the seek functions
    @@ refs/refs-internal.h: void base_ref_iterator_init(struct ref_iterator *iter,
      /*
     - * Seek the iterator to the first reference matching the given prefix. Should
     - * behave the same as if a new iterator was created with the same prefix.
    -+ * Seek the iterator to the first matching reference. If set_prefix is set,
    -+ * it would behave the same as if a new iterator was created with the same
    -+ * prefix.
    ++ * Seek the iterator to the first matching reference. If the
    ++ * REF_ITERATOR_SEEK_SET_PREFIX flag is set, it would behave the same as if a
    ++ * new iterator was created with the provided refname as prefix.
       */
      typedef int ref_iterator_seek_fn(struct ref_iterator *ref_iterator,
     -				 const char *prefix);
-:  ---------- > 4:  3f89eeef26 ref-filter: remove unnecessary else clause
4:  3a0c89acbe ! 5:  7ee7d83cf0 for-each-ref: introduce a '--start-after' option
    @@ ref-filter.c: static int filter_exclude_match(struct ref_filter *filter, const c
     +	strbuf_release(&sb);
     +	return ret;
     +}
    ++
    ++static int for_each_fullref_with_seek(struct ref_filter *filter, each_ref_fn cb,
    ++				       void *cb_data, unsigned int flags)
    ++{
    ++	struct ref_iterator *iter;
    ++	int ret = 0;
    ++
    ++	iter = refs_ref_iterator_begin(get_main_ref_store(the_repository), "",
    ++				       NULL, 0, flags);
    ++	if (filter->start_after)
    ++		ret = start_ref_iterator_after(iter, filter->start_after);
    ++
    ++	if (ret)
    ++		return ret;
    ++
    ++	return do_for_each_ref_iterator(iter, cb, cb_data);
    ++}
     +
      /*
       * 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
     @@ ref-filter.c: static int for_each_fullref_in_pattern(struct ref_filter *filter,
    - 				       each_ref_fn cb,
    - 				       void *cb_data)
      {
    -+	struct ref_iterator *iter;
    -+	int flags = 0, ret = 0;
    -+
      	if (filter->kind & FILTER_REFS_ROOT_REFS) {
      		/* In this case, we want to print all refs including root refs. */
     -		return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
     -						       cb, cb_data);
    -+		flags |= DO_FOR_EACH_INCLUDE_ROOT_REFS;
    -+		goto non_prefix_iter;
    ++		return for_each_fullref_with_seek(filter, cb, cb_data,
    ++						  DO_FOR_EACH_INCLUDE_ROOT_REFS);
      	}
      
      	if (!filter->match_as_path) {
    @@ ref-filter.c: static int for_each_fullref_in_pattern(struct ref_filter *filter,
      		 */
     -		return refs_for_each_fullref_in(get_main_ref_store(the_repository),
     -						"", NULL, cb, cb_data);
    -+		goto non_prefix_iter;
    ++		return for_each_fullref_with_seek(filter, cb, cb_data, 0);
      	}
      
      	if (filter->ignore_case) {
    @@ ref-filter.c: static int for_each_fullref_in_pattern(struct ref_filter *filter,
      		 */
     -		return refs_for_each_fullref_in(get_main_ref_store(the_repository),
     -						"", NULL, cb, cb_data);
    -+		goto non_prefix_iter;
    ++		return for_each_fullref_with_seek(filter, cb, cb_data, 0);
      	}
      
      	if (!filter->name_patterns[0]) {
      		/* no patterns; we have to look at everything */
     -		return refs_for_each_fullref_in(get_main_ref_store(the_repository),
     -						 "", filter->exclude.v, cb, cb_data);
    -+		goto non_prefix_iter;
    ++		return for_each_fullref_with_seek(filter, cb, cb_data, 0);
      	}
      
      	return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository),
    - 						 NULL, filter->name_patterns,
    - 						 filter->exclude.v,
    - 						 cb, cb_data);
    -+
    -+non_prefix_iter:
    -+	iter = refs_ref_iterator_begin(get_main_ref_store(the_repository), "",
    -+				       NULL, 0, flags);
    -+	if (filter->start_after)
    -+		ret = start_ref_iterator_after(iter, filter->start_after);
    -+
    -+	if (ret)
    -+		return ret;
    -+
    -+	return do_for_each_ref_iterator(iter, cb, cb_data);
    - }
    +@@ ref-filter.c: void filter_is_base(struct repository *r,
      
    - /*
    -@@ ref-filter.c: static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
    - 	init_contains_cache(&filter->internal.no_contains_cache);
    + static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref_fn fn, void *cb_data)
    + {
    ++	const char *prefix = NULL;
    + 	int ret = 0;
      
    - 	/*  Simple per-ref filtering */
    --	if (!filter->kind)
    -+	if (!filter->kind) {
    - 		die("filter_refs: invalid type");
    --	else {
    -+	} else {
    -+		const char *prefix = NULL;
    -+
    - 		/*
    - 		 * For common cases where we need only branches or remotes or tags,
    - 		 * we only iterate through those refs. If a mix of refs is needed,
    + 	filter->kind = type & FILTER_REFS_KIND_MASK;
     @@ ref-filter.c: static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
    - 		 * of filter_ref_kind().
    - 		 */
    - 		if (filter->kind == FILTER_REFS_BRANCHES)
    --			ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
    --						       "refs/heads/", NULL,
    --						       fn, cb_data);
    -+			prefix = "refs/heads/";
    - 		else if (filter->kind == FILTER_REFS_REMOTES)
    --			ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
    --						       "refs/remotes/", NULL,
    --						       fn, cb_data);
    -+			prefix = "refs/remotes/";
    - 		else if (filter->kind == FILTER_REFS_TAGS)
    --			ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
    --						       "refs/tags/", NULL, fn,
    --						       cb_data);
    --		else if (filter->kind & FILTER_REFS_REGULAR)
    -+			prefix = "refs/tags/";
    + 	 * of filter_ref_kind().
    + 	 */
    + 	if (filter->kind == FILTER_REFS_BRANCHES)
    +-		ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
    +-					       "refs/heads/", NULL,
    +-					       fn, cb_data);
    ++		prefix = "refs/heads/";
    + 	else if (filter->kind == FILTER_REFS_REMOTES)
    +-		ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
    +-					       "refs/remotes/", NULL,
    +-					       fn, cb_data);
    ++		prefix = "refs/remotes/";
    + 	else if (filter->kind == FILTER_REFS_TAGS)
    +-		ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
    +-					       "refs/tags/", NULL, fn,
    +-					       cb_data);
    +-	else if (filter->kind & FILTER_REFS_REGULAR)
    ++		prefix = "refs/tags/";
     +
    -+		if (prefix) {
    -+			struct ref_iterator *iter;
    ++	if (prefix) {
    ++		struct ref_iterator *iter;
     +
    -+			iter = refs_ref_iterator_begin(get_main_ref_store(the_repository),
    -+						       "", NULL, 0, 0);
    ++		iter = refs_ref_iterator_begin(get_main_ref_store(the_repository),
    ++					       "", NULL, 0, 0);
     +
    -+			if (filter->start_after)
    -+				ret = start_ref_iterator_after(iter, filter->start_after);
    -+			else if (prefix)
    -+				ret = ref_iterator_seek(iter, prefix, 1);
    ++		if (filter->start_after)
    ++			ret = start_ref_iterator_after(iter, filter->start_after);
    ++		else if (prefix)
    ++			ret = ref_iterator_seek(iter, prefix, 1);
     +
    -+			if (!ret)
    -+				ret = do_for_each_ref_iterator(iter, fn, cb_data);
    -+		} else if (filter->kind & FILTER_REFS_REGULAR) {
    - 			ret = for_each_fullref_in_pattern(filter, fn, cb_data);
    -+		}
    ++		if (!ret)
    ++			ret = do_for_each_ref_iterator(iter, fn, cb_data);
    ++	} else if (filter->kind & FILTER_REFS_REGULAR) {
    + 		ret = for_each_fullref_in_pattern(filter, fn, cb_data);
    ++	}
      
    - 		/*
    - 		 * When printing all ref types, HEAD is already included,
    + 	/*
    + 	 * When printing all ref types, HEAD is already included,
     
      ## ref-filter.h ##
     @@ ref-filter.h: struct ref_array {
    @@ t/t6302-for-each-ref-filter.sh: test_expect_success 'validate worktree atom' '
     +	refs/tags/three
     +	refs/tags/two
     +	EOF
    -+	git for-each-ref --format="%(refname)" --start-after=refs/lost >actual &&
    ++	git for-each-ref --format="%(refname)" --start-after=refs/odd/ >actual &&
     +	test_cmp expect actual
     +'
     +
    @@ t/t6302-for-each-ref-filter.sh: test_expect_success 'validate worktree atom' '
     +	refs/tags/three
     +	refs/tags/two
     +	EOF
    -+	git for-each-ref --format="%(refname)" --start-after=refs/odd/ >actual &&
    ++	git for-each-ref --format="%(refname)" --start-after=refs/lost >actual &&
     +	test_cmp expect actual
     +'
     +

base-commit: cf6f63ea6bf35173e02e18bdc6a4ba41288acff9 change-id: 20250605-306-git-for-each-ref-pagination-0ba8a29ae646

Thanks
- Karthik
Previous: Karthik NayakNext: Karthik Nayak
Message 70 of 102 in “for-each-ref: introduce seeking functionality via '--skip-until'”
  1. 0/4 for-each-ref: introduce seeking functionality via '--skip-until'Karthik Nayak, Jul 1, 2025
  2. 2/4 ref-cache: remove unused function 'find_ref_entry()'Karthik Nayak, Jul 1, 2025
  3. Junio C HamanoJul 14, 2025
  4. 1/4 refs: expose `ref_iterator` via 'refs.h'Karthik Nayak, Jul 1, 2025
  5. 3/4 refs: selectively set prefix in the seek functionsKarthik Nayak, Jul 1, 2025
  6. Patrick SteinhardtJul 3, 2025
  7. Karthik NayakJul 3, 2025
  8. 4/4 for-each-ref: introduce a '--skip-until' optionKarthik Nayak, Jul 1, 2025
  9. Patrick SteinhardtJul 3, 2025
  10. Karthik NayakJul 3, 2025
  11. Patrick SteinhardtJul 3, 2025
  12. Junio C HamanoJul 1, 2025
  13. Karthik NayakJul 2, 2025
  14. Junio C HamanoJul 1, 2025
  15. Karthik NayakJul 2, 2025
  16. Karthik NayakJul 3, 2025
  17. Phillip WoodJul 2, 2025
  18. Karthik NayakJul 2, 2025
  19. Patrick SteinhardtJul 3, 2025
  20. Junio C HamanoJul 3, 2025
  21. Patrick SteinhardtJul 3, 2025
  22. Karthik NayakJul 3, 2025
  23. 0/4 for-each-ref: introduce seeking functionality via '--skip-until'Karthik Nayak, Jul 4, 2025
  24. 1/4 refs: expose `ref_iterator` via 'refs.h'Karthik Nayak, Jul 4, 2025
  25. 2/4 ref-cache: remove unused function 'find_ref_entry()'Karthik Nayak, Jul 4, 2025
  26. 3/4 refs: selectively set prefix in the seek functionsKarthik Nayak, Jul 4, 2025
  27. 4/4 for-each-ref: introduce a '--skip-until' optionKarthik Nayak, Jul 4, 2025
  28. Junio C HamanoJul 7, 2025
  29. Karthik NayakJul 7, 2025
  30. Andreas SchwabJul 4, 2025
  31. Karthik NayakJul 4, 2025
  32. Andreas SchwabJul 4, 2025
  33. Karthik NayakJul 4, 2025
  34. Andreas SchwabJul 4, 2025
  35. Karthik NayakJul 7, 2025
  36. Junio C HamanoJul 4, 2025
  37. Karthik NayakJul 7, 2025
  38. Phillip WoodJul 7, 2025
  39. Karthik NayakJul 8, 2025
  40. 0/4 for-each-ref: introduce seeking functionality via '--start-after'Karthik Nayak, Jul 8, 2025
  41. 1/4 refs: expose `ref_iterator` via 'refs.h'Karthik Nayak, Jul 8, 2025
  42. 2/4 ref-cache: remove unused function 'find_ref_entry()'Karthik Nayak, Jul 8, 2025
  43. 3/4 refs: selectively set prefix in the seek functionsKarthik Nayak, Jul 8, 2025
  44. Patrick SteinhardtJul 10, 2025
  45. Karthik NayakJul 11, 2025
  46. Junio C HamanoJul 14, 2025
  47. Karthik NayakJul 15, 2025
  48. Junio C HamanoJul 15, 2025
  49. Karthik NayakJul 16, 2025
  50. Junio C HamanoJul 16, 2025
  51. Junio C HamanoJul 16, 2025
  52. Karthik NayakJul 17, 2025
  53. Junio C HamanoJul 17, 2025
  54. 4/4 for-each-ref: introduce a '--start-after' optionKarthik Nayak, Jul 8, 2025
  55. Junio C HamanoJul 8, 2025
  56. Karthik NayakJul 9, 2025
  57. 0/4 for-each-ref: introduce seeking functionality via '--start-after'Karthik Nayak, Jul 11, 2025
  58. 2/4 ref-cache: remove unused function 'find_ref_entry()'Karthik Nayak, Jul 11, 2025
  59. 1/4 refs: expose `ref_iterator` via 'refs.h'Karthik Nayak, Jul 11, 2025
  60. 3/4 refs: selectively set prefix in the seek functionsKarthik Nayak, Jul 11, 2025
  61. Christian CouderJul 14, 2025
  62. Karthik NayakJul 15, 2025
  63. 4/4 for-each-ref: introduce a '--start-after' optionKarthik Nayak, Jul 11, 2025
  64. Christian CouderJul 14, 2025
  65. Junio C HamanoJul 14, 2025
  66. Karthik NayakJul 15, 2025
  67. Christian CouderJul 14, 2025
  68. Junio C HamanoJul 14, 2025
  69. Karthik NayakJul 15, 2025
  70. 0/5 for-each-ref: introduce seeking functionality via '--start-after'Karthik Nayak, Jul 15, 2025
  71. 1/5 refs: expose `ref_iterator` via 'refs.h'Karthik Nayak, Jul 15, 2025
  72. 2/5 ref-cache: remove unused function 'find_ref_entry()'Karthik Nayak, Jul 15, 2025
  73. Junio C HamanoJul 17, 2025
  74. Karthik NayakJul 17, 2025
  75. Junio C HamanoJul 17, 2025
  76. 3/5 refs: selectively set prefix in the seek functionsKarthik Nayak, Jul 15, 2025
  77. Jeff KingJul 17, 2025
  78. Karthik NayakJul 17, 2025
  79. Jeff KingJul 17, 2025
  80. 4/5 ref-filter: remove unnecessary else clauseKarthik Nayak, Jul 15, 2025
  81. 5/5 for-each-ref: introduce a '--start-after' optionKarthik Nayak, Jul 15, 2025
  82. Junio C HamanoJul 17, 2025
  83. Karthik NayakJul 22, 2025
  84. Junio C HamanoJul 15, 2025
  85. Kyle LippincottJul 17, 2025
  86. Jeff KingJul 17, 2025
  87. Kyle LippincottJul 17, 2025
  88. Karthik NayakJul 17, 2025
  89. Kyle LippincottJul 17, 2025
  90. Jeff KingJul 17, 2025
  91. Jeff KingJul 17, 2025
  92. Karthik NayakJul 21, 2025
  93. Jeff KingJul 21, 2025
  94. Karthik NayakJul 22, 2025
  95. Junio C HamanoJul 17, 2025
  96. ref-iterator-seek: correctly initialize the prefix_state for a new levelJunio C Hamano, Jul 23, 2025
  97. Kyle LippincottJul 23, 2025
  98. Jeff KingJul 23, 2025
  99. Karthik NayakJul 24, 2025
  100. Junio C HamanoJul 24, 2025
  101. ref-cache: set prefix_state when seekingKarthik Nayak, Jul 24, 2025
  102. Junio C HamanoJul 24, 2025

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.