From: Karthik Nayak <karthik.188@gmail.com>
To: karthik.188@gmail.com
Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im
Subject: [PATCH v4 1/7] refs: create and use `ref_update_expects_existing_old_ref()`
Date: Wed, 5 Jun 2024 12:29:52 +0200 [thread overview]
Message-ID: <20240605102958.716432-2-knayak@gitlab.com> (raw)
In-Reply-To: <https://lore.kernel.org/r/20240530120940.456817-1-knayak@gitlab.com>
From: Karthik Nayak <karthik.188@gmail.com>
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>
---
refs.c | 6 ++++++
refs/files-backend.c | 3 +--
refs/refs-internal.h | 6 ++++++
refs/reftable-backend.c | 2 +-
4 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/refs.c b/refs.c
index 8260c27cde..50d8d7d777 100644
--- a/refs.c
+++ b/refs.c
@@ -2679,3 +2679,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);
+}
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 5f3089d947..194e74eb4d 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2412,8 +2412,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;
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 53a6c5d842..ee298ec0d5 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -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 */
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 1af86bbdec..b838cf8f00 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -824,7 +824,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
¤t_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.
--
2.43.GIT
next prev parent reply other threads:[~2024-06-05 10:30 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <https://lore.kernel.org/r/20240530120940.456817-1-knayak@gitlab.com>
2024-06-05 10:29 ` [PATCH v4 0/7] update-ref: add symref support for --stdin Karthik Nayak
2024-06-06 8:22 ` Karthik Nayak
2024-06-06 11:03 ` Karthik Nayak
2024-06-07 13:32 ` [PATCH v5 " Karthik Nayak
2024-06-07 13:32 ` [PATCH v5 1/7] refs: create and use `ref_update_expects_existing_old_ref()` Karthik Nayak
2024-06-07 13:32 ` [PATCH v5 2/7] refs: specify error for regular refs with `old_target` Karthik Nayak
2024-06-10 6:54 ` Patrick Steinhardt
2024-06-10 8:26 ` Karthik Nayak
2024-06-07 13:33 ` [PATCH v5 3/7] update-ref: add support for 'symref-verify' command Karthik Nayak
2024-06-07 13:33 ` [PATCH v5 4/7] update-ref: add support for 'symref-delete' command Karthik Nayak
2024-06-07 13:33 ` [PATCH v5 5/7] update-ref: add support for 'symref-create' command Karthik Nayak
2024-06-07 13:33 ` [PATCH v5 6/7] reftable: pick either 'oid' or 'target' for new updates Karthik Nayak
2024-06-07 13:33 ` [PATCH v5 7/7] update-ref: add support for 'symref-update' command Karthik Nayak
2024-06-05 10:29 ` Karthik Nayak [this message]
2024-06-05 10:29 ` [PATCH v4 2/7] refs: specify error for regular refs with `old_target` Karthik Nayak
2024-06-06 11:02 ` Patrick Steinhardt
2024-06-06 14:20 ` Karthik Nayak
2024-06-05 10:29 ` [PATCH v4 3/7] update-ref: add support for 'symref-verify' command Karthik Nayak
2024-06-05 10:29 ` [PATCH v4 4/7] update-ref: add support for 'symref-delete' command Karthik Nayak
2024-06-05 10:29 ` [PATCH v4 5/7] update-ref: add support for 'symref-create' command Karthik Nayak
2024-06-05 10:29 ` [PATCH v4 6/7] reftable: pick either 'oid' or 'target' for new updates Karthik Nayak
2024-06-05 10:29 ` [PATCH v4 7/7] update-ref: add support for 'symref-update' command Karthik Nayak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240605102958.716432-2-knayak@gitlab.com \
--to=karthik.188@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).