refs: allow to skip creation of reflog entries

The ref backends do not have any way to disable the creation of reflog entries. This will be required for upcoming ref format migration logic so that we do not create any entries that didn't exist in the original ref database. Provide a new `REF_SKIP_CREATE_REFLOG` flag that allows the caller to disable reflog entry creation. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 6, 2024 at 07:29 UTC fbd1a693c7343d4b37ae6d99b19f15d1293b77c2
5 files changed +20 -2
refs.c
+6
@@ -1194,6 +1194,12 @@ int ref_transaction_update(struct ref_transaction *transaction,
1194 {
1195 assert(err);
1196
1197 + if ((flags & REF_FORCE_CREATE_REFLOG) &&
1198 + (flags & REF_SKIP_CREATE_REFLOG)) {
1199 + strbuf_addstr(err, _("refusing to force and skip creation of reflog"));
1200 + return -1;
1201 + }
1202 +
1203 if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
1204 ((new_oid && !is_null_oid(new_oid)) ?
1205 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
refs.h
+7 -1
@@ -659,13 +659,19 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
659 */
660 #define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
661
662 +/*
663 + * Skip creation of a reflog entry, even if it would have otherwise been
664 + * created.
665 + */
666 +#define REF_SKIP_CREATE_REFLOG (1 << 12)
667 +
668 /*
669 * Bitmask of all of the flags that are allowed to be passed in to
670 * ref_transaction_update() and friends:
671 */
672 #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
673 (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
668 - REF_SKIP_REFNAME_VERIFICATION)
674 + REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG)
675
676 /*
677 * Add a reference update to transaction. `new_oid` is the value that
refs/files-backend.c
+4
@@ -1750,6 +1750,9 @@ static int files_log_ref_write(struct files_ref_store *refs,
1750 {
1751 int logfd, result;
1752
1753 + if (flags & REF_SKIP_CREATE_REFLOG)
1754 + return 0;
1755 +
1756 if (log_all_ref_updates == LOG_REFS_UNSET)
1757 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1758
@@ -2251,6 +2254,7 @@ static int split_head_update(struct ref_update *update,
2254 struct ref_update *new_update;
2255
2256 if ((update->flags & REF_LOG_ONLY) ||
2257 + (update->flags & REF_SKIP_CREATE_REFLOG) ||
2258 (update->flags & REF_IS_PRUNING) ||
2259 (update->flags & REF_UPDATE_VIA_HEAD))
2260 return 0;
refs/reftable-backend.c
+2 -1
@@ -1103,7 +1103,8 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
1103
1104 if (ret)
1105 goto done;
1106 - } else if (u->flags & REF_HAVE_NEW &&
1106 + } else if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&
1107 + (u->flags & REF_HAVE_NEW) &&
1108 (u->flags & REF_FORCE_CREATE_REFLOG ||
1109 should_write_log(&arg->refs->base, u->refname))) {
1110 struct reftable_log_record *log;
t/helper/test-ref-store.c
+1
@@ -126,6 +126,7 @@ static struct flag_definition transaction_flags[] = {
126 FLAG_DEF(REF_FORCE_CREATE_REFLOG),
127 FLAG_DEF(REF_SKIP_OID_VERIFICATION),
128 FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION),
129 + FLAG_DEF(REF_SKIP_CREATE_REFLOG),
130 { NULL, 0 }
131 };
132