]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: create and use `ref_update_expects_existing_old_ref()`
authorKarthik Nayak <karthik.188@gmail.com>
Fri, 7 Jun 2024 13:32:58 +0000 (15:32 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Jun 2024 17:25:44 +0000 (10:25 -0700)
The files and reftable backend, need to check if a ref must exist, so
that the required validation can be done. A ref must exist only when the
`old_oid` value of the update has been explicitly set and it is not the
`null_oid` value.

Since we also support symrefs now, we need to ensure that even when
`old_target` is set a ref must exist. While this was missed when we
added symref support in transactions, there are no active users of this
path. As we introduce the 'symref-verify' command in the upcoming
commits, it is important to fix this.

So let's export this to a function called
`ref_update_expects_existing_old_ref()` and expose it internally via
'refs-internal.h'.

Helped-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
refs/files-backend.c
refs/refs-internal.h
refs/reftable-backend.c

diff --git a/refs.c b/refs.c
index fa5471d219b60bb516cb6e2135598e2f4a6aa09b..00e87f8ee3bf8a69d74ed8da89e405968f5b264a 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2863,3 +2863,9 @@ int ref_update_check_old_target(const char *referent, struct ref_update *update,
                            referent, update->old_target);
        return -1;
 }
+
+int ref_update_expects_existing_old_ref(struct ref_update *update)
+{
+       return (update->flags & REF_HAVE_OLD) &&
+               (!is_null_oid(&update->old_oid) || update->old_target);
+}
index 3957bfa5795af70ecb46c014b801978db88ced46..ef760869ca65ea9cbb7995c13331f2b16fb708a1 100644 (file)
@@ -2411,8 +2411,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                               struct strbuf *err)
 {
        struct strbuf referent = STRBUF_INIT;
-       int mustexist = (update->flags & REF_HAVE_OLD) &&
-               !is_null_oid(&update->old_oid);
+       int mustexist = ref_update_expects_existing_old_ref(update);
        int ret = 0;
        struct ref_lock *lock;
 
index 53a6c5d84200a9b5efc80d4dfcf649aa86d1747e..ee298ec0d5a92d18af10bece3a8c906f3237f3c0 100644 (file)
@@ -765,4 +765,10 @@ int ref_update_has_null_new_value(struct ref_update *update);
 int ref_update_check_old_target(const char *referent, struct ref_update *update,
                                struct strbuf *err);
 
+/*
+ * Check if the ref must exist, this means that the old_oid or
+ * old_target is non NULL.
+ */
+int ref_update_expects_existing_old_ref(struct ref_update *update);
+
 #endif /* REFS_REFS_INTERNAL_H */
index 1b92c396b65759060391aa159f38ef20e1470f7a..019b99181d9a9bb031b7a5d55ccf100b1da2d9e2 100644 (file)
@@ -827,7 +827,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
                                              &current_oid, &referent, &u->type);
                if (ret < 0)
                        goto done;
-               if (ret > 0 && (!(u->flags & REF_HAVE_OLD) || is_null_oid(&u->old_oid))) {
+               if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
                        /*
                         * The reference does not exist, and we either have no
                         * old object ID or expect the reference to not exist.