refs: move `original_update_refname` to 'refs.c'

The files backend and the reftable backend implement `original_update_refname` to obtain the original refname of the update. Move it out to 'refs.c' and only expose it internally to the refs library. This will be used in an upcoming commit to also introduce another common functionality for the two backends. We also rename the function to `ref_update_original_update_refname` to keep it consistent with the upcoming other 'ref_update_*' functions that'll be introduced. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed May 7, 2024 at 14:58 UTC e9965ba477de5df7546773920c581569bb54f315
4 files changed +25 -33
refs.c
+8
@@ -2814,3 +2814,11 @@ int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg
2814 {
2815 return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
2816 }
2817 +
2818 +const char *ref_update_original_update_refname(struct ref_update *update)
2819 +{
2820 + while (update->parent_update)
2821 + update = update->parent_update;
2822 +
2823 + return update->refname;
2824 +}
refs/files-backend.c
+5 -16
@@ -2423,17 +2423,6 @@ static int split_symref_update(struct ref_update *update,
2423 return 0;
2424 }
2425
2426 -/*
2427 - * Return the refname under which update was originally requested.
2428 - */
2429 -static const char *original_update_refname(struct ref_update *update)
2430 -{
2431 - while (update->parent_update)
2432 - update = update->parent_update;
2433 -
2434 - return update->refname;
2435 -}
2436 -
2426 /*
2427 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2428 * are consistent with oid, which is the reference's current value. If
@@ -2450,16 +2439,16 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
2439 if (is_null_oid(&update->old_oid))
2440 strbuf_addf(err, "cannot lock ref '%s': "
2441 "reference already exists",
2453 - original_update_refname(update));
2442 + ref_update_original_update_refname(update));
2443 else if (is_null_oid(oid))
2444 strbuf_addf(err, "cannot lock ref '%s': "
2445 "reference is missing but expected %s",
2457 - original_update_refname(update),
2446 + ref_update_original_update_refname(update),
2447 oid_to_hex(&update->old_oid));
2448 else
2449 strbuf_addf(err, "cannot lock ref '%s': "
2450 "is at %s but expected %s",
2462 - original_update_refname(update),
2451 + ref_update_original_update_refname(update),
2452 oid_to_hex(oid),
2453 oid_to_hex(&update->old_oid));
2454
@@ -2513,7 +2502,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
2502
2503 reason = strbuf_detach(err, NULL);
2504 strbuf_addf(err, "cannot lock ref '%s': %s",
2516 - original_update_refname(update), reason);
2505 + ref_update_original_update_refname(update), reason);
2506 free(reason);
2507 goto out;
2508 }
@@ -2533,7 +2522,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
2522 if (update->flags & REF_HAVE_OLD) {
2523 strbuf_addf(err, "cannot lock ref '%s': "
2524 "error reading reference",
2536 - original_update_refname(update));
2525 + ref_update_original_update_refname(update));
2526 ret = TRANSACTION_GENERIC_ERROR;
2527 goto out;
2528 }
refs/refs-internal.h
+5
@@ -749,4 +749,9 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo,
749 */
750 struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store);
751
752 +/*
753 + * Return the refname under which update was originally requested.
754 + */
755 +const char *ref_update_original_update_refname(struct ref_update *update);
756 +
757 #endif /* REFS_REFS_INTERNAL_H */
refs/reftable-backend.c
+7 -17
@@ -578,16 +578,6 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
578 return ret;
579 }
580
581 -/*
582 - * Return the refname under which update was originally requested.
583 - */
584 -static const char *original_update_refname(struct ref_update *update)
585 -{
586 - while (update->parent_update)
587 - update = update->parent_update;
588 - return update->refname;
589 -}
590 -
581 struct reftable_transaction_update {
582 struct ref_update *update;
583 struct object_id current_oid;
@@ -866,7 +856,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
856 /* The reference does not exist, but we expected it to. */
857 strbuf_addf(err, _("cannot lock ref '%s': "
858 "unable to resolve reference '%s'"),
869 - original_update_refname(u), u->refname);
859 + ref_update_original_update_refname(u), u->refname);
860 ret = -1;
861 goto done;
862 }
@@ -938,17 +928,17 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
928 if (u->flags & REF_HAVE_OLD && !oideq(&current_oid, &u->old_oid)) {
929 if (is_null_oid(&u->old_oid))
930 strbuf_addf(err, _("cannot lock ref '%s': "
941 - "reference already exists"),
942 - original_update_refname(u));
931 + "reference already exists"),
932 + ref_update_original_update_refname(u));
933 else if (is_null_oid(&current_oid))
934 strbuf_addf(err, _("cannot lock ref '%s': "
945 - "reference is missing but expected %s"),
946 - original_update_refname(u),
935 + "reference is missing but expected %s"),
936 + ref_update_original_update_refname(u),
937 oid_to_hex(&u->old_oid));
938 else
939 strbuf_addf(err, _("cannot lock ref '%s': "
950 - "is at %s but expected %s"),
951 - original_update_refname(u),
940 + "is at %s but expected %s"),
941 + ref_update_original_update_refname(u),
942 oid_to_hex(&current_oid),
943 oid_to_hex(&u->old_oid));
944 ret = -1;