refs: allow passing flags when setting up a transaction

Allow passing flags when setting up a transaction such that the behaviour of the transaction itself can be altered. This functionality will be used in a subsequent patch. Adapt callers accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 20, 2024 at 08:51 UTC a0efef144686ca2c46caad98df72507ba2606ce5
14 files changed +33 -26
branch.c
+1 -1
@@ -627,7 +627,7 @@ void create_branch(struct repository *r,
627 else
628 msg = xstrfmt("branch: Created from %s", start_name);
629 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
630 - &err);
630 + 0, &err);
631 if (!transaction ||
632 ref_transaction_update(transaction, ref.buf,
633 &oid, forcing ? NULL : null_oid(),
builtin/clone.c
+1 -1
@@ -574,7 +574,7 @@ static void write_remote_refs(const struct ref *local_refs)
574 struct strbuf err = STRBUF_INIT;
575
576 t = ref_store_transaction_begin(get_main_ref_store(the_repository),
577 - &err);
577 + 0, &err);
578 if (!t)
579 die("%s", err.buf);
580
builtin/fast-import.c
+2 -2
@@ -1634,7 +1634,7 @@ static int update_branch(struct branch *b)
1634 }
1635 }
1636 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1637 - &err);
1637 + 0, &err);
1638 if (!transaction ||
1639 ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
1640 NULL, NULL, 0, msg, &err) ||
@@ -1669,7 +1669,7 @@ static void dump_tags(void)
1669 struct ref_transaction *transaction;
1670
1671 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1672 - &err);
1672 + 0, &err);
1673 if (!transaction) {
1674 failure |= error("%s", err.buf);
1675 goto cleanup;
builtin/fetch.c
+2 -2
@@ -669,7 +669,7 @@ static int s_update_ref(const char *action,
669 */
670 if (!transaction) {
671 transaction = our_transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
672 - &err);
672 + 0, &err);
673 if (!transaction) {
674 ret = STORE_REF_ERROR_OTHER;
675 goto out;
@@ -1671,7 +1671,7 @@ static int do_fetch(struct transport *transport,
1671
1672 if (atomic_fetch) {
1673 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1674 - &err);
1674 + 0, &err);
1675 if (!transaction) {
1676 retcode = -1;
1677 goto cleanup;
builtin/receive-pack.c
+2 -2
@@ -1849,7 +1849,7 @@ static void execute_commands_non_atomic(struct command *commands,
1849 continue;
1850
1851 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1852 - &err);
1852 + 0, &err);
1853 if (!transaction) {
1854 rp_error("%s", err.buf);
1855 strbuf_reset(&err);
@@ -1878,7 +1878,7 @@ static void execute_commands_atomic(struct command *commands,
1878 const char *reported_error = "atomic push failure";
1879
1880 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1881 - &err);
1881 + 0, &err);
1882 if (!transaction) {
1883 rp_error("%s", err.buf);
1884 strbuf_reset(&err);
builtin/replace.c
+1 -1
@@ -201,7 +201,7 @@ static int replace_object_oid(const char *object_ref,
201 }
202
203 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
204 - &err);
204 + 0, &err);
205 if (!transaction ||
206 ref_transaction_update(transaction, ref.buf, repl, &prev,
207 NULL, NULL, 0, NULL, &err) ||
builtin/tag.c
+1 -1
@@ -681,7 +681,7 @@ int cmd_tag(int argc,
681 }
682
683 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
684 - &err);
684 + 0, &err);
685 if (!transaction ||
686 ref_transaction_update(transaction, ref.buf, &object, &prev,
687 NULL, NULL,
builtin/update-ref.c
+2 -2
@@ -612,7 +612,7 @@ static void update_refs_stdin(void)
612 int i, j;
613
614 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
615 - &err);
615 + 0, &err);
616 if (!transaction)
617 die("%s", err.buf);
618
@@ -680,7 +680,7 @@ static void update_refs_stdin(void)
680 */
681 state = cmd->state;
682 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
683 - &err);
683 + 0, &err);
684 if (!transaction)
685 die("%s", err.buf);
686
refs.c
+7 -5
@@ -918,7 +918,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
918 struct ref_transaction *transaction;
919 struct strbuf err = STRBUF_INIT;
920
921 - transaction = ref_store_transaction_begin(refs, &err);
921 + transaction = ref_store_transaction_begin(refs, 0, &err);
922 if (!transaction ||
923 ref_transaction_delete(transaction, refname, old_oid,
924 NULL, flags, msg, &err) ||
@@ -1116,6 +1116,7 @@ int read_ref_at(struct ref_store *refs, const char *refname,
1116 }
1117
1118 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
1119 + unsigned int flags,
1120 struct strbuf *err)
1121 {
1122 struct ref_transaction *tr;
@@ -1123,6 +1124,7 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
1124
1125 CALLOC_ARRAY(tr, 1);
1126 tr->ref_store = refs;
1127 + tr->flags = flags;
1128 return tr;
1129 }
1130
@@ -1309,7 +1311,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
1311 struct strbuf err = STRBUF_INIT;
1312 int ret = 0;
1313
1312 - t = ref_store_transaction_begin(refs, &err);
1314 + t = ref_store_transaction_begin(refs, 0, &err);
1315 if (!t ||
1316 ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,
1317 flags, msg, &err) ||
@@ -2120,7 +2122,7 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
2122 struct strbuf err = STRBUF_INIT;
2123 int ret = 0;
2124
2123 - transaction = ref_store_transaction_begin(refs, &err);
2125 + transaction = ref_store_transaction_begin(refs, 0, &err);
2126 if (!transaction ||
2127 ref_transaction_update(transaction, ref, NULL, NULL,
2128 target, NULL, REF_NO_DEREF,
@@ -2527,7 +2529,7 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,
2529 * individual updates can't fail, so we can pack all of the
2530 * updates into a single transaction.
2531 */
2530 - transaction = ref_store_transaction_begin(refs, &err);
2532 + transaction = ref_store_transaction_begin(refs, 0, &err);
2533 if (!transaction) {
2534 ret = error("%s", err.buf);
2535 goto out;
@@ -2833,7 +2835,7 @@ int repo_migrate_ref_storage_format(struct repository *repo,
2835 if (ret < 0)
2836 goto done;
2837
2836 - transaction = ref_store_transaction_begin(new_refs, errbuf);
2838 + transaction = ref_store_transaction_begin(new_refs, 0, errbuf);
2839 if (!transaction)
2840 goto done;
2841
refs.h
+2 -1
@@ -234,7 +234,7 @@ char *repo_default_branch_name(struct repository *r, int quiet);
234 * struct strbuf err = STRBUF_INIT;
235 * int ret = 0;
236 *
237 - * transaction = ref_store_transaction_begin(refs, &err);
237 + * transaction = ref_store_transaction_begin(refs, 0, &err);
238 * if (!transaction ||
239 * ref_transaction_update(...) ||
240 * ref_transaction_create(...) ||
@@ -584,6 +584,7 @@ enum action_on_err {
584 * be freed by calling ref_transaction_free().
585 */
586 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
587 + unsigned int flags,
588 struct strbuf *err);
589
590 /*
refs/files-backend.c
+7 -4
@@ -1252,7 +1252,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
1252 if (check_refname_format(r->name, 0))
1253 return;
1254
1255 - transaction = ref_store_transaction_begin(&refs->base, &err);
1255 + transaction = ref_store_transaction_begin(&refs->base, 0, &err);
1256 if (!transaction)
1257 goto cleanup;
1258 ref_transaction_add_update(
@@ -1396,7 +1396,8 @@ static int files_pack_refs(struct ref_store *ref_store,
1396 if (!should_pack_refs(refs, opts))
1397 return 0;
1398
1399 - transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
1399 + transaction = ref_store_transaction_begin(refs->packed_ref_store,
1400 + 0, &err);
1401 if (!transaction)
1402 return -1;
1403
@@ -2867,7 +2868,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,
2868 */
2869 if (!packed_transaction) {
2870 packed_transaction = ref_store_transaction_begin(
2870 - refs->packed_ref_store, err);
2871 + refs->packed_ref_store,
2872 + transaction->flags, err);
2873 if (!packed_transaction) {
2874 ret = TRANSACTION_GENERIC_ERROR;
2875 goto cleanup;
@@ -3174,7 +3176,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
3176 &affected_refnames))
3177 BUG("initial ref transaction called with existing refs");
3178
3177 - packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);
3179 + packed_transaction = ref_store_transaction_begin(refs->packed_ref_store,
3180 + transaction->flags, err);
3181 if (!packed_transaction) {
3182 ret = TRANSACTION_GENERIC_ERROR;
3183 goto cleanup;
refs/refs-internal.h
+1
@@ -193,6 +193,7 @@ struct ref_transaction {
193 size_t nr;
194 enum ref_transaction_state state;
195 void *backend_data;
196 + unsigned int flags;
197 };
198
199 /*
sequencer.c
+3 -3
@@ -662,7 +662,7 @@ static int fast_forward_to(struct repository *r,
662 strbuf_addf(&sb, "%s: fast-forward", action_name(opts));
663
664 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
665 - &err);
665 + 0, &err);
666 if (!transaction ||
667 ref_transaction_update(transaction, "HEAD",
668 to, unborn && !is_rebase_i(opts) ?
@@ -1297,7 +1297,7 @@ int update_head_with_reflog(const struct commit *old_head,
1297 }
1298
1299 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1300 - err);
1300 + 0, err);
1301 if (!transaction ||
1302 ref_transaction_update(transaction, "HEAD", new_head,
1303 old_head ? &old_head->object.oid : null_oid(),
@@ -3890,7 +3890,7 @@ static int do_label(struct repository *r, const char *name, int len)
3890 strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
3891 strbuf_addf(&msg, "rebase (label) '%.*s'", len, name);
3892
3893 - transaction = ref_store_transaction_begin(refs, &err);
3893 + transaction = ref_store_transaction_begin(refs, 0, &err);
3894 if (!transaction) {
3895 error("%s", err.buf);
3896 ret = -1;
walker.c
+1 -1
@@ -290,7 +290,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,
290
291 if (write_ref) {
292 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
293 - &err);
293 + 0, &err);
294 if (!transaction) {
295 error("%s", err.buf);
296 goto done;