From patchwork Fri Oct 25 20:57:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13851784 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (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 AABBE217F32 for ; Fri, 25 Oct 2024 20:57:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889853; cv=none; b=j65eeIqLStfFCQGSBBw/iaF7Y8emazW/ZBMnf4bR8dneN6hWCXfruwWUwDpfu2AtyhZL664LkdzJfKls9+bWnT0OK8GYxhgam2gOxMjMzR/xD/hb4pbom0w2Xci62j6C3C45bq/QeV0ZW0paRroP/Ncc+EZNJ+8E3UQtT5kurbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889853; c=relaxed/simple; bh=trotKDKj0V2+d2IcSm2zLzMIndsSymQeFHxS6gvlWF0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eoRYZ6kfUEI4JB5YAjs0+tGI+gEs1Oyc68snIOFJxi49Q/nsQF/4kVbF/U01UD4uZh776JUBexjT0eqmeKWCdwRQT+fc3bzEUGUBki0msrCj71vkcJtdBpbWg6BB3EZeyILvgfw1yrxWAfnuE0YkKXJcBVfIJQTkFX9hg/QAc9s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=XZyPnK5C; arc=none smtp.client-ip=185.70.40.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="XZyPnK5C" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1729889849; x=1730149049; bh=9cbp4mFdzY8oKrUF0dWYZ0KOeePNfKNdzzgE081WdE4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=XZyPnK5CAXD8edrdvQrhrgbT+PuXjj3jUO9VevUhb6N773OopEH33drGJx51QtxZG sjamMD0Ytnl5LPA7x0A1vRNEV1kDWZHRSItb2mPwFZMaOsDSoDtvAxuuq/BKPHJteh b6TdyrOQCBckxzWbtNaLJMUI4jlniGndNhVH8/vrEkLpcpBvf868YMkBAcXphwp0MO mnCakxP30EyhdE5NSmVCjbkOf0BivLwOkJgZBWrqYLTanoQfkdvk32xdU0tYgpUHE6 phmmLs5WiK54K5sufXv35noicefYP0X1TyVMO20eWJX3e7DNImxnV0FeaXmMpeRB92 u6EVIbsgq5ZlA== Date: Fri, 25 Oct 2024 20:57:25 +0000 To: git@vger.kernel.org From: Caleb White Cc: Junio C Hamano , Eric Sunshine , Phillip Wood , shejialuo , Kristoffer Haugsbakk , Caleb White Subject: [PATCH v3 1/5] worktree: refactor infer_backlink() to use strbuf* Message-ID: <20241025-wt_relative_paths-v3-1-8860a5321c01@pm.me> In-Reply-To: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> References: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: c417bfdd8b730a59c4f553b143f3379e65965a73 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The infer_backlink() function initializes a `strbuf` for the inferred backlink and returns the result as a `char*` by detaching the `strbuf`. The next patch needs the backlink returned from infer_backlink() as a `strbuf`. It seemed inefficient to convert from `strbuf` to `char*` and back to `strbuf` again. This refactors infer_backlink() to return an integer result and accept a pre-allocated `strbuf` for the inferred backlink path, improving efficiency. Signed-off-by: Caleb White --- worktree.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/worktree.c b/worktree.c index ec95ea2986107b3bc12d38b0825d7c6e87402bc6..ad60ba0b5843f1676e89b05eca3c82aace5fb49b 100644 --- a/worktree.c +++ b/worktree.c @@ -641,13 +641,15 @@ static int is_main_worktree_path(const char *path) * won't know which /worktrees//gitdir to repair. However, we may * be able to infer the gitdir by manually reading /path/to/worktree/.git, * extracting the , and checking if /worktrees/ exists. + * + * Returns -1 on failure and strbuf.len on success. */ -static char *infer_backlink(const char *gitfile) +static int infer_backlink(const char *gitfile, struct strbuf *inferred) { struct strbuf actual = STRBUF_INIT; - struct strbuf inferred = STRBUF_INIT; const char *id; + strbuf_reset(inferred); if (strbuf_read_file(&actual, gitfile, 0) < 0) goto error; if (!starts_with(actual.buf, "gitdir:")) @@ -658,17 +660,16 @@ static char *infer_backlink(const char *gitfile) id++; /* advance past '/' to point at */ if (!*id) goto error; - strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id); - if (!is_directory(inferred.buf)) + strbuf_git_common_path(inferred, the_repository, "worktrees/%s", id); + if (!is_directory(inferred->buf)) goto error; strbuf_release(&actual); - return strbuf_detach(&inferred, NULL); - + return inferred->len; error: strbuf_release(&actual); - strbuf_release(&inferred); - return NULL; + strbuf_reset(inferred); /* clear invalid path */ + return -1; } /* @@ -680,10 +681,11 @@ void repair_worktree_at_path(const char *path, { struct strbuf dotgit = STRBUF_INIT; struct strbuf realdotgit = STRBUF_INIT; + struct strbuf backlink = STRBUF_INIT; + struct strbuf inferred_backlink = STRBUF_INIT; struct strbuf gitdir = STRBUF_INIT; struct strbuf olddotgit = STRBUF_INIT; - char *backlink = NULL; - char *inferred_backlink = NULL; + char *dotgit_contents = NULL; const char *repair = NULL; int err; @@ -699,23 +701,23 @@ void repair_worktree_at_path(const char *path, goto done; } - inferred_backlink = infer_backlink(realdotgit.buf); - backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err)); - if (err == READ_GITFILE_ERR_NOT_A_FILE) { + infer_backlink(realdotgit.buf, &inferred_backlink); + dotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err)); + if (dotgit_contents) { + strbuf_addstr(&backlink, dotgit_contents); + } else if (err == READ_GITFILE_ERR_NOT_A_FILE) { fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); goto done; } else if (err == READ_GITFILE_ERR_NOT_A_REPO) { - if (inferred_backlink) { + if (inferred_backlink.len) { /* * Worktree's .git file does not point at a repository * but we found a .git/worktrees/ in this * repository with the same as recorded in the * worktree's .git file so make the worktree point at - * the discovered .git/worktrees/. (Note: backlink - * is already NULL, so no need to free it first.) + * the discovered .git/worktrees/. */ - backlink = inferred_backlink; - inferred_backlink = NULL; + strbuf_swap(&backlink, &inferred_backlink); } else { fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); goto done; @@ -743,13 +745,10 @@ void repair_worktree_at_path(const char *path, * in the "copy" repository. In this case, point the "copy" worktree's * .git file at the "copy" repository. */ - if (inferred_backlink && fspathcmp(backlink, inferred_backlink)) { - free(backlink); - backlink = inferred_backlink; - inferred_backlink = NULL; - } + if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) + strbuf_swap(&backlink, &inferred_backlink); - strbuf_addf(&gitdir, "%s/gitdir", backlink); + strbuf_addf(&gitdir, "%s/gitdir", backlink.buf); if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) repair = _("gitdir unreadable"); else { @@ -763,9 +762,10 @@ void repair_worktree_at_path(const char *path, write_file(gitdir.buf, "%s", realdotgit.buf); } done: - free(backlink); - free(inferred_backlink); + free(dotgit_contents); strbuf_release(&olddotgit); + strbuf_release(&backlink); + strbuf_release(&inferred_backlink); strbuf_release(&gitdir); strbuf_release(&realdotgit); strbuf_release(&dotgit); From patchwork Fri Oct 25 20:57:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13851785 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (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 6BA1320A5D0 for ; Fri, 25 Oct 2024 20:57:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889867; cv=none; b=rXkQEUwr28VIEEuSvgp1UB5+WJxzHA7W0jkgYbgyMmu98iQF/IgjoYNiqtuO+CubnbfTfOFGclHpOeecpQwdksRl2DZUTO6FAdSo6fGct7VF8Ts2sbWAN5q2jpgBeiCf17ZGh6cdgDRCLudG+n80v1sacIbogpvtXrT6VX6Rg9I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889867; c=relaxed/simple; bh=Bkl/2AWOFnKo7ln1HGemkK5KQ3H2mgfg7unYwkhWY5Q=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=K7XP0iOpVzLv6PyPsbmNT7P5OsRcaTX2ZF+6TscSXbeMA169ak1FyISN13r1r1klxvKWscGELM0wfY2+xddZ8mDh0N7IsVSDw1zxGRo3z43J8FepR5cmRC5JVRHerMlJalChBK+FlthyIH9WbEHZGsX4Ia3tFC9ub9QkcwM6QgE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=iE70EFdV; arc=none smtp.client-ip=185.70.43.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="iE70EFdV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1729889857; x=1730149057; bh=jNtNJGyVenlIOUm2MVXtx9PxL+4aL3J2iUp+VYab8ao=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=iE70EFdVGPG4Utmn2OVHBO+60QVl9Sf9penVTcRL/lFb891znVLwt1rMMAjW6qKSw fCsXJJs5ydcDZDxQNrwUac1cERfeeZXb99Ae9VuobV3MRwjQ+lLU3GBErzRyJ5ADf/ MF/kCURejoeV7jZpmPZve21cLQZbuJkB08w2lF7GpPoaWf0lX7V/w/9ad3QL6KM46F wQiRLHSc2FMcGxsP9dP13EG3FUeuC8FEL/oAdARLelCxMX/CwokxZREEh6lmAbRaDt s+gyxCkSPrk7kdl2b5TuJvpp4bKbAka4p3Ao0H3nnsr7h4ees7tgkaLqnhtLgtvB5w ie7aDvrMAVkgw== Date: Fri, 25 Oct 2024 20:57:32 +0000 To: git@vger.kernel.org From: Caleb White Cc: Junio C Hamano , Eric Sunshine , Phillip Wood , shejialuo , Kristoffer Haugsbakk , Caleb White Subject: [PATCH v3 2/5] worktree: support worktrees linked with relative paths Message-ID: <20241025-wt_relative_paths-v3-2-8860a5321c01@pm.me> In-Reply-To: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> References: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: d01868eaee9de7f7d2e6f91368f6a440b5513941 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Git stores absolute paths when linking worktrees to the main repository. However, this can cause problems when moving repositories and worktrees, or when working in containerized environments where absolute paths differ between systems. The worktree links break, and users are required to manually execute `worktree repair` to repair them, leading to workflow disruptions. Additionally, when repositories are mapped inside containers with different absolute paths, they may become unusable. Repairing worktrees in the container can then break them outside the container. In some cases, relative paths can eliminate the need for `worktree repair`. If both the repository and worktrees are moved together while preserving their relative locations, the links remain intact. Common examples include manually moving repositories and worktrees (or by tarballing) or mapping both inside containers that use different absolute paths. This patch adds support for _reading_ linking files which could contain relative paths (computing and _writing_ the relative paths will come in the next patch). Generally, relative paths are resolved into absolute paths before any operations or comparisons are performed. The `worktree.path` struct member has also been updated to always contain the absolute path of a worktree. This ensures that worktree consumers never have to worry about trying to resolve the absolute path themselves. Signed-off-by: Caleb White --- worktree.c | 131 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 92 insertions(+), 39 deletions(-) diff --git a/worktree.c b/worktree.c index ad60ba0b5843f1676e89b05eca3c82aace5fb49b..cf578c447589425d642fc8aa7a7fa07600e60a70 100644 --- a/worktree.c +++ b/worktree.c @@ -110,6 +110,12 @@ struct worktree *get_linked_worktree(const char *id, strbuf_rtrim(&worktree_path); strbuf_strip_suffix(&worktree_path, "/.git"); + if (!is_absolute_path(worktree_path.buf)) { + strbuf_strip_suffix(&path, "gitdir"); + strbuf_addbuf(&path, &worktree_path); + strbuf_realpath_forgiving(&worktree_path, path.buf, 0); + } + CALLOC_ARRAY(worktree, 1); worktree->repo = the_repository; worktree->path = strbuf_detach(&worktree_path, NULL); @@ -564,28 +570,38 @@ static void repair_gitfile(struct worktree *wt, { struct strbuf dotgit = STRBUF_INIT; struct strbuf repo = STRBUF_INIT; - char *backlink; + struct strbuf backlink = STRBUF_INIT; + char *dotgit_contents = NULL; const char *repair = NULL; int err; /* missing worktree can't be repaired */ if (!file_exists(wt->path)) - return; + goto done; if (!is_directory(wt->path)) { fn(1, wt->path, _("not a directory"), cb_data); - return; + goto done; } strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1); strbuf_addf(&dotgit, "%s/.git", wt->path); - backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); + dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); + + if (dotgit_contents) { + if (is_absolute_path(dotgit_contents)) { + strbuf_addstr(&backlink, dotgit_contents); + } else { + strbuf_addf(&backlink, "%s/%s", wt->path, dotgit_contents); + strbuf_realpath_forgiving(&backlink, backlink.buf, 0); + } + } if (err == READ_GITFILE_ERR_NOT_A_FILE) fn(1, wt->path, _(".git is not a file"), cb_data); else if (err) repair = _(".git file broken"); - else if (fspathcmp(backlink, repo.buf)) + else if (fspathcmp(backlink.buf, repo.buf)) repair = _(".git file incorrect"); if (repair) { @@ -593,9 +609,11 @@ static void repair_gitfile(struct worktree *wt, write_file(dotgit.buf, "gitdir: %s", repo.buf); } - free(backlink); +done: + free(dotgit_contents); strbuf_release(&repo); strbuf_release(&dotgit); + strbuf_release(&backlink); } static void repair_noop(int iserr UNUSED, @@ -685,6 +703,7 @@ void repair_worktree_at_path(const char *path, struct strbuf inferred_backlink = STRBUF_INIT; struct strbuf gitdir = STRBUF_INIT; struct strbuf olddotgit = STRBUF_INIT; + struct strbuf realolddotgit = STRBUF_INIT; char *dotgit_contents = NULL; const char *repair = NULL; int err; @@ -702,9 +721,17 @@ void repair_worktree_at_path(const char *path, } infer_backlink(realdotgit.buf, &inferred_backlink); + strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0); dotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err)); if (dotgit_contents) { - strbuf_addstr(&backlink, dotgit_contents); + if (is_absolute_path(dotgit_contents)) { + strbuf_addstr(&backlink, dotgit_contents); + } else { + strbuf_addbuf(&backlink, &realdotgit); + strbuf_strip_suffix(&backlink, ".git"); + strbuf_addstr(&backlink, dotgit_contents); + strbuf_realpath_forgiving(&backlink, backlink.buf, 0); + } } else if (err == READ_GITFILE_ERR_NOT_A_FILE) { fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); goto done; @@ -722,7 +749,7 @@ void repair_worktree_at_path(const char *path, fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); goto done; } - } else if (err) { + } else { fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data); goto done; } @@ -753,7 +780,13 @@ void repair_worktree_at_path(const char *path, repair = _("gitdir unreadable"); else { strbuf_rtrim(&olddotgit); - if (fspathcmp(olddotgit.buf, realdotgit.buf)) + if (is_absolute_path(olddotgit.buf)) { + strbuf_addbuf(&realolddotgit, &olddotgit); + } else { + strbuf_addf(&realolddotgit, "%s/%s", backlink.buf, olddotgit.buf); + strbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0); + } + if (fspathcmp(realolddotgit.buf, realdotgit.buf)) repair = _("gitdir incorrect"); } @@ -764,6 +797,7 @@ void repair_worktree_at_path(const char *path, done: free(dotgit_contents); strbuf_release(&olddotgit); + strbuf_release(&realolddotgit); strbuf_release(&backlink); strbuf_release(&inferred_backlink); strbuf_release(&gitdir); @@ -774,69 +808,88 @@ void repair_worktree_at_path(const char *path, int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire) { struct stat st; - char *path; + struct strbuf dotgit = STRBUF_INIT; + struct strbuf gitdir = STRBUF_INIT; + struct strbuf repo = STRBUF_INIT; + struct strbuf file = STRBUF_INIT; + char *path = NULL; + int rc = 0; int fd; size_t len; ssize_t read_result; *wtpath = NULL; - if (!is_directory(git_path("worktrees/%s", id))) { + strbuf_realpath(&repo, git_common_path("worktrees/%s", id), 1); + strbuf_addf(&gitdir, "%s/gitdir", repo.buf); + if (!is_directory(repo.buf)) { strbuf_addstr(reason, _("not a valid directory")); - return 1; + rc = 1; + goto done; } - if (file_exists(git_path("worktrees/%s/locked", id))) - return 0; - if (stat(git_path("worktrees/%s/gitdir", id), &st)) { + strbuf_addf(&file, "%s/locked", repo.buf); + if (file_exists(file.buf)) { + goto done; + } + if (stat(gitdir.buf, &st)) { strbuf_addstr(reason, _("gitdir file does not exist")); - return 1; + rc = 1; + goto done; } - fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY); + fd = open(gitdir.buf, O_RDONLY); if (fd < 0) { strbuf_addf(reason, _("unable to read gitdir file (%s)"), strerror(errno)); - return 1; + rc = 1; + goto done; } len = xsize_t(st.st_size); path = xmallocz(len); read_result = read_in_full(fd, path, len); + close(fd); if (read_result < 0) { strbuf_addf(reason, _("unable to read gitdir file (%s)"), strerror(errno)); - close(fd); - free(path); - return 1; - } - close(fd); - - if (read_result != len) { + rc = 1; + goto done; + } else if (read_result != len) { strbuf_addf(reason, _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), (uintmax_t)len, (uintmax_t)read_result); - free(path); - return 1; + rc = 1; + goto done; } while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) len--; if (!len) { strbuf_addstr(reason, _("invalid gitdir file")); - free(path); - return 1; + rc = 1; + goto done; } path[len] = '\0'; - if (!file_exists(path)) { - if (stat(git_path("worktrees/%s/index", id), &st) || - st.st_mtime <= expire) { + if (is_absolute_path(path)) { + strbuf_addstr(&dotgit, path); + } else { + strbuf_addf(&dotgit, "%s/%s", repo.buf, path); + strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0); + } + if (!file_exists(dotgit.buf)) { + strbuf_reset(&file); + strbuf_addf(&file, "%s/index", repo.buf); + if (stat(file.buf, &st) || st.st_mtime <= expire) { strbuf_addstr(reason, _("gitdir file points to non-existent location")); - free(path); - return 1; - } else { - *wtpath = path; - return 0; + rc = 1; + goto done; } } - *wtpath = path; - return 0; + *wtpath = strbuf_detach(&dotgit, NULL); +done: + free(path); + strbuf_release(&dotgit); + strbuf_release(&gitdir); + strbuf_release(&repo); + strbuf_release(&file); + return rc; } static int move_config_setting(const char *key, const char *value, From patchwork Fri Oct 25 20:57:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13851787 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (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 568F620D50C for ; Fri, 25 Oct 2024 20:57:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889877; cv=none; b=Qtqi3hkfnR3oCt4YUTgpsH07SpWIiLCxpSStH+kwr5hbo9Ixl/GFwmuremUVl9kWisMDEFL+X+8Kx50zpUIUrhbWBoiUIuQ/RYLxF+Scv8jZH19YcLZz9WMhxpvYenwOKKfm8YEP9hQ40P7ZXE722MZPX2XFn+j0RZzMWnz/MPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889877; c=relaxed/simple; bh=dUqsaIulVDMb5l3dLQXq4UF1yoVnuSterUw4AVgnfZs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uoh1oLC3ckFRAyeKlr7iHcoF/VXrXVuGB2UT6ZYDQuScgJvZV3uxJDkVdXo467S46ouW/ZpF3VQDx25nHEQrIStV3J/xua1K742b0jEFWXY/xR3pSsz8+FKWO4Ivjby+3DE9U3HJtt020hc2Sivgw1znBvm8rKP5vqM0R6sOu0Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=ry1PDJl6; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="ry1PDJl6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1729889865; x=1730149065; bh=fsYVpjzuiHST3qklgDVe5Hb25UCfYKSvs8vKVP8+Q4Q=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=ry1PDJl6jwmHWH1HjOTX4HgqnJP87PRHHUePTCoPz7MQOgZpxZf9nChMpbuQenXwW MP8BH/zU0G+5lRL/MQzxQLZOQsTre5Hqa4/XIlgpKVNUbPKY/rGkbBkIUz7L+ATzgO /vU1en4wWekkDC68CBT6g9xHIOc+ijEbciJsBoUq0EUyX/R/iMoAcpfs7poRXRj34+ lYbSuLLzuF/gMPf+NFyPYtQOHasqqczPsm8XSe3ZonFMUMldVfJA/NEw8j5vLnlRcN 5PaCCNsY5iFqFgGJZMrOWqQ8+/iFQFC7eBY57SRrAzeN50KbFpMJxOIEN7DUZWe3+F 7wrzwdYpyPIOw== Date: Fri, 25 Oct 2024 20:57:40 +0000 To: git@vger.kernel.org From: Caleb White Cc: Junio C Hamano , Eric Sunshine , Phillip Wood , shejialuo , Kristoffer Haugsbakk , Caleb White Subject: [PATCH v3 3/5] worktree: optionally link worktrees with relative paths Message-ID: <20241025-wt_relative_paths-v3-3-8860a5321c01@pm.me> In-Reply-To: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> References: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 94148703e29743e22460caaa005caeb08feb77ba Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Git stores absolute paths when linking worktrees to the main repository. However, this can cause problems when moving repositories and worktrees, or when working in containerized environments where absolute paths differ between systems. The worktree links break, and users are required to manually execute `worktree repair` to repair them, leading to workflow disruptions. Additionally, when repositories are mapped inside containers with different absolute paths, they may become unusable. Repairing worktrees in the container can then break them outside the container. In some cases, relative paths can eliminate the need for `worktree repair`. If both the repository and worktrees are moved together while preserving their relative locations, the links remain intact. Common examples include moving repositories and worktrees or mapping both inside containers that use different absolute paths. This patch adds support for optionally _writing_ relative paths to the linking files. If desired, relative paths can be created through the `--relative-paths` CLI option for `worktree {add,move,repair}` as well as the `worktree.useRelativePaths` config option. If an existing worktree uses absolute paths, executing `worktree {move,repair}` with the relative option will create relative paths (and visa versa). A new function, `write_worktree_linking_files()` is introduced to clean the actual relative path computation in different locations. The function accepts a strbuf to the worktree `.git` file and a strbuf to the repository `gitdir` file and then writes the appropriate path to each as configured by the user. Additionally, `repair_worktrees_after_gitdir_move()` has been introduced to address the case where both the `/.git` and `/worktrees//gitdir` links are broken after the gitdir is moved (such as during a re-initialization). This function repairs both sides of the worktree link using the old gitdir path to reestablish the correct paths after a move. Signed-off-by: Caleb White --- Documentation/config/worktree.txt | 5 ++ Documentation/git-worktree.txt | 9 +++ builtin/worktree.c | 18 +++--- setup.c | 2 +- t/t0001-init.sh | 20 +++++-- t/t2400-worktree-add.sh | 41 ++++++++++++++ t/t2402-worktree-list.sh | 22 ++++++++ t/t2403-worktree-move.sh | 22 ++++++++ t/t2406-worktree-repair.sh | 26 +++++++++ worktree.c | 116 +++++++++++++++++++++++++++++++------- worktree.h | 24 ++++++++ 11 files changed, 270 insertions(+), 35 deletions(-) diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt index 048e349482df6c892055720eb53cdcd6c327b6ed..d0589c21d034f07f70ca172947520d7d78720cca 100644 --- a/Documentation/config/worktree.txt +++ b/Documentation/config/worktree.txt @@ -7,3 +7,8 @@ worktree.guessRemote:: such a branch exists, it is checked out and set as "upstream" for the new branch. If no such match can be found, it falls back to creating a new branch from the current HEAD. +worktree.useRelativePaths:: + If set to `true`, the worktree paths will be stored relative to the + repository’s top-level directory, rather than using absolute paths. + This is particularly useful for setups where the repository may be moved + between different locations or environments. diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 70437c815f13852bd2eb862176b8b933e6de0acf..4a6ad481ee1d44b43683bd67881a2771e2b090c7 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -216,6 +216,15 @@ To remove a locked worktree, specify `--force` twice. This can also be set up as the default behaviour by using the `worktree.guessRemote` config option. +--[no-]relative-paths:: + The worktree paths will be stored relative to the repository’s + top-level directory, rather than using absolute paths. + This is particularly useful for setups where the repository may be moved + between different locations or environments. ++ +This can also be set up as the default behaviour by using the +`worktree.useRelativePaths` config option. + --[no-]track:: When creating a new branch, if `` is a branch, mark it as "upstream" from the new branch. This is the diff --git a/builtin/worktree.c b/builtin/worktree.c index fc31d072a620d7b455d7f150bd3a9e773ee9d4ed..bb06830d6fe82aa97833c6e87f034115dfaa23bd 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value, if (!strcmp(var, "worktree.guessremote")) { guess_remote = git_config_bool(var, value); return 0; + } else if (!strcmp(var, "worktree.userelativepaths")) { + use_relative_paths = git_config_bool(var, value); + return 0; } return git_default_config(var, value, ctx, cb); @@ -414,7 +417,7 @@ static int add_worktree(const char *path, const char *refname, const struct add_opts *opts) { struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; - struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT; + struct strbuf sb = STRBUF_INIT; const char *name; struct strvec child_env = STRVEC_INIT; unsigned int counter = 0; @@ -490,11 +493,7 @@ static int add_worktree(const char *path, const char *refname, strbuf_reset(&sb); strbuf_addf(&sb, "%s/gitdir", sb_repo.buf); - strbuf_realpath(&realpath, sb_git.buf, 1); - write_file(sb.buf, "%s", realpath.buf); - strbuf_realpath(&realpath, repo_get_common_dir(the_repository), 1); - write_file(sb_git.buf, "gitdir: %s/worktrees/%s", - realpath.buf, name); + write_worktree_linking_files(sb_git, sb); strbuf_reset(&sb); strbuf_addf(&sb, "%s/commondir", sb_repo.buf); write_file(sb.buf, "../.."); @@ -582,7 +581,6 @@ static int add_worktree(const char *path, const char *refname, strbuf_release(&sb_repo); strbuf_release(&sb_git); strbuf_release(&sb_name); - strbuf_release(&realpath); free_worktree(wt); return ret; } @@ -794,6 +792,8 @@ static int add(int ac, const char **av, const char *prefix) PARSE_OPT_NOARG | PARSE_OPT_OPTARG), OPT_BOOL(0, "guess-remote", &guess_remote, N_("try to match the new branch name with a remote-tracking branch")), + OPT_BOOL(0, "relative-paths", &use_relative_paths, + N_("use relative paths for worktrees")), OPT_END() }; int ret; @@ -1187,6 +1187,8 @@ static int move_worktree(int ac, const char **av, const char *prefix) OPT__FORCE(&force, N_("force move even if worktree is dirty or locked"), PARSE_OPT_NOCOMPLETE), + OPT_BOOL(0, "relative-paths", &use_relative_paths, + N_("use relative paths for worktrees")), OPT_END() }; struct worktree **worktrees, *wt; @@ -1380,6 +1382,8 @@ static int repair(int ac, const char **av, const char *prefix) const char **p; const char *self[] = { ".", NULL }; struct option options[] = { + OPT_BOOL(0, "relative-paths", &use_relative_paths, + N_("use relative paths for worktrees")), OPT_END() }; int rc = 0; diff --git a/setup.c b/setup.c index 94e79b2e487f3faa537547e190acf9b7ea0be3b5..7b648de0279116b381eea46800ad130606926103 100644 --- a/setup.c +++ b/setup.c @@ -2420,7 +2420,7 @@ static void separate_git_dir(const char *git_dir, const char *git_link) if (rename(src, git_dir)) die_errno(_("unable to move %s to %s"), src, git_dir); - repair_worktrees(NULL, NULL); + repair_worktrees_after_gitdir_move(src); } write_file(git_link, "gitdir: %s", git_dir); diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..03ac9624cb418d05adb8dfb786c31b809c73efec 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -435,19 +435,27 @@ sep_git_dir_worktree () { test_when_finished "rm -rf mainwt linkwt seprepo" && git init mainwt && test_commit -C mainwt gumby && - git -C mainwt worktree add --detach ../linkwt && - git -C "$1" init --separate-git-dir ../seprepo && + git -C mainwt worktree add $1 --detach ../linkwt && + git -C "$2" init --separate-git-dir ../seprepo && git -C mainwt rev-parse --git-common-dir >expect && git -C linkwt rev-parse --git-common-dir >actual && test_cmp expect actual } -test_expect_success 're-init to move gitdir with linked worktrees' ' - sep_git_dir_worktree mainwt +test_expect_success 're-init to move gitdir with linked worktrees (absolute)' ' + sep_git_dir_worktree --no-relative-paths mainwt ' -test_expect_success 're-init to move gitdir within linked worktree' ' - sep_git_dir_worktree linkwt +test_expect_success 're-init to move gitdir within linked worktree (absolute)' ' + sep_git_dir_worktree --no-relative-paths linkwt +' + +test_expect_success 're-init to move gitdir with linked worktrees (relative)' ' + sep_git_dir_worktree --relative-paths mainwt +' + +test_expect_success 're-init to move gitdir within linked worktree (relative)' ' + sep_git_dir_worktree --relative-paths linkwt ' test_expect_success MINGW '.git hidden' ' diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index cfc4aeb1798c6d023909cec771e5b74e983af5ea..630c13230b3cc762ce8d943e22be8891aa2b1871 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -1207,4 +1207,45 @@ test_expect_success '"add" with initialized submodule, with submodule.recurse se git -C project-clone -c submodule.recurse worktree add ../project-5 ' +test_expect_success 'can create worktrees with relative paths' ' + test_when_finished "git worktree remove relative" && + git config worktree.useRelativePaths false && + git worktree add --relative-paths ./relative && + cat relative/.git >actual && + echo "gitdir: ../.git/worktrees/relative" >expect && + test_cmp expect actual && + cat .git/worktrees/relative/gitdir >actual && + echo "../../../relative/.git" >expect && + test_cmp expect actual + +' + +test_expect_success 'can create worktrees with absolute paths' ' + git config worktree.useRelativePaths true && + git worktree add ./relative && + cat relative/.git >actual && + echo "gitdir: ../.git/worktrees/relative" >expect && + test_cmp expect actual && + git worktree add --no-relative-paths ./absolute && + cat absolute/.git >actual && + echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect && + test_cmp expect actual +' + +test_expect_success 'move repo without breaking relative internal links' ' + test_when_finished rm -rf repo moved && + git init repo && + ( + cd repo && + git config worktree.useRelativePaths true && + test_commit initial && + git worktree add wt1 && + cd .. && + mv repo moved && + cd moved/wt1 && + git status >out 2>err && + test_must_be_empty err + ) +' + test_done diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh index 33ea9cb21ba07c9563530b54da06753eaa993fe2..780daa6cd6351f8fa9434619cc212aade8f01420 100755 --- a/t/t2402-worktree-list.sh +++ b/t/t2402-worktree-list.sh @@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' ' ' test_expect_success 'linked worktrees are sorted' ' + test_when_finished "rm -rf sorted" && mkdir sorted && git init sorted/main && ( @@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' ' test_cmp expected sorted/main/actual ' +test_expect_success 'linked worktrees with relative paths are shown with absolute paths' ' + test_when_finished "rm -rf sorted" && + mkdir sorted && + git init sorted/main && + ( + cd sorted/main && + test_tick && + test_commit new && + git worktree add --relative-paths ../first && + git worktree add ../second && + git worktree list --porcelain >out && + grep ^worktree out >actual + ) && + cat >expected <<-EOF && + worktree $(pwd)/sorted/main + worktree $(pwd)/sorted/first + worktree $(pwd)/sorted/second + EOF + test_cmp expected sorted/main/actual +' + test_expect_success 'worktree path when called in .git directory' ' git worktree list >list1 && git -C .git worktree list >list2 && diff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh index 901342ea09b51a8e832f1109fbb737df84283aa2..6ce9ed3f1e6b3f73d2a290e770233eec30221fe5 100755 --- a/t/t2403-worktree-move.sh +++ b/t/t2403-worktree-move.sh @@ -247,4 +247,26 @@ test_expect_success 'not remove a repo with initialized submodule' ' ) ' +test_expect_success 'move worktree with absolute path to relative path' ' + git config worktree.useRelativePaths false && + git worktree add ./absolute && + git worktree move --relative-paths absolute relative && + cat relative/.git >actual && + echo "gitdir: ../.git/worktrees/absolute" >expect && + test_cmp expect actual && + git config worktree.useRelativePaths true && + git worktree move relative relative2 && + cat relative2/.git >actual && + echo "gitdir: ../.git/worktrees/absolute" >expect && + test_cmp expect actual +' + +test_expect_success 'move worktree with relative path to absolute path' ' + git config worktree.useRelativePaths true && + git worktree move --no-relative-paths relative2 absolute && + cat absolute/.git >actual && + echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect && + test_cmp expect actual +' + test_done diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh index 7686e60f6ad186519b275f11a5e14064c905b207..84451e903b2ef3c645c0311faf055c846588baf6 100755 --- a/t/t2406-worktree-repair.sh +++ b/t/t2406-worktree-repair.sh @@ -216,4 +216,30 @@ test_expect_success 'repair copied main and linked worktrees' ' test_cmp dup/linked.expect dup/linked/.git ' +test_expect_success 'repair absolute worktree to use relative paths' ' + test_when_finished "rm -rf main side sidemoved" && + test_create_repo main && + test_commit -C main init && + git -C main worktree add --detach ../side && + echo "../../../../sidemoved/.git" >expect-gitdir && + echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile && + mv side sidemoved && + git -C main worktree repair --relative-paths ../sidemoved && + test_cmp expect-gitdir main/.git/worktrees/side/gitdir && + test_cmp expect-gitfile sidemoved/.git +' + +test_expect_success 'repair relative worktree to use absolute paths' ' + test_when_finished "rm -rf main side sidemoved" && + test_create_repo main && + test_commit -C main init && + git -C main worktree add --relative-paths --detach ../side && + echo "$(pwd)/sidemoved/.git" >expect-gitdir && + echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile && + mv side sidemoved && + git -C main worktree repair ../sidemoved && + test_cmp expect-gitdir main/.git/worktrees/side/gitdir && + test_cmp expect-gitfile sidemoved/.git +' + test_done diff --git a/worktree.c b/worktree.c index cf578c447589425d642fc8aa7a7fa07600e60a70..0d1685b892fcddf74a91304424a94e5ee847388b 100644 --- a/worktree.c +++ b/worktree.c @@ -14,6 +14,8 @@ #include "wt-status.h" #include "config.h" +int use_relative_paths = 0; + void free_worktree(struct worktree *worktree) { if (!worktree) @@ -379,18 +381,24 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg, void update_worktree_location(struct worktree *wt, const char *path_) { struct strbuf path = STRBUF_INIT; + struct strbuf dotgit = STRBUF_INIT; + struct strbuf gitdir = STRBUF_INIT; if (is_main_worktree(wt)) BUG("can't relocate main worktree"); + strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1); strbuf_realpath(&path, path_, 1); + strbuf_addf(&dotgit, "%s/.git", path.buf); if (fspathcmp(wt->path, path.buf)) { - write_file(git_common_path("worktrees/%s/gitdir", wt->id), - "%s/.git", path.buf); + write_worktree_linking_files(dotgit, gitdir); + free(wt->path); wt->path = strbuf_detach(&path, NULL); } strbuf_release(&path); + strbuf_release(&dotgit); + strbuf_release(&gitdir); } int is_worktree_being_rebased(const struct worktree *wt, @@ -569,6 +577,7 @@ static void repair_gitfile(struct worktree *wt, worktree_repair_fn fn, void *cb_data) { struct strbuf dotgit = STRBUF_INIT; + struct strbuf gitdir = STRBUF_INIT; struct strbuf repo = STRBUF_INIT; struct strbuf backlink = STRBUF_INIT; char *dotgit_contents = NULL; @@ -586,6 +595,7 @@ static void repair_gitfile(struct worktree *wt, strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1); strbuf_addf(&dotgit, "%s/.git", wt->path); + strbuf_addf(&gitdir, "%s/gitdir", repo.buf); dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); if (dotgit_contents) { @@ -603,16 +613,19 @@ static void repair_gitfile(struct worktree *wt, repair = _(".git file broken"); else if (fspathcmp(backlink.buf, repo.buf)) repair = _(".git file incorrect"); + else if (use_relative_paths == is_absolute_path(dotgit_contents)) + repair = _(".git file absolute/relative path mismatch"); if (repair) { fn(0, wt->path, repair, cb_data); - write_file(dotgit.buf, "gitdir: %s", repo.buf); + write_worktree_linking_files(dotgit, gitdir); } done: free(dotgit_contents); strbuf_release(&repo); strbuf_release(&dotgit); + strbuf_release(&gitdir); strbuf_release(&backlink); } @@ -636,6 +649,45 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data) free_worktrees(worktrees); } +void repair_worktree_after_gitdir_move(struct worktree *wt, + const char *old_path) +{ + struct strbuf gitdir = STRBUF_INIT; + struct strbuf dotgit = STRBUF_INIT; + + if (is_main_worktree(wt)) + goto done; + + strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1); + + if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0) + goto done; + + strbuf_rtrim(&dotgit); + if (!is_absolute_path(dotgit.buf)) { + strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id); + strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0); + } + + if (!file_exists(dotgit.buf)) + goto done; + + write_worktree_linking_files(dotgit, gitdir); +done: + strbuf_release(&gitdir); + strbuf_release(&dotgit); +} + +void repair_worktrees_after_gitdir_move(const char *old_path) +{ + struct worktree **worktrees = get_worktrees_internal(1); + struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ + + for (; *wt; wt++) + repair_worktree_after_gitdir_move(*wt, old_path); + free_worktrees(worktrees); +} + static int is_main_worktree_path(const char *path) { struct strbuf target = STRBUF_INIT; @@ -698,12 +750,10 @@ void repair_worktree_at_path(const char *path, worktree_repair_fn fn, void *cb_data) { struct strbuf dotgit = STRBUF_INIT; - struct strbuf realdotgit = STRBUF_INIT; struct strbuf backlink = STRBUF_INIT; struct strbuf inferred_backlink = STRBUF_INIT; struct strbuf gitdir = STRBUF_INIT; struct strbuf olddotgit = STRBUF_INIT; - struct strbuf realolddotgit = STRBUF_INIT; char *dotgit_contents = NULL; const char *repair = NULL; int err; @@ -715,25 +765,25 @@ void repair_worktree_at_path(const char *path, goto done; strbuf_addf(&dotgit, "%s/.git", path); - if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) { + if (!strbuf_realpath(&dotgit, dotgit.buf, 0)) { fn(1, path, _("not a valid path"), cb_data); goto done; } - infer_backlink(realdotgit.buf, &inferred_backlink); + infer_backlink(dotgit.buf, &inferred_backlink); strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0); - dotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err)); + dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); if (dotgit_contents) { if (is_absolute_path(dotgit_contents)) { strbuf_addstr(&backlink, dotgit_contents); } else { - strbuf_addbuf(&backlink, &realdotgit); + strbuf_addbuf(&backlink, &dotgit); strbuf_strip_suffix(&backlink, ".git"); strbuf_addstr(&backlink, dotgit_contents); strbuf_realpath_forgiving(&backlink, backlink.buf, 0); } } else if (err == READ_GITFILE_ERR_NOT_A_FILE) { - fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); + fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); goto done; } else if (err == READ_GITFILE_ERR_NOT_A_REPO) { if (inferred_backlink.len) { @@ -746,11 +796,11 @@ void repair_worktree_at_path(const char *path, */ strbuf_swap(&backlink, &inferred_backlink); } else { - fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); + fn(1, dotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); goto done; } } else { - fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data); + fn(1, dotgit.buf, _("unable to locate repository; .git file broken"), cb_data); goto done; } @@ -778,30 +828,28 @@ void repair_worktree_at_path(const char *path, strbuf_addf(&gitdir, "%s/gitdir", backlink.buf); if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) repair = _("gitdir unreadable"); + else if (use_relative_paths == is_absolute_path(olddotgit.buf)) + repair = _("gitdir absolute/relative path mismatch"); else { strbuf_rtrim(&olddotgit); - if (is_absolute_path(olddotgit.buf)) { - strbuf_addbuf(&realolddotgit, &olddotgit); - } else { - strbuf_addf(&realolddotgit, "%s/%s", backlink.buf, olddotgit.buf); - strbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0); + if (!is_absolute_path(olddotgit.buf)) { + strbuf_insertf(&olddotgit, 0, "%s/", backlink.buf); + strbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0); } - if (fspathcmp(realolddotgit.buf, realdotgit.buf)) + if (fspathcmp(olddotgit.buf, dotgit.buf)) repair = _("gitdir incorrect"); } if (repair) { fn(0, gitdir.buf, repair, cb_data); - write_file(gitdir.buf, "%s", realdotgit.buf); + write_worktree_linking_files(dotgit, gitdir); } done: free(dotgit_contents); strbuf_release(&olddotgit); - strbuf_release(&realolddotgit); strbuf_release(&backlink); strbuf_release(&inferred_backlink); strbuf_release(&gitdir); - strbuf_release(&realdotgit); strbuf_release(&dotgit); } @@ -963,3 +1011,29 @@ int init_worktree_config(struct repository *r) free(main_worktree_file); return res; } + +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir) +{ + struct strbuf path = STRBUF_INIT; + struct strbuf repo = STRBUF_INIT; + struct strbuf tmp = STRBUF_INIT; + + strbuf_addbuf(&path, &dotgit); + strbuf_strip_suffix(&path, "/.git"); + strbuf_realpath(&path, path.buf, 1); + strbuf_addbuf(&repo, &gitdir); + strbuf_strip_suffix(&repo, "/gitdir"); + strbuf_realpath(&repo, repo.buf, 1); + + if (use_relative_paths) { + write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp)); + write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp)); + } else { + write_file(gitdir.buf, "%s/.git", path.buf); + write_file(dotgit.buf, "gitdir: %s", repo.buf); + } + + strbuf_release(&path); + strbuf_release(&repo); + strbuf_release(&tmp); +} diff --git a/worktree.h b/worktree.h index 11279d0c8fe249bb30642563bf221a8de7f3b0a3..5929089891c97318a8f5329f7938264c717050d5 100644 --- a/worktree.h +++ b/worktree.h @@ -5,6 +5,8 @@ struct strbuf; +extern int use_relative_paths; + struct worktree { /* The repository this worktree belongs to. */ struct repository *repo; @@ -131,6 +133,16 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path, */ void repair_worktrees(worktree_repair_fn, void *cb_data); +/* + * Repair the linked worktrees after the gitdir has been moved. + */ +void repair_worktrees_after_gitdir_move(const char *old_path); + +/* + * Repair the linked worktree after the gitdir has been moved. + */ +void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path); + /* * Repair administrative files corresponding to the worktree at the given path. * The worktree's .git file pointing at the repository must be intact for the @@ -205,4 +217,16 @@ void strbuf_worktree_ref(const struct worktree *wt, */ int init_worktree_config(struct repository *r); +/** + * Write the .git file and gitdir file that links the worktree to the repository. + * + * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir` + * is the path to the repository's `gitdir` file. + * + * Example + * dotgit: "/path/to/foo/.git" + * gitdir: "/path/to/repo/worktrees/foo/gitdir" + */ +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir); + #endif From patchwork Fri Oct 25 20:57:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13851786 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (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 69C9220EA27 for ; Fri, 25 Oct 2024 20:57:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889875; cv=none; b=WPMrbEkgjLrMafAN1JmZ3/cK136pGfLRpWb/bxg7JHoW2F46KXk/AAeU/uSG5TiDh2dNhfZvK6yIVqyg43qJVrVAyxFucmoD2yk8fazyOAatL2niGBFdL8E/W6XS1PVxG8J2mF5ggw6Aj27NuoySp+YdZhqkZiQ8vNF3DGhVyt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889875; c=relaxed/simple; bh=XTl0nfODBot2Eb0c1Ey4Yy1aruGS9AL3nbHrfixvIxs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=M96nDgQ4ljr7HkvU7ZjGYhC92/ab1LcZhyhEB7zQtOgPIgUyc4qDmF6dpRKQwNkAgIQQWUISyXexl512+w9Q+0/x+jhNXCgmDFGT8ofXb3S8E6F3Ip0rnzpQ+tKomh9KQSxLn7bl2RjXPEKBD4+FpwTNdJMh8KLTti9MRO0D8sk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=D/B3xrJY; arc=none smtp.client-ip=185.70.43.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="D/B3xrJY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1729889871; x=1730149071; bh=Io6mCsVlP/8Mvzo8Ou8I0J9BJuWiZ/nllFdNrw1viv8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=D/B3xrJYpxX51REOqTQOHveuwAADqeLATpIQyJZsOurVK8/kgduwYSTd+4VUl0zST L4NHyudnbx65bgU5gcH2Q/r/brnE+iLc5gjVan/IvcFsBYuMQQQAT9Fzi/057vpDfh wYU0uitYzWPX2YsSUvUBc9e1uF6rlRzsyyd9jioyn9aLlWEJ2dY3iIYhv5dpNKKClE y5zRkXbTG9WNX4efN+vWGP1jndrDeU8FiAVPzwKpUn1a/+0bhub5hYYKkZswsVIX/H IEAqj8DwgtCYjON2njvc0ZE9Z7ddw7t9XsmnJjjfcsqOnGrpjTrzU6ZgxUOJW0iYJZ DzMwuoRIGzoFg== Date: Fri, 25 Oct 2024 20:57:46 +0000 To: git@vger.kernel.org From: Caleb White Cc: Junio C Hamano , Eric Sunshine , Phillip Wood , shejialuo , Kristoffer Haugsbakk , Caleb White Subject: [PATCH v3 4/5] worktree: add test for path handling in linked worktrees Message-ID: <20241025-wt_relative_paths-v3-4-8860a5321c01@pm.me> In-Reply-To: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> References: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: ac67440ae3b5945a098a07d76d3393945470e6a2 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A failure scenario reported in an earlier patch series[1] that several `git worktree` subcommands failed or misbehaved when invoked from within linked worktrees that used relative paths. This adds a test that executes a `worktree prune` command inside both an internally and an externally linked worktree and asserts that the other worktree was not pruned. [1]: https://lore.kernel.org/git/CAPig+cQXFy=xPVpoSq6Wq0pxMRCjS=WbkgdO+3LySPX=q0nPCw@mail.gmail.com/ Reported-by: Eric Sunshine Signed-off-by: Caleb White --- t/t2401-worktree-prune.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh index 71aa9bcd620f8a504fe628a2d7332d8b47fd4701..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755 --- a/t/t2401-worktree-prune.sh +++ b/t/t2401-worktree-prune.sh @@ -120,4 +120,24 @@ test_expect_success 'prune duplicate (main/linked)' ' ! test -d .git/worktrees/wt ' +test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' ' + test_when_finished rm -rf repo wt_ext && + git init repo && + ( + cd repo && + git config worktree.useRelativePaths true && + echo content >file && + git add file && + git commit -m msg && + git worktree add ../wt_ext && + git worktree add wt_int && + cd wt_int && + git worktree prune -v >out && + test_must_be_empty out && + cd ../../wt_ext && + git worktree prune -v >out && + test_must_be_empty out + ) +' + test_done From patchwork Fri Oct 25 20:57:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13851788 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (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 D7098214428 for ; Fri, 25 Oct 2024 20:58:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889884; cv=none; b=myOPc8xkU3kKiSLiQe7Eu4zj6MmT4Xhkvwv2Tu0hXHhr2bLG8IWu4YVEY51CnoLi8M3eO3jN/NWJ97zWF95ev4r3Kc+gtWtRC4DqiSZeNCAuEz0JqDUKV8aZ2eBOIHAXx4hut0p5hCAgwYB92E5DN+2YcwPCxyvUhtLWZqo0F2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889884; c=relaxed/simple; bh=EiXe1EWu2/FWy5VhsDK+b7gs3KFaSyIZSpQ0tCxrAAw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mO+RjdT83NoQcO5pu67NTV+c6txMLQ0WOdqjBLFnfE4ZJvtX57EyqnELv5qOEyJTJyUcE6e6ZEq25hhJ3Qu1s/EaRhZX7WhKgtxF8IR9Q+NoH7VRv2nMcyWYiJ9zFIqwapVThZF5r472yXx0JQcL2l18DiZuz6VscDglQ8pBHCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=FAjPxlxu; arc=none smtp.client-ip=185.70.40.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="FAjPxlxu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1729889879; x=1730149079; bh=Tft1KdxQIulu/BlDQoxDXmHCZMwMNZ5u2OzHbRU89JM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=FAjPxlxu4BcSiec6xlNw1iD7ODbh/CNz3PQ3dKHOjk2Th2rMEj3w0o/HwiFBTi90C bjF6fFhW2HRrfh+P90V/VUU4GDuEHwoQIZ5ESTV6jEIK7GxsgSmqpt64ftosN2+tYe wzPPpHn3U8gONAIwTMXSzkDamR4hA0YGz9kU8a0Fi/XCwFSNNy9IsESBWY650x8NNO HtcMQzyN4fOxaowi8FXZZEQHPt5U9MbjcdlBfkH3yUViEoKzEW6lWYTZn9qF6NlvhR m+X7IQw3AlXfGkEs6eEJ0kAQ64ctdWFP0CZGOHImdPDM6B31DcUN7HfW0uST9XgGPz ERcuNGrHiVg1w== Date: Fri, 25 Oct 2024 20:57:52 +0000 To: git@vger.kernel.org From: Caleb White Cc: Junio C Hamano , Eric Sunshine , Phillip Wood , shejialuo , Kristoffer Haugsbakk , Caleb White Subject: [PATCH v3 5/5] worktree: add `relativeWorktrees` extension Message-ID: <20241025-wt_relative_paths-v3-5-8860a5321c01@pm.me> In-Reply-To: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> References: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 97e6321f4b3dae7d142ed809497ff06df3b378c0 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A new extension, `relativeWorktrees`, is added to indicate that at least one worktree in the repository has been linked with relative paths. This extension is automatically set when a worktree is created or repaired using the `--relative-paths` option, or when the `worktree.useRelativePaths` config is set to `true`. The `relativeWorktrees` extension ensures older Git versions do not attempt to automatically prune worktrees with relative paths, as they would not not recognize the paths as being valid. Suggested-by: Junio C Hamano Signed-off-by: Caleb White --- Documentation/config/extensions.txt | 6 ++++++ repository.c | 1 + repository.h | 1 + setup.c | 7 +++++++ setup.h | 1 + t/t2400-worktree-add.sh | 13 +++++++++++++ worktree.c | 8 ++++++++ 7 files changed, 37 insertions(+) diff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt index 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644 --- a/Documentation/config/extensions.txt +++ b/Documentation/config/extensions.txt @@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or linkgit:git-clone[1]. Trying to change it after initialization will not work and will produce hard-to-diagnose issues. +relativeWorktrees:: + If enabled, indicates at least one worktree has been linked with + relative paths. Automatically set if a worktree has been created or + repaired with either the `--relative-paths` option or with the + `worktree.useRelativePaths` config set to `true`. + worktreeConfig:: If enabled, then worktrees will load config settings from the `$GIT_DIR/config.worktree` file in addition to the diff --git a/repository.c b/repository.c index f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644 --- a/repository.c +++ b/repository.c @@ -283,6 +283,7 @@ int repo_init(struct repository *repo, repo_set_compat_hash_algo(repo, format.compat_hash_algo); repo_set_ref_storage_format(repo, format.ref_storage_format); repo->repository_format_worktree_config = format.worktree_config; + repo->repository_format_relative_worktrees = format.relative_worktrees; /* take ownership of format.partial_clone */ repo->repository_format_partial_clone = format.partial_clone; diff --git a/repository.h b/repository.h index 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644 --- a/repository.h +++ b/repository.h @@ -150,6 +150,7 @@ struct repository { /* Configurations */ int repository_format_worktree_config; + int repository_format_relative_worktrees; /* Indicate if a repository has a different 'commondir' from 'gitdir' */ unsigned different_commondir:1; diff --git a/setup.c b/setup.c index 7b648de0279116b381eea46800ad130606926103..6bf56cf72c4b46a95f46f9b3901f7e77d702cec7 100644 --- a/setup.c +++ b/setup.c @@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var, "extensions.refstorage", value); data->ref_storage_format = format; return EXTENSION_OK; + } else if (!strcmp(ext, "relativeworktrees")) { + data->relative_worktrees = git_config_bool(var, value); + return EXTENSION_OK; } return EXTENSION_UNKNOWN; } @@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok) repo_fmt.ref_storage_format); the_repository->repository_format_worktree_config = repo_fmt.worktree_config; + the_repository->repository_format_relative_worktrees = + repo_fmt.relative_worktrees; /* take ownership of repo_fmt.partial_clone */ the_repository->repository_format_partial_clone = repo_fmt.partial_clone; @@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt) fmt->ref_storage_format); the_repository->repository_format_worktree_config = fmt->worktree_config; + the_repository->repository_format_relative_worktrees = + fmt->relative_worktrees; the_repository->repository_format_partial_clone = xstrdup_or_null(fmt->partial_clone); clear_repository_format(&repo_fmt); diff --git a/setup.h b/setup.h index e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644 --- a/setup.h +++ b/setup.h @@ -129,6 +129,7 @@ struct repository_format { int precious_objects; char *partial_clone; /* value of extensions.partialclone */ int worktree_config; + int relative_worktrees; int is_bare; int hash_algo; int compat_hash_algo; diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 630c13230b3cc762ce8d943e22be8891aa2b1871..d36d8a4db0e924877787697579544f10f92dc0cf 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -1248,4 +1248,17 @@ test_expect_success 'move repo without breaking relative internal links' ' ) ' +test_expect_success 'relative worktree sets extension config' ' + test_when_finished "rm -rf repo" && + git init repo && + git -C repo commit --allow-empty -m base && + git -C repo worktree add --relative-paths ./foo && + git -C repo config get core.repositoryformatversion >actual && + echo 1 >expected && + test_cmp expected actual && + git -C repo config get extensions.relativeworktrees >actual && + echo true >expected && + test_cmp expected actual +' + test_done diff --git a/worktree.c b/worktree.c index 0d1685b892fcddf74a91304424a94e5ee847388b..41ebddb495d146e302fedefdb03e0d2637fb2831 100644 --- a/worktree.c +++ b/worktree.c @@ -1025,6 +1025,14 @@ void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir) strbuf_strip_suffix(&repo, "/gitdir"); strbuf_realpath(&repo, repo.buf, 1); + if (use_relative_paths && !the_repository->repository_format_relative_worktrees) { + if (upgrade_repository_format(1) < 0) + die(_("unable to upgrade repository format to support relative worktrees")); + if (git_config_set_gently("extensions.relativeWorktrees", "true")) + die(_("unable to set extensions.relativeWorktrees setting")); + the_repository->repository_format_relative_worktrees = 1; + } + if (use_relative_paths) { write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp)); write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));