From patchwork Fri Jun 7 13:32:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689972 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 72ABA19AA5B for ; Fri, 7 Jun 2024 13:33:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767192; cv=none; b=mtiT1KtlIELRGxj4Vrr/q+JuPBWXwh08hea1/dgRKS/eDnjPD/skmX282XbNxJqkoko1ouC246TMLiLSiY51YWKn/XWfDfe8Ga153Rl7X0qADKdDDmp6JnAZkw+Gou67hqgFNHyt23aF+jNbj1CW8VXp411l+N8lc3V6CH/M/Gc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767192; c=relaxed/simple; bh=t2Sh2uOYymZ8PY1NxFZ+tdvRzLosECEg/jwYIX9U8wg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rp+mmOGEsGE1PYnfXD9pxTOEfA0DdECgN/5TBKqtsSrz3zzdAnoBB3Ht2pga2tpFsdxsmVpmMj7Wd8O+DMXcB2CNoUrW9jNdijYw0FdgdL7kSxX8U44eKicJjGM5OlCqBpPOiwhZbF3CvTDpkVsQM/uXll6MPzuAhRA+1zK4Hcs= 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=GVPdWEss; arc=none smtp.client-ip=209.85.208.53 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="GVPdWEss" Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-57c60b13a56so417682a12.0 for ; Fri, 07 Jun 2024 06:33:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767188; x=1718371988; 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=o/VAe/ORVbUECRLIQtAb+P3zV3BjjcPefMr5fQ54+9M=; b=GVPdWEss7/ppWYcjV0n7qjfE7uOTI3Vh50Zaf4d7ArOsl4rqx5Mqqy79RY5UkoyM8C kXaTF5mHsaaSDUgwfrUOYf5pATJ0r/Sg09tG9lhD+uj1Ba3dSiD5GxU2Gb0FW8G5x3pD okgZDEzZfFFgPNsZq9xzZjmi+Fyaeyhg5j3pgr/OWhqVJAP95RkyXvQil3vqcSzhwtAL Hbd4skHXapfOdXIByRO7Fv3JrzvsorLzwH6/0ZjuMSiKHV+LNp+dJ5QfRDSKY8WfaKyE swhQ2eLqjgN2d94zTwirzb/sQe6ztU8C/gAjad117G0fk521IJ5B4eRBEDyoRNWcmI0G +uLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767188; x=1718371988; 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=o/VAe/ORVbUECRLIQtAb+P3zV3BjjcPefMr5fQ54+9M=; b=kNe2UNGaN/ItgJ8rF28r/BkSSPiG9j/dTlSi8ha5xrBs2U5bowC9lztUW3K4vBRqo6 fotd0nRv+97NMi18lYwIs6eOB7iftUq6nyDGj2MRYBJWKaWNxzNaly9rOJr+qps4Y+VV rFBlIeXXsvUnQlUO55u1AcalNO1/CC1PJ3qjhv5Ssmd4+NNXCpUMsGqkB/ApyZ4lRqjK 8zm2s5Xkru0UcOKdYJJ1C6+vZZaRLMu/wsoN6cKF9DpWo6Lnor46rJs2slSom5skqsxI lp8T45h1vzRnteb8IbJ4ejkQLEjo0zBPwLf4rgpPGNBYoMUPSZxljE+NG89v6OHq7c7S dESw== X-Gm-Message-State: AOJu0YxHkhRB0EL7MmmYCiVXEKMiP6bJYXUg2PgUklPgo8ZwWfsMkr2b p1HpZPrnnR8VD93uWTMq4Hh5lOgyoARUsfXo7Dm3VtPX/RRQ+Yi5HqQ9LJph X-Google-Smtp-Source: AGHT+IFQ8eOGtpIaCBfOPJ20VudUroU3iW6VyCgW4NGnGI3x+FmIx9GEoS8pcCrz3VJoVB9jQBKvNg== X-Received: by 2002:a50:d79c:0:b0:57a:259a:489a with SMTP id 4fb4d7f45d1cf-57c508921b8mr1623650a12.14.1717767187356; Fri, 07 Jun 2024 06:33:07 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:06 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 1/7] refs: create and use `ref_update_expects_existing_old_ref()` Date: Fri, 7 Jun 2024 15:32:58 +0200 Message-ID: <20240607133304.2333280-2-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 files and reftable backend, need to check if a ref must exist, so that the required validation can be done. A ref must exist only when the `old_oid` value of the update has been explicitly set and it is not the `null_oid` value. Since we also support symrefs now, we need to ensure that even when `old_target` is set a ref must exist. While this was missed when we added symref support in transactions, there are no active users of this path. As we introduce the 'symref-verify' command in the upcoming commits, it is important to fix this. So let's export this to a function called `ref_update_expects_existing_old_ref()` and expose it internally via 'refs-internal.h'. Helped-by: Patrick Steinhardt Signed-off-by: Karthik Nayak --- refs.c | 6 ++++++ refs/files-backend.c | 3 +-- refs/refs-internal.h | 6 ++++++ refs/reftable-backend.c | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/refs.c b/refs.c index 8260c27cde..50d8d7d777 100644 --- a/refs.c +++ b/refs.c @@ -2679,3 +2679,9 @@ int ref_update_check_old_target(const char *referent, struct ref_update *update, referent, update->old_target); return -1; } + +int ref_update_expects_existing_old_ref(struct ref_update *update) +{ + return (update->flags & REF_HAVE_OLD) && + (!is_null_oid(&update->old_oid) || update->old_target); +} diff --git a/refs/files-backend.c b/refs/files-backend.c index 5f3089d947..194e74eb4d 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -2412,8 +2412,7 @@ static int lock_ref_for_update(struct files_ref_store *refs, struct strbuf *err) { struct strbuf referent = STRBUF_INIT; - int mustexist = (update->flags & REF_HAVE_OLD) && - !is_null_oid(&update->old_oid); + int mustexist = ref_update_expects_existing_old_ref(update); int ret = 0; struct ref_lock *lock; diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 53a6c5d842..ee298ec0d5 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -765,4 +765,10 @@ int ref_update_has_null_new_value(struct ref_update *update); int ref_update_check_old_target(const char *referent, struct ref_update *update, struct strbuf *err); +/* + * Check if the ref must exist, this means that the old_oid or + * old_target is non NULL. + */ +int ref_update_expects_existing_old_ref(struct ref_update *update); + #endif /* REFS_REFS_INTERNAL_H */ diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 1af86bbdec..b838cf8f00 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -824,7 +824,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, ¤t_oid, &referent, &u->type); if (ret < 0) goto done; - if (ret > 0 && (!(u->flags & REF_HAVE_OLD) || is_null_oid(&u->old_oid))) { + if (ret > 0 && !ref_update_expects_existing_old_ref(u)) { /* * The reference does not exist, and we either have no * old object ID or expect the reference to not exist. From patchwork Fri Jun 7 13:32:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689971 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 6F6E219AA5A for ; Fri, 7 Jun 2024 13:33:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767192; cv=none; b=j7AUHJwzoR2vBl/F6G/xzn1ykAynDRiRlDejxlivVvudMOriP4CviKtgODzYQ2Ek+5E+PVJAxVV+JW6nabZ9e6OKG1z3FFc9RUgKdNKB9QeWQ3GtnnFv+R1/v4msTGIExoPU40McPBpo1DrwiySEjZFOLvv257PiAmR0/GNj4KU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767192; c=relaxed/simple; bh=LeBg6I0inzCH069uGqVhz17t46bmb4xO3YXRPb4rsGc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DXGjy29BDN+vIMVPZ+HbF4R75DIfML7wa+mwdc9gExEkRr6CzbyIIngJdheAd4y5z9dxC8FQFkO4pNelc9IJBewIq04lvvp798EVZlckCQtSyC3CnIRdT5XdljgkO3Ah/o4EdOjp02STXPaSoxG0ACdhMMyvXCO4qexH7FTCyUA= 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=nLrQBVbu; arc=none smtp.client-ip=209.85.208.48 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="nLrQBVbu" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-579fa270e53so3211843a12.3 for ; Fri, 07 Jun 2024 06:33:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767189; x=1718371989; 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=jGDFtCyuHoAE6524CRKSHIhfq1Okh4M+NjKMp35vqwQ=; b=nLrQBVbuSeJXzwlQpW9szefDUmR1aML2OEWqcCSlzTYXjiXJlqncJksf4jePJc3m1M PuSsGDBWwC2voNnFW5iUwtKxhL/1LH8aeLirnYLZtCJ6EXED5Iv1grJMoM72dzLQap6D 8X36y1rJxQuggEil4nmPoMWkjDzy9eo+69LA+LLWEytks9kgUdyoRUwXFHI/SDpjBwV3 nkzaxaLC2jwHBvnwaGbbciCzn86s09N8PmTOW7ChPBClndXjpqoxDltU/7ZhZhMQmfIe 21dtDfmcmKSK4pt/hi7uAANqVoJ/R5vR4XALhsM26X3ZrOfREPXhPq3JWkhMxSUZoiWc AESg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767189; x=1718371989; 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=jGDFtCyuHoAE6524CRKSHIhfq1Okh4M+NjKMp35vqwQ=; b=ryffJU6qpEuaSaYoWBlHFei/3yHt/awk98r1h+jFDt6oj3J3vSeipXh9qYsi2Pziq/ sIHCz9QoS75PijUg6fMcw1aMAh+2uCNRH46q1DsOs1xWcBGi1Uc+u91qUWSSLaYUqtLE HVhZZnBMQropeHaG/DuhUpfoWlXSRu91n+QaAYzT95AlwDWlJXsqrR1P2ygcVZGDz0FG LM0T3Nbze8RbUsoI6FPH8DNhXlmrMSw/5ciYP5P2e20V9dLrVv4oTNowy9uxF3omXPg0 FLmmawz4sF9dQblxm/5DmTOZaP1lDh0vOBnnb/RS1rXISQaAAUb+c3y55Cc3pI5f5qVH HQiw== X-Gm-Message-State: AOJu0YzCz8y7bvoY/JS4KczcbBnQqgoRC92rqFK+kBBmudFDlcl078iA JCYx55ioAxMmm/RfDgnlo+ARfJrFViWDNEtdKJ7si97H/anlVWQF X-Google-Smtp-Source: AGHT+IGHLqCkMf9J95454a35WvjChqbfnpiz2C3jZ5X3/9mNlTsx4qGDbP5gvUUjQqZJK5ZumPb1mg== X-Received: by 2002:a50:9b5a:0:b0:579:7b6c:5ab3 with SMTP id 4fb4d7f45d1cf-57c508fc040mr1908139a12.22.1717767188671; Fri, 07 Jun 2024 06:33:08 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:07 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 2/7] refs: specify error for regular refs with `old_target` Date: Fri, 7 Jun 2024 15:32:59 +0200 Message-ID: <20240607133304.2333280-3-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 When a reference update tries to update a symref, but the ref in question is actually a regular ref, we raise an error. However the error raised in this situation is: verifying symref target: '': reference is missing but expected which is very generic and doesn't indicate the mismatch of types. Let's make this error more specific: cannot lock ref '': expected symref with target '': but is a regular ref so that users have a clearer understanding. Signed-off-by: Karthik Nayak --- refs/files-backend.c | 14 ++++++++------ refs/reftable-backend.c | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 194e74eb4d..fc57c9d220 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -2491,14 +2491,16 @@ static int lock_ref_for_update(struct files_ref_store *refs, /* * 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. + * fail with an error. */ if (update->old_target) { - if (ref_update_check_old_target(referent.buf, update, err)) { - ret = TRANSACTION_GENERIC_ERROR; - goto out; - } + strbuf_addf(err, _("cannot lock ref '%s': " + "expected symref with target '%s': " + "but is a regular ref"), + ref_update_original_update_refname(update), + update->old_target); + ret = TRANSACTION_GENERIC_ERROR; + goto out; } else if (check_old_oid(update, &lock->old_oid, err)) { ret = TRANSACTION_GENERIC_ERROR; goto out; diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index b838cf8f00..c66ab9ecd8 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -928,6 +928,16 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * backend returns, which keeps our tests happy. */ if (u->old_target) { + if (!(u->type & REF_ISSYMREF)) { + strbuf_addf(err, _("cannot lock ref '%s': " + "expected symref with target '%s': " + "but is a regular ref"), + ref_update_original_update_refname(u), + u->old_target); + ret = -1; + goto done; + } + if (ref_update_check_old_target(referent.buf, u, err)) { ret = -1; goto done; From patchwork Fri Jun 7 13:33:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689973 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 AF4C119AA68 for ; Fri, 7 Jun 2024 13:33:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767193; cv=none; b=t2rtsaH6RU9b31xFQ5cYyU3e+y8hX01YYE3l1zDWkO+lYPPJdL+VrKiYixLo0/KWHPcKK0+4Pm+pNnQktd+dPZupwr1CR4WTfmmd0DQ6XITsRe1LtK4c3AICQwrIcD6BbTfroQhGrJt4u55WTT8TA9cqdiFePCG3MsXn8jOMxoQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767193; c=relaxed/simple; bh=DulOYP/O2rDjF+Kq+d881KvIYFo+QkDB93eOxpKGvSM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XAKqJckxSdKvuD96cKC9yj5xXTVLlkeBFWkMpTYv9a2IDnFABe5IL0f+NW+84drEzaSLLoIsGLcH7EbD9Z0pHJ1py5niocLDIDtEm+QR2hp1DsB61gHBYXsY8q/V0GR0xFF03FxLYMJNOyZ7uhSOSuNWwZM2rOoN95WI+mnkL78= 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=lkFt+7bN; arc=none smtp.client-ip=209.85.208.48 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="lkFt+7bN" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-57a30b3a6cbso2747356a12.1 for ; Fri, 07 Jun 2024 06:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767190; x=1718371990; 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=rJvj8oLK2adoo0Kr1iYI2blFmO78bImN1wU6pM5d93A=; b=lkFt+7bNjJRFie389KVe6ZFWrje0aXjscWQNfU/99T8lxaWu7oQVfU5xZezjiRcBoK PCunio5bmap0qi74jMUkQE1xXvzMDcPeMn9DXfq8rfpIE9SQxXyOYeH52LV+1AfjucO+ EhpjAfmM09d6+Eoh7DaIeym02zwbc+ar4pEJDYfcBz+oPAKjiFKXHkO/7pYUvrQPNLm6 Qm7KPX6k0N9XexVMjbMnwEH1j6pJ73DDZEpb7/sj4voJho5mAeHHtWXAzRlYSXaXzv6k CoJORKFiFnQ8LMqVzXvTWZykqHJWWLCpeV8rev2AAA3VC5+nXhoYDFc5v2Ve3VsKpzfs pFEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767190; x=1718371990; 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=rJvj8oLK2adoo0Kr1iYI2blFmO78bImN1wU6pM5d93A=; b=X2xUKZz1Nxz/fbRPttfsl2QspsIL+Til/YihKRxAbapx9geP92/h+KinUg9+Hu7fRj P7DPTZZ8C5QlMwHEbG/w1f4Ju6QPV0lcAOT4rQkGMgG7x4zgqgrLkJ3JKmL/Y1qZD6k4 EeSRwVZLMKIKaGUl+8JTFtn2xxwl4Ye/tNrPnt6eRhK/HVt5YDCZ0ivq6fPfzbWo9mw4 KfruwisN1n8bS/uATjdi4ccFiwIVQW3IIxHsOu/rT8nlL2nAvF9/sJncq/TjFSQwGK5l L4V6eW/kbjtQYw/XeYnIJjqseqZ1ssH9/E+kpOpO/q2OlbaX0P0DcBzGDhmQ8eH3JSmZ SUvA== X-Gm-Message-State: AOJu0Yzu6s/IKig2QVlT6QbMGPSPAL4VqhyNcuWokUo8IkjPDno3iIh4 dQKXv/IWbC6clDuQAEQejkFQVPX/8/yOn3uJixMBa1bw2EW+GM4gly3oFwfg X-Google-Smtp-Source: AGHT+IGS/n5JjRQfG6yHJ6r2uS2rL/FY9C2q6MrdxSbBEYdJESQjJjW16WdQzwBV2+Bq2VEv+2d6Lg== X-Received: by 2002:a50:9359:0:b0:57c:5fcf:b570 with SMTP id 4fb4d7f45d1cf-57c5fcfb69bmr742851a12.32.1717767189793; Fri, 07 Jun 2024 06:33:09 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:09 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 3/7] update-ref: add support for 'symref-verify' command Date: Fri, 7 Jun 2024 15:33:00 +0200 Message-ID: <20240607133304.2333280-4-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 'symref-verify' command allows users to verify if a provided contains the provided without changing the . If is not provided, the command will verify that the doesn't exist. The command allows users to verify symbolic refs within a transaction, and this means users can perform a set of changes in a transaction only when the verification holds good. Since we're checking for symbolic refs, this command will only work with the 'no-deref' mode. This is because any dereferenced symbolic ref will point to an object and not a ref and the regular 'verify' command can be used in such situations. Add required tests for symref support in 'verify'. Since we're here, also add reflog checks for the pre-existing 'verify' tests, there is no divergence from behavior, but we never tested to ensure that reflog wasn't affected by the 'verify' command. Helped-by: Patrick Steinhardt Signed-off-by: Karthik Nayak --- Documentation/git-update-ref.txt | 7 +++ builtin/update-ref.c | 80 +++++++++++++++++++++++---- refs.c | 11 +++- refs.h | 1 + t/t1400-update-ref.sh | 94 +++++++++++++++++++++++++++++++- t/t1416-ref-transaction-hooks.sh | 30 ++++++++++ 6 files changed, 208 insertions(+), 15 deletions(-) diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt index 374a2ebd2b..9fe78b3501 100644 --- a/Documentation/git-update-ref.txt +++ b/Documentation/git-update-ref.txt @@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form: create SP SP LF delete SP [SP ] LF verify SP [SP ] LF + symref-verify SP [SP ] LF option SP LF start LF prepare LF @@ -86,6 +87,7 @@ quoting: create SP NUL NUL delete SP NUL [] NUL verify SP NUL [] NUL + symref-verify SP [NUL ] NUL option SP NUL start NUL prepare NUL @@ -117,6 +119,11 @@ verify:: Verify against but do not change it. If is zero or missing, the ref must not exist. +symref-verify:: + Verify symbolic against but do not change it. + If is missing, the ref must not exist. Can only be + used in `no-deref` mode. + option:: Modify the behavior of the next command naming a . The only valid option is `no-deref` to avoid dereferencing diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 6cda1c08aa..50f5472160 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -76,6 +76,29 @@ static char *parse_refname(const char **next) return strbuf_detach(&ref, NULL); } +/* + * Wrapper around parse_refname which skips the next delimiter. + */ +static char *parse_next_refname(const char **next) +{ + if (line_termination) { + /* Without -z, consume SP and use next argument */ + if (!**next || **next == line_termination) + return NULL; + if (**next != ' ') + die("expected SP but got: %s", *next); + } else { + /* With -z, read the next NUL-terminated line */ + if (**next) + return NULL; + } + /* Skip the delimiter */ + (*next)++; + + return parse_refname(next); +} + + /* * The value being parsed is (as opposed to ; the * difference affects which error messages are generated): @@ -297,11 +320,47 @@ static void parse_cmd_verify(struct ref_transaction *transaction, die("verify %s: extra input: %s", refname, next); if (ref_transaction_verify(transaction, refname, &old_oid, - update_flags, &err)) + NULL, update_flags, &err)) + die("%s", err.buf); + + update_flags = default_flags; + free(refname); + strbuf_release(&err); +} + +static void parse_cmd_symref_verify(struct ref_transaction *transaction, + const char *next, const char *end) +{ + struct strbuf err = STRBUF_INIT; + struct object_id old_oid; + char *refname, *old_target; + + if (!(update_flags & REF_NO_DEREF)) + die("symref-verify: cannot operate with deref mode"); + + refname = parse_refname(&next); + if (!refname) + die("symref-verify: missing "); + + /* + * old_ref is optional, if not provided, we need to ensure that the + * ref doesn't exist. + */ + old_target = parse_next_refname(&next); + if (!old_target) + oidcpy(&old_oid, null_oid()); + + if (*next != line_termination) + die("symref-verify %s: extra input: %s", refname, next); + + if (ref_transaction_verify(transaction, refname, + old_target ? NULL : &old_oid, + old_target, update_flags, &err)) die("%s", err.buf); update_flags = default_flags; free(refname); + free(old_target); strbuf_release(&err); } @@ -380,15 +439,16 @@ static const struct parse_cmd { unsigned args; enum update_refs_state state; } command[] = { - { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN }, - { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, - { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, - { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, - { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, - { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, - { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED }, - { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED }, - { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED }, + { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN }, + { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, + { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, + { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, + { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN }, + { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, + { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, + { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED }, + { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED }, + { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED }, }; static void update_refs_stdin(void) diff --git a/refs.c b/refs.c index 50d8d7d777..cdc4d25557 100644 --- a/refs.c +++ b/refs.c @@ -1297,14 +1297,19 @@ int ref_transaction_delete(struct ref_transaction *transaction, int ref_transaction_verify(struct ref_transaction *transaction, const char *refname, const struct object_id *old_oid, + const char *old_target, unsigned int flags, struct strbuf *err) { - if (!old_oid) - BUG("verify called with old_oid set to NULL"); + if (!old_target && !old_oid) + BUG("verify called with old_oid and old_target set to NULL"); + if (old_oid && old_target) + BUG("verify called with both old_oid and old_target set"); + if (old_target && !(flags & REF_NO_DEREF)) + BUG("verify cannot operate on symrefs with deref mode"); return ref_transaction_update(transaction, refname, NULL, old_oid, - NULL, NULL, + NULL, old_target, flags, NULL, err); } diff --git a/refs.h b/refs.h index 34568ee1fb..906299351c 100644 --- a/refs.h +++ b/refs.h @@ -736,6 +736,7 @@ int ref_transaction_delete(struct ref_transaction *transaction, int ref_transaction_verify(struct ref_transaction *transaction, const char *refname, const struct object_id *old_oid, + const char *old_target, unsigned int flags, struct strbuf *err); diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh index bbee2783ab..52801be07d 100755 --- a/t/t1400-update-ref.sh +++ b/t/t1400-update-ref.sh @@ -892,17 +892,23 @@ test_expect_success 'stdin update/create/verify combination works' ' ' test_expect_success 'stdin verify succeeds for correct value' ' + test-tool ref-store main for-each-reflog-ent $m >before && git rev-parse $m >expect && echo "verify $m $m" >stdin && git update-ref --stdin actual && - test_cmp expect actual + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent $m >after && + test_cmp before after ' test_expect_success 'stdin verify succeeds for missing reference' ' + test-tool ref-store main for-each-reflog-ent $m >before && echo "verify refs/heads/missing $Z" >stdin && git update-ref --stdin after && + test_cmp before after ' test_expect_success 'stdin verify treats no value as missing' ' @@ -1643,4 +1649,88 @@ test_expect_success PIPE 'transaction flushes status updates' ' test_cmp expected actual ' +format_command () { + if test "$1" = "-z" + then + shift + printf "$F" "$@" + else + echo "$@" + fi +} + +for type in "" "-z" +do + + test_expect_success "stdin $type symref-verify fails without --no-deref" ' + git symbolic-ref refs/heads/symref $a && + format_command $type "symref-verify refs/heads/symref" "$a" >stdin && + test_must_fail git update-ref --stdin $type err && + grep "fatal: symref-verify: cannot operate with deref mode" err + ' + + test_expect_success "stdin $type symref-verify fails with too many arguments" ' + format_command $type "symref-verify refs/heads/symref" "$a" "$a" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + if test "$type" = "-z" + then + grep "fatal: unknown command: $a" err + else + grep "fatal: symref-verify refs/heads/symref: extra input: $a" err + fi + ' + + test_expect_success "stdin $type symref-verify succeeds for correct value" ' + git symbolic-ref refs/heads/symref >expect && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >before && + format_command $type "symref-verify refs/heads/symref" "$a" >stdin && + git update-ref --stdin $type --no-deref actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >after && + test_cmp before after + ' + + test_expect_success "stdin $type symref-verify fails with no value" ' + git symbolic-ref refs/heads/symref >expect && + format_command $type "symref-verify refs/heads/symref" "" >stdin && + test_must_fail git update-ref --stdin $type --no-deref stdin && + git update-ref --stdin $type --no-deref before && + format_command $type "symref-verify refs/heads/missing" "refs/heads/unknown" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + grep "fatal: cannot lock ref ${SQ}refs/heads/missing${SQ}: unable to resolve reference ${SQ}refs/heads/missing${SQ}" err && + test_must_fail git rev-parse --verify -q refs/heads/missing && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >after && + test_cmp before after + ' + + test_expect_success "stdin $type symref-verify fails for wrong value" ' + git symbolic-ref refs/heads/symref >expect && + format_command $type "symref-verify refs/heads/symref" "$b" >stdin && + test_must_fail git update-ref --stdin $type --no-deref actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-verify fails for mistaken null value" ' + git symbolic-ref refs/heads/symref >expect && + format_command $type "symref-verify refs/heads/symref" "$Z" >stdin && + test_must_fail git update-ref --stdin $type --no-deref actual && + test_cmp expect actual + ' + +done + test_done diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index 067fd57290..fd58b902f4 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -157,4 +157,34 @@ test_expect_success 'hook captures git-symbolic-ref updates' ' test_cmp expect actual ' +test_expect_success 'hook gets all queued symref updates' ' + test_when_finished "rm actual" && + + git update-ref refs/heads/branch $POST_OID && + git symbolic-ref refs/heads/symref refs/heads/main && + + test_hook reference-transaction <<-\EOF && + echo "$*" >>actual + while read -r line + do + printf "%s\n" "$line" + done >>actual + EOF + + cat >expect <<-EOF && + prepared + ref:refs/heads/main $ZERO_OID refs/heads/symref + committed + ref:refs/heads/main $ZERO_OID refs/heads/symref + EOF + + git update-ref --no-deref --stdin <<-EOF && + start + symref-verify refs/heads/symref refs/heads/main + prepare + commit + EOF + test_cmp expect actual +' + test_done From patchwork Fri Jun 7 13:33:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689974 Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (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 293EA19AA7B for ; Fri, 7 Jun 2024 13:33:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767195; cv=none; b=i5aENjVbDagudjnZhtnzmfcvLEFfeDavypv2OWIVnLojnOmtm7oT8LRRHtuO+kOUtMDjjFJNWF12+EmOj/gTcTszpoLh70XpSDi/RHvPXdnymqvtqnYDrZF3tihL91MPAkqG+3JcKvXutM+i2Rx3LRwIIo3iJLxRlGQXbBtcXAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767195; c=relaxed/simple; bh=O2+/Q+Pfe5DyK11g9zZwE8t13U2HhLgOEVhqVIWKfbE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UHV6zAgXtvMvwk9/XzZWoV/SunAnaP+ztX9gE/WA7VEPmMXuytY2qLmn2sXxz2/J62rA1DlKbDwpmT1gACiimMf3MDW2l9rpe6JBhNRGwl1vPu5hU046XRqCRT2aDx8kA+iUfc/tKh1/nfr5+IeTsBgtite0RymqoUWsw5765/o= 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=XRRJWQSs; arc=none smtp.client-ip=209.85.208.179 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="XRRJWQSs" Received: by mail-lj1-f179.google.com with SMTP id 38308e7fff4ca-2eabd22d404so22433491fa.2 for ; Fri, 07 Jun 2024 06:33:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767191; x=1718371991; 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=nI8zy/rRUgerxKZmHDTJz4Abr5iixEv9BJDLAsh9Fec=; b=XRRJWQSsLBoPUCxTXw6KcAIl5GHl78zAGVPhA2qjHqDfo4J1ZLu8p9Apy1V5loOCjt 82oNVUEC4FhUv34cE+Plo19x+Qavf5EsqPh8Y0xFDPgS8+EpivMEJw+Y1B1J68OGLRmw qr+lVLkW6CJlmQYMUHpBuofaIhzI93ELmQYGhI4h6e4aNJaEZUqM5md+7MBKsmDOi1HR +LKEYaSqEo2XW00H84ZERgVWMdGfSOVWlm+tdvEDGsK7/dLmPTjlvHihzI7zL0UOGrJV Ycvb1K18vBP2CgREmPszHZ9iYftEWZ8k6morrvLfA8boOyEXsrlNotSXE6VhB22W1ZLb q/wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767191; x=1718371991; 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=nI8zy/rRUgerxKZmHDTJz4Abr5iixEv9BJDLAsh9Fec=; b=hF/xL5oldV/GuNMFzY/tbvu9JybORuEssW55N+RFPmbX9yT0LZdNiIdleQkeRug4xy iNpssge/Bhu6C42/LY81hJtAYRZ/yINuaJViwCWbUl+e0yK2AngnMswdlRRfLwUNN1Id 2o2dqVjFRRthuSZKsXYSfTj4+e9WT2yCxFJwuuxDiKXOF72e7WDx33wpW2nP1ooB3kAq fUYcAwxxt02efCdV1eLDxFgYsPG/CB7sr2IZsEL6GW164bcbDLub3N524jebTygq8T67 vnTYVW71DxIEzz53jv75ZgBi+SudBVVtXwSPs1e5Rq03JNcTeACh8O462WJyzA+c/QbR 650A== X-Gm-Message-State: AOJu0YxoKykqGvAtZEKHmj5r0nvSd4d/D68HMCoJqcQu9y1TLIM7EUcC 5KU1CJ2cF9jY0ITXTPKdIX4CXeLA7zRgsjmjY7PT1/3TcF6L8hxR X-Google-Smtp-Source: AGHT+IED6m3Gc7bbaGI7Qo9chjkjmCAferv65KxsUFX3NmG5OOdD8t03+phNb+RL/PBdcxQhIoK7Vg== X-Received: by 2002:a2e:a16f:0:b0:2ea:e26d:c9b5 with SMTP id 38308e7fff4ca-2eae26dca5amr9717931fa.30.1717767190962; Fri, 07 Jun 2024 06:33:10 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:10 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 4/7] update-ref: add support for 'symref-delete' command Date: Fri, 7 Jun 2024 15:33:01 +0200 Message-ID: <20240607133304.2333280-5-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 Add a new command 'symref-delete' to allow deletions of symbolic refs in a transaction via the '--stdin' mode of the 'git-update-ref' command. The 'symref-delete' command can, when given an , delete the provided only when it points to . This command is only compatible with the 'no-deref' mode because we optionally want to check the 'old_target' of the ref being deleted. De-referencing a symbolic ref would provide a regular ref and we already have the 'delete' command for regular refs. While users can also use 'git symbolic-ref -d' to delete symbolic refs, the 'symref-delete' command in 'git-update-ref' allows users to do so within a transaction, which promises atomicity of the operation and can be batched with other commands. When no 'old_target' is provided it can also delete regular refs, similar to how the 'delete' command can delete symrefs when no 'old_oid' is provided. Helped-by: Patrick Steinhardt Signed-off-by: Karthik Nayak --- Documentation/git-update-ref.txt | 5 +++ builtin/fetch.c | 4 +- builtin/receive-pack.c | 3 +- builtin/update-ref.c | 33 +++++++++++++++- refs.c | 14 +++++-- refs.h | 4 +- t/t1400-update-ref.sh | 68 ++++++++++++++++++++++++++++++++ t/t1416-ref-transaction-hooks.sh | 19 ++++++++- 8 files changed, 140 insertions(+), 10 deletions(-) diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt index 9fe78b3501..16e02f6979 100644 --- a/Documentation/git-update-ref.txt +++ b/Documentation/git-update-ref.txt @@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form: create SP SP LF delete SP [SP ] LF verify SP [SP ] LF + symref-delete SP [SP ] LF symref-verify SP [SP ] LF option SP LF start LF @@ -87,6 +88,7 @@ quoting: create SP NUL NUL delete SP NUL [] NUL verify SP NUL [] NUL + symref-delete SP [NUL ] NUL symref-verify SP [NUL ] NUL option SP NUL start NUL @@ -119,6 +121,9 @@ verify:: Verify against but do not change it. If is zero or missing, the ref must not exist. +symref-delete:: + Delete after verifying it exists with , if given. + symref-verify:: Verify symbolic against but do not change it. If is missing, the ref must not exist. Can only be diff --git a/builtin/fetch.c b/builtin/fetch.c index 75255dc600..d63100e0d3 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -1386,8 +1386,8 @@ static int prune_refs(struct display_state *display_state, if (!dry_run) { if (transaction) { for (ref = stale_refs; ref; ref = ref->next) { - result = ref_transaction_delete(transaction, ref->name, NULL, 0, - "fetch: prune", &err); + result = ref_transaction_delete(transaction, ref->name, NULL, + NULL, 0, "fetch: prune", &err); if (result) goto cleanup; } diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 01c1f04ece..0a30fac239 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1576,7 +1576,8 @@ static const char *update(struct command *cmd, struct shallow_info *si) if (ref_transaction_delete(transaction, namespaced_name, old_oid, - 0, "push", &err)) { + NULL, 0, + "push", &err)) { rp_error("%s", err.buf); ret = "failed to delete"; } else { diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 50f5472160..0cb7eef3c6 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -293,7 +293,7 @@ static void parse_cmd_delete(struct ref_transaction *transaction, if (ref_transaction_delete(transaction, refname, have_old ? &old_oid : NULL, - update_flags, msg, &err)) + NULL, update_flags, msg, &err)) die("%s", err.buf); update_flags = default_flags; @@ -301,6 +301,36 @@ static void parse_cmd_delete(struct ref_transaction *transaction, strbuf_release(&err); } + +static void parse_cmd_symref_delete(struct ref_transaction *transaction, + const char *next, const char *end) +{ + struct strbuf err = STRBUF_INIT; + char *refname, *old_target; + + if (!(update_flags & REF_NO_DEREF)) + die("symref-delete: cannot operate with deref mode"); + + refname = parse_refname(&next); + if (!refname) + die("symref-delete: missing "); + + old_target = parse_next_refname(&next); + + if (*next != line_termination) + die("symref-delete %s: extra input: %s", refname, next); + + if (ref_transaction_delete(transaction, refname, NULL, + old_target, update_flags, msg, &err)) + die("%s", err.buf); + + update_flags = default_flags; + free(refname); + free(old_target); + strbuf_release(&err); +} + + static void parse_cmd_verify(struct ref_transaction *transaction, const char *next, const char *end) { @@ -443,6 +473,7 @@ static const struct parse_cmd { { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, + { "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN }, { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN }, { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, diff --git a/refs.c b/refs.c index cdc4d25557..01f3188a09 100644 --- a/refs.c +++ b/refs.c @@ -950,7 +950,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg, transaction = ref_store_transaction_begin(refs, &err); if (!transaction || ref_transaction_delete(transaction, refname, old_oid, - flags, msg, &err) || + NULL, flags, msg, &err) || ref_transaction_commit(transaction, &err)) { error("%s", err.buf); ref_transaction_free(transaction); @@ -1283,14 +1283,20 @@ int ref_transaction_create(struct ref_transaction *transaction, int ref_transaction_delete(struct ref_transaction *transaction, const char *refname, const struct object_id *old_oid, - unsigned int flags, const char *msg, + const char *old_target, + unsigned int flags, + const char *msg, struct strbuf *err) { if (old_oid && is_null_oid(old_oid)) BUG("delete called with old_oid set to zeros"); + if (old_oid && old_target) + BUG("delete called with both old_oid and old_target set"); + if (old_target && !(flags & REF_NO_DEREF)) + BUG("delete cannot operate on symrefs with deref mode"); return ref_transaction_update(transaction, refname, null_oid(), old_oid, - NULL, NULL, flags, + NULL, old_target, flags, msg, err); } @@ -2599,7 +2605,7 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg, for_each_string_list_item(item, refnames) { ret = ref_transaction_delete(transaction, item->string, - NULL, flags, msg, &err); + NULL, NULL, flags, msg, &err); if (ret) { warning(_("could not delete reference %s: %s"), item->string, err.buf); diff --git a/refs.h b/refs.h index 906299351c..974cf4dd08 100644 --- a/refs.h +++ b/refs.h @@ -722,7 +722,9 @@ int ref_transaction_create(struct ref_transaction *transaction, int ref_transaction_delete(struct ref_transaction *transaction, const char *refname, const struct object_id *old_oid, - unsigned int flags, const char *msg, + const char *old_target, + unsigned int flags, + const char *msg, struct strbuf *err); /* diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh index 52801be07d..9dbe28bd13 100755 --- a/t/t1400-update-ref.sh +++ b/t/t1400-update-ref.sh @@ -1731,6 +1731,74 @@ do test_cmp expect actual ' + test_expect_success "stdin $type symref-delete fails without --no-deref" ' + git symbolic-ref refs/heads/symref $a && + format_command $type "symref-delete refs/heads/symref" "$a" >stdin && + test_must_fail git update-ref --stdin $type err && + grep "fatal: symref-delete: cannot operate with deref mode" err + ' + + test_expect_success "stdin $type symref-delete fails with no ref" ' + format_command $type "symref-delete " >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + grep "fatal: symref-delete: missing " err + ' + + test_expect_success "stdin $type symref-delete fails deleting regular ref" ' + test_when_finished "git update-ref -d refs/heads/regularref" && + git update-ref refs/heads/regularref $a && + format_command $type "symref-delete refs/heads/regularref" "$a" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + grep "fatal: cannot lock ref ${SQ}refs/heads/regularref${SQ}: expected symref with target ${SQ}$a${SQ}: but is a regular ref" err + ' + + test_expect_success "stdin $type symref-delete fails with too many arguments" ' + format_command $type "symref-delete refs/heads/symref" "$a" "$a" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + if test "$type" = "-z" + then + grep "fatal: unknown command: $a" err + else + grep "fatal: symref-delete refs/heads/symref: extra input: $a" err + fi + ' + + test_expect_success "stdin $type symref-delete fails with wrong old value" ' + format_command $type "symref-delete refs/heads/symref" "$m" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + grep "fatal: verifying symref target: ${SQ}refs/heads/symref${SQ}: is at $a but expected refs/heads/main" err && + git symbolic-ref refs/heads/symref >expect && + echo $a >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-delete works with right old value" ' + format_command $type "symref-delete refs/heads/symref" "$a" >stdin && + git update-ref --stdin $type --no-deref stdin && + format_command $type "symref-delete refs/heads/symref" "" >stdin && + git update-ref --stdin $type --no-deref stdin && + git update-ref --stdin $type --no-deref stdin && + git update-ref --stdin $type --no-deref >actual @@ -171,16 +172,32 @@ test_expect_success 'hook gets all queued symref updates' ' done >>actual EOF - cat >expect <<-EOF && + # In the files backend, "delete" also triggers an additional transaction + # update on the packed-refs backend, which constitutes additional reflog + # entries. + if test_have_prereq REFFILES + then + cat >expect <<-EOF + aborted + $ZERO_OID $ZERO_OID refs/heads/symrefd + EOF + else + >expect + fi && + + cat >>expect <<-EOF && prepared ref:refs/heads/main $ZERO_OID refs/heads/symref + ref:refs/heads/main $ZERO_OID refs/heads/symrefd committed ref:refs/heads/main $ZERO_OID refs/heads/symref + ref:refs/heads/main $ZERO_OID refs/heads/symrefd EOF git update-ref --no-deref --stdin <<-EOF && start symref-verify refs/heads/symref refs/heads/main + symref-delete refs/heads/symrefd refs/heads/main prepare commit EOF From patchwork Fri Jun 7 13:33:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689975 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 05B3219AD45 for ; Fri, 7 Jun 2024 13:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767196; cv=none; b=Y97yLwy+2MCEJ0sCZs0ELFjdmWIwfoj+rD6ngWQY1NJcksqe8/Ki3nLa3J45Qp0Xh2uApkrwXbHmEoNWAC3uY4tVdz0HIsDZcPKt65n29qjwjRAJfA/f9Zdx0zffOinLMi34WE9OZQr546Bj/1+xD5Ku9679a0Kyx4d5+8+1iBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767196; c=relaxed/simple; bh=tccQa9apapwPadVEimKtQhpBVPCWKWR/m8xEkXqMkyA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V3/Crv3+ZOfaIjMf7SZg+uj7OOriUdN7cZo3+ZXGUCU05ojin/Fn6pOh9Q719e6WlYXlQ3ATD5uaAsbdzfAh5LMgd1AbzY/tBb2sC0u9w99vdjlEGFBm/yuUWMjpnTkBjGEMa8GqOxuahfI4ejCG7pb4HM7SIv8jzxzd6YgXXsE= 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=fvMoeTFn; arc=none smtp.client-ip=209.85.208.48 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="fvMoeTFn" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-57864327f6eso2528273a12.1 for ; Fri, 07 Jun 2024 06:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767192; x=1718371992; 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=nFcj+mdknZOgGisB0w6Lt0rRxZc3ODf2cQ1u0SvmRjM=; b=fvMoeTFnv4gFEolVQcmDJiTnCM4VnEs7MLl1f6GP08HqTecDNE4ewxtWaGnK6frHDK tILzDVIfdOYHWjFuv8AioxgorLUN7ssooEOmB+VXNNqRk57cw3X7GFvR3iY0up14xARi +VMGUKY29rPk5Edq50dfbAYQH4cZOc34K0U//9L7Hn6vrYnlbHCKi772jr5V7zHCTu8T v/YRCukrMn84zEYtxkf0nSL8mNJ81EuvzIEM1dzVH7X7BiJR7ckvIgN5qbfo0pC0jpsM B9ILwhiCgU/z+hlc41mRovUy69p5/SLTmJmHKykMhzZO3vMgcTZ81ZFVVnNaRPajRJps KWqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767192; x=1718371992; 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=nFcj+mdknZOgGisB0w6Lt0rRxZc3ODf2cQ1u0SvmRjM=; b=irv/BBAGbfSa4jJES1mwW+64ZF0hlcSb4M275GcwwVdAE8GYZS+1KnEC7AiPajYtD2 1XFx2csZ6FRDILHNhNoc44n6oF3aWOPvAhv1IEVxzKokbDLHjtALcgj9lOjQd+FWfx0n NHRIGTGSyZ2uW2jxyRZyxFq/xdadxqngNW94W3VDrBL6Tjj34PUmkxKIJn34nBnBUAaP YMBwWqmXget2psfKt25sZU1Cfp6ekDyGqNTqVT3e7vMm3S6JPmaRqWhbKlEvD1v6+ah0 KjZhlHbvVGY4oMwbO2O3YMpbw+gH2yNrWaPKQXzNgq780EOpyJKJm5quNkSZO2zoz06g s8tA== X-Gm-Message-State: AOJu0YwI7UlH0JDg4J/tuN/s/95vmOumdU/tFxFbCR1/L0AzW5mSKyYx qffBjsDH0BXatY9pXMRfTI9bN3vIzZ9v5K0d/4dEJ0666aIZr5x2 X-Google-Smtp-Source: AGHT+IEJI++86zx4Kp/8R0fdLyXl56Aq+qqPHGwH3JyObXLaWEdiXADTZfOEB1BLs3Sgmhu6S0jOAw== X-Received: by 2002:a50:8a82:0:b0:578:d846:fc0a with SMTP id 4fb4d7f45d1cf-57c4e412760mr2106232a12.20.1717767191869; Fri, 07 Jun 2024 06:33:11 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:11 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 5/7] update-ref: add support for 'symref-create' command Date: Fri, 7 Jun 2024 15:33:02 +0200 Message-ID: <20240607133304.2333280-6-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 Add 'symref-create' command to the '--stdin' mode 'git-update-ref' to allow creation of symbolic refs in a transaction. The 'symref-create' command takes in a , which the created will point to. Also, support the 'core.prefersymlinkrefs' config, wherein if the config is set and the filesystem supports symlinks, we create the symbolic ref as a symlink. We fallback to creating a regular symref if creating the symlink is unsuccessful. Helped-by: Patrick Steinhardt Signed-off-by: Karthik Nayak --- Documentation/git-update-ref.txt | 6 +++ builtin/clone.c | 2 +- builtin/update-ref.c | 32 +++++++++++++++- refs.c | 9 +++-- refs.h | 1 + t/t0600-reffiles-backend.sh | 32 ++++++++++++++++ t/t1400-update-ref.sh | 65 ++++++++++++++++++++++++++++++++ t/t1416-ref-transaction-hooks.sh | 3 ++ t/t5605-clone-local.sh | 2 +- 9 files changed, 146 insertions(+), 6 deletions(-) diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt index 16e02f6979..364ef78af1 100644 --- a/Documentation/git-update-ref.txt +++ b/Documentation/git-update-ref.txt @@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form: create SP SP LF delete SP [SP ] LF verify SP [SP ] LF + symref-create SP SP LF symref-delete SP [SP ] LF symref-verify SP [SP ] LF option SP LF @@ -88,6 +89,7 @@ quoting: create SP NUL NUL delete SP NUL [] NUL verify SP NUL [] NUL + symref-create SP NUL NUL symref-delete SP [NUL ] NUL symref-verify SP [NUL ] NUL option SP NUL @@ -121,6 +123,10 @@ verify:: Verify against but do not change it. If is zero or missing, the ref must not exist. +symref-create: + Create symbolic ref with after verifying + it does not exist. + symref-delete:: Delete after verifying it exists with , if given. diff --git a/builtin/clone.c b/builtin/clone.c index 23993b905b..6ddb3084e6 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -576,7 +576,7 @@ static void write_remote_refs(const struct ref *local_refs) if (!r->peer_ref) continue; if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid, - 0, NULL, &err)) + NULL, 0, NULL, &err)) die("%s", err.buf); } diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 0cb7eef3c6..9c40e94626 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -257,7 +257,7 @@ static void parse_cmd_create(struct ref_transaction *transaction, if (*next != line_termination) die("create %s: extra input: %s", refname, next); - if (ref_transaction_create(transaction, refname, &new_oid, + if (ref_transaction_create(transaction, refname, &new_oid, NULL, update_flags | create_reflog_flag, msg, &err)) die("%s", err.buf); @@ -267,6 +267,35 @@ static void parse_cmd_create(struct ref_transaction *transaction, strbuf_release(&err); } + +static void parse_cmd_symref_create(struct ref_transaction *transaction, + const char *next, const char *end) +{ + struct strbuf err = STRBUF_INIT; + char *refname, *new_target; + + refname = parse_refname(&next); + if (!refname) + die("symref-create: missing "); + + new_target = parse_next_refname(&next); + if (!new_target) + die("symref-create %s: missing ", refname); + + if (*next != line_termination) + die("symref-create %s: extra input: %s", refname, next); + + if (ref_transaction_create(transaction, refname, NULL, new_target, + update_flags | create_reflog_flag, + msg, &err)) + die("%s", err.buf); + + update_flags = default_flags; + free(refname); + free(new_target); + strbuf_release(&err); +} + static void parse_cmd_delete(struct ref_transaction *transaction, const char *next, const char *end) { @@ -473,6 +502,7 @@ static const struct parse_cmd { { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, + { "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN }, { "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN }, { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN }, { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, diff --git a/refs.c b/refs.c index 01f3188a09..8807e87e3c 100644 --- a/refs.c +++ b/refs.c @@ -1268,15 +1268,18 @@ int ref_transaction_update(struct ref_transaction *transaction, int ref_transaction_create(struct ref_transaction *transaction, const char *refname, const struct object_id *new_oid, + const char *new_target, unsigned int flags, const char *msg, struct strbuf *err) { - if (!new_oid || is_null_oid(new_oid)) { - strbuf_addf(err, "'%s' has a null OID", refname); + if (new_oid && new_target) + BUG("create called with both new_oid and new_target set"); + if ((!new_oid || is_null_oid(new_oid)) && !new_target) { + strbuf_addf(err, "'%s' has neither a valid OID nor a target", refname); return 1; } return ref_transaction_update(transaction, refname, new_oid, - null_oid(), NULL, NULL, flags, + null_oid(), new_target, NULL, flags, msg, err); } diff --git a/refs.h b/refs.h index 974cf4dd08..28e3bb8a42 100644 --- a/refs.h +++ b/refs.h @@ -708,6 +708,7 @@ int ref_transaction_update(struct ref_transaction *transaction, int ref_transaction_create(struct ref_transaction *transaction, const char *refname, const struct object_id *new_oid, + const char *new_target, unsigned int flags, const char *msg, struct strbuf *err); diff --git a/t/t0600-reffiles-backend.sh b/t/t0600-reffiles-backend.sh index 92f570313d..b2a771ff2b 100755 --- a/t/t0600-reffiles-backend.sh +++ b/t/t0600-reffiles-backend.sh @@ -468,4 +468,36 @@ test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' ' esac ' +test_expect_success SYMLINKS 'symref transaction supports symlinks' ' + test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" && + git update-ref refs/heads/new @ && + test_config core.prefersymlinkrefs true && + cat >stdin <<-EOF && + start + symref-create TEST_SYMREF_HEAD refs/heads/new + prepare + commit + EOF + git update-ref --no-deref --stdin stdin <<-EOF && + start + symref-create TEST_SYMREF_HEAD refs/heads/new + prepare + commit + EOF + git update-ref --no-deref --stdin actual && + echo refs/heads/new >expect && + test_cmp expect actual +' + test_done diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh index 9dbe28bd13..253263320d 100755 --- a/t/t1400-update-ref.sh +++ b/t/t1400-update-ref.sh @@ -1799,6 +1799,71 @@ do git update-ref --stdin $type --no-deref stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + if test "$type" = "-z" + then + grep "fatal: unknown command: $a" err + else + grep "fatal: symref-create refs/heads/symref: extra input: $a" err + fi + ' + + test_expect_success "stdin $type symref-create fails with no target" ' + format_command $type "symref-create refs/heads/symref" >stdin && + test_must_fail git update-ref --stdin $type --no-deref stdin && + test_must_fail git update-ref --stdin $type --no-deref stdin && + git update-ref --stdin $type --no-deref expect && + echo $a >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-create works with --no-deref" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + format_command $type "symref-create refs/heads/symref" "$a" && + git update-ref --stdin $type err + ' + + test_expect_success "stdin $type create dangling symref ref works" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + format_command $type "symref-create refs/heads/symref" "refs/heads/unkown" >stdin && + git update-ref --stdin $type --no-deref expect && + echo refs/heads/unkown >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-create does not create reflogs by default" ' + test_when_finished "git symbolic-ref -d refs/symref" && + format_command $type "symref-create refs/symref" "$a" >stdin && + git update-ref --stdin $type --no-deref expect && + echo $a >actual && + test_cmp expect actual && + test_must_fail git reflog exists refs/symref + ' + + test_expect_success "stdin $type symref-create reflogs with --create-reflog" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + format_command $type "symref-create refs/heads/symref" "$a" >stdin && + git update-ref --create-reflog --stdin $type --no-deref expect && + echo $a >actual && + test_cmp expect actual && + git reflog exists refs/heads/symref + ' + done test_done diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index ccde1b944b..ff77dcca6b 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -189,15 +189,18 @@ test_expect_success 'hook gets all queued symref updates' ' prepared ref:refs/heads/main $ZERO_OID refs/heads/symref ref:refs/heads/main $ZERO_OID refs/heads/symrefd + $ZERO_OID ref:refs/heads/main refs/heads/symrefc committed ref:refs/heads/main $ZERO_OID refs/heads/symref ref:refs/heads/main $ZERO_OID refs/heads/symrefd + $ZERO_OID ref:refs/heads/main refs/heads/symrefc EOF git update-ref --no-deref --stdin <<-EOF && start symref-verify refs/heads/symref refs/heads/main symref-delete refs/heads/symrefd refs/heads/main + symref-create refs/heads/symrefc refs/heads/main prepare commit EOF diff --git a/t/t5605-clone-local.sh b/t/t5605-clone-local.sh index a3055869bc..339d8c786f 100755 --- a/t/t5605-clone-local.sh +++ b/t/t5605-clone-local.sh @@ -163,7 +163,7 @@ test_expect_success REFFILES 'local clone from repo with corrupt refs fails grac echo a >corrupt/.git/refs/heads/topic && test_must_fail git clone corrupt working 2>err && - grep "has a null OID" err + grep "has neither a valid OID nor a target" err ' test_done From patchwork Fri Jun 7 13:33:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689976 Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) (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 C445019AD51 for ; Fri, 7 Jun 2024 13:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767196; cv=none; b=Md+W0bDHmIqKjg6+C0gDyth+4N5xSL6ZB3NRaWxTj6KG6qgTYVc8uDlMD7DdQer0ASVwrbZeCAP65/0gHFQADhwDDCeCzEJ02umMvWfiwNyVZ41z4T7JnD0I1rCJQPBIaLMZ7gjUbit5NojuFm/GtjRTwF2chTep9Lfnca/OP6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767196; c=relaxed/simple; bh=gCl1bW6Soiyhq7iaW8FjxD8DXoKp1eBVIEg7NcM50Gk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RaKx/EsrG0IBdrh2ESMHxTccudpmb8L0ElTUI5xdcGc2vOaT/RdMJN9alXIVM3ncipEgdkCGkrLrmqzIzwdRPJ0gwAnjXYJwxysLuI/k9HzIShUSoBkK4QCfYjNqIKPzfoRjBxjn+bxuG+1xT+AV0KoMSwsB/Auk/VUAra5GO6g= 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=Q+iPKl73; arc=none smtp.client-ip=209.85.208.172 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="Q+iPKl73" Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2eab0bc74cdso22249881fa.3 for ; Fri, 07 Jun 2024 06:33:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767193; x=1718371993; 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=dt/wb1T6tDU4bKkwhv2ajv8Gms0P9089s6p137lo7V0=; b=Q+iPKl73vCyU1ap3AcWGycjccnwmGbzQ5k58vrGC+z/MWDk+DVdVhM92Dcwa098met 95RpNIpFwXprZjiTmqRPCPuyISsfUhCTDAZoN+oTtRtyFz52Ojlm2lGZ9F2wUACo8cld nezMCJlYI8XF4mhA8+ix5ZUL2so78xLobdE03IGJA66xSA8x9MGWBWafQtpWlspshjLT UVCBe+erYHwY6N24YZki0n06qQjtnV5ZIzWzIyIbS0U408ghLcD2qShR3hmjHMbCqlNX EuwS6eG+JMpXTqKPGIwFcLwqPZnKeDc+X/xdBQLk7FvaPWJW0v16JTElRUlEOnjRyW+I F34Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767193; x=1718371993; 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=dt/wb1T6tDU4bKkwhv2ajv8Gms0P9089s6p137lo7V0=; b=hAnC+epPD4LPgh13gXdqgCNjGSQnM2wF1pgp/P5neY9vS57SSAhxUeHjgE7uEM2gmO R9axWBGVj2zIKv44mkhtOsXgSgqVjGYxmT0NHS/HKceHPDrfFpxSQMvnX63SEeLrFIgI opZUvuHHA+z5xypOHLgMfd8AdZd0zwap09SRC7oNLUZQdRJpMgV/K/9ZyHf9tID2Szpe f1XrjCNKmgME3I7ZIp6kRDxiTs/FeEj0CcVpT5qJNXYOP10A3NtI+DkKwMCBHHM4ZaRV bx9s4b+mdS2DuUSnVw9643wDDqzU7IJxKgD7g1NG1g6Ia4TnLDcTocCXJoB6A3XnnBLd 9e9w== X-Gm-Message-State: AOJu0YxaJRyrb5GnlabB633zWXxgHKjtCXjyNIYcBoW1zOSRk2Pb3PNQ DVgox2ZCjSp5VZyu/P77F/AwGQwUjDy4MInSYqhqsg28EnQwuz3fvrRVLOop X-Google-Smtp-Source: AGHT+IG1RSHNbue/xbgs/J/ltqYWgerKdqd98O+WJfPSOdHYyAYLLe9VKc6IjyGPnZPSkNo99+/i3A== X-Received: by 2002:a2e:8058:0:b0:2ea:e030:ae40 with SMTP id 38308e7fff4ca-2eae030b8e7mr12643031fa.13.1717767192619; Fri, 07 Jun 2024 06:33:12 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:12 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 6/7] reftable: pick either 'oid' or 'target' for new updates Date: Fri, 7 Jun 2024 15:33:03 +0200 Message-ID: <20240607133304.2333280-7-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 When creating a reference transaction update, we can provide the old/new oid/target for the update. We have checks in place to ensure that for each old/new, either oid or target is set and not both. In the reftable backend, when dealing with updates without the `REF_NO_DEREF` flag, we don't selectively propagate data as needed. Since there are no active users of the path, this is not caught. As we want to introduce the 'symref-update' command in the upcoming commit, which would use this flow, correct it. Helped-by: Patrick Steinhardt Signed-off-by: Karthik Nayak --- refs/reftable-backend.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index c66ab9ecd8..1c46fc87f8 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -895,8 +895,9 @@ 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->new_target, - u->old_target, u->msg); + u->new_target ? NULL : &u->new_oid, + u->old_target ? NULL : &u->old_oid, + u->new_target, u->old_target, u->msg); new_update->parent_update = u; From patchwork Fri Jun 7 13:33:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13689977 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (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 F143519AD46 for ; Fri, 7 Jun 2024 13:33:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767198; cv=none; b=OmICqiXNeHg6BaZxJOGcYPLuz0yU0XWfnDRVdfNWzpGwPBVcp9KxhLwDiaU3wj6KkIlTIUocWDFxdsQ5RM0Eta55bI7uzLhoXB39P/h3g5a8MW79B2n5patt12hUBcit51thNIc6qfFa4XX/03He4XTV9z4M7e5dxY2XXvwn+2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767198; c=relaxed/simple; bh=ZYte2GA4NtkkNHUAA//rDbe+qAlhvQEYN+TgOrAs+e8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OXLYaCfWMBPubbwC5tvffd60dICc4ZbAFrZD173BUYQHBVNzn0TNsPrwHX71jAv2bs4FzGViabD1iLMVAgUlpCB1NfJzAqtEXq1OcE9FgCDayLnrIEW/Gs3r/GYrxiN6bRZmgOfCO+JB5HRXnAfwKkXliIGtbCNSLrJX4HVyWWM= 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=hWnlRH7y; arc=none smtp.client-ip=209.85.218.48 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="hWnlRH7y" Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a689ad8d1f6so270319066b.2 for ; Fri, 07 Jun 2024 06:33:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717767194; x=1718371994; 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=Aqia56H/ga97gsbwKVJBW7ZCm3I7W80LHmdo6OOrmoo=; b=hWnlRH7yynI3OYlsGdccTTOCucXQVD7yf7Fua6tiM46uf4jnQkBsXOcKa4FCuCOvPg 6Vlq/52658m9KPXQWEGco2pjvHdrxkcV28uotQ4ofN2hqA5N35EXTyNT4yWYwmmKUANb eUQ7V7G3BEiPS7juLMFsoPndyciRplKoYoejZsIfGqqgs8ez1SNtU5v0To5CcaPbiADD EktEo2zkyF+oxe72utmRP7Q3paQZOHD7TmAEXA2MtjdojtfXgcsAEkrvSb/oalRg3F9J pnmPzlpc4XNjqN3FaFacDFGqlGp8LaB0QxuVUlZqmzBaD/bwrfn8+VW1SUvlN/RRHg6c 72BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717767194; x=1718371994; 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=Aqia56H/ga97gsbwKVJBW7ZCm3I7W80LHmdo6OOrmoo=; b=bo+BBOMfLA3kCMSmYtRUdinTqbodCXlXbe2a22Y9djbaiCuwEgIdsxEbrj8lSPLtWQ IE95ClaXNGEZgg6S+i79E1tpCae81yYThQiaq51Ssb8CS3BcrbJytkkSzuUDdz88MJV6 F5eKmnqKaAAgqJKUtP1kTHShBsoIPSoTkWkw1VgS/gNGbguRCWxkxvVgvehdC2MbI6A4 dcSmpXb8Q7thzdX9CR5/c5XtJl0lgT1BEYZOPWGlScvjLT9nBhiAqEQkLtEFkOxpNk8b T+7eVGIhREje+gbwlQCaqc0E6F8scPzPfi4o8JFzJBs6YaMmHTQNSlyQuyoIWD8c1Xcr iZuw== X-Gm-Message-State: AOJu0Yx3J13lVgKzvU+FvFs0qX1PQdkMk18ZYcFGr0Lmoumloy2TwHjg SUcqkeqdHwroPsHEYM1i00s6uoYyz7vXwSu9Cn9grqeQVtjSUtub X-Google-Smtp-Source: AGHT+IE6DortFN6eWdQcGVT3RHrS3U33vf//tJZRYhLCag12+eZXbZMJu7E0jCkOZygI0O+W6EnACQ== X-Received: by 2002:a17:906:3c04:b0:a6a:285d:62d8 with SMTP id a640c23a62f3a-a6cdb9f5c12mr177493566b.74.1717767193417; Fri, 07 Jun 2024 06:33:13 -0700 (PDT) Received: from localhost.localdomain ([185.223.147.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae2340f6sm2764167a12.90.2024.06.07.06.33.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:33:13 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v5 7/7] update-ref: add support for 'symref-update' command Date: Fri, 7 Jun 2024 15:33:04 +0200 Message-ID: <20240607133304.2333280-8-knayak@gitlab.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240607133304.2333280-1-knayak@gitlab.com> References: <20240605102958.716432-1-knayak@gitlab.com> <20240607133304.2333280-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 Add 'symref-update' command to the '--stdin' mode of 'git-update-ref' to allow updates of symbolic refs. The 'symref-update' command takes in a , which the will be updated to. If the doesn't exist it will be created. It also optionally takes either an `ref ` or `oid `. If the is provided, it checks to see if the targets the before the update. If is provided it checks to ensure that it is a regular ref and is the OID before the update. This by extension also means that this when a zero is provided, it ensures that the ref didn't exist before. The divergence in syntax from the regular `update` command is because if we don't use a `(ref | oid)` prefix for the old_value, then there is ambiguity around if the value provided should be treated as an oid or a reference. This is more so the reason, because we allow anything committish to be provided as an oid. While 'symref-verify' and 'symref-delete' also take in `` we do not have this divergence there as those commands only work with symrefs. Whereas 'symref-update' also works with regular refs and allows users to convert regular refs to symrefs. The command allows users to perform symbolic ref updates within a transaction. This provides atomicity and allows users to perform a set of operations together. This command supports deref mode, to ensure that we can update dereferenced regular refs to symrefs. Helped-by: Patrick Steinhardt Helped-by: Junio C Hamano Signed-off-by: Karthik Nayak --- Documentation/git-update-ref.txt | 7 ++ builtin/update-ref.c | 92 ++++++++++++++ t/t1400-update-ref.sh | 203 +++++++++++++++++++++++++++++++ t/t1416-ref-transaction-hooks.sh | 4 + 4 files changed, 306 insertions(+) diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt index 364ef78af1..afcf33cf60 100644 --- a/Documentation/git-update-ref.txt +++ b/Documentation/git-update-ref.txt @@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form: create SP SP LF delete SP [SP ] LF verify SP [SP ] LF + symref-update SP SP [SP (ref SP | oid SP )] LF symref-create SP SP LF symref-delete SP [SP ] LF symref-verify SP [SP ] LF @@ -89,6 +90,7 @@ quoting: create SP NUL NUL delete SP NUL [] NUL verify SP NUL [] NUL + symref-update SP NUL [NUL (ref NUL | oid NUL )] NUL symref-create SP NUL NUL symref-delete SP [NUL ] NUL symref-verify SP [NUL ] NUL @@ -119,6 +121,11 @@ delete:: Delete after verifying it exists with , if given. If given, may not be zero. +symref-update:: + Set to after verifying or , + if given. Specify a zero to ensure that the ref does not + exist before the update. + verify:: Verify against but do not change it. If is zero or missing, the ref must not exist. diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 9c40e94626..471fa5c8d1 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -98,6 +98,42 @@ static char *parse_next_refname(const char **next) return parse_refname(next); } +/* + * Wrapper around parse_arg which skips the next delimiter. + */ +static char *parse_next_arg(const char **next) +{ + struct strbuf arg = STRBUF_INIT; + + if (line_termination) { + /* Without -z, consume SP and use next argument */ + if (!**next || **next == line_termination) + return NULL; + if (**next != ' ') + die("expected SP but got: %s", *next); + } else { + /* With -z, read the next NUL-terminated line */ + if (**next) + return NULL; + } + /* Skip the delimiter */ + (*next)++; + + if (line_termination) { + /* Without -z, use the next argument */ + *next = parse_arg(*next, &arg); + } else { + /* With -z, use everything up to the next NUL */ + strbuf_addstr(&arg, *next); + *next += arg.len; + } + + if (arg.len) + return strbuf_detach(&arg, NULL); + + strbuf_release(&arg); + return NULL; +} /* * The value being parsed is (as opposed to ; the @@ -237,6 +273,61 @@ static void parse_cmd_update(struct ref_transaction *transaction, strbuf_release(&err); } +static void parse_cmd_symref_update(struct ref_transaction *transaction, + const char *next, const char *end) +{ + char *refname, *new_target, *old_arg; + char *old_target = NULL; + struct strbuf err = STRBUF_INIT; + struct object_id old_oid; + int have_old_oid = 0; + + refname = parse_refname(&next); + if (!refname) + die("symref-update: missing "); + + new_target = parse_next_refname(&next); + if (!new_target) + die("symref-update %s: missing ", refname); + + old_arg = parse_next_arg(&next); + if (old_arg) { + old_target = parse_next_arg(&next); + if (!old_target) + die("symref-update %s: expected old value", refname); + + if (!strcmp(old_arg, "oid")) { + if (repo_get_oid(the_repository, old_target, &old_oid)) + die("symref-update %s: invalid oid: %s", refname, old_target); + + have_old_oid = 1; + } else if (!strcmp(old_arg, "ref")) { + if (check_refname_format(old_target, REFNAME_ALLOW_ONELEVEL)) + die("symref-update %s: invalid ref: %s", refname, old_target); + } else { + die("symref-update %s: invalid arg '%s' for old value", refname, old_arg); + } + } + + if (*next != line_termination) + die("symref-update %s: extra input: %s", refname, next); + + if (ref_transaction_update(transaction, refname, NULL, + have_old_oid ? &old_oid : NULL, + new_target, + have_old_oid ? NULL : old_target, + update_flags | create_reflog_flag, + msg, &err)) + die("%s", err.buf); + + update_flags = default_flags; + free(refname); + free(old_arg); + free(old_target); + free(new_target); + strbuf_release(&err); +} + static void parse_cmd_create(struct ref_transaction *transaction, const char *next, const char *end) { @@ -502,6 +593,7 @@ static const struct parse_cmd { { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, + { "symref-update", parse_cmd_symref_update, 4, UPDATE_REFS_OPEN }, { "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN }, { "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN }, { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN }, diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh index 253263320d..eb1691860d 100755 --- a/t/t1400-update-ref.sh +++ b/t/t1400-update-ref.sh @@ -1362,6 +1362,7 @@ test_expect_success 'fails with duplicate HEAD update' ' ' test_expect_success 'fails with duplicate ref update via symref' ' + test_when_finished "git symbolic-ref -d refs/heads/symref2" && git branch target2 $A && git symbolic-ref refs/heads/symref2 refs/heads/target2 && cat >stdin <<-EOF && @@ -1864,6 +1865,208 @@ do git reflog exists refs/heads/symref ' + test_expect_success "stdin $type symref-update fails with too many arguments" ' + format_command $type "symref-update refs/heads/symref" "$a" "ref" "$a" "$a" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + if test "$type" = "-z" + then + grep "fatal: unknown command: $a" err + else + grep "fatal: symref-update refs/heads/symref: extra input: $a" err + fi + ' + + test_expect_success "stdin $type symref-update fails with wrong old value argument" ' + format_command $type "symref-update refs/heads/symref" "$a" "foo" "$a" "$a" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + grep "fatal: symref-update refs/heads/symref: invalid arg ${SQ}foo${SQ} for old value" err + ' + + test_expect_success "stdin $type symref-update creates with zero old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + format_command $type "symref-update refs/heads/symref" "$a" "oid" "$Z" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update creates with no old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + format_command $type "symref-update refs/heads/symref" "$a" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update creates dangling" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + test_must_fail git rev-parse refs/heads/nonexistent && + format_command $type "symref-update refs/heads/symref" "refs/heads/nonexistent" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update fails with wrong old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + git symbolic-ref refs/heads/symref $a && + format_command $type "symref-update refs/heads/symref" "$m" "ref" "$b" >stdin && + test_must_fail git update-ref --stdin $type --no-deref err && + grep "fatal: verifying symref target: ${SQ}refs/heads/symref${SQ}: is at $a but expected $b" err && + test_must_fail git rev-parse --verify -q $c + ' + + test_expect_success "stdin $type symref-update updates dangling ref" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + test_must_fail git rev-parse refs/heads/nonexistent && + git symbolic-ref refs/heads/symref refs/heads/nonexistent && + format_command $type "symref-update refs/heads/symref" "$a" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update updates dangling ref with old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + test_must_fail git rev-parse refs/heads/nonexistent && + git symbolic-ref refs/heads/symref refs/heads/nonexistent && + format_command $type "symref-update refs/heads/symref" "$a" "ref" "refs/heads/nonexistent" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update fails update dangling ref with wrong old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + test_must_fail git rev-parse refs/heads/nonexistent && + git symbolic-ref refs/heads/symref refs/heads/nonexistent && + format_command $type "symref-update refs/heads/symref" "$a" "ref" "refs/heads/wrongref" >stdin && + test_must_fail git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update works with right old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + git symbolic-ref refs/heads/symref $a && + format_command $type "symref-update refs/heads/symref" "$m" "ref" "$a" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update works with no old value" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + git symbolic-ref refs/heads/symref $a && + format_command $type "symref-update refs/heads/symref" "$m" >stdin && + git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update fails with empty old ref-target" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + git symbolic-ref refs/heads/symref $a && + format_command $type "symref-update refs/heads/symref" "$m" "ref" "" >stdin && + test_must_fail git update-ref --stdin $type --no-deref expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update creates (with deref)" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + format_command $type "symref-update refs/heads/symref" "$a" >stdin && + git update-ref --stdin $type expect && + git symbolic-ref --no-recurse refs/heads/symref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$Z $(git rev-parse $a)" actual + ' + + test_expect_success "stdin $type symref-update regular ref to symref with correct old-oid" ' + test_when_finished "git symbolic-ref -d --no-recurse refs/heads/regularref" && + git update-ref --no-deref refs/heads/regularref $a && + format_command $type "symref-update refs/heads/regularref" "$a" "oid" "$(git rev-parse $a)" >stdin && + git update-ref --stdin $type expect && + git symbolic-ref --no-recurse refs/heads/regularref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual && + grep "$(git rev-parse $a) $(git rev-parse $a)" actual + ' + + test_expect_success "stdin $type symref-update regular ref to symref fails with wrong old-oid" ' + test_when_finished "git update-ref -d refs/heads/regularref" && + git update-ref --no-deref refs/heads/regularref $a && + format_command $type "symref-update refs/heads/regularref" "$a" "oid" "$(git rev-parse refs/heads/target2)" >stdin && + test_must_fail git update-ref --stdin $type err && + grep "fatal: cannot lock ref ${SQ}refs/heads/regularref${SQ}: is at $(git rev-parse $a) but expected $(git rev-parse refs/heads/target2)" err && + echo $(git rev-parse $a) >expect && + git rev-parse refs/heads/regularref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update regular ref to symref fails with invalid old-oid" ' + test_when_finished "git update-ref -d refs/heads/regularref" && + git update-ref --no-deref refs/heads/regularref $a && + format_command $type "symref-update refs/heads/regularref" "$a" "oid" "not-a-ref-oid" >stdin && + test_must_fail git update-ref --stdin $type err && + grep "fatal: symref-update refs/heads/regularref: invalid oid: not-a-ref-oid" err && + echo $(git rev-parse $a) >expect && + git rev-parse refs/heads/regularref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update existing symref with zero old-oid" ' + test_when_finished "git symbolic-ref -d --no-recurse refs/heads/symref" && + git symbolic-ref refs/heads/symref refs/heads/target2 && + format_command $type "symref-update refs/heads/symref" "$a" "oid" "$Z" >stdin && + test_must_fail git update-ref --stdin $type err && + grep "fatal: cannot lock ref ${SQ}refs/heads/symref${SQ}: reference already exists" err && + echo refs/heads/target2 >expect && + git symbolic-ref refs/heads/symref >actual && + test_cmp expect actual + ' + + test_expect_success "stdin $type symref-update regular ref to symref (with deref)" ' + test_when_finished "git symbolic-ref -d refs/heads/symref" && + test_when_finished "git update-ref -d --no-deref refs/heads/symref2" && + git update-ref refs/heads/symref2 $a && + git symbolic-ref --no-recurse refs/heads/symref refs/heads/symref2 && + format_command $type "symref-update refs/heads/symref" "$a" >stdin && + git update-ref $type --stdin expect && + git symbolic-ref --no-recurse refs/heads/symref2 >actual && + test_cmp expect actual && + echo refs/heads/symref2 >expect && + git symbolic-ref --no-recurse refs/heads/symref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual && + grep "$(git rev-parse $a) $(git rev-parse $a)" actual + ' + + test_expect_success "stdin $type symref-update regular ref to symref" ' + test_when_finished "git symbolic-ref -d --no-recurse refs/heads/regularref" && + git update-ref --no-deref refs/heads/regularref $a && + format_command $type "symref-update refs/heads/regularref" "$a" >stdin && + git update-ref $type --stdin expect && + git symbolic-ref --no-recurse refs/heads/regularref >actual && + test_cmp expect actual && + test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual && + grep "$(git rev-parse $a) $(git rev-parse $a)" actual + ' + done test_done diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index ff77dcca6b..5a812ca3c0 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -163,6 +163,7 @@ test_expect_success 'hook gets all queued symref updates' ' git update-ref refs/heads/branch $POST_OID && git symbolic-ref refs/heads/symref refs/heads/main && git symbolic-ref refs/heads/symrefd refs/heads/main && + git symbolic-ref refs/heads/symrefu refs/heads/main && test_hook reference-transaction <<-\EOF && echo "$*" >>actual @@ -190,10 +191,12 @@ test_expect_success 'hook gets all queued symref updates' ' ref:refs/heads/main $ZERO_OID refs/heads/symref ref:refs/heads/main $ZERO_OID refs/heads/symrefd $ZERO_OID ref:refs/heads/main refs/heads/symrefc + ref:refs/heads/main ref:refs/heads/branch refs/heads/symrefu committed ref:refs/heads/main $ZERO_OID refs/heads/symref ref:refs/heads/main $ZERO_OID refs/heads/symrefd $ZERO_OID ref:refs/heads/main refs/heads/symrefc + ref:refs/heads/main ref:refs/heads/branch refs/heads/symrefu EOF git update-ref --no-deref --stdin <<-EOF && @@ -201,6 +204,7 @@ test_expect_success 'hook gets all queued symref updates' ' symref-verify refs/heads/symref refs/heads/main symref-delete refs/heads/symrefd refs/heads/main symref-create refs/heads/symrefc refs/heads/main + symref-update refs/heads/symrefu refs/heads/branch ref refs/heads/main prepare commit EOF