Skip to contentSkip to conversation

refs/files: keep root refs out of loose ref cache

PATCH Superseded Andy Koppe 3 participants 13 messages Oct 14 – Oct 21 Documentation

lore
Share & tools
lore

Andy Koppe

author
#0 lore raw

PATCH 1/1 refs/files: keep root refs out of loose ref cache

With the files backend, root refs such as HEAD or ORIG_HEAD are only added to the loose ref cache if REFS_FOR_EACH_INCLUDE_ROOT_REFS is passed to the iteration that creates the cache. If the cache has already been created by an earlier iteration, root refs are silently omitted. Conversely, once they have been added, any later iteration also yields them, whether or not it asked for them. This has been the case since d0f00c1ac17 (refs: introduce refs_for_each_include_root_refs()).

For example, git for-each-ref --include-root-refs --merged=main omits root refs that are merged into main, because looking up the given commit loads the replace refs, which populates the loose ref cache first. The reftable backend is not affected.

Fix this by keeping the root refs out of the shared loose ref cache. Instead, read them into a separate ref cache whenever an iteration asks for them, and overlay its iterator on the loose ref iterator. The cache is freed when the iteration finishes.

---
 refs/files-backend.c           | 38 +++++++++++++++++++++++++++-------
 t/t6302-for-each-ref-filter.sh | 12 +++++++++++
 2 files changed, 42 insertions(+), 8 deletions(-)
refs/files-backend.c +30 -8
index 71628550f2..74409878b6 100644
@@ -481,8 +481,21 @@ static void add_root_refs(struct files_ref_store *refs,
 	for_each_root_ref(refs, fill_root_ref, &data);
 }
 
-static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
-					     unsigned int flags)
+/*
+ * Create a separate ref cache containing only the root refs. These are kept
+ * out of the loose ref cache, which may already exist and is shared by
+ * iterations that should not include them.
+ */
+static struct ref_cache *create_root_ref_cache(struct files_ref_store *refs)
+{
+	struct ref_cache *cache = create_ref_cache(&refs->base, NULL);
+
+	cache->root->flag &= ~REF_INCOMPLETE;
+	add_root_refs(refs, get_ref_dir(cache->root));
+	return cache;
+}
+
+static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
 {
 	if (!refs->loose) {
 		struct ref_dir *dir;
@@ -499,9 +512,6 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
 
 		dir = get_ref_dir(refs->loose->root);
 
-		if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS)
-			add_root_refs(refs, dir);
-
 		/*
 		 * Add an incomplete entry for "refs/" (to be filled
 		 * lazily):
@@ -1004,6 +1014,7 @@ struct files_ref_iterator {
 	struct ref_iterator base;
 
 	struct ref_iterator *iter0;
+	struct ref_cache *root_refs;
 	struct repository *repo;
 	unsigned int flags;
 };
@@ -1053,6 +1064,7 @@ static void files_ref_iterator_release(struct ref_iterator *ref_iterator)
 	struct files_ref_iterator *iter =
 		(struct files_ref_iterator *)ref_iterator;
 	ref_iterator_free(iter->iter0);
+	free_ref_cache(iter->root_refs);
 }
 
 static struct ref_iterator_vtable files_ref_iterator_vtable = {
@@ -1068,6 +1080,7 @@ static struct ref_iterator *files_ref_iterator_begin(
 {
 	struct files_ref_store *refs;
 	struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
+	struct ref_cache *root_refs = NULL;
 	struct files_ref_iterator *iter;
 	struct ref_iterator *ref_iterator;
 	unsigned int required_flags = REF_STORE_READ;
@@ -1094,9 +1107,17 @@ static struct ref_iterator *files_ref_iterator_begin(
 	 * disk, and re-reads it if not.
 	 */
 
-	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
+	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
 					      prefix, ref_store->repo, 1);
 
+	if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+		root_refs = create_root_ref_cache(refs);
+		loose_iter = overlay_ref_iterator_begin(
+				cache_ref_iterator_begin(root_refs, prefix,
+							 ref_store->repo, 0),
+				loose_iter);
+	}
+
 	/*
 	 * The packed-refs file might contain broken references, for
 	 * example an old version of a reference that points at an
@@ -1118,6 +1139,7 @@ static struct ref_iterator *files_ref_iterator_begin(
 	ref_iterator = &iter->base;
 	base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
 	iter->iter0 = overlay_iter;
+	iter->root_refs = root_refs;
 	iter->repo = ref_store->repo;
 	iter->flags = flags;
 
@@ -1490,7 +1512,7 @@ static int should_pack_refs(struct files_ref_store *refs,
 	if (limit < 16)
 		limit = 16;
 
-	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
+	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
 					refs->base.repo, 0);
 	while ((ret = ref_iterator_advance(iter)) == ITER_OK) {
 		if (should_pack_ref(refs, &iter->ref, opts))
@@ -1530,7 +1552,7 @@ static int files_optimize(struct ref_store *ref_store,
 
 	packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
 
-	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
+	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
 					refs->base.repo, 0);
 	while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
 		/*
t/t6302-for-each-ref-filter.sh +12 -0
index 7f060d97bf..a2fc257f98 100755
@@ -96,6 +96,18 @@ test_expect_success '--include-root-refs omits dangling symrefs' '
 	)
 '
 
+test_expect_success '--include-root-refs with --merged' '
+	cat >expect <<-\EOF &&
+	HEAD
+	ORIG_HEAD
+	EOF
+	test_when_finished "git update-ref -d ORIG_HEAD" &&
+	git update-ref ORIG_HEAD main &&
+	git for-each-ref --format="%(refname)" --include-root-refs \
+		--merged=main "*HEAD" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'filtering with --points-at' '
 	cat >expect <<-\EOF &&
 	refs/heads/main
Signature · 1 line
2.56.0
#1 lore raw
Quoted 5 lines · Andy Koppe
refs/files-backend.c +0 -0
index 71628550f2..74409878b6 100644

refs/files-backend.c static void add_root_refs(struct files_ref_store *refs, · line 488

Quoted 10 lines
refs/files-backend.c +5 -2
@@ -481,8 +481,21 @@ static void add_root_refs(struct files_ref_store *refs,
 	for_each_root_ref(refs, fill_root_ref, &data);
 }
 
-static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
-					     unsigned int flags)
+/*
+ * Create a separate ref cache containing only the root refs. These are kept
+ * out of the loose ref cache, which may already exist and is shared by
+ * iterations that should not include them.
+ */

This raises the question whether an alternative fix could have been to teach the cache itself to skip over root refs in case the caller is not interested in them. The answer may very well be a "no", but I would be curious to learn why you settled on the proposed fix instead.

Quoted 18 lines
refs/files-backend.c +10 -1
+static struct ref_cache *create_root_ref_cache(struct files_ref_store *refs)
+{
+	struct ref_cache *cache = create_ref_cache(&refs->base, NULL);
+
+	cache->root->flag &= ~REF_INCOMPLETE;
+	add_root_refs(refs, get_ref_dir(cache->root));
+	return cache;
+}
+
+static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
 {
 	if (!refs->loose) {
 		struct ref_dir *dir;
@@ -1094,9 +1107,17 @@ static struct ref_iterator *files_ref_iterator_begin(
 	 * disk, and re-reads it if not.
 	 */
 
-	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),

refs/files-backend.c static struct ref_iterator *files_ref_iterator_begin( · line 1119

Quoted 10 lines
refs/files-backend.c +8 -0
+	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
 					      prefix, ref_store->repo, 1);
 
+	if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+		root_refs = create_root_ref_cache(refs);
+		loose_iter = overlay_ref_iterator_begin(
+				cache_ref_iterator_begin(root_refs, prefix,
+							 ref_store->repo, 0),
+				loose_iter);
+	}

It's a bit weird that we recreate the root ref "cache" for every single ref iterator we create that asks for root refs. Is there a reason why you have decided to not actually cache the root refs the same way as we cache the loose refs?

Thanks!

Patrick

#2 lore raw
Quoted 15 lines · Patrick Steinhardt
Quoted 5 lines

+/*
+ * Create a separate ref cache containing only the root refs. These are kept
+ * out of the loose ref cache, which may already exist and is shared by
+ * iterations that should not include them.
+ */

This raises the question whether an alternative fix could have been to teach the cache itself to skip over root refs in case the caller is not interested in them. The answer may very well be a "no", but I would be curious to learn why you settled on the proposed fix instead.
...

Quoted 4 lines
refs/files-backend.c +1 -1
-	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
+	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
 					      prefix, ref_store->repo, 1);
Quoted 12 lines

refs/files-backend.c static struct ref_iterator *files_ref_iterator_begin( · line 1119

Quoted 7 lines
refs/files-backend.c +7 -0
+	if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+		root_refs = create_root_ref_cache(refs);
+		loose_iter = overlay_ref_iterator_begin(
+				cache_ref_iterator_begin(root_refs, prefix,
+							 ref_store->repo, 0),
+				loose_iter);
+	}

It's a bit weird that we recreate the root ref "cache" for every single ref iterator we create that asks for root refs. Is there a reason why you have decided to not actually cache the root refs the same way as we cache the loose refs?

Curious.

So an alternative would be to have two caches, the primary one for things under refs/ hierarchy and the other one for those outside, and callers can choose to use only the primary one or use both of them?

author
#3 lore raw
Quoted 29 lines · Junio C Hamano
Quoted 23 lines · Patrick Steinhardt
Quoted 5 lines

+/*
+ * Create a separate ref cache containing only the root refs. These are kept
+ * out of the loose ref cache, which may already exist and is shared by
+ * iterations that should not include them.
+ */

This raises the question whether an alternative fix could have been to teach the cache itself to skip over root refs in case the caller is not interested in them. The answer may very well be a "no", but I would be curious to learn why you settled on the proposed fix instead.
...

refs/files-backend.c static struct ref_iterator *files_ref_iterator_begin( · line 1119

Quoted 11 lines
refs/files-backend.c +8 -1
-    loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
+    loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
                                           prefix, ref_store->repo, 1);
 
+    if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+            root_refs = create_root_ref_cache(refs);
+            loose_iter = overlay_ref_iterator_begin(
+                            cache_ref_iterator_begin(root_refs, prefix,
+                                                     ref_store->repo, 0),
+                            loose_iter);
+    }
Quoted 4 lines

It's a bit weird that we recreate the root ref "cache" for every single ref iterator we create that asks for root refs. Is there a reason why you have decided to not actually cache the root refs the same way as we cache the loose refs?

Quoted 6 lines

Curious.

So an alternative would be to have two caches, the primary one for things under refs/ hierarchy and the other one for those outside, and callers can choose to use only the primary one or use both of them?

Thanks for considering this.

I think it makes for a simpler change with less overhead to not actually cache them, as there are only two uses of the REFS_FOR_EACH_INCLUDE_ROOT_REFS flag: in builtin/for-each-ref.c for git for-each-ref --include-root-refs, and in builtin/refs.c for git refs migrate. And both only invoke it once.

I considered keeping the root refs in the loose ref cache but skipping them during iteration if the flag is unset. But that would mean more impact on that much more common case.

A cleaner approach might be to drop the flag and change all call sites that currently ask for all refs without setting the flag, so that they explicitly ask for "refs/" only. That way root refs wouldn't need as much special handling. But of course that would be a much bigger and more risky change.

Andy

#4 lore raw
Quoted 49 lines · Andy Koppe
Quoted 29 lines · Junio C Hamano
Quoted 23 lines · Patrick Steinhardt
Quoted 5 lines

+/*
+ * Create a separate ref cache containing only the root refs. These are kept
+ * out of the loose ref cache, which may already exist and is shared by
+ * iterations that should not include them.
+ */

This raises the question whether an alternative fix could have been to teach the cache itself to skip over root refs in case the caller is not interested in them. The answer may very well be a "no", but I would be curious to learn why you settled on the proposed fix instead.
...

refs/files-backend.c static struct ref_iterator *files_ref_iterator_begin( · line 1119

Quoted 11 lines
refs/files-backend.c +8 -1
-    loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
+    loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
                                           prefix, ref_store->repo, 1);
 
+    if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+            root_refs = create_root_ref_cache(refs);
+            loose_iter = overlay_ref_iterator_begin(
+                            cache_ref_iterator_begin(root_refs, prefix,
+                                                     ref_store->repo, 0),
+                            loose_iter);
+    }
Quoted 4 lines

It's a bit weird that we recreate the root ref "cache" for every single ref iterator we create that asks for root refs. Is there a reason why you have decided to not actually cache the root refs the same way as we cache the loose refs?

Quoted 6 lines

Curious.

So an alternative would be to have two caches, the primary one for things under refs/ hierarchy and the other one for those outside, and callers can choose to use only the primary one or use both of them?

Thanks for considering this.

I think it makes for a simpler change with less overhead to not actually cache them, as there are only two uses of the REFS_FOR_EACH_INCLUDE_ROOT_REFS flag: in builtin/for-each-ref.c for git for-each-ref --include-root-refs, and in builtin/refs.c for git refs migrate. And both only invoke it once.

I considered keeping the root refs in the loose ref cache but skipping them during iteration if the flag is unset. But that would mean more impact on that much more common case.

Quoted 5 lines

A cleaner approach might be to drop the flag and change all call sites that currently ask for all refs without setting the flag, so that they explicitly ask for "refs/" only. That way root refs wouldn't need as much special handling. But of course that would be a much bigger and more risky change.

That's fair. I guess the part that triggers me is that it's called a "cache" whereas we don't really use it as one in this instance. But that's a historical wart, and I agree that the proposed solution is good enough.

Thanks!

Patrick

#5 lore raw
Quoted 4 lines · Patrick Steinhardt
Quoted 3 lines · Andy Koppe
Quoted 2 lines · Junio C Hamano

Patrick Steinhardt <[email protected]> writes:
...

So in short ...

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

This raises the question whether an alternative fix could have been to teach the cache itself to skip over root refs in case the caller is not interested in them. The answer may very well be a "no", but I would be curious to learn why you settled on the proposed fix instead.

... with this cleared in the proposed log message of v2 iteration, ...

Quoted 10 lines
Quoted 5 lines

A cleaner approach might be to drop the flag and change all call sites that currently ask for all refs without setting the flag, so that they explicitly ask for "refs/" only. That way root refs wouldn't need as much special handling. But of course that would be a much bigger and more risky change.

That's fair. I guess the part that triggers me is that it's called a "cache" whereas we don't really use it as one in this instance. But that's a historical wart, and I agree that the proposed solution is good enough.

... the topic will be ready to go?

Thanks.

author
#6 lore raw

PATCH v2 1/1 refs/files: keep root refs out of loose ref cache

In reply to Andy Koppe

With the files backend, root refs such as HEAD or ORIG_HEAD are only added to the loose ref cache if REFS_FOR_EACH_INCLUDE_ROOT_REFS is passed when the cache is created. If the cache has already been created for an earlier ref iterator, root refs are silently omitted. Conversely, once they have been added, any later iterator also yields them, whether or not they were requested. This has been the case since d0f00c1ac1 (refs: introduce refs_for_each_include_root_refs(), 2024-02-23).

For example, git for-each-ref --include-root-refs --merged=main omits root refs that are merged into main, because looking up the given commit loads the replace refs, which populates the loose ref cache first. The reftable backend is not affected, as it filters root refs for each iterator rather than caching them.

Fix this by keeping the root refs out of the loose ref cache. Instead, read them into a separate, temporary ref cache whenever an iterator is requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS, and overlay an iterator over that cache on the loose ref iterator. The cache is freed along with the iterator.

Alternatively, the root refs could always be added to the loose ref cache and skipped during iteration unless requested. But that would mean reading them in every command that uses the loose ref cache, even though only two call sites actually request them. Also, keeping the root ref cache for reuse by later iterators is not worthwhile, because those two call sites only request the root refs once per command.

Correct the documentation, help text and flag comment for --include-root-refs, which wrongly claim that it lists pseudorefs.

---
Changes since v1:
- Move create_root_ref_cache() below get_loose_ref_cache() for a smaller diff, and tweak its comment.
- Correct the documentation, help text and refs.h comment for --include-root-refs, which claimed that it lists pseudorefs.
- Consider alternative approaches in the log message.

 Documentation/for-each-ref-options.adoc |  3 +-
 builtin/for-each-ref.c                  |  2 +-
 refs.h                                  |  4 +--
 refs/files-backend.c                    | 37 +++++++++++++++++++------
 t/t6302-for-each-ref-filter.sh          | 12 ++++++++
 5 files changed, 46 insertions(+), 12 deletions(-)
Documentation/for-each-ref-options.adoc +2 -1
index 54e2fa95c2..b93ffbd74d 100644
@@ -73,7 +73,8 @@ TAB %(refname)`.
 	using the same rules as _<pattern>_ above.
 
 `--include-root-refs`::
-	List root refs (`HEAD` and pseudorefs) apart from regular refs.
+	List root refs such as `HEAD` and `ORIG_HEAD` in addition to regular
+	refs. (The pseudorefs `FETCH_HEAD` and `MERGE_HEAD` are not included.)
 
 `--start-after=<marker>`::
     Allows paginating the output by skipping references up to and including the
builtin/for-each-ref.c +1 -1
index 4a2fc421db..a12e46cdbe 100644
@@ -48,7 +48,7 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re
 		OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
 		OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
 		OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
-		OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")),
+		OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include root refs like HEAD")),
 		OPT_END(),
 	};
 
refs.h +2 -2
index 9979446d15..5319e0b570 100644
@@ -444,8 +444,8 @@ enum refs_for_each_flag {
 	REFS_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2),
 
 	/*
-	 * Include root refs i.e. HEAD and pseudorefs along with the regular
-	 * refs.
+	 * Include root refs such as HEAD and ORIG_HEAD in addition to the
+	 * regular refs. Pseudorefs are not root refs and thus not included.
 	 */
 	REFS_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3),
 };
refs/files-backend.c +29 -8
index 71628550f2..9d1e527114 100644
@@ -481,8 +481,7 @@ static void add_root_refs(struct files_ref_store *refs,
 	for_each_root_ref(refs, fill_root_ref, &data);
 }
 
-static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
-					     unsigned int flags)
+static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
 {
 	if (!refs->loose) {
 		struct ref_dir *dir;
@@ -499,9 +498,6 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
 
 		dir = get_ref_dir(refs->loose->root);
 
-		if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS)
-			add_root_refs(refs, dir);
-
 		/*
 		 * Add an incomplete entry for "refs/" (to be filled
 		 * lazily):
@@ -511,6 +507,19 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
 	return refs->loose;
 }
 
+/*
+ * Root refs are kept out of the loose ref cache because ref iterators must
+ * only include them when requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS.
+ */
+static struct ref_cache *create_root_ref_cache(struct files_ref_store *refs)
+{
+	struct ref_cache *cache = create_ref_cache(&refs->base, NULL);
+
+	cache->root->flag &= ~REF_INCOMPLETE;
+	add_root_refs(refs, get_ref_dir(cache->root));
+	return cache;
+}
+
 static int read_ref_internal(struct ref_store *ref_store, const char *refname,
 			     struct object_id *oid, struct strbuf *referent,
 			     unsigned int *type, int *failure_errno, int skip_packed_refs)
@@ -1004,6 +1013,7 @@ struct files_ref_iterator {
 	struct ref_iterator base;
 
 	struct ref_iterator *iter0;
+	struct ref_cache *root_refs;
 	struct repository *repo;
 	unsigned int flags;
 };
@@ -1053,6 +1063,7 @@ static void files_ref_iterator_release(struct ref_iterator *ref_iterator)
 	struct files_ref_iterator *iter =
 		(struct files_ref_iterator *)ref_iterator;
 	ref_iterator_free(iter->iter0);
+	free_ref_cache(iter->root_refs);
 }
 
 static struct ref_iterator_vtable files_ref_iterator_vtable = {
@@ -1068,6 +1079,7 @@ static struct ref_iterator *files_ref_iterator_begin(
 {
 	struct files_ref_store *refs;
 	struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
+	struct ref_cache *root_refs = NULL;
 	struct files_ref_iterator *iter;
 	struct ref_iterator *ref_iterator;
 	unsigned int required_flags = REF_STORE_READ;
@@ -1094,9 +1106,17 @@ static struct ref_iterator *files_ref_iterator_begin(
 	 * disk, and re-reads it if not.
 	 */
 
-	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
+	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
 					      prefix, ref_store->repo, 1);
 
+	if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+		root_refs = create_root_ref_cache(refs);
+		loose_iter = overlay_ref_iterator_begin(
+				cache_ref_iterator_begin(root_refs, prefix,
+							 ref_store->repo, 0),
+				loose_iter);
+	}
+
 	/*
 	 * The packed-refs file might contain broken references, for
 	 * example an old version of a reference that points at an
@@ -1118,6 +1138,7 @@ static struct ref_iterator *files_ref_iterator_begin(
 	ref_iterator = &iter->base;
 	base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
 	iter->iter0 = overlay_iter;
+	iter->root_refs = root_refs;
 	iter->repo = ref_store->repo;
 	iter->flags = flags;
 
@@ -1490,7 +1511,7 @@ static int should_pack_refs(struct files_ref_store *refs,
 	if (limit < 16)
 		limit = 16;
 
-	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
+	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
 					refs->base.repo, 0);
 	while ((ret = ref_iterator_advance(iter)) == ITER_OK) {
 		if (should_pack_ref(refs, &iter->ref, opts))
@@ -1530,7 +1551,7 @@ static int files_optimize(struct ref_store *ref_store,
 
 	packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
 
-	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
+	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
 					refs->base.repo, 0);
 	while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
 		/*
t/t6302-for-each-ref-filter.sh +12 -0
index 7f060d97bf..a2fc257f98 100755
@@ -96,6 +96,18 @@ test_expect_success '--include-root-refs omits dangling symrefs' '
 	)
 '
 
+test_expect_success '--include-root-refs with --merged' '
+	cat >expect <<-\EOF &&
+	HEAD
+	ORIG_HEAD
+	EOF
+	test_when_finished "git update-ref -d ORIG_HEAD" &&
+	git update-ref ORIG_HEAD main &&
+	git for-each-ref --format="%(refname)" --include-root-refs \
+		--merged=main "*HEAD" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'filtering with --points-at' '
 	cat >expect <<-\EOF &&
 	refs/heads/main
Signature · 1 line
2.56.0
#7 lore raw
Quoted 6 lines · Andy Koppe

Changes since v1:
- Move create_root_ref_cache() below get_loose_ref_cache() for a smaller diff, and tweak its comment.
- Correct the documentation, help text and refs.h comment for --include-root-refs, which claimed that it lists pseudorefs.
- Consider alternative approaches in the log message.

It would help to also get a range-diff relative to v1 so that a reviewer can see what exxactly has changed between the two versions.

Quoted 5 lines
Documentation/for-each-ref-options.adoc +0 -0
index 54e2fa95c2..b93ffbd74d 100644

Documentation/for-each-ref-options.adoc TAB %(refname)`. · line 80

Quoted 9 lines
Documentation/for-each-ref-options.adoc +2 -1
@@ -73,7 +73,8 @@ TAB %(refname)`.
 	using the same rules as _<pattern>_ above.
 
 `--include-root-refs`::
-	List root refs (`HEAD` and pseudorefs) apart from regular refs.
+	List root refs such as `HEAD` and `ORIG_HEAD` in addition to regular
+	refs. (The pseudorefs `FETCH_HEAD` and `MERGE_HEAD` are not included.)
 
 `--start-after=<marker>`::
     Allows paginating the output by skipping references up to and including the

These changes make sense, but they are unrelated to the fix. They should thus go into a separate commit.

Patrick

author
#8 lore raw
Quoted 10 lines · Patrick Steinhardt
Quoted 6 lines · Andy Koppe

Changes since v1:
- Move create_root_ref_cache() below get_loose_ref_cache() for a smaller diff, and tweak its comment.
- Correct the documentation, help text and refs.h comment for --include-root-refs, which claimed that it lists pseudorefs.
- Consider alternative approaches in the log message.

It would help to also get a range-diff relative to v1 so that a reviewer can see what exxactly has changed between the two versions.

Done with v3, relative to v1.

Quoted 12 lines
Quoted 12 lines
Documentation/for-each-ref-options.adoc +2 -1
index 54e2fa95c2..b93ffbd74d 100644
@@ -73,7 +73,8 @@ TAB %(refname)`.
      using the same rules as _<pattern>_ above.
 
 `--include-root-refs`::
-     List root refs (`HEAD` and pseudorefs) apart from regular refs.
+     List root refs such as `HEAD` and `ORIG_HEAD` in addition to regular
+     refs. (The pseudorefs `FETCH_HEAD` and `MERGE_HEAD` are not included.)
 
Quoted 5 lines

Documentation/for-each-ref-options.adoc TAB %(refname)`. · line 80

Quoted 2 lines
Documentation/for-each-ref-options.adoc +0 -0
 `--start-after=<marker>`::
     Allows paginating the output by skipping references up to and including the

These changes make sense, but they are unrelated to the fix. They should thus go into a separate commit.

Dropped from v3. I'll put them into a separate patch, along with other fixes for outdated uses of "pseudoref".

Andy

author
#9 lore raw

PATCH v3 1/1 refs/files: keep root refs out of loose ref cache

In reply to Andy Koppe

With the files backend, root refs such as HEAD or ORIG_HEAD are only added to the loose ref cache if REFS_FOR_EACH_INCLUDE_ROOT_REFS is passed when the cache is created. If the cache has already been created for an earlier ref iterator, root refs are silently omitted. Conversely, once they have been added, any later iterator also yields them, whether or not they were requested. This has been the case since d0f00c1ac1 (refs: introduce refs_for_each_include_root_refs(), 2024-02-23).

For example, git for-each-ref --include-root-refs --merged=main omits root refs that are merged into main, because looking up the given commit loads the replace refs, which populates the loose ref cache first. The reftable backend is not affected, as it filters root refs for each iterator rather than caching them.

Fix this by keeping the root refs out of the loose ref cache. Instead, read them into a separate, temporary ref cache whenever an iterator is requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS, and overlay an iterator over that cache on the loose ref iterator. The cache is freed along with the iterator.

Alternatively, the root refs could always be added to the loose ref cache and skipped during iteration unless requested. But that would mean reading them in every command that uses the loose ref cache, even though only two call sites actually request them. Also, keeping the root ref cache for reuse by later iterators is not worthwhile, because those two call sites only request the root refs once per command.

---
Changes from v1:
- Amend commit message for clarity and to discuss alternatives.
- Move new create_root_ref_cache() below get_loose_ref_cache() because its comment references the loose ref cache and because it makes for a cleaner diff. Tighten up the comment.

Changes from v2:
- Drop fixes to documentation of REFS_FOR_EACH_INCLUDE_ROOT_REFS and --include-root-refs added in v2. I'll create a separate patch with those as well as other fixes for outdated uses of "pseudoref".

The range diff shows the changes from v1, because they're of more interest than dropping the documentation changes I'd added in v2.

 refs/files-backend.c           | 37 ++++++++++++++++++++++++++--------
 t/t6302-for-each-ref-filter.sh | 12 +++++++++++
 2 files changed, 41 insertions(+), 8 deletions(-)
refs/files-backend.c +29 -8
index 71628550f2..9d1e527114 100644
@@ -481,8 +481,7 @@ static void add_root_refs(struct files_ref_store *refs,
 	for_each_root_ref(refs, fill_root_ref, &data);
 }
 
-static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
-					     unsigned int flags)
+static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
 {
 	if (!refs->loose) {
 		struct ref_dir *dir;
@@ -499,9 +498,6 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
 
 		dir = get_ref_dir(refs->loose->root);
 
-		if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS)
-			add_root_refs(refs, dir);
-
 		/*
 		 * Add an incomplete entry for "refs/" (to be filled
 		 * lazily):
@@ -511,6 +507,19 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
 	return refs->loose;
 }
 
+/*
+ * Root refs are kept out of the loose ref cache because ref iterators must
+ * only include them when requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS.
+ */
+static struct ref_cache *create_root_ref_cache(struct files_ref_store *refs)
+{
+	struct ref_cache *cache = create_ref_cache(&refs->base, NULL);
+
+	cache->root->flag &= ~REF_INCOMPLETE;
+	add_root_refs(refs, get_ref_dir(cache->root));
+	return cache;
+}
+
 static int read_ref_internal(struct ref_store *ref_store, const char *refname,
 			     struct object_id *oid, struct strbuf *referent,
 			     unsigned int *type, int *failure_errno, int skip_packed_refs)
@@ -1004,6 +1013,7 @@ struct files_ref_iterator {
 	struct ref_iterator base;
 
 	struct ref_iterator *iter0;
+	struct ref_cache *root_refs;
 	struct repository *repo;
 	unsigned int flags;
 };
@@ -1053,6 +1063,7 @@ static void files_ref_iterator_release(struct ref_iterator *ref_iterator)
 	struct files_ref_iterator *iter =
 		(struct files_ref_iterator *)ref_iterator;
 	ref_iterator_free(iter->iter0);
+	free_ref_cache(iter->root_refs);
 }
 
 static struct ref_iterator_vtable files_ref_iterator_vtable = {
@@ -1068,6 +1079,7 @@ static struct ref_iterator *files_ref_iterator_begin(
 {
 	struct files_ref_store *refs;
 	struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
+	struct ref_cache *root_refs = NULL;
 	struct files_ref_iterator *iter;
 	struct ref_iterator *ref_iterator;
 	unsigned int required_flags = REF_STORE_READ;
@@ -1094,9 +1106,17 @@ static struct ref_iterator *files_ref_iterator_begin(
 	 * disk, and re-reads it if not.
 	 */
 
-	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
+	loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
 					      prefix, ref_store->repo, 1);
 
+	if (flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS) {
+		root_refs = create_root_ref_cache(refs);
+		loose_iter = overlay_ref_iterator_begin(
+				cache_ref_iterator_begin(root_refs, prefix,
+							 ref_store->repo, 0),
+				loose_iter);
+	}
+
 	/*
 	 * The packed-refs file might contain broken references, for
 	 * example an old version of a reference that points at an
@@ -1118,6 +1138,7 @@ static struct ref_iterator *files_ref_iterator_begin(
 	ref_iterator = &iter->base;
 	base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
 	iter->iter0 = overlay_iter;
+	iter->root_refs = root_refs;
 	iter->repo = ref_store->repo;
 	iter->flags = flags;
 
@@ -1490,7 +1511,7 @@ static int should_pack_refs(struct files_ref_store *refs,
 	if (limit < 16)
 		limit = 16;
 
-	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
+	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
 					refs->base.repo, 0);
 	while ((ret = ref_iterator_advance(iter)) == ITER_OK) {
 		if (should_pack_ref(refs, &iter->ref, opts))
@@ -1530,7 +1551,7 @@ static int files_optimize(struct ref_store *ref_store,
 
 	packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
 
-	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
+	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
 					refs->base.repo, 0);
 	while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
 		/*
t/t6302-for-each-ref-filter.sh +12 -0
index 7f060d97bf..a2fc257f98 100755
@@ -96,6 +96,18 @@ test_expect_success '--include-root-refs omits dangling symrefs' '
 	)
 '
 
+test_expect_success '--include-root-refs with --merged' '
+	cat >expect <<-\EOF &&
+	HEAD
+	ORIG_HEAD
+	EOF
+	test_when_finished "git update-ref -d ORIG_HEAD" &&
+	git update-ref ORIG_HEAD main &&
+	git for-each-ref --format="%(refname)" --include-root-refs \
+		--merged=main "*HEAD" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'filtering with --points-at' '
 	cat >expect <<-\EOF &&
 	refs/heads/main

Range-diff against v1:
1: 5db169a16e ! 1: 49f2b11aad refs/files: keep root refs out of loose ref cache
@@ Commit message

With the files backend, root refs such as HEAD or ORIG_HEAD are only added to the loose ref cache if REFS_FOR_EACH_INCLUDE_ROOT_REFS is
- passed to the iteration that creates the cache. If the cache has already
- been created by an earlier iteration, root refs are silently omitted.
- Conversely, once they have been added, any later iteration also yields
- them, whether or not it asked for them. This has been the case since
- d0f00c1ac17 (refs: introduce refs_for_each_include_root_refs()).
+ passed when the cache is created. If the cache has already been created
+ for an earlier ref iterator, root refs are silently omitted. Conversely,
+ once they have been added, any later iterator also yields them, whether
+ or not they were requested. This has been the case since d0f00c1ac1
+ (refs: introduce refs_for_each_include_root_refs(), 2024-02-23).

For example, git for-each-ref --include-root-refs --merged=main omits root refs that are merged into main, because looking up the given commit loads the replace refs, which populates the loose ref cache first. The
- reftable backend is not affected.
+ reftable backend is not affected, as it filters root refs for each
+ iterator rather than caching them.

- Fix this by keeping the root refs out of the shared loose ref cache.
- Instead, read them into a separate ref cache whenever an iteration asks
- for them, and overlay its iterator on the loose ref iterator. The cache
- is freed when the iteration finishes.
+ Fix this by keeping the root refs out of the loose ref cache. Instead,
+ read them into a separate, temporary ref cache whenever an iterator is
+ requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS, and overlay an iterator
+ over that cache on the loose ref iterator. The cache is freed along with
+ the iterator.
+
+ Alternatively, the root refs could always be added to the loose ref
+ cache and skipped during iteration unless requested. But that would mean
+ reading them in every command that uses the loose ref cache, even though
+ only two call sites actually request them. Also, keeping the root ref
+ cache for reuse by later iterators is not worthwhile, because those two
+ call sites only request the root refs once per command.

@@ refs/files-backend.c: static void add_root_refs(struct files_ref_store *refs,

-static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
- unsigned int flags)
-+/*
-+ * Create a separate ref cache containing only the root refs. These are kept
-+ * out of the loose ref cache, which may already exist and is shared by
-+ * iterations that should not include them.
-+ */
-+static struct ref_cache *create_root_ref_cache(struct files_ref_store *refs)
-+{
-+ struct ref_cache *cache = create_ref_cache(&refs->base, NULL);
-+
-+ cache->root->flag &= ~REF_INCOMPLETE;
-+ add_root_refs(refs, get_ref_dir(cache->root));
-+ return cache;
-+}
-+
+static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
{
if (!refs->loose) {
@@ refs/files-backend.c: static struct ref_cache *get_loose_ref_cache(struct files_
/*
* Add an incomplete entry for "refs/" (to be filled
* lazily):
+@@ refs/files-backend.c: static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
+ return refs->loose;
+ }
+
++/*
++ * Root refs are kept out of the loose ref cache because ref iterators must
++ * only include them when requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS.
++ */
++static struct ref_cache *create_root_ref_cache(struct files_ref_store *refs)
++{
++ struct ref_cache *cache = create_ref_cache(&refs->base, NULL);
++
++ cache->root->flag &= ~REF_INCOMPLETE;
++ add_root_refs(refs, get_ref_dir(cache->root));
++ return cache;
++}
++
+ static int read_ref_internal(struct ref_store *ref_store, const char *refname,
+ struct object_id *oid, struct strbuf *referent,
+ unsigned int *type, int *failure_errno, int skip_packed_refs)
@@ refs/files-backend.c: struct files_ref_iterator {
struct ref_iterator base;

Signature · 1 line
2.56.0
#10 lore raw
Quoted 20 lines · Andy Koppe

With the files backend, root refs such as HEAD or ORIG_HEAD are only added to the loose ref cache if REFS_FOR_EACH_INCLUDE_ROOT_REFS is passed when the cache is created. If the cache has already been created for an earlier ref iterator, root refs are silently omitted. Conversely, once they have been added, any later iterator also yields them, whether or not they were requested. This has been the case since d0f00c1ac1 (refs: introduce refs_for_each_include_root_refs(), 2024-02-23).

For example, git for-each-ref --include-root-refs --merged=main omits root refs that are merged into main, because looking up the given commit loads the replace refs, which populates the loose ref cache first. The reftable backend is not affected, as it filters root refs for each iterator rather than caching them.

Fix this by keeping the root refs out of the loose ref cache. Instead, read them into a separate, temporary ref cache whenever an iterator is requested with REFS_FOR_EACH_INCLUDE_ROOT_REFS, and overlay an iterator over that cache on the loose ref iterator. The cache is freed along with the iterator.

Quoted 6 lines

Alternatively, the root refs could always be added to the loose ref cache and skipped during iteration unless requested. But that would mean reading them in every command that uses the loose ref cache, even though only two call sites actually request them. Also, keeping the root ref cache for reuse by later iterators is not worthwhile, because those two call sites only request the root refs once per command.

Thanks for spelling out alternative implementations and why we didn't use them.

Quoted 8 lines

---
Changes from v1:
- Amend commit message for clarity and to discuss alternatives.
- Move new create_root_ref_cache() below get_loose_ref_cache() because its comment references the loose ref cache and because it makes for a cleaner diff. Tighten up the comment.

Quoted 7 lines

Changes from v2:
- Drop fixes to documentation of REFS_FOR_EACH_INCLUDE_ROOT_REFS and --include-root-refs added in v2. I'll create a separate patch with those as well as other fixes for outdated uses of "pseudoref".

The range diff shows the changes from v1, because they're of more interest than dropping the documentation changes I'd added in v2.

Thanks for this. By the way, I would recommend to have a look at b4, which handles all of the bookkeeping for you.

In any case, this patch looks good to me. Thanks!

Patrick

#11 lore raw
Quoted 10 lines · Patrick Steinhardt
Quoted 7 lines

Changes from v2:
- Drop fixes to documentation of REFS_FOR_EACH_INCLUDE_ROOT_REFS and --include-root-refs added in v2. I'll create a separate patch with those as well as other fixes for outdated uses of "pseudoref".

The range diff shows the changes from v1, because they're of more interest than dropping the documentation changes I'd added in v2.

Thanks for this. By the way, I would recommend to have a look at b4, which handles all of the bookkeeping for you.

Perhaps it cuts both ways. Can you tell b4 to do the flexible thing the author did in this case?

Quoted 1 line

In any case, this patch looks good to me. Thanks!

Thanks for writing and reviewing. Let me mark the topic for 'next'.

#12 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Patrick Steinhardt
Quoted 5 lines

Changes from v2:
- Drop fixes to documentation of REFS_FOR_EACH_INCLUDE_ROOT_REFS and --include-root-refs added in v2. I'll create a separate patch with those as well as other fixes for outdated uses of "pseudoref".

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

The range diff shows the changes from v1, because they're of more interest than dropping the documentation changes I'd added in v2.

Thanks for this. By the way, I would recommend to have a look at b4, which handles all of the bookkeeping for you.

Perhaps it cuts both ways. Can you tell b4 to do the flexible thing the author did in this case?

No, it's more restricted in this case. But mistakes like this wouldn't happen anymore if one were to use it right from the start, so it would not even have been necessary to have the flexibility :)

Patrick