From patchwork Thu Oct 31 05:05:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857568 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 1081610E0 for ; Thu, 31 Oct 2024 05:05:35 +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=1730351138; cv=none; b=Hbiip16v1rUVMSSG2ME+e4Er10C5OH0xG2T93Ry6j7YXmcBIvimyAz8kfQkJhdUqQwlOBpJqtEiMqNe+bXsv4Ox8AD+6i7OvjKJKrTGTS7VaSkbjNwNYg9CFPIp8Fg5OToFoyufAVdI+n1y84iWyFJEyTAvS8MTg3A+tAP0gtas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351138; c=relaxed/simple; bh=FNqfxejyTh/NAYvcDN4jJ+OBaGZ998m+fpzdNMlcqsg=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HP82PRlpNC6fxOe1mzy14xKRMB3Hrep1Vp8A9ttkv9CnXkdudhpvz3PrDlUs5n8/fBXlAyWl/9iHsbultf5jJRN+7E3Wnt2/TiVxXvgWdvpKrqomRJSABf8B7w199lQrr6Hi2GHGsugR2wX4PqCz5OelBb/eG8w8xK1stEjBO7E= 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=RI+joepU; 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="RI+joepU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351134; x=1730610334; bh=h0cqGO+in4oPoDTpVBaHNPN66AD5WU3sI3fLGAcaPYI=; 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=RI+joepUKaKZ/7oRR2Z7EYiP2FSAIvsQdZKdRaei/g5Giios9O09siOyED24EpP3d C4qWv7cxoULJdOnIAZsOEQYscyipWhNn7tuij41v+CAeN36pQ6QA++d9zibB5V0/la j8q3JY2euP52not1U9EcyopPxBwkyKgGm/jPTPOwISpksdYeTWclzrDZvsmYg8xaDn BHt/85SW1ibsgMFBnZ+kNQoBC3R2W6TJB7DjYf93ytmDpq4KoKDMWH4Uhhng0rWh0T EWXVwhuAdaOsiVuMg6mQzsRLYLr5sEQaxWp0MRoy0B/AcZzTODRWRbag27UQstEmBk St48seddht/fg== Date: Thu, 31 Oct 2024 05:05:27 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 1/8] setup: correctly reinitialize repository version Message-ID: <20241031-wt_relative_options-v3-1-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 6308f8c10b8ecdf5f80626e19f5c459499832f68 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When reinitializing a repository, Git does not account for extensions other than `objectformat` and `refstorage` when determining the repository version. This can lead to a repository being downgraded to version 0 if extensions are set, causing Git future operations to fail. This patch teaches Git to check if other extensions are defined in the config to ensure that the repository version is set correctly. Signed-off-by: Caleb White --- setup.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/setup.c b/setup.c index 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644 --- a/setup.c +++ b/setup.c @@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo, enum ref_storage_format ref_storage_format, int reinit) { - char repo_version_string[10]; - int repo_version = GIT_REPO_VERSION; + struct strbuf repo_version = STRBUF_INIT; + int target_version = GIT_REPO_VERSION; /* * Note that we initialize the repository version to 1 when the ref @@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo, */ if (hash_algo != GIT_HASH_SHA1 || ref_storage_format != REF_STORAGE_FORMAT_FILES) - repo_version = GIT_REPO_VERSION_READ; - - /* This forces creation of new config file */ - xsnprintf(repo_version_string, sizeof(repo_version_string), - "%d", repo_version); - git_config_set("core.repositoryformatversion", repo_version_string); + target_version = GIT_REPO_VERSION_READ; if (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN) git_config_set("extensions.objectformat", @@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo, ref_storage_format_to_name(ref_storage_format)); else if (reinit) git_config_set_gently("extensions.refstorage", NULL); + + if (reinit) { + struct strbuf config = STRBUF_INIT; + struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; + + strbuf_git_common_path(&config, the_repository, "config"); + read_repository_format(&repo_fmt, config.buf); + + if (repo_fmt.v1_only_extensions.nr) + target_version = GIT_REPO_VERSION_READ; + + strbuf_release(&config); + clear_repository_format(&repo_fmt); + } + + strbuf_addf(&repo_version, "%d", target_version); + git_config_set("core.repositoryformatversion", repo_version.buf); + + strbuf_release(&repo_version); } static int is_reinit(void) @@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path, adjust_shared_perm(repo_get_git_dir(the_repository)); } - initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0); + initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit); /* Check filemode trustability */ path = git_path_buf(&buf, "config"); From patchwork Thu Oct 31 05:05:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857570 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 ADFFA83CD9 for ; Thu, 31 Oct 2024 05:05:49 +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=1730351151; cv=none; b=OoOn2CS4r93SA/e9XJx72E49nDZOszj69NLTmE8zv4M2l3vP+7SGCg9RRNr1OIo5eH4PY+z2jZhLKfBu9dt61eDpHsbiGznIY/STQGiChD//qNjwj0P/nI4VXVtMlon+USwgZ4Rj6cLIne6qjmkQtUT3PNFIDvOo6Cu5o05pEC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351151; c=relaxed/simple; bh=8MxPZdn1L5978tfqkhcficZ+8i6hBfaQ8ew5JQtRmOM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ah6Lqcso4rOd4QjBMOwWdbIfG5czkrBWmPnPeLAfICphp0SiUQnLvZ3XJa7ORmkj5yMLYoBoc1fjm8BWmdx73yMx0/I/Jmo/tMdnytzuzwZpGxDBK9WwlySLpGLm+XZe8GgYr/GquxoHDhB1EcgI4cUAxg6V9k0G/ECcNlO9mtc= 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=blslXw5f; 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="blslXw5f" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351140; x=1730610340; bh=nMqMaP/mEzs1WtgxDUhbCeyuGGDh1Au4R56Bq4fXY+g=; 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=blslXw5febQ00cDnYMlfFS1xDTN1ryAy7XwigoZNQRUiPCw9ZFVWHA1vaHtNbgIhf AEUiXR4roHflmnTsljfMT98gLMWzVGFC/zv508/LudNzsCPJz2cKXS/zNwreAfcAn7 lcAvAxAOSw3kJCFEdDzZ0h1NufjFUCKnS5iXWKHDhHuL8i6aD5h8XT36TVKDx/i1dJ vKEoZJgSjgfzIppu8rUPFsZgrJBtLWOuG3gzNPq1ynefiLdZzbVtfPaQkzQ5d9FgVa Uha8AvdwOYUEXy6xN65EDAPVVZg8SIVI/RuocQMqIIFAb6R8YkcMa+Mw0VN25Y25Dk 8ybiVSBwnqpvw== Date: Thu, 31 Oct 2024 05:05:35 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 2/8] worktree: add `relativeWorktrees` extension Message-ID: <20241031-wt_relative_options-v3-2-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 48e13aed45093cf48a2dd391c43aab745379c629 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 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 + 5 files changed, 16 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 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 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; From patchwork Thu Oct 31 05:05:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857569 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 510B67F460 for ; Thu, 31 Oct 2024 05:05:48 +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=1730351150; cv=none; b=pNYlKNT0aR/XifpPzYCo7oSVGZrmck7RRDPFOub0c3V4F0DM1P9R1g3GvstgjfxSrBgixiqJHFuSduJlgrESBgsmyfa0PSnd7YKI8UJwAASVZnHej2mWs1nsVR0ccRFu2r7YFdO9+5fIQYC3XOQDqV4LlI7hWbrcjoU76HyDmvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351150; c=relaxed/simple; bh=/Se9OEAa8Z5ULWhKCgQOxmzssQTQloBNxRN/a/DiHvE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VGSsp1lM7I1166QGUEOUK3U1eNFtc5KVYLeTOHwi679tJSud3c4gPjAIC3JXhXtG2UCLPqYmxb6/7zQ/VvwmdVcZtPZ2JA1In6cQUg5IUCOK0QAOQ8/bIX5fm82sFcE7n8KM/LoMMDI5Rn/WY0xIe8qhckiywNq0i6ErDa50EDY= 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=sMzY+LPL; 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="sMzY+LPL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351146; x=1730610346; bh=I4JVVGWtkHbLhQbETsc5Fv8E4IGvq+MC12C3u0SKQBk=; 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=sMzY+LPLEo4jZ7NJntdDhT+nXBzm/N1KnOPew0oQurhrXYSAWHELDCN60KcO1Oa5E KD+i/M2evil2TtBjMdgyxcpOK1u2vvBbSAdr51uqut49DYZ6AS0yZ5B2kfEnsmGdgv JYt/UxAXdg+UWg2S/aZtPFjTOdiRmnufLjF027wS5sLrvI4/WUkc2UbgRIE7wN3PtL 2p/5aUkU2ewOB4UMkZOHfDzFvdqKIC0xaQvPRWP/ekh3wqUF8S4qBZlfqgI8p+2bUy 0b+pbu8sWAfQc4lDLa3disoVJPMbLN/RC3aUtCnVNTVYu6Gw1iBL2LaIH41e+cW9vY iGUeMzPzZw6KQ== Date: Thu, 31 Oct 2024 05:05:42 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 3/8] worktree: refactor infer_backlink return Message-ID: <20241031-wt_relative_options-v3-3-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 82849a08d8776b0f172a80f16f24be8b2f6d8aaa Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The previous round[1] was merged a bit early before reviewer feedback could be applied. This correctly indents a code block and updates the `infer_backlink` function to return `-1` on failure and strbuf.len on success. [1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me Signed-off-by: Caleb White --- worktree.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/worktree.c b/worktree.c index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..9346d51c438cbd029e9e57591edd8c9f30cc7638 100644 --- a/worktree.c +++ b/worktree.c @@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id, 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); + strbuf_strip_suffix(&path, "gitdir"); + strbuf_addbuf(&path, &worktree_path); + strbuf_realpath_forgiving(&worktree_path, path.buf, 0); } CALLOC_ARRAY(worktree, 1); @@ -725,12 +725,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 int infer_backlink(const char *gitfile, struct strbuf *inferred) +static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred) { struct strbuf actual = STRBUF_INIT; const char *id; + strbuf_reset(inferred); if (strbuf_read_file(&actual, gitfile, 0) < 0) goto error; if (!starts_with(actual.buf, "gitdir:")) @@ -741,18 +744,16 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred) id++; /* advance past '/' to point at */ if (!*id) goto error; - strbuf_reset(inferred); strbuf_git_common_path(inferred, the_repository, "worktrees/%s", id); if (!is_directory(inferred->buf)) goto error; strbuf_release(&actual); - return 1; - + return inferred->len; error: strbuf_release(&actual); strbuf_reset(inferred); /* clear invalid path */ - return 0; + return -1; } /* From patchwork Thu Oct 31 05:05:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857571 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (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 088FB10E0 for ; Thu, 31 Oct 2024 05:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351158; cv=none; b=eA4GXp7UWMOzo2bKbEEVFXRkyhnWtwGVgTileax/SetO4BeO4eKfbR5lctFV6sNTMd5byD/IhYiJq+C4gfE4mxsU/aDAdwQA9Ndg3p+Z5DKL1kevjUUsDeNDBPh5rqA71uZ1UCZxo1fLNRxxN/rOwThwEK0Mu6q4Mc03PfdIZQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351158; c=relaxed/simple; bh=MGlYr4BJ2MzPAyfhZjOJw6s8y3kIjGM7tcLl9OI2rR4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RTYNhhTiSbIXzFGQCMsztdtfyGA8FSO1+YkfZpwrL6WvOYlF9XiWVasYSr7UNIwUwYBuiDKTsYQpboPfjyOpigpW82KSEaOP4mOvGtt5To5CkHeVtYgZq7W6RAIoJFRf8oZ8Qt6FZqMFiMoON7xxmyZqr1vIauBzl2OseX5xtQw= 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=NI0tQUZj; arc=none smtp.client-ip=185.70.40.131 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="NI0tQUZj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351153; x=1730610353; bh=Pz2745YuyaR91mhAlfM9GNNbzXiADDE1WJj81J2OlEc=; 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=NI0tQUZjNu0gE9N6jtiUgTsECYvSGar+mBS54Q9VoqxfdoixNZ1Oi/GYewZ3pq9Yy vwXqC5CjqhB6XeW8P9muGKOn/mqwLqOZHqe9ks2Fwc3/JEjZjWFL2hcqR+cIgItLYo vNYRH8WNpD7ttYrmMjT9+JNX7OmAe6pPICWEANaDIN0Rb0QcEes9yecv/5qi5PiJvH hLvptbl1RhgDI1vcPU+FoOiDrBT6N5Bw/ru5qxJpKkuwjd9lEA3HeLeKdFnxFQl0aN a0TvPGJs8gjhCef2xgsz6i74zAvdjPpXIQk7IRCSiWgYzEvaJ/kyNyhan19Lj+9mNZ fBi+LrY0LpOLg== Date: Thu, 31 Oct 2024 05:05:50 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 4/8] worktree: add `write_worktree_linking_files()` function Message-ID: <20241031-wt_relative_options-v3-4-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 3529d4b8fe93935826dc31684a7eaba6891f8a64 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A new helper function, `write_worktree_linking_files()`, centralizes the logic for computing and writing either relative or absolute paths, based on the provided configuration. This function accepts `strbuf` pointers to both the worktree’s `.git` link and the repository’s `gitdir`, and then writes the appropriate path to each. The `relativeWorktrees` extension is automatically set when a worktree is linked with relative paths. Signed-off-by: Caleb White --- worktree.c | 36 ++++++++++++++++++++++++++++++++++++ worktree.h | 14 ++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/worktree.c b/worktree.c index 9346d51c438cbd029e9e57591edd8c9f30cc7638..ab6f1e036fa019285a1a5152decb9aed5202909f 100644 --- a/worktree.c +++ b/worktree.c @@ -1032,3 +1032,39 @@ int init_worktree_config(struct repository *r) free(main_worktree_file); return res; } + +void write_worktree_linking_files(struct strbuf dotgit, + struct strbuf gitdir, + int use_relative_paths) +{ + 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 && !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)); + } 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 e96118621638667d87c5d7e0452ed10bd1ddf606..4786aa545ca1fa12bb16a55ddf5a59c06503b2c5 100644 --- a/worktree.h +++ b/worktree.h @@ -215,4 +215,18 @@ 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, + int use_relative_paths); + #endif From patchwork Thu Oct 31 05:05:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857572 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 59E907F460 for ; Thu, 31 Oct 2024 05:06:07 +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=1730351170; cv=none; b=tZFZCq7qpyeiixe0zkhlA6ttYx5nlT0MxycMZzRfTxWwMhprDD/aivDtvOS/pwTGg6yUGR5kFhyw2aT2ckT+OzY6CB9th+aAkp5MWkNTmGuqwzkYWeCsWC5MrHsAe3MdGX7x+4Sdxmbk64mUVEQ8sLo8hhYLyjU3qny33oAeSW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351170; c=relaxed/simple; bh=OcYqUYmq7kUZ6ad7NFd3vq6yg/qHaGu+ef+tfaztmfk=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bRuAvcbl6tKozJ/62ISI67haWH1wZY/851jUi9AKS3z9q3OlHC0MdW0p/8cg5tH85ux7dbFURbgdyD9A0tSGDc0jrpo01HbCKQufWfCvPozHqmeHBYKdHRXX8OAxCxjMt7xiHsBqGhQvoBQ+V5trBa4SPIHRVys+1jdHszq4Sr8= 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=HeQrbw6Z; 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="HeQrbw6Z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351165; x=1730610365; bh=4nf/0s7fmJxQRO4KCdzARXQdLcqah9m6kU/QB1LLZlM=; 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=HeQrbw6ZJlDphY03Ta88Zkt667CWTu04PXDSp88HtmzK927oVvbirnSQpazxtQFy8 eKbEwtdUVfhSiRW10iQGOthmv1fZw+N6wjtoazSuF8a5XqwwvsOTWbG4y9jTMYcQgf fFuyIspOFxLThVp/p/5cPzuybhEkM5AyohkOqN/T/WvDyKW+UVYGO1HHw4O/7kGVm1 uE5ld5/8uzF8uPJq/pEPMBnryPbJga2MUOd0oAA5H1GNCMgSuQPiQYEDIhHz9Cmuuv uwY2HWTkQZwLQycruyxxIHpFhDa+c5MsXcnKMbNpJvZ3RTXy67ZcfVex5E+vlJC+vb uaZ7AMPqbZ2bg== Date: Thu, 31 Oct 2024 05:05:58 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 5/8] worktree: add relative cli/config options to `add` command Message-ID: <20241031-wt_relative_options-v3-5-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: e8e3a01afea4f7150b6606fcec5edc77e77fe15b Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This introduces the `--[no-]relative-paths` CLI option and `worktree.useRelativePaths` configuration setting to the `worktree add` command. When enabled these options allow worktrees to be linked using relative paths, enhancing portability across environments where absolute paths may differ (e.g., containerized setups, shared network drives). Git still creates absolute paths by default, but these options allow users to opt-in to relative paths if desired. The t2408 test file is removed and more comprehensive tests are written for the various worktree operations in their own files. Signed-off-by: Caleb White --- Documentation/config/worktree.txt | 10 ++++++++ Documentation/git-worktree.txt | 4 +++ builtin/worktree.c | 19 +++++++------- t/t2400-worktree-add.sh | 54 +++++++++++++++++++++++++++++++++++++++ t/t2401-worktree-prune.sh | 3 ++- t/t2402-worktree-list.sh | 22 ++++++++++++++++ t/t2408-worktree-relative.sh | 39 ---------------------------- 7 files changed, 102 insertions(+), 49 deletions(-) diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt index 048e349482df6c892055720eb53cdcd6c327b6ed..5e35c7d018aecdedca0642b11e45df6d19024d42 100644 --- a/Documentation/config/worktree.txt +++ b/Documentation/config/worktree.txt @@ -7,3 +7,13 @@ 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:: + Link worktrees using relative paths (when "true") or absolute + paths (when "false"). This is particularly useful for setups + where the repository and worktrees may be moved between + different locations or environments. Defaults to "false". ++ +Note that setting `worktree.useRelativePaths` to "true" implies enabling the +`extension.relativeWorktrees` config (see linkgit:git-config[1]), +thus making it incompatible with older versions of Git. diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 70437c815f13852bd2eb862176b8b933e6de0acf..88d2261012166a929b7f167d79720e4d965fd71b 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -216,6 +216,10 @@ 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:: + Overrides the `worktree.useRelativePaths` config option, see + linkgit:git-config[1]. + --[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 dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -120,12 +120,14 @@ struct add_opts { int quiet; int checkout; int orphan; + int relative_paths; const char *keep_locked; }; static int show_only; static int verbose; static int guess_remote; +static int use_relative_paths; static timestamp_t expire; static int git_worktree_config(const char *var, const char *value, @@ -134,6 +136,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,8 +419,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, sb_tmp = STRBUF_INIT; - struct strbuf sb_path_realpath = STRBUF_INIT, sb_repo_realpath = STRBUF_INIT; + struct strbuf sb = STRBUF_INIT; const char *name; struct strvec child_env = STRVEC_INIT; unsigned int counter = 0; @@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname, strbuf_reset(&sb); strbuf_addf(&sb, "%s/gitdir", sb_repo.buf); - strbuf_realpath(&sb_path_realpath, path, 1); - strbuf_realpath(&sb_repo_realpath, sb_repo.buf, 1); - write_file(sb.buf, "%s/.git", relative_path(sb_path_realpath.buf, sb_repo_realpath.buf, &sb_tmp)); - write_file(sb_git.buf, "gitdir: %s", relative_path(sb_repo_realpath.buf, sb_path_realpath.buf, &sb_tmp)); + write_worktree_linking_files(sb_git, sb, opts->relative_paths); strbuf_reset(&sb); strbuf_addf(&sb, "%s/commondir", sb_repo.buf); write_file(sb.buf, "../.."); @@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname, strvec_clear(&child_env); strbuf_release(&sb); - strbuf_release(&sb_tmp); strbuf_release(&symref); strbuf_release(&sb_repo); - strbuf_release(&sb_repo_realpath); strbuf_release(&sb_git); - strbuf_release(&sb_path_realpath); strbuf_release(&sb_name); free_worktree(wt); return ret; @@ -796,12 +794,15 @@ 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", &opts.relative_paths, + N_("use relative paths for worktrees")), OPT_END() }; int ret; memset(&opts, 0, sizeof(opts)); opts.checkout = 1; + opts.relative_paths = use_relative_paths; ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0); if (!!opts.detach + !!new_branch + !!new_branch_force > 1) die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach"); diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index cfc4aeb1798c6d023909cec771e5b74e983af5ea..d36d8a4db0e924877787697579544f10f92dc0cf 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -1207,4 +1207,58 @@ 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_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/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh index 976d048e3efc74be9cd909ce76d552b3944d2e10..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755 --- a/t/t2401-worktree-prune.sh +++ b/t/t2401-worktree-prune.sh @@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' ' ! test -d .git/worktrees/wt ' -test_expect_success 'not prune proper worktrees when run inside linked worktree' ' +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 && 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/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh deleted file mode 100755 index a3136db7e28cb20926ff44211e246ce625a6e51a..0000000000000000000000000000000000000000 --- a/t/t2408-worktree-relative.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh - -test_description='test worktrees linked with relative paths' - -TEST_PASSES_SANITIZE_LEAK=true -. ./test-lib.sh - -test_expect_success 'links worktrees with relative paths' ' - test_when_finished rm -rf repo && - git init repo && - ( - cd repo && - test_commit initial && - git worktree add wt1 && - echo "../../../wt1/.git" >expected_gitdir && - cat .git/worktrees/wt1/gitdir >actual_gitdir && - echo "gitdir: ../.git/worktrees/wt1" >expected_git && - cat wt1/.git >actual_git && - test_cmp expected_gitdir actual_gitdir && - test_cmp expected_git actual_git - ) -' - -test_expect_success 'move repo without breaking relative internal links' ' - test_when_finished rm -rf repo moved && - git init repo && - ( - cd repo && - 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 From patchwork Thu Oct 31 05:06:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857573 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 D66A57F460 for ; Thu, 31 Oct 2024 05:06:12 +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=1730351175; cv=none; b=G5UmefVJL48npl+7HF3tKZ3qdKis7SC59BGoMCQPmDWsZ5G2pphyTODOTc82tWLujVXsxj5wFkGX8LFDY1NVY1aW41VGaAnK3CAJlW9Ax5KuNreBGMgC3oKqmjOevB663EmN3YVnNOrK73J1/fUu6wef2SJPMeGRe5PADUZgJQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351175; c=relaxed/simple; bh=i1yN7/D47SaLpCixDV1krhD1Lnr/bNLATK25Zr1s8fs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bq1ClDfmikJM7DL0Gg+YRMrvq81p2YvpMtvGqk/TSwnFj1gwIWm/i7+0r6Qd/jahP9dJql9iYiqSQ8vvMiVP4Rr4SEs1+2sxJI9q8kwEw0IsnhCO8L6tT6TNzD515QOf0H5ws6BC47vQxyVkNMqVgHVmk68hZ9oC/LfQfnfmFjs= 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=Q0WZEpgU; 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="Q0WZEpgU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351170; x=1730610370; bh=FPVsN+dpkjuPzI93fmpmkajfAMhILun+V5LsX4KFflg=; 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=Q0WZEpgUTK7UmLjocN9aBrQnAZ+rXPr7FjFSfIBFAd4QojpiaSJymkd7aD2tFNsHL 0mo88havjRGdySDGLOjD27a+5ki4HWQ2OA/JYMKwLTzDHa1+r5M0QNZbbk6S/j64wa e6NTSJj6HDzjHbRhLl80muplrZ0zYzrBjpobmvu2plJVHtXK7juntExiqmZcQt4Uwf KUdeVNadAcKfC7PiZTrU4oBovx9IRf631ZubvYbss1PS8YlUgUOh/VolqYgjK2+7hT nl2R3Sh3db0BlYjr3rrQQBaOLJQbgloutOjLr90xJKaEF+9tXkiMaz2joT+JVqR7eQ jFfYitiRkM5Sw== Date: Thu, 31 Oct 2024 05:06:06 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 6/8] worktree: add relative cli/config options to `move` command Message-ID: <20241031-wt_relative_options-v3-6-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: c4332f4a9264fcf4765fcdc33f87f99f1e34551a Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This teaches the `worktree move` command to respect the `--[no-]relative-paths` CLI option and `worktree.useRelativePaths` config setting. If an existing worktree is moved with `--relative-paths` the new path will be relative (and visa-versa). Signed-off-by: Caleb White --- builtin/worktree.c | 4 +++- t/t2403-worktree-move.sh | 22 ++++++++++++++++++++++ worktree.c | 23 ++++++++++------------- worktree.h | 3 ++- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -1190,6 +1190,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; @@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix) if (rename(wt->path, dst.buf) == -1) die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf); - update_worktree_location(wt, dst.buf); + update_worktree_location(wt, dst.buf, use_relative_paths); strbuf_release(&dst); free_worktrees(worktrees); 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/worktree.c b/worktree.c index ab6f1e036fa019285a1a5152decb9aed5202909f..6b640cd9549ecb060236f7eddf1390caa181f1a0 100644 --- a/worktree.c +++ b/worktree.c @@ -376,32 +376,29 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg, return ret; } -void update_worktree_location(struct worktree *wt, const char *path_) +void update_worktree_location(struct worktree *wt, + const char *path_, + int use_relative_paths) { struct strbuf path = STRBUF_INIT; - struct strbuf repo = STRBUF_INIT; - struct strbuf file = STRBUF_INIT; - struct strbuf tmp = 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(&repo, git_common_path("worktrees/%s", wt->id), 1); + 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)) { - strbuf_addf(&file, "%s/gitdir", repo.buf); - write_file(file.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp)); - strbuf_reset(&file); - strbuf_addf(&file, "%s/.git", path.buf); - write_file(file.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp)); + write_worktree_linking_files(dotgit, gitdir, use_relative_paths); free(wt->path); wt->path = strbuf_detach(&path, NULL); } strbuf_release(&path); - strbuf_release(&repo); - strbuf_release(&file); - strbuf_release(&tmp); + strbuf_release(&dotgit); + strbuf_release(&gitdir); } int is_worktree_being_rebased(const struct worktree *wt, diff --git a/worktree.h b/worktree.h index 4786aa545ca1fa12bb16a55ddf5a59c06503b2c5..6dd5dfc9b71870320c96aae35a7a983b5b9ba9a7 100644 --- a/worktree.h +++ b/worktree.h @@ -118,7 +118,8 @@ int validate_worktree(const struct worktree *wt, * Update worktrees/xxx/gitdir with the new path. */ void update_worktree_location(struct worktree *wt, - const char *path_); + const char *path_, + int use_relative_paths); typedef void (* worktree_repair_fn)(int iserr, const char *path, const char *msg, void *cb_data); From patchwork Thu Oct 31 05:06:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857574 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 12B3A7F460 for ; Thu, 31 Oct 2024 05:06:21 +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=1730351185; cv=none; b=t0JKbajwrBHrAu6rWkC+1e+xTZTSTLptJD2ASOMo6vTc9sN9M2NBKUvJT0KbSRPWyMQzfHGcoeZbWKwfeUbHlN8nXJPWAkd8zvWPeRZVlObslm2lCdib4/4ZzEF6B+uOH0GXU6HWrbH+0fP48yBXv2MgpRrH7txdqZjQThy5q6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351185; c=relaxed/simple; bh=NaLIt9/R/ZopuKOarezqXA+gWg+UCfOoNy5xfYn/qPM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qnvmeYwv2w8o5jPZZhhP7PNOXauXOONP2i1VtcHtYR+BFdJJVoBtwmylPpevQjFBqMzsGd9/CUkrMrhalH+zR5ycfzGLms/2w8oin1MsARF8tqv7TNLNfQSvRBX56fUzZZ2v0xIeanGOeV6yqZGydjWI3jaRwW/Lmme/lC1iDjY= 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=CD0Z9VX/; 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="CD0Z9VX/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351180; x=1730610380; bh=Y7cTFxySM2FlwWOAO6t25hoFSiE1K8/ErABiqRK2G3I=; 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=CD0Z9VX/3D5oIAyOkq1r+nks8wNqe0YolT11fAQGJbxX13S6AquS1a7escTBtA2n4 xsx/WmTSmQ+eU/Rfu5vmlkLzgaQuSvKdaD4oYYWR+6rtkmLf/M7vNIRa/cAAykyeIC 6pWx2kGiv/82tJo0uKa+A74gHvnhCcKRnsn7XYv6fER6otVXPBSDxi8P0B26OsPPpA yIieNaQRvt7GfKUgqBh3UlTiVJISRnIs1LJfaKu/eog76AjE7/oAo/ozDd/lS9XVJ4 YN6ioEtde22twmdMAFXNNHIrad9LvcE4Ysln7RWeFXd4EZUvuCVXuKiolzIWlsRfYJ +/VTSxzS3ZaYA== Date: Thu, 31 Oct 2024 05:06:14 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 7/8] worktree: add relative cli/config options to `repair` command Message-ID: <20241031-wt_relative_options-v3-7-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 3fb959f0db07f09a074521c7fa9ac0859989c7a8 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This teaches the `worktree repair` command to respect the `--[no-]relative-paths` CLI option and `worktree.useRelativePaths` config setting. If an existing worktree with an absolute path is repaired with `--relative-paths`, the links will be replaced with relative paths, even if the original path was correct. This allows a user to covert existing worktrees between absolute/relative as desired. Signed-off-by: Caleb White --- Documentation/git-worktree.txt | 3 +++ builtin/worktree.c | 6 +++-- t/t2406-worktree-repair.sh | 26 +++++++++++++++++++ worktree.c | 58 +++++++++++++++++++++--------------------- worktree.h | 5 ++-- 5 files changed, 65 insertions(+), 33 deletions(-) diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 88d2261012166a929b7f167d79720e4d965fd71b..1d86323def16b15fbc85365aaea18ce6a2ef433a 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -219,6 +219,9 @@ This can also be set up as the default behaviour by using the --[no-]relative-paths:: Overrides the `worktree.useRelativePaths` config option, see linkgit:git-config[1]. ++ +With `repair`, the linking files will be updated if there's an absolute/relative +mismatch, even if the links are correct. --[no-]track:: When creating a new branch, if `` is a branch, diff --git a/builtin/worktree.c b/builtin/worktree.c index 302151506981718658db1cd338cd9064688f5c14..fde9ff4dc9a734c655e95ccd62774282950cbba6 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -1385,6 +1385,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; @@ -1392,8 +1394,8 @@ static int repair(int ac, const char **av, const char *prefix) ac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0); p = ac > 0 ? av : self; for (; *p; p++) - repair_worktree_at_path(*p, report_repair, &rc); - repair_worktrees(report_repair, &rc); + repair_worktree_at_path(*p, report_repair, &rc, use_relative_paths); + repair_worktrees(report_repair, &rc, use_relative_paths); return rc; } 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 6b640cd9549ecb060236f7eddf1390caa181f1a0..2cb994ac462debf966ac51b5a4f33c30cfebd4ef 100644 --- a/worktree.c +++ b/worktree.c @@ -574,12 +574,14 @@ int other_head_refs(each_ref_fn fn, void *cb_data) * pointing at /worktrees/. */ static void repair_gitfile(struct worktree *wt, - worktree_repair_fn fn, void *cb_data) + worktree_repair_fn fn, + void *cb_data, + int use_relative_paths) { struct strbuf dotgit = STRBUF_INIT; + struct strbuf gitdir = STRBUF_INIT; struct strbuf repo = STRBUF_INIT; struct strbuf backlink = STRBUF_INIT; - struct strbuf tmp = STRBUF_INIT; char *dotgit_contents = NULL; const char *repair = NULL; int err; @@ -595,6 +597,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) { @@ -612,18 +615,20 @@ 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", relative_path(repo.buf, wt->path, &tmp)); + write_worktree_linking_files(dotgit, gitdir, use_relative_paths); } done: free(dotgit_contents); strbuf_release(&repo); strbuf_release(&dotgit); + strbuf_release(&gitdir); strbuf_release(&backlink); - strbuf_release(&tmp); } static void repair_noop(int iserr UNUSED, @@ -634,7 +639,7 @@ static void repair_noop(int iserr UNUSED, /* nothing */ } -void repair_worktrees(worktree_repair_fn fn, void *cb_data) +void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths) { struct worktree **worktrees = get_worktrees_internal(1); struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ @@ -642,7 +647,7 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data) if (!fn) fn = repair_noop; for (; *wt; wt++) - repair_gitfile(*wt, fn, cb_data); + repair_gitfile(*wt, fn, cb_data, use_relative_paths); free_worktrees(worktrees); } @@ -758,16 +763,15 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred) * the worktree's path. */ void repair_worktree_at_path(const char *path, - worktree_repair_fn fn, void *cb_data) + worktree_repair_fn fn, + void *cb_data, + int use_relative_paths) { 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; - struct strbuf tmp = STRBUF_INIT; char *dotgit_contents = NULL; const char *repair = NULL; int err; @@ -779,25 +783,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) { @@ -810,11 +814,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; } @@ -836,39 +840,35 @@ 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.len && fspathcmp(backlink.buf, inferred_backlink.buf)) { + if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) strbuf_swap(&backlink, &inferred_backlink); - } 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", relative_path(realdotgit.buf, backlink.buf, &tmp)); + write_worktree_linking_files(dotgit, gitdir, use_relative_paths); } 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); - strbuf_release(&tmp); } int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire) diff --git a/worktree.h b/worktree.h index 6dd5dfc9b71870320c96aae35a7a983b5b9ba9a7..f158a2ee429cc7af971bf4a6665c058a9e15614a 100644 --- a/worktree.h +++ b/worktree.h @@ -130,7 +130,7 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path, * function, if non-NULL, is called with the path of the worktree and a * description of the repair or error, along with the callback user-data. */ -void repair_worktrees(worktree_repair_fn, void *cb_data); +void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths); /* * Repair the linked worktrees after the gitdir has been moved. @@ -152,7 +152,8 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path * worktree and a description of the repair or error, along with the callback * user-data. */ -void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data); +void repair_worktree_at_path(const char *, worktree_repair_fn, + void *cb_data, int use_relative_paths); /* * Free up the memory for a worktree. From patchwork Thu Oct 31 05:06:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb White X-Patchwork-Id: 13857575 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (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 A1C3913B590 for ; Thu, 31 Oct 2024 05:06:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351196; cv=none; b=bWk9Pw/T7lMXUboAclgfU/T5p+ush4hj0JC7i52q+Gly0YmEKNQfZ8gUtym1SnHqjGLfyNs9hLyT10RKbO+emw2AKb74ZzwxbpHlnXe6O2XOBVT02ihLFjkS25iyZrYr0DqZfE3eqt3owsMTGdUTxQqnazFbJshG0sZKs8EIajI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730351196; c=relaxed/simple; bh=Ugjkf74Fo1MulgkqG7G9TkIq657STI4NcK+koZ9BYvo=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FLiOJgADYz4DRZvbiDP32zJBNeDZF9j+ONhaCpBZzvRmlpJipxGQqXm+uGVxYiJTCmnc09RNfln02lN4iHXQ5fjVE3Xs4fP9ZV5zy+BRFup6+8ozLMkJBXOXw2WGJsokK6l/97GlcoNFOmySx6paQhcout9x30UmeY9QVrl1MD0= 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=Gf90hZzS; arc=none smtp.client-ip=185.70.40.134 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="Gf90hZzS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1730351185; x=1730610385; bh=U7NPavXSWlhmJ/iw0vxNYrKPBygr4STXhsC4rw5z1yA=; 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=Gf90hZzS+BItKmNtql6NmEILTmzXKFXVJybKnkH05ySgMdVZzIxTulBh6jiUxXCZr oznvcyv1Fs/HMFTzzybMoliwpAutPiTfcBYO/bM6gYtVMh2qZ1AQ3nu03VQEm/f4mQ o39tAHD5Xg8vPcNKplgudWO21co4Kykmn6V/oNjofQulwqSOg3OvJZ3C1ks8VLSrXG wYrcKV+xGeKXUHkAR+U8aXCr++I+8r6xUrWBU2dvFa5199wniZ8qyckuVzLHX/Lm9b w/NZ36QH0ReFOnrO1uh0c1fdfORh8bIwuVMp9n8K9Hs5OrfL8g/KhMiWhJHZ9BemRI EIiLsWBqGjItw== Date: Thu, 31 Oct 2024 05:06:21 +0000 To: git@vger.kernel.org From: Caleb White Cc: Taylor Blau , Phillip Wood , Junio C Hamano , Eric Sunshine , Caleb White Subject: [PATCH v3 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Message-ID: <20241031-wt_relative_options-v3-8-3e44ccdf64e6@pm.me> In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> References: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me> Feedback-ID: 31210263:user:proton X-Pm-Message-ID: 9f953c7ab8cf35ffaa6a6dc82db72153a4f81ee9 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This refactors `repair_worktree_after_gitdir_move()` to use the new `write_worktree_linking_files` function. It also preserves the relativity of the linking files; e.g., if an existing worktree used absolute paths then the repaired paths will be absolute (and visa-versa). This also adds a test case for reinitializing a repository that has relative worktrees. Signed-off-by: Caleb White --- t/t0001-init.sh | 22 ++++++++++++++++++---- worktree.c | 29 ++++++++--------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..264951592a711f6879555b785905e799db6c5dd4 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' ' sep_git_dir_worktree () { test_when_finished "rm -rf mainwt linkwt seprepo" && git init mainwt && + if test "relative" = $2 + then + git -C mainwt config worktree.useRelativePaths true + else + git -C mainwt config worktree.useRelativePaths false + fi test_commit -C mainwt gumby && git -C mainwt worktree add --detach ../linkwt && git -C "$1" init --separate-git-dir ../seprepo && @@ -442,12 +448,20 @@ sep_git_dir_worktree () { 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 mainwt absolute +' + +test_expect_success 're-init to move gitdir within linked worktree (absolute)' ' + sep_git_dir_worktree linkwt absolute +' + +test_expect_success 're-init to move gitdir with linked worktrees (relative)' ' + sep_git_dir_worktree mainwt relative ' -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 (relative)' ' + sep_git_dir_worktree linkwt relative ' test_expect_success MINGW '.git hidden' ' diff --git a/worktree.c b/worktree.c index 2cb994ac462debf966ac51b5a4f33c30cfebd4ef..2c2e9fd8fdc22d4fe3705227d72b8dbd293038b2 100644 --- a/worktree.c +++ b/worktree.c @@ -653,45 +653,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path) { - struct strbuf path = STRBUF_INIT; - struct strbuf repo = STRBUF_INIT; struct strbuf gitdir = STRBUF_INIT; struct strbuf dotgit = STRBUF_INIT; - struct strbuf olddotgit = STRBUF_INIT; - struct strbuf tmp = STRBUF_INIT; + int is_relative_path; if (is_main_worktree(wt)) goto done; - strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1); - strbuf_addf(&gitdir, "%s/gitdir", repo.buf); + strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1); - if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) + if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0) goto done; - strbuf_rtrim(&olddotgit); - if (is_absolute_path(olddotgit.buf)) { - strbuf_addbuf(&dotgit, &olddotgit); - } else { - strbuf_addf(&dotgit, "%s/worktrees/%s/%s", old_path, wt->id, olddotgit.buf); + strbuf_rtrim(&dotgit); + is_relative_path = ! is_absolute_path(dotgit.buf); + if (is_relative_path) { + 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; - strbuf_addbuf(&path, &dotgit); - strbuf_strip_suffix(&path, "/.git"); - - write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp)); - write_file(gitdir.buf, "%s", relative_path(dotgit.buf, repo.buf, &tmp)); + write_worktree_linking_files(dotgit, gitdir, is_relative_path); done: - strbuf_release(&path); - strbuf_release(&repo); strbuf_release(&gitdir); strbuf_release(&dotgit); - strbuf_release(&olddotgit); - strbuf_release(&tmp); } void repair_worktrees_after_gitdir_move(const char *old_path)