From patchwork Thu Dec 28 09:59:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13505789 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 D727F6FAB for ; Thu, 28 Dec 2023 09:59:54 +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="TfAbnIVx"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="T68rvJZT" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id EBCC25C0085 for ; Thu, 28 Dec 2023 04:59:53 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 28 Dec 2023 04:59:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1703757593; x=1703843993; bh=j8h3XSBNUC wJJsubKxBRsOxn9lll4c6gsTaFRb8eJb0=; b=TfAbnIVxkcJmQV98A1IdjqGPdg NYTRP6mjtloPkk1WsNGwSqG74MktpjYds6dyUlcLVAvyVy/IvDDZ43OLPtOpEL+2 7XansR+vBs+7WxFYR2pqRFFAPeHO3O3evG0oS0bdTlSHYKT59ZFgx/rpa25VKqFa j1ujrb2sB4duCce/5M9nf4tUYS20SE5TgTbx7KkfT2ubEfv1CxyYFkZkZwsRj5dT /hrU6i8t0oWfLbJtMYvNLEW5y/76bwF8Wov0JYOPXGOsPovREENe6GyyDF3+0sCo hzZZQiImY96l/tkGkcIJuO4TtawowSwEKpMapLO8anisD1qYufYeuEtvBfYA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1703757593; x=1703843993; bh=j8h3XSBNUCwJJsubKxBRsOxn9lll 4c6gsTaFRb8eJb0=; b=T68rvJZThJ6TM5qRGYI8aD1mbRTO8VmdpoqrHZ2gmzFz CdNuKJfKeb/OTfqtKTcKoe0rySHkwFkXfI8fA3OH7PEqm1OYQgsf032qKyGyagFp 2sG5+blz02kzzFMoViHC0wmGl7kqsRFbplbCZiLNwoWIWVHo9pSi5uMQ8XDapehS Ao4kMb/9h3m7Ve/zvFrgL/m4aqXgyWKNC8uLHe+sOIjV30T5Aqj5but5t8gx2pUr CJNdRRu7s37aKhOqo00D9opFG022zNd1xPhYmyHe8hGTG5xwUmZ4G1nply93X5Hk ey2yJ32sMlhYWfLRO0Uyt6OYUG5ZvoqoD1RkV/U+ug== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdefuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 28 Dec 2023 04:59:53 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 96b02384 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 28 Dec 2023 09:57:39 +0000 (UTC) Date: Thu, 28 Dec 2023 10:59:51 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 1/6] refs: prepare `refs_init_db()` for initializing worktree refs Message-ID: <6cb4e0a99fff52c35ea293f855fd3a34bcba7ab1.1703754513.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 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 16bfa21df7..87e2659db7 100644 --- a/refs.c +++ b/refs.c @@ -1928,11 +1928,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 ff113bb12a..6090e92578 100644 --- a/refs.h +++ b/refs.h @@ -123,7 +123,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 83b7a0ba65..c061def8b5 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 ad8b1d143f..387eeb5037 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 b9fa097a29..3f10bccf44 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 4af83bf9a5..a33b28c19d 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 bc90bbd033..a4eb2a38ac 100644 --- a/setup.c +++ b/setup.c @@ -1919,7 +1919,7 @@ void create_reference_database(const char *initial_branch, int quiet) safe_create_dir(git_path("refs"), 1); adjust_shared_perm(git_path("refs")); - 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 Thu Dec 28 09:59:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13505790 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 85C6E6FC3 for ; Thu, 28 Dec 2023 09:59:59 +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="HLVKBmgc"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="JAoI8Lgw" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id CFF215C009A for ; Thu, 28 Dec 2023 04:59:58 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 28 Dec 2023 04:59:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1703757598; x=1703843998; bh=fxKrwS3vh3 ofL/RRvCENk+WHQYo5/52Qf+L1olVvcoY=; b=HLVKBmgcIWejh7yopX6Yv9TAgn plYpSCdFU20bDtPeMwjIpTWG0IlsfDdD/UK4USKvm0/7BTwOz9rd0xvDWY2G8g5o O2ugtYUdNV+ahhME4aibLSvk3x/W3aKmxpJa1OiugDQsgn7x0BDvc9KSE2W36saC ZYDXDeD4L0Nwz2Q7vNUK2h4YpDyDaYjG2tDFeD7S2d0NUhcCggzsUTtmOJzy1SMd jjbPN/tj2lPdFtMyniF0bbEu2NTIGn7iUnm2homOC1GSY3RjGUWjgZylXTDBOlKO Mxw043uEf9LE0aOD5V76uKU9Vux265iV+GO9qmAWRu0L/Zrc9N05PWYKTqBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1703757598; x=1703843998; bh=fxKrwS3vh3ofL/RRvCENk+WHQYo5 /52Qf+L1olVvcoY=; b=JAoI8Lgwqgx3Zitl0X9hDNqu5NSoRuoXknxltN63mqU/ 5mq26ke2yOFEgcqSrhuNC56O6rKRK2rq8eTCcKhRzXR37Cmtj0jPg9iS3lyPjjQm NX/487CMv2UN0wleUqviuVPk0OUfvSNeaR9THXPQWMlox7Xaqdq935Bv3MmBIo3c QVZ4XBWzj7XsAPYlYpZUF/fH3sLJGa8DKObhH2EVqKyCmI3unM2KfSkDhKDmKG3a 11PEtpveZPhVZ8kcdnOnNhnhM3Fcd2oznaU6KXno7Wqu8ZZPRWvrwXFHp376jT7q YqjJbpO+r6FOQfJFbWTQy47wokx/dbk0DsBzgNJDZA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdefuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 28 Dec 2023 04:59:58 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id a583fa65 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 28 Dec 2023 09:57:43 +0000 (UTC) Date: Thu, 28 Dec 2023 10:59:56 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 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. 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 387eeb5037..ed47c5dc08 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 a4eb2a38ac..f2d55994e2 100644 --- a/setup.c +++ b/setup.c @@ -1904,21 +1904,6 @@ void create_reference_database(const char *initial_branch, int quiet) 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")); - if (refs_init_db(get_main_ref_store(the_repository), 0, &err)) die("failed to set up refs db: %s", err.buf); From patchwork Thu Dec 28 10:00:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13505791 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 77D6D6FAC for ; Thu, 28 Dec 2023 10:00:03 +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="Sef1pxP2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="jZlhvSZN" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id CF81C5C00A2 for ; Thu, 28 Dec 2023 05:00:02 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 28 Dec 2023 05:00:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1703757602; x=1703844002; bh=iwvaUQHaTE LOPKhtFF6yxqrh7pyW71euzw/BNKYQj84=; b=Sef1pxP2xS1beEpKhY58rTlmiR hM+QYmsnp5mew1huCAxskcH9AE7Xzkx8fMu5IWKi1InqyzeLohT4rx6Wc4H37GyM o5LGrKTcQl0Fz1zwOFG0lIen9k8kw7sIB5L811dfdOUKoU/I7LJUjoqJwDPnM+L1 eFCc+cqLx6a/DtKdAQMhfw3nFqCZ0hfp/jVWsN08a68qFZJZeeaIzCd+q7MaY+nE dQJN+Jxah/528q9sMXZNbcl4XQEyxnRJZIqubr6++TfY9jZBguS4/6Dv2rSIKe4h IIhIrkovHvZEALLkm2LrAktJdk6SkLFoabFeEelL+0LeEAk4nSmIHKzFHIZA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1703757602; x=1703844002; bh=iwvaUQHaTELOPKhtFF6yxqrh7pyW 71euzw/BNKYQj84=; b=jZlhvSZNxFQSsnflLeYeo6Cfy5D4Y7QCF87g5EIp3cFg YXCMt3+8ysGAffJUFzsfQY14QE18ARft8pOfFYJkXH9kg6cvxpGaI7BDSB012Sw4 z4yRzkgD9olIapV2AqzkoI54RpOxfSZbaKUANRH/KNck9IfzNKp0GVh7HwPBkCm1 ullRJ4y6N1Mevng+Oz9Pgbp/ZkzMjfWPqGJ2h2ZRA5IServd9/9ORL/lSG55IVy6 BNZVGWogH/BnNcClKD+acPPsZDdyOMPWbKHxkd6SLKlALXtnFpjiug+lKk1VRS3k pZ95q9pBHcZZOlwOcKHLLKuuzV58fA4SLXa9OJXmQQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdefuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 28 Dec 2023 05:00:01 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 54f20a0f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 28 Dec 2023 09:57:48 +0000 (UTC) Date: Thu, 28 Dec 2023 11:00:00 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 3/6] refs/files: skip creation of "refs/{heads,tags}" for worktrees Message-ID: <3cf6ceb274d20ce76d0be65e4362a33579627052.1703754513.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 fo 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 6090e92578..8ed890841b 100644 --- a/refs.h +++ b/refs.h @@ -123,6 +123,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 ed47c5dc08..a82d6c453f 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 Thu Dec 28 10:00:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13505792 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 937EC6FAC for ; Thu, 28 Dec 2023 10:00:07 +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="VvfJS63l"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="1fvpl4vT" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id C037F5C00A5 for ; Thu, 28 Dec 2023 05:00:06 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 28 Dec 2023 05:00:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1703757606; x=1703844006; bh=Nn0WPkgjVx YWxUJ8/aEpygOfG+H9cMfHQPQWQW9nDlg=; b=VvfJS63lZSzDghSNRPaddGrGpT qXsFj8Co/cLUQXeBgUhEaV21IwGz9eiv13VYM3CeXWonBLMO4hOGWHI40GH0bHHM T1lzA5K1vTpCUmkaH5VnBEdUxjfo8jKZlHL+7CW1TC4kmnhLBqrH+T1n1I4xlzc5 xQqd8tboy6F3NUgYw0Xtu1+xAv3MOcZnkvl0RiKpnbbKkeG1cUxLXujJuTuh3V95 WFexfM/dTuzlnLljAaaDOziJOMUIcaccksbKqLdDpqGbUcfFk6w3q3gDNZgB6EKt JdLZoYBMtXnMpCSrL97iWq7cM5ydXmWOJR7LZIFuFsaE8zTZd+6vkR/K/vxg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1703757606; x=1703844006; bh=Nn0WPkgjVxYWxUJ8/aEpygOfG+H9 cMfHQPQWQW9nDlg=; b=1fvpl4vTVK/drfZJoE/IazZnfTHaxVBIweIU2NU6pSRl iH/QqcOQNJLXZ7QSmJB9AZfIpf+zmr1vFHxeYwvoPcFjUjTkW2hA+E02uyN4ilCQ 6qEN0cduuFTPlzfOqa9pmBf9TC0mTUa2fenzCJpHaxJ+dT1LdqD92ijYzr7gcmOG rcvaQDNhu2oh99ScXpTq+MEN/IGRorfx2M+KpV6HVwoorYI7vit2XDW7q4OlNIeh H3uKxLDiYyh8JRDCjAKYWq2Zu2Zv+Lh3UVw49rAQOK3wrjk2C860WzVyj380hRq3 /zB5x3Va0cZU/uNJwVp4CrL8w0JL8as70accxj09jA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdefuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgepudenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 28 Dec 2023 05:00:05 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 4e2fe4e7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 28 Dec 2023 09:57:52 +0000 (UTC) Date: Thu, 28 Dec 2023 11:00:04 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 4/6] builtin/worktree: move setup of commondir file earlier Message-ID: <1a6337fc513f4c3750649a940a84be94b5855a35.1703754513.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: 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 Thu Dec 28 10:00:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13505793 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 5FCD36FA2 for ; Thu, 28 Dec 2023 10:00:12 +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="I3/D84z9"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="KY0astpN" Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id B3CAD5C0080 for ; Thu, 28 Dec 2023 05:00:11 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 28 Dec 2023 05:00:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1703757611; x=1703844011; bh=Uz+mJhMwGn z4Jebfyn7Np3s43vNh7O9hEbS1/8QDsUY=; b=I3/D84z9xVp55yc+E9yA+u2xWM WXfqy9OW0boukGQdYbi+2uUc2FVwTENaftgdok0bJN3JYYgB2BRxVHwNhohgARgO JM5TqwnvFIn5vq/48G5aQMvmrZ7tig4MuM/hxtIQRS5OSQMEE7Udw4FFrupHXrRG zsFjQqmNA8orrAMenvFvAQM7CtKzIrba/FVSR7bmVnQzYxX4i07IVqbmxjJP9ggK GG34jlfIl7n3f2x9wTYpw7zTWwHjZD1SzQR42P8Jy9NhDL6I5NL5bZbTvSq5q3Ix a6iazXVRGnkYadFFy6onucvQwPs3q1S27v9A0PiyQIygAMjUZlscoeSEAP9w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1703757611; x=1703844011; bh=Uz+mJhMwGnz4Jebfyn7Np3s43vNh 7O9hEbS1/8QDsUY=; b=KY0astpNYlw37aPvUvPI42qXoJGLawYK2rufxfbOh1vM SB4qCIvyh2vMim4j31hBfwTiSjYd3SzbdHwM3rqvjpOSiA4oHkIu9O0kkm3UEHkn vADCLogHhc60NvPNT+2ojBwmfvvJlho6Hggh4a+8cdT8+e991VKld0vJFxZHL41m N28RVvonz+sUZ/vJbV0Aym1MksezJuuS0p/0UMHwbV9vvutl8NMfUmFjMIlTxZTv +C8UPUKr75hIHgnVIs+tgIgxB6kY9ttKM4JfLTUWhBMWFynWqM4icGwfpJv3J1Gq ZvgCR+TTK/nJKJwNsLMYElfGjxIYuEp8M3WirnuODw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdefuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 28 Dec 2023 05:00:10 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 96dadba1 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 28 Dec 2023 09:57:56 +0000 (UTC) Date: Thu, 28 Dec 2023 11:00:09 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 5/6] worktree: expose interface to look up worktree by name 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: 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 | 25 +++++++++++++++---------- worktree.h | 11 +++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/worktree.c b/worktree.c index a56a6c2a3d..085f2cc41a 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); } @@ -74,7 +79,7 @@ static struct worktree *get_main_worktree(void) return worktree; } -static struct worktree *get_linked_worktree(const char *id) +struct worktree *get_linked_worktree(const char *id) { struct worktree *worktree = NULL; struct strbuf path = STRBUF_INIT; diff --git a/worktree.h b/worktree.h index ce45b66de9..8a75691eac 100644 --- a/worktree.h +++ b/worktree.h @@ -57,6 +57,12 @@ 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); + /* * Return the worktree corresponding to `path`, or NULL if no such worktree * exists. @@ -134,6 +140,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 Thu Dec 28 10:00:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13505794 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 6C2467464 for ; Thu, 28 Dec 2023 10:00:16 +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="rMjoeB1s"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="OyY3g92D" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 9D4805C0082 for ; Thu, 28 Dec 2023 05:00:15 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 28 Dec 2023 05:00:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1703757615; x=1703844015; bh=WTL4I6GSGW 6hOjVZXEDcxr8lvd9yVthR5I0I15Z4c3k=; b=rMjoeB1s4xkCL6KoIpWhCN5bwE /V7i4EXCBKUv2qMaqmZsJP9F375ISfza2tPQYHsmJWf2Axc6DJYr192FPfS3V2Pl +xbZgJzPNhHDHKulmORtgSywhlfRm2nvX0YniBIVhuVERycFd4wq3NtV2psCGxix oxUixfk65jINB/Y4d0KWrS3uH8qlcmqH9KiwWJt1ppBQf0HpmHTHyeMjJXLuhwTf ebgDP9GOdih3imRy7PDHrtrdiLzISljS50YW6DOJL8O0Uz9RbSuHd5join0EnV+w oGEU4I/B33U0jjmNhCkhWHFBiP+TXpb9dd1VZJb9LqAfQfO5grnHeh7WmkUg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1703757615; x=1703844015; bh=WTL4I6GSGW6hOjVZXEDcxr8lvd9y VthR5I0I15Z4c3k=; b=OyY3g92DCJBAc/K5qgHVQkWiJGwAU7Ti1qrcDi81EuLs W0NTeaZ1Q1QUwLzu/t6q3cx2rQv/3kyDF67Osg0gljwIMyjxNANIhHialCSNh7WS e+MLmC/x/PBWLiVx9uxQWOuqBEaxlgGHKCnHD1hUsx6Na6mKf+NHRWC04luQ4g8X Sya/XF15Yzv6mT8U/XOQyEv+DArVR2tQy36hBBPa1QY6I8+AnwUP9sAatnUXafeL rSc7Aam4hpV1uuWZHNbsaWrl8tGLzf+NZ5Y52s2SY53Vp2Ec7tudJqjka+v9Fhy6 Ey7T2zP8PthcE6/fPbGuForIHifwEg6QwWoe31Mwfg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdefuddgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgepudenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 28 Dec 2023 05:00:14 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 8ce601bc (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 28 Dec 2023 09:58:01 +0000 (UTC) Date: Thu, 28 Dec 2023 11:00:13 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 6/6] builtin/worktree: create refdb via ref 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 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..9d935bee84 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); + 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; }