git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] refs/ref-cache: stop seeking into empty directories
@ 2025-09-24  7:55 Karthik Nayak
  2025-09-24 12:28 ` Patrick Steinhardt
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Karthik Nayak @ 2025-09-24  7:55 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak

The 'cache_ref_iterator_seek()' function is used to seek the
`ref_iterator` to the desired reference in the ref-cache mechanism. We
use the seeking functionality to implement the '--start-after' flag in
'git-for-each-ref(1)'.

When using the files-backend with packed-refs, it is possible that some
of the refs directories are empty. For e.g. just after repacking, the
'refs/heads' directory would be empty. The ref-cache seek mechanism
doesn't take this into consideration, causing SEGFAULT as we try to
access entries within the directory. Fix this by breaking out of the
loop when we enter an empty directory.

Add tests which simulate this behavior and also provide coverage over
using the feature over packed-refs.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 refs/ref-cache.c               |  3 ++
 t/t6302-for-each-ref-filter.sh | 65 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/refs/ref-cache.c b/refs/ref-cache.c
index c180e0aad7..8a260028ec 100644
--- a/refs/ref-cache.c
+++ b/refs/ref-cache.c
@@ -507,6 +507,9 @@ static int cache_ref_iterator_seek(struct ref_iterator *ref_iterator,
 			slash = strchr(slash, '/');
 			len = slash ? (size_t)(slash - refname) : strlen(refname);
 
+			if (dir->nr == 0)
+				break;
+
 			for (idx = 0; idx < dir->nr; idx++) {
 				cmp = strncmp(refname, dir->entries[idx]->name, len);
 				if (cmp <= 0)
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index 9b80ea1e3b..d14567cb62 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -754,4 +754,69 @@ test_expect_success 'start after used with custom sort order' '
 	test_cmp expect actual
 '
 
+test_expect_success 'start after with packed refs' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit default &&
+
+		git update-ref --stdin <<-EOF &&
+		create refs/heads/branch @
+		create refs/heads/side @
+		create refs/odd/spot @
+		create refs/tags/one @
+		create refs/tags/two @
+		commit
+		EOF
+
+		cat >expect <<-\EOF &&
+		refs/tags/default
+		refs/tags/one
+		refs/tags/two
+		EOF
+
+		git pack-refs --all &&
+		git for-each-ref --format="%(refname)" --start-after=refs/odd/spot >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'start after with packed refs and some loose refs' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit default &&
+
+		git update-ref --stdin <<-EOF &&
+		create refs/heads/branch @
+		create refs/heads/side @
+		create refs/odd/spot @
+		create refs/tags/one @
+		create refs/tags/two @
+		commit
+		EOF
+
+		git pack-refs --all &&
+
+		git update-ref --stdin <<-EOF &&
+		create refs/heads/foo @
+		create refs/odd/tee @
+		commit
+		EOF
+
+		cat >expect <<-\EOF &&
+		refs/odd/tee
+		refs/tags/default
+		refs/tags/one
+		refs/tags/two
+		EOF
+
+
+		git for-each-ref --format="%(refname)" --start-after=refs/odd/spot >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done




^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-10-01 12:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-24  7:55 [PATCH] refs/ref-cache: stop seeking into empty directories Karthik Nayak
2025-09-24 12:28 ` Patrick Steinhardt
2025-09-25  8:20   ` Karthik Nayak
2025-09-25  9:15 ` [PATCH v2] refs/ref-cache: fix SEGFAULT when seeking in " Karthik Nayak
2025-09-25 17:07   ` Junio C Hamano
2025-09-26  7:41     ` Karthik Nayak
2025-09-26  7:44       ` Karthik Nayak
2025-09-26 16:30       ` Junio C Hamano
2025-09-29 14:47         ` Karthik Nayak
2025-10-01 12:17 ` [PATCH v3] " Karthik Nayak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).