Skip to content

Commit aba381c

Browse files
KarthikNayakgitster
authored andcommitted
refs: create and use ref_update_expects_existing_old_ref()
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 <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 939d49e commit aba381c

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

refs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,3 +2863,9 @@ int ref_update_check_old_target(const char *referent, struct ref_update *update,
28632863
referent, update->old_target);
28642864
return -1;
28652865
}
2866+
2867+
int ref_update_expects_existing_old_ref(struct ref_update *update)
2868+
{
2869+
return (update->flags & REF_HAVE_OLD) &&
2870+
(!is_null_oid(&update->old_oid) || update->old_target);
2871+
}

refs/files-backend.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,8 +2411,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
24112411
struct strbuf *err)
24122412
{
24132413
struct strbuf referent = STRBUF_INIT;
2414-
int mustexist = (update->flags & REF_HAVE_OLD) &&
2415-
!is_null_oid(&update->old_oid);
2414+
int mustexist = ref_update_expects_existing_old_ref(update);
24162415
int ret = 0;
24172416
struct ref_lock *lock;
24182417

refs/refs-internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,10 @@ int ref_update_has_null_new_value(struct ref_update *update);
765765
int ref_update_check_old_target(const char *referent, struct ref_update *update,
766766
struct strbuf *err);
767767

768+
/*
769+
* Check if the ref must exist, this means that the old_oid or
770+
* old_target is non NULL.
771+
*/
772+
int ref_update_expects_existing_old_ref(struct ref_update *update);
773+
768774
#endif /* REFS_REFS_INTERNAL_H */

refs/reftable-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
827827
&current_oid, &referent, &u->type);
828828
if (ret < 0)
829829
goto done;
830-
if (ret > 0 && (!(u->flags & REF_HAVE_OLD) || is_null_oid(&u->old_oid))) {
830+
if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
831831
/*
832832
* The reference does not exist, and we either have no
833833
* old object ID or expect the reference to not exist.

0 commit comments

Comments
 (0)