Skip to contentSkip to conversation

refs: cleanup code around optimizations

PATCH Superseded Karthik Nayak 3 participants 14 messages Oct 15, 2025 – Oct 16, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover refs: cleanup code around optimizations

This is extracted from a recent series I sent [1], which I've since dropped to follow up with a different approach. I think these patches hold value individually.

They mostly cleanup code around 'git refs optimize' which was added recently in db0babf9b2 (Merge branch 'ms/refs-optimize', 2025-10-02). The code in the refs subsystem contains both 'pack-refs' and 'optimize' functions, which are one and the same.

This series unifies this to only retain the 'optimize' functions and naming, since it backend generic.

This is based on top of master 143f58ef75 (Sync with Git 2.51.1, 2025-10-15) with 'ps/ref-peeled-tags' merged in.

[1]: 20251010-562-add-option-to-check-if-reference-backend-needs-repacking-v1-0-c7962be584fa@gmail.com

---
 pack-refs.c                   |  8 ++++----
 refs.c                        |  8 +-------
 refs.h                        | 24 +++++++++---------------
 refs/debug.c                  |  8 ++++----
 refs/files-backend.c          | 22 ++++++----------------
 refs/packed-backend.c         |  6 +++---
 refs/refs-internal.h          |  5 +----
 refs/reftable-backend.c       | 15 ++++-----------
 t/pack-refs-tests.sh          |  2 --
 t/t0601-reffiles-pack-refs.sh |  2 ++
 t/t1463-refs-optimize.sh      |  2 ++
 11 files changed, 36 insertions(+), 66 deletions(-)

Karthik Nayak (4):
refs: move to using the '.optimize' functions
refs: cleanup code around optimization
refs: rename 'pack_refs_opts' to 'refs_optimize_opts' t/pack-refs-tests: move the 'test_done' to callees

base-commit: 854a80fd48848f942c4a566c9880dc5f089887af
change-id: 20251015-refs-code-cleanup-871a7ce7c8dd

Thanks
- Karthik

#1 lore raw

PATCH 1/4 refs: move to using the '.optimize' functions

The struct ref_store variable, exposes two ways to optimize a reftable backend:

1. pack_refs
2. optimize

The former was specific to the 'files' + 'packed' refs backend. The latter is more generic and covers all backends. While the naming is different, both of these functions perform the same functionality.

In the following commit, we will consolidate this code to only maintain the 'optimize' functions. In preparation, modify the backends so that they exclusively implement the optimize callback, only. All users of the refs subsystem already use the 'optimize' function so there is no changes needed on the callee side.

---
 refs/debug.c            |  8 ++++----
 refs/files-backend.c    | 14 ++------------
 refs/packed-backend.c   |  6 +++---
 refs/reftable-backend.c | 13 +++----------
 4 files changed, 12 insertions(+), 29 deletions(-)
refs/debug.c +4 -4
index 01499b9033..40cd1d9c15 100644
@@ -116,11 +116,11 @@ static int debug_transaction_abort(struct ref_store *refs,
 	return res;
 }
 
-static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)
+static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->pack_refs(drefs->refs, opts);
-	trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
+	int res = drefs->refs->be->optimize(drefs->refs, opts);
+	trace_printf_key(&trace_refs, "optimize: %d\n", res);
 	return res;
 }
 
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
 	.transaction_finish = debug_transaction_finish,
 	.transaction_abort = debug_transaction_abort,
 
-	.pack_refs = debug_pack_refs,
+	.optimize = debug_optimize,
 	.rename_ref = debug_rename_ref,
 	.copy_ref = debug_copy_ref,
 
refs/files-backend.c +2 -12
index ed8a1729d6..92d90fc508 100644
@@ -1444,8 +1444,8 @@ static int should_pack_refs(struct files_ref_store *refs,
 	return 0;
 }
 
-static int files_pack_refs(struct ref_store *ref_store,
-			   struct pack_refs_opts *opts)
+static int files_optimize(struct ref_store *ref_store,
+			  struct pack_refs_opts *opts)
 {
 	struct files_ref_store *refs =
 		files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
@@ -1512,15 +1512,6 @@ static int files_pack_refs(struct ref_store *ref_store,
 	return 0;
 }
 
-static int files_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
-{
-	/*
-	 * For the "files" backend, "optimizing" is the same as "packing".
-	 * So, we just call the existing worker function for packing.
-	 */
-	return files_pack_refs(ref_store, opts);
-}
-
 /*
  * People using contrib's git-new-workdir have .git/logs/refs ->
  * /some/other/path/.git/logs/refs, and that may live on another device.
@@ -3969,7 +3960,6 @@ struct ref_storage_be refs_be_files = {
 	.transaction_finish = files_transaction_finish,
 	.transaction_abort = files_transaction_abort,
 
-	.pack_refs = files_pack_refs,
 	.optimize = files_optimize,
 	.rename_ref = files_rename_ref,
 	.copy_ref = files_copy_ref,
refs/packed-backend.c +3 -3
index 1ab0c50393..20cf9fab18 100644
@@ -1773,8 +1773,8 @@ static int packed_transaction_finish(struct ref_store *ref_store,
 	return ret;
 }
 
-static int packed_pack_refs(struct ref_store *ref_store UNUSED,
-			    struct pack_refs_opts *pack_opts UNUSED)
+static int packed_optimize(struct ref_store *ref_store UNUSED,
+			   struct pack_refs_opts *pack_opts UNUSED)
 {
 	/*
 	 * Packed refs are already packed. It might be that loose refs
@@ -2129,7 +2129,7 @@ struct ref_storage_be refs_be_packed = {
 	.transaction_finish = packed_transaction_finish,
 	.transaction_abort = packed_transaction_abort,
 
-	.pack_refs = packed_pack_refs,
+	.optimize = packed_optimize,
 	.rename_ref = NULL,
 	.copy_ref = NULL,
 
refs/reftable-backend.c +3 -10
index 6bbfd5618d..43cc66a48e 100644
@@ -1700,11 +1700,11 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,
 	return ret;
 }
 
-static int reftable_be_pack_refs(struct ref_store *ref_store,
-				 struct pack_refs_opts *opts)
+static int reftable_be_optimize(struct ref_store *ref_store,
+				struct pack_refs_opts *opts)
 {
 	struct reftable_ref_store *refs =
-		reftable_be_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, "pack_refs");
+		reftable_be_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, "optimize_refs");
 	struct reftable_stack *stack;
 	int ret;
 
@@ -1733,12 +1733,6 @@ static int reftable_be_pack_refs(struct ref_store *ref_store,
 	return ret;
 }
 
-static int reftable_be_optimize(struct ref_store *ref_store,
-				struct pack_refs_opts *opts)
-{
-	return reftable_be_pack_refs(ref_store, opts);
-}
-
 struct write_create_symref_arg {
 	struct reftable_ref_store *refs;
 	struct reftable_stack *stack;
@@ -2761,7 +2755,6 @@ struct ref_storage_be refs_be_reftable = {
 	.transaction_finish = reftable_be_transaction_finish,
 	.transaction_abort = reftable_be_transaction_abort,
 
-	.pack_refs = reftable_be_pack_refs,
 	.optimize = reftable_be_optimize,
 	.rename_ref = reftable_be_rename_ref,
 	.copy_ref = reftable_be_copy_ref,
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH 1/4] refs: move to using the '.optimize' functions

Quoted 1 line · Karthik Nayak

The struct ref_store variable, exposes two ways to optimize a reftable

s/variable,/variable/

Quoted 43 lines

backend:

1. pack_refs
2. optimize

The former was specific to the 'files' + 'packed' refs backend. The latter is more generic and covers all backends. While the naming is different, both of these functions perform the same functionality.

In the following commit, we will consolidate this code to only maintain the 'optimize' functions. In preparation, modify the backends so that they exclusively implement the optimize callback, only. All users of the refs subsystem already use the 'optimize' function so there is no changes needed on the callee side.

---
 refs/debug.c            |  8 ++++----
 refs/files-backend.c    | 14 ++------------
 refs/packed-backend.c   |  6 +++---
 refs/reftable-backend.c | 13 +++----------
 4 files changed, 12 insertions(+), 29 deletions(-)
refs/debug.c +3 -3
index 01499b9033..40cd1d9c15 100644
@@ -116,11 +116,11 @@ static int debug_transaction_abort(struct ref_store *refs,
 	return res;
 }
 
-static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)
+static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->pack_refs(drefs->refs, opts);
-	trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
+	int res = drefs->refs->be->optimize(drefs->refs, opts);
+	trace_printf_key(&trace_refs, "optimize: %d\n", res);
 	return res;
 }
 

refs/debug.c struct ref_storage_be refs_be_debug = { · line 433

Quoted 5 lines
refs/debug.c +1 -1
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
 	.transaction_finish = debug_transaction_finish,
 	.transaction_abort = debug_transaction_abort,
 
-	.pack_refs = debug_pack_refs,
+	.optimize = debug_optimize,

question: Was the debug backend not using either of these callbacks? From the commit message, it sounds like all the backends were using the optimize callback.

Quoted 35 lines
refs/debug.c +0 -0
 	.rename_ref = debug_rename_ref,
 	.copy_ref = debug_copy_ref,
 
refs/files-backend.c +2 -11
index ed8a1729d6..92d90fc508 100644
@@ -1444,8 +1444,8 @@ static int should_pack_refs(struct files_ref_store *refs,
 	return 0;
 }
 
-static int files_pack_refs(struct ref_store *ref_store,
-			   struct pack_refs_opts *opts)
+static int files_optimize(struct ref_store *ref_store,
+			  struct pack_refs_opts *opts)
 {
 	struct files_ref_store *refs =
 		files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
@@ -1512,15 +1512,6 @@ static int files_pack_refs(struct ref_store *ref_store,
 	return 0;
 }
 
-static int files_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
-{
-	/*
-	 * For the "files" backend, "optimizing" is the same as "packing".
-	 * So, we just call the existing worker function for packing.
-	 */
-	return files_pack_refs(ref_store, opts);
-}
-
 /*
  * People using contrib's git-new-workdir have .git/logs/refs ->
  * /some/other/path/.git/logs/refs, and that may live on another device.

refs/files-backend.c struct ref_storage_be refs_be_files = { · line 3963

Quoted 5 lines
refs/files-backend.c +0 -1
@@ -3969,7 +3960,6 @@ struct ref_storage_be refs_be_files = {
 	.transaction_finish = files_transaction_finish,
 	.transaction_abort = files_transaction_abort,
 
-	.pack_refs = files_pack_refs,
 	.optimize = files_optimize,

Ok, we are removing the "pack_refs" callback and its implementations from all the backends in favor of using the just using the more generic "optimize" callback. Make sense.

It does look like we still have refs_pack_refs() which references the "optimize" callback. It looks like there are no users, but should we also remove it as part of this patch?

-Justin

#3 lore raw

Re: [PATCH 1/4] refs: move to using the '.optimize' functions

Quoted 24 lines · Justin Tobler
Quoted 23 lines · Karthik Nayak
refs/debug.c +3 -3
index 01499b9033..40cd1d9c15 100644
@@ -116,11 +116,11 @@ static int debug_transaction_abort(struct ref_store *refs,
 	return res;
 }
 
-static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)
+static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->pack_refs(drefs->refs, opts);
-	trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
+	int res = drefs->refs->be->optimize(drefs->refs, opts);
+	trace_printf_key(&trace_refs, "optimize: %d\n", res);
 	return res;
 }
 
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
 	.transaction_finish = debug_transaction_finish,
 	.transaction_abort = debug_transaction_abort,
 
Quoted 6 lines

refs/debug.c struct ref_storage_be refs_be_debug = { · line 433

Quoted 2 lines
refs/debug.c +1 -1
-	.pack_refs = debug_pack_refs,
+	.optimize = debug_optimize,

question: Was the debug backend not using either of these callbacks? From the commit message, it sounds like all the backends were using the optimize callback.

Doesn't look like it. Overall I kind of doubt the value that this backend has. I have never had even a single use case for it, and I have been working with references extensively over the last two or three years by now.

Maybe we should just drop it eventually?

Patrick

#4 lore raw

Re: [PATCH 1/4] refs: move to using the '.optimize' functions

Quoted 32 lines · Patrick Steinhardt
Quoted 24 lines · Justin Tobler
Quoted 23 lines · Karthik Nayak
refs/debug.c +3 -3
index 01499b9033..40cd1d9c15 100644
@@ -116,11 +116,11 @@ static int debug_transaction_abort(struct ref_store *refs,
 	return res;
 }
 
-static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)
+static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->pack_refs(drefs->refs, opts);
-	trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
+	int res = drefs->refs->be->optimize(drefs->refs, opts);
+	trace_printf_key(&trace_refs, "optimize: %d\n", res);
 	return res;
 }
 
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
 	.transaction_finish = debug_transaction_finish,
 	.transaction_abort = debug_transaction_abort,
 
Quoted 6 lines

refs/debug.c struct ref_storage_be refs_be_debug = { · line 433

Quoted 2 lines
refs/debug.c +1 -1
-	.pack_refs = debug_pack_refs,
+	.optimize = debug_optimize,

question: Was the debug backend not using either of these callbacks? From the commit message, it sounds like all the backends were using the optimize callback.

Quoted 8 lines

Doesn't look like it. Overall I kind of doubt the value that this backend has. I have never had even a single use case for it, and I have been working with references extensively over the last two or three years by now.

Maybe we should just drop it eventually?

Patrick

I'd be happy to drop it too as a Git developer. But I can merit in keeping it. It does wrap around all reference subsystem calls and if tracing is enabled it would log these reference calls. This can be used in bug reports.

However, a counter argument would be that these only track the surface level APIs to each backend, but not really the internal details.

Considering this and also the fact that we need to update the backend for every change made, I'm for dropping it too.

Karthik

#5 lore raw

Re: [PATCH 1/4] refs: move to using the '.optimize' functions

Quoted 28 lines · Karthik Nayak
Quoted 13 lines · Patrick Steinhardt
Quoted 11 lines · Justin Tobler

refs/debug.c struct ref_storage_be refs_be_debug = { · line 433

Quoted 6 lines · Karthik Nayak
refs/debug.c +1 -1
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
 	.transaction_finish = debug_transaction_finish,
 	.transaction_abort = debug_transaction_abort,
 
-	.pack_refs = debug_pack_refs,
+	.optimize = debug_optimize,

question: Was the debug backend not using either of these callbacks? From the commit message, it sounds like all the backends were using the optimize callback.

Quoted 8 lines

Doesn't look like it. Overall I kind of doubt the value that this backend has. I have never had even a single use case for it, and I have been working with references extensively over the last two or three years by now.

Maybe we should just drop it eventually?

Patrick

I'd be happy to drop it too as a Git developer. But I can merit in keeping it. It does wrap around all reference subsystem calls and if tracing is enabled it would log these reference calls. This can be used in bug reports.

Quoted 5 lines

However, a counter argument would be that these only track the surface level APIs to each backend, but not really the internal details.

Considering this and also the fact that we need to update the backend for every change made, I'm for dropping it too.

Digging a bit deeper: I think this actually _is_ exposed via our tracing API. So if you run Git with GIT_TRACE_REFS= set it will indeed use that debug backend. TIL.

So with that I'll reverse my stance and say that this probably makes sense to keep.

Patrick

#6 lore raw

Re: [PATCH 1/4] refs: move to using the '.optimize' functions

In reply to Justin Tobler
Quoted 5 lines · Justin Tobler
Quoted 1 line · Karthik Nayak

The struct ref_store variable, exposes two ways to optimize a reftable

s/variable,/variable/

Will fix.

Quoted 23 lines
Quoted 23 lines
refs/debug.c +3 -3
index 01499b9033..40cd1d9c15 100644
@@ -116,11 +116,11 @@ static int debug_transaction_abort(struct ref_store *refs,
 	return res;
 }
 
-static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)
+static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->pack_refs(drefs->refs, opts);
-	trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
+	int res = drefs->refs->be->optimize(drefs->refs, opts);
+	trace_printf_key(&trace_refs, "optimize: %d\n", res);
 	return res;
 }
 
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
 	.transaction_finish = debug_transaction_finish,
 	.transaction_abort = debug_transaction_abort,
 
Quoted 7 lines

refs/debug.c struct ref_storage_be refs_be_debug = { · line 433

Quoted 2 lines
refs/debug.c +1 -1
-	.pack_refs = debug_pack_refs,
+	.optimize = debug_optimize,

question: Was the debug backend not using either of these callbacks? From the commit message, it sounds like all the backends were using the optimize callback.

Since there are no users of refs_pack_refs() which uses the '.pack_refs' field, there are no users of 'debug_pack_refs()'.

But if tracing is enabled then the debug backend would wrap around the other backends and any calls to refs_optimize() would call debug_optimize().

Quoted 45 lines
Quoted 35 lines
refs/debug.c +0 -0
 	.rename_ref = debug_rename_ref,
 	.copy_ref = debug_copy_ref,
 
refs/files-backend.c +2 -11
index ed8a1729d6..92d90fc508 100644
@@ -1444,8 +1444,8 @@ static int should_pack_refs(struct files_ref_store *refs,
 	return 0;
 }
 
-static int files_pack_refs(struct ref_store *ref_store,
-			   struct pack_refs_opts *opts)
+static int files_optimize(struct ref_store *ref_store,
+			  struct pack_refs_opts *opts)
 {
 	struct files_ref_store *refs =
 		files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
@@ -1512,15 +1512,6 @@ static int files_pack_refs(struct ref_store *ref_store,
 	return 0;
 }
 
-static int files_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
-{
-	/*
-	 * For the "files" backend, "optimizing" is the same as "packing".
-	 * So, we just call the existing worker function for packing.
-	 */
-	return files_pack_refs(ref_store, opts);
-}
-
 /*
  * People using contrib's git-new-workdir have .git/logs/refs ->
  * /some/other/path/.git/logs/refs, and that may live on another device.

refs/files-backend.c struct ref_storage_be refs_be_files = { · line 3963

Quoted 5 lines
refs/files-backend.c +0 -1
@@ -3969,7 +3960,6 @@ struct ref_storage_be refs_be_files = {
 	.transaction_finish = files_transaction_finish,
 	.transaction_abort = files_transaction_abort,
 
-	.pack_refs = files_pack_refs,
 	.optimize = files_optimize,

Ok, we are removing the "pack_refs" callback and its implementations from all the backends in favor of using the just using the more generic "optimize" callback. Make sense.

Quoted 5 lines

It does look like we still have refs_pack_refs() which references the "optimize" callback. It looks like there are no users, but should we also remove it as part of this patch?

-Justin

It's done in the next commit, let me just squash that in to make it easier.

#7 lore raw

PATCH 2/4 refs: cleanup code around optimization

In reply to Karthik Nayak

The previous commit, moved all backends to only use/support the 'optimize' function within the ref_store structure. With this, cleanup all references to the 'pack_refs' field of the structure and code around it.

---
 refs.c               | 6 ------
 refs.h               | 8 +-------
 refs/refs-internal.h | 3 ---
 3 files changed, 1 insertion(+), 16 deletions(-)
refs.c +0 -6
index a41a94ae55..b9a4a60646 100644
@@ -2313,12 +2313,6 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
 	refs->gitdir = xstrdup(path);
 }
 
-/* backend functions */
-int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
-{
-	return refs->be->pack_refs(refs, opts);
-}
-
 int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts)
 {
 	return refs->be->optimize(refs, opts);
refs.h +1 -7
index 23437d1220..04e917fec0 100644
@@ -499,7 +499,7 @@ void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
 				const struct string_list *refnames);
 
 /*
- * Flags for controlling behaviour of pack_refs()
+ * Flags for controlling behaviour of refs_optimize()
  * PACK_REFS_PRUNE: Prune loose refs after packing
  * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
  *                 result are decided by the ref backend. Backends may ignore
@@ -514,12 +514,6 @@ struct pack_refs_opts {
 	struct string_list *includes;
 };
 
-/*
- * Write a packed-refs file for the current repository.
- * flags: Combination of the above PACK_REFS_* flags.
- */
-int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts);
-
 /*
  * Optimize the ref store. The exact behavior is up to the backend.
  * For the files backend, this is equivalent to packing refs.
refs/refs-internal.h +0 -3
index 4671517dad..fc5149df5b 100644
@@ -422,8 +422,6 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,
 				      struct ref_transaction *transaction,
 				      struct strbuf *err);
 
-typedef int pack_refs_fn(struct ref_store *ref_store,
-			 struct pack_refs_opts *opts);
 typedef int optimize_fn(struct ref_store *ref_store,
 			struct pack_refs_opts *opts);
 typedef int rename_ref_fn(struct ref_store *ref_store,
@@ -550,7 +548,6 @@ struct ref_storage_be {
 	ref_transaction_finish_fn *transaction_finish;
 	ref_transaction_abort_fn *transaction_abort;
 
-	pack_refs_fn *pack_refs;
 	optimize_fn *optimize;
 	rename_ref_fn *rename_ref;
 	copy_ref_fn *copy_ref;
Signature · 1 line
2.51.0
#8 lore raw

Re: [PATCH 2/4] refs: cleanup code around optimization

Quoted 1 line · Karthik Nayak

The previous commit moved all backends to only use/support the

s/commit,/commit/

Quoted 16 lines

'optimize' function within the ref_store structure. With this, cleanup all references to the 'pack_refs' field of the structure and code around it.

---
 refs.c               | 6 ------
 refs.h               | 8 +-------
 refs/refs-internal.h | 3 ---
 3 files changed, 1 insertion(+), 16 deletions(-)
refs.c +0 -0
index a41a94ae55..b9a4a60646 100644

refs.c void base_ref_store_init(struct ref_store *refs, struct repository *repo, · line 2320

Quoted 8 lines
refs.c +0 -5
@@ -2313,12 +2313,6 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
 	refs->gitdir = xstrdup(path);
 }
 
-/* backend functions */
-int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
-{
-	return refs->be->pack_refs(refs, opts);
-}

Ah ok, so we are removing refs_pack_refs() here in this patch. Maybe we can just squash this change into the previous patch. Probably not a big deal either way.

The other cleanups in this patch look good.

-Justin

#9 lore raw

PATCH 3/4 refs: rename 'pack_refs_opts' to 'refs_optimize_opts'

In reply to Karthik Nayak

The previous commit removed all references to 'pack_refs()' within the refs subsystem. Continue this cleanup by also renaming 'pack_refs_opts' to 'refs_optimize_opts' and the respective flags accordingly. Keeping the naming consistent will make the code easier to maintain.

---
 pack-refs.c             |  8 ++++----
 refs.c                  |  2 +-
 refs.h                  | 16 ++++++++--------
 refs/debug.c            |  2 +-
 refs/files-backend.c    | 10 +++++-----
 refs/packed-backend.c   |  2 +-
 refs/refs-internal.h    |  2 +-
 refs/reftable-backend.c |  4 ++--
 8 files changed, 23 insertions(+), 23 deletions(-)
pack-refs.c +4 -4
index 1a5e07d8b8..d0ffed93c1 100644
@@ -14,10 +14,10 @@ int pack_refs_core(int argc,
 {
 	struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
 	struct string_list included_refs = STRING_LIST_INIT_NODUP;
-	struct pack_refs_opts pack_refs_opts = {
+	struct refs_optimize_opts pack_refs_opts = {
 		.exclusions = &excludes,
 		.includes = &included_refs,
-		.flags = PACK_REFS_PRUNE,
+		.flags = REFS_OPTIMIZE_PRUNE,
 	};
 	struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
 	struct string_list_item *item;
@@ -26,8 +26,8 @@ int pack_refs_core(int argc,
 
 	struct option opts[] = {
 		OPT_BOOL(0, "all",   &pack_all, N_("pack everything")),
-		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
-		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
+		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), REFS_OPTIMIZE_PRUNE),
+		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), REFS_OPTIMIZE_AUTO),
 		OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
 			N_("references to include")),
 		OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
refs.c +1 -1
index b9a4a60646..0d0831f29b 100644
@@ -2313,7 +2313,7 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
 	refs->gitdir = xstrdup(path);
 }
 
-int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts)
+int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts)
 {
 	return refs->be->optimize(refs, opts);
 }
refs.h +8 -8
index 04e917fec0..d2630af97f 100644
@@ -500,15 +500,15 @@ void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
 
 /*
  * Flags for controlling behaviour of refs_optimize()
- * PACK_REFS_PRUNE: Prune loose refs after packing
- * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
- *                 result are decided by the ref backend. Backends may ignore
- *                 this flag and fall back to a normal repack.
+ * REFS_OPTIMIZE_PRUNE: Prune loose refs after packing
+ * REFS_OPTIMIZE_AUTO: Pack refs on a best effort basis. The heuristics and end
+ *                     result are decided by the ref backend. Backends may ignore
+ *                     this flag and fall back to a normal repack.
  */
-#define PACK_REFS_PRUNE (1 << 0)
-#define PACK_REFS_AUTO  (1 << 1)
+#define REFS_OPTIMIZE_PRUNE (1 << 0)
+#define REFS_OPTIMIZE_AUTO  (1 << 1)
 
-struct pack_refs_opts {
+struct refs_optimize_opts {
 	unsigned int flags;
 	struct ref_exclusions *exclusions;
 	struct string_list *includes;
@@ -518,7 +518,7 @@ struct pack_refs_opts {
  * Optimize the ref store. The exact behavior is up to the backend.
  * For the files backend, this is equivalent to packing refs.
  */
-int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts);
+int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts);
 
 /*
  * Setup reflog before using. Fill in err and return -1 on failure.
refs/debug.c +1 -1
index 40cd1d9c15..2defd2d465 100644
@@ -116,7 +116,7 @@ static int debug_transaction_abort(struct ref_store *refs,
 	return res;
 }
 
-static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
+static int debug_optimize(struct ref_store *ref_store, struct refs_optimize_opts *opts)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
 	int res = drefs->refs->be->optimize(drefs->refs, opts);
refs/files-backend.c +5 -5
index 92d90fc508..2f60395cd2 100644
@@ -1355,7 +1355,7 @@ static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_
  */
 static int should_pack_ref(struct files_ref_store *refs,
 			   const struct reference *ref,
-			   struct pack_refs_opts *opts)
+			   struct refs_optimize_opts *opts)
 {
 	struct string_list_item *item;
 
@@ -1383,7 +1383,7 @@ static int should_pack_ref(struct files_ref_store *refs,
 }
 
 static int should_pack_refs(struct files_ref_store *refs,
-			    struct pack_refs_opts *opts)
+			    struct refs_optimize_opts *opts)
 {
 	struct ref_iterator *iter;
 	size_t packed_size;
@@ -1391,7 +1391,7 @@ static int should_pack_refs(struct files_ref_store *refs,
 	size_t limit;
 	int ret;
 
-	if (!(opts->flags & PACK_REFS_AUTO))
+	if (!(opts->flags & REFS_OPTIMIZE_AUTO))
 		return 1;
 
 	ret = packed_refs_size(refs->packed_ref_store, &packed_size);
@@ -1445,7 +1445,7 @@ static int should_pack_refs(struct files_ref_store *refs,
 }
 
 static int files_optimize(struct ref_store *ref_store,
-			  struct pack_refs_opts *opts)
+			  struct refs_optimize_opts *opts)
 {
 	struct files_ref_store *refs =
 		files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
@@ -1488,7 +1488,7 @@ static int files_optimize(struct ref_store *ref_store,
 			    iter->ref.name, err.buf);
 
 		/* Schedule the loose reference for pruning if requested. */
-		if ((opts->flags & PACK_REFS_PRUNE)) {
+		if ((opts->flags & REFS_OPTIMIZE_PRUNE)) {
 			struct ref_to_prune *n;
 			FLEX_ALLOC_STR(n, name, iter->ref.name);
 			oidcpy(&n->oid, iter->ref.oid);
refs/packed-backend.c +1 -1
index 20cf9fab18..0aa0ff6701 100644
@@ -1774,7 +1774,7 @@ static int packed_transaction_finish(struct ref_store *ref_store,
 }
 
 static int packed_optimize(struct ref_store *ref_store UNUSED,
-			   struct pack_refs_opts *pack_opts UNUSED)
+			   struct refs_optimize_opts *pack_opts UNUSED)
 {
 	/*
 	 * Packed refs are already packed. It might be that loose refs
refs/refs-internal.h +1 -1
index fc5149df5b..dee42f231d 100644
@@ -423,7 +423,7 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,
 				      struct strbuf *err);
 
 typedef int optimize_fn(struct ref_store *ref_store,
-			struct pack_refs_opts *opts);
+			struct refs_optimize_opts *opts);
 typedef int rename_ref_fn(struct ref_store *ref_store,
 			  const char *oldref, const char *newref,
 			  const char *logmsg);
refs/reftable-backend.c +2 -2
index 43cc66a48e..c23c45f3bf 100644
@@ -1701,7 +1701,7 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,
 }
 
 static int reftable_be_optimize(struct ref_store *ref_store,
-				struct pack_refs_opts *opts)
+				struct refs_optimize_opts *opts)
 {
 	struct reftable_ref_store *refs =
 		reftable_be_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, "optimize_refs");
@@ -1715,7 +1715,7 @@ static int reftable_be_optimize(struct ref_store *ref_store,
 	if (!stack)
 		stack = refs->main_backend.stack;
 
-	if (opts->flags & PACK_REFS_AUTO)
+	if (opts->flags & REFS_OPTIMIZE_AUTO)
 		ret = reftable_stack_auto_compact(stack);
 	else
 		ret = reftable_stack_compact_all(stack, NULL);
Signature · 1 line
2.51.0
#10 lore raw

Re: [PATCH 3/4] refs: rename 'pack_refs_opts' to 'refs_optimize_opts'

Quoted 23 lines · Karthik Nayak

The previous commit removed all references to 'pack_refs()' within the refs subsystem. Continue this cleanup by also renaming 'pack_refs_opts' to 'refs_optimize_opts' and the respective flags accordingly. Keeping the naming consistent will make the code easier to maintain.

---
 pack-refs.c             |  8 ++++----
 refs.c                  |  2 +-
 refs.h                  | 16 ++++++++--------
 refs/debug.c            |  2 +-
 refs/files-backend.c    | 10 +++++-----
 refs/packed-backend.c   |  2 +-
 refs/refs-internal.h    |  2 +-
 refs/reftable-backend.c |  4 ++--
 8 files changed, 23 insertions(+), 23 deletions(-)
pack-refs.c +0 -0
index 1a5e07d8b8..d0ffed93c1 100644

pack-refs.c int pack_refs_core(int argc, · line 17

Quoted 5 lines
pack-refs.c +1 -1
@@ -14,10 +14,10 @@ int pack_refs_core(int argc,
 {
 	struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
 	struct string_list included_refs = STRING_LIST_INIT_NODUP;
-	struct pack_refs_opts pack_refs_opts = {
+	struct refs_optimize_opts pack_refs_opts = {

We could rename the variable name here to, but probably not a big deal either way.

Quoted 59 lines
pack-refs.c +3 -3
 		.exclusions = &excludes,
 		.includes = &included_refs,
-		.flags = PACK_REFS_PRUNE,
+		.flags = REFS_OPTIMIZE_PRUNE,
 	};
 	struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
 	struct string_list_item *item;
@@ -26,8 +26,8 @@ int pack_refs_core(int argc,
 
 	struct option opts[] = {
 		OPT_BOOL(0, "all",   &pack_all, N_("pack everything")),
-		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
-		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
+		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), REFS_OPTIMIZE_PRUNE),
+		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), REFS_OPTIMIZE_AUTO),
 		OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
 			N_("references to include")),
 		OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
refs.c +1 -1
index b9a4a60646..0d0831f29b 100644
@@ -2313,7 +2313,7 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
 	refs->gitdir = xstrdup(path);
 }
 
-int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts)
+int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts)
 {
 	return refs->be->optimize(refs, opts);
 }
refs.h +7 -7
index 04e917fec0..d2630af97f 100644
@@ -500,15 +500,15 @@ void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
 
 /*
  * Flags for controlling behaviour of refs_optimize()
- * PACK_REFS_PRUNE: Prune loose refs after packing
- * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
- *                 result are decided by the ref backend. Backends may ignore
- *                 this flag and fall back to a normal repack.
+ * REFS_OPTIMIZE_PRUNE: Prune loose refs after packing
+ * REFS_OPTIMIZE_AUTO: Pack refs on a best effort basis. The heuristics and end
+ *                     result are decided by the ref backend. Backends may ignore
+ *                     this flag and fall back to a normal repack.
  */
-#define PACK_REFS_PRUNE (1 << 0)
-#define PACK_REFS_AUTO  (1 << 1)
+#define REFS_OPTIMIZE_PRUNE (1 << 0)
+#define REFS_OPTIMIZE_AUTO  (1 << 1)
 
-struct pack_refs_opts {
+struct refs_optimize_opts {
 	unsigned int flags;
 	struct ref_exclusions *exclusions;
 	struct string_list *includes;

refs.h struct pack_refs_opts { · line 521

Quoted 5 lines
refs.h +1 -1
@@ -518,7 +518,7 @@ struct pack_refs_opts {
  * Optimize the ref store. The exact behavior is up to the backend.
  * For the files backend, this is equivalent to packing refs.
  */
-int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts);
+int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts);

I noticed when poking around the code that the optimize callback was still using the pack_refs_opts. Nice to see this clean up. :)

Quoted 2 lines
refs.h +0 -0
 /*
  * Setup reflog before using. Fill in err and return -1 on failure.

[snip]

refs/packed-backend.c static int packed_transaction_finish(struct ref_store *ref_store, · line 1777

Quoted 10 lines
refs/packed-backend.c +1 -1
index 20cf9fab18..0aa0ff6701 100644
@@ -1774,7 +1774,7 @@ static int packed_transaction_finish(struct ref_store *ref_store,
 }
 
 static int packed_optimize(struct ref_store *ref_store UNUSED,
-			   struct pack_refs_opts *pack_opts UNUSED)
+			   struct refs_optimize_opts *pack_opts UNUSED)

Also not a big deal, but we could rename pack_opts here to just opts. It's not even used anyways.

All the other trivial renames in this patch look good.

-Justin

#11 lore raw

Re: [PATCH 3/4] refs: rename 'pack_refs_opts' to 'refs_optimize_opts'

Quoted 19 lines · Justin Tobler
Quoted 18 lines · Karthik Nayak

The previous commit removed all references to 'pack_refs()' within the refs subsystem. Continue this cleanup by also renaming 'pack_refs_opts' to 'refs_optimize_opts' and the respective flags accordingly. Keeping the naming consistent will make the code easier to maintain.

---
 pack-refs.c             |  8 ++++----
 refs.c                  |  2 +-
 refs.h                  | 16 ++++++++--------
 refs/debug.c            |  2 +-
 refs/files-backend.c    | 10 +++++-----
 refs/packed-backend.c   |  2 +-
 refs/refs-internal.h    |  2 +-
 refs/reftable-backend.c |  4 ++--
 8 files changed, 23 insertions(+), 23 deletions(-)
Quoted 14 lines

pack-refs.c int pack_refs_core(int argc, · line 17

Quoted 10 lines
pack-refs.c +1 -1
index 1a5e07d8b8..d0ffed93c1 100644
@@ -14,10 +14,10 @@ int pack_refs_core(int argc,
 {
 	struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
 	struct string_list included_refs = STRING_LIST_INIT_NODUP;
-	struct pack_refs_opts pack_refs_opts = {
+	struct refs_optimize_opts pack_refs_opts = {

We could rename the variable name here to, but probably not a big deal either way.

Since I'm re-rolling, let me do that :)

Quoted 85 lines
Quoted 59 lines
pack-refs.c +3 -3
 		.exclusions = &excludes,
 		.includes = &included_refs,
-		.flags = PACK_REFS_PRUNE,
+		.flags = REFS_OPTIMIZE_PRUNE,
 	};
 	struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
 	struct string_list_item *item;
@@ -26,8 +26,8 @@ int pack_refs_core(int argc,
 
 	struct option opts[] = {
 		OPT_BOOL(0, "all",   &pack_all, N_("pack everything")),
-		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
-		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
+		OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), REFS_OPTIMIZE_PRUNE),
+		OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), REFS_OPTIMIZE_AUTO),
 		OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
 			N_("references to include")),
 		OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
refs.c +1 -1
index b9a4a60646..0d0831f29b 100644
@@ -2313,7 +2313,7 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
 	refs->gitdir = xstrdup(path);
 }
 
-int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts)
+int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts)
 {
 	return refs->be->optimize(refs, opts);
 }
refs.h +7 -7
index 04e917fec0..d2630af97f 100644
@@ -500,15 +500,15 @@ void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
 
 /*
  * Flags for controlling behaviour of refs_optimize()
- * PACK_REFS_PRUNE: Prune loose refs after packing
- * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
- *                 result are decided by the ref backend. Backends may ignore
- *                 this flag and fall back to a normal repack.
+ * REFS_OPTIMIZE_PRUNE: Prune loose refs after packing
+ * REFS_OPTIMIZE_AUTO: Pack refs on a best effort basis. The heuristics and end
+ *                     result are decided by the ref backend. Backends may ignore
+ *                     this flag and fall back to a normal repack.
  */
-#define PACK_REFS_PRUNE (1 << 0)
-#define PACK_REFS_AUTO  (1 << 1)
+#define REFS_OPTIMIZE_PRUNE (1 << 0)
+#define REFS_OPTIMIZE_AUTO  (1 << 1)
 
-struct pack_refs_opts {
+struct refs_optimize_opts {
 	unsigned int flags;
 	struct ref_exclusions *exclusions;
 	struct string_list *includes;

refs.h struct pack_refs_opts { · line 521

Quoted 5 lines
refs.h +1 -1
@@ -518,7 +518,7 @@ struct pack_refs_opts {
  * Optimize the ref store. The exact behavior is up to the backend.
  * For the files backend, this is equivalent to packing refs.
  */
-int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts);
+int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts);

I noticed when poking around the code that the optimize callback was still using the pack_refs_opts. Nice to see this clean up. :)

Quoted 2 lines
refs.h +0 -0
 /*
  * Setup reflog before using. Fill in err and return -1 on failure.

[snip]

refs/packed-backend.c static int packed_transaction_finish(struct ref_store *ref_store, · line 1777

Quoted 10 lines
refs/packed-backend.c +1 -1
index 20cf9fab18..0aa0ff6701 100644
@@ -1774,7 +1774,7 @@ static int packed_transaction_finish(struct ref_store *ref_store,
 }
 
 static int packed_optimize(struct ref_store *ref_store UNUSED,
-			   struct pack_refs_opts *pack_opts UNUSED)
+			   struct refs_optimize_opts *pack_opts UNUSED)

Also not a big deal, but we could rename pack_opts here to just opts. It's not even used anyways.

Quoted 4 lines

All the other trivial renames in this patch look good.

-Justin

Will do,

Thanks for the review.

#12 lore raw

PATCH 4/4 t/pack-refs-tests: move the 'test_done' to callees

In reply to Karthik Nayak

In ac0bad0af4 (t0601: refactor tests to be shareable, 2025-09-19), we refactored 't/t0601-reffiles-pack-refs.sh' to move all of the tests to 't/pack-refs-tests.sh', which became a common test suite which was also used by 't/t1463-refs-optimize.sh'.

This also moved the 'test_done' directive to 't/pack-refs-tests.sh'. Which inhibits additional tests from being added to either of the tests. Let's move the directive out to both the tests, so that we can add additional specific tests to them. Also the test flow logic shouldn't be part of tests which can be embedded in other test scripts.

---
 t/pack-refs-tests.sh          | 2 --
 t/t0601-reffiles-pack-refs.sh | 2 ++
 t/t1463-refs-optimize.sh      | 2 ++
 3 files changed, 4 insertions(+), 2 deletions(-)
t/pack-refs-tests.sh +0 -2
index 095823d915..81086c3690 100644
@@ -459,5 +459,3 @@ test_expect_success 'pack-refs does not store invalid peeled tag value' '
 		test_grep ! "^\^" .git/packed-refs
 	)
 '
-
-test_done
t/t0601-reffiles-pack-refs.sh +2 -0
index 12cf5d1dcb..3c706978ef 100755
@@ -18,3 +18,5 @@ export GIT_TEST_DEFAULT_REF_FORMAT
 . ./test-lib.sh
 
 . "$TEST_DIRECTORY"/pack-refs-tests.sh
+
+test_done
t/t1463-refs-optimize.sh +2 -0
index c11c905d79..9afe3c1ed7 100755
@@ -15,3 +15,5 @@ export GIT_TEST_DEFAULT_REF_FORMAT
 
 pack_refs='refs optimize'
 . "$TEST_DIRECTORY"/pack-refs-tests.sh
+
+test_done
Signature · 1 line
2.51.0
#13 lore raw
In reply to Karthik Nayak
Quoted 9 lines · Karthik Nayak

This is extracted from a recent series I sent [1], which I've since dropped to follow up with a different approach. I think these patches hold value individually.

They mostly cleanup code around 'git refs optimize' which was added recently in db0babf9b2 (Merge branch 'ms/refs-optimize', 2025-10-02). The code in the refs subsystem contains both 'pack-refs' and 'optimize' functions, which are one and the same.

Quoted 5 lines

This series unifies this to only retain the 'optimize' functions and naming, since it backend generic.

This is based on top of master 143f58ef75 (Sync with Git 2.51.1, 2025-10-15) with 'ps/ref-peeled-tags' merged in.

Thanks, these patches all look good to me, except for the small nits that Justin has spotted. I guess this can use one more tiny reroll before it's ready to be merged.

Patrick