All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bence Ferdinandy <bence@ferdinandy.com>
To: git@vger.kernel.org
Cc: "Fővárosi Vízművek Zrt." <noreply@vizmuvek.hu>,
	"Bence Ferdinandy" <bence@ferdinandy.com>
Subject: [PATCH v10 6/8] refs: add create_only option to refs_update_symref_extended
Date: Mon, 21 Oct 2024 15:37:03 +0200	[thread overview]
Message-ID: <20241021134354.705636-7-bence@ferdinandy.com> (raw)
In-Reply-To: <20241021134354.705636-1-bence@ferdinandy.com>

Allow the caller to specify that it only wants to update the symref if
it does not already exist. Silently ignore the error from the
transaction API if the symref already exists.

Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com>
---

Notes:
    v4: new patch
    v5: no change
    
    v6: - switched from bool to int for create_only
        - refactored logic in refs_update_symref with goto as layed out by
          Junio
    
    v7: - change commit prefix to be more in line with project standards
        - refactored code to accommodate changes in the first patch, but
          otherwise no change
    
    v8: no change
    
    v9: - no change (except for following through the
          refs_update_symref_extended refactoring)
    
    v10: no change

 builtin/remote.c |  2 +-
 refs.c           | 32 +++++++++++++++++++++++---------
 refs.h           |  2 +-
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index 108f1271d3..b1eba75a2b 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1471,7 +1471,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		if (!refs_ref_exists(refs, b_remote_head.buf))
 			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
 		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"remote set-head", &b_local_head))
+					"remote set-head", &b_local_head, 0))
 			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
 			report_set_head_auto(argv[0], head_name, &b_local_head);
diff --git a/refs.c b/refs.c
index 24a4172cd2..093ee11ab0 100644
--- a/refs.c
+++ b/refs.c
@@ -2116,31 +2116,45 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
 {
-	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
 }
 
 int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent)
+		       struct strbuf *referent, int create_only)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
-	int ret = 0;
+	int ret = 0, prepret = 0;
 
 	transaction = ref_store_transaction_begin(refs, &err);
-	if (!transaction ||
-		ref_transaction_update(transaction, ref, NULL, NULL,
-				   target, NULL, REF_NO_DEREF,
-				   logmsg, &err) ||
-		ref_transaction_prepare(transaction, &err)) {
+	if (!transaction) {
+	error_return:
 		ret = error("%s", err.buf);
 		goto cleanup;
 	}
+	if (create_only) {
+		if (ref_transaction_create(transaction, ref, NULL, target,
+					   REF_NO_DEREF, logmsg, &err))
+			goto error_return;
+		prepret = ref_transaction_prepare(transaction, &err);
+		if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+			goto error_return;
+	} else {
+		if (ref_transaction_update(transaction, ref, NULL, NULL,
+					   target, NULL, REF_NO_DEREF,
+					   logmsg, &err) ||
+			ref_transaction_prepare(transaction, &err))
+			goto error_return;
+	}
+
 	if (referent)
 		refs_read_symbolic_ref(refs, ref, referent);
 
+	if (prepret == TRANSACTION_CREATE_EXISTS)
+		goto cleanup;
 	if (ref_transaction_commit(transaction, &err))
-		ret = error("%s", err.buf);
+		goto error_return;
 
 cleanup:
 	strbuf_release(&err);
diff --git a/refs.h b/refs.h
index a5bc25442b..458582ebcf 100644
--- a/refs.h
+++ b/refs.h
@@ -575,7 +575,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
 
 int refs_update_symref_extended(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent);
+		       struct strbuf *referent, int create_only);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
-- 
2.47.0.94.g8861098b6d


  parent reply	other threads:[~2024-10-21 13:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1088915169.629942.1729445083543@FVRT-HAMMYAS-P.vizmuvek.hu>
2024-10-21 13:36 ` [PATCH v10 0/8] set-head/fetch remote/HEAD updates Bence Ferdinandy
2024-10-21 13:36   ` [PATCH v10 1/8] t/t5505-remote: set default branch to main Bence Ferdinandy
2024-10-21 13:36   ` [PATCH v10 2/8] refs: atomically record overwritten ref in update_symref Bence Ferdinandy
2024-10-21 13:37   ` [PATCH v10 3/8] remote set-head: refactor for readability Bence Ferdinandy
2024-10-21 13:37   ` [PATCH v10 4/8] remote set-head: better output for --auto Bence Ferdinandy
2024-10-21 13:37   ` [PATCH v10 5/8] refs: add TRANSACTION_CREATE_EXISTS error Bence Ferdinandy
2024-10-21 13:37   ` Bence Ferdinandy [this message]
2024-10-21 13:37   ` [PATCH v10 7/8] fetch: set remote/HEAD if it does not exist Bence Ferdinandy
2024-10-21 13:37   ` [PATCH v10 8/8] fetch set_head: handle mirrored bare repositories Bence Ferdinandy
2024-10-21 20:43   ` [PATCH v10 0/8] set-head/fetch remote/HEAD updates Taylor Blau
2024-10-21 21:06     ` Bence Ferdinandy
2024-10-22 17:39   ` Taylor Blau
2024-10-22 18:33     ` Bence Ferdinandy
2024-10-22 19:04       ` Taylor Blau
2024-10-22 19:09         ` Bence Ferdinandy

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=20241021134354.705636-7-bence@ferdinandy.com \
    --to=bence@ferdinandy.com \
    --cc=git@vger.kernel.org \
    --cc=noreply@vizmuvek.hu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.