From patchwork Mon Jan 8 10:05:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13513241 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC3BF13ADC for ; Mon, 8 Jan 2024 10:05:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="JpcfkzPt"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="p4yz3V74" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 1349A5C17AE; Mon, 8 Jan 2024 05:05:30 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 08 Jan 2024 05:05:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1704708330; x=1704794730; bh=u03mXpqfwD pGRcsqqauszz3ZYI9hwvjIVDUwcoetir4=; b=JpcfkzPt0mHmPCjQ/WXhuWfMvY x2SBzhZjl4meQD8Jyy1n/VxFD3h8sgiHTBQLtQnSbrbaN4w3z2eb345TIEDhRL7o nbNb5kiBTq4rQixAP53PQla4c3MVxWgUkEnN+b51GL/dHD6baL5inkmgHocmSsnX NuUfvhERcEZk4Qc6eDCJiqCiAT1lGevCYrl8zlKTPPsDJzgswkd8vFjOAeSAAWE9 GPmRimcmIgNwZoWvFUT0pYhf/LihWuIRZ/EhIXercGAGLnvrxMm7dBIRK1EuPhHz veJZdkCjNZMH3T1aUq5hX2gh6qLPIfu/mK6UR0zrh0ycPBP5YPgorbzewvTg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1704708330; x=1704794730; bh=u03mXpqfwDpGRcsqqauszz3ZYI9h wvjIVDUwcoetir4=; b=p4yz3V74hnQUiO9ehdTasn6dlR1USsEzzUyZEGM8O3cV q2I3aPb5Qyk3YlpX6uKGnXhW71mhp8gUZ8ry86S4/0r9BfveuY43pYs26GnzsIh2 STC0QCrRDR2rLRt7fKaHdUkHo2MeJCv1NznsnYZp53qqa778LQ7MiB5saMipS9rA pIB2KDJZ8aK30grt1Ybv6inKel1ayXbpzDVfRtWRgwYAVyQm+rY+mqSlosJsYsYk nA1mpOLZ94e95y9VfqrgQoNHUkUTHhOLcGlR3PSEduMFXf5fdAfb0ikH+r0XiwZ/ ltmrVdWf/u+W3eTj6nX4WeXy6ddjAP5GXUsPowvRlg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehjedguddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 8 Jan 2024 05:05:28 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 27d864aa (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 8 Jan 2024 10:02:52 +0000 (UTC) Date: Mon, 8 Jan 2024 11:05:26 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Karthik Nayak , Eric Sunshine Subject: [PATCH v2 1/6] refs: prepare `refs_init_db()` for initializing worktree refs Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The purpose of `refs_init_db()` is to initialize the on-disk files of a new ref database. The function is quite inflexible right now though, as callers can neither specify the `struct ref_store` nor can they pass any flags. Refactor the interface to accept both of these. This will be required so that we can start initializing per-worktree ref databases via the ref backend instead of open-coding the initialization in "worktree.c". Signed-off-by: Patrick Steinhardt --- refs.c | 6 ++---- refs.h | 2 +- refs/debug.c | 4 ++-- refs/files-backend.c | 4 +++- refs/packed-backend.c | 1 + refs/refs-internal.h | 4 +++- setup.c | 2 +- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/refs.c b/refs.c index fdbf5f4cb1..254272ba6f 100644 --- a/refs.c +++ b/refs.c @@ -1944,11 +1944,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, } /* backend functions */ -int refs_init_db(struct strbuf *err) +int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err) { - struct ref_store *refs = get_main_ref_store(the_repository); - - return refs->be->init_db(refs, err); + return refs->be->init_db(refs, flags, err); } const char *resolve_ref_unsafe(const char *refname, int resolve_flags, diff --git a/refs.h b/refs.h index 916b874ae3..114caa272a 100644 --- a/refs.h +++ b/refs.h @@ -126,7 +126,7 @@ int should_autocreate_reflog(const char *refname); int is_branch(const char *refname); -int refs_init_db(struct strbuf *err); +int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err); /* * Return the peeled value of the oid currently being iterated via diff --git a/refs/debug.c b/refs/debug.c index b9775f2c37..634681ca44 100644 --- a/refs/debug.c +++ b/refs/debug.c @@ -33,10 +33,10 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor return (struct ref_store *)res; } -static int debug_init_db(struct ref_store *refs, struct strbuf *err) +static int debug_init_db(struct ref_store *refs, int flags, struct strbuf *err) { struct debug_ref_store *drefs = (struct debug_ref_store *)refs; - int res = drefs->refs->be->init_db(drefs->refs, err); + int res = drefs->refs->be->init_db(drefs->refs, flags, err); trace_printf_key(&trace_refs, "init_db: %d\n", res); return res; } diff --git a/refs/files-backend.c b/refs/files-backend.c index 43fd0ac760..153efe6662 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3220,7 +3220,9 @@ static int files_reflog_expire(struct ref_store *ref_store, return -1; } -static int files_init_db(struct ref_store *ref_store, struct strbuf *err UNUSED) +static int files_init_db(struct ref_store *ref_store, + int flags UNUSED, + struct strbuf *err UNUSED) { struct files_ref_store *refs = files_downcast(ref_store, REF_STORE_WRITE, "init_db"); diff --git a/refs/packed-backend.c b/refs/packed-backend.c index 8d1090e284..217f052d34 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -1246,6 +1246,7 @@ static const char PACKED_REFS_HEADER[] = "# pack-refs with: peeled fully-peeled sorted \n"; static int packed_init_db(struct ref_store *ref_store UNUSED, + int flags UNUSED, struct strbuf *err UNUSED) { /* Nothing to do. */ diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 8e9f04cc67..82219829b0 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -529,7 +529,9 @@ typedef struct ref_store *ref_store_init_fn(struct repository *repo, const char *gitdir, unsigned int flags); -typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err); +typedef int ref_init_db_fn(struct ref_store *refs, + int flags, + struct strbuf *err); typedef int ref_transaction_prepare_fn(struct ref_store *refs, struct ref_transaction *transaction, diff --git a/setup.c b/setup.c index 1ab1a66bcb..6c8f656f7c 100644 --- a/setup.c +++ b/setup.c @@ -1943,7 +1943,7 @@ void create_reference_database(unsigned int ref_storage_format, adjust_shared_perm(git_path("refs")); repo_set_ref_storage_format(the_repository, ref_storage_format); - if (refs_init_db(&err)) + if (refs_init_db(get_main_ref_store(the_repository), 0, &err)) die("failed to set up refs db: %s", err.buf); /* From patchwork Mon Jan 8 10:05:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13513242 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80A5614266 for ; Mon, 8 Jan 2024 10:05:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="g4zmtaX3"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="sEQJRLtG" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 5F51C5C17B9; Mon, 8 Jan 2024 05:05:33 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 08 Jan 2024 05:05:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1704708333; x=1704794733; bh=s4xbS+L9qu J9xOdGKy77LMJFrpA3anHhlbHcNIQgu7w=; b=g4zmtaX3rF1PzYUa3+5SuChwew PHc3IT84O+KW+wMPcbHN5PTy76gTgDR+SyKOQ+M4VQs0ne8YuPwWKEDv5u9lrGLl D43JE1BMpgAsRbz7jHBk1anQeLGQe6+ayzYr83Nin+wR+WQVHEkvIAJQopUc/cMU 9cUqvMIQBjAr0V7SotHqcAPhJbBlgX5++Z/GyRRaDa3M+EDkYJt9vPk8EbXLJDXo U8nExi4s4U4Vv1VjWdQgrGB/UnuCvPnLIH8F8dnMVW4KzEoOVBIJp0j58WNI21xh MOpKGzmwIQy8ZspYlibLfKiV0GWO0eIiU34q+0E6s7gHafs4T+sM60B61vgg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1704708333; x=1704794733; bh=s4xbS+L9quJ9xOdGKy77LMJFrpA3 anHhlbHcNIQgu7w=; b=sEQJRLtGXVZ+3mPEb7ACcKrxlwkyRLoSYzvuMwPaR+yz 8o9xm0ca7bPI7dgPiTR5qzI9oEF8ZtjYRwiw8JABhW5ghUomO700hzJlGN8jYqEO 2FW6szMiiZSHxi5eeKIHqM3s8TQKUM4ximKI9nc/oOycB4d+qxNFTf6COmI+9Tw+ +0kmsQpQBKtHn6SjkTIJsM+ni9Ktm0F1caqgjBl+GghHLiW0zv/fMYaGSKV5o9lg 8PyVd/eaS1e4porefAH5OKxJ0m+qFC9lZ3gc3RmBj7ZnlaZp28nJvV3Nosv/MXRD lyHdh95DzNamIGLjxCg1DtVpeewdsrluZZas/aQYxA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehjedguddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 8 Jan 2024 05:05:32 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 64c7f77c (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 8 Jan 2024 10:02:56 +0000 (UTC) Date: Mon, 8 Jan 2024 11:05:30 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Karthik Nayak , Eric Sunshine Subject: [PATCH v2 2/6] setup: move creation of "refs/" into the files backend Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: When creating the ref database we unconditionally create the "refs/" directory in "setup.c". This is a mandatory prerequisite for all Git repositories regardless of the ref backend in use, because Git will be unable to detect the directory as a repository if "refs/" doesn't exist. We are about to add another new caller that will want to create a ref database when creating worktrees. We would require the same logic to create the "refs/" directory even though the caller really should not care about such low-level details. Ideally, the ref database should be fully initialized after calling `refs_init_db()`. Move the code to create the directory into the files backend itself to make it so. This means that future ref backends will also need to have equivalent logic around to ensure that the directory exists, but it seems a lot more sensible to have it this way round than to require callers to create the directory themselves. An alternative to this would be to create "refs/" in `refs_init_db()` directly. This feels conceptually unclean though as the creation of the refdb is now cluttered across different callsites. Furthermore, both the "files" and the upcoming "reftable" backend write backend-specific data into the "refs/" directory anyway, so splitting up this logic would only make it harder to reason about. Signed-off-by: Patrick Steinhardt --- refs/files-backend.c | 17 +++++++++++++++++ setup.c | 15 --------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 153efe6662..054ecdbca3 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3228,9 +3228,26 @@ static int files_init_db(struct ref_store *ref_store, files_downcast(ref_store, REF_STORE_WRITE, "init_db"); struct strbuf sb = STRBUF_INIT; + /* + * We need to create a "refs" dir in any case so that older versions of + * Git can tell that this is a repository. This serves two main purposes: + * + * - Clients will know to stop walking the parent-directory chain when + * detecting the Git repository. Otherwise they may end up detecting + * a Git repository in a parent directory instead. + * + * - Instead of failing to detect a repository with unknown reference + * format altogether, old clients will print an error saying that + * they do not understand the reference format extension. + */ + strbuf_addf(&sb, "%s/refs", ref_store->gitdir); + safe_create_dir(sb.buf, 1); + adjust_shared_perm(sb.buf); + /* * Create .git/refs/{heads,tags} */ + strbuf_reset(&sb); files_ref_path(refs, &sb, "refs/heads"); safe_create_dir(sb.buf, 1); diff --git a/setup.c b/setup.c index 6c8f656f7c..abb271e017 100644 --- a/setup.c +++ b/setup.c @@ -1927,21 +1927,6 @@ void create_reference_database(unsigned int ref_storage_format, struct strbuf err = STRBUF_INIT; int reinit = is_reinit(); - /* - * We need to create a "refs" dir in any case so that older versions of - * Git can tell that this is a repository. This serves two main purposes: - * - * - Clients will know to stop walking the parent-directory chain when - * detecting the Git repository. Otherwise they may end up detecting - * a Git repository in a parent directory instead. - * - * - Instead of failing to detect a repository with unknown reference - * format altogether, old clients will print an error saying that - * they do not understand the reference format extension. - */ - safe_create_dir(git_path("refs"), 1); - adjust_shared_perm(git_path("refs")); - repo_set_ref_storage_format(the_repository, ref_storage_format); if (refs_init_db(get_main_ref_store(the_repository), 0, &err)) die("failed to set up refs db: %s", err.buf); From patchwork Mon Jan 8 10:05:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13513243 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A26F71400F for ; Mon, 8 Jan 2024 10:05:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="V974oVQU"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Q7IXRSgK" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id B2E7C5C17BC; Mon, 8 Jan 2024 05:05:37 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Mon, 08 Jan 2024 05:05:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1704708337; x=1704794737; bh=kRKIjfy0Xu uAM0F79enq0MA25gw+f/ErEj+yqZEvgJ0=; b=V974oVQUbU748e2w5Aq1b97wAM uJq26qKy38+xXxJfitrPxVlvMClDUz5mmQoUz/uZxXPiWw9Meep1Bfb2sUjwpMgX HC9EhXUJYqilljBNUN7BZeKlpJ6z3T7DlzMhda2rzzXI5Y6ws1CupM+G6TPovDLl 4DXo0vvEPoTcYsN1ikk9J3enEDbo6X7znxXdzPjGdyQFdUJJ2SEg9u7/XSS9+9rT rr4gWbM1wfg2gKyXhNwmTwKbNugAtr4Y0xnTIIXUuC5wkjY4RrhMMPcnfVSXL6ZE 8F7JjpKWlSUlepXsyfKNPXXw2LrGa/E2jGNlmfRA6TdlNEPpZd83G8vwwBGg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1704708337; x=1704794737; bh=kRKIjfy0XuuAM0F79enq0MA25gw+ f/ErEj+yqZEvgJ0=; b=Q7IXRSgKHNY8F6PFVxFZhksH9RzrWyHOp+0ulfFwJx8+ MwtqI9SSt07u33RJoJAOPjRWl0Fb3fLsUagcfwmlNUJ/GwTePuXoNOiXsLoKtQxa PTnwkkfnCjVdJq2FnuwxPD/Ya3bW/+2ei8FaQw5wqO6mhzNbPSfwNxUVDrJQepjh 2kKpZ58eeSxACTSPDGA8NgO61OtvobAQGVSM3bXQsflFHSGMLki96RF3KM6IxUX+ reRjkfzB0j0aOI/SlqccqQzLgQCYBDM1Y8qZJ/r7DIOda5ExqzWHyjW5qCZgJ571 gQLfqW6GJyqIY2TzIZ6HCuihqxZSITzCmQ2Qd8GkxQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 8 Jan 2024 05:05:36 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id db3492e0 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 8 Jan 2024 10:03:00 +0000 (UTC) Date: Mon, 8 Jan 2024 11:05:35 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Karthik Nayak , Eric Sunshine Subject: [PATCH v2 3/6] refs/files: skip creation of "refs/{heads,tags}" for worktrees Message-ID: <9e99efeaa384286a51a34993fa47f5c42bd81fa0.1704705733.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The files ref backend will create both "refs/heads" and "refs/tags" in the Git directory. While this logic makes sense for normal repositories, it does not for worktrees because those refs are "common" refs that would always be contained in the main repository's ref database. Introduce a new flag telling the backend that it is expected to create a per-worktree ref database and skip creation of these dirs in the files backend when the flag is set. No other backends (currently) need worktree-specific logic, so this is the only required change to start creating per-worktree ref databases via `refs_init_db()`. Signed-off-by: Patrick Steinhardt --- refs.h | 2 ++ refs/files-backend.c | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/refs.h b/refs.h index 114caa272a..c2dfe451a1 100644 --- a/refs.h +++ b/refs.h @@ -126,6 +126,8 @@ int should_autocreate_reflog(const char *refname); int is_branch(const char *refname); +#define REFS_INIT_DB_IS_WORKTREE (1 << 0) + int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err); /* diff --git a/refs/files-backend.c b/refs/files-backend.c index 054ecdbca3..6dae37e351 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3221,7 +3221,7 @@ static int files_reflog_expire(struct ref_store *ref_store, } static int files_init_db(struct ref_store *ref_store, - int flags UNUSED, + int flags, struct strbuf *err UNUSED) { struct files_ref_store *refs = @@ -3245,15 +3245,21 @@ static int files_init_db(struct ref_store *ref_store, adjust_shared_perm(sb.buf); /* - * Create .git/refs/{heads,tags} + * There is no need to create directories for common refs when creating + * a worktree ref store. */ - strbuf_reset(&sb); - files_ref_path(refs, &sb, "refs/heads"); - safe_create_dir(sb.buf, 1); + if (!(flags & REFS_INIT_DB_IS_WORKTREE)) { + /* + * Create .git/refs/{heads,tags} + */ + strbuf_reset(&sb); + files_ref_path(refs, &sb, "refs/heads"); + safe_create_dir(sb.buf, 1); - strbuf_reset(&sb); - files_ref_path(refs, &sb, "refs/tags"); - safe_create_dir(sb.buf, 1); + strbuf_reset(&sb); + files_ref_path(refs, &sb, "refs/tags"); + safe_create_dir(sb.buf, 1); + } strbuf_release(&sb); return 0; From patchwork Mon Jan 8 10:05:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13513244 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1989A14A8D for ; Mon, 8 Jan 2024 10:05:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="OKVc/T5Y"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="uYkSuIKE" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 386855C17BC; Mon, 8 Jan 2024 05:05:42 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 08 Jan 2024 05:05:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1704708342; x=1704794742; bh=rq39K/0IFR FERDws20nMVI9KKN0Ty1Qanko7HeTZ4wI=; b=OKVc/T5YyOTpar7KsUwdAs7w6j /b6FEOwr9PZRWBRO2oiR3WIQGF7w4g1KRJatHG+H2sjATgUnvW6We83zHGeYkYMg QWNZKO/mmdF8jtFHxiR+RQEmpElYJDzsKlnLj1WfsJot1b8PSmS1LdBuCpxReKJy Op4ffpsGsehyNZ5oPalHorl6RJ3F9EheyTjn2EpZQQvZPRe+RxrCc0voy+p9lJ3F NDWAtVE5iL7NlePSXzXM7u7RuC5h5jBYtHQ9xIs6sRQs7eXDwGCp7YTK5tCkLtQv 53W1O9UUKN8wIkmZDBzRWVzK2rlp1l0b+MglPa+Bv1qtBRhd7C0z62ld+drw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1704708342; x=1704794742; bh=rq39K/0IFRFERDws20nMVI9KKN0T y1Qanko7HeTZ4wI=; b=uYkSuIKEjRl6oKceKe53Zv4memsuVLWQk3LIuscHyh8c LjRvIjvQEEUGcdtKGjvG1Q6wZztSNH5DFL8HzLtLu4YzKHN3O5L79rRJ0ryrdc1s hT77QJ1co4JqDRXa8JzODaT2/xX3XZqEw9WczQuj96pc2x2CpHNnuTNCe0M8876z nBtMRxEZANHYNs+bhH5YzAv8j3JdEpZ1gnJGrYeQSUsptFFIYupSNwH6vqQJ6W1W ab6TIa681kI6ylFD4TigzTEMjWmkw87RBRXSmNRWrFTSp1Sn41PMcuYiAi/OWYDv 0QbCTS+3dhy/DHYY6BNJ0DJym3XKB5Jn6Hi06ZZzBg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 8 Jan 2024 05:05:40 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id b5c5205f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 8 Jan 2024 10:03:04 +0000 (UTC) Date: Mon, 8 Jan 2024 11:05:39 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Karthik Nayak , Eric Sunshine Subject: [PATCH v2 4/6] builtin/worktree: move setup of commondir file earlier Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Shuffle around how we create supporting worktree files so that we first ensure that the worktree has all link files ("gitdir", "commondir") before we try to initialize the ref database by writing "HEAD". This will be required by a subsequent commit where we start to initialize the ref database via `refs_init_db()`, which will require an initialized `struct worktree *`. Signed-off-by: Patrick Steinhardt --- builtin/worktree.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index 4ac1621541..58937a2a68 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -495,6 +495,10 @@ static int add_worktree(const char *path, const char *refname, strbuf_realpath(&realpath, get_git_common_dir(), 1); write_file(sb_git.buf, "gitdir: %s/worktrees/%s", realpath.buf, name); + strbuf_reset(&sb); + strbuf_addf(&sb, "%s/commondir", sb_repo.buf); + write_file(sb.buf, "../.."); + /* * This is to keep resolve_ref() happy. We need a valid HEAD * or is_git_directory() will reject the directory. Any value which @@ -505,9 +509,6 @@ static int add_worktree(const char *path, const char *refname, strbuf_reset(&sb); strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); write_file(sb.buf, "%s", oid_to_hex(null_oid())); - strbuf_reset(&sb); - strbuf_addf(&sb, "%s/commondir", sb_repo.buf); - write_file(sb.buf, "../.."); /* * If the current worktree has sparse-checkout enabled, then copy From patchwork Mon Jan 8 10:05:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13513245 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D85B14A90 for ; Mon, 8 Jan 2024 10:05:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="anJwvVMR"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="2LKoM+hi" Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 939E35C17BF; Mon, 8 Jan 2024 05:05:46 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 08 Jan 2024 05:05:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1704708346; x=1704794746; bh=yS4bNgNyoO WJgSOZGpQCn0dV8NLN59V9hdpmjptq+hM=; b=anJwvVMRbdEafQ3K93HXpsEZf0 KY6sxLzAQ0hPywTDRnwyBXWRa+h8PHZjaGZKSl/1ycxBuSmTuIcXpGzf1OAxndUR UDPh6s9UTszwBdjCzoWu1kpDp/q243/RFJKLAZGjB5Lvu3xxOeeehzwoCynVfTax W0NU1kMhXGmZMjvchS7xmBxoNDLxTbjHJ5vtuZsPXfYaYxbMRUis3DpVcY/pcR+W FebcEhBR2hwPY8F0biXEoikjS4NYzBUUpFaTWhDuPQFyz+woePWDaRBDIx2UyVCs ni1kV7iwpH5QSs8B4Zw5wAVMOtucE5F9KiIU5aHswAixkW6DQjt9iDGe+KWg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1704708346; x=1704794746; bh=yS4bNgNyoOWJgSOZGpQCn0dV8NLN 59V9hdpmjptq+hM=; b=2LKoM+hi1FyDKxnXjP4WGNTWTP17qO4hFWFPgi1JFAw8 WKKfNOqz9KBqXxwtf9F8eLoK4+Lkq7uT9kKkRL27KCYgHe2OvbOq3NXL1uymBE+S /W46WA1dw2c2tu/SfqeFMaheN+tkW9bZUKM7fN3LvzsmSY6Jfz7PrlLdmgAZpcoJ V8xmBgSijGWShyo4bGV4LiwAMz+8jhQv3cI1k8MNWj1D2j+7Q+2pLZPkoDP1Bv2+ yyWJzvzgSx5hOnUvdSJLP+M2BX2Bid6JtYJbzomD1ynsODw3cuSmtI15pl2KjrMX /fONPh9b8wD89a+3LfxUvE8dPozbyrhCSUKLERw1hQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 8 Jan 2024 05:05:45 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id b088f2c9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 8 Jan 2024 10:03:09 +0000 (UTC) Date: Mon, 8 Jan 2024 11:05:43 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Karthik Nayak , Eric Sunshine Subject: [PATCH v2 5/6] worktree: expose interface to look up worktree by name Message-ID: <5525a9f9c2dbc424ef5ba4f766eb6143185bfe5d.1704705733.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Our worktree interfaces do not provide a way to look up a worktree by its name. Expose `get_linked_worktree()` to allow for this usecase. As callers are responsible for freeing this worktree, introduce a new function `free_worktree()` that does so. Signed-off-by: Patrick Steinhardt --- worktree.c | 27 ++++++++++++++++----------- worktree.h | 12 ++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/worktree.c b/worktree.c index cc34a3419b..5d5dd46609 100644 --- a/worktree.c +++ b/worktree.c @@ -12,18 +12,23 @@ #include "wt-status.h" #include "config.h" +void free_worktree(struct worktree *worktree) +{ + if (!worktree) + return; + free(worktree->path); + free(worktree->id); + free(worktree->head_ref); + free(worktree->lock_reason); + free(worktree->prune_reason); + free(worktree); +} + void free_worktrees(struct worktree **worktrees) { int i = 0; - - for (i = 0; worktrees[i]; i++) { - free(worktrees[i]->path); - free(worktrees[i]->id); - free(worktrees[i]->head_ref); - free(worktrees[i]->lock_reason); - free(worktrees[i]->prune_reason); - free(worktrees[i]); - } + for (i = 0; worktrees[i]; i++) + free_worktree(worktrees[i]); free (worktrees); } @@ -75,8 +80,8 @@ static struct worktree *get_main_worktree(int skip_reading_head) return worktree; } -static struct worktree *get_linked_worktree(const char *id, - int skip_reading_head) +struct worktree *get_linked_worktree(const char *id, + int skip_reading_head) { struct worktree *worktree = NULL; struct strbuf path = STRBUF_INIT; diff --git a/worktree.h b/worktree.h index ce45b66de9..f14784a2ff 100644 --- a/worktree.h +++ b/worktree.h @@ -57,6 +57,13 @@ struct worktree *find_worktree(struct worktree **list, const char *prefix, const char *arg); +/* + * Look up the worktree corresponding to `id`, or NULL of no such worktree + * exists. + */ +struct worktree *get_linked_worktree(const char *id, + int skip_reading_head); + /* * Return the worktree corresponding to `path`, or NULL if no such worktree * exists. @@ -134,6 +141,11 @@ void repair_worktrees(worktree_repair_fn, void *cb_data); */ void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data); +/* + * Free up the memory for a worktree. + */ +void free_worktree(struct worktree *); + /* * Free up the memory for worktree(s) */ From patchwork Mon Jan 8 10:05:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13513246 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D63C4210F2 for ; Mon, 8 Jan 2024 10:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="gkeKAhtc"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="S1qSJJOK" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id E893C5C17C3; Mon, 8 Jan 2024 05:05:50 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 08 Jan 2024 05:05:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1704708350; x=1704794750; bh=gX7stEsAK4 V1XFenI/L6h4u3PJkj3hggP8LgwP3VYis=; b=gkeKAhtcdS8eLoMXqzZVHgRWOQ KkgRVYjmLm6GPR5YrEz/XAAZUFb2nvS9mPmSjMlSdxXVYFCKlVrUTDOOmr+4mtUJ AuT269txnkqPAK6VAes9ZRXus8YklZaYtIHOibCduy3wkQQg+Xswec+YINPAE7Pd JPjLwts5M6Y7aUTyDnfco4HyFgD9E5dUAHwVI3n7XigR7HYiAJMy6xczp3wsJUUT PzJehN6YsmU4oEA44p7xqru+dW6rN+Ek9An4iiyItLuDbKIVrsd6juyyyj96zUc+ 6ZPG4iT5a4mIlACZhfG5bUhk8LnCF0iCYceDPhAM9YXS/86Bggsjfg0DpSlw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1704708350; x=1704794750; bh=gX7stEsAK4V1XFenI/L6h4u3PJkj 3hggP8LgwP3VYis=; b=S1qSJJOKXZmUo9c9K14Ouchyzu6XqrQ0HIP0QzjyPjYy 2obzRrYjrSq2wD38CTdKl34hP1l/WcYz1lnlf9UJ0az92C2qFK6WCEVy0ruB18aU 84JC8Za/ISVzUr7LT6JWSmCZTIu9HtL77EiegKXEe48UMLiwpHn6uDxuGbRS/uq+ q1NoKo0Vovh6q4Jzpi/Ah9/3leht5Qbbd4XPKQJGBq25gU6LfGTNrjCG1apYYXr5 aj/lCuYXii5dfF8mkutQfNjF3fzsGxabS/scrXdkMIB8UaekKwcrRD3A5kq6QJDz ffKhM5MdLoCAkui9crPeqLPUmwCjzuQeksj98n34Mw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehjedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 8 Jan 2024 05:05:49 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id d370fb0a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 8 Jan 2024 10:03:13 +0000 (UTC) Date: Mon, 8 Jan 2024 11:05:47 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Karthik Nayak , Eric Sunshine Subject: [PATCH v2 6/6] builtin/worktree: create refdb via ref backend Message-ID: <240dc40de18a79c45192466289c4e1d85d90e901.1704705733.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: When creating a worktree we create the worktree's ref database manually by first writing a "HEAD" file so that the directory is recognized as a Git repository by other commands, and then running git-update-ref(1) or git-symbolic-ref(1) to write the actual value. But while this is fine for the files backend, this logic simply assumes too much about how the ref backend works and will leave behind an invalid ref database once any other ref backend lands. Refactor the code to instead use `refs_init_db()` to initialize the ref database so that git-worktree(1) itself does not need to know about how to initialize it. This will allow future ref backends to customize how the per-worktree ref database is set up. Furthermore, as we now already have a worktree ref store around, we can also avoid spawning external commands to write the HEAD reference and instead use the refs API to do so. Note that we do not have an equivalent to passing the `--quiet` flag to git-symbolic-ref(1) as we did before. This flag does not have an effect anyway though, as git-symbolic-ref(1) only honors it when reading a symref, but never when writing one. Signed-off-by: Patrick Steinhardt --- builtin/worktree.c | 48 +++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index 58937a2a68..dd10446d81 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -416,7 +416,6 @@ static int add_worktree(const char *path, const char *refname, struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT; const char *name; - struct child_process cp = CHILD_PROCESS_INIT; struct strvec child_env = STRVEC_INIT; unsigned int counter = 0; int len, ret; @@ -424,7 +423,8 @@ static int add_worktree(const char *path, const char *refname, struct commit *commit = NULL; int is_branch = 0; struct strbuf sb_name = STRBUF_INIT; - struct worktree **worktrees; + struct worktree **worktrees, *wt = NULL; + struct ref_store *wt_refs; worktrees = get_worktrees(); check_candidate_path(path, opts->force, worktrees, "add"); @@ -500,15 +500,26 @@ static int add_worktree(const char *path, const char *refname, write_file(sb.buf, "../.."); /* - * This is to keep resolve_ref() happy. We need a valid HEAD - * or is_git_directory() will reject the directory. Any value which - * looks like an object ID will do since it will be immediately - * replaced by the symbolic-ref or update-ref invocation in the new - * worktree. + * Set up the ref store of the worktree and create the HEAD reference. */ - strbuf_reset(&sb); - strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); - write_file(sb.buf, "%s", oid_to_hex(null_oid())); + wt = get_linked_worktree(name, 1); + if (!wt) { + ret = error(_("could not find created worktree '%s'"), name); + goto done; + } + wt_refs = get_worktree_ref_store(wt); + + ret = refs_init_db(wt_refs, REFS_INIT_DB_IS_WORKTREE, &sb); + if (ret) + goto done; + + if (!is_branch && commit) + 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); + if (ret) + goto done; /* * If the current worktree has sparse-checkout enabled, then copy @@ -527,22 +538,6 @@ static int add_worktree(const char *path, const char *refname, strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); - cp.git_cmd = 1; - - if (!is_branch && commit) { - strvec_pushl(&cp.args, "update-ref", "HEAD", - oid_to_hex(&commit->object.oid), NULL); - } else { - strvec_pushl(&cp.args, "symbolic-ref", "HEAD", - symref.buf, NULL); - if (opts->quiet) - strvec_push(&cp.args, "--quiet"); - } - - strvec_pushv(&cp.env, child_env.v); - ret = run_command(&cp); - if (ret) - goto done; if (opts->orphan && (ret = make_worktree_orphan(refname, opts, &child_env))) @@ -588,6 +583,7 @@ static int add_worktree(const char *path, const char *refname, strbuf_release(&sb_git); strbuf_release(&sb_name); strbuf_release(&realpath); + free_worktree(wt); return ret; }