When initializing the reftable stack the caller may optionally pass some
write options. These write options mix up two different concerns though:
- Of course, they allow the caller to configure how new reftables are
being written.
- But they also allow the caller to configure the stack itself, like
its hash ID and the `on_reload` callback.
This is somewhat awkward, as it doesn't easily give the caller the
flexibility to for example write multiple reftables with different
options. Furthermore, this requires us to eagerly parse relevant
configuration when initializing the reftable backend.
Refactor the code by splitting out those options that configure the
stack itself. Creating a new stack will thus only require this limited
set of options, whereas the caller is expected to pass write options to
all functions that end up writing tables.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committedJun 25, 2026 at 11:20 UTC44f46f2be5b6fb47733e495be3de21eeec4c7ede
14 files changed+258-198
refs/reftable-backend.c
+18-11
index 5115a3f4ce..608d71cf10 100644--- a/refs/reftable-backend.c+++ b/refs/reftable-backend.c@@ -48,9 +48,9 @@ static void reftable_backend_on_reload(void *payload) static int reftable_backend_init(struct reftable_backend *be, const char *path,- const struct reftable_write_options *_opts)+ const struct reftable_stack_options *_opts) {- struct reftable_write_options opts = *_opts;+ struct reftable_stack_options opts = *_opts; opts.on_reload = reftable_backend_on_reload; opts.on_reload_payload = be; return reftable_new_stack(&be->stack, path, &opts);@@ -140,6 +140,7 @@ struct reftable_ref_store { * is populated lazily when we try to resolve `worktrees/$worktree` refs. */ struct strmap worktree_backends;+ struct reftable_stack_options stack_options; struct reftable_write_options write_options; unsigned int store_flags;@@ -190,7 +191,7 @@ static int backend_for_worktree(struct reftable_backend **out, CALLOC_ARRAY(*out, 1); store->err = ret = reftable_backend_init(*out, worktree_dir.buf,- &store->write_options);+ &store->stack_options); if (ret < 0) { free(*out); goto out;@@ -404,10 +405,10 @@ static struct ref_store *reftable_be_init(struct repository *repo, switch (repo->hash_algo->format_id) { case GIT_SHA1_FORMAT_ID:- refs->write_options.hash_id = REFTABLE_HASH_SHA1;+ refs->stack_options.hash_id = REFTABLE_HASH_SHA1; break; case GIT_SHA256_FORMAT_ID:- refs->write_options.hash_id = REFTABLE_HASH_SHA256;+ refs->stack_options.hash_id = REFTABLE_HASH_SHA256; break; default: BUG("unknown hash algorithm %d", repo->hash_algo->format_id);@@ -441,7 +442,7 @@ static struct ref_store *reftable_be_init(struct repository *repo, } strbuf_addstr(&path, "/reftable"); refs->err = reftable_backend_init(&refs->main_backend, path.buf,- &refs->write_options);+ &refs->stack_options); if (refs->err) goto done;@@ -457,7 +458,7 @@ static struct ref_store *reftable_be_init(struct repository *repo, strbuf_addstr(&refdir, "/reftable"); refs->err = reftable_backend_init(&refs->worktree_backend, refdir.buf,- &refs->write_options);+ &refs->stack_options); if (refs->err) goto done; }@@ -997,6 +998,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out, struct reftable_addition *addition; ret = reftable_stack_new_addition(&addition, be->stack,+ &refs->write_options, REFTABLE_STACK_NEW_ADDITION_RELOAD); if (ret) { if (ret == REFTABLE_LOCK_ERROR)@@ -1685,9 +1687,9 @@ static int reftable_be_optimize(struct ref_store *ref_store, stack = refs->main_backend.stack; if (opts->flags & REFS_OPTIMIZE_AUTO)- ret = reftable_stack_auto_compact(stack);+ ret = reftable_stack_auto_compact(stack, &refs->write_options); else- ret = reftable_stack_compact_all(stack, NULL);+ ret = reftable_stack_compact_all(stack, &refs->write_options, NULL); if (ret < 0) { ret = error(_("unable to compact stack: %s"), reftable_error_str(ret));@@ -1721,8 +1723,8 @@ static int reftable_be_optimize_required(struct ref_store *ref_store, if (opts->flags & REFS_OPTIMIZE_AUTO) use_heuristics = true;- return reftable_stack_compaction_required(stack, use_heuristics,- required);+ return reftable_stack_compaction_required(stack, &refs->write_options,+ use_heuristics, required); } struct write_create_symref_arg {@@ -1979,6 +1981,7 @@ static int reftable_be_rename_ref(struct ref_store *ref_store, if (ret) goto done; ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,+ &refs->write_options, REFTABLE_STACK_NEW_ADDITION_RELOAD); done:@@ -2009,6 +2012,7 @@ static int reftable_be_copy_ref(struct ref_store *ref_store, if (ret) goto done; ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,+ &refs->write_options, REFTABLE_STACK_NEW_ADDITION_RELOAD); done:@@ -2374,6 +2378,7 @@ static int reftable_be_create_reflog(struct ref_store *ref_store, arg.stack = be->stack; ret = reftable_stack_add(be->stack, &write_reflog_existence_table, &arg,+ &refs->write_options, REFTABLE_STACK_NEW_ADDITION_RELOAD); done:@@ -2446,6 +2451,7 @@ static int reftable_be_delete_reflog(struct ref_store *ref_store, arg.stack = be->stack; ret = reftable_stack_add(be->stack, &write_reflog_delete_table, &arg,+ &refs->write_options, REFTABLE_STACK_NEW_ADDITION_RELOAD); assert(ret != REFTABLE_API_ERROR);@@ -2568,6 +2574,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, goto done; ret = reftable_stack_new_addition(&add, be->stack,+ &refs->write_options, REFTABLE_STACK_NEW_ADDITION_RELOAD); if (ret < 0) goto done;
reftable/reftable-stack.h
+27-3
index 5f7be573fa..11f9963f4f 100644--- a/reftable/reftable-stack.h+++ b/reftable/reftable-stack.h@@ -26,11 +26,29 @@ */ struct reftable_stack;+/* Options related to opening a stack. */+struct reftable_stack_options {+ /*+ * 4-byte identifier ("sha1", "s256") of the hash. Defaults to SHA1 if+ * unset.+ */+ enum reftable_hash hash_id;++ /*+ * Callback function to execute whenever the stack is being reloaded.+ * This can be used e.g. to discard cached information that relies on+ * the old stack's data. The payload data will be passed as argument to+ * the callback.+ */+ void (*on_reload)(void *payload);+ void *on_reload_payload;+};+ /* open a new reftable stack. The tables along with the table list will be * stored in 'dir'. Typically, this should be .git/reftables. */ int reftable_new_stack(struct reftable_stack **dest, const char *dir,- const struct reftable_write_options *opts);+ const struct reftable_stack_options *opts); /* returns the update_index at which a next table should be written. */ uint64_t reftable_stack_next_update_index(struct reftable_stack *st);@@ -52,6 +70,7 @@ enum { */ int reftable_stack_new_addition(struct reftable_addition **dest, struct reftable_stack *st,+ const struct reftable_write_options *opts, unsigned int flags); /* Adds a reftable to transaction. */@@ -77,7 +96,9 @@ void reftable_addition_destroy(struct reftable_addition *add); int reftable_stack_add(struct reftable_stack *st, int (*write_table)(struct reftable_writer *wr, void *write_arg),- void *write_arg, unsigned flags);+ void *write_arg,+ const struct reftable_write_options *opts,+ unsigned flags); struct reftable_iterator;@@ -122,6 +143,7 @@ struct reftable_log_expiry_config { /* compacts all reftables into a giant table. Expire reflog entries if config is * non-NULL */ int reftable_stack_compact_all(struct reftable_stack *st,+ const struct reftable_write_options *opts, struct reftable_log_expiry_config *config); /*@@ -132,11 +154,13 @@ int reftable_stack_compact_all(struct reftable_stack *st, * compacted to maintain geometric progression. */ int reftable_stack_compaction_required(struct reftable_stack *st,+ const struct reftable_write_options *opts, bool use_heuristics, bool *required); /* heuristically compact unbalanced table stack. */-int reftable_stack_auto_compact(struct reftable_stack *st);+int reftable_stack_auto_compact(struct reftable_stack *st,+ const struct reftable_write_options *opts); /* delete stale .ref tables. */ int reftable_stack_clean(struct reftable_stack *st);
reftable/reftable-writer.h
+2-15
index a66db415c8..6ff4ddfc60 100644--- a/reftable/reftable-writer.h+++ b/reftable/reftable-writer.h@@ -28,11 +28,6 @@ struct reftable_write_options { /* how often to write complete keys in each block. */ uint16_t restart_interval;- /* 4-byte identifier ("sha1", "s256") of the hash.- * Defaults to SHA1 if unset- */- enum reftable_hash hash_id;- /* Default mode for creating files. If unset, use 0666 (+umask) */ unsigned int default_permissions;@@ -60,15 +55,6 @@ struct reftable_write_options { * negative value will cause us to block indefinitely. */ long lock_timeout_ms;-- /*- * Callback function to execute whenever the stack is being reloaded.- * This can be used e.g. to discard cached information that relies on- * the old stack's data. The payload data will be passed as argument to- * the callback.- */- void (*on_reload)(void *payload);- void *on_reload_payload; }; /* reftable_block_stats holds statistics for a single block type */@@ -114,7 +100,8 @@ struct reftable_writer; int reftable_writer_new(struct reftable_writer **out, ssize_t (*writer_func)(void *, const void *, size_t), int (*flush_func)(void *),- void *writer_arg, const struct reftable_write_options *opts);+ void *writer_arg, enum reftable_hash hash_id,+ const struct reftable_write_options *opts); /* * Set the range of update indices for the records we will add. When writing a
reftable/stack.c
+66-34
index 1fba96ddb3..ab12926708 100644--- a/reftable/stack.c+++ b/reftable/stack.c@@ -501,10 +501,10 @@ out: } int reftable_new_stack(struct reftable_stack **dest, const char *dir,- const struct reftable_write_options *_opts)+ const struct reftable_stack_options *_opts) { struct reftable_buf list_file_name = REFTABLE_BUF_INIT;- struct reftable_write_options opts = { 0 };+ struct reftable_stack_options opts = { 0 }; struct reftable_stack *p; int err;@@ -629,6 +629,7 @@ int reftable_stack_reload(struct reftable_stack *st) struct reftable_addition { struct reftable_flock tables_list_lock; struct reftable_stack *stack;+ struct reftable_write_options opts; char **new_tables; size_t new_tables_len, new_tables_cap;@@ -657,6 +658,7 @@ static void reftable_addition_close(struct reftable_addition *add) static int reftable_stack_init_addition(struct reftable_addition *add, struct reftable_stack *st,+ const struct reftable_write_options *opts, unsigned int flags) { struct reftable_buf lock_file_name = REFTABLE_BUF_INIT;@@ -664,15 +666,17 @@ static int reftable_stack_init_addition(struct reftable_addition *add, memset(add, 0, sizeof(*add)); add->stack = st;+ if (opts)+ add->opts = *opts; err = flock_acquire(&add->tables_list_lock, st->list_file,- st->opts.lock_timeout_ms);+ add->opts.lock_timeout_ms); if (err < 0) goto done;- if (st->opts.default_permissions) {+ if (add->opts.default_permissions) { if (chmod(add->tables_list_lock.path,- st->opts.default_permissions) < 0) {+ add->opts.default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; }@@ -702,12 +706,14 @@ done: static int stack_try_add(struct reftable_stack *st, int (*write_table)(struct reftable_writer *wr, void *arg),- void *arg, unsigned flags)+ void *arg,+ const struct reftable_write_options *opts,+ unsigned flags) { struct reftable_addition add; int err;- err = reftable_stack_init_addition(&add, st, flags);+ err = reftable_stack_init_addition(&add, st, opts, flags); if (err < 0) goto done;@@ -723,9 +729,11 @@ done: int reftable_stack_add(struct reftable_stack *st, int (*write)(struct reftable_writer *wr, void *arg),- void *arg, unsigned flags)+ void *arg,+ const struct reftable_write_options *opts,+ unsigned flags) {- int err = stack_try_add(st, write, arg, flags);+ int err = stack_try_add(st, write, arg, opts, flags); if (err < 0) { if (err == REFTABLE_OUTDATED_ERROR) { /* Ignore error return, we want to propagate@@ -810,7 +818,7 @@ int reftable_addition_commit(struct reftable_addition *add) if (err) goto done;- if (!add->stack->opts.disable_auto_compact) {+ if (!add->opts.disable_auto_compact) { /* * Auto-compact the stack to keep the number of tables in * control. It is possible that a concurrent writer is already@@ -820,7 +828,7 @@ int reftable_addition_commit(struct reftable_addition *add) * concurrent writer, which causes `REFTABLE_OUTDATED_ERROR`. * Both of these errors are benign, so we simply ignore them. */- err = reftable_stack_auto_compact(add->stack);+ err = reftable_stack_auto_compact(add->stack, &add->opts); if (err < 0 && err != REFTABLE_LOCK_ERROR && err != REFTABLE_OUTDATED_ERROR) goto done;@@ -834,6 +842,7 @@ done: int reftable_stack_new_addition(struct reftable_addition **dest, struct reftable_stack *st,+ const struct reftable_write_options *opts, unsigned int flags) { int err;@@ -842,7 +851,7 @@ int reftable_stack_new_addition(struct reftable_addition **dest, if (!*dest) return REFTABLE_OUT_OF_MEMORY_ERROR;- err = reftable_stack_init_addition(*dest, st, flags);+ err = reftable_stack_init_addition(*dest, st, opts, flags); if (err) { reftable_free(*dest); *dest = NULL;@@ -862,7 +871,7 @@ int reftable_addition_add(struct reftable_addition *add, struct reftable_writer *wr = NULL; struct reftable_tmpfile tab_file = REFTABLE_TMPFILE_INIT; struct fd_writer writer = {- .opts = &add->stack->opts,+ .opts = &add->opts, }; int err = 0;@@ -883,9 +892,9 @@ int reftable_addition_add(struct reftable_addition *add, err = tmpfile_from_pattern(&tab_file, temp_tab_file_name.buf); if (err < 0) goto done;- if (add->stack->opts.default_permissions) {+ if (add->opts.default_permissions) { if (chmod(tab_file.path,- add->stack->opts.default_permissions)) {+ add->opts.default_permissions)) { err = REFTABLE_IO_ERROR; goto done; }@@ -893,7 +902,7 @@ int reftable_addition_add(struct reftable_addition *add, writer.fd = tab_file.fd; err = reftable_writer_new(&wr, fd_writer_write, fd_writer_flush,- &writer, &add->stack->opts);+ &writer, add->stack->opts.hash_id, &add->opts); if (err < 0) goto done;@@ -1066,13 +1075,14 @@ done: static int stack_compact_locked(struct reftable_stack *st, size_t first, size_t last, struct reftable_log_expiry_config *config,+ const struct reftable_write_options *opts, struct reftable_tmpfile *tab_file_out) { struct reftable_buf next_name = REFTABLE_BUF_INIT; struct reftable_buf tab_file_path = REFTABLE_BUF_INIT; struct reftable_writer *wr = NULL; struct fd_writer writer= {- .opts = &st->opts,+ .opts = opts, }; struct reftable_tmpfile tab_file = REFTABLE_TMPFILE_INIT; int err = 0;@@ -1094,15 +1104,15 @@ static int stack_compact_locked(struct reftable_stack *st, if (err < 0) goto done;- if (st->opts.default_permissions &&- chmod(tab_file.path, st->opts.default_permissions) < 0) {+ if (opts->default_permissions &&+ chmod(tab_file.path, opts->default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; } writer.fd = tab_file.fd; err = reftable_writer_new(&wr, fd_writer_write, fd_writer_flush,- &writer, &st->opts);+ &writer, st->opts.hash_id, opts); if (err < 0) goto done;@@ -1150,6 +1160,7 @@ enum stack_compact_range_flags { static int stack_compact_range(struct reftable_stack *st, size_t first, size_t last, struct reftable_log_expiry_config *expiry,+ const struct reftable_write_options *opts, unsigned int flags) { struct reftable_buf tables_list_buf = REFTABLE_BUF_INIT;@@ -1175,7 +1186,7 @@ static int stack_compact_range(struct reftable_stack *st, * Hold the lock so that we can read "tables.list" and lock all tables * which are part of the user-specified range. */- err = flock_acquire(&tables_list_lock, st->list_file, st->opts.lock_timeout_ms);+ err = flock_acquire(&tables_list_lock, st->list_file, opts->lock_timeout_ms); if (err < 0) goto done;@@ -1274,7 +1285,7 @@ static int stack_compact_range(struct reftable_stack *st, * these tables may end up with an empty new table in case tombstones * end up cancelling out all refs in that range. */- err = stack_compact_locked(st, first, last, expiry, &new_table);+ err = stack_compact_locked(st, first, last, expiry, opts, &new_table); if (err < 0) { if (err != REFTABLE_EMPTY_TABLE_ERROR) goto done;@@ -1286,13 +1297,13 @@ static int stack_compact_range(struct reftable_stack *st, * "tables.list". We'll then replace the compacted range of tables with * the new table. */- err = flock_acquire(&tables_list_lock, st->list_file, st->opts.lock_timeout_ms);+ err = flock_acquire(&tables_list_lock, st->list_file, opts->lock_timeout_ms); if (err < 0) goto done;- if (st->opts.default_permissions) {+ if (opts->default_permissions) { if (chmod(tables_list_lock.path,- st->opts.default_permissions) < 0) {+ opts->default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; }@@ -1513,10 +1524,16 @@ done: } int reftable_stack_compact_all(struct reftable_stack *st,+ const struct reftable_write_options *opts, struct reftable_log_expiry_config *config) {+ struct reftable_write_options opts_default = { 0 }; size_t last = st->merged->tables_len ? st->merged->tables_len - 1 : 0;- return stack_compact_range(st, 0, last, config, 0);++ if (!opts)+ opts = &opts_default;++ return stack_compact_range(st, 0, last, config, opts, 0); } static int segment_size(struct segment *s)@@ -1601,6 +1618,7 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n, } static int stack_segments_for_compaction(struct reftable_stack *st,+ const struct reftable_write_options *opts, struct segment *seg) { int version = (st->opts.hash_id == REFTABLE_HASH_SHA1) ? 1 : 2;@@ -1615,13 +1633,14 @@ static int stack_segments_for_compaction(struct reftable_stack *st, sizes[i] = st->tables[i]->size - overhead; *seg = suggest_compaction_segment(sizes, st->merged->tables_len,- st->opts.auto_compaction_factor);+ opts->auto_compaction_factor); reftable_free(sizes); return 0; } static int update_segment_if_compaction_required(struct reftable_stack *st,+ const struct reftable_write_options *opts, struct segment *seg, bool use_geometric, bool *required)@@ -1638,7 +1657,7 @@ static int update_segment_if_compaction_required(struct reftable_stack *st, return 0; }- err = stack_segments_for_compaction(st, seg);+ err = stack_segments_for_compaction(st, opts, seg); if (err) return err;@@ -1647,27 +1666,40 @@ static int update_segment_if_compaction_required(struct reftable_stack *st, } int reftable_stack_compaction_required(struct reftable_stack *st,+ const struct reftable_write_options *opts, bool use_heuristics, bool *required) {+ struct reftable_write_options opts_default = { 0 }; struct segment seg;- return update_segment_if_compaction_required(st, &seg, use_heuristics,- required);++ if (!opts)+ opts = &opts_default;++ return update_segment_if_compaction_required(st, opts, &seg,+ use_heuristics, required); }-int reftable_stack_auto_compact(struct reftable_stack *st)+int reftable_stack_auto_compact(struct reftable_stack *st,+ const struct reftable_write_options *opts) {+ struct reftable_write_options opts_default = { 0 }; struct segment seg; bool required; int err;- err = update_segment_if_compaction_required(st, &seg, true, &required);+ if (!opts)+ opts = &opts_default;++ err = update_segment_if_compaction_required(st, opts, &seg, true,+ &required); if (err) return err; if (required) return stack_compact_range(st, seg.start, seg.end - 1,- NULL, STACK_COMPACT_RANGE_BEST_EFFORT);+ NULL, opts,+ STACK_COMPACT_RANGE_BEST_EFFORT); return 0; }@@ -1807,7 +1839,7 @@ static int reftable_stack_clean_locked(struct reftable_stack *st) int reftable_stack_clean(struct reftable_stack *st) { struct reftable_addition *add = NULL;- int err = reftable_stack_new_addition(&add, st, 0);+ int err = reftable_stack_new_addition(&add, st, NULL, 0); if (err < 0) { goto done; }