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. Consolidate this code to only maintain the 'optimize' functions. Do this by modifying 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. Finally, cleanup all references to the 'pack_refs' field of the structure and code around it. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Oct 20, 2025 at 10:18 UTC 9b93ab8a9c61c53b3b9b2b3ba60c3e5d66b8ff56
7 files changed +12 -44
refs.c
-6
@@ -2313,12 +2313,6 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
2313 refs->gitdir = xstrdup(path);
2314 }
2315
2316 -/* backend functions */
2317 -int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
2318 -{
2319 - return refs->be->pack_refs(refs, opts);
2320 -}
2321 -
2316 int refs_optimize(struct ref_store *refs, struct pack_refs_opts *opts)
2317 {
2318 return refs->be->optimize(refs, opts);
refs.h
-6
@@ -514,12 +514,6 @@ struct pack_refs_opts {
514 struct string_list *includes;
515 };
516
517 -/*
518 - * Write a packed-refs file for the current repository.
519 - * flags: Combination of the above PACK_REFS_* flags.
520 - */
521 -int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts);
522 -
517 /*
518 * Optimize the ref store. The exact behavior is up to the backend.
519 * For the files backend, this is equivalent to packing refs.
refs/debug.c
+4 -4
@@ -116,11 +116,11 @@ static int debug_transaction_abort(struct ref_store *refs,
116 return res;
117 }
118
119 -static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *opts)
119 +static int debug_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
120 {
121 struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
122 - int res = drefs->refs->be->pack_refs(drefs->refs, opts);
123 - trace_printf_key(&trace_refs, "pack_refs: %d\n", res);
122 + int res = drefs->refs->be->optimize(drefs->refs, opts);
123 + trace_printf_key(&trace_refs, "optimize: %d\n", res);
124 return res;
125 }
126
@@ -430,7 +430,7 @@ struct ref_storage_be refs_be_debug = {
430 .transaction_finish = debug_transaction_finish,
431 .transaction_abort = debug_transaction_abort,
432
433 - .pack_refs = debug_pack_refs,
433 + .optimize = debug_optimize,
434 .rename_ref = debug_rename_ref,
435 .copy_ref = debug_copy_ref,
436
refs/files-backend.c
+2 -12
@@ -1444,8 +1444,8 @@ static int should_pack_refs(struct files_ref_store *refs,
1444 return 0;
1445 }
1446
1447 -static int files_pack_refs(struct ref_store *ref_store,
1448 - struct pack_refs_opts *opts)
1447 +static int files_optimize(struct ref_store *ref_store,
1448 + struct pack_refs_opts *opts)
1449 {
1450 struct files_ref_store *refs =
1451 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
@@ -1512,15 +1512,6 @@ static int files_pack_refs(struct ref_store *ref_store,
1512 return 0;
1513 }
1514
1515 -static int files_optimize(struct ref_store *ref_store, struct pack_refs_opts *opts)
1516 -{
1517 - /*
1518 - * For the "files" backend, "optimizing" is the same as "packing".
1519 - * So, we just call the existing worker function for packing.
1520 - */
1521 - return files_pack_refs(ref_store, opts);
1522 -}
1523 -
1515 /*
1516 * People using contrib's git-new-workdir have .git/logs/refs ->
1517 * /some/other/path/.git/logs/refs, and that may live on another device.
@@ -3975,7 +3966,6 @@ struct ref_storage_be refs_be_files = {
3966 .transaction_finish = files_transaction_finish,
3967 .transaction_abort = files_transaction_abort,
3968
3978 - .pack_refs = files_pack_refs,
3969 .optimize = files_optimize,
3970 .rename_ref = files_rename_ref,
3971 .copy_ref = files_copy_ref,
refs/packed-backend.c
+3 -3
@@ -1773,8 +1773,8 @@ cleanup:
1773 return ret;
1774 }
1775
1776 -static int packed_pack_refs(struct ref_store *ref_store UNUSED,
1777 - struct pack_refs_opts *pack_opts UNUSED)
1776 +static int packed_optimize(struct ref_store *ref_store UNUSED,
1777 + struct pack_refs_opts *pack_opts UNUSED)
1778 {
1779 /*
1780 * Packed refs are already packed. It might be that loose refs
@@ -2129,7 +2129,7 @@ struct ref_storage_be refs_be_packed = {
2129 .transaction_finish = packed_transaction_finish,
2130 .transaction_abort = packed_transaction_abort,
2131
2132 - .pack_refs = packed_pack_refs,
2132 + .optimize = packed_optimize,
2133 .rename_ref = NULL,
2134 .copy_ref = NULL,
2135
refs/refs-internal.h
-3
@@ -422,8 +422,6 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,
422 struct ref_transaction *transaction,
423 struct strbuf *err);
424
425 -typedef int pack_refs_fn(struct ref_store *ref_store,
426 - struct pack_refs_opts *opts);
425 typedef int optimize_fn(struct ref_store *ref_store,
426 struct pack_refs_opts *opts);
427 typedef int rename_ref_fn(struct ref_store *ref_store,
@@ -550,7 +548,6 @@ struct ref_storage_be {
548 ref_transaction_finish_fn *transaction_finish;
549 ref_transaction_abort_fn *transaction_abort;
550
553 - pack_refs_fn *pack_refs;
551 optimize_fn *optimize;
552 rename_ref_fn *rename_ref;
553 copy_ref_fn *copy_ref;
refs/reftable-backend.c
+3 -10
@@ -1700,11 +1700,11 @@ done:
1700 return ret;
1701 }
1702
1703 -static int reftable_be_pack_refs(struct ref_store *ref_store,
1704 - struct pack_refs_opts *opts)
1703 +static int reftable_be_optimize(struct ref_store *ref_store,
1704 + struct pack_refs_opts *opts)
1705 {
1706 struct reftable_ref_store *refs =
1707 - reftable_be_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, "pack_refs");
1707 + reftable_be_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, "optimize_refs");
1708 struct reftable_stack *stack;
1709 int ret;
1710
@@ -1733,12 +1733,6 @@ out:
1733 return ret;
1734 }
1735
1736 -static int reftable_be_optimize(struct ref_store *ref_store,
1737 - struct pack_refs_opts *opts)
1738 -{
1739 - return reftable_be_pack_refs(ref_store, opts);
1740 -}
1741 -
1736 struct write_create_symref_arg {
1737 struct reftable_ref_store *refs;
1738 struct reftable_stack *stack;
@@ -2761,7 +2755,6 @@ struct ref_storage_be refs_be_reftable = {
2755 .transaction_finish = reftable_be_transaction_finish,
2756 .transaction_abort = reftable_be_transaction_abort,
2757
2764 - .pack_refs = reftable_be_pack_refs,
2758 .optimize = reftable_be_optimize,
2759 .rename_ref = reftable_be_rename_ref,
2760 .copy_ref = reftable_be_copy_ref,