From patchwork Mon Jun 3 09:30:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13683414 Received: from wfout4-smtp.messagingengine.com (wfout4-smtp.messagingengine.com [64.147.123.147]) (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 B234884DED for ; Mon, 3 Jun 2024 09:30:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.147 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717407056; cv=none; b=bro27Mb7fdVRYHgFt8UF/iIUxrTrBGdVDvisYBnBCimW4V59ATXnHYZJrg1CDBdrYF+NPtMpZUwKrCvTJjszb81OpMjTMDXHC9Jmnkja52dN1IhpWlLwQHg8zXNgYgNygjAvAT1W6chKK5l+ZbThnixjOarSrcHMPAY7UxFT/JM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717407056; c=relaxed/simple; bh=HkSxfCdoU0+D8rl7AdPRT6mgbvvL3+PefBnLqhhF2xg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=o1WqFTNy0GusG1+hZN8cdo4POhspwUjO69E/RxgdHs7NN8tYPTajxMr97ENNLzHfOheD5NxSLhiuNZKCoeNkVQkXkIry0RW1iQH8+JxQhkYkK8llut8PhnD45oF4N+nE5wmx9MC7vOerPW5kpIWyGYfa/uStNm++0zoIPU7mDdw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=fKMtfjXR; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=FiIHv/P3; arc=none smtp.client-ip=64.147.123.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="fKMtfjXR"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="FiIHv/P3" Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfout.west.internal (Postfix) with ESMTP id A46161C000F9; Mon, 3 Jun 2024 05:30:53 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 03 Jun 2024 05:30:54 -0400 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=fm1; t=1717407053; x=1717493453; bh=3m6b3UGykD 5g60tNqaZouN6O85Rhwr2sK5OvzEpMBbE=; b=fKMtfjXRJ8NYFIT8vY8qEkjN+M Ayr0d88JSqV4bbpzP49lrSoIz9rjfoUPFoZxNl4flpqsH3emvqFeBurx9QEas+dZ j4gdTMxtOSZXPTAlIRDobUtOczrTFQC/z1jIUILjdLKFUiU8QIZz70OLmA8owkWJ w7ZPm75e1uQz57X+eeXPU6C7lAmvJ7lmsJG6EsENU0A3bED0TUzwlxFpkpp62dz5 rv0lQPRWZBQWC4FTej3aZ1b8vSzEcJJjGLhR+C6qp5pjiw5pzHiBEibjGb16+3NC nFuwYcoDLqlsNy5CyPt8C5tlUkPpjdoA6upoH95EUzUHWtYaJror4GSANC4w== 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= fm1; t=1717407053; x=1717493453; bh=3m6b3UGykD5g60tNqaZouN6O85Rh wr2sK5OvzEpMBbE=; b=FiIHv/P34/9JwoU+rGrf9eKX22u+I9WL1z9CUhk3GU29 wO/rHKCgvL5wMI30zqeN76vZFTj37nIUKeaPHBS8+9NC6WtWjDwjbLjorc/JtR6y s4yFIpuTUmgr9edmeVoEi+eZkbYw5rY6Voi6cnwAoQ7ukDi1zd7PH1MsiZ6pSFs1 0j1390ToyMSLbTY/CZB75WZJToJepkOidDoaTLCB5f3n0pmQnLqCh6i/SYZXIndf nnMto8h4A8UwTM6fGrg1Zxz/2MpIGjeszWOuHkyMwROewq3Mk9sGgxfnxaRqAYqH DiHz7IFD1S4h+BXI06eq1lATrsiAv4ot4D/HYxIBwA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdelvddgudeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 3 Jun 2024 05:30:51 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 92298797 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 3 Jun 2024 09:30:27 +0000 (UTC) Date: Mon, 3 Jun 2024 11:30:49 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Eric Sunshine , Junio C Hamano , Ramsay Jones , Justin Tobler Subject: [PATCH v4 09/12] worktree: don't store main worktree twice Message-ID: <7a89aae51526faefc0ccce0a73cbd54aec8e8879.1717402363.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: In `get_worktree_ref_store()` we either return the repository's main ref store, or we look up the ref store via the map of worktree ref stores. Which of these worktrees gets picked depends on the `is_current` bit of the worktree, which indicates whether the worktree is the one that corresponds to `the_repository`. The bit is getting set in `get_worktrees()`, but only after we have computed the list of all worktrees. This is too late though, because at that time we have already called `get_worktree_ref_store()` on each of the worktrees via `add_head_info()`. The consequence is that the current worktree will not have been marked accordingly, which means that we did not use the main ref store, but instead created a new ref store. We thus have two separate ref stores now that map to the same ref database. Fix this by setting `is_current` before we call `add_head_info()`. Signed-off-by: Patrick Steinhardt --- worktree.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/worktree.c b/worktree.c index 12eadacc61..70844d023a 100644 --- a/worktree.c +++ b/worktree.c @@ -53,6 +53,15 @@ static void add_head_info(struct worktree *wt) wt->is_detached = 1; } +static int is_current_worktree(struct worktree *wt) +{ + char *git_dir = absolute_pathdup(get_git_dir()); + const char *wt_git_dir = get_worktree_git_dir(wt); + int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir)); + free(git_dir); + return is_current; +} + /** * get the main worktree */ @@ -76,6 +85,7 @@ static struct worktree *get_main_worktree(int skip_reading_head) */ worktree->is_bare = (is_bare_repository_cfg == 1) || is_bare_repository(); + worktree->is_current = is_current_worktree(worktree); if (!skip_reading_head) add_head_info(worktree); return worktree; @@ -102,6 +112,7 @@ struct worktree *get_linked_worktree(const char *id, worktree->repo = the_repository; worktree->path = strbuf_detach(&worktree_path, NULL); worktree->id = xstrdup(id); + worktree->is_current = is_current_worktree(worktree); if (!skip_reading_head) add_head_info(worktree); @@ -111,23 +122,6 @@ struct worktree *get_linked_worktree(const char *id, return worktree; } -static void mark_current_worktree(struct worktree **worktrees) -{ - char *git_dir = absolute_pathdup(get_git_dir()); - int i; - - for (i = 0; worktrees[i]; i++) { - struct worktree *wt = worktrees[i]; - const char *wt_git_dir = get_worktree_git_dir(wt); - - if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) { - wt->is_current = 1; - break; - } - } - free(git_dir); -} - /* * NEEDSWORK: This function exists so that we can look up metadata of a * worktree without trying to access any of its internals like the refdb. It @@ -164,7 +158,6 @@ static struct worktree **get_worktrees_internal(int skip_reading_head) ALLOC_GROW(list, counter + 1, alloc); list[counter] = NULL; - mark_current_worktree(list); return list; }