From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: David Turner <novalis@novalis.org>, Han-Wen Nienhuys <hanwen@google.com>
Subject: [PATCH 3/4] refs: deduplicate code to delete references
Date: Tue, 14 Nov 2023 09:58:46 +0100 [thread overview]
Message-ID: <bb9f45543daf32e6485a57781e1a71e9503936f8.1699951815.git.ps@pks.im> (raw)
In-Reply-To: <cover.1699951815.git.ps@pks.im>
[-- Attachment #1: Type: text/plain, Size: 5709 bytes --]
Both the files and the packed-refs reference backends now use the same
generic transactions-based code to delete references. Let's pull these
implementations up into `refs_delete_refs()` to deduplicate the code.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
refs.c | 48 ++++++++++++++++++++++++++++++++++++++++---
refs/files-backend.c | 46 +----------------------------------------
refs/packed-backend.c | 45 +---------------------------------------
3 files changed, 47 insertions(+), 92 deletions(-)
diff --git a/refs.c b/refs.c
index fcae5dddc6..16bfa21df7 100644
--- a/refs.c
+++ b/refs.c
@@ -2599,13 +2599,55 @@ void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
int refs_delete_refs(struct ref_store *refs, const char *logmsg,
struct string_list *refnames, unsigned int flags)
{
+ struct ref_transaction *transaction;
+ struct strbuf err = STRBUF_INIT;
+ struct string_list_item *item;
+ int ret = 0, failures = 0;
char *msg;
- int retval;
+
+ if (!refnames->nr)
+ return 0;
msg = normalize_reflog_message(logmsg);
- retval = refs->be->delete_refs(refs, msg, refnames, flags);
+
+ /*
+ * Since we don't check the references' old_oids, the
+ * individual updates can't fail, so we can pack all of the
+ * updates into a single transaction.
+ */
+ transaction = ref_store_transaction_begin(refs, &err);
+ if (!transaction) {
+ ret = error("%s", err.buf);
+ goto out;
+ }
+
+ for_each_string_list_item(item, refnames) {
+ ret = ref_transaction_delete(transaction, item->string,
+ NULL, flags, msg, &err);
+ if (ret) {
+ warning(_("could not delete reference %s: %s"),
+ item->string, err.buf);
+ strbuf_reset(&err);
+ failures = 1;
+ }
+ }
+
+ ret = ref_transaction_commit(transaction, &err);
+ if (ret) {
+ if (refnames->nr == 1)
+ error(_("could not delete reference %s: %s"),
+ refnames->items[0].string, err.buf);
+ else
+ error(_("could not delete references: %s"), err.buf);
+ }
+
+out:
+ if (!ret && failures)
+ ret = -1;
+ ref_transaction_free(transaction);
+ strbuf_release(&err);
free(msg);
- return retval;
+ return ret;
}
int delete_refs(const char *msg, struct string_list *refnames,
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 778d3a96ba..8d28810e67 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1268,51 +1268,7 @@ static int files_pack_refs(struct ref_store *ref_store,
static int files_delete_refs(struct ref_store *ref_store, const char *msg,
struct string_list *refnames, unsigned int flags)
{
- struct ref_transaction *transaction;
- struct strbuf err = STRBUF_INIT;
- struct string_list_item *item;
- int ret = 0, failures = 0;
-
- if (!refnames->nr)
- return 0;
-
- /*
- * Since we don't check the references' old_oids, the
- * individual updates can't fail, so we can pack all of the
- * updates into a single transaction.
- */
- transaction = ref_store_transaction_begin(ref_store, &err);
- if (!transaction) {
- ret = error("%s", err.buf);
- goto out;
- }
-
- for_each_string_list_item(item, refnames) {
- ret = ref_transaction_delete(transaction, item->string,
- NULL, flags, msg, &err);
- if (ret) {
- warning(_("could not delete reference %s: %s"),
- item->string, err.buf);
- strbuf_reset(&err);
- failures = 1;
- }
- }
-
- ret = ref_transaction_commit(transaction, &err);
- if (ret) {
- if (refnames->nr == 1)
- error(_("could not delete reference %s: %s"),
- refnames->items[0].string, err.buf);
- else
- error(_("could not delete references: %s"), err.buf);
- }
-
-out:
- if (!ret && failures)
- ret = -1;
- ref_transaction_free(transaction);
- strbuf_release(&err);
- return ret;
+ return refs_delete_refs(ref_store, msg, refnames, flags);
}
/*
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 59c78d7941..1589577005 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -1691,50 +1691,7 @@ static int packed_initial_transaction_commit(struct ref_store *ref_store UNUSED,
static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
struct string_list *refnames, unsigned int flags)
{
- struct packed_ref_store *refs =
- packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
- struct strbuf err = STRBUF_INIT;
- struct ref_transaction *transaction;
- struct string_list_item *item;
- int ret;
-
- (void)refs; /* We need the check above, but don't use the variable */
-
- if (!refnames->nr)
- return 0;
-
- /*
- * Since we don't check the references' old_oids, the
- * individual updates can't fail, so we can pack all of the
- * updates into a single transaction.
- */
-
- transaction = ref_store_transaction_begin(ref_store, &err);
- if (!transaction)
- return -1;
-
- for_each_string_list_item(item, refnames) {
- if (ref_transaction_delete(transaction, item->string, NULL,
- flags, msg, &err)) {
- warning(_("could not delete reference %s: %s"),
- item->string, err.buf);
- strbuf_reset(&err);
- }
- }
-
- ret = ref_transaction_commit(transaction, &err);
-
- if (ret) {
- if (refnames->nr == 1)
- error(_("could not delete reference %s: %s"),
- refnames->items[0].string, err.buf);
- else
- error(_("could not delete references: %s"), err.buf);
- }
-
- ref_transaction_free(transaction);
- strbuf_release(&err);
- return ret;
+ return refs_delete_refs(ref_store, msg, refnames, flags);
}
static int packed_pack_refs(struct ref_store *ref_store UNUSED,
--
2.42.0
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2023-11-14 8:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-14 8:58 [PATCH 0/4] refs: remove virtual `delete_refs()` function Patrick Steinhardt
2023-11-14 8:58 ` [PATCH 1/4] t5510: ensure that the packed-refs file needs locking Patrick Steinhardt
2023-11-14 8:58 ` [PATCH 2/4] refs/files: use transactions to delete references Patrick Steinhardt
2023-11-14 8:58 ` Patrick Steinhardt [this message]
2023-11-14 8:58 ` [PATCH 4/4] refs: remove `delete_refs` callback from backends Patrick Steinhardt
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=bb9f45543daf32e6485a57781e1a71e9503936f8.1699951815.git.ps@pks.im \
--to=ps@pks.im \
--cc=git@vger.kernel.org \
--cc=hanwen@google.com \
--cc=novalis@novalis.org \
/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).