From patchwork Fri May 3 12:41:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652739 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE8AFB67E for ; Fri, 3 May 2024 12:41:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740083; cv=none; b=uGuSVyLSoBdZlEhxu/OmP94ry3l4egwVMuLWM4LZq8KdpogHyHDVqwLl9Q+5wOIOvOH3bWwgVwBpep/b4s6oJX1y73vSEgpsiTXVYW+MsmvC3Y2Fg/+KIG9a2rcMDdBWVrWN8bazkDKE5CbbhdWiTiT5BNn3olU1cyHII0wblAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740083; c=relaxed/simple; bh=3HPQ5eXYNbnllE/1DwRDbEQtKegBlLOLmBF4h7M/gAQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c+VnGiTXvJARcT6j8BFRnqfe4tPkHeQGO+72orOsocaOZRW8xLdtFqVWkjeMrUSpCPB78YK6TnFk1Q55+wrzGqpA15s7apBCrsvj39HeRQ2LxJxcCEMDdcoOqgamSkV7I4cTV7MiEpuhQ5HXYLXD+TIr8kVGczU+lntMkKP4i2g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DW8PLH6V; arc=none smtp.client-ip=209.85.218.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DW8PLH6V" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a524ecaf215so1286184966b.2 for ; Fri, 03 May 2024 05:41:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740079; x=1715344879; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ztXes8DqWxKHn9jH3Q0PG79XcoVYWlFDKpa7feSuZgI=; b=DW8PLH6VdoL8KcVazuaYXq0ALYeojsoMg1VZKwgXN9YtzWWFyQZcmiipRAUR5IbpTW sLiqoOBHcqERHELF8tYD2j++APQeHZDWB3DKh2D50/pcif1BF04s/J57yeogogTfbbtH HK7mLSdNlG/UNDgmMtbb+8VrdJOKYnfxoF+zafLqOH9Oje79O6Ld7aqkPFpC8uA6xFNR dCJIL6iTsSEhr02fjoDv9oAqQSG3hQI/NtE1rZ8ne72xXf3SfzEFR2wBGe+XQrVeta0G aufrdyB5mBbH4sqt97+iAWOlvM+i7cJkKsnlM/wT2tDgz8gl05EQ5bGIRr0y56+Rtew0 OUMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740079; x=1715344879; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ztXes8DqWxKHn9jH3Q0PG79XcoVYWlFDKpa7feSuZgI=; b=J7cO/cwN0gE8vPjuTV+lR+2QKGaw+yETeSVR3N3ZlQ97BD7Kxhcturg60R4AyPq2g2 MgoGZOH+21wiv6NSCfaR0o5GFi/5f7SjC5IlfZ8mUYljd+CIExZnuC+0AFIcGBgWDNJf yumjY3srTsxmqj2QRFnDf3HwcTY9HB6J8G88ddg2ksF3jCcsRc4PGkV3agem5bmDrY9r 7GT4/Ce/gXYOVIZhFfueZXWpsroPbJP0GlfmNBbCr+oPrC4yy43gCYO6OeItN4xsLn4C HUEa0yr+H1sk1u8V/ULkgHy762UuTVOzJ7y82YbM4NBHY2goqoajcIENpeh3k/R5ZH2R ZafQ== X-Forwarded-Encrypted: i=1; AJvYcCVR/lZua3mxTMSpc3wLfu4aXqux5oGBdrp9uYwLzZelAMVhkJoIUNOoQ5hK/LiXFm1wmzlJ8FkJWkVZFxggdY8f5kf3 X-Gm-Message-State: AOJu0YwjNvTT7rm/Ovq1pTBNML/NYefoHMMm/1wXmPo3n03dwYSImCy7 TjchSK9AHBC1BSpYqZMfMeGwvk5fYEaITC6JEVmjIFAPOBlUl9G4 X-Google-Smtp-Source: AGHT+IGB53q1DbATlsLayNcnKc53/zJz1IFA3Pr55oTsC9sYtvRlZT22p+/kDTOePzwfohjc/Lv5Vg== X-Received: by 2002:a17:907:c93:b0:a59:9b75:b84 with SMTP id gi19-20020a1709070c9300b00a599b750b84mr413584ejc.35.1714740078747; Fri, 03 May 2024 05:41:18 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:18 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 1/7] refs: accept symref values in `ref_transaction_update()` Date: Fri, 3 May 2024 14:41:09 +0200 Message-ID: <20240503124115.252413-2-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The function `ref_transaction_update()` obtains ref information and flags to create a `ref_update` and add them to the transaction at hand. To extend symref support in transactions, we need to also accept the old and new ref targets and process it. This commit adds the required parameters to the function and modifies all call sites. The two parameters added are `new_target` and `old_target`. The `new_target` is used to denote what the reference should point to when the transaction is applied. Some functions allow this parameter to be NULL, meaning that the reference is not changed. The `old_target` denotes the value the reference must have before the update. Some functions allow this parameter to be NULL, meaning that the old value of the reference is not checked. We also update the internal function `ref_transaction_add_update()` similarly to take the two new parameters. Signed-off-by: Karthik Nayak --- branch.c | 2 +- builtin/fast-import.c | 5 +++-- builtin/fetch.c | 2 +- builtin/receive-pack.c | 1 + builtin/replace.c | 2 +- builtin/tag.c | 1 + builtin/update-ref.c | 1 + refs.c | 22 +++++++++++++++++----- refs.h | 18 +++++++++++++++++- refs/files-backend.c | 12 ++++++------ refs/refs-internal.h | 14 ++++++++++++++ refs/reftable-backend.c | 4 ++-- sequencer.c | 9 +++++---- walker.c | 2 +- 14 files changed, 71 insertions(+), 24 deletions(-) diff --git a/branch.c b/branch.c index e4a738fc7b..48af4c3ceb 100644 --- a/branch.c +++ b/branch.c @@ -627,7 +627,7 @@ void create_branch(struct repository *r, if (!transaction || ref_transaction_update(transaction, ref.buf, &oid, forcing ? NULL : null_oid(), - 0, msg, &err) || + NULL, NULL, 0, msg, &err) || ref_transaction_commit(transaction, &err)) die("%s", err.buf); ref_transaction_free(transaction); diff --git a/builtin/fast-import.c b/builtin/fast-import.c index dc5a9d32dd..297dfb91a1 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -1634,7 +1634,7 @@ static int update_branch(struct branch *b) transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_update(transaction, b->name, &b->oid, &old_oid, - 0, msg, &err) || + NULL, NULL, 0, msg, &err) || ref_transaction_commit(transaction, &err)) { ref_transaction_free(transaction); error("%s", err.buf); @@ -1675,7 +1675,8 @@ static void dump_tags(void) strbuf_addf(&ref_name, "refs/tags/%s", t->name); if (ref_transaction_update(transaction, ref_name.buf, - &t->oid, NULL, 0, msg, &err)) { + &t->oid, NULL, NULL, NULL, + 0, msg, &err)) { failure |= error("%s", err.buf); goto cleanup; } diff --git a/builtin/fetch.c b/builtin/fetch.c index 5857d860db..66840b7c5b 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -668,7 +668,7 @@ static int s_update_ref(const char *action, ret = ref_transaction_update(transaction, ref->name, &ref->new_oid, check_old ? &ref->old_oid : NULL, - 0, msg, &err); + NULL, NULL, 0, msg, &err); if (ret) { ret = STORE_REF_ERROR_OTHER; goto out; diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index e8d7df14b6..b150ef39a8 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1595,6 +1595,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) if (ref_transaction_update(transaction, namespaced_name, new_oid, old_oid, + NULL, NULL, 0, "push", &err)) { rp_error("%s", err.buf); diff --git a/builtin/replace.c b/builtin/replace.c index da59600ad2..7690687b0e 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -201,7 +201,7 @@ static int replace_object_oid(const char *object_ref, transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_update(transaction, ref.buf, repl, &prev, - 0, NULL, &err) || + NULL, NULL, 0, NULL, &err) || ref_transaction_commit(transaction, &err)) res = error("%s", err.buf); diff --git a/builtin/tag.c b/builtin/tag.c index 9a33cb50b4..40a65fdebc 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -660,6 +660,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) transaction = ref_transaction_begin(&err); if (!transaction || ref_transaction_update(transaction, ref.buf, &object, &prev, + NULL, NULL, create_reflog ? REF_FORCE_CREATE_REFLOG : 0, reflog_msg.buf, &err) || ref_transaction_commit(transaction, &err)) { diff --git a/builtin/update-ref.c b/builtin/update-ref.c index e46afbc46d..21fdbf6ac8 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -204,6 +204,7 @@ static void parse_cmd_update(struct ref_transaction *transaction, if (ref_transaction_update(transaction, refname, &new_oid, have_old ? &old_oid : NULL, + NULL, NULL, update_flags | create_reflog_flag, msg, &err)) die("%s", err.buf); diff --git a/refs.c b/refs.c index 55d2e0b2cb..47bc9dd103 100644 --- a/refs.c +++ b/refs.c @@ -1228,6 +1228,7 @@ struct ref_update *ref_transaction_add_update( const char *refname, unsigned int flags, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, const char *old_target, const char *msg) { struct ref_update *update; @@ -1235,6 +1236,11 @@ struct ref_update *ref_transaction_add_update( if (transaction->state != REF_TRANSACTION_OPEN) BUG("update called for transaction that is not open"); + if (old_oid && !is_null_oid(old_oid) && old_target) + BUG("only one of old_oid and old_target should be non NULL"); + if (new_oid && !is_null_oid(new_oid) && new_target) + BUG("only one of new_oid and new_target should be non NULL"); + FLEX_ALLOC_STR(update, refname, refname); ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); transaction->updates[transaction->nr++] = update; @@ -1253,6 +1259,8 @@ int ref_transaction_update(struct ref_transaction *transaction, const char *refname, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, + const char *old_target, unsigned int flags, const char *msg, struct strbuf *err) { @@ -1280,7 +1288,8 @@ int ref_transaction_update(struct ref_transaction *transaction, flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); ref_transaction_add_update(transaction, refname, flags, - new_oid, old_oid, msg); + new_oid, old_oid, new_target, + old_target, msg); return 0; } @@ -1295,7 +1304,8 @@ int ref_transaction_create(struct ref_transaction *transaction, return 1; } return ref_transaction_update(transaction, refname, new_oid, - null_oid(), flags, msg, err); + null_oid(), NULL, NULL, flags, + msg, err); } int ref_transaction_delete(struct ref_transaction *transaction, @@ -1308,7 +1318,8 @@ int ref_transaction_delete(struct ref_transaction *transaction, BUG("delete called with old_oid set to zeros"); return ref_transaction_update(transaction, refname, null_oid(), old_oid, - flags, msg, err); + NULL, NULL, flags, + msg, err); } int ref_transaction_verify(struct ref_transaction *transaction, @@ -1321,6 +1332,7 @@ int ref_transaction_verify(struct ref_transaction *transaction, BUG("verify called with old_oid set to NULL"); return ref_transaction_update(transaction, refname, NULL, old_oid, + NULL, NULL, flags, NULL, err); } @@ -1335,8 +1347,8 @@ int refs_update_ref(struct ref_store *refs, const char *msg, t = ref_store_transaction_begin(refs, &err); if (!t || - ref_transaction_update(t, refname, new_oid, old_oid, flags, msg, - &err) || + ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL, + flags, msg, &err) || ref_transaction_commit(t, &err)) { ret = 1; ref_transaction_free(t); diff --git a/refs.h b/refs.h index d278775e08..c7851bf587 100644 --- a/refs.h +++ b/refs.h @@ -648,6 +648,16 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err); * before the update. A copy of this value is made in the * transaction. * + * new_target -- the target reference that the reference will be + * updated to point to. If the reference is a regular reference, + * it will be converted to a symbolic reference. Cannot be set + * together with `new_oid`. A copy of this value is made in the + * transaction. + * + * old_target -- the reference that the reference must be pointing to. + * Canont be set together with `old_oid`. A copy of this value is + * made in the transaction. + * * flags -- flags affecting the update, passed to * update_ref_lock(). Possible flags: REF_NO_DEREF, * REF_FORCE_CREATE_REFLOG. See those constants for more @@ -713,7 +723,11 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err); * beforehand. The old value is checked after the lock is taken to * prevent races. If the old value doesn't agree with old_oid, the * whole transaction fails. If old_oid is NULL, then the previous - * value is not checked. + * value is not checked. If `old_target` is not NULL, treat the reference + * as a symbolic ref and validate that its target before the update is + * `old_target`. If the `new_target` is not NULL, then the reference + * will be updated to a symbolic ref which targets `new_target`. + * Together, these allow us to update between regular refs and symrefs. * * See the above comment "Reference transaction updates" for more * information. @@ -722,6 +736,8 @@ int ref_transaction_update(struct ref_transaction *transaction, const char *refname, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, + const char *old_target, unsigned int flags, const char *msg, struct strbuf *err); diff --git a/refs/files-backend.c b/refs/files-backend.c index a098d14ea0..e4d0aa3d41 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1198,7 +1198,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) ref_transaction_add_update( transaction, r->name, REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING, - null_oid(), &r->oid, NULL); + null_oid(), &r->oid, NULL, NULL, NULL); if (ref_transaction_commit(transaction, &err)) goto cleanup; @@ -1292,7 +1292,7 @@ static int files_pack_refs(struct ref_store *ref_store, * packed-refs transaction: */ if (ref_transaction_update(transaction, iter->refname, - iter->oid, NULL, + iter->oid, NULL, NULL, NULL, REF_NO_DEREF, NULL, &err)) die("failure preparing to create packed reference %s: %s", iter->refname, err.buf); @@ -2309,7 +2309,7 @@ static int split_head_update(struct ref_update *update, transaction, "HEAD", update->flags | REF_LOG_ONLY | REF_NO_DEREF, &update->new_oid, &update->old_oid, - update->msg); + NULL, NULL, update->msg); /* * Add "HEAD". This insertion is O(N) in the transaction @@ -2372,7 +2372,7 @@ static int split_symref_update(struct ref_update *update, new_update = ref_transaction_add_update( transaction, referent, new_flags, &update->new_oid, &update->old_oid, - update->msg); + NULL, NULL, update->msg); new_update->parent_update = update; @@ -2763,7 +2763,7 @@ static int files_transaction_prepare(struct ref_store *ref_store, packed_transaction, update->refname, REF_HAVE_NEW | REF_NO_DEREF, &update->new_oid, NULL, - NULL); + NULL, NULL, NULL); } } @@ -3048,7 +3048,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store, ref_transaction_add_update(packed_transaction, update->refname, update->flags & ~REF_HAVE_OLD, &update->new_oid, &update->old_oid, - NULL); + NULL, NULL, NULL); } if (packed_refs_lock(refs->packed_ref_store, 0, err)) { diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 56641aa57a..108f4ec419 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -124,6 +124,19 @@ struct ref_update { */ struct object_id old_oid; + /* + * If set, point the reference to this value. This can also be + * used to convert regular references to become symbolic refs. + * Cannot be set together with `new_oid`. + */ + const char *new_target; + + /* + * If set, check that the reference previously pointed to this + * value. Cannot be set together with `old_oid`. + */ + const char *old_target; + /* * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags. @@ -173,6 +186,7 @@ struct ref_update *ref_transaction_add_update( const char *refname, unsigned int flags, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, const char *old_target, const char *msg); /* diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 1cda48c504..6104471199 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -829,7 +829,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, new_update = ref_transaction_add_update( transaction, "HEAD", u->flags | REF_LOG_ONLY | REF_NO_DEREF, - &u->new_oid, &u->old_oid, u->msg); + &u->new_oid, &u->old_oid, NULL, NULL, u->msg); string_list_insert(&affected_refnames, new_update->refname); } @@ -908,7 +908,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, */ new_update = ref_transaction_add_update( transaction, referent.buf, new_flags, - &u->new_oid, &u->old_oid, u->msg); + &u->new_oid, &u->old_oid, NULL, NULL, u->msg); new_update->parent_update = u; /* diff --git a/sequencer.c b/sequencer.c index 88de4dc20f..61e007d85f 100644 --- a/sequencer.c +++ b/sequencer.c @@ -665,7 +665,7 @@ static int fast_forward_to(struct repository *r, if (!transaction || ref_transaction_update(transaction, "HEAD", to, unborn && !is_rebase_i(opts) ? - null_oid() : from, + null_oid() : from, NULL, NULL, 0, sb.buf, &err) || ref_transaction_commit(transaction, &err)) { ref_transaction_free(transaction); @@ -1298,7 +1298,7 @@ int update_head_with_reflog(const struct commit *old_head, if (!transaction || ref_transaction_update(transaction, "HEAD", new_head, old_head ? &old_head->object.oid : null_oid(), - 0, sb.buf, err) || + NULL, NULL, 0, sb.buf, err) || ref_transaction_commit(transaction, err)) { ret = -1; } @@ -3832,8 +3832,9 @@ static int do_label(struct repository *r, const char *name, int len) } else if (repo_get_oid(r, "HEAD", &head_oid)) { error(_("could not read HEAD")); ret = -1; - } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, - NULL, 0, msg.buf, &err) < 0 || + } else if (ref_transaction_update(transaction, ref_name.buf, + &head_oid, NULL, NULL, NULL, + 0, msg.buf, &err) < 0 || ref_transaction_commit(transaction, &err)) { error("%s", err.buf); ret = -1; diff --git a/walker.c b/walker.c index c0fd632d92..1b3df43906 100644 --- a/walker.c +++ b/walker.c @@ -324,7 +324,7 @@ int walker_fetch(struct walker *walker, int targets, char **target, strbuf_reset(&refname); strbuf_addf(&refname, "refs/%s", write_ref[i]); if (ref_transaction_update(transaction, refname.buf, - oids + i, NULL, 0, + oids + i, NULL, NULL, NULL, 0, msg ? msg : "fetch (unknown)", &err)) { error("%s", err.buf); From patchwork Fri May 3 12:41:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652738 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 787FF21A06 for ; Fri, 3 May 2024 12:41:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740083; cv=none; b=TVPHnoIsBt6YAB5FEFZnkzwn3YKPv8meU19tCfQxKQKNIfZk03DH12Rkr2zdJrFaLLt86ONFzUpoUqtZMuJkuxLJoBgJXgItHzIk02Xj2eIKekJwMyeGTsyJYKTGLZHHdc+y4MatVaSTkX+ddpt9LWoaJSf2/XjnhZCDWWx4Vt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740083; c=relaxed/simple; bh=/z+GjUanz0VJg8cfRSx5x7luD4TqFaFjagpMOYcgiek=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JpDTb1kfq4+iYgeixaTWSf5wK5FLGVe6/TMYZqq8Tusc2vXbgwl1VMf4Bg32ZlK6uL6p5qaFCTpGk2kyafT9sTGdW3fssrkHTDkE4xCz4ybc7OSMV5Jt1fFTJABfASgu/Gg2NHSnlUGoVh6e5whwzipVNZ18+YRrTmHkG2M5TIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G95+QaXY; arc=none smtp.client-ip=209.85.218.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G95+QaXY" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a58fbbcd77aso871351666b.2 for ; Fri, 03 May 2024 05:41:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740080; x=1715344880; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FERSfw3Ff4dZG+nIeAH/tJSTfDrXGZVZI6a+PJbbfNA=; b=G95+QaXYpql3CbtGLal3jjsQ2GWJ7kRQO2sj+e4fRH+0yMou+76j2Z+JVqnLpnVfCq bkmT+XGr4vrJW87wjF/dqoLRmiH7vZ7UuU7dL2i7tSDISiMDNJrEELjGeVvxBv1ymhxs 4AEhWsDhR1OB4iTcKNb4aEJ061HwhBqGdGonVREiw8mJP2+TMy4ZRy/enWVcRtVY54iU 0uXcge+1f8feixLHIYANRdPHv7uYxfB5pbcmpeKy5W1HMkxKAe1zk35qrv72yAZm3fkt EZVKi3La6kpXuBXE2MnR3fZNaIiAdxkN+yDf3L4X1Jd7EGznPtKVKT/AoSbU2vEJGv9+ vsFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740080; x=1715344880; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FERSfw3Ff4dZG+nIeAH/tJSTfDrXGZVZI6a+PJbbfNA=; b=SrmgYYOzADqjQcfouX8K5KCqse/tidwQkmWSFQV4Dw7/bsYf0g2vX5pLL/7yGxtbbP NQAGCNAUMxFq5jVHnbC+vbicTIigPifLyFh2qLsXshiwJttukkpwnar9ZYoYCRao1DFW WCRmVvfrZNwmPL4F7zkn1ZQ5F9yvDxHdHYGAcS6NwgWLGw4f1C8gH26QZ4U84gVgoEf3 YI6ngg6uYcXzu+oRqhlYTznLtWIe0Wn4I552EsRmZxyDYFnmKhQxCUp7o7ObIyG/u6pd sX1pJGxHbQWLBUgSjhZoURCXuMAscjzlXpot0PDZNkNXq+IF/GzN/m5Zyb809/J9AWH2 zXGg== X-Forwarded-Encrypted: i=1; AJvYcCUScgG3s7hqUlWCkKMEI9ydsLl9LR1/eC/po7ZChzv9wudDj2OonX52OJoq3G7IrFtUEIwS506ht8fCoFRXGH2J1f0U X-Gm-Message-State: AOJu0YzPyMvRSCHJQHdxcUejekuiqIYdGlTnfIwUjDG0OSyx+3icToao Ja7MJNfxl/RujjMdrUQrdOuIY6hCxydAiawSedfXlPesiogoy1OnXZd+cQ== X-Google-Smtp-Source: AGHT+IHhJWuKJDyU4Btcd385WtV3tLRaYEjUGflIr9hZyrfuDnf3t56DI/dPLaV/y2SNMFh8/8PJvA== X-Received: by 2002:a17:906:8a49:b0:a59:9bf1:4088 with SMTP id gx9-20020a1709068a4900b00a599bf14088mr504614ejc.27.1714740079502; Fri, 03 May 2024 05:41:19 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:19 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 2/7] files-backend: extract out `create_symref_lock()` Date: Fri, 3 May 2024 14:41:10 +0200 Message-ID: <20240503124115.252413-3-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The function `create_symref_locked()` creates a symref by creating a '.lock' file and then committing the symref lock, which creates the final symref. Extract the early half of `create_symref_locked()` into a new helper function `create_symref_lock()`. Because the name of the new function is too similar to the original, rename the original to `create_and_commit_symref()` to avoid confusion. The new function `create_symref_locked()` can be used to create the symref lock in a separate step from that of committing it. This allows to add transactional support for symrefs, where the lock would be created in the preparation step and the lock would be committed in the finish step. Signed-off-by: Karthik Nayak --- refs/files-backend.c | 51 ++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index e4d0aa3d41..74a713090c 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1920,27 +1920,49 @@ static void update_symref_reflog(struct files_ref_store *refs, } } -static int create_symref_locked(struct files_ref_store *refs, - struct ref_lock *lock, const char *refname, - const char *target, const char *logmsg) +static int create_symref_lock(struct files_ref_store *refs, + struct ref_lock *lock, const char *refname, + const char *target, struct strbuf *err) { + if (!fdopen_lock_file(&lock->lk, "w")) { + strbuf_addf(err, "unable to fdopen %s: %s", + get_lock_file_path(&lock->lk), strerror(errno)); + return -1; + } + + if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0) { + strbuf_addf(err, "unable to write to %s: %s", + get_lock_file_path(&lock->lk), strerror(errno)); + return -1; + } + + return 0; +} + +static int create_and_commit_symref(struct files_ref_store *refs, + struct ref_lock *lock, const char *refname, + const char *target, const char *logmsg) +{ + struct strbuf err = STRBUF_INIT; + int ret; + if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { update_symref_reflog(refs, lock, refname, target, logmsg); return 0; } - if (!fdopen_lock_file(&lock->lk, "w")) - return error("unable to fdopen %s: %s", - get_lock_file_path(&lock->lk), strerror(errno)); + ret = create_symref_lock(refs, lock, refname, target, &err); + if (!ret) { + update_symref_reflog(refs, lock, refname, target, logmsg); - update_symref_reflog(refs, lock, refname, target, logmsg); + if (commit_ref(lock) < 0) + return error("unable to write symref for %s: %s", refname, + strerror(errno)); + } else { + return error("%s", err.buf); + } - /* no error check; commit_ref will check ferror */ - fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target); - if (commit_ref(lock) < 0) - return error("unable to write symref for %s: %s", refname, - strerror(errno)); - return 0; + return ret; } static int files_create_symref(struct ref_store *ref_store, @@ -1960,7 +1982,8 @@ static int files_create_symref(struct ref_store *ref_store, return -1; } - ret = create_symref_locked(refs, lock, refname, target, logmsg); + ret = create_and_commit_symref(refs, lock, refname, target, logmsg); + unlock_ref(lock); return ret; } From patchwork Fri May 3 12:41:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652740 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B35B515219B for ; Fri, 3 May 2024 12:41:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740084; cv=none; b=Ee7rYyvZ1zlPWL/DlZX30I2v7cl6iD7KThaABmRqg9QhjwlTh3AmrWBdphyIdHKEoIzGDSUODA7iQKb1tQPbLXUzcrFKj6XyPJ9mA038S8FIvy8hJyY3erzvXEoCDPsHK5eWXkezk/Kw+l7yokbFdcaE8tBGsejqTGARFIPtFWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740084; c=relaxed/simple; bh=J3XeoV5nK4oO06ErdSoXYkZHupjSmcQ6E+RZ+e2434I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AOuPsaUUuQgDtygeDUEIu4KarJT93mkx0YOdsIhVXmFsmtxeWnOGEL6geOmH6rDzKd135YrANdflqJAmtApyKd6q54Y+ps0IE70yF8NsldH4lCu52/WDSC7VSgdFIuEhy4wGv46yHslyWlTk2j4mPRy7lbHnOluR57Skv+V00VY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ks3hlRUt; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ks3hlRUt" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a58fbbcd77aso871356666b.2 for ; Fri, 03 May 2024 05:41:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740081; x=1715344881; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kmW6xXSQ5+h67vylxhJjiPfg1sbJtAkdP1MeiqW3zzs=; b=ks3hlRUtIORTn6V6SGBzwBBSDEbFmlKV3fEXAdvZji7YCwBC3D7SwvL3LFG+4pYSY8 ydrceubkXh96CJgk1/HJxDeTB2u4R4ta7QCUk2+x+cVJfhhDRJVuoCqzAUEk/PpPNcDl CD2ATGgRHiJe+Qn405QC5VmcwHrE6S7/J3h4bcFdgkaShy9d9sTKt71lXPbdAtuiKsnT +6TJJcS4duMs9hHQtNMtXQtlHtrne3UyWm6m8Rp9LUu4JTWNKuOb2A8/8Kt1+Ojxolgj B3j3/oUuQLh2hHcTCxQLHa4xLYapN2JX4GqN/KxtB1V+Twfy4fdmKYqbM3txCJsnxFwH XBMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740081; x=1715344881; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kmW6xXSQ5+h67vylxhJjiPfg1sbJtAkdP1MeiqW3zzs=; b=QIwmKQEgJA9gVXc4dD51omJUHxKpGV7+FpxWtVmzvzQGgm29D6t7NJQYsx+1bSL/qX sy6O0XWfwBhr5Gt/KMmGpzdHdRCWk635fc1UavFHm2KbR7ZSMhXsRruOlQZsN1rFue9i H9VDn14WowgEFdpmaPk5jwMo00FqCryoT+iGYcS2TMQ7/a88ofcT5PBGU1IlzCzx+mp+ a1hwWhWg4BxLKurb5swVjdNvl7b72nKG6Y7mCZdczN5VCqf/XAQD3zOF9cuozk0K/X5b qH5ZiIrDuPaGX3mcOgWYSZ3dkU0Zm630P2SHYTL57S6+spKVec/dypxpYB6qb5ozBvyy Eqkg== X-Forwarded-Encrypted: i=1; AJvYcCW2RPDm5WF3/gN9dDB33E3Y6uuMrde74Fi+DVrTXmdfzR7bKfopTg6GAxV3uxAZZyPEIMuVjy/HKcUZEQ+KXhyu04Rz X-Gm-Message-State: AOJu0YyRjnHF6z1F7Ks54uYgqPrF17R62tAC4Ec/jxoj6vcntAxY6IAE 0YTDx4MwDVO0t00fOTO+taWBwOTjbVdO9Dyl9JT6tL2NP3dsK+MJ X-Google-Smtp-Source: AGHT+IFwPvT1OtdGueRSQFyO5EPBWBYCBI6JUDjn7Na8MaSMl7ncGpOq/vDZ1s9ZmiYRbzeHQRxSzg== X-Received: by 2002:a17:907:3ce:b0:a59:9b75:b8c with SMTP id su14-20020a17090703ce00b00a599b750b8cmr573837ejb.14.1714740080942; Fri, 03 May 2024 05:41:20 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:20 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 3/7] refs: support symrefs in 'reference-transaction' hook Date: Fri, 3 May 2024 14:41:11 +0200 Message-ID: <20240503124115.252413-4-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The 'reference-transaction' hook runs whenever a reference update is made to the system. In a previous commit, we added the `old_target` and `new_target` fields to the `reference_transaction_update()`. In following commits we'll also add the code to handle symref's in the reference backends. Support symrefs also in the 'reference-transaction' hook, by modifying the current format: SP SP LF to be be: SP SP LF where for regular refs the output would not change and remain the same. But when either 'old-value' or 'new-value' is a symref, we print the ref as 'ref:'. This does break backward compatibility, but the 'reference-transaction' hook's documentation always stated that support for symbolic references may be added in the future. We do not add any tests in this commit since there is no git command which activates this flow, in an upcoming commit, we'll start using transaction based symref updates as the default, we'll add tests there for the hook too. Signed-off-by: Karthik Nayak --- Documentation/githooks.txt | 14 +++++++++----- refs.c | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index ee9b92c90d..06e997131b 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -486,7 +486,7 @@ reference-transaction This hook is invoked by any Git command that performs reference updates. It executes whenever a reference transaction is prepared, committed or aborted and may thus get called multiple times. The hook -does not cover symbolic references (but that may change in the future). +also supports symbolic reference updates. The hook takes exactly one argument, which is the current state the given reference transaction is in: @@ -503,16 +503,20 @@ given reference transaction is in: For each reference update that was added to the transaction, the hook receives on standard input a line of the format: - SP SP LF + SP SP LF -where `` is the old object name passed into the reference -transaction, `` is the new object name to be stored in the +where `` is the old object name passed into the reference +transaction, `` is the new object name to be stored in the ref and `` is the full name of the ref. When force updating the reference regardless of its current value or when the reference is -to be created anew, `` is the all-zeroes object name. To +to be created anew, `` is the all-zeroes object name. To distinguish these cases, you can inspect the current value of `` via `git rev-parse`. +For symbolic reference updates the `` and `` +fields could denote references instead of objects. A reference will be +denoted with a 'ref:' prefix, like `ref:`. + The exit status of the hook is ignored for any state except for the "prepared" state. In the "prepared" state, a non-zero exit status will cause the transaction to be aborted. The hook will not be called with diff --git a/refs.c b/refs.c index 47bc9dd103..203a101988 100644 --- a/refs.c +++ b/refs.c @@ -2350,10 +2350,22 @@ static int run_transaction_hook(struct ref_transaction *transaction, struct ref_update *update = transaction->updates[i]; strbuf_reset(&buf); - strbuf_addf(&buf, "%s %s %s\n", - oid_to_hex(&update->old_oid), - oid_to_hex(&update->new_oid), - update->refname); + + if (!(update->flags & REF_HAVE_OLD)) + strbuf_addf(&buf, "%s ", oid_to_hex(null_oid())); + else if (update->old_target) + strbuf_addf(&buf, "ref:%s ", update->old_target); + else + strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid)); + + if (!(update->flags & REF_HAVE_NEW)) + strbuf_addf(&buf, "%s ", oid_to_hex(null_oid())); + else if (update->new_target) + strbuf_addf(&buf, "ref:%s ", update->new_target); + else + strbuf_addf(&buf, "%s ", oid_to_hex(&update->new_oid)); + + strbuf_addf(&buf, "%s\n", update->refname); if (write_in_full(proc.in, buf.buf, buf.len) < 0) { if (errno != EPIPE) { From patchwork Fri May 3 12:41:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652741 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2111C15358C for ; Fri, 3 May 2024 12:41:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740086; cv=none; b=h8AjMnN0RX1uD2bCTnFG8i2KiQRxvi3YIAr6H7S/igwJPohGCCyoIYm34nG8XL7WtYMp0VSdPM1jxr6SITayt/6SflMlaDNuTLvt4OuDpS3sbZ3hp3CMav298FssNZFVENBqHcZqf7DgsfRglBjwbhjN0n6y33QtidPslVzWj7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740086; c=relaxed/simple; bh=Kdac1qCpv3UB/wooFqvLJSxfaEvGVimfD4bjGpIcbB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b/qxa9aj4T6CYoNAKw1AAFa36ooxP22fV7XODG2PreRn2NGaML4KvIcuaOtu7Yi9wnbwKcS3DyblDtaWJwkYdWD/GY1yrramg7LlWbBL+3soWv2ngzFG1usJwjriGa6o+FIrsBMQZNqiqI+k5Tvlcix690GhMDu8pA4kfMPVyxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Y1ghAnMi; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y1ghAnMi" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a58fc650f8fso848455166b.1 for ; Fri, 03 May 2024 05:41:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740082; x=1715344882; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DqLqPassxpwBAxYFxgDxSiwECD7TJs4WuXQZV6qmn3Y=; b=Y1ghAnMiKecbtwM/Q85PZRb7t0KPrffkUfTVqDq4vP1CUJP5DW/CNkDjvUHn/67+Zd JCLF+Ya2VmnlsIyrnZMvNNjStYPMb3Du+zb6NS1tTVTP8QDUrrFhHd8o/YlJnR8Wok5i x/wOixdRawi3Gg9+DZK0+pvSnx5WD7hMKZXbZ/wrIQ6mrZE/6L8NWDbg1SvBIcDlYdo4 cJmiRDZ1XwI5WJtSKBRckDCKq+tBbmVkRR+4EYcfyGkIq/A7eR9nx7kW3blnyj9PkC4J joCvXLidCfMOm3DDWwP3HAJ9rW5quFL6K18FTDVPglHVGvZrPiza0MIYoUqnekae75aJ x4rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740082; x=1715344882; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DqLqPassxpwBAxYFxgDxSiwECD7TJs4WuXQZV6qmn3Y=; b=hqDBqmGoEo8KlRC95Qqv4g5RRwol5VHD166JCkhLZwKCiDsvIdFllGOvBNpVznXLLM 9tzCL//3G2r2gALVh5kxn+p4wL5p478mx6HAVhRmNbe9jzF+lMk3CJF6KO21Hs8tA9Bv 9edQGHm0ol+NWK75acqD2JzaPHYAJngcc9Ht1+2MWoWuhvYq6vL+0YnQlgq8asm3fzGZ QdyDpo1siW7MpRxtvTrxt1bo6JY1MgE/aTv7VzqKvUlRClZNdUZRLOHz39yqTul43Lwx eSBAbBnBlAwCVjruypDSNQUPl2ldrZf2LriUxYn5v7G8/HJ3w80Ov1Di877mwKnN9fvt cYrA== X-Forwarded-Encrypted: i=1; AJvYcCXw4YVoa2R8FQsrS/SvPKpC8+1PsiFEodDB0Q6dXE5L7tzOKlirj2cW4xp/kOar9+MAna6RubdchCNWq03016IMuOUJ X-Gm-Message-State: AOJu0YxN0ZqXgksHEb/xMhiftNyiv/ZzH3K4UU3fJH6mYiaxAphVjEo8 vc/d0KP/pGzadMI8eHAk3H46/0HBp4PN/CLOkDdrtR1/hlKJs0e4 X-Google-Smtp-Source: AGHT+IEyImSp96s1kOgn9IutfwbWS4qqMATw7msuLcd+8lubxnFwsCZ2N3ZPwhDvOia7F3d7Vy+j7Q== X-Received: by 2002:a17:907:762f:b0:a59:9c4d:da39 with SMTP id jy15-20020a170907762f00b00a599c4dda39mr420856ejc.45.1714740082272; Fri, 03 May 2024 05:41:22 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:21 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 4/7] refs: add support for transactional symref updates Date: Fri, 3 May 2024 14:41:12 +0200 Message-ID: <20240503124115.252413-5-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The reference backends currently support transactional reference updates. While this is exposed to users via 'git-update-ref' and its '--stdin' mode, it is also used internally within various commands. However, we never supported transactional updates of symrefs. Let's add support for symrefs in both the 'files' and the 'reftable' backend. Here, we add and use `ref_update_has_null_new_value()`, a helper function which is used to check if there is a new_value in a reference update. The new value could either be a symref target `new_target` or a OID `new_oid`. With this, now transactional updates (verify, create, delete, update) can be used for: - regular refs - symbolic refs - conversion of regular to symbolic refs and vice versa This also allows us to expose this to users via new commands in 'git-update-ref' in the future. Note that a dangling symref update does not record a new reflog entry, which is unchanged before and after this commit. Signed-off-by: Karthik Nayak --- refs.c | 15 ++++- refs/files-backend.c | 137 ++++++++++++++++++++++++++++++++++------ refs/refs-internal.h | 7 ++ refs/reftable-backend.c | 77 +++++++++++++++++----- 4 files changed, 198 insertions(+), 38 deletions(-) diff --git a/refs.c b/refs.c index 203a101988..acb153b6f8 100644 --- a/refs.c +++ b/refs.c @@ -1217,6 +1217,8 @@ void ref_transaction_free(struct ref_transaction *transaction) for (i = 0; i < transaction->nr; i++) { free(transaction->updates[i]->msg); + free((char *)transaction->updates[i]->new_target); + free((char *)transaction->updates[i]->old_target); free(transaction->updates[i]); } free(transaction->updates); @@ -1247,10 +1249,13 @@ struct ref_update *ref_transaction_add_update( update->flags = flags; - if (flags & REF_HAVE_NEW) + update->new_target = xstrdup_or_null(new_target); + update->old_target = xstrdup_or_null(old_target); + if ((flags & REF_HAVE_NEW) && new_oid) oidcpy(&update->new_oid, new_oid); - if (flags & REF_HAVE_OLD) + if ((flags & REF_HAVE_OLD) && old_oid) oidcpy(&update->old_oid, old_oid); + update->msg = normalize_reflog_message(msg); return update; } @@ -1286,6 +1291,7 @@ int ref_transaction_update(struct ref_transaction *transaction, flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); + flags |= (new_target ? REF_HAVE_NEW : 0) | (old_target ? REF_HAVE_OLD : 0); ref_transaction_add_update(transaction, refname, flags, new_oid, old_oid, new_target, @@ -2814,3 +2820,8 @@ int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg { return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); } + +int ref_update_has_null_new_value(struct ref_update *update) +{ + return !update->new_target && is_null_oid(&update->new_oid); +} diff --git a/refs/files-backend.c b/refs/files-backend.c index 74a713090c..e1f0ca74c0 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -2434,6 +2434,36 @@ static const char *original_update_refname(struct ref_update *update) return update->refname; } +/* + * Check whether the old_target values stored in update are consistent + * with current_target, which is the symbolic reference's current value. + * If everything is OK, return 0; otherwise, write an error message to + * err and return -1. + */ +static int check_old_target(struct ref_update *update, + const char *current_target, + struct strbuf *err) +{ + if (!update->old_target) + BUG("called without old_target set"); + + if (!strcmp(update->old_target, current_target)) + return 0; + + if (!strcmp(current_target, "")) + strbuf_addf(err, "cannot lock ref '%s': " + "reference is missing but expected %s", + original_update_refname(update), + update->old_target); + else + strbuf_addf(err, "cannot lock ref '%s': " + "is at %s but expected %s", + original_update_refname(update), + current_target, update->old_target); + + return -1; +} + /* * Check whether the REF_HAVE_OLD and old_oid values stored in update * are consistent with oid, which is the reference's current value. If @@ -2494,7 +2524,7 @@ static int lock_ref_for_update(struct files_ref_store *refs, files_assert_main_repository(refs, "lock_ref_for_update"); - if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid)) + if ((update->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(update)) update->flags |= REF_DELETING; if (head_ref) { @@ -2537,7 +2567,14 @@ static int lock_ref_for_update(struct files_ref_store *refs, ret = TRANSACTION_GENERIC_ERROR; goto out; } - } else if (check_old_oid(update, &lock->old_oid, err)) { + } + + if (update->old_target) { + if (check_old_target(update, referent.buf, err)) { + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + } else if (check_old_oid(update, &lock->old_oid, err)) { ret = TRANSACTION_GENERIC_ERROR; goto out; } @@ -2558,7 +2595,17 @@ static int lock_ref_for_update(struct files_ref_store *refs, } else { struct ref_update *parent_update; - if (check_old_oid(update, &lock->old_oid, err)) { + /* + * Even if the ref is a regular ref, if `old_target` is set, we + * check the referent value. Ideally `old_target` should only + * be set for symrefs, but we're strict about its usage. + */ + if (update->old_target) { + if (check_old_target(update, referent.buf, err)) { + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + } else if (check_old_oid(update, &lock->old_oid, err)) { ret = TRANSACTION_GENERIC_ERROR; goto out; } @@ -2576,9 +2623,27 @@ static int lock_ref_for_update(struct files_ref_store *refs, } } - if ((update->flags & REF_HAVE_NEW) && - !(update->flags & REF_DELETING) && - !(update->flags & REF_LOG_ONLY)) { + if (update->new_target && !(update->flags & REF_LOG_ONLY)) { + if (create_symref_lock(refs, lock, update->refname, update->new_target, err)) { + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + + if (close_ref_gently(lock)) { + strbuf_addf(err, "couldn't close '%s.lock'", + update->refname); + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + + /* + * Once we have created the symref lock, the commit + * phase of the transaction only needs to commit the lock. + */ + update->flags |= REF_NEEDS_COMMIT; + } else if ((update->flags & REF_HAVE_NEW) && + !(update->flags & REF_DELETING) && + !(update->flags & REF_LOG_ONLY)) { if (!(update->type & REF_ISSYMREF) && oideq(&lock->old_oid, &update->new_oid)) { /* @@ -2841,6 +2906,43 @@ static int files_transaction_prepare(struct ref_store *ref_store, return ret; } +static int parse_and_write_reflog(struct files_ref_store *refs, + struct ref_update *update, + struct ref_lock *lock, + struct strbuf *err) +{ + if (update->new_target) { + /* + * We want to get the resolved OID for the target, to ensure + * that the correct value is added to the reflog. + */ + if (!refs_resolve_ref_unsafe(&refs->base, update->new_target, + RESOLVE_REF_READING, + &update->new_oid, NULL)) { + /* + * TODO: currently we skip creating reflogs for dangling + * symref updates. It would be nice to capture this as + * zero oid updates however. + */ + return 0; + } + } + + if (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, + &update->new_oid, update->msg, update->flags, err)) { + char *old_msg = strbuf_detach(err, NULL); + + strbuf_addf(err, "cannot update the ref '%s': %s", + lock->ref_name, old_msg); + free(old_msg); + unlock_ref(lock); + update->backend_data = NULL; + return -1; + } + + return 0; +} + static int files_transaction_finish(struct ref_store *ref_store, struct ref_transaction *transaction, struct strbuf *err) @@ -2871,23 +2973,20 @@ static int files_transaction_finish(struct ref_store *ref_store, if (update->flags & REF_NEEDS_COMMIT || update->flags & REF_LOG_ONLY) { - if (files_log_ref_write(refs, - lock->ref_name, - &lock->old_oid, - &update->new_oid, - update->msg, update->flags, - err)) { - char *old_msg = strbuf_detach(err, NULL); - - strbuf_addf(err, "cannot update the ref '%s': %s", - lock->ref_name, old_msg); - free(old_msg); - unlock_ref(lock); - update->backend_data = NULL; + if (parse_and_write_reflog(refs, update, lock, err)) { ret = TRANSACTION_GENERIC_ERROR; goto cleanup; } } + + /* + * We try creating a symlink, if that succeeds we continue to the + * next update. If not, we try and create a regular symref. + */ + if (update->new_target && prefer_symlink_refs) + if (!create_ref_symlink(lock, update->new_target)) + continue; + if (update->flags & REF_NEEDS_COMMIT) { clear_loose_ref_cache(refs); if (commit_ref(lock)) { diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 108f4ec419..03eda70ea4 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -749,4 +749,11 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo, */ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store); +/* + * Helper function to check if the new value is null, this + * takes into consideration that the update could be a regular + * ref or a symbolic ref. + */ +int ref_update_has_null_new_value(struct ref_update *update); + #endif /* REFS_REFS_INTERNAL_H */ diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 6104471199..54c4d8b771 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -856,7 +856,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * There is no need to write the reference deletion * when the reference in question doesn't exist. */ - if (u->flags & REF_HAVE_NEW && !is_null_oid(&u->new_oid)) { + if ((u->flags & REF_HAVE_NEW) && !ref_update_has_null_new_value(u)) { ret = queue_transaction_update(refs, tx_data, u, ¤t_oid, err); if (ret) @@ -907,8 +907,10 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * intertwined with the locking in files-backend.c. */ new_update = ref_transaction_add_update( - transaction, referent.buf, new_flags, - &u->new_oid, &u->old_oid, NULL, NULL, u->msg); + transaction, referent.buf, new_flags, + &u->new_oid, &u->old_oid, u->new_target, + u->old_target, u->msg); + new_update->parent_update = u; /* @@ -938,7 +940,22 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * individual refs. But the error messages match what the files * backend returns, which keeps our tests happy. */ - if (u->flags & REF_HAVE_OLD && !oideq(¤t_oid, &u->old_oid)) { + if (u->old_target) { + if (strcmp(referent.buf, u->old_target)) { + if (!strcmp(referent.buf, "")) + strbuf_addf(err, "verifying symref target: '%s': " + "reference is missing but expected %s", + original_update_refname(u), + u->old_target); + else + strbuf_addf(err, "verifying symref target: '%s': " + "is at %s but expected %s", + original_update_refname(u), + referent.buf, u->old_target); + ret = -1; + goto done; + } + } else if ((u->flags & REF_HAVE_OLD) && !oideq(¤t_oid, &u->old_oid)) { if (is_null_oid(&u->old_oid)) strbuf_addf(err, _("cannot lock ref '%s': " "reference already exists"), @@ -1043,7 +1060,7 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data * - `core.logAllRefUpdates` tells us to create the reflog for * the given ref. */ - if (u->flags & REF_HAVE_NEW && !(u->type & REF_ISSYMREF) && is_null_oid(&u->new_oid)) { + if ((u->flags & REF_HAVE_NEW) && !(u->type & REF_ISSYMREF) && ref_update_has_null_new_value(u)) { struct reftable_log_record log = {0}; struct reftable_iterator it = {0}; @@ -1084,24 +1101,50 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data (u->flags & REF_FORCE_CREATE_REFLOG || should_write_log(&arg->refs->base, u->refname))) { struct reftable_log_record *log; + int create_reflog = 1; + + if (u->new_target) { + if (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target, + RESOLVE_REF_READING, &u->new_oid, NULL)) { + /* + * TODO: currently we skip creating reflogs for dangling + * symref updates. It would be nice to capture this as + * zero oid updates however. + */ + create_reflog = 0; + } + } - ALLOC_GROW(logs, logs_nr + 1, logs_alloc); - log = &logs[logs_nr++]; - memset(log, 0, sizeof(*log)); - - fill_reftable_log_record(log); - log->update_index = ts; - log->refname = xstrdup(u->refname); - memcpy(log->value.update.new_hash, u->new_oid.hash, GIT_MAX_RAWSZ); - memcpy(log->value.update.old_hash, tx_update->current_oid.hash, GIT_MAX_RAWSZ); - log->value.update.message = - xstrndup(u->msg, arg->refs->write_options.block_size / 2); + if (create_reflog) { + ALLOC_GROW(logs, logs_nr + 1, logs_alloc); + log = &logs[logs_nr++]; + memset(log, 0, sizeof(*log)); + + fill_reftable_log_record(log); + log->update_index = ts; + log->refname = xstrdup(u->refname); + memcpy(log->value.update.new_hash, u->new_oid.hash, GIT_MAX_RAWSZ); + memcpy(log->value.update.old_hash, tx_update->current_oid.hash, GIT_MAX_RAWSZ); + log->value.update.message = + xstrndup(u->msg, arg->refs->write_options.block_size / 2); + } } if (u->flags & REF_LOG_ONLY) continue; - if (u->flags & REF_HAVE_NEW && is_null_oid(&u->new_oid)) { + if (u->new_target) { + struct reftable_ref_record ref = { + .refname = (char *)u->refname, + .value_type = REFTABLE_REF_SYMREF, + .value.symref = (char *)u->new_target, + .update_index = ts, + }; + + ret = reftable_writer_add_ref(writer, &ref); + if (ret < 0) + goto done; + } else if ((u->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(u)) { struct reftable_ref_record ref = { .refname = (char *)u->refname, .update_index = ts, From patchwork Fri May 3 12:41:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652742 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A99B2153827 for ; Fri, 3 May 2024 12:41:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740087; cv=none; b=POmuezAOOHdk0mrw9km8f8ABmxTM6dwvO3RYubAtw0wSy8DKfPv9wsauO+xQK8pEs/kjmb5CPgTxhNkBKKO44kiQexXJ3TFTdqIJVdejOGDPJ59kZWEVK/HIQ7n+nTB7+vnoeqsgkSuKHjEol6sJ40MyRzqnb3YpKDIEbkN7XOs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740087; c=relaxed/simple; bh=x5NJzEm/l+tdpypU2YQeD1LsrlF6/p0kmKnaRHbXr+8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=md93mEiXL2nsz0hqMR3bzkxnhVP6ZNNcfzh0YOiLmbhfQpSiYW70bTZqtnafS4CgkmOSJwSRUvvsqnhhlWmXUL9wUvkFFgpbAYHcVYi1xUHsqCYKMZJAse7BzLQddjmbTujpiAcWzmy6N1VUvvBoOmLebR88vxDojUemiMY47fg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LFTpE1Eo; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LFTpE1Eo" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a58fbbcd77aso871365366b.2 for ; Fri, 03 May 2024 05:41:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740083; x=1715344883; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qOXZV2jE+XCtep9w+nE1i/SMX3mRLFWlzoISh0VSnYg=; b=LFTpE1EodAIC/ixqbP3o0MB7sqS2dfX9zKSdGyzNGEOsf7MXdGxSyxamw2a/Et9Y0s ykM/Fl9H+ke6jwYoax7DzHXWL6+f94UUTrNkLxQuX1Uy4PVlXhaaeaiLpX2q4TffkSqT B3KMWCj/ry63zGr7XfexDkwZkA8yADrx9GQT6lWentdwdrLD8XY3IEhJSyuNPILXeSBT O20Jwj1FklLFNrJz94CXmm8nMWe8m1BKHdHN7wF5pGZZuWzjaUDEeqQ2mqpMmhQDFpb0 RJD4GCuQVND7lo2FREaCHc2xt5yyKJnSCAMavokHd68iEXHRDCs/cYsHXne7zwuEMCCQ eagg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740083; x=1715344883; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qOXZV2jE+XCtep9w+nE1i/SMX3mRLFWlzoISh0VSnYg=; b=Qyjngak4cYEm60uHdsH/FjJ1MuqDMR0L7RRA7vZq7GKi10mzDhWfIOE5RO71v80+PN 7n08a6rRhQNOL0cjg2BECj2PZkO47DHbRUZtf8Dx2MtEbF87DIdO97VrXy6PO4Z8ACK7 aV+YcsPorfgebkyXK9mgNiSsjsoA9Z8tjJcWFvwTZNiqeTNkA3UTNHaznBkQBq5HdIYr HYlWjCo+lwInoWYSlt7F8MNHy34ROm5Te2Tk8W/LIwR5wijF3kRmQZbxjuRRPnWxSEdH xHVrsjamcEzNPD61o1ioouTeg9eCzGhTM987KFaay9aCLk79RDFMIRvbeb/LZz9YM4J+ eNmQ== X-Forwarded-Encrypted: i=1; AJvYcCVs6UXmFdjhe+sKr4LhBTs53KqUqPjyGzimVT1eAWomIt/DEbxnuQA1Kd50t9QHt7qs5szCeyR7t0iZBIJnmObOn7M5 X-Gm-Message-State: AOJu0Yy7yaildhT05Z5BsOSm+KW48JPIHE45Tb29c+crCSQ1jWUG5sn5 0ufxBf1MN+oeXvNwKeJDk0jx/m9DGJ1D/O4MTcPIIm/5tMV9gu3f X-Google-Smtp-Source: AGHT+IEGqU8hQ2dfMuGtEP8GfPhzCOB0xmN1A2pGOkY21PGR6N/ZKYqK5xpDwzCEo/PygHTYTB3Sag== X-Received: by 2002:a17:907:101a:b0:a58:84e9:c669 with SMTP id ox26-20020a170907101a00b00a5884e9c669mr1701543ejb.52.1714740083041; Fri, 03 May 2024 05:41:23 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:22 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 5/7] refs: use transaction in `refs_create_symref()` Date: Fri, 3 May 2024 14:41:13 +0200 Message-ID: <20240503124115.252413-6-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The `refs_create_symref()` function updates a symref to a given new target. To do this, it uses a ref-backend specific function `create_symref()`. In the previous commits, we introduced symref support in transactions. This means we can now use transactions to perform symref updates and don't have to resort to `create_symref()`. Doing this allows us to remove and cleanup `create_symref()`, which we will do in the following commit. Modify the expected error message for a test in 't/t0610-reftable-basics.sh', since the error is now thrown from 'refs.c'. This is because in transactional updates, F/D conflicts are caught before we're in the reference backend. Signed-off-by: Karthik Nayak --- refs.c | 24 +++++++++++++++++------- t/t0610-reftable-basics.sh | 2 +- t/t1416-ref-transaction-hooks.sh | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/refs.c b/refs.c index acb153b6f8..195861b09b 100644 --- a/refs.c +++ b/refs.c @@ -2289,14 +2289,24 @@ int refs_create_symref(struct ref_store *refs, const char *refs_heads_master, const char *logmsg) { - char *msg; - int retval; + struct ref_transaction *transaction; + struct strbuf err = STRBUF_INIT; + int ret = 0; - msg = normalize_reflog_message(logmsg); - retval = refs->be->create_symref(refs, ref_target, refs_heads_master, - msg); - free(msg); - return retval; + transaction = ref_store_transaction_begin(refs, &err); + if (!transaction || + ref_transaction_update(transaction, ref_target, NULL, NULL, + refs_heads_master, NULL, REF_NO_DEREF, + logmsg, &err) || + ref_transaction_commit(transaction, &err)) { + ret = error("%s", err.buf); + } + + strbuf_release(&err); + if (transaction) + ref_transaction_free(transaction); + + return ret; } int create_symref(const char *ref_target, const char *refs_heads_master, diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh index 178791e086..9e8d22bcbd 100755 --- a/t/t0610-reftable-basics.sh +++ b/t/t0610-reftable-basics.sh @@ -286,7 +286,7 @@ test_expect_success 'ref transaction: creating symbolic ref fails with F/D confl git init repo && test_commit -C repo A && cat >expect <<-EOF && - error: unable to write symref for refs/heads: file/directory conflict + error: ${SQ}refs/heads/main${SQ} exists; cannot create ${SQ}refs/heads${SQ} EOF test_must_fail git -C repo symbolic-ref refs/heads refs/heads/foo 2>err && test_cmp expect err diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index 2092488090..067fd57290 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -134,4 +134,27 @@ test_expect_success 'interleaving hook calls succeed' ' test_cmp expect target-repo.git/actual ' +test_expect_success 'hook captures git-symbolic-ref updates' ' + test_when_finished "rm actual" && + + test_hook reference-transaction <<-\EOF && + echo "$*" >>actual + while read -r line + do + printf "%s\n" "$line" + done >>actual + EOF + + git symbolic-ref refs/heads/symref refs/heads/main && + + cat >expect <<-EOF && + prepared + $ZERO_OID ref:refs/heads/main refs/heads/symref + committed + $ZERO_OID ref:refs/heads/main refs/heads/symref + EOF + + test_cmp expect actual +' + test_done From patchwork Fri May 3 12:41:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652743 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AFD715219B for ; Fri, 3 May 2024 12:41:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740087; cv=none; b=GuirkctFAXzkqslEqBMutbdWYOl64WtaQkeM1YO3jt+Ri4YpxF6XNu47mqTL3M56ViWuPlkBfVUWYSxTl4u+MjdrrJxqpgwsXxNnXYLqzEe7Z7IE2nO3XgFN+Rg+qhParItTB+ZyDmOupwIzY941Gy6gHMi2EC+IjCgcq+BrZo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740087; c=relaxed/simple; bh=M8uZzTWP5riag+MhzZOFXsg1MdCr3znkpwZSnVjr9Hk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rJXYVokxXi/Obr2xH1CNhaG+6wsFQLIHpMdm/XYNOYOtlW+4ysiEdCFCLN5FMyP9wDfBxe0ScX6+oxyecHPRCmSkM/m80SArKC2cM1DDxqhOiE1jV913Ik9VLWriASzsNHsK8mURfoFuwiHYw5w/ZosngdMHK/WlaxJPSgRQFYo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gIaL1Zca; arc=none smtp.client-ip=209.85.218.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gIaL1Zca" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a51addddbd4so1137497666b.0 for ; Fri, 03 May 2024 05:41:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740084; x=1715344884; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nqhgek0HElC0rAfv9jQjHsyARw2W/VzMAuJ6Bo3w27s=; b=gIaL1Zca1oek/PoHhS3wntFll61SAwXYxBbr3fzDdDeIGAtIdf0Jg9VxFaS4CH1Vsg q/bGyBJbiBifydM8WnCpN3+ccbVV9f+ARGDIYIn53uipNkSMgSt/PSLK7F2FYIkTKt0Q EU7q5gt0ieXw+BsSHEMWHyoey2Sdz7CowAytjlPxi2tspybFggURTk660yog4uVo8ljA SMb+4h84CBAzsfQm79euDeJDous1VQ0kb2yU5sX07YwQvc+mdrZCcrYJI+vh9sWly8na TPxmO1UNiitvoIju+aYa8MWJNb2Au61eGrRB/3lvf8AWC1/Kr2at4FHmcde2iBEfypMo ZC6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740084; x=1715344884; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nqhgek0HElC0rAfv9jQjHsyARw2W/VzMAuJ6Bo3w27s=; b=PnUSM6PAF0x2KQBVweJx9Ygsjlz/psCMrldYwMVniJf+RBkVi8fepWOaIP02ibWVRY W3tzutME/XIokGEqyEHF+pNvnci5bCn3dGYFg/VFzYcdYyychyMtxxLBnUKA315U5UFE eOodmw63xxSIv1SqgiRrTJaeZJm5jXbnJHhckTnuh4JEfz2wU9shPcgbob1uP5FkAgit loNEA/G4xJasYqp6Qvfei8ve+JCAwL7OMEJP0iDQS+vP3mfGWFCyDxe8eTkuXeQYJ1Uc O4EScNvBNhN3YT18F8xm+2XHsNnq8u8fhDEudXEoBE45pazQzdd61CHeX5tNhM1sMCPn sxMg== X-Forwarded-Encrypted: i=1; AJvYcCX2tnet1vFZIfhZat4FOvM7sPV7fozt809SNyvOk80A3nKy0V3Gpb8ABArMy8cv9Ae67vDbwKsjMMjQEQY9VHqojf3e X-Gm-Message-State: AOJu0Yxe2TWZYoeauhDrFHVpWiL56M/on45ts/ZRaeYDMH77kLV3HFt/ nrXDJFplZuyOEb5V6eLc9UpYUlbI2ysRsROS65fC5e5C5Wf/KEGX X-Google-Smtp-Source: AGHT+IEq/a6Y5MxOVxWGot/0Sfeixq/AltXFC9NpufWOaRj6pwleCxOF+iyVmbXTfONc4wBdzC8pQw== X-Received: by 2002:a17:906:341a:b0:a58:ed1d:93e4 with SMTP id c26-20020a170906341a00b00a58ed1d93e4mr1344489ejb.65.1714740084123; Fri, 03 May 2024 05:41:24 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:23 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 6/7] refs: rename `refs_create_symref()` to `refs_update_symref()` Date: Fri, 3 May 2024 14:41:14 +0200 Message-ID: <20240503124115.252413-7-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak The `refs_create_symref()` function is used to update/create a symref. But it doesn't check the old target of the symref, if existing. It force updates the symref. In this regard, the name `refs_create_symref()` is a bit misleading. So let's rename it to `refs_update_symref()`. This is akin to how 'git-update-ref(1)' also allows us to create apart from update. While we're here, rename the arguments in the function to clarify what they actually signify and reduce confusion. Signed-off-by: Karthik Nayak --- builtin/branch.c | 2 +- builtin/worktree.c | 2 +- refs.c | 12 +++++------- refs.h | 2 +- t/helper/test-ref-store.c | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index dd3e3a7dc0..4491f7a20c 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -555,7 +555,7 @@ static int replace_each_worktree_head_symref(struct worktree **worktrees, continue; refs = get_worktree_ref_store(worktrees[i]); - if (refs_create_symref(refs, "HEAD", newref, logmsg)) + if (refs_update_symref(refs, "HEAD", newref, logmsg)) ret = error(_("HEAD of working tree %s is not updated"), worktrees[i]->path); } diff --git a/builtin/worktree.c b/builtin/worktree.c index 7c6c72536b..480202c517 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -517,7 +517,7 @@ static int add_worktree(const char *path, const char *refname, ret = refs_update_ref(wt_refs, NULL, "HEAD", &commit->object.oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR); else - ret = refs_create_symref(wt_refs, "HEAD", symref.buf, NULL); + ret = refs_update_symref(wt_refs, "HEAD", symref.buf, NULL); if (ret) goto done; diff --git a/refs.c b/refs.c index 195861b09b..3645b805c1 100644 --- a/refs.c +++ b/refs.c @@ -2284,10 +2284,8 @@ int peel_iterated_oid(const struct object_id *base, struct object_id *peeled) return peel_object(base, peeled) ? -1 : 0; } -int refs_create_symref(struct ref_store *refs, - const char *ref_target, - const char *refs_heads_master, - const char *logmsg) +int refs_update_symref(struct ref_store *refs, const char *ref, + const char *target, const char *logmsg) { struct ref_transaction *transaction; struct strbuf err = STRBUF_INIT; @@ -2295,8 +2293,8 @@ int refs_create_symref(struct ref_store *refs, transaction = ref_store_transaction_begin(refs, &err); if (!transaction || - ref_transaction_update(transaction, ref_target, NULL, NULL, - refs_heads_master, NULL, REF_NO_DEREF, + ref_transaction_update(transaction, ref, NULL, NULL, + target, NULL, REF_NO_DEREF, logmsg, &err) || ref_transaction_commit(transaction, &err)) { ret = error("%s", err.buf); @@ -2312,7 +2310,7 @@ int refs_create_symref(struct ref_store *refs, int create_symref(const char *ref_target, const char *refs_heads_master, const char *logmsg) { - return refs_create_symref(get_main_ref_store(the_repository), ref_target, + return refs_update_symref(get_main_ref_store(the_repository), ref_target, refs_heads_master, logmsg); } diff --git a/refs.h b/refs.h index c7851bf587..71cc1c58e0 100644 --- a/refs.h +++ b/refs.h @@ -606,7 +606,7 @@ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg); -int refs_create_symref(struct ref_store *refs, const char *refname, +int refs_update_symref(struct ref_store *refs, const char *refname, const char *target, const char *logmsg); int create_symref(const char *refname, const char *target, const char *logmsg); diff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c index 82bbf6e2e6..4651e4ced7 100644 --- a/t/helper/test-ref-store.c +++ b/t/helper/test-ref-store.c @@ -118,7 +118,7 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv) const char *target = notnull(*argv++, "target"); const char *logmsg = *argv++; - return refs_create_symref(refs, refname, target, logmsg); + return refs_update_symref(refs, refname, target, logmsg); } static struct flag_definition transaction_flags[] = { From patchwork Fri May 3 12:41:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karthik Nayak X-Patchwork-Id: 13652744 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 777B3153833 for ; Fri, 3 May 2024 12:41:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740088; cv=none; b=YZ2hPDZpwIkZgcpvF/HCY9aMp9daRh58TXHPxhIJaH/XGsOp1Qeyouc6KvTD5brO17vHKD4nQAyLcvHsSIVNCpHLTyhOnYhnr5ljWc/URu0TZfuwuMIGMJu/J3fd60e4anwCFMunfiPwZM747ny+NMujwbwMjJyYYletWPCoZ24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714740088; c=relaxed/simple; bh=To+A7WGp1Q2xlRJXIf5ASSid9HGeRYxmVU7Rh1a7BNQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rldZI2QgzZ+SA9WTVewcx13e2uGlQg/YW40Ou7VIRR/NTIRKpo61QS4QS8CGvGzEYFE0fXiCbWhy7Ym59ZU3/SWMlvRqM4IA1tr2EAycx9EQev5k+z0MbkZwFjv2h+eHkqHk/177BH6jbNElrkEqFzVwMl8FKVaLInA1DoWKA1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AZr140GP; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AZr140GP" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a556d22fa93so1127967366b.3 for ; Fri, 03 May 2024 05:41:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714740085; x=1715344885; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D7qwr5qn8sTHxT7n55xU/sDDCM8q1Eq1oy5LuHFy/+Q=; b=AZr140GPvNPnBSbmL86iQyoNpf603oDF32lCGHv6dbaPHhjzBQ7ZgPGe1vU7l4lXCa X2fmCdys8LoPMvwR4s0X2Lraox7Qbm15QpdCwiIov/iXXa+iBOf3V3zDQQ71hnSfqPaM CYgb/zUAUPGKn9DlQ5svT4MTgvAlN/a97pTVSE40jlZ10wZd8f4HMuq31+7a7Ax09vzM 1al994FrlKh8ZfITOEUJrGoQIpoxAvc599FhxMgPXKx+snMJWOHPl+zLcmZ495knCrEt 9MoXpCbL3BWfnDQ2GcKN7dnGNwwP4kKS+qngIK8lyiGB/vGICzsfujqyCxaeJ5rPmGxX IH/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714740085; x=1715344885; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D7qwr5qn8sTHxT7n55xU/sDDCM8q1Eq1oy5LuHFy/+Q=; b=YLlCBqINKJp+YaC15R9GtQPZcCGXEtEMkhlPxPWq3TJgr2Ld2s/bDmpRC57IDjgc/b O6UsGhqvtLT9ewH2DC6j+TdCm2aPnI5JbOYyrc/bkiQf+D78pIjNZ0kxJnrvDQjKgsdx sPLzjSmkkWed9ScpgCFPke24lXV1M6Fg5Gl1wh3WBae2tF+WBWbpI/7ApGI40D2HMPPe Tv1NKyCxq/ZsCbFhAlaTSB02uXRNsXLn8/+DVK9Y/mnnRg7j2XuT7sB2en5sW2JYjlLT RXI9g8B3iCzo0LZNKNcgHST9RdCmuaDkDIrEDRpBnBUsphnFnk2w+Y1UURTrwewrkZ0T ue7Q== X-Forwarded-Encrypted: i=1; AJvYcCVwPde0+4ar41EP6LhQ/QQYdwgs4TjUzgqNw/0y7YNZto/rT2PNbKP3Tv7k5dcv10DO+DDzLb8hrZhgNXSVM+AuU+bl X-Gm-Message-State: AOJu0YzuoeD8blMsAzPSWXIUtv83yO8PFu0bdhMJXHmC1NBqjd0Cn/jv H7HDdn6RTAXJzxuU97OgTh+3+swWb+kssmvZKTF7GiI1WO85TgyG X-Google-Smtp-Source: AGHT+IGi3hz4C/Edf4Bv6mR0EV8+5efLP43q2TGk+cjv030D/C8zYZ0VQ10miR20WJIzTA2bsHvjGQ== X-Received: by 2002:a17:906:1555:b0:a51:8d60:215a with SMTP id c21-20020a170906155500b00a518d60215amr1510128ejd.27.1714740084916; Fri, 03 May 2024 05:41:24 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id ot38-20020a170906cce600b00a5994c5c948sm533323ejb.133.2024.05.03.05.41.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 05:41:24 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: christian.couder@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v6 7/7] refs: remove `create_symref` and associated dead code Date: Fri, 3 May 2024 14:41:15 +0200 Message-ID: <20240503124115.252413-8-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240503124115.252413-1-knayak@gitlab.com> References: <20240501202229.2695774-1-knayak@gitlab.com> <20240503124115.252413-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Karthik Nayak In the previous commits, we converted `refs_create_symref()` to utilize transactions to perform symref updates. Earlier `refs_create_symref()` used `create_symref()` to do the same. We can now remove `create_symref()` and any code associated with it which is no longer used. We remove `create_symref()` code from all the reference backends and also remove it entirely from the `ref_storage_be` struct. Signed-off-by: Karthik Nayak --- refs/debug.c | 13 ------- refs/files-backend.c | 67 -------------------------------- refs/packed-backend.c | 1 - refs/refs-internal.h | 5 --- refs/reftable-backend.c | 86 ----------------------------------------- 5 files changed, 172 deletions(-) diff --git a/refs/debug.c b/refs/debug.c index c7531b17f0..8be316bb67 100644 --- a/refs/debug.c +++ b/refs/debug.c @@ -131,18 +131,6 @@ static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *o return res; } -static int debug_create_symref(struct ref_store *ref_store, - const char *ref_name, const char *target, - const char *logmsg) -{ - struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; - int res = drefs->refs->be->create_symref(drefs->refs, ref_name, target, - logmsg); - trace_printf_key(&trace_refs, "create_symref: %s -> %s \"%s\": %d\n", ref_name, - target, logmsg, res); - return res; -} - static int debug_rename_ref(struct ref_store *ref_store, const char *oldref, const char *newref, const char *logmsg) { @@ -441,7 +429,6 @@ struct ref_storage_be refs_be_debug = { .initial_transaction_commit = debug_initial_transaction_commit, .pack_refs = debug_pack_refs, - .create_symref = debug_create_symref, .rename_ref = debug_rename_ref, .copy_ref = debug_copy_ref, diff --git a/refs/files-backend.c b/refs/files-backend.c index e1f0ca74c0..3ce260d07d 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1903,23 +1903,6 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target) return ret; } -static void update_symref_reflog(struct files_ref_store *refs, - struct ref_lock *lock, const char *refname, - const char *target, const char *logmsg) -{ - struct strbuf err = STRBUF_INIT; - struct object_id new_oid; - - if (logmsg && - refs_resolve_ref_unsafe(&refs->base, target, - RESOLVE_REF_READING, &new_oid, NULL) && - files_log_ref_write(refs, refname, &lock->old_oid, - &new_oid, logmsg, 0, &err)) { - error("%s", err.buf); - strbuf_release(&err); - } -} - static int create_symref_lock(struct files_ref_store *refs, struct ref_lock *lock, const char *refname, const char *target, struct strbuf *err) @@ -1939,55 +1922,6 @@ static int create_symref_lock(struct files_ref_store *refs, return 0; } -static int create_and_commit_symref(struct files_ref_store *refs, - struct ref_lock *lock, const char *refname, - const char *target, const char *logmsg) -{ - struct strbuf err = STRBUF_INIT; - int ret; - - if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { - update_symref_reflog(refs, lock, refname, target, logmsg); - return 0; - } - - ret = create_symref_lock(refs, lock, refname, target, &err); - if (!ret) { - update_symref_reflog(refs, lock, refname, target, logmsg); - - if (commit_ref(lock) < 0) - return error("unable to write symref for %s: %s", refname, - strerror(errno)); - } else { - return error("%s", err.buf); - } - - return ret; -} - -static int files_create_symref(struct ref_store *ref_store, - const char *refname, const char *target, - const char *logmsg) -{ - struct files_ref_store *refs = - files_downcast(ref_store, REF_STORE_WRITE, "create_symref"); - struct strbuf err = STRBUF_INIT; - struct ref_lock *lock; - int ret; - - lock = lock_ref_oid_basic(refs, refname, &err); - if (!lock) { - error("%s", err.buf); - strbuf_release(&err); - return -1; - } - - ret = create_and_commit_symref(refs, lock, refname, target, logmsg); - - unlock_ref(lock); - return ret; -} - static int files_reflog_exists(struct ref_store *ref_store, const char *refname) { @@ -3413,7 +3347,6 @@ struct ref_storage_be refs_be_files = { .initial_transaction_commit = files_initial_transaction_commit, .pack_refs = files_pack_refs, - .create_symref = files_create_symref, .rename_ref = files_rename_ref, .copy_ref = files_copy_ref, diff --git a/refs/packed-backend.c b/refs/packed-backend.c index 4e826c05ff..a937e7dbfc 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -1714,7 +1714,6 @@ struct ref_storage_be refs_be_packed = { .initial_transaction_commit = packed_initial_transaction_commit, .pack_refs = packed_pack_refs, - .create_symref = NULL, .rename_ref = NULL, .copy_ref = NULL, diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 03eda70ea4..0c620d4bce 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -566,10 +566,6 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs, typedef int pack_refs_fn(struct ref_store *ref_store, struct pack_refs_opts *opts); -typedef int create_symref_fn(struct ref_store *ref_store, - const char *ref_target, - const char *refs_heads_master, - const char *logmsg); typedef int rename_ref_fn(struct ref_store *ref_store, const char *oldref, const char *newref, const char *logmsg); @@ -690,7 +686,6 @@ struct ref_storage_be { ref_transaction_commit_fn *initial_transaction_commit; pack_refs_fn *pack_refs; - create_symref_fn *create_symref; rename_ref_fn *rename_ref; copy_ref_fn *copy_ref; diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 54c4d8b771..999b5bca21 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -1275,91 +1275,6 @@ struct write_create_symref_arg { const char *logmsg; }; -static int write_create_symref_table(struct reftable_writer *writer, void *cb_data) -{ - struct write_create_symref_arg *create = cb_data; - uint64_t ts = reftable_stack_next_update_index(create->stack); - struct reftable_ref_record ref = { - .refname = (char *)create->refname, - .value_type = REFTABLE_REF_SYMREF, - .value.symref = (char *)create->target, - .update_index = ts, - }; - struct reftable_log_record log = {0}; - struct object_id new_oid; - struct object_id old_oid; - int ret; - - reftable_writer_set_limits(writer, ts, ts); - - ret = reftable_writer_add_ref(writer, &ref); - if (ret) - return ret; - - /* - * Note that it is important to try and resolve the reference before we - * write the log entry. This is because `should_write_log()` will munge - * `core.logAllRefUpdates`, which is undesirable when we create a new - * repository because it would be written into the config. As HEAD will - * not resolve for new repositories this ordering will ensure that this - * never happens. - */ - if (!create->logmsg || - !refs_resolve_ref_unsafe(&create->refs->base, create->target, - RESOLVE_REF_READING, &new_oid, NULL) || - !should_write_log(&create->refs->base, create->refname)) - return 0; - - fill_reftable_log_record(&log); - log.refname = xstrdup(create->refname); - log.update_index = ts; - log.value.update.message = xstrndup(create->logmsg, - create->refs->write_options.block_size / 2); - memcpy(log.value.update.new_hash, new_oid.hash, GIT_MAX_RAWSZ); - if (refs_resolve_ref_unsafe(&create->refs->base, create->refname, - RESOLVE_REF_READING, &old_oid, NULL)) - memcpy(log.value.update.old_hash, old_oid.hash, GIT_MAX_RAWSZ); - - ret = reftable_writer_add_log(writer, &log); - reftable_log_record_release(&log); - return ret; -} - -static int reftable_be_create_symref(struct ref_store *ref_store, - const char *refname, - const char *target, - const char *logmsg) -{ - struct reftable_ref_store *refs = - reftable_be_downcast(ref_store, REF_STORE_WRITE, "create_symref"); - struct reftable_stack *stack = stack_for(refs, refname, &refname); - struct write_create_symref_arg arg = { - .refs = refs, - .stack = stack, - .refname = refname, - .target = target, - .logmsg = logmsg, - }; - int ret; - - ret = refs->err; - if (ret < 0) - goto done; - - ret = reftable_stack_reload(stack); - if (ret) - goto done; - - ret = reftable_stack_add(stack, &write_create_symref_table, &arg); - -done: - assert(ret != REFTABLE_API_ERROR); - if (ret) - error("unable to write symref for %s: %s", refname, - reftable_error_str(ret)); - return ret; -} - struct write_copy_arg { struct reftable_ref_store *refs; struct reftable_stack *stack; @@ -2267,7 +2182,6 @@ struct ref_storage_be refs_be_reftable = { .initial_transaction_commit = reftable_be_initial_transaction_commit, .pack_refs = reftable_be_pack_refs, - .create_symref = reftable_be_create_symref, .rename_ref = reftable_be_rename_ref, .copy_ref = reftable_be_copy_ref,