{"thread":{"id":"62433","subject":"[PATCH v4 1/8] setup: correctly reinitialize repository version","startedAt":"2024-11-01T04:38:27Z","lastAt":"2024-12-03T05:22:06Z","messageCount":60,"participants":["Caleb White","Junio C Hamano","Kristoffer Haugsbakk","Phillip Wood","phillip.wood123@gmail.com"],"isPatch":true,"patchVersion":4,"patchTotal":8},"messages":[{"id":"506426","messageId":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":null,"subject":"[PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:15Z","receivedAt":"2024-11-01T04:38:27Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This series introduces the `--[no-]relative-paths` CLI option for\n`git worktree {add, move, repair}` commands, as well as the\n`worktree.useRelativePaths` configuration setting. When enabled,\nthese options allow worktrees to be linked using relative paths,\nenhancing portability across environments where absolute paths\nmay differ (e.g., containerized setups, shared network drives).\nGit still creates absolute paths by default, but these options allow\nusers to opt-in to relative paths if desired.\n\nUsing the `--relative-paths` option with `worktree {move, repair}`\nwill convert absolute paths to relative ones, while `--no-relative-paths`\ndoes the reverse. For cases where users want consistency in path handling,\nthe config option `worktree.useRelativePaths` provides a persistent setting.\n\nA new extension, `relativeWorktrees`, is added to indicate that at least\none worktree in the repository has been linked with relative paths. This\nextension is automatically set when a worktree is created or repaired\nusing the `--relative-paths` option, or when the\n`worktree.useRelativePaths` config is set to `true`.\n\nThe `relativeWorktrees` extension ensures older Git versions do not\nattempt to automatically prune worktrees with relative paths, as they\nwould not not recognize the paths as being valid.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\nThe base for this patch series is obtained by applying the following\npatch onto 6a11438f43:\n- cw/config-extensions topic (doc: consolidate extensions in git-config\n  documentation, 2024-10-22, <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>)\n\nLink to original patch series:\nhttps://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n\n---\nChanges in v4:\n- Fixed failing test in ci\n- Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me\nChanges in v3:\n- Split patches into smaller edits.\n- Moved tests into the patches with the relevant code changes.\n- Removed global `use_relative_paths` and instead pass parameter to functions.\n- Changed `infer_backlink` return type from `int` to `ssize_t`.\n- Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.\n- Reordered patches\n- Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me\nChanges in v2:\n- Fixed a bug where repositories with valid extensions would be downgraded\n  to v0 during reinitialization, causing future operations to fail.\n- Split patch [1/2] into three separate patches.\n- Updated cover letter and commit messages.\n- Updated documentation wording.\n- Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me\n\n---\nCaleb White (8):\n      setup: correctly reinitialize repository version\n      worktree: add `relativeWorktrees` extension\n      worktree: refactor infer_backlink return\n      worktree: add `write_worktree_linking_files()` function\n      worktree: add relative cli/config options to `add` command\n      worktree: add relative cli/config options to `move` command\n      worktree: add relative cli/config options to `repair` command\n      worktree: refactor `repair_worktree_after_gitdir_move()`\n\n Documentation/config/extensions.txt |   6 ++\n Documentation/config/worktree.txt   |  10 +++\n Documentation/git-worktree.txt      |   7 ++\n builtin/worktree.c                  |  29 ++++---\n repository.c                        |   1 +\n repository.h                        |   1 +\n setup.c                             |  39 +++++++--\n setup.h                             |   1 +\n t/t0001-init.sh                     |  22 ++++-\n t/t2400-worktree-add.sh             |  54 ++++++++++++\n t/t2401-worktree-prune.sh           |   3 +-\n t/t2402-worktree-list.sh            |  22 +++++\n t/t2403-worktree-move.sh            |  22 +++++\n t/t2406-worktree-repair.sh          |  26 ++++++\n t/t2408-worktree-relative.sh        |  39 ---------\n t/t5504-fetch-receive-strict.sh     |   6 +-\n worktree.c                          | 163 ++++++++++++++++++++----------------\n worktree.h                          |  22 ++++-\n 18 files changed, 331 insertions(+), 142 deletions(-)\n---\nbase-commit: 6a11438f43469f3815f2f0fc997bd45792ff04c0\nchange-id: 20241025-wt_relative_options-afa41987bc32\nprerequisite-change-id: 20241020-cleanup-extension-docs-f365868711bf:v1\nprerequisite-patch-id: 60a443b24e92938b9b6f4a016a7bab87e13bf3ea\n\nBest regards,\n-- \nCaleb White <cdwhite3@pm.me>\n\n\n"},{"id":"506425","messageId":"20241031-wt_relative_options-v4-1-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 1/8] setup: correctly reinitialize repository version","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:20Z","receivedAt":"2024-11-01T04:38:28Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"When reinitializing a repository, Git does not account for extensions\nother than `objectformat` and `refstorage` when determining the\nrepository version. This can lead to a repository being downgraded to\nversion 0 if extensions are set, causing Git future operations to fail.\n\nThis patch teaches Git to check if other extensions are defined in the\nconfig to ensure that the repository version is set correctly.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n setup.c                         | 32 +++++++++++++++++++++++---------\n t/t5504-fetch-receive-strict.sh |  6 +++---\n 2 files changed, 26 insertions(+), 12 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,\n \t\t\t\t   enum ref_storage_format ref_storage_format,\n \t\t\t\t   int reinit)\n {\n-\tchar repo_version_string[10];\n-\tint repo_version = GIT_REPO_VERSION;\n+\tstruct strbuf repo_version = STRBUF_INIT;\n+\tint target_version = GIT_REPO_VERSION;\n \n \t/*\n \t * Note that we initialize the repository version to 1 when the ref\n@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,\n \t */\n \tif (hash_algo != GIT_HASH_SHA1 ||\n \t    ref_storage_format != REF_STORAGE_FORMAT_FILES)\n-\t\trepo_version = GIT_REPO_VERSION_READ;\n-\n-\t/* This forces creation of new config file */\n-\txsnprintf(repo_version_string, sizeof(repo_version_string),\n-\t\t  \"%d\", repo_version);\n-\tgit_config_set(\"core.repositoryformatversion\", repo_version_string);\n+\t\ttarget_version = GIT_REPO_VERSION_READ;\n \n \tif (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)\n \t\tgit_config_set(\"extensions.objectformat\",\n@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,\n \t\t\t       ref_storage_format_to_name(ref_storage_format));\n \telse if (reinit)\n \t\tgit_config_set_gently(\"extensions.refstorage\", NULL);\n+\n+\tif (reinit) {\n+\t\tstruct strbuf config = STRBUF_INIT;\n+\t\tstruct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;\n+\n+\t\tstrbuf_git_common_path(&config, the_repository, \"config\");\n+\t\tread_repository_format(&repo_fmt, config.buf);\n+\n+\t\tif (repo_fmt.v1_only_extensions.nr)\n+\t\t\ttarget_version = GIT_REPO_VERSION_READ;\n+\n+\t\tstrbuf_release(&config);\n+\t\tclear_repository_format(&repo_fmt);\n+\t}\n+\n+\tstrbuf_addf(&repo_version, \"%d\", target_version);\n+\tgit_config_set(\"core.repositoryformatversion\", repo_version.buf);\n+\n+\tstrbuf_release(&repo_version);\n }\n \n static int is_reinit(void)\n@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,\n \t\tadjust_shared_perm(repo_get_git_dir(the_repository));\n \t}\n \n-\tinitialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);\n+\tinitialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);\n \n \t/* Check filemode trustability */\n \tpath = git_path_buf(&buf, \"config\");\ndiff --git a/t/t5504-fetch-receive-strict.sh b/t/t5504-fetch-receive-strict.sh\nindex 138e6778a477650ecbe2dc3e480c5fe83d4bb485..290d2a591adae02acf5bcf24dbbff2a0bbfceac8 100755\n--- a/t/t5504-fetch-receive-strict.sh\n+++ b/t/t5504-fetch-receive-strict.sh\n@@ -171,7 +171,7 @@ test_expect_success 'fsck with invalid or bogus skipList input' '\n \ttest_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \ttest_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err\n+\ttest_grep \"invalid object name: \" err\n '\n \n test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '\n@@ -234,7 +234,7 @@ test_expect_success 'push with receive.fsck.skipList' '\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \tgit --git-dir=dst/.git config receive.fsck.skipList config &&\n \ttest_must_fail git push --porcelain dst bogus 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err &&\n+\ttest_grep \"invalid object name: \" err &&\n \n \tgit --git-dir=dst/.git config receive.fsck.skipList SKIP &&\n \tgit push --porcelain dst bogus\n@@ -263,7 +263,7 @@ test_expect_success 'fetch with fetch.fsck.skipList' '\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \tgit --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&\n \ttest_must_fail git --git-dir=dst/.git fetch \"file://$(pwd)\" $refspec 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err &&\n+\ttest_grep \"invalid object name: \" err &&\n \n \tgit --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&\n \tgit --git-dir=dst/.git fetch \"file://$(pwd)\" $refspec\n\n-- \n2.47.0\n\n\n"},{"id":"506427","messageId":"20241031-wt_relative_options-v4-2-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 2/8] worktree: add `relativeWorktrees` extension","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:27Z","receivedAt":"2024-11-01T04:38:32Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"A new extension, `relativeWorktrees`, is added to indicate that at least\none worktree in the repository has been linked with relative paths.\nThis ensures older Git versions do not attempt to automatically prune\nworktrees with relative paths, as they would not not recognize the\npaths as being valid.\n\nSuggested-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/config/extensions.txt | 6 ++++++\n repository.c                        | 1 +\n repository.h                        | 1 +\n setup.c                             | 7 +++++++\n setup.h                             | 1 +\n 5 files changed, 16 insertions(+)\n\ndiff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt\nindex 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644\n--- a/Documentation/config/extensions.txt\n+++ b/Documentation/config/extensions.txt\n@@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or\n linkgit:git-clone[1]. Trying to change it after initialization will not\n work and will produce hard-to-diagnose issues.\n \n+relativeWorktrees::\n+\tIf enabled, indicates at least one worktree has been linked with\n+\trelative paths. Automatically set if a worktree has been created or\n+\trepaired with either the `--relative-paths` option or with the\n+\t`worktree.useRelativePaths` config set to `true`.\n+\n worktreeConfig::\n \tIf enabled, then worktrees will load config settings from the\n \t`$GIT_DIR/config.worktree` file in addition to the\ndiff --git a/repository.c b/repository.c\nindex f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644\n--- a/repository.c\n+++ b/repository.c\n@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,\n \trepo_set_compat_hash_algo(repo, format.compat_hash_algo);\n \trepo_set_ref_storage_format(repo, format.ref_storage_format);\n \trepo->repository_format_worktree_config = format.worktree_config;\n+\trepo->repository_format_relative_worktrees = format.relative_worktrees;\n \n \t/* take ownership of format.partial_clone */\n \trepo->repository_format_partial_clone = format.partial_clone;\ndiff --git a/repository.h b/repository.h\nindex 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -150,6 +150,7 @@ struct repository {\n \n \t/* Configurations */\n \tint repository_format_worktree_config;\n+\tint repository_format_relative_worktrees;\n \n \t/* Indicate if a repository has a different 'commondir' from 'gitdir' */\n \tunsigned different_commondir:1;\ndiff --git a/setup.c b/setup.c\nindex 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var,\n \t\t\t\t     \"extensions.refstorage\", value);\n \t\tdata->ref_storage_format = format;\n \t\treturn EXTENSION_OK;\n+\t} else if (!strcmp(ext, \"relativeworktrees\")) {\n+\t\tdata->relative_worktrees = git_config_bool(var, value);\n+\t\treturn EXTENSION_OK;\n \t}\n \treturn EXTENSION_UNKNOWN;\n }\n@@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\t\t\t\t\t    repo_fmt.ref_storage_format);\n \t\t\tthe_repository->repository_format_worktree_config =\n \t\t\t\trepo_fmt.worktree_config;\n+\t\t\tthe_repository->repository_format_relative_worktrees =\n+\t\t\t\trepo_fmt.relative_worktrees;\n \t\t\t/* take ownership of repo_fmt.partial_clone */\n \t\t\tthe_repository->repository_format_partial_clone =\n \t\t\t\trepo_fmt.partial_clone;\n@@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt)\n \t\t\t\t    fmt->ref_storage_format);\n \tthe_repository->repository_format_worktree_config =\n \t\tfmt->worktree_config;\n+\tthe_repository->repository_format_relative_worktrees =\n+\t\tfmt->relative_worktrees;\n \tthe_repository->repository_format_partial_clone =\n \t\txstrdup_or_null(fmt->partial_clone);\n \tclear_repository_format(&repo_fmt);\ndiff --git a/setup.h b/setup.h\nindex e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644\n--- a/setup.h\n+++ b/setup.h\n@@ -129,6 +129,7 @@ struct repository_format {\n \tint precious_objects;\n \tchar *partial_clone; /* value of extensions.partialclone */\n \tint worktree_config;\n+\tint relative_worktrees;\n \tint is_bare;\n \tint hash_algo;\n \tint compat_hash_algo;\n\n-- \n2.47.0\n\n\n"},{"id":"506428","messageId":"20241031-wt_relative_options-v4-3-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 3/8] worktree: refactor infer_backlink return","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:32Z","receivedAt":"2024-11-01T04:38:43Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"The previous round[1] was merged a bit early before reviewer feedback\ncould be applied. This correctly indents a code block and updates the\n`infer_backlink` function to return `-1` on failure and strbuf.len on\nsuccess.\n\n[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n worktree.c | 17 +++++++++--------\n 1 file changed, 9 insertions(+), 8 deletions(-)\n\ndiff --git a/worktree.c b/worktree.c\nindex 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..9346d51c438cbd029e9e57591edd8c9f30cc7638 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id,\n \tstrbuf_strip_suffix(&worktree_path, \"/.git\");\n \n \tif (!is_absolute_path(worktree_path.buf)) {\n-\t    strbuf_strip_suffix(&path, \"gitdir\");\n-\t    strbuf_addbuf(&path, &worktree_path);\n-\t    strbuf_realpath_forgiving(&worktree_path, path.buf, 0);\n+\t\tstrbuf_strip_suffix(&path, \"gitdir\");\n+\t\tstrbuf_addbuf(&path, &worktree_path);\n+\t\tstrbuf_realpath_forgiving(&worktree_path, path.buf, 0);\n \t}\n \n \tCALLOC_ARRAY(worktree, 1);\n@@ -725,12 +725,15 @@ static int is_main_worktree_path(const char *path)\n  * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may\n  * be able to infer the gitdir by manually reading /path/to/worktree/.git,\n  * extracting the <id>, and checking if <repo>/worktrees/<id> exists.\n+ *\n+ * Returns -1 on failure and strbuf.len on success.\n  */\n-static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n+static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n {\n \tstruct strbuf actual = STRBUF_INIT;\n \tconst char *id;\n \n+\tstrbuf_reset(inferred);\n \tif (strbuf_read_file(&actual, gitfile, 0) < 0)\n \t\tgoto error;\n \tif (!starts_with(actual.buf, \"gitdir:\"))\n@@ -741,18 +744,16 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n \tid++; /* advance past '/' to point at <id> */\n \tif (!*id)\n \t\tgoto error;\n-\tstrbuf_reset(inferred);\n \tstrbuf_git_common_path(inferred, the_repository, \"worktrees/%s\", id);\n \tif (!is_directory(inferred->buf))\n \t\tgoto error;\n \n \tstrbuf_release(&actual);\n-\treturn 1;\n-\n+\treturn inferred->len;\n error:\n \tstrbuf_release(&actual);\n \tstrbuf_reset(inferred); /* clear invalid path */\n-\treturn 0;\n+\treturn -1;\n }\n \n /*\n\n-- \n2.47.0\n\n\n"},{"id":"506429","messageId":"20241031-wt_relative_options-v4-4-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 4/8] worktree: add `write_worktree_linking_files()` function","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:38Z","receivedAt":"2024-11-01T04:38:44Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"A new helper function, `write_worktree_linking_files()`, centralizes\nthe logic for computing and writing either relative or absolute\npaths, based on the provided configuration. This function accepts\n`strbuf` pointers to both the worktree’s `.git` link and the\nrepository’s `gitdir`, and then writes the appropriate path to each.\nThe `relativeWorktrees` extension is automatically set when a worktree\nis linked with relative paths.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n worktree.c | 36 ++++++++++++++++++++++++++++++++++++\n worktree.h | 14 ++++++++++++++\n 2 files changed, 50 insertions(+)\n\ndiff --git a/worktree.c b/worktree.c\nindex 9346d51c438cbd029e9e57591edd8c9f30cc7638..ab6f1e036fa019285a1a5152decb9aed5202909f 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -1032,3 +1032,39 @@ int init_worktree_config(struct repository *r)\n \tfree(main_worktree_file);\n \treturn res;\n }\n+\n+void write_worktree_linking_files(struct strbuf dotgit,\n+\t\t\t\t  struct strbuf gitdir,\n+\t\t\t\t  int use_relative_paths)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct strbuf repo = STRBUF_INIT;\n+\tstruct strbuf tmp = STRBUF_INIT;\n+\n+\tstrbuf_addbuf(&path, &dotgit);\n+\tstrbuf_strip_suffix(&path, \"/.git\");\n+\tstrbuf_realpath(&path, path.buf, 1);\n+\tstrbuf_addbuf(&repo, &gitdir);\n+\tstrbuf_strip_suffix(&repo, \"/gitdir\");\n+\tstrbuf_realpath(&repo, repo.buf, 1);\n+\n+\tif (use_relative_paths && !the_repository->repository_format_relative_worktrees) {\n+\t\tif (upgrade_repository_format(1) < 0)\n+\t\t\tdie(_(\"unable to upgrade repository format to support relative worktrees\"));\n+\t\tif (git_config_set_gently(\"extensions.relativeWorktrees\", \"true\"))\n+\t\t\tdie(_(\"unable to set extensions.relativeWorktrees setting\"));\n+\t\tthe_repository->repository_format_relative_worktrees = 1;\n+\t}\n+\n+\tif (use_relative_paths) {\n+\t\twrite_file(gitdir.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n+\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n+\t} else {\n+\t\twrite_file(gitdir.buf, \"%s/.git\", path.buf);\n+\t\twrite_file(dotgit.buf, \"gitdir: %s\", repo.buf);\n+\t}\n+\n+\tstrbuf_release(&path);\n+\tstrbuf_release(&repo);\n+\tstrbuf_release(&tmp);\n+}\ndiff --git a/worktree.h b/worktree.h\nindex e96118621638667d87c5d7e0452ed10bd1ddf606..4786aa545ca1fa12bb16a55ddf5a59c06503b2c5 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -215,4 +215,18 @@ void strbuf_worktree_ref(const struct worktree *wt,\n  */\n int init_worktree_config(struct repository *r);\n \n+/**\n+ * Write the .git file and gitdir file that links the worktree to the repository.\n+ *\n+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`\n+ * is the path to the repository's `gitdir` file.\n+ *\n+ * Example\n+ *  dotgit: \"/path/to/foo/.git\"\n+ *  gitdir: \"/path/to/repo/worktrees/foo/gitdir\"\n+ */\n+void write_worktree_linking_files(struct strbuf dotgit,\n+\t\t\t\t  struct strbuf gitdir,\n+\t\t\t\t  int use_relative_paths);\n+\n #endif\n\n-- \n2.47.0\n\n\n"},{"id":"506430","messageId":"20241031-wt_relative_options-v4-6-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 6/8] worktree: add relative cli/config options to `move` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:48Z","receivedAt":"2024-11-01T04:38:55Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This teaches the `worktree move` command to respect the\n`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\nconfig setting. If an existing worktree is moved with `--relative-paths`\nthe new path will be relative (and visa-versa).\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n builtin/worktree.c       |  4 +++-\n t/t2403-worktree-move.sh | 22 ++++++++++++++++++++++\n worktree.c               | 23 ++++++++++-------------\n worktree.h               |  3 ++-\n 4 files changed, 37 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n \t\tOPT__FORCE(&force,\n \t\t\t N_(\"force move even if worktree is dirty or locked\"),\n \t\t\t PARSE_OPT_NOCOMPLETE),\n+\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tstruct worktree **worktrees, *wt;\n@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n \tif (rename(wt->path, dst.buf) == -1)\n \t\tdie_errno(_(\"failed to move '%s' to '%s'\"), wt->path, dst.buf);\n \n-\tupdate_worktree_location(wt, dst.buf);\n+\tupdate_worktree_location(wt, dst.buf, use_relative_paths);\n \n \tstrbuf_release(&dst);\n \tfree_worktrees(worktrees);\ndiff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh\nindex 901342ea09b51a8e832f1109fbb737df84283aa2..6ce9ed3f1e6b3f73d2a290e770233eec30221fe5 100755\n--- a/t/t2403-worktree-move.sh\n+++ b/t/t2403-worktree-move.sh\n@@ -247,4 +247,26 @@ test_expect_success 'not remove a repo with initialized submodule' '\n \t)\n '\n \n+test_expect_success 'move worktree with absolute path to relative path' '\n+\tgit config worktree.useRelativePaths false &&\n+\tgit worktree add ./absolute &&\n+\tgit worktree move --relative-paths absolute relative &&\n+\tcat relative/.git >actual &&\n+\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect actual &&\n+\tgit config worktree.useRelativePaths true &&\n+\tgit worktree move relative relative2 &&\n+\tcat relative2/.git >actual &&\n+\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'move worktree with relative path to absolute path' '\n+\tgit config worktree.useRelativePaths true &&\n+\tgit worktree move --no-relative-paths relative2 absolute &&\n+\tcat absolute/.git >actual &&\n+\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\ndiff --git a/worktree.c b/worktree.c\nindex ab6f1e036fa019285a1a5152decb9aed5202909f..6b640cd9549ecb060236f7eddf1390caa181f1a0 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -376,32 +376,29 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,\n \treturn ret;\n }\n \n-void update_worktree_location(struct worktree *wt, const char *path_)\n+void update_worktree_location(struct worktree *wt,\n+\t\t\t      const char *path_,\n+\t\t\t      int use_relative_paths)\n {\n \tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf repo = STRBUF_INIT;\n-\tstruct strbuf file = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n+\tstruct strbuf dotgit = STRBUF_INIT;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n \n \tif (is_main_worktree(wt))\n \t\tBUG(\"can't relocate main worktree\");\n \n-\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n+\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n \tstrbuf_realpath(&path, path_, 1);\n+\tstrbuf_addf(&dotgit, \"%s/.git\", path.buf);\n \tif (fspathcmp(wt->path, path.buf)) {\n-\t\tstrbuf_addf(&file, \"%s/gitdir\", repo.buf);\n-\t\twrite_file(file.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n-\t\tstrbuf_reset(&file);\n-\t\tstrbuf_addf(&file, \"%s/.git\", path.buf);\n-\t\twrite_file(file.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \n \t\tfree(wt->path);\n \t\twt->path = strbuf_detach(&path, NULL);\n \t}\n \tstrbuf_release(&path);\n-\tstrbuf_release(&repo);\n-\tstrbuf_release(&file);\n-\tstrbuf_release(&tmp);\n+\tstrbuf_release(&dotgit);\n+\tstrbuf_release(&gitdir);\n }\n \n int is_worktree_being_rebased(const struct worktree *wt,\ndiff --git a/worktree.h b/worktree.h\nindex 4786aa545ca1fa12bb16a55ddf5a59c06503b2c5..6dd5dfc9b71870320c96aae35a7a983b5b9ba9a7 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -118,7 +118,8 @@ int validate_worktree(const struct worktree *wt,\n  * Update worktrees/xxx/gitdir with the new path.\n  */\n void update_worktree_location(struct worktree *wt,\n-\t\t\t      const char *path_);\n+\t\t\t      const char *path_,\n+\t\t\t      int use_relative_paths);\n \n typedef void (* worktree_repair_fn)(int iserr, const char *path,\n \t\t\t\t    const char *msg, void *cb_data);\n\n-- \n2.47.0\n\n\n"},{"id":"506431","messageId":"20241031-wt_relative_options-v4-5-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 5/8] worktree: add relative cli/config options to `add` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:43Z","receivedAt":"2024-11-01T04:38:57Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This introduces the `--[no-]relative-paths` CLI option and\n`worktree.useRelativePaths` configuration setting to the `worktree add`\ncommand. When enabled these options allow worktrees to be linked using\nrelative paths, enhancing portability across environments where absolute\npaths may differ (e.g., containerized setups, shared network drives).\nGit still creates absolute paths by default, but these options allow\nusers to opt-in to relative paths if desired.\n\nThe t2408 test file is removed and more comprehensive tests are\nwritten for the various worktree operations in their own files.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/config/worktree.txt | 10 ++++++++\n Documentation/git-worktree.txt    |  4 +++\n builtin/worktree.c                | 19 +++++++-------\n t/t2400-worktree-add.sh           | 54 +++++++++++++++++++++++++++++++++++++++\n t/t2401-worktree-prune.sh         |  3 ++-\n t/t2402-worktree-list.sh          | 22 ++++++++++++++++\n t/t2408-worktree-relative.sh      | 39 ----------------------------\n 7 files changed, 102 insertions(+), 49 deletions(-)\n\ndiff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt\nindex 048e349482df6c892055720eb53cdcd6c327b6ed..5e35c7d018aecdedca0642b11e45df6d19024d42 100644\n--- a/Documentation/config/worktree.txt\n+++ b/Documentation/config/worktree.txt\n@@ -7,3 +7,13 @@ worktree.guessRemote::\n \tsuch a branch exists, it is checked out and set as \"upstream\"\n \tfor the new branch.  If no such match can be found, it falls\n \tback to creating a new branch from the current HEAD.\n+\n+worktree.useRelativePaths::\n+\tLink worktrees using relative paths (when \"true\") or absolute\n+\tpaths (when \"false\"). This is particularly useful for setups\n+\twhere the repository and worktrees may be moved between\n+\tdifferent locations or environments. Defaults to \"false\".\n++\n+Note that setting `worktree.useRelativePaths` to \"true\" implies enabling the\n+`extension.relativeWorktrees` config (see linkgit:git-config[1]),\n+thus making it incompatible with older versions of Git.\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 70437c815f13852bd2eb862176b8b933e6de0acf..88d2261012166a929b7f167d79720e4d965fd71b 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -216,6 +216,10 @@ To remove a locked worktree, specify `--force` twice.\n This can also be set up as the default behaviour by using the\n `worktree.guessRemote` config option.\n \n+--[no-]relative-paths::\n+\tOverrides the `worktree.useRelativePaths` config option, see\n+\tlinkgit:git-config[1].\n+\n --[no-]track::\n \tWhen creating a new branch, if `<commit-ish>` is a branch,\n \tmark it as \"upstream\" from the new branch.  This is the\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -120,12 +120,14 @@ struct add_opts {\n \tint quiet;\n \tint checkout;\n \tint orphan;\n+\tint relative_paths;\n \tconst char *keep_locked;\n };\n \n static int show_only;\n static int verbose;\n static int guess_remote;\n+static int use_relative_paths;\n static timestamp_t expire;\n \n static int git_worktree_config(const char *var, const char *value,\n@@ -134,6 +136,9 @@ static int git_worktree_config(const char *var, const char *value,\n \tif (!strcmp(var, \"worktree.guessremote\")) {\n \t\tguess_remote = git_config_bool(var, value);\n \t\treturn 0;\n+\t} else if (!strcmp(var, \"worktree.userelativepaths\")) {\n+\t\tuse_relative_paths = git_config_bool(var, value);\n+\t\treturn 0;\n \t}\n \n \treturn git_default_config(var, value, ctx, cb);\n@@ -414,8 +419,7 @@ static int add_worktree(const char *path, const char *refname,\n \t\t\tconst struct add_opts *opts)\n {\n \tstruct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;\n-\tstruct strbuf sb = STRBUF_INIT, sb_tmp = STRBUF_INIT;\n-\tstruct strbuf sb_path_realpath = STRBUF_INIT, sb_repo_realpath = STRBUF_INIT;\n+\tstruct strbuf sb = STRBUF_INIT;\n \tconst char *name;\n \tstruct strvec child_env = STRVEC_INIT;\n \tunsigned int counter = 0;\n@@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname,\n \n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"%s/gitdir\", sb_repo.buf);\n-\tstrbuf_realpath(&sb_path_realpath, path, 1);\n-\tstrbuf_realpath(&sb_repo_realpath, sb_repo.buf, 1);\n-\twrite_file(sb.buf, \"%s/.git\", relative_path(sb_path_realpath.buf, sb_repo_realpath.buf, &sb_tmp));\n-\twrite_file(sb_git.buf, \"gitdir: %s\", relative_path(sb_repo_realpath.buf, sb_path_realpath.buf, &sb_tmp));\n+\twrite_worktree_linking_files(sb_git, sb, opts->relative_paths);\n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"%s/commondir\", sb_repo.buf);\n \twrite_file(sb.buf, \"../..\");\n@@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname,\n \n \tstrvec_clear(&child_env);\n \tstrbuf_release(&sb);\n-\tstrbuf_release(&sb_tmp);\n \tstrbuf_release(&symref);\n \tstrbuf_release(&sb_repo);\n-\tstrbuf_release(&sb_repo_realpath);\n \tstrbuf_release(&sb_git);\n-\tstrbuf_release(&sb_path_realpath);\n \tstrbuf_release(&sb_name);\n \tfree_worktree(wt);\n \treturn ret;\n@@ -796,12 +794,15 @@ static int add(int ac, const char **av, const char *prefix)\n \t\t\t     PARSE_OPT_NOARG | PARSE_OPT_OPTARG),\n \t\tOPT_BOOL(0, \"guess-remote\", &guess_remote,\n \t\t\t N_(\"try to match the new branch name with a remote-tracking branch\")),\n+\t\tOPT_BOOL(0, \"relative-paths\", &opts.relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tint ret;\n \n \tmemset(&opts, 0, sizeof(opts));\n \topts.checkout = 1;\n+\topts.relative_paths = use_relative_paths;\n \tac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);\n \tif (!!opts.detach + !!new_branch + !!new_branch_force > 1)\n \t\tdie(_(\"options '%s', '%s', and '%s' cannot be used together\"), \"-b\", \"-B\", \"--detach\");\ndiff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\nindex cfc4aeb1798c6d023909cec771e5b74e983af5ea..d36d8a4db0e924877787697579544f10f92dc0cf 100755\n--- a/t/t2400-worktree-add.sh\n+++ b/t/t2400-worktree-add.sh\n@@ -1207,4 +1207,58 @@ test_expect_success '\"add\" with initialized submodule, with submodule.recurse se\n \tgit -C project-clone -c submodule.recurse worktree add ../project-5\n '\n \n+test_expect_success 'can create worktrees with relative paths' '\n+\ttest_when_finished \"git worktree remove relative\" &&\n+\tgit config worktree.useRelativePaths false &&\n+\tgit worktree add --relative-paths ./relative &&\n+\tcat relative/.git >actual &&\n+\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n+\ttest_cmp expect actual &&\n+\tcat .git/worktrees/relative/gitdir >actual &&\n+\techo \"../../../relative/.git\" >expect &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_expect_success 'can create worktrees with absolute paths' '\n+\tgit config worktree.useRelativePaths true &&\n+\tgit worktree add ./relative &&\n+\tcat relative/.git >actual &&\n+\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n+\ttest_cmp expect actual &&\n+\tgit worktree add --no-relative-paths ./absolute &&\n+\tcat absolute/.git >actual &&\n+\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'move repo without breaking relative internal links' '\n+\ttest_when_finished rm -rf repo moved &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit config worktree.useRelativePaths true &&\n+\t\ttest_commit initial &&\n+\t\tgit worktree add wt1 &&\n+\t\tcd .. &&\n+\t\tmv repo moved &&\n+\t\tcd moved/wt1 &&\n+\t\tgit status >out 2>err &&\n+\t\ttest_must_be_empty err\n+\t)\n+'\n+\n+test_expect_success 'relative worktree sets extension config' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\tgit -C repo commit --allow-empty -m base &&\n+\tgit -C repo worktree add --relative-paths ./foo &&\n+\tgit -C repo config get core.repositoryformatversion >actual &&\n+\techo 1 >expected &&\n+\ttest_cmp expected actual &&\n+\tgit -C repo config get extensions.relativeworktrees >actual &&\n+\techo true >expected &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\ndiff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh\nindex 976d048e3efc74be9cd909ce76d552b3944d2e10..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755\n--- a/t/t2401-worktree-prune.sh\n+++ b/t/t2401-worktree-prune.sh\n@@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' '\n \t! test -d .git/worktrees/wt\n '\n \n-test_expect_success 'not prune proper worktrees when run inside linked worktree' '\n+test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '\n \ttest_when_finished rm -rf repo wt_ext &&\n \tgit init repo &&\n \t(\n \t    cd repo &&\n+\t    git config worktree.useRelativePaths true &&\n \t    echo content >file &&\n \t    git add file &&\n \t    git commit -m msg &&\ndiff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh\nindex 33ea9cb21ba07c9563530b54da06753eaa993fe2..780daa6cd6351f8fa9434619cc212aade8f01420 100755\n--- a/t/t2402-worktree-list.sh\n+++ b/t/t2402-worktree-list.sh\n@@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' '\n '\n \n test_expect_success 'linked worktrees are sorted' '\n+\ttest_when_finished \"rm -rf sorted\" &&\n \tmkdir sorted &&\n \tgit init sorted/main &&\n \t(\n@@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' '\n \ttest_cmp expected sorted/main/actual\n '\n \n+test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '\n+\ttest_when_finished \"rm -rf sorted\" &&\n+\tmkdir sorted &&\n+\tgit init sorted/main &&\n+\t(\n+\t\tcd sorted/main &&\n+\t\ttest_tick &&\n+\t\ttest_commit new &&\n+\t\tgit worktree add --relative-paths ../first &&\n+\t\tgit worktree add ../second &&\n+\t\tgit worktree list --porcelain >out &&\n+\t\tgrep ^worktree out >actual\n+\t) &&\n+\tcat >expected <<-EOF &&\n+\tworktree $(pwd)/sorted/main\n+\tworktree $(pwd)/sorted/first\n+\tworktree $(pwd)/sorted/second\n+\tEOF\n+\ttest_cmp expected sorted/main/actual\n+'\n+\n test_expect_success 'worktree path when called in .git directory' '\n \tgit worktree list >list1 &&\n \tgit -C .git worktree list >list2 &&\ndiff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh\ndeleted file mode 100755\nindex a3136db7e28cb20926ff44211e246ce625a6e51a..0000000000000000000000000000000000000000\n--- a/t/t2408-worktree-relative.sh\n+++ /dev/null\n@@ -1,39 +0,0 @@\n-#!/bin/sh\n-\n-test_description='test worktrees linked with relative paths'\n-\n-TEST_PASSES_SANITIZE_LEAK=true\n-. ./test-lib.sh\n-\n-test_expect_success 'links worktrees with relative paths' '\n-\ttest_when_finished rm -rf repo &&\n-\tgit init repo &&\n-\t(\n-\t\tcd repo &&\n-\t\ttest_commit initial &&\n-\t\tgit worktree add wt1 &&\n-\t\techo \"../../../wt1/.git\" >expected_gitdir &&\n-\t\tcat .git/worktrees/wt1/gitdir >actual_gitdir &&\n-\t\techo \"gitdir: ../.git/worktrees/wt1\" >expected_git &&\n-\t\tcat wt1/.git >actual_git &&\n-\t\ttest_cmp expected_gitdir actual_gitdir &&\n-\t\ttest_cmp expected_git actual_git\n-\t)\n-'\n-\n-test_expect_success 'move repo without breaking relative internal links' '\n-\ttest_when_finished rm -rf repo moved &&\n-\tgit init repo &&\n-\t(\n-\t\tcd repo &&\n-\t\ttest_commit initial &&\n-\t\tgit worktree add wt1 &&\n-\t\tcd .. &&\n-\t\tmv repo moved &&\n-\t\tcd moved/wt1 &&\n-\t\tgit status >out 2>err &&\n-\t\ttest_must_be_empty err\n-\t)\n-'\n-\n-test_done\n\n-- \n2.47.0\n\n\n"},{"id":"506432","messageId":"20241031-wt_relative_options-v4-7-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 7/8] worktree: add relative cli/config options to `repair` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:38:54Z","receivedAt":"2024-11-01T04:39:02Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This teaches the `worktree repair` command to respect the\n`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\nconfig setting. If an existing worktree with an absolute path is repaired\nwith `--relative-paths`, the links will be replaced with relative paths,\neven if the original path was correct. This allows a user to covert\nexisting worktrees between absolute/relative as desired.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/git-worktree.txt |  3 +++\n builtin/worktree.c             |  6 +++--\n t/t2406-worktree-repair.sh     | 26 +++++++++++++++++++\n worktree.c                     | 58 +++++++++++++++++++++---------------------\n worktree.h                     |  5 ++--\n 5 files changed, 65 insertions(+), 33 deletions(-)\n\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 88d2261012166a929b7f167d79720e4d965fd71b..1d86323def16b15fbc85365aaea18ce6a2ef433a 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -219,6 +219,9 @@ This can also be set up as the default behaviour by using the\n --[no-]relative-paths::\n \tOverrides the `worktree.useRelativePaths` config option, see\n \tlinkgit:git-config[1].\n++\n+With `repair`, the linking files will be updated if there's an absolute/relative\n+mismatch, even if the links are correct.\n \n --[no-]track::\n \tWhen creating a new branch, if `<commit-ish>` is a branch,\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 302151506981718658db1cd338cd9064688f5c14..fde9ff4dc9a734c655e95ccd62774282950cbba6 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -1385,6 +1385,8 @@ static int repair(int ac, const char **av, const char *prefix)\n \tconst char **p;\n \tconst char *self[] = { \".\", NULL };\n \tstruct option options[] = {\n+\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tint rc = 0;\n@@ -1392,8 +1394,8 @@ static int repair(int ac, const char **av, const char *prefix)\n \tac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);\n \tp = ac > 0 ? av : self;\n \tfor (; *p; p++)\n-\t\trepair_worktree_at_path(*p, report_repair, &rc);\n-\trepair_worktrees(report_repair, &rc);\n+\t\trepair_worktree_at_path(*p, report_repair, &rc, use_relative_paths);\n+\trepair_worktrees(report_repair, &rc, use_relative_paths);\n \treturn rc;\n }\n \ndiff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh\nindex 7686e60f6ad186519b275f11a5e14064c905b207..84451e903b2ef3c645c0311faf055c846588baf6 100755\n--- a/t/t2406-worktree-repair.sh\n+++ b/t/t2406-worktree-repair.sh\n@@ -216,4 +216,30 @@ test_expect_success 'repair copied main and linked worktrees' '\n \ttest_cmp dup/linked.expect dup/linked/.git\n '\n \n+test_expect_success 'repair absolute worktree to use relative paths' '\n+\ttest_when_finished \"rm -rf main side sidemoved\" &&\n+\ttest_create_repo main &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --detach ../side &&\n+\techo \"../../../../sidemoved/.git\" >expect-gitdir &&\n+\techo \"gitdir: ../main/.git/worktrees/side\" >expect-gitfile &&\n+\tmv side sidemoved &&\n+\tgit -C main worktree repair --relative-paths ../sidemoved &&\n+\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n+\ttest_cmp expect-gitfile sidemoved/.git\n+'\n+\n+test_expect_success 'repair relative worktree to use absolute paths' '\n+\ttest_when_finished \"rm -rf main side sidemoved\" &&\n+\ttest_create_repo main &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --relative-paths --detach ../side &&\n+\techo \"$(pwd)/sidemoved/.git\" >expect-gitdir &&\n+\techo \"gitdir: $(pwd)/main/.git/worktrees/side\" >expect-gitfile &&\n+\tmv side sidemoved &&\n+\tgit -C main worktree repair ../sidemoved &&\n+\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n+\ttest_cmp expect-gitfile sidemoved/.git\n+'\n+\n test_done\ndiff --git a/worktree.c b/worktree.c\nindex 6b640cd9549ecb060236f7eddf1390caa181f1a0..2cb994ac462debf966ac51b5a4f33c30cfebd4ef 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -574,12 +574,14 @@ int other_head_refs(each_ref_fn fn, void *cb_data)\n  * pointing at <repo>/worktrees/<id>.\n  */\n static void repair_gitfile(struct worktree *wt,\n-\t\t\t   worktree_repair_fn fn, void *cb_data)\n+\t\t\t   worktree_repair_fn fn,\n+\t\t\t   void *cb_data,\n+\t\t\t   int use_relative_paths)\n {\n \tstruct strbuf dotgit = STRBUF_INIT;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf repo = STRBUF_INIT;\n \tstruct strbuf backlink = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n \tchar *dotgit_contents = NULL;\n \tconst char *repair = NULL;\n \tint err;\n@@ -595,6 +597,7 @@ static void repair_gitfile(struct worktree *wt,\n \n \tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n \tstrbuf_addf(&dotgit, \"%s/.git\", wt->path);\n+\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n \tdotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));\n \n \tif (dotgit_contents) {\n@@ -612,18 +615,20 @@ static void repair_gitfile(struct worktree *wt,\n \t\trepair = _(\".git file broken\");\n \telse if (fspathcmp(backlink.buf, repo.buf))\n \t\trepair = _(\".git file incorrect\");\n+\telse if (use_relative_paths == is_absolute_path(dotgit_contents))\n+\t\trepair = _(\".git file absolute/relative path mismatch\");\n \n \tif (repair) {\n \t\tfn(0, wt->path, repair, cb_data);\n-\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, wt->path, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \t}\n \n done:\n \tfree(dotgit_contents);\n \tstrbuf_release(&repo);\n \tstrbuf_release(&dotgit);\n+\tstrbuf_release(&gitdir);\n \tstrbuf_release(&backlink);\n-\tstrbuf_release(&tmp);\n }\n \n static void repair_noop(int iserr UNUSED,\n@@ -634,7 +639,7 @@ static void repair_noop(int iserr UNUSED,\n \t/* nothing */\n }\n \n-void repair_worktrees(worktree_repair_fn fn, void *cb_data)\n+void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths)\n {\n \tstruct worktree **worktrees = get_worktrees_internal(1);\n \tstruct worktree **wt = worktrees + 1; /* +1 skips main worktree */\n@@ -642,7 +647,7 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)\n \tif (!fn)\n \t\tfn = repair_noop;\n \tfor (; *wt; wt++)\n-\t\trepair_gitfile(*wt, fn, cb_data);\n+\t\trepair_gitfile(*wt, fn, cb_data, use_relative_paths);\n \tfree_worktrees(worktrees);\n }\n \n@@ -758,16 +763,15 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n  * the worktree's path.\n  */\n void repair_worktree_at_path(const char *path,\n-\t\t\t     worktree_repair_fn fn, void *cb_data)\n+\t\t\t     worktree_repair_fn fn,\n+\t\t\t     void *cb_data,\n+\t\t\t     int use_relative_paths)\n {\n \tstruct strbuf dotgit = STRBUF_INIT;\n-\tstruct strbuf realdotgit = STRBUF_INIT;\n \tstruct strbuf backlink = STRBUF_INIT;\n \tstruct strbuf inferred_backlink = STRBUF_INIT;\n \tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf olddotgit = STRBUF_INIT;\n-\tstruct strbuf realolddotgit = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n \tchar *dotgit_contents = NULL;\n \tconst char *repair = NULL;\n \tint err;\n@@ -779,25 +783,25 @@ void repair_worktree_at_path(const char *path,\n \t\tgoto done;\n \n \tstrbuf_addf(&dotgit, \"%s/.git\", path);\n-\tif (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {\n+\tif (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {\n \t\tfn(1, path, _(\"not a valid path\"), cb_data);\n \t\tgoto done;\n \t}\n \n-\tinfer_backlink(realdotgit.buf, &inferred_backlink);\n+\tinfer_backlink(dotgit.buf, &inferred_backlink);\n \tstrbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);\n-\tdotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));\n+\tdotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));\n \tif (dotgit_contents) {\n \t\tif (is_absolute_path(dotgit_contents)) {\n \t\t\tstrbuf_addstr(&backlink, dotgit_contents);\n \t\t} else {\n-\t\t\tstrbuf_addbuf(&backlink, &realdotgit);\n+\t\t\tstrbuf_addbuf(&backlink, &dotgit);\n \t\t\tstrbuf_strip_suffix(&backlink, \".git\");\n \t\t\tstrbuf_addstr(&backlink, dotgit_contents);\n \t\t\tstrbuf_realpath_forgiving(&backlink, backlink.buf, 0);\n \t\t}\n \t} else if (err == READ_GITFILE_ERR_NOT_A_FILE) {\n-\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git is not a file\"), cb_data);\n+\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git is not a file\"), cb_data);\n \t\tgoto done;\n \t} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {\n \t\tif (inferred_backlink.len) {\n@@ -810,11 +814,11 @@ void repair_worktree_at_path(const char *path,\n \t\t\t */\n \t\t\tstrbuf_swap(&backlink, &inferred_backlink);\n \t\t} else {\n-\t\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git file does not reference a repository\"), cb_data);\n+\t\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git file does not reference a repository\"), cb_data);\n \t\t\tgoto done;\n \t\t}\n \t} else {\n-\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git file broken\"), cb_data);\n+\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git file broken\"), cb_data);\n \t\tgoto done;\n \t}\n \n@@ -836,39 +840,35 @@ void repair_worktree_at_path(const char *path,\n \t * in the \"copy\" repository. In this case, point the \"copy\" worktree's\n \t * .git file at the \"copy\" repository.\n \t */\n-\tif (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) {\n+\tif (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))\n \t\tstrbuf_swap(&backlink, &inferred_backlink);\n-\t}\n \n \tstrbuf_addf(&gitdir, \"%s/gitdir\", backlink.buf);\n \tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n \t\trepair = _(\"gitdir unreadable\");\n+\telse if (use_relative_paths == is_absolute_path(olddotgit.buf))\n+\t\trepair = _(\"gitdir absolute/relative path mismatch\");\n \telse {\n \t\tstrbuf_rtrim(&olddotgit);\n-\t\tif (is_absolute_path(olddotgit.buf)) {\n-\t\t\tstrbuf_addbuf(&realolddotgit, &olddotgit);\n-\t\t} else {\n-\t\t\tstrbuf_addf(&realolddotgit, \"%s/%s\", backlink.buf, olddotgit.buf);\n-\t\t\tstrbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0);\n+\t\tif (!is_absolute_path(olddotgit.buf)) {\n+\t\t\tstrbuf_insertf(&olddotgit, 0, \"%s/\", backlink.buf);\n+\t\t\tstrbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0);\n \t\t}\n-\t\tif (fspathcmp(realolddotgit.buf, realdotgit.buf))\n+\t\tif (fspathcmp(olddotgit.buf, dotgit.buf))\n \t\t\trepair = _(\"gitdir incorrect\");\n \t}\n \n \tif (repair) {\n \t\tfn(0, gitdir.buf, repair, cb_data);\n-\t\twrite_file(gitdir.buf, \"%s\", relative_path(realdotgit.buf, backlink.buf, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \t}\n done:\n \tfree(dotgit_contents);\n \tstrbuf_release(&olddotgit);\n-\tstrbuf_release(&realolddotgit);\n \tstrbuf_release(&backlink);\n \tstrbuf_release(&inferred_backlink);\n \tstrbuf_release(&gitdir);\n-\tstrbuf_release(&realdotgit);\n \tstrbuf_release(&dotgit);\n-\tstrbuf_release(&tmp);\n }\n \n int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)\ndiff --git a/worktree.h b/worktree.h\nindex 6dd5dfc9b71870320c96aae35a7a983b5b9ba9a7..f158a2ee429cc7af971bf4a6665c058a9e15614a 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -130,7 +130,7 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,\n  * function, if non-NULL, is called with the path of the worktree and a\n  * description of the repair or error, along with the callback user-data.\n  */\n-void repair_worktrees(worktree_repair_fn, void *cb_data);\n+void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths);\n \n /*\n  * Repair the linked worktrees after the gitdir has been moved.\n@@ -152,7 +152,8 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path\n  * worktree and a description of the repair or error, along with the callback\n  * user-data.\n  */\n-void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);\n+void repair_worktree_at_path(const char *, worktree_repair_fn,\n+\t\t\t     void *cb_data, int use_relative_paths);\n \n /*\n  * Free up the memory for a worktree.\n\n-- \n2.47.0\n\n\n"},{"id":"506433","messageId":"20241031-wt_relative_options-v4-8-07a3dc0f02a3@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v4 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T04:39:01Z","receivedAt":"2024-11-01T04:39:09Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This refactors `repair_worktree_after_gitdir_move()` to use the new\n`write_worktree_linking_files` function. It also preserves the\nrelativity of the linking files; e.g., if an existing worktree used\nabsolute paths then the repaired paths will be absolute (and visa-versa).\n\nThis also adds a test case for reinitializing a repository that has\nrelative worktrees.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n t/t0001-init.sh | 22 ++++++++++++++++++----\n worktree.c      | 29 ++++++++---------------------\n 2 files changed, 26 insertions(+), 25 deletions(-)\n\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..264951592a711f6879555b785905e799db6c5dd4 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n sep_git_dir_worktree ()  {\n \ttest_when_finished \"rm -rf mainwt linkwt seprepo\" &&\n \tgit init mainwt &&\n+\tif test \"relative\" = $2\n+\tthen\n+\t\tgit -C mainwt config worktree.useRelativePaths true\n+\telse\n+\t\tgit -C mainwt config worktree.useRelativePaths false\n+\tfi\n \ttest_commit -C mainwt gumby &&\n \tgit -C mainwt worktree add --detach ../linkwt &&\n \tgit -C \"$1\" init --separate-git-dir ../seprepo &&\n@@ -442,12 +448,20 @@ sep_git_dir_worktree ()  {\n \ttest_cmp expect actual\n }\n \n-test_expect_success 're-init to move gitdir with linked worktrees' '\n-\tsep_git_dir_worktree mainwt\n+test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '\n+\tsep_git_dir_worktree mainwt absolute\n+'\n+\n+test_expect_success 're-init to move gitdir within linked worktree (absolute)' '\n+\tsep_git_dir_worktree linkwt absolute\n+'\n+\n+test_expect_success 're-init to move gitdir with linked worktrees (relative)' '\n+\tsep_git_dir_worktree mainwt relative\n '\n \n-test_expect_success 're-init to move gitdir within linked worktree' '\n-\tsep_git_dir_worktree linkwt\n+test_expect_success 're-init to move gitdir within linked worktree (relative)' '\n+\tsep_git_dir_worktree linkwt relative\n '\n \n test_expect_success MINGW '.git hidden' '\ndiff --git a/worktree.c b/worktree.c\nindex 2cb994ac462debf966ac51b5a4f33c30cfebd4ef..2c2e9fd8fdc22d4fe3705227d72b8dbd293038b2 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -653,45 +653,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat\n \n void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)\n {\n-\tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf repo = STRBUF_INIT;\n \tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf dotgit = STRBUF_INIT;\n-\tstruct strbuf olddotgit = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n+\tint is_relative_path;\n \n \tif (is_main_worktree(wt))\n \t\tgoto done;\n \n-\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n-\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n+\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n \n-\tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n+\tif (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)\n \t\tgoto done;\n \n-\tstrbuf_rtrim(&olddotgit);\n-\tif (is_absolute_path(olddotgit.buf)) {\n-\t\tstrbuf_addbuf(&dotgit, &olddotgit);\n-\t} else {\n-\t\tstrbuf_addf(&dotgit, \"%s/worktrees/%s/%s\", old_path, wt->id, olddotgit.buf);\n+\tstrbuf_rtrim(&dotgit);\n+\tis_relative_path = ! is_absolute_path(dotgit.buf);\n+\tif (is_relative_path) {\n+\t\tstrbuf_insertf(&dotgit, 0, \"%s/worktrees/%s/\", old_path, wt->id);\n \t\tstrbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);\n \t}\n \n \tif (!file_exists(dotgit.buf))\n \t\tgoto done;\n \n-\tstrbuf_addbuf(&path, &dotgit);\n-\tstrbuf_strip_suffix(&path, \"/.git\");\n-\n-\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n-\twrite_file(gitdir.buf, \"%s\", relative_path(dotgit.buf, repo.buf, &tmp));\n+\twrite_worktree_linking_files(dotgit, gitdir, is_relative_path);\n done:\n-\tstrbuf_release(&path);\n-\tstrbuf_release(&repo);\n \tstrbuf_release(&gitdir);\n \tstrbuf_release(&dotgit);\n-\tstrbuf_release(&olddotgit);\n-\tstrbuf_release(&tmp);\n }\n \n void repair_worktrees_after_gitdir_move(const char *old_path)\n\n-- \n2.47.0\n\n\n"},{"id":"506436","messageId":"xmqqzfmj2zp9.fsf@gitster.g","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-11-01T07:14:10Z","receivedAt":"2024-11-01T07:14:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Caleb White <cdwhite3@pm.me> writes:\n\n> The base for this patch series is obtained by applying the following\n> patch onto 6a11438f43:\n> - cw/config-extensions topic (doc: consolidate extensions in git-config\n>   documentation, 2024-10-22, <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>)\n\nI am slowing getting back to speed, but with the above I noticed two\nthings.\n\n - Do not encourage people to _apply_ random patches before your\n   series when the official tree has the same patches.  In this\n   case, you even know the branch the official tree uses\n   (i.e. cw/config-extensions), so tell readers to _merge_ the\n   topic before applying your series instead.\n\n - The notes/amlog I snarfed from the broken-out repository of\n   Taylor does not seem to record the message ID\n   <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me> Perhaps\n   I was looking at a wrong repository?\n\nThanks.\n"},{"id":"506443","messageId":"D5AVE5JXJ2AE.29R7FKWCHWIKQ@pm.me","threadId":"62433","inReplyTo":"xmqqzfmj2zp9.fsf@gitster.g","subject":"Re: [PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-01T13:18:28Z","receivedAt":"2024-11-01T13:18:40Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Fri Nov 1, 2024 at 2:14 AM CDT, Junio C Hamano wrote:\n> Caleb White <cdwhite3@pm.me> writes:\n>\n>> The base for this patch series is obtained by applying the following\n>> patch onto 6a11438f43:\n>> - cw/config-extensions topic (doc: consolidate extensions in git-config\n>>   documentation, 2024-10-22, <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>)\n>\n> I am slowing getting back to speed, but with the above I noticed two\n> things.\n\nWelcome back! I hope you had a good break :).\n\n>  - Do not encourage people to _apply_ random patches before your\n>    series when the official tree has the same patches.  In this\n>    case, you even know the branch the official tree uses\n>    (i.e. cw/config-extensions), so tell readers to _merge_ the\n>    topic before applying your series instead.\n\nAh, my apologies. I will make sure to do that in the future. When\nI first added this note, a topic branch[1] had not been created yet and\nI forgot to update it when it was.\n\n>  - The notes/amlog I snarfed from the broken-out repository of\n>    Taylor does not seem to record the message ID\n>    <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me> Perhaps\n>    I was looking at a wrong repository?\n\nHmm, I'm not sure. Here's the latest update in the What's Cooking report:\n\n    * cw/config-extensions (2024-10-22) 1 commit\n     (merged to 'next' on 2024-10-30 at 875fa0b619)\n     + doc: consolidate extensions in git-config documentation\n     (this branch is used by cw/worktree-extension.)\n\n     Centralize documentation for repository extensions into a single place.\n\n     Will merge to 'master'?\n     source: <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>\n\nBest,\n\n[1]: https://github.com/ttaylorr/git/tree/cw/config-extensions\n\n"},{"id":"506478","messageId":"xmqqh68qymkc.fsf@gitster.g","threadId":"62433","inReplyTo":"D5AVE5JXJ2AE.29R7FKWCHWIKQ@pm.me","subject":"Re: [PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-11-02T10:09:07Z","receivedAt":"2024-11-02T10:09:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Caleb White <cdwhite3@pm.me> writes:\n\n> Ah, my apologies. I will make sure to do that in the future. When\n> I first added this note, a topic branch[1] had not been created yet and\n> I forgot to update it when it was.\n\nI see.  I think Taylor has done a great job maintaining these\ntopics, so I'll see how well they have been reviewed during the past\nfew weeks.\n\nThanks.\n"},{"id":"506484","messageId":"3249fe48-f777-4aa9-9ee0-2c84272ed278@app.fastmail.com","threadId":"62433","inReplyTo":"xmqqzfmj2zp9.fsf@gitster.g","subject":"Re: [PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2024-11-02T14:36:09Z","receivedAt":"2024-11-02T14:37:14Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Fri, Nov 1, 2024, at 08:14, Junio C Hamano wrote:\n>  - The notes/amlog I snarfed from the broken-out repository of\n>    Taylor does not seem to record the message ID\n>    <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me> Perhaps\n>    I was looking at a wrong repository?\n>\n> Thanks.\n\nYou can find that amlog on https://github.com/git/git/\n\n-- \nKristoffer Haugsbakk\n"},{"id":"507614","messageId":"20fdb5ba-075f-48b5-9e92-f85378500710@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-5-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 5/8] worktree: add relative cli/config options to `add` command","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-19T15:07:16Z","receivedAt":"2024-11-19T15:07:21Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 01/11/2024 04:38, Caleb White wrote:\n> This introduces the `--[no-]relative-paths` CLI option and\n> `worktree.useRelativePaths` configuration setting to the `worktree add`\n> command. When enabled these options allow worktrees to be linked using\n> relative paths, enhancing portability across environments where absolute\n> paths may differ (e.g., containerized setups, shared network drives).\n> Git still creates absolute paths by default, but these options allow\n> users to opt-in to relative paths if desired.\n> \n> The t2408 test file is removed and more comprehensive tests are\n> written for the various worktree operations in their own files.\n\nThis looks good, I've left a couple of comments below - there are a few \nplaces where the tests could be cleaned up (I've only commented on the \nfirst instance of each cleanup) but the coverage looks good and the \nimplementation looks good to me too. I'll leave it here for today, sorry \nit has taken me so long to get round to looking at these. I should be \nable to look at the rest of the patches this week.\n\n> +worktree.useRelativePaths::\n> +\tLink worktrees using relative paths (when \"true\") or absolute\n> +\tpaths (when \"false\"). This is particularly useful for setups\n> +\twhere the repository and worktrees may be moved between\n> +\tdifferent locations or environments. Defaults to \"false\".\n> ++\n> +Note that setting `worktree.useRelativePaths` to \"true\" implies enabling the\n> +`extension.relativeWorktrees` config (see linkgit:git-config[1]),\n> +thus making it incompatible with older versions of Git.\n> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\n> index 70437c815f13852bd2eb862176b8b933e6de0acf..88d2261012166a929b7f167d79720e4d965fd71b 100644\n> --- a/Documentation/git-worktree.txt\n> +++ b/Documentation/git-worktree.txt\n> @@ -216,6 +216,10 @@ To remove a locked worktree, specify `--force` twice.\n>   This can also be set up as the default behaviour by using the\n>   `worktree.guessRemote` config option.\n>   \n> +--[no-]relative-paths::\n> +\tOverrides the `worktree.useRelativePaths` config option, see\n> +\tlinkgit:git-config[1].\n\nI think it would be helpful to describe what this option does directly, \nrather than in terms of a config setting which is documented elsewhere \nand that may not be set\n\n>   --[no-]track::\n>   \tWhen creating a new branch, if `<commit-ish>` is a branch,\n>   \tmark it as \"upstream\" from the new branch.  This is the\n> diff --git a/builtin/worktree.c b/builtin/worktree.c\n> index dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644\n> --- a/builtin/worktree.c\n> +++ b/builtin/worktree.c\n> @@ -120,12 +120,14 @@ struct add_opts {\n>   \tint quiet;\n>   \tint checkout;\n>   \tint orphan;\n> +\tint relative_paths;\n\nExcellent - thanks for getting rind of the global in /worktree.c\n\n> diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\n> index cfc4aeb1798c6d023909cec771e5b74e983af5ea..d36d8a4db0e924877787697579544f10f92dc0cf 100755\n> --- a/t/t2400-worktree-add.sh\n> +++ b/t/t2400-worktree-add.sh\n> @@ -1207,4 +1207,58 @@ test_expect_success '\"add\" with initialized submodule, with submodule.recurse se\n>   \tgit -C project-clone -c submodule.recurse worktree add ../project-5\n>   '\n>   \n> +test_expect_success 'can create worktrees with relative paths' '\n> +\ttest_when_finished \"git worktree remove relative\" &&\n> +\tgit config worktree.useRelativePaths false &&\n\nUsing test_config instead of \"git config\" ensures the setting is cleared \nat the end of the test\n\n> +\tgit worktree add --relative-paths ./relative &&\n> +\tcat relative/.git >actual &&\n\nThere's no need to copy the file just so we can say \"actual\" below\n\n> +\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n> +\ttest_cmp expect actual &&\n> +\tcat .git/worktrees/relative/gitdir >actual &&\n> +\techo \"../../../relative/.git\" >expect &&\n> +\ttest_cmp expect actual\n> +\n> +'\n> +\n> +test_expect_success 'can create worktrees with absolute paths' '\n> +\tgit config worktree.useRelativePaths true &&\n> +\tgit worktree add ./relative &&\n> +\tcat relative/.git >actual &&\n> +\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n> +\ttest_cmp expect actual &&\n> +\tgit worktree add --no-relative-paths ./absolute &&\n> +\tcat absolute/.git >actual &&\n> +\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n> +\ttest_cmp expect actual\n> +'\n\nWe're only checking \".git\" here and not the \"gitdir\" file as well, I \nguess that's ok as the implemntation that writes the files is tested \nabove and here we're interesting in testing the config setting.\n\n> +test_expect_success 'move repo without breaking relative internal links' '\n> +\ttest_when_finished rm -rf repo moved &&\n> +\tgit init repo &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit config worktree.useRelativePaths true &&\n> +\t\ttest_commit initial &&\n> +\t\tgit worktree add wt1 &&\n> +\t\tcd .. &&\n> +\t\tmv repo moved &&\n> +\t\tcd moved/wt1 &&\n> +\t\tgit status >out 2>err &&\n> +\t\ttest_must_be_empty err\n\nThis relies on \"git status\" failing if the worktree links are broken. A \nmore direct test might be to check the output of \"git worktree list\" or \n\"git rev-parse --git-dir\".\n\n> +\t)\n> +'\n> +\n> +test_expect_success 'relative worktree sets extension config' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tgit init repo &&\n> +\tgit -C repo commit --allow-empty -m base &&\n> +\tgit -C repo worktree add --relative-paths ./foo &&\n> +\tgit -C repo config get core.repositoryformatversion >actual &&\n> +\techo 1 >expected &&\n> +\ttest_cmp expected actual &&\n\nThis can be done with\n\n\ttest_cmp_config -C repo 1 core.repositoryformatversion\n\n> +\tgit -C repo config get extensions.relativeworktrees >actual &&\n> +\techo true >expected &&\n> +\ttest_cmp expected actual\n> +'\nBest Wishes\n\nPhillip\n"},{"id":"507615","messageId":"47f2b6bf-1e10-4b01-8365-467cc5ca7779@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-2-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 2/8] worktree: add `relativeWorktrees` extension","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-19T15:07:52Z","receivedAt":"2024-11-19T15:07:55Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 01/11/2024 04:38, Caleb White wrote:\n> A new extension, `relativeWorktrees`, is added to indicate that at least\n> one worktree in the repository has been linked with relative paths.\n\nOut of interest what happens in the later patches if we add a worktree \nwith a relative path which sets this extension and then convert it to an \nabsolute path? Will we clear this extension or do we leave it set? I'm \nmainly curious, I don't think it's the end of the world if we leave the \nextension set.\n\n> This ensures older Git versions do not attempt to automatically prune\n> worktrees with relative paths, as they would not not recognize the\n> paths as being valid.\n\nSounds sensible. I'm not familiar with the code that handles repository \nextensions but the patch below looks reasonable to a layman.\n\nBest Wishes\n\nPhillip\n\n> Suggested-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Caleb White <cdwhite3@pm.me>\n> ---\n>   Documentation/config/extensions.txt | 6 ++++++\n>   repository.c                        | 1 +\n>   repository.h                        | 1 +\n>   setup.c                             | 7 +++++++\n>   setup.h                             | 1 +\n>   5 files changed, 16 insertions(+)\n> \n> diff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt\n> index 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644\n> --- a/Documentation/config/extensions.txt\n> +++ b/Documentation/config/extensions.txt\n> @@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or\n>   linkgit:git-clone[1]. Trying to change it after initialization will not\n>   work and will produce hard-to-diagnose issues.\n>   \n> +relativeWorktrees::\n> +\tIf enabled, indicates at least one worktree has been linked with\n> +\trelative paths. Automatically set if a worktree has been created or\n> +\trepaired with either the `--relative-paths` option or with the\n> +\t`worktree.useRelativePaths` config set to `true`.\n> +\n>   worktreeConfig::\n>   \tIf enabled, then worktrees will load config settings from the\n>   \t`$GIT_DIR/config.worktree` file in addition to the\n> diff --git a/repository.c b/repository.c\n> index f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644\n> --- a/repository.c\n> +++ b/repository.c\n> @@ -283,6 +283,7 @@ int repo_init(struct repository *repo,\n>   \trepo_set_compat_hash_algo(repo, format.compat_hash_algo);\n>   \trepo_set_ref_storage_format(repo, format.ref_storage_format);\n>   \trepo->repository_format_worktree_config = format.worktree_config;\n> +\trepo->repository_format_relative_worktrees = format.relative_worktrees;\n>   \n>   \t/* take ownership of format.partial_clone */\n>   \trepo->repository_format_partial_clone = format.partial_clone;\n> diff --git a/repository.h b/repository.h\n> index 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644\n> --- a/repository.h\n> +++ b/repository.h\n> @@ -150,6 +150,7 @@ struct repository {\n>   \n>   \t/* Configurations */\n>   \tint repository_format_worktree_config;\n> +\tint repository_format_relative_worktrees;\n>   \n>   \t/* Indicate if a repository has a different 'commondir' from 'gitdir' */\n>   \tunsigned different_commondir:1;\n> diff --git a/setup.c b/setup.c\n> index 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var,\n>   \t\t\t\t     \"extensions.refstorage\", value);\n>   \t\tdata->ref_storage_format = format;\n>   \t\treturn EXTENSION_OK;\n> +\t} else if (!strcmp(ext, \"relativeworktrees\")) {\n> +\t\tdata->relative_worktrees = git_config_bool(var, value);\n> +\t\treturn EXTENSION_OK;\n>   \t}\n>   \treturn EXTENSION_UNKNOWN;\n>   }\n> @@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok)\n>   \t\t\t\t\t\t    repo_fmt.ref_storage_format);\n>   \t\t\tthe_repository->repository_format_worktree_config =\n>   \t\t\t\trepo_fmt.worktree_config;\n> +\t\t\tthe_repository->repository_format_relative_worktrees =\n> +\t\t\t\trepo_fmt.relative_worktrees;\n>   \t\t\t/* take ownership of repo_fmt.partial_clone */\n>   \t\t\tthe_repository->repository_format_partial_clone =\n>   \t\t\t\trepo_fmt.partial_clone;\n> @@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt)\n>   \t\t\t\t    fmt->ref_storage_format);\n>   \tthe_repository->repository_format_worktree_config =\n>   \t\tfmt->worktree_config;\n> +\tthe_repository->repository_format_relative_worktrees =\n> +\t\tfmt->relative_worktrees;\n>   \tthe_repository->repository_format_partial_clone =\n>   \t\txstrdup_or_null(fmt->partial_clone);\n>   \tclear_repository_format(&repo_fmt);\n> diff --git a/setup.h b/setup.h\n> index e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644\n> --- a/setup.h\n> +++ b/setup.h\n> @@ -129,6 +129,7 @@ struct repository_format {\n>   \tint precious_objects;\n>   \tchar *partial_clone; /* value of extensions.partialclone */\n>   \tint worktree_config;\n> +\tint relative_worktrees;\n>   \tint is_bare;\n>   \tint hash_algo;\n>   \tint compat_hash_algo;\n> \n\n"},{"id":"507616","messageId":"977c1e75-1fef-4827-91ae-cc1a6ce7508e@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-3-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 3/8] worktree: refactor infer_backlink return","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-19T15:08:11Z","receivedAt":"2024-11-19T15:08:14Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nThanks for splitting this out into a separate patch\n\nOn 01/11/2024 04:38, Caleb White wrote:\n> -static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n> +static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n>   {\n>   \tstruct strbuf actual = STRBUF_INIT;\n>   \tconst char *id;\n>   \n> +\tstrbuf_reset(inferred);\n\nI think the code was clearer when we reset the buf just before using it. \nThat way it is easy to see that we add the path to an empty buffer.\n\n>   \tif (strbuf_read_file(&actual, gitfile, 0) < 0)\n>   \t\tgoto error;\n>   \tif (!starts_with(actual.buf, \"gitdir:\"))\n> @@ -741,18 +744,16 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n>   \tid++; /* advance past '/' to point at <id> */\n>   \tif (!*id)\n>   \t\tgoto error;\n> -\tstrbuf_reset(inferred);\n>   \tstrbuf_git_common_path(inferred, the_repository, \"worktrees/%s\", id);\n>   \tif (!is_directory(inferred->buf))\n>   \t\tgoto error;\n>   \n>   \tstrbuf_release(&actual);\n> -\treturn 1;\n> -\n> +\treturn inferred->len;\n>   error:\n>   \tstrbuf_release(&actual);\n>   \tstrbuf_reset(inferred); /* clear invalid path */\n> -\treturn 0;\n> +\treturn -1;\n\nWhy don't we need to update the callers of this function to account for \nthe new return value?\n\nBest Wishes\n\nPhillip\n\n\n"},{"id":"507679","messageId":"D5QQQCUL0DR7.31L4W6C4NT6UU@pm.me","threadId":"62433","inReplyTo":"20fdb5ba-075f-48b5-9e92-f85378500710@gmail.com","subject":"Re: [PATCH v4 5/8] worktree: add relative cli/config options to `add` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-20T05:01:58Z","receivedAt":"2024-11-20T05:02:11Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Tue Nov 19, 2024 at 9:07 AM CST, Phillip Wood wrote:\n> Hi Caleb\n>\n> This looks good, I've left a couple of comments below - there are a few\n> places where the tests could be cleaned up (I've only commented on the\n> first instance of each cleanup) but the coverage looks good and the\n> implementation looks good to me too. I'll leave it here for today, sorry\n> it has taken me so long to get round to looking at these. I should be\n> able to look at the rest of the patches this week.\n\nNo worries sir, thank you for taking the time to review. I greatly\nappreciate it. I'll start implementing the changes on my end, but I'll\nwait until you finish reviewing before sending v5.\n\n>> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\n>> index 70437c815f13852bd2eb862176b8b933e6de0acf..88d2261012166a929b7f167d79720e4d965fd71b 100644\n>> --- a/Documentation/git-worktree.txt\n>> +++ b/Documentation/git-worktree.txt\n>> @@ -216,6 +216,10 @@ To remove a locked worktree, specify `--force` twice.\n>>   This can also be set up as the default behaviour by using the\n>>   `worktree.guessRemote` config option.\n>>\n>> +--[no-]relative-paths::\n>> +\tOverrides the `worktree.useRelativePaths` config option, see\n>> +\tlinkgit:git-config[1].\n>\n> I think it would be helpful to describe what this option does directly,\n> rather than in terms of a config setting which is documented elsewhere\n> and that may not be set\n\nI originally had a description here, but it was requested by another\nreviewer that I remove the duplicate information and simply reference\nthe config option. I don't mind which direction we go, but we need\nsome consensus on this.\n\n>> diff --git a/builtin/worktree.c b/builtin/worktree.c\n>> index dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644\n>> --- a/builtin/worktree.c\n>> +++ b/builtin/worktree.c\n>> @@ -120,12 +120,14 @@ struct add_opts {\n>>   \tint quiet;\n>>   \tint checkout;\n>>   \tint orphan;\n>> +\tint relative_paths;\n>\n> Excellent - thanks for getting rind of the global in /worktree.c\n\nThanks! I'm happy with how this turned out.\n\n>> diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\n>> index cfc4aeb1798c6d023909cec771e5b74e983af5ea..d36d8a4db0e924877787697579544f10f92dc0cf 100755\n>> --- a/t/t2400-worktree-add.sh\n>> +++ b/t/t2400-worktree-add.sh\n>> @@ -1207,4 +1207,58 @@ test_expect_success '\"add\" with initialized submodule, with submodule.recurse se\n>>   \tgit -C project-clone -c submodule.recurse worktree add ../project-5\n>>   '\n>>\n>> +test_expect_success 'can create worktrees with relative paths' '\n>> +\ttest_when_finished \"git worktree remove relative\" &&\n>> +\tgit config worktree.useRelativePaths false &&\n>\n> Using test_config instead of \"git config\" ensures the setting is cleared\n> at the end of the test\n\nAh, good to know! I'll update.\n\n>> +\tgit worktree add --relative-paths ./relative &&\n>> +\tcat relative/.git >actual &&\n>\n> There's no need to copy the file just so we can say \"actual\" below\n\nGood point. I saw this pattern in other tests and thought it was\nstandard practice. I'll update.\n\n>> +test_expect_success 'can create worktrees with absolute paths' '\n>> +\tgit config worktree.useRelativePaths true &&\n>> +\tgit worktree add ./relative &&\n>> +\tcat relative/.git >actual &&\n>> +\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n>> +\ttest_cmp expect actual &&\n>> +\tgit worktree add --no-relative-paths ./absolute &&\n>> +\tcat absolute/.git >actual &&\n>> +\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n>> +\ttest_cmp expect actual\n>> +'\n>\n> We're only checking \".git\" here and not the \"gitdir\" file as well, I\n> guess that's ok as the implemntation that writes the files is tested\n> above and here we're interesting in testing the config setting.\n\nI can just add a test for the other file to be complete.\n\n>> +test_expect_success 'move repo without breaking relative internal links' '\n>> +\ttest_when_finished rm -rf repo moved &&\n>> +\tgit init repo &&\n>> +\t(\n>> +\t\tcd repo &&\n>> +\t\tgit config worktree.useRelativePaths true &&\n>> +\t\ttest_commit initial &&\n>> +\t\tgit worktree add wt1 &&\n>> +\t\tcd .. &&\n>> +\t\tmv repo moved &&\n>> +\t\tcd moved/wt1 &&\n>> +\t\tgit status >out 2>err &&\n>> +\t\ttest_must_be_empty err\n>\n> This relies on \"git status\" failing if the worktree links are broken. A\n> more direct test might be to check the output of \"git worktree list\" or\n> \"git rev-parse --git-dir\".\n\nAll three commands would have the same error message, but I suppose I\ncould switch to `git worktree list` for a more direct test.\n\n>> +test_expect_success 'relative worktree sets extension config' '\n>> +\ttest_when_finished \"rm -rf repo\" &&\n>> +\tgit init repo &&\n>> +\tgit -C repo commit --allow-empty -m base &&\n>> +\tgit -C repo worktree add --relative-paths ./foo &&\n>> +\tgit -C repo config get core.repositoryformatversion >actual &&\n>> +\techo 1 >expected &&\n>> +\ttest_cmp expected actual &&\n>\n> This can be done with\n>\n> \ttest_cmp_config -C repo 1 core.repositoryformatversion\n\nSweet! I like it.\n\n\nBest,\n\nCaleb\n\n\n"},{"id":"507680","messageId":"D5QQZ2X595GX.2FRO57TTXZ548@pm.me","threadId":"62433","inReplyTo":"47f2b6bf-1e10-4b01-8365-467cc5ca7779@gmail.com","subject":"Re: [PATCH v4 2/8] worktree: add `relativeWorktrees` extension","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-20T05:13:23Z","receivedAt":"2024-11-20T05:13:35Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Tue Nov 19, 2024 at 9:07 AM CST, Phillip Wood wrote:\n> On 01/11/2024 04:38, Caleb White wrote:\n>> A new extension, `relativeWorktrees`, is added to indicate that at least\n>> one worktree in the repository has been linked with relative paths.\n>\n> Out of interest what happens in the later patches if we add a worktree\n> with a relative path which sets this extension and then convert it to an\n> absolute path? Will we clear this extension or do we leave it set? I'm\n> mainly curious, I don't think it's the end of the world if we leave the\n> extension set.\n\nThe extension would remain set. I was going to try and unset the\nextension, but it is only safe to do that if all worktrees use absolute\n(meaning they would all need to be checked) and I decided it wasn't that\nimportant. I suppose the check could be added in the `repair_worktrees()`\nfunction during `git worktree repair` if it is deemed important.\n\n\nBest,\n\nCaleb\n\n"},{"id":"507682","messageId":"D5QR4BHKBZH9.231NGDY7RCV0L@pm.me","threadId":"62433","inReplyTo":"977c1e75-1fef-4827-91ae-cc1a6ce7508e@gmail.com","subject":"Re: [PATCH v4 3/8] worktree: refactor infer_backlink return","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-20T05:20:12Z","receivedAt":"2024-11-20T05:20:18Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Tue Nov 19, 2024 at 9:08 AM CST, Phillip Wood wrote:\n> Hi Caleb\n>\n> Thanks for splitting this out into a separate patch\n>\n> On 01/11/2024 04:38, Caleb White wrote:\n>> -static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n>> +static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n>>   {\n>>   \tstruct strbuf actual = STRBUF_INIT;\n>>   \tconst char *id;\n>>\n>> +\tstrbuf_reset(inferred);\n>\n> I think the code was clearer when we reset the buf just before using it.\n> That way it is easy to see that we add the path to an empty buffer.\n\nI moved it up to the top of the function to make it more clear that the\nbuffer is reset before being used. But I can move it back.\n\n>>   \tif (strbuf_read_file(&actual, gitfile, 0) < 0)\n>>   \t\tgoto error;\n>>   \tif (!starts_with(actual.buf, \"gitdir:\"))\n>> @@ -741,18 +744,16 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n>>   \tid++; /* advance past '/' to point at <id> */\n>>   \tif (!*id)\n>>   \t\tgoto error;\n>> -\tstrbuf_reset(inferred);\n>>   \tstrbuf_git_common_path(inferred, the_repository, \"worktrees/%s\", id);\n>>   \tif (!is_directory(inferred->buf))\n>>   \t\tgoto error;\n>>\n>>   \tstrbuf_release(&actual);\n>> -\treturn 1;\n>> -\n>> +\treturn inferred->len;\n>>   error:\n>>   \tstrbuf_release(&actual);\n>>   \tstrbuf_reset(inferred); /* clear invalid path */\n>> -\treturn 0;\n>> +\treturn -1;\n>\n> Why don't we need to update the callers of this function to account for\n> the new return value?\n\nOriginally this function was called inside an `if` statement, however,\nanother topic extracted the call to a separate line and so this return\nwas no longer used. I decided to keep the return anyway in case it was\nuseful in the future.\n\n\nBest,\n\nCaleb\n\n"},{"id":"507938","messageId":"75b4023a-4005-49ff-a06d-e50fcd1c2b50@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-6-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 6/8] worktree: add relative cli/config options to `move` command","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T15:55:31Z","receivedAt":"2024-11-22T15:55:34Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nThis looks good, I've left a couple of comments on the tests - they \ncould be cleaned up like the last patch. The implementation looks good.\n\nOn 01/11/2024 04:38, Caleb White wrote:\n> This teaches the `worktree move` command to respect the\n> `--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\n> config setting. If an existing worktree is moved with `--relative-paths`\n> the new path will be relative (and visa-versa).\n> \n> Signed-off-by: Caleb White <cdwhite3@pm.me>\n> ---\n>   builtin/worktree.c       |  4 +++-\n>   t/t2403-worktree-move.sh | 22 ++++++++++++++++++++++\n>   worktree.c               | 23 ++++++++++-------------\n>   worktree.h               |  3 ++-\n>   4 files changed, 37 insertions(+), 15 deletions(-)\n> \n> diff --git a/builtin/worktree.c b/builtin/worktree.c\n> index e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644\n> --- a/builtin/worktree.c\n> +++ b/builtin/worktree.c\n> @@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n>   \t\tOPT__FORCE(&force,\n>   \t\t\t N_(\"force move even if worktree is dirty or locked\"),\n>   \t\t\t PARSE_OPT_NOCOMPLETE),\n> +\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n> +\t\t\t N_(\"use relative paths for worktrees\")),\n\nBy the time this function is called we've already initialized \nuse_relative_paths from the config setting - good\n\n> [...]\n> +test_expect_success 'move worktree with absolute path to relative path' '\n> +\tgit config worktree.useRelativePaths false &&\n> +\tgit worktree add ./absolute &&\n> +\tgit worktree move --relative-paths absolute relative &&\n> +\tcat relative/.git >actual &&\n> +\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n> +\ttest_cmp expect actual &&\n> +\tgit config worktree.useRelativePaths true &&\n> +\tgit worktree move relative relative2 &&\n> +\tcat relative2/.git >actual &&\n> +\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n> +\ttest_cmp expect actual\n> +'\n\nAs with the last patch we should use test_config and stop copying files \njust to compare them. It's probably worth checking the gitdir file as \nwell as .git here as well.\n\n> +test_expect_success 'move worktree with relative path to absolute path' '\n> +\tgit config worktree.useRelativePaths true &&\n> +\tgit worktree move --no-relative-paths relative2 absolute &&\n> +\tcat absolute/.git >actual &&\n> +\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n> +\ttest_cmp expect actual\n> +'\n> +\n>   test_done\n> [...]\n> -\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n> +\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n\nWe used to append \"/gitdir\" below but now we do it all in one - nice.\n\n>   \tstrbuf_realpath(&path, path_, 1);\n> +\tstrbuf_addf(&dotgit, \"%s/.git\", path.buf);\n>   \tif (fspathcmp(wt->path, path.buf)) {\n\nget_linked_worktree guarentees that wt->path is absolute so this looks good.\n\n> -\t\tstrbuf_addf(&file, \"%s/gitdir\", repo.buf);\n> -\t\twrite_file(file.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n> -\t\tstrbuf_reset(&file);\n> -\t\tstrbuf_addf(&file, \"%s/.git\", path.buf);\n> -\t\twrite_file(file.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n> +\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n\nThis is a nice simplification\n\nBest Wishes\n\nPhillip\n"},{"id":"507939","messageId":"135739ad-6722-449b-9f9b-31c0bbc9f9cb@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-7-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 7/8] worktree: add relative cli/config options to `repair` command","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T15:55:43Z","receivedAt":"2024-11-22T15:55:46Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 01/11/2024 04:38, Caleb White wrote:\n> This teaches the `worktree repair` command to respect the\n> `--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\n> config setting. If an existing worktree with an absolute path is repaired\n> with `--relative-paths`, the links will be replaced with relative paths,\n> even if the original path was correct. This allows a user to covert\n> existing worktrees between absolute/relative as desired.\n\nThis looks pretty good the strbuf changes rather mask the real meat of \nthe patch though.\n\n> Signed-off-by: Caleb White <cdwhite3@pm.me>\n> diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh\n> index 7686e60f6ad186519b275f11a5e14064c905b207..84451e903b2ef3c645c0311faf055c846588baf6 100755\n> --- a/t/t2406-worktree-repair.sh\n> +++ b/t/t2406-worktree-repair.sh\n> @@ -216,4 +216,30 @@ test_expect_success 'repair copied main and linked worktrees' '\n>   \ttest_cmp dup/linked.expect dup/linked/.git\n>   '\n>   \n> +test_expect_success 'repair absolute worktree to use relative paths' '\n> +\ttest_when_finished \"rm -rf main side sidemoved\" &&\n> +\ttest_create_repo main &&\n> +\ttest_commit -C main init &&\n> +\tgit -C main worktree add --detach ../side &&\n> +\techo \"../../../../sidemoved/.git\" >expect-gitdir &&\n> +\techo \"gitdir: ../main/.git/worktrees/side\" >expect-gitfile &&\n> +\tmv side sidemoved &&\n> +\tgit -C main worktree repair --relative-paths ../sidemoved &&\n> +\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n> +\ttest_cmp expect-gitfile sidemoved/.git\n> +'\n> +\n> +test_expect_success 'repair relative worktree to use absolute paths' '\n> +\ttest_when_finished \"rm -rf main side sidemoved\" &&\n> +\ttest_create_repo main &&\n> +\ttest_commit -C main init &&\n> +\tgit -C main worktree add --relative-paths --detach ../side &&\n> +\techo \"$(pwd)/sidemoved/.git\" >expect-gitdir &&\n> +\techo \"gitdir: $(pwd)/main/.git/worktrees/side\" >expect-gitfile &&\n> +\tmv side sidemoved &&\n> +\tgit -C main worktree repair ../sidemoved &&\n> +\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n> +\ttest_cmp expect-gitfile sidemoved/.git\n> +'\n\nThese tests looks sensibile, we should probably check that \"git worktree \nrepair\" repects worktree.userelativepaths. I wonder if we have any \ncoverage of repair_worktrees() as I think in these tests the problem is \nfixed by repair_worktree_at_path() before we call repair_worktrees().\n\n>   test_done\n> diff --git a/worktree.c b/worktree.c\n> index 6b640cd9549ecb060236f7eddf1390caa181f1a0..2cb994ac462debf966ac51b5a4f33c30cfebd4ef 100644\n> --- a/worktree.c\n> +++ b/worktree.c\n> @@ -574,12 +574,14 @@ int other_head_refs(each_ref_fn fn, void *cb_data)\n>    * pointing at <repo>/worktrees/<id>.\n>    */\n>   static void repair_gitfile(struct worktree *wt,\n> -\t\t\t   worktree_repair_fn fn, void *cb_data)\n> +\t\t\t   worktree_repair_fn fn,\n> +\t\t\t   void *cb_data,\n\nStyle wise leaving \"fn\" and \"cb_data\" on the same line would be fine. \nThat applies to all the functions.\n\n> +\t\t\t   int use_relative_paths)\n>   {\n\n> [...]\n>   \tif (dotgit_contents) {\n> @@ -612,18 +615,20 @@ static void repair_gitfile(struct worktree *wt,\n>   \t\trepair = _(\".git file broken\");\n>   \telse if (fspathcmp(backlink.buf, repo.buf))\n>   \t\trepair = _(\".git file incorrect\");\n> +\telse if (use_relative_paths == is_absolute_path(dotgit_contents))\n> +\t\trepair = _(\".git file absolute/relative path mismatch\");\n\nComparing ints as booleans makes me nervous in case we have a non-zero \nvalue that isn't 1 but is_absolute_path() returns 0 or 1 and we know \nuse_relative_paths is 0 or 1.\n\n>   \tif (repair) {\n>   \t\tfn(0, wt->path, repair, cb_data);\n> -\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, wt->path, &tmp));\n> +\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n\nWe used to update only the \".git\", now we'll update both. In the case \nwhere we're changing to/from absolute/relative paths that's good because \nwe'll update the \"gitdir\" file as well. In the other cases it looks like \nwe've we've found this worktree via the \"gitdir\" file so it should be \nsafe to write the same value back to that file.\n\n> [...]\n>   void repair_worktree_at_path(const char *path,\n> -\t\t\t     worktree_repair_fn fn, void *cb_data)\n> +\t\t\t     worktree_repair_fn fn,\n> +\t\t\t     void *cb_data,\n> +\t\t\t     int use_relative_paths)\n>   {\n>   \tstruct strbuf dotgit = STRBUF_INIT;\n> -\tstruct strbuf realdotgit = STRBUF_INIT;\n>   \tstruct strbuf backlink = STRBUF_INIT;\n>   \tstruct strbuf inferred_backlink = STRBUF_INIT;\n>   \tstruct strbuf gitdir = STRBUF_INIT;\n>   \tstruct strbuf olddotgit = STRBUF_INIT;\n> -\tstruct strbuf realolddotgit = STRBUF_INIT;\n> -\tstruct strbuf tmp = STRBUF_INIT;\n>\n>   \tchar *dotgit_contents = NULL;\n>   \tconst char *repair = NULL;\n>   \tint err;\n> @@ -779,25 +783,25 @@ void repair_worktree_at_path(const char *path,\n>   \t\tgoto done;\n>   \n>   \tstrbuf_addf(&dotgit, \"%s/.git\", path);\n> -\tif (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {\n> +\tif (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {\n\nThis works because strbuf_realpath() copies dotgit.buf before it resets \ndotgit but that does not seem to be documented and looking at the output of\n\n     git grep strbuf_realpath | grep \\\\.buf\n\nI don't see any other callers relying on this outside of your earlier \nchanges to this file. Given that I wonder if we should leave it as is \nwhich would also simplify this patch as the interesting changes are \nswamped by the strbuf tweaking.\n\n> [...]   \n>   \tif (repair) {\n>   \t\tfn(0, gitdir.buf, repair, cb_data);\n> -\t\twrite_file(gitdir.buf, \"%s\", relative_path(realdotgit.buf, backlink.buf, &tmp));\n> +\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n\nWe used to just update \"gitdir\" but we now update \".git\" as well. As \nabove that's good when we're repairing a relative/absolute mismatch. In \nthe other cases dotgit always contains the path to the \".git\" file in \nthe worktree so that should be fine.\n\nBest Wishes\n\nPhillip\n\n"},{"id":"507940","messageId":"db700d82-c775-4e0d-a171-9caf6489a3d8@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T15:57:16Z","receivedAt":"2024-11-22T15:57:19Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nThanks for working on this, I've left a few comments on the tests and in \nthe last couple of patches there is some distracting re-factoring of how \nwhich strbufs be use but fundamentally this series looks sound to me. We \nshould certainly try and get this merged before the next release.\n\nBest Wishes\n\nPhillip\n\nOn 01/11/2024 04:38, Caleb White wrote:\n> This series introduces the `--[no-]relative-paths` CLI option for\n> `git worktree {add, move, repair}` commands, as well as the\n> `worktree.useRelativePaths` configuration setting. When enabled,\n> these options allow worktrees to be linked using relative paths,\n> enhancing portability across environments where absolute paths\n> may differ (e.g., containerized setups, shared network drives).\n> Git still creates absolute paths by default, but these options allow\n> users to opt-in to relative paths if desired.\n> \n> Using the `--relative-paths` option with `worktree {move, repair}`\n> will convert absolute paths to relative ones, while `--no-relative-paths`\n> does the reverse. For cases where users want consistency in path handling,\n> the config option `worktree.useRelativePaths` provides a persistent setting.\n> \n> A new extension, `relativeWorktrees`, is added to indicate that at least\n> one worktree in the repository has been linked with relative paths. This\n> extension is automatically set when a worktree is created or repaired\n> using the `--relative-paths` option, or when the\n> `worktree.useRelativePaths` config is set to `true`.\n> \n> The `relativeWorktrees` extension ensures older Git versions do not\n> attempt to automatically prune worktrees with relative paths, as they\n> would not not recognize the paths as being valid.\n> \n> Signed-off-by: Caleb White <cdwhite3@pm.me>\n> ---\n> The base for this patch series is obtained by applying the following\n> patch onto 6a11438f43:\n> - cw/config-extensions topic (doc: consolidate extensions in git-config\n>    documentation, 2024-10-22, <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>)\n> \n> Link to original patch series:\n> https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n> \n> ---\n> Changes in v4:\n> - Fixed failing test in ci\n> - Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me\n> Changes in v3:\n> - Split patches into smaller edits.\n> - Moved tests into the patches with the relevant code changes.\n> - Removed global `use_relative_paths` and instead pass parameter to functions.\n> - Changed `infer_backlink` return type from `int` to `ssize_t`.\n> - Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.\n> - Reordered patches\n> - Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me\n> Changes in v2:\n> - Fixed a bug where repositories with valid extensions would be downgraded\n>    to v0 during reinitialization, causing future operations to fail.\n> - Split patch [1/2] into three separate patches.\n> - Updated cover letter and commit messages.\n> - Updated documentation wording.\n> - Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me\n> \n> ---\n> Caleb White (8):\n>        setup: correctly reinitialize repository version\n>        worktree: add `relativeWorktrees` extension\n>        worktree: refactor infer_backlink return\n>        worktree: add `write_worktree_linking_files()` function\n>        worktree: add relative cli/config options to `add` command\n>        worktree: add relative cli/config options to `move` command\n>        worktree: add relative cli/config options to `repair` command\n>        worktree: refactor `repair_worktree_after_gitdir_move()`\n> \n>   Documentation/config/extensions.txt |   6 ++\n>   Documentation/config/worktree.txt   |  10 +++\n>   Documentation/git-worktree.txt      |   7 ++\n>   builtin/worktree.c                  |  29 ++++---\n>   repository.c                        |   1 +\n>   repository.h                        |   1 +\n>   setup.c                             |  39 +++++++--\n>   setup.h                             |   1 +\n>   t/t0001-init.sh                     |  22 ++++-\n>   t/t2400-worktree-add.sh             |  54 ++++++++++++\n>   t/t2401-worktree-prune.sh           |   3 +-\n>   t/t2402-worktree-list.sh            |  22 +++++\n>   t/t2403-worktree-move.sh            |  22 +++++\n>   t/t2406-worktree-repair.sh          |  26 ++++++\n>   t/t2408-worktree-relative.sh        |  39 ---------\n>   t/t5504-fetch-receive-strict.sh     |   6 +-\n>   worktree.c                          | 163 ++++++++++++++++++++----------------\n>   worktree.h                          |  22 ++++-\n>   18 files changed, 331 insertions(+), 142 deletions(-)\n> ---\n> base-commit: 6a11438f43469f3815f2f0fc997bd45792ff04c0\n> change-id: 20241025-wt_relative_options-afa41987bc32\n> prerequisite-change-id: 20241020-cleanup-extension-docs-f365868711bf:v1\n> prerequisite-patch-id: 60a443b24e92938b9b6f4a016a7bab87e13bf3ea\n> \n> Best regards,\n\n"},{"id":"507941","messageId":"50187d7d-2a96-4df2-baaa-ea27c9bd5dcf@gmail.com","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-8-07a3dc0f02a3@pm.me","subject":"Re: [PATCH v4 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T15:58:20Z","receivedAt":"2024-11-22T15:58:22Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 01/11/2024 04:39, Caleb White wrote:\n> This refactors `repair_worktree_after_gitdir_move()` to use the new\n> `write_worktree_linking_files` function. It also preserves the\n> relativity of the linking files; e.g., if an existing worktree used\n> absolute paths then the repaired paths will be absolute (and visa-versa).\n\nIt would be helpful to give a brief explanation of what this function is \nused for as it seems to be called from setup.c rather than as part of \n\"git worktree repair\"\n\n> This also adds a test case for reinitializing a repository that has\n> relative worktrees.\n\nI'm not sure exactly what this is doing - are we breaking something and \nthen calling \"git init\" to repair it?\n\nMy understanding of what this function is trying to do is rather limited \nbut as far as I can see the conversion looks OK though the strbuf \nchanges are quite distracting.\n\n> Signed-off-by: Caleb White <cdwhite3@pm.me>\n> ---\n>   t/t0001-init.sh | 22 ++++++++++++++++++----\n>   worktree.c      | 29 ++++++++---------------------\n>   2 files changed, 26 insertions(+), 25 deletions(-)\n> \n> diff --git a/t/t0001-init.sh b/t/t0001-init.sh\n> index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..264951592a711f6879555b785905e799db6c5dd4 100755\n> --- a/t/t0001-init.sh\n> +++ b/t/t0001-init.sh\n> @@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n>   sep_git_dir_worktree ()  {\n>   \ttest_when_finished \"rm -rf mainwt linkwt seprepo\" &&\n>   \tgit init mainwt &&\n> +\tif test \"relative\" = $2\n> +\tthen\n> +\t\tgit -C mainwt config worktree.useRelativePaths true\n\ntest_config is your friend here (it accepts -C <repo>)\n\nBest Wishes\n\nPhillip\n\n> +\telse\n> +\t\tgit -C mainwt config worktree.useRelativePaths false\n> +\tfi\n>   \ttest_commit -C mainwt gumby &&\n>   \tgit -C mainwt worktree add --detach ../linkwt &&\n>   \tgit -C \"$1\" init --separate-git-dir ../seprepo &&\n> @@ -442,12 +448,20 @@ sep_git_dir_worktree ()  {\n>   \ttest_cmp expect actual\n>   }\n>   \n> -test_expect_success 're-init to move gitdir with linked worktrees' '\n> -\tsep_git_dir_worktree mainwt\n> +test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '\n> +\tsep_git_dir_worktree mainwt absolute\n> +'\n> +\n> +test_expect_success 're-init to move gitdir within linked worktree (absolute)' '\n> +\tsep_git_dir_worktree linkwt absolute\n> +'\n> +\n> +test_expect_success 're-init to move gitdir with linked worktrees (relative)' '\n> +\tsep_git_dir_worktree mainwt relative\n>   '\n>   \n> -test_expect_success 're-init to move gitdir within linked worktree' '\n> -\tsep_git_dir_worktree linkwt\n> +test_expect_success 're-init to move gitdir within linked worktree (relative)' '\n> +\tsep_git_dir_worktree linkwt relative\n>   '\n>   \n>   test_expect_success MINGW '.git hidden' '\n> diff --git a/worktree.c b/worktree.c\n> index 2cb994ac462debf966ac51b5a4f33c30cfebd4ef..2c2e9fd8fdc22d4fe3705227d72b8dbd293038b2 100644\n> --- a/worktree.c\n> +++ b/worktree.c\n> @@ -653,45 +653,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat\n>   \n>   void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)\n>   {\n> -\tstruct strbuf path = STRBUF_INIT;\n> -\tstruct strbuf repo = STRBUF_INIT;\n>   \tstruct strbuf gitdir = STRBUF_INIT;\n>   \tstruct strbuf dotgit = STRBUF_INIT;\n> -\tstruct strbuf olddotgit = STRBUF_INIT;\n> -\tstruct strbuf tmp = STRBUF_INIT;\n> +\tint is_relative_path;\n>   \n>   \tif (is_main_worktree(wt))\n>   \t\tgoto done;\n>   \n> -\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n> -\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n> +\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n>   \n> -\tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n> +\tif (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)\n>   \t\tgoto done;\n>   \n> -\tstrbuf_rtrim(&olddotgit);\n> -\tif (is_absolute_path(olddotgit.buf)) {\n> -\t\tstrbuf_addbuf(&dotgit, &olddotgit);\n> -\t} else {\n> -\t\tstrbuf_addf(&dotgit, \"%s/worktrees/%s/%s\", old_path, wt->id, olddotgit.buf);\n> +\tstrbuf_rtrim(&dotgit);\n> +\tis_relative_path = ! is_absolute_path(dotgit.buf);\n> +\tif (is_relative_path) {\n> +\t\tstrbuf_insertf(&dotgit, 0, \"%s/worktrees/%s/\", old_path, wt->id);\n>   \t\tstrbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);\n>   \t}\n>   \n>   \tif (!file_exists(dotgit.buf))\n>   \t\tgoto done;\n>   \n> -\tstrbuf_addbuf(&path, &dotgit);\n> -\tstrbuf_strip_suffix(&path, \"/.git\");\n> -\n> -\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n> -\twrite_file(gitdir.buf, \"%s\", relative_path(dotgit.buf, repo.buf, &tmp));\n> +\twrite_worktree_linking_files(dotgit, gitdir, is_relative_path);\n>   done:\n> -\tstrbuf_release(&path);\n> -\tstrbuf_release(&repo);\n>   \tstrbuf_release(&gitdir);\n>   \tstrbuf_release(&dotgit);\n> -\tstrbuf_release(&olddotgit);\n> -\tstrbuf_release(&tmp);\n>   }\n>   \n>   void repair_worktrees_after_gitdir_move(const char *old_path)\n> \n\n"},{"id":"507942","messageId":"7ddddf98-840d-4d36-b3bb-6d5265a1362f@gmail.com","threadId":"62433","inReplyTo":"D5QQQCUL0DR7.31L4W6C4NT6UU@pm.me","subject":"Re: [PATCH v4 5/8] worktree: add relative cli/config options to `add` command","fromName":"","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T16:44:35Z","receivedAt":"2024-11-22T16:44:37Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 20/11/2024 05:01, Caleb White wrote:\n> On Tue Nov 19, 2024 at 9:07 AM CST, Phillip Wood wrote:\n> \n>>> +--[no-]relative-paths::\n>>> +\tOverrides the `worktree.useRelativePaths` config option, see\n>>> +\tlinkgit:git-config[1].\n>>\n>> I think it would be helpful to describe what this option does directly,\n>> rather than in terms of a config setting which is documented elsewhere\n>> and that may not be set\n> \n> I originally had a description here, but it was requested by another\n> reviewer that I remove the duplicate information and simply reference\n> the config option. I don't mind which direction we go, but we need\n> some consensus on this.\n\nOh right, from a user's point of view describing an option in terms of a \nconfig option that may not be set seems a bit confusing. Looking at the \nman page we describe --[no-]guess-remote and then mention the config \noption rather than just referring the reader to the config man page.\n\nBest Wishes\n\nPhillip\n\n>>> diff --git a/builtin/worktree.c b/builtin/worktree.c\n>>> index dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644\n>>> --- a/builtin/worktree.c\n>>> +++ b/builtin/worktree.c\n>>> @@ -120,12 +120,14 @@ struct add_opts {\n>>>    \tint quiet;\n>>>    \tint checkout;\n>>>    \tint orphan;\n>>> +\tint relative_paths;\n>>\n>> Excellent - thanks for getting rind of the global in /worktree.c\n> \n> Thanks! I'm happy with how this turned out.\n> \n>>> diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\n>>> index cfc4aeb1798c6d023909cec771e5b74e983af5ea..d36d8a4db0e924877787697579544f10f92dc0cf 100755\n>>> --- a/t/t2400-worktree-add.sh\n>>> +++ b/t/t2400-worktree-add.sh\n>>> @@ -1207,4 +1207,58 @@ test_expect_success '\"add\" with initialized submodule, with submodule.recurse se\n>>>    \tgit -C project-clone -c submodule.recurse worktree add ../project-5\n>>>    '\n>>>\n>>> +test_expect_success 'can create worktrees with relative paths' '\n>>> +\ttest_when_finished \"git worktree remove relative\" &&\n>>> +\tgit config worktree.useRelativePaths false &&\n>>\n>> Using test_config instead of \"git config\" ensures the setting is cleared\n>> at the end of the test\n> \n> Ah, good to know! I'll update.\n> \n>>> +\tgit worktree add --relative-paths ./relative &&\n>>> +\tcat relative/.git >actual &&\n>>\n>> There's no need to copy the file just so we can say \"actual\" below\n> \n> Good point. I saw this pattern in other tests and thought it was\n> standard practice. I'll update.\n> \n>>> +test_expect_success 'can create worktrees with absolute paths' '\n>>> +\tgit config worktree.useRelativePaths true &&\n>>> +\tgit worktree add ./relative &&\n>>> +\tcat relative/.git >actual &&\n>>> +\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n>>> +\ttest_cmp expect actual &&\n>>> +\tgit worktree add --no-relative-paths ./absolute &&\n>>> +\tcat absolute/.git >actual &&\n>>> +\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n>>> +\ttest_cmp expect actual\n>>> +'\n>>\n>> We're only checking \".git\" here and not the \"gitdir\" file as well, I\n>> guess that's ok as the implemntation that writes the files is tested\n>> above and here we're interesting in testing the config setting.\n> \n> I can just add a test for the other file to be complete.\n> \n>>> +test_expect_success 'move repo without breaking relative internal links' '\n>>> +\ttest_when_finished rm -rf repo moved &&\n>>> +\tgit init repo &&\n>>> +\t(\n>>> +\t\tcd repo &&\n>>> +\t\tgit config worktree.useRelativePaths true &&\n>>> +\t\ttest_commit initial &&\n>>> +\t\tgit worktree add wt1 &&\n>>> +\t\tcd .. &&\n>>> +\t\tmv repo moved &&\n>>> +\t\tcd moved/wt1 &&\n>>> +\t\tgit status >out 2>err &&\n>>> +\t\ttest_must_be_empty err\n>>\n>> This relies on \"git status\" failing if the worktree links are broken. A\n>> more direct test might be to check the output of \"git worktree list\" or\n>> \"git rev-parse --git-dir\".\n> \n> All three commands would have the same error message, but I suppose I\n> could switch to `git worktree list` for a more direct test.\n> \n>>> +test_expect_success 'relative worktree sets extension config' '\n>>> +\ttest_when_finished \"rm -rf repo\" &&\n>>> +\tgit init repo &&\n>>> +\tgit -C repo commit --allow-empty -m base &&\n>>> +\tgit -C repo worktree add --relative-paths ./foo &&\n>>> +\tgit -C repo config get core.repositoryformatversion >actual &&\n>>> +\techo 1 >expected &&\n>>> +\ttest_cmp expected actual &&\n>>\n>> This can be done with\n>>\n>> \ttest_cmp_config -C repo 1 core.repositoryformatversion\n> \n> Sweet! I like it.\n> \n> \n> Best,\n> \n> Caleb\n> \n> \n\n"},{"id":"507943","messageId":"e207382e-3e6d-477a-9a1d-48949df06160@gmail.com","threadId":"62433","inReplyTo":"D5QQZ2X595GX.2FRO57TTXZ548@pm.me","subject":"Re: [PATCH v4 2/8] worktree: add `relativeWorktrees` extension","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T16:44:41Z","receivedAt":"2024-11-22T16:44:43Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 20/11/2024 05:13, Caleb White wrote:\n> On Tue Nov 19, 2024 at 9:07 AM CST, Phillip Wood wrote:\n>> On 01/11/2024 04:38, Caleb White wrote:\n>>> A new extension, `relativeWorktrees`, is added to indicate that at least\n>>> one worktree in the repository has been linked with relative paths.\n>>\n>> Out of interest what happens in the later patches if we add a worktree\n>> with a relative path which sets this extension and then convert it to an\n>> absolute path? Will we clear this extension or do we leave it set? I'm\n>> mainly curious, I don't think it's the end of the world if we leave the\n>> extension set.\n> \n> The extension would remain set. I was going to try and unset the\n> extension, but it is only safe to do that if all worktrees use absolute\n> (meaning they would all need to be checked) and I decided it wasn't that\n> important.\n\nYes, I don't think we need to do that just now\n\n  I suppose the check could be added in the `repair_worktrees()`\n> function during `git worktree repair` if it is deemed important.\n\nThat could be a nice addition in the future\n\nBest Wishes\n\nPhillip\n\n> \n> Best,\n> \n> Caleb\n> \n\n"},{"id":"507944","messageId":"18dd2d59-aa12-4921-bd2a-2bbc0027c63c@gmail.com","threadId":"62433","inReplyTo":"D5QR4BHKBZH9.231NGDY7RCV0L@pm.me","subject":"Re: [PATCH v4 3/8] worktree: refactor infer_backlink return","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-22T16:44:45Z","receivedAt":"2024-11-22T16:44:47Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 20/11/2024 05:20, Caleb White wrote:\n> On Tue Nov 19, 2024 at 9:08 AM CST, Phillip Wood wrote:\n>>>    \tstrbuf_release(&actual);\n>>> -\treturn 1;\n>>> -\n>>> +\treturn inferred->len;\n>>>    error:\n>>>    \tstrbuf_release(&actual);\n>>>    \tstrbuf_reset(inferred); /* clear invalid path */\n>>> -\treturn 0;\n>>> +\treturn -1;\n>>\n>> Why don't we need to update the callers of this function to account for\n>> the new return value?\n> \n> Originally this function was called inside an `if` statement, however,\n> another topic extracted the call to a separate line and so this return\n> was no longer used. I decided to keep the return anyway in case it was\n> useful in the future.\n\nIf we don't use the return value I'd be inclined to leave it as is.\n\nBest Wishes\n\nPhillip\n\n> \n> Best,\n> \n> Caleb\n> \n\n"},{"id":"507958","messageId":"svDK3f3yQtR2snjDN2RJPcvSLOV200EFlIpOhyyk1DfgPI9EYZzH9Um-GcnCcZuwbZ4u5V_khXizC-l1cOX_aahBlCDWPYjpdkYJfr34YBU=@pm.me","threadId":"62433","inReplyTo":"18dd2d59-aa12-4921-bd2a-2bbc0027c63c@gmail.com","subject":"Re: [PATCH v4 3/8] worktree: refactor infer_backlink return","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-22T19:26:02Z","receivedAt":"2024-11-22T19:26:12Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Friday, November 22nd, 2024 at 10:44, Phillip Wood <phillip.wood123@gmail.com> wrote:\n> On 20/11/2024 05:20, Caleb White wrote:\n> > Originally this function was called inside an `if` statement, however,\n> > another topic extracted the call to a separate line and so this return\n> > was no longer used. I decided to keep the return anyway in case it was\n> > useful in the future.\n> \n> If we don't use the return value I'd be inclined to leave it as is.\n\nThis is being changed in response to feedback on the previous topic\n(which was merged a bit too prematurely). I would prefer to leave\nthese changes in if that's alright to respect the previous reviewers\nand given that I've already made the changes.\n\nBest,\n\nCaleb\n"},{"id":"507959","messageId":"3El2QMJvXAu8Y_znK4CRouDl7FPQLWczoqz3Fkwl8D44cLGctSw86pXw6B-uVKqf0-G99SMjSbHAYY0dTJVKs9VJLvDYlpC_kIaTqdRw2xU=@pm.me","threadId":"62433","inReplyTo":"e207382e-3e6d-477a-9a1d-48949df06160@gmail.com","subject":"Re: [PATCH v4 2/8] worktree: add `relativeWorktrees` extension","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-22T19:27:17Z","receivedAt":"2024-11-22T19:27:21Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Friday, November 22nd, 2024 at 10:44, Phillip Wood <phillip.wood123@gmail.com> wrote:\n> On 20/11/2024 05:13, Caleb White wrote:\n> > The extension would remain set. I was going to try and unset the\n> > extension, but it is only safe to do that if all worktrees use absolute\n> > (meaning they would all need to be checked) and I decided it wasn't that\n> > important.\n> \n> Yes, I don't think we need to do that just now\n> \n> I suppose the check could be added in the `repair_worktrees()`\n> \n> > function during `git worktree repair` if it is deemed important.\n> \n> \n> That could be a nice addition in the future\n\nI'm glad you think so too :), and I'll keep it in mind for the future.\n\nBest,\n\nCaleb\n"},{"id":"507969","messageId":"D5T9IZ3CT3KB.9QNSOHHW7VYL@pm.me","threadId":"62433","inReplyTo":"75b4023a-4005-49ff-a06d-e50fcd1c2b50@gmail.com","subject":"Re: [PATCH v4 6/8] worktree: add relative cli/config options to `move` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-23T04:11:02Z","receivedAt":"2024-11-23T04:11:15Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Fri Nov 22, 2024 at 9:55 AM CST, Phillip Wood wrote:\n> On 01/11/2024 04:38, Caleb White wrote:\n>> [...]\n>> +test_expect_success 'move worktree with absolute path to relative path' '\n>> +\tgit config worktree.useRelativePaths false &&\n>> +\tgit worktree add ./absolute &&\n>> +\tgit worktree move --relative-paths absolute relative &&\n>> +\tcat relative/.git >actual &&\n>> +\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n>> +\ttest_cmp expect actual &&\n>> +\tgit config worktree.useRelativePaths true &&\n>> +\tgit worktree move relative relative2 &&\n>> +\tcat relative2/.git >actual &&\n>> +\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n>> +\ttest_cmp expect actual\n>> +'\n>\n> As with the last patch we should use test_config and stop copying files\n> just to compare them. It's probably worth checking the gitdir file as\n> well as .git here as well.\n\nUpdated, thanks for pointing that out.\n\nBest,\n\nCaleb\n\n"},{"id":"507970","messageId":"D5TA24EUHQ7N.XV0HCIO6MRS6@pm.me","threadId":"62433","inReplyTo":"50187d7d-2a96-4df2-baaa-ea27c9bd5dcf@gmail.com","subject":"Re: [PATCH v4 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-23T04:36:02Z","receivedAt":"2024-11-23T04:36:09Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Fri Nov 22, 2024 at 9:58 AM CST, Phillip Wood wrote:\n> On 01/11/2024 04:39, Caleb White wrote:\n>> This refactors `repair_worktree_after_gitdir_move()` to use the new\n>> `write_worktree_linking_files` function. It also preserves the\n>> relativity of the linking files; e.g., if an existing worktree used\n>> absolute paths then the repaired paths will be absolute (and visa-versa).\n>\n> It would be helpful to give a brief explanation of what this function is\n> used for as it seems to be called from setup.c rather than as part of\n> \"git worktree repair\"\n\nI updated the commit message to include a brief explanation of what the\nfunction does.\n\n>> This also adds a test case for reinitializing a repository that has\n>> relative worktrees.\n>\n> I'm not sure exactly what this is doing - are we breaking something and\n> then calling \"git init\" to repair it?\n\nApparently (I didn't know this either, and this is likely a rare use-case),\n`git init` allows you to \"re-initialize\" an existing repository which\ncan move the .git directory. After which, git would just repair all the\nworktrees. This worked fine for absolute paths as the `gitdir` files\nstill contained the absolute paths. However, for relative paths, both\nof the linking files need to be updated (and I need to know the original\nlocation of the `.git` directory to do so). This function was created\nin the previous topic to handle this case.\n\n> My understanding of what this function is trying to do is rather limited\n> but as far as I can see the conversion looks OK though the strbuf\n> changes are quite distracting.\n\nYeah, I was able to clean up a lot because of the new `write_worktree_linking_files()`\nfunction.\n\n>> +++ b/t/t0001-init.sh\n>> @@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n>>   sep_git_dir_worktree ()  {\n>>   \ttest_when_finished \"rm -rf mainwt linkwt seprepo\" &&\n>>   \tgit init mainwt &&\n>> +\tif test \"relative\" = $2\n>> +\tthen\n>> +\t\tgit -C mainwt config worktree.useRelativePaths true\n>\n> test_config is your friend here (it accepts -C <repo>)\n\nUpdated, thanks!\n\nBest,\n\nCaleb\n\n"},{"id":"507971","messageId":"D5TA5KT15GQL.1YOJ4I225DJNG@pm.me","threadId":"62433","inReplyTo":"7ddddf98-840d-4d36-b3bb-6d5265a1362f@gmail.com","subject":"Re: [PATCH v4 5/8] worktree: add relative cli/config options to `add` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-23T04:40:33Z","receivedAt":"2024-11-23T04:40:55Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Fri Nov 22, 2024 at 10:44 AM CST, phillip.wood123 wrote:\n> On 20/11/2024 05:01, Caleb White wrote:\n>> On Tue Nov 19, 2024 at 9:07 AM CST, Phillip Wood wrote:\n>>\n>>>> +--[no-]relative-paths::\n>>>> +\tOverrides the `worktree.useRelativePaths` config option, see\n>>>> +\tlinkgit:git-config[1].\n>>>\n>>> I think it would be helpful to describe what this option does directly,\n>>> rather than in terms of a config setting which is documented elsewhere\n>>> and that may not be set\n>>\n>> I originally had a description here, but it was requested by another\n>> reviewer that I remove the duplicate information and simply reference\n>> the config option. I don't mind which direction we go, but we need\n>> some consensus on this.\n>\n> Oh right, from a user's point of view describing an option in terms of a\n> config option that may not be set seems a bit confusing. Looking at the\n> man page we describe --[no-]guess-remote and then mention the config\n> option rather than just referring the reader to the config man page.\n\nI agree. I'll add a short description of the option here. Thanks for the\nfeedback!\n\nBest,\n\nCaleb\n\n"},{"id":"507972","messageId":"D5TBGJS8FWBE.3QYEZWZUS0C71@pm.me","threadId":"62433","inReplyTo":"135739ad-6722-449b-9f9b-31c0bbc9f9cb@gmail.com","subject":"Re: [PATCH v4 7/8] worktree: add relative cli/config options to `repair` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-23T05:41:55Z","receivedAt":"2024-11-23T05:42:09Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Fri Nov 22, 2024 at 9:55 AM CST, Phillip Wood wrote:\n> On 01/11/2024 04:38, Caleb White wrote:\n>> +test_expect_success 'repair relative worktree to use absolute paths' '\n>> +\ttest_when_finished \"rm -rf main side sidemoved\" &&\n>> +\ttest_create_repo main &&\n>> +\ttest_commit -C main init &&\n>> +\tgit -C main worktree add --relative-paths --detach ../side &&\n>> +\techo \"$(pwd)/sidemoved/.git\" >expect-gitdir &&\n>> +\techo \"gitdir: $(pwd)/main/.git/worktrees/side\" >expect-gitfile &&\n>> +\tmv side sidemoved &&\n>> +\tgit -C main worktree repair ../sidemoved &&\n>> +\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n>> +\ttest_cmp expect-gitfile sidemoved/.git\n>> +'\n>\n> These tests looks sensibile, we should probably check that \"git worktree\n> repair\" repects worktree.userelativepaths. I wonder if we have any\n> coverage of repair_worktrees() as I think in these tests the problem is\n> fixed by repair_worktree_at_path() before we call repair_worktrees().\n\nI'll add a test case for this.\n\n>>   test_done\n>> diff --git a/worktree.c b/worktree.c\n>> index 6b640cd9549ecb060236f7eddf1390caa181f1a0..2cb994ac462debf966ac51b5a4f33c30cfebd4ef 100644\n>> --- a/worktree.c\n>> +++ b/worktree.c\n>> @@ -574,12 +574,14 @@ int other_head_refs(each_ref_fn fn, void *cb_data)\n>>    * pointing at <repo>/worktrees/<id>.\n>>    */\n>>   static void repair_gitfile(struct worktree *wt,\n>> -\t\t\t   worktree_repair_fn fn, void *cb_data)\n>> +\t\t\t   worktree_repair_fn fn,\n>> +\t\t\t   void *cb_data,\n>\n> Style wise leaving \"fn\" and \"cb_data\" on the same line would be fine.\n> That applies to all the functions.\n\nAh, good to know---I'm used to every argument being on its own line but\nI can revert.\n\n>> +\telse if (use_relative_paths == is_absolute_path(dotgit_contents))\n>> +\t\trepair = _(\".git file absolute/relative path mismatch\");\n>\n> Comparing ints as booleans makes me nervous in case we have a non-zero\n> value that isn't 1 but is_absolute_path() returns 0 or 1 and we know\n> use_relative_paths is 0 or 1.\n\nYes, both are either 0 or 1, so this should be safe.\n\n>>   \tif (repair) {\n>>   \t\tfn(0, wt->path, repair, cb_data);\n>> -\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, wt->path, &tmp));\n>> +\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n>\n> We used to update only the \".git\", now we'll update both. In the case\n> where we're changing to/from absolute/relative paths that's good because\n> we'll update the \"gitdir\" file as well. In the other cases it looks like\n> we've we've found this worktree via the \"gitdir\" file so it should be\n> safe to write the same value back to that file.\n\nYes, there is an edge case that a file is written with the same (correct)\ncontents, but I think this is acceptable given that it would be more\ncomplicated to check if the contents are the same before writing (which\nwould involve reading the file).\n\n>> [...]\n>>   void repair_worktree_at_path(const char *path,\n>> -\t\t\t     worktree_repair_fn fn, void *cb_data)\n>> +\t\t\t     worktree_repair_fn fn,\n>> +\t\t\t     void *cb_data,\n>> +\t\t\t     int use_relative_paths)\n>>   {\n>>   \tstruct strbuf dotgit = STRBUF_INIT;\n>> -\tstruct strbuf realdotgit = STRBUF_INIT;\n>>   \tstruct strbuf backlink = STRBUF_INIT;\n>>   \tstruct strbuf inferred_backlink = STRBUF_INIT;\n>>   \tstruct strbuf gitdir = STRBUF_INIT;\n>>   \tstruct strbuf olddotgit = STRBUF_INIT;\n>> -\tstruct strbuf realolddotgit = STRBUF_INIT;\n>> -\tstruct strbuf tmp = STRBUF_INIT;\n>>\n>>   \tchar *dotgit_contents = NULL;\n>>   \tconst char *repair = NULL;\n>>   \tint err;\n>> @@ -779,25 +783,25 @@ void repair_worktree_at_path(const char *path,\n>>   \t\tgoto done;\n>>\n>>   \tstrbuf_addf(&dotgit, \"%s/.git\", path);\n>> -\tif (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {\n>> +\tif (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {\n>\n> This works because strbuf_realpath() copies dotgit.buf before it resets\n> dotgit but that does not seem to be documented and looking at the output of\n>\n>      git grep strbuf_realpath | grep \\\\.buf\n>\n> I don't see any other callers relying on this outside of your earlier\n> changes to this file. Given that I wonder if we should leave it as is\n> which would also simplify this patch as the interesting changes are\n> swamped by the strbuf tweaking.\n\nI'd like to keep this if that's okay. All of the strbuf tweaking was\nso that way there is a consistency the variable names across the\nimplementations---all the calls to `write_worktree_linking_files()` use\n`dotgit` and `gitdir` as the strubuf names so it should be easier to\nfollow now.\n\nBest,\n\nCaleb\n\n"},{"id":"507973","messageId":"D5TBIXHUMTGL.16V33H0O4GTWT@pm.me","threadId":"62433","inReplyTo":"db700d82-c775-4e0d-a171-9caf6489a3d8@gmail.com","subject":"Re: [PATCH v4 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-23T05:45:00Z","receivedAt":"2024-11-23T05:45:21Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Fri Nov 22, 2024 at 9:57 AM CST, Phillip Wood wrote:\n> Thanks for working on this, I've left a few comments on the tests and in\n> the last couple of patches there is some distracting re-factoring of how\n> which strbufs be use but fundamentally this series looks sound to me. We\n> should certainly try and get this merged before the next release.\n\nThank you sir! I appreciate the feedback and your time---I will re-roll soon.\n\nBest,\n\nCaleb\n\n"},{"id":"507982","messageId":"60325a17-82e3-4a4c-a6fd-d3b597f1c2bc@gmail.com","threadId":"62433","inReplyTo":"D5TBGJS8FWBE.3QYEZWZUS0C71@pm.me","subject":"Re: [PATCH v4 7/8] worktree: add relative cli/config options to `repair` command","fromName":"","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-24T19:27:32Z","receivedAt":"2024-11-24T19:27:35Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 23/11/2024 05:41, Caleb White wrote:\n> On Fri Nov 22, 2024 at 9:55 AM CST, Phillip Wood wrote:\n>> On 01/11/2024 04:38, Caleb White wrote:\n>> We used to update only the \".git\", now we'll update both. In the case\n>> where we're changing to/from absolute/relative paths that's good because\n>> we'll update the \"gitdir\" file as well. In the other cases it looks like\n>> we've we've found this worktree via the \"gitdir\" file so it should be\n>> safe to write the same value back to that file.\n> \n> Yes, there is an edge case that a file is written with the same (correct)\n> contents, but I think this is acceptable given that it would be more\n> complicated to check if the contents are the same before writing (which\n> would involve reading the file).\n\nThat's fine - it might be worth explaining it in the commit message \nthough (the same goes for the other patches where we start writing both \nfiles instead of one).\n\n>>>    \tstrbuf_addf(&dotgit, \"%s/.git\", path);\n>>> -\tif (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {\n>>> +\tif (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {\n>>\n>> This works because strbuf_realpath() copies dotgit.buf before it resets\n>> dotgit but that does not seem to be documented and looking at the output of\n>>\n>>       git grep strbuf_realpath | grep \\\\.buf\n>>\n>> I don't see any other callers relying on this outside of your earlier\n>> changes to this file. Given that I wonder if we should leave it as is\n>> which would also simplify this patch as the interesting changes are\n>> swamped by the strbuf tweaking.\n> \n> I'd like to keep this if that's okay.\n\nLet's leave it as is and see what Junio thinks - my worry is that if \nstrbuf_realpath() gets refactored in the future it could break this code.\n\n> All of the strbuf tweaking was\n> so that way there is a consistency the variable names across the\n> implementations---all the calls to `write_worktree_linking_files()` use\n> `dotgit` and `gitdir` as the strubuf names so it should be easier to\n> follow now.\n\nYes the end state is nice, I just found it a bit confusing getting there.\n\nBest Wishes\n\nPhillip\n\n> Best,\n> \n> Caleb\n> \n\n"},{"id":"508118","messageId":"D5VO2YCU3DM3.2DS3BX0F0NDW6@pm.me","threadId":"62433","inReplyTo":"60325a17-82e3-4a4c-a6fd-d3b597f1c2bc@gmail.com","subject":"Re: [PATCH v4 7/8] worktree: add relative cli/config options to `repair` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T00:00:43Z","receivedAt":"2024-11-26T00:00:49Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Sun Nov 24, 2024 at 1:27 PM CST, phillip.wood123 wrote:\n> On 23/11/2024 05:41, Caleb White wrote:\n>> Yes, there is an edge case that a file is written with the same (correct)\n>> contents, but I think this is acceptable given that it would be more\n>> complicated to check if the contents are the same before writing (which\n>> would involve reading the file).\n>\n> That's fine - it might be worth explaining it in the commit message\n> though (the same goes for the other patches where we start writing both\n> files instead of one).\n\nI can make a note of it in the commit messages.\n\n>>> This works because strbuf_realpath() copies dotgit.buf before it resets\n>>> dotgit but that does not seem to be documented and looking at the output of\n>>>\n>>>       git grep strbuf_realpath | grep \\\\.buf\n>>>\n>>> I don't see any other callers relying on this outside of your earlier\n>>> changes to this file. Given that I wonder if we should leave it as is\n>>> which would also simplify this patch as the interesting changes are\n>>> swamped by the strbuf tweaking.\n>>\n>> I'd like to keep this if that's okay.\n>\n> Let's leave it as is and see what Junio thinks - my worry is that if\n> strbuf_realpath() gets refactored in the future it could break this code.\n\nThe tests will catch any breakages during a refactor, so I'm not too\nworried about that.\n\n\nBest,\n\nCaleb\n\n"},{"id":"508123","messageId":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me","subject":"[PATCH v5 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:51:31Z","receivedAt":"2024-11-26T01:51:37Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This series introduces the `--[no-]relative-paths` CLI option for\n`git worktree {add, move, repair}` commands, as well as the\n`worktree.useRelativePaths` configuration setting. When enabled,\nthese options allow worktrees to be linked using relative paths,\nenhancing portability across environments where absolute paths\nmay differ (e.g., containerized setups, shared network drives).\nGit still creates absolute paths by default, but these options allow\nusers to opt-in to relative paths if desired.\n\nUsing the `--relative-paths` option with `worktree {move, repair}`\nwill convert absolute paths to relative ones, while `--no-relative-paths`\ndoes the reverse. For cases where users want consistency in path handling,\nthe config option `worktree.useRelativePaths` provides a persistent setting.\n\nA new extension, `relativeWorktrees`, is added to indicate that at least\none worktree in the repository has been linked with relative paths. This\nextension is automatically set when a worktree is created or repaired\nusing the `--relative-paths` option, or when the\n`worktree.useRelativePaths` config is set to `true`.\n\nThe `relativeWorktrees` extension ensures older Git versions do not\nattempt to automatically prune worktrees with relative paths, as they\nwould not not recognize the paths as being valid.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\nThe base for this patch series is 090d24e9af.\n\nLink to original patch series:\nhttps://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n\n---\nChanges in v5:\n- Added docs to `--relative-paths` option.\n- Added test coverage for `repair_worktrees()` and relative paths.\n- Move `strbuf_reset` call in `infer_backlink()`.\n- Cleaned up tests.\n- Slight stylistic changes.\n- Tweaked commit messages.\n- Updated base to 090d24e9af.\n- Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me\nChanges in v4:\n- Fixed failing test in ci\n- Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me\nChanges in v3:\n- Split patches into smaller edits.\n- Moved tests into the patches with the relevant code changes.\n- Removed global `use_relative_paths` and instead pass parameter to functions.\n- Changed `infer_backlink` return type from `int` to `ssize_t`.\n- Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.\n- Reordered patches\n- Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me\nChanges in v2:\n- Fixed a bug where repositories with valid extensions would be downgraded\n  to v0 during reinitialization, causing future operations to fail.\n- Split patch [1/2] into three separate patches.\n- Updated cover letter and commit messages.\n- Updated documentation wording.\n- Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me\n\n---\nCaleb White (8):\n      setup: correctly reinitialize repository version\n      worktree: add `relativeWorktrees` extension\n      worktree: refactor infer_backlink return\n      worktree: add `write_worktree_linking_files()` function\n      worktree: add relative cli/config options to `add` command\n      worktree: add relative cli/config options to `move` command\n      worktree: add relative cli/config options to `repair` command\n      worktree: refactor `repair_worktree_after_gitdir_move()`\n\n Documentation/config/extensions.txt |   6 ++\n Documentation/config/worktree.txt   |  10 +++\n Documentation/git-worktree.txt      |   8 ++\n builtin/worktree.c                  |  29 ++++---\n repository.c                        |   1 +\n repository.h                        |   1 +\n setup.c                             |  39 ++++++---\n setup.h                             |   1 +\n t/t0001-init.sh                     |  22 ++++-\n t/t2400-worktree-add.sh             |  45 +++++++++++\n t/t2401-worktree-prune.sh           |   3 +-\n t/t2402-worktree-list.sh            |  22 +++++\n t/t2403-worktree-move.sh            |  25 ++++++\n t/t2406-worktree-repair.sh          |  39 +++++++++\n t/t2408-worktree-relative.sh        |  39 ---------\n t/t5504-fetch-receive-strict.sh     |   6 +-\n worktree.c                          | 157 ++++++++++++++++++++----------------\n worktree.h                          |  22 ++++-\n 18 files changed, 333 insertions(+), 142 deletions(-)\n---\nbase-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559\nchange-id: 20241025-wt_relative_options-afa41987bc32\n\nBest regards,\n-- \nCaleb White <cdwhite3@pm.me>\n\n\n"},{"id":"508124","messageId":"20241125-wt_relative_options-v5-1-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 1/8] setup: correctly reinitialize repository version","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:51:38Z","receivedAt":"2024-11-26T01:51:47Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"When reinitializing a repository, Git does not account for extensions\nother than `objectformat` and `refstorage` when determining the\nrepository version. This can lead to a repository being downgraded to\nversion 0 if extensions are set, causing Git future operations to fail.\n\nThis patch teaches Git to check if other extensions are defined in the\nconfig to ensure that the repository version is set correctly.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n setup.c                         | 32 +++++++++++++++++++++++---------\n t/t5504-fetch-receive-strict.sh |  6 +++---\n 2 files changed, 26 insertions(+), 12 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,\n \t\t\t\t   enum ref_storage_format ref_storage_format,\n \t\t\t\t   int reinit)\n {\n-\tchar repo_version_string[10];\n-\tint repo_version = GIT_REPO_VERSION;\n+\tstruct strbuf repo_version = STRBUF_INIT;\n+\tint target_version = GIT_REPO_VERSION;\n \n \t/*\n \t * Note that we initialize the repository version to 1 when the ref\n@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,\n \t */\n \tif (hash_algo != GIT_HASH_SHA1 ||\n \t    ref_storage_format != REF_STORAGE_FORMAT_FILES)\n-\t\trepo_version = GIT_REPO_VERSION_READ;\n-\n-\t/* This forces creation of new config file */\n-\txsnprintf(repo_version_string, sizeof(repo_version_string),\n-\t\t  \"%d\", repo_version);\n-\tgit_config_set(\"core.repositoryformatversion\", repo_version_string);\n+\t\ttarget_version = GIT_REPO_VERSION_READ;\n \n \tif (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)\n \t\tgit_config_set(\"extensions.objectformat\",\n@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,\n \t\t\t       ref_storage_format_to_name(ref_storage_format));\n \telse if (reinit)\n \t\tgit_config_set_gently(\"extensions.refstorage\", NULL);\n+\n+\tif (reinit) {\n+\t\tstruct strbuf config = STRBUF_INIT;\n+\t\tstruct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;\n+\n+\t\tstrbuf_git_common_path(&config, the_repository, \"config\");\n+\t\tread_repository_format(&repo_fmt, config.buf);\n+\n+\t\tif (repo_fmt.v1_only_extensions.nr)\n+\t\t\ttarget_version = GIT_REPO_VERSION_READ;\n+\n+\t\tstrbuf_release(&config);\n+\t\tclear_repository_format(&repo_fmt);\n+\t}\n+\n+\tstrbuf_addf(&repo_version, \"%d\", target_version);\n+\tgit_config_set(\"core.repositoryformatversion\", repo_version.buf);\n+\n+\tstrbuf_release(&repo_version);\n }\n \n static int is_reinit(void)\n@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,\n \t\tadjust_shared_perm(repo_get_git_dir(the_repository));\n \t}\n \n-\tinitialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);\n+\tinitialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);\n \n \t/* Check filemode trustability */\n \tpath = git_path_buf(&buf, \"config\");\ndiff --git a/t/t5504-fetch-receive-strict.sh b/t/t5504-fetch-receive-strict.sh\nindex 138e6778a477650ecbe2dc3e480c5fe83d4bb485..290d2a591adae02acf5bcf24dbbff2a0bbfceac8 100755\n--- a/t/t5504-fetch-receive-strict.sh\n+++ b/t/t5504-fetch-receive-strict.sh\n@@ -171,7 +171,7 @@ test_expect_success 'fsck with invalid or bogus skipList input' '\n \ttest_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \ttest_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err\n+\ttest_grep \"invalid object name: \" err\n '\n \n test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '\n@@ -234,7 +234,7 @@ test_expect_success 'push with receive.fsck.skipList' '\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \tgit --git-dir=dst/.git config receive.fsck.skipList config &&\n \ttest_must_fail git push --porcelain dst bogus 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err &&\n+\ttest_grep \"invalid object name: \" err &&\n \n \tgit --git-dir=dst/.git config receive.fsck.skipList SKIP &&\n \tgit push --porcelain dst bogus\n@@ -263,7 +263,7 @@ test_expect_success 'fetch with fetch.fsck.skipList' '\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \tgit --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&\n \ttest_must_fail git --git-dir=dst/.git fetch \"file://$(pwd)\" $refspec 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err &&\n+\ttest_grep \"invalid object name: \" err &&\n \n \tgit --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&\n \tgit --git-dir=dst/.git fetch \"file://$(pwd)\" $refspec\n\n-- \n2.47.0\n\n\n"},{"id":"508125","messageId":"20241125-wt_relative_options-v5-2-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 2/8] worktree: add `relativeWorktrees` extension","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:51:44Z","receivedAt":"2024-11-26T01:51:55Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"A new extension, `relativeWorktrees`, is added to indicate that at least\none worktree in the repository has been linked with relative paths.\nThis ensures older Git versions do not attempt to automatically prune\nworktrees with relative paths, as they would not not recognize the\npaths as being valid.\n\nSuggested-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/config/extensions.txt | 6 ++++++\n repository.c                        | 1 +\n repository.h                        | 1 +\n setup.c                             | 7 +++++++\n setup.h                             | 1 +\n 5 files changed, 16 insertions(+)\n\ndiff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt\nindex 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644\n--- a/Documentation/config/extensions.txt\n+++ b/Documentation/config/extensions.txt\n@@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or\n linkgit:git-clone[1]. Trying to change it after initialization will not\n work and will produce hard-to-diagnose issues.\n \n+relativeWorktrees::\n+\tIf enabled, indicates at least one worktree has been linked with\n+\trelative paths. Automatically set if a worktree has been created or\n+\trepaired with either the `--relative-paths` option or with the\n+\t`worktree.useRelativePaths` config set to `true`.\n+\n worktreeConfig::\n \tIf enabled, then worktrees will load config settings from the\n \t`$GIT_DIR/config.worktree` file in addition to the\ndiff --git a/repository.c b/repository.c\nindex f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644\n--- a/repository.c\n+++ b/repository.c\n@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,\n \trepo_set_compat_hash_algo(repo, format.compat_hash_algo);\n \trepo_set_ref_storage_format(repo, format.ref_storage_format);\n \trepo->repository_format_worktree_config = format.worktree_config;\n+\trepo->repository_format_relative_worktrees = format.relative_worktrees;\n \n \t/* take ownership of format.partial_clone */\n \trepo->repository_format_partial_clone = format.partial_clone;\ndiff --git a/repository.h b/repository.h\nindex 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -150,6 +150,7 @@ struct repository {\n \n \t/* Configurations */\n \tint repository_format_worktree_config;\n+\tint repository_format_relative_worktrees;\n \n \t/* Indicate if a repository has a different 'commondir' from 'gitdir' */\n \tunsigned different_commondir:1;\ndiff --git a/setup.c b/setup.c\nindex 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var,\n \t\t\t\t     \"extensions.refstorage\", value);\n \t\tdata->ref_storage_format = format;\n \t\treturn EXTENSION_OK;\n+\t} else if (!strcmp(ext, \"relativeworktrees\")) {\n+\t\tdata->relative_worktrees = git_config_bool(var, value);\n+\t\treturn EXTENSION_OK;\n \t}\n \treturn EXTENSION_UNKNOWN;\n }\n@@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\t\t\t\t\t    repo_fmt.ref_storage_format);\n \t\t\tthe_repository->repository_format_worktree_config =\n \t\t\t\trepo_fmt.worktree_config;\n+\t\t\tthe_repository->repository_format_relative_worktrees =\n+\t\t\t\trepo_fmt.relative_worktrees;\n \t\t\t/* take ownership of repo_fmt.partial_clone */\n \t\t\tthe_repository->repository_format_partial_clone =\n \t\t\t\trepo_fmt.partial_clone;\n@@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt)\n \t\t\t\t    fmt->ref_storage_format);\n \tthe_repository->repository_format_worktree_config =\n \t\tfmt->worktree_config;\n+\tthe_repository->repository_format_relative_worktrees =\n+\t\tfmt->relative_worktrees;\n \tthe_repository->repository_format_partial_clone =\n \t\txstrdup_or_null(fmt->partial_clone);\n \tclear_repository_format(&repo_fmt);\ndiff --git a/setup.h b/setup.h\nindex e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644\n--- a/setup.h\n+++ b/setup.h\n@@ -129,6 +129,7 @@ struct repository_format {\n \tint precious_objects;\n \tchar *partial_clone; /* value of extensions.partialclone */\n \tint worktree_config;\n+\tint relative_worktrees;\n \tint is_bare;\n \tint hash_algo;\n \tint compat_hash_algo;\n\n-- \n2.47.0\n\n\n"},{"id":"508126","messageId":"20241125-wt_relative_options-v5-3-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 3/8] worktree: refactor infer_backlink return","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:51:50Z","receivedAt":"2024-11-26T01:51:58Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"The previous round[1] was merged a bit early before reviewer feedback\ncould be applied. This correctly indents a code block and updates the\n`infer_backlink` function to return `-1` on failure and strbuf.len on\nsuccess.\n\n[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n worktree.c | 15 ++++++++-------\n 1 file changed, 8 insertions(+), 7 deletions(-)\n\ndiff --git a/worktree.c b/worktree.c\nindex 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..afde5394c8760c328f12aa321be3ac5c199cc0f1 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id,\n \tstrbuf_strip_suffix(&worktree_path, \"/.git\");\n \n \tif (!is_absolute_path(worktree_path.buf)) {\n-\t    strbuf_strip_suffix(&path, \"gitdir\");\n-\t    strbuf_addbuf(&path, &worktree_path);\n-\t    strbuf_realpath_forgiving(&worktree_path, path.buf, 0);\n+\t\tstrbuf_strip_suffix(&path, \"gitdir\");\n+\t\tstrbuf_addbuf(&path, &worktree_path);\n+\t\tstrbuf_realpath_forgiving(&worktree_path, path.buf, 0);\n \t}\n \n \tCALLOC_ARRAY(worktree, 1);\n@@ -725,8 +725,10 @@ static int is_main_worktree_path(const char *path)\n  * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may\n  * be able to infer the gitdir by manually reading /path/to/worktree/.git,\n  * extracting the <id>, and checking if <repo>/worktrees/<id> exists.\n+ *\n+ * Returns -1 on failure and strbuf.len on success.\n  */\n-static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n+static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n {\n \tstruct strbuf actual = STRBUF_INIT;\n \tconst char *id;\n@@ -747,12 +749,11 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n \t\tgoto error;\n \n \tstrbuf_release(&actual);\n-\treturn 1;\n-\n+\treturn inferred->len;\n error:\n \tstrbuf_release(&actual);\n \tstrbuf_reset(inferred); /* clear invalid path */\n-\treturn 0;\n+\treturn -1;\n }\n \n /*\n\n-- \n2.47.0\n\n\n"},{"id":"508127","messageId":"20241125-wt_relative_options-v5-4-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 4/8] worktree: add `write_worktree_linking_files()` function","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:51:58Z","receivedAt":"2024-11-26T01:52:06Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"A new helper function, `write_worktree_linking_files()`, centralizes\nthe logic for computing and writing either relative or absolute\npaths, based on the provided configuration. This function accepts\n`strbuf` pointers to both the worktree’s `.git` link and the\nrepository’s `gitdir`, and then writes the appropriate path to each.\nThe `relativeWorktrees` extension is automatically set when a worktree\nis linked with relative paths.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n worktree.c | 35 +++++++++++++++++++++++++++++++++++\n worktree.h | 13 +++++++++++++\n 2 files changed, 48 insertions(+)\n\ndiff --git a/worktree.c b/worktree.c\nindex afde5394c8760c328f12aa321be3ac5c199cc0f1..cf05045cc973f121a0a76b5ccfa731acf25d1a73 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -1032,3 +1032,38 @@ int init_worktree_config(struct repository *r)\n \tfree(main_worktree_file);\n \treturn res;\n }\n+\n+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,\n+\t\t\t\t  int use_relative_paths)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct strbuf repo = STRBUF_INIT;\n+\tstruct strbuf tmp = STRBUF_INIT;\n+\n+\tstrbuf_addbuf(&path, &dotgit);\n+\tstrbuf_strip_suffix(&path, \"/.git\");\n+\tstrbuf_realpath(&path, path.buf, 1);\n+\tstrbuf_addbuf(&repo, &gitdir);\n+\tstrbuf_strip_suffix(&repo, \"/gitdir\");\n+\tstrbuf_realpath(&repo, repo.buf, 1);\n+\n+\tif (use_relative_paths && !the_repository->repository_format_relative_worktrees) {\n+\t\tif (upgrade_repository_format(1) < 0)\n+\t\t\tdie(_(\"unable to upgrade repository format to support relative worktrees\"));\n+\t\tif (git_config_set_gently(\"extensions.relativeWorktrees\", \"true\"))\n+\t\t\tdie(_(\"unable to set extensions.relativeWorktrees setting\"));\n+\t\tthe_repository->repository_format_relative_worktrees = 1;\n+\t}\n+\n+\tif (use_relative_paths) {\n+\t\twrite_file(gitdir.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n+\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n+\t} else {\n+\t\twrite_file(gitdir.buf, \"%s/.git\", path.buf);\n+\t\twrite_file(dotgit.buf, \"gitdir: %s\", repo.buf);\n+\t}\n+\n+\tstrbuf_release(&path);\n+\tstrbuf_release(&repo);\n+\tstrbuf_release(&tmp);\n+}\ndiff --git a/worktree.h b/worktree.h\nindex e96118621638667d87c5d7e0452ed10bd1ddf606..fd040f5d999697b603df929679bdddd2ff7f6eea 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -215,4 +215,17 @@ void strbuf_worktree_ref(const struct worktree *wt,\n  */\n int init_worktree_config(struct repository *r);\n \n+/**\n+ * Write the .git file and gitdir file that links the worktree to the repository.\n+ *\n+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`\n+ * is the path to the repository's `gitdir` file.\n+ *\n+ * Example\n+ *  dotgit: \"/path/to/foo/.git\"\n+ *  gitdir: \"/path/to/repo/worktrees/foo/gitdir\"\n+ */\n+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,\n+\t\t\t\t  int use_relative_paths);\n+\n #endif\n\n-- \n2.47.0\n\n\n"},{"id":"508128","messageId":"20241125-wt_relative_options-v5-5-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 5/8] worktree: add relative cli/config options to `add` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:52:06Z","receivedAt":"2024-11-26T01:52:18Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This introduces the `--[no-]relative-paths` CLI option and\n`worktree.useRelativePaths` configuration setting to the `worktree add`\ncommand. When enabled these options allow worktrees to be linked using\nrelative paths, enhancing portability across environments where absolute\npaths may differ (e.g., containerized setups, shared network drives).\nGit still creates absolute paths by default, but these options allow\nusers to opt-in to relative paths if desired.\n\nThe t2408 test file is removed and more comprehensive tests are\nwritten for the various worktree operations in their own files.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/config/worktree.txt | 10 +++++++++\n Documentation/git-worktree.txt    |  5 +++++\n builtin/worktree.c                | 19 +++++++++--------\n t/t2400-worktree-add.sh           | 45 +++++++++++++++++++++++++++++++++++++++\n t/t2401-worktree-prune.sh         |  3 ++-\n t/t2402-worktree-list.sh          | 22 +++++++++++++++++++\n t/t2408-worktree-relative.sh      | 39 ---------------------------------\n 7 files changed, 94 insertions(+), 49 deletions(-)\n\ndiff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt\nindex 048e349482df6c892055720eb53cdcd6c327b6ed..5e35c7d018aecdedca0642b11e45df6d19024d42 100644\n--- a/Documentation/config/worktree.txt\n+++ b/Documentation/config/worktree.txt\n@@ -7,3 +7,13 @@ worktree.guessRemote::\n \tsuch a branch exists, it is checked out and set as \"upstream\"\n \tfor the new branch.  If no such match can be found, it falls\n \tback to creating a new branch from the current HEAD.\n+\n+worktree.useRelativePaths::\n+\tLink worktrees using relative paths (when \"true\") or absolute\n+\tpaths (when \"false\"). This is particularly useful for setups\n+\twhere the repository and worktrees may be moved between\n+\tdifferent locations or environments. Defaults to \"false\".\n++\n+Note that setting `worktree.useRelativePaths` to \"true\" implies enabling the\n+`extension.relativeWorktrees` config (see linkgit:git-config[1]),\n+thus making it incompatible with older versions of Git.\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 70437c815f13852bd2eb862176b8b933e6de0acf..60a671bbc255aa7763b4f77511c09f6a02783e00 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -216,6 +216,11 @@ To remove a locked worktree, specify `--force` twice.\n This can also be set up as the default behaviour by using the\n `worktree.guessRemote` config option.\n \n+--[no-]relative-paths::\n+\tLink worktrees using relative paths or absolute paths (default).\n+\tOverrides the `worktree.useRelativePaths` config option, see\n+\tlinkgit:git-config[1].\n+\n --[no-]track::\n \tWhen creating a new branch, if `<commit-ish>` is a branch,\n \tmark it as \"upstream\" from the new branch.  This is the\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -120,12 +120,14 @@ struct add_opts {\n \tint quiet;\n \tint checkout;\n \tint orphan;\n+\tint relative_paths;\n \tconst char *keep_locked;\n };\n \n static int show_only;\n static int verbose;\n static int guess_remote;\n+static int use_relative_paths;\n static timestamp_t expire;\n \n static int git_worktree_config(const char *var, const char *value,\n@@ -134,6 +136,9 @@ static int git_worktree_config(const char *var, const char *value,\n \tif (!strcmp(var, \"worktree.guessremote\")) {\n \t\tguess_remote = git_config_bool(var, value);\n \t\treturn 0;\n+\t} else if (!strcmp(var, \"worktree.userelativepaths\")) {\n+\t\tuse_relative_paths = git_config_bool(var, value);\n+\t\treturn 0;\n \t}\n \n \treturn git_default_config(var, value, ctx, cb);\n@@ -414,8 +419,7 @@ static int add_worktree(const char *path, const char *refname,\n \t\t\tconst struct add_opts *opts)\n {\n \tstruct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;\n-\tstruct strbuf sb = STRBUF_INIT, sb_tmp = STRBUF_INIT;\n-\tstruct strbuf sb_path_realpath = STRBUF_INIT, sb_repo_realpath = STRBUF_INIT;\n+\tstruct strbuf sb = STRBUF_INIT;\n \tconst char *name;\n \tstruct strvec child_env = STRVEC_INIT;\n \tunsigned int counter = 0;\n@@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname,\n \n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"%s/gitdir\", sb_repo.buf);\n-\tstrbuf_realpath(&sb_path_realpath, path, 1);\n-\tstrbuf_realpath(&sb_repo_realpath, sb_repo.buf, 1);\n-\twrite_file(sb.buf, \"%s/.git\", relative_path(sb_path_realpath.buf, sb_repo_realpath.buf, &sb_tmp));\n-\twrite_file(sb_git.buf, \"gitdir: %s\", relative_path(sb_repo_realpath.buf, sb_path_realpath.buf, &sb_tmp));\n+\twrite_worktree_linking_files(sb_git, sb, opts->relative_paths);\n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"%s/commondir\", sb_repo.buf);\n \twrite_file(sb.buf, \"../..\");\n@@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname,\n \n \tstrvec_clear(&child_env);\n \tstrbuf_release(&sb);\n-\tstrbuf_release(&sb_tmp);\n \tstrbuf_release(&symref);\n \tstrbuf_release(&sb_repo);\n-\tstrbuf_release(&sb_repo_realpath);\n \tstrbuf_release(&sb_git);\n-\tstrbuf_release(&sb_path_realpath);\n \tstrbuf_release(&sb_name);\n \tfree_worktree(wt);\n \treturn ret;\n@@ -796,12 +794,15 @@ static int add(int ac, const char **av, const char *prefix)\n \t\t\t     PARSE_OPT_NOARG | PARSE_OPT_OPTARG),\n \t\tOPT_BOOL(0, \"guess-remote\", &guess_remote,\n \t\t\t N_(\"try to match the new branch name with a remote-tracking branch\")),\n+\t\tOPT_BOOL(0, \"relative-paths\", &opts.relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tint ret;\n \n \tmemset(&opts, 0, sizeof(opts));\n \topts.checkout = 1;\n+\topts.relative_paths = use_relative_paths;\n \tac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);\n \tif (!!opts.detach + !!new_branch + !!new_branch_force > 1)\n \t\tdie(_(\"options '%s', '%s', and '%s' cannot be used together\"), \"-b\", \"-B\", \"--detach\");\ndiff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\nindex cfc4aeb1798c6d023909cec771e5b74e983af5ea..fc1a909123e7932194ac433d6b5e194a26ed8233 100755\n--- a/t/t2400-worktree-add.sh\n+++ b/t/t2400-worktree-add.sh\n@@ -1207,4 +1207,49 @@ test_expect_success '\"add\" with initialized submodule, with submodule.recurse se\n \tgit -C project-clone -c submodule.recurse worktree add ../project-5\n '\n \n+test_expect_success 'can create worktrees with relative paths' '\n+\ttest_when_finished \"git worktree remove relative\" &&\n+\ttest_config worktree.useRelativePaths false &&\n+\tgit worktree add --relative-paths ./relative &&\n+\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n+\ttest_cmp expect relative/.git &&\n+\techo \"../../../relative/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/relative/gitdir\n+'\n+\n+test_expect_success 'can create worktrees with absolute paths' '\n+\ttest_config worktree.useRelativePaths true &&\n+\tgit worktree add ./relative &&\n+\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n+\ttest_cmp expect relative/.git &&\n+\tgit worktree add --no-relative-paths ./absolute &&\n+\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect absolute/.git &&\n+\techo \"$(pwd)/absolute/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir\n+'\n+\n+test_expect_success 'move repo without breaking relative internal links' '\n+\ttest_when_finished rm -rf repo moved &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\tgit worktree add --relative-paths wt1 &&\n+\t\tcd .. &&\n+\t\tmv repo moved &&\n+\t\tcd moved/wt1 &&\n+\t\tgit worktree list >out 2>err &&\n+\t\ttest_must_be_empty err\n+\t)\n+'\n+\n+test_expect_success 'relative worktree sets extension config' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\tgit -C repo commit --allow-empty -m base &&\n+\tgit -C repo worktree add --relative-paths ./foo &&\n+\ttest_cmp_config -C repo 1 core.repositoryformatversion\n+'\n+\n test_done\ndiff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh\nindex 976d048e3efc74be9cd909ce76d552b3944d2e10..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755\n--- a/t/t2401-worktree-prune.sh\n+++ b/t/t2401-worktree-prune.sh\n@@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' '\n \t! test -d .git/worktrees/wt\n '\n \n-test_expect_success 'not prune proper worktrees when run inside linked worktree' '\n+test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '\n \ttest_when_finished rm -rf repo wt_ext &&\n \tgit init repo &&\n \t(\n \t    cd repo &&\n+\t    git config worktree.useRelativePaths true &&\n \t    echo content >file &&\n \t    git add file &&\n \t    git commit -m msg &&\ndiff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh\nindex 33ea9cb21ba07c9563530b54da06753eaa993fe2..780daa6cd6351f8fa9434619cc212aade8f01420 100755\n--- a/t/t2402-worktree-list.sh\n+++ b/t/t2402-worktree-list.sh\n@@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' '\n '\n \n test_expect_success 'linked worktrees are sorted' '\n+\ttest_when_finished \"rm -rf sorted\" &&\n \tmkdir sorted &&\n \tgit init sorted/main &&\n \t(\n@@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' '\n \ttest_cmp expected sorted/main/actual\n '\n \n+test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '\n+\ttest_when_finished \"rm -rf sorted\" &&\n+\tmkdir sorted &&\n+\tgit init sorted/main &&\n+\t(\n+\t\tcd sorted/main &&\n+\t\ttest_tick &&\n+\t\ttest_commit new &&\n+\t\tgit worktree add --relative-paths ../first &&\n+\t\tgit worktree add ../second &&\n+\t\tgit worktree list --porcelain >out &&\n+\t\tgrep ^worktree out >actual\n+\t) &&\n+\tcat >expected <<-EOF &&\n+\tworktree $(pwd)/sorted/main\n+\tworktree $(pwd)/sorted/first\n+\tworktree $(pwd)/sorted/second\n+\tEOF\n+\ttest_cmp expected sorted/main/actual\n+'\n+\n test_expect_success 'worktree path when called in .git directory' '\n \tgit worktree list >list1 &&\n \tgit -C .git worktree list >list2 &&\ndiff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh\ndeleted file mode 100755\nindex a3136db7e28cb20926ff44211e246ce625a6e51a..0000000000000000000000000000000000000000\n--- a/t/t2408-worktree-relative.sh\n+++ /dev/null\n@@ -1,39 +0,0 @@\n-#!/bin/sh\n-\n-test_description='test worktrees linked with relative paths'\n-\n-TEST_PASSES_SANITIZE_LEAK=true\n-. ./test-lib.sh\n-\n-test_expect_success 'links worktrees with relative paths' '\n-\ttest_when_finished rm -rf repo &&\n-\tgit init repo &&\n-\t(\n-\t\tcd repo &&\n-\t\ttest_commit initial &&\n-\t\tgit worktree add wt1 &&\n-\t\techo \"../../../wt1/.git\" >expected_gitdir &&\n-\t\tcat .git/worktrees/wt1/gitdir >actual_gitdir &&\n-\t\techo \"gitdir: ../.git/worktrees/wt1\" >expected_git &&\n-\t\tcat wt1/.git >actual_git &&\n-\t\ttest_cmp expected_gitdir actual_gitdir &&\n-\t\ttest_cmp expected_git actual_git\n-\t)\n-'\n-\n-test_expect_success 'move repo without breaking relative internal links' '\n-\ttest_when_finished rm -rf repo moved &&\n-\tgit init repo &&\n-\t(\n-\t\tcd repo &&\n-\t\ttest_commit initial &&\n-\t\tgit worktree add wt1 &&\n-\t\tcd .. &&\n-\t\tmv repo moved &&\n-\t\tcd moved/wt1 &&\n-\t\tgit status >out 2>err &&\n-\t\ttest_must_be_empty err\n-\t)\n-'\n-\n-test_done\n\n-- \n2.47.0\n\n\n"},{"id":"508129","messageId":"20241125-wt_relative_options-v5-6-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 6/8] worktree: add relative cli/config options to `move` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:52:13Z","receivedAt":"2024-11-26T01:52:22Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This teaches the `worktree move` command to respect the\n`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\nconfig setting. If an existing worktree is moved with `--relative-paths`\nthe new path will be relative (and visa-versa).\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n builtin/worktree.c       |  4 +++-\n t/t2403-worktree-move.sh | 25 +++++++++++++++++++++++++\n worktree.c               | 22 +++++++++-------------\n worktree.h               |  4 ++--\n 4 files changed, 39 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n \t\tOPT__FORCE(&force,\n \t\t\t N_(\"force move even if worktree is dirty or locked\"),\n \t\t\t PARSE_OPT_NOCOMPLETE),\n+\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tstruct worktree **worktrees, *wt;\n@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n \tif (rename(wt->path, dst.buf) == -1)\n \t\tdie_errno(_(\"failed to move '%s' to '%s'\"), wt->path, dst.buf);\n \n-\tupdate_worktree_location(wt, dst.buf);\n+\tupdate_worktree_location(wt, dst.buf, use_relative_paths);\n \n \tstrbuf_release(&dst);\n \tfree_worktrees(worktrees);\ndiff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh\nindex 901342ea09b51a8e832f1109fbb737df84283aa2..422c1a05580057b18ab8bfdfe38da4d723749493 100755\n--- a/t/t2403-worktree-move.sh\n+++ b/t/t2403-worktree-move.sh\n@@ -247,4 +247,29 @@ test_expect_success 'not remove a repo with initialized submodule' '\n \t)\n '\n \n+test_expect_success 'move worktree with absolute path to relative path' '\n+\ttest_config worktree.useRelativePaths false &&\n+\tgit worktree add ./absolute &&\n+\tgit worktree move --relative-paths absolute relative &&\n+\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect relative/.git &&\n+\techo \"../../../relative/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir &&\n+\ttest_config worktree.useRelativePaths true &&\n+\tgit worktree move relative relative2 &&\n+\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect relative2/.git &&\n+\techo \"../../../relative2/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir\n+'\n+\n+test_expect_success 'move worktree with relative path to absolute path' '\n+\ttest_config worktree.useRelativePaths true &&\n+\tgit worktree move --no-relative-paths relative2 absolute &&\n+\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect absolute/.git &&\n+\techo \"$(pwd)/absolute/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir\n+'\n+\n test_done\ndiff --git a/worktree.c b/worktree.c\nindex cf05045cc973f121a0a76b5ccfa731acf25d1a73..c749cb16994cf46ccccd4c2880ac917e497671b8 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -376,32 +376,28 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,\n \treturn ret;\n }\n \n-void update_worktree_location(struct worktree *wt, const char *path_)\n+void update_worktree_location(struct worktree *wt, const char *path_,\n+\t\t\t      int use_relative_paths)\n {\n \tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf repo = STRBUF_INIT;\n-\tstruct strbuf file = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n+\tstruct strbuf dotgit = STRBUF_INIT;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n \n \tif (is_main_worktree(wt))\n \t\tBUG(\"can't relocate main worktree\");\n \n-\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n+\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n \tstrbuf_realpath(&path, path_, 1);\n+\tstrbuf_addf(&dotgit, \"%s/.git\", path.buf);\n \tif (fspathcmp(wt->path, path.buf)) {\n-\t\tstrbuf_addf(&file, \"%s/gitdir\", repo.buf);\n-\t\twrite_file(file.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n-\t\tstrbuf_reset(&file);\n-\t\tstrbuf_addf(&file, \"%s/.git\", path.buf);\n-\t\twrite_file(file.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \n \t\tfree(wt->path);\n \t\twt->path = strbuf_detach(&path, NULL);\n \t}\n \tstrbuf_release(&path);\n-\tstrbuf_release(&repo);\n-\tstrbuf_release(&file);\n-\tstrbuf_release(&tmp);\n+\tstrbuf_release(&dotgit);\n+\tstrbuf_release(&gitdir);\n }\n \n int is_worktree_being_rebased(const struct worktree *wt,\ndiff --git a/worktree.h b/worktree.h\nindex fd040f5d999697b603df929679bdddd2ff7f6eea..9c699d080d8ebf37712044136679db3821ee1f63 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -117,8 +117,8 @@ int validate_worktree(const struct worktree *wt,\n /*\n  * Update worktrees/xxx/gitdir with the new path.\n  */\n-void update_worktree_location(struct worktree *wt,\n-\t\t\t      const char *path_);\n+void update_worktree_location(struct worktree *wt, const char *path_,\n+\t\t\t      int use_relative_paths);\n \n typedef void (* worktree_repair_fn)(int iserr, const char *path,\n \t\t\t\t    const char *msg, void *cb_data);\n\n-- \n2.47.0\n\n\n"},{"id":"508130","messageId":"20241125-wt_relative_options-v5-7-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 7/8] worktree: add relative cli/config options to `repair` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:52:21Z","receivedAt":"2024-11-26T01:52:28Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This teaches the `worktree repair` command to respect the\n`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\nconfig setting. If an existing worktree with an absolute path is repaired\nwith `--relative-paths`, the links will be replaced with relative paths,\neven if the original path was correct. This allows a user to covert\nexisting worktrees between absolute/relative as desired.\n\nTo simplify things, both linking files are written when one of the files\nneeds to be repaired. In some cases, this fixes the other file before it\nis checked, in other cases this results in a correct file being written\nwith the same contents.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/git-worktree.txt |  3 +++\n builtin/worktree.c             |  6 +++--\n t/t2406-worktree-repair.sh     | 39 +++++++++++++++++++++++++++++\n worktree.c                     | 56 ++++++++++++++++++++----------------------\n worktree.h                     |  5 ++--\n 5 files changed, 76 insertions(+), 33 deletions(-)\n\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 60a671bbc255aa7763b4f77511c09f6a02783e00..8340b7f028e6c1c3bae3de0879e9754098466d14 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -220,6 +220,9 @@ This can also be set up as the default behaviour by using the\n \tLink worktrees using relative paths or absolute paths (default).\n \tOverrides the `worktree.useRelativePaths` config option, see\n \tlinkgit:git-config[1].\n++\n+With `repair`, the linking files will be updated if there's an absolute/relative\n+mismatch, even if the links are correct.\n \n --[no-]track::\n \tWhen creating a new branch, if `<commit-ish>` is a branch,\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 302151506981718658db1cd338cd9064688f5c14..fde9ff4dc9a734c655e95ccd62774282950cbba6 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -1385,6 +1385,8 @@ static int repair(int ac, const char **av, const char *prefix)\n \tconst char **p;\n \tconst char *self[] = { \".\", NULL };\n \tstruct option options[] = {\n+\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tint rc = 0;\n@@ -1392,8 +1394,8 @@ static int repair(int ac, const char **av, const char *prefix)\n \tac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);\n \tp = ac > 0 ? av : self;\n \tfor (; *p; p++)\n-\t\trepair_worktree_at_path(*p, report_repair, &rc);\n-\trepair_worktrees(report_repair, &rc);\n+\t\trepair_worktree_at_path(*p, report_repair, &rc, use_relative_paths);\n+\trepair_worktrees(report_repair, &rc, use_relative_paths);\n \treturn rc;\n }\n \ndiff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh\nindex 7686e60f6ad186519b275f11a5e14064c905b207..49b70b999518d47e1edd72a61a847b427f4c67a1 100755\n--- a/t/t2406-worktree-repair.sh\n+++ b/t/t2406-worktree-repair.sh\n@@ -216,4 +216,43 @@ test_expect_success 'repair copied main and linked worktrees' '\n \ttest_cmp dup/linked.expect dup/linked/.git\n '\n \n+test_expect_success 'repair worktree with relative path with missing gitfile' '\n+\ttest_when_finished \"rm -rf main wt\" &&\n+\ttest_create_repo main &&\n+\tgit -C main config worktree.useRelativePaths true &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --detach ../wt &&\n+\trm wt/.git &&\n+\ttest_path_is_missing wt/.git &&\n+\tgit -C main worktree repair &&\n+\techo \"gitdir: ../main/.git/worktrees/wt\" >expect &&\n+\ttest_cmp expect wt/.git\n+'\n+\n+test_expect_success 'repair absolute worktree to use relative paths' '\n+\ttest_when_finished \"rm -rf main side sidemoved\" &&\n+\ttest_create_repo main &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --detach ../side &&\n+\techo \"../../../../sidemoved/.git\" >expect-gitdir &&\n+\techo \"gitdir: ../main/.git/worktrees/side\" >expect-gitfile &&\n+\tmv side sidemoved &&\n+\tgit -C main worktree repair --relative-paths ../sidemoved &&\n+\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n+\ttest_cmp expect-gitfile sidemoved/.git\n+'\n+\n+test_expect_success 'repair relative worktree to use absolute paths' '\n+\ttest_when_finished \"rm -rf main side sidemoved\" &&\n+\ttest_create_repo main &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --relative-paths --detach ../side &&\n+\techo \"$(pwd)/sidemoved/.git\" >expect-gitdir &&\n+\techo \"gitdir: $(pwd)/main/.git/worktrees/side\" >expect-gitfile &&\n+\tmv side sidemoved &&\n+\tgit -C main worktree repair ../sidemoved &&\n+\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n+\ttest_cmp expect-gitfile sidemoved/.git\n+'\n+\n test_done\ndiff --git a/worktree.c b/worktree.c\nindex c749cb16994cf46ccccd4c2880ac917e497671b8..2e76bbc1494afc125997f803dc39846a0b95a84f 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -573,12 +573,13 @@ int other_head_refs(each_ref_fn fn, void *cb_data)\n  * pointing at <repo>/worktrees/<id>.\n  */\n static void repair_gitfile(struct worktree *wt,\n-\t\t\t   worktree_repair_fn fn, void *cb_data)\n+\t\t\t   worktree_repair_fn fn, void *cb_data,\n+\t\t\t   int use_relative_paths)\n {\n \tstruct strbuf dotgit = STRBUF_INIT;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf repo = STRBUF_INIT;\n \tstruct strbuf backlink = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n \tchar *dotgit_contents = NULL;\n \tconst char *repair = NULL;\n \tint err;\n@@ -594,6 +595,7 @@ static void repair_gitfile(struct worktree *wt,\n \n \tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n \tstrbuf_addf(&dotgit, \"%s/.git\", wt->path);\n+\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n \tdotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));\n \n \tif (dotgit_contents) {\n@@ -611,18 +613,20 @@ static void repair_gitfile(struct worktree *wt,\n \t\trepair = _(\".git file broken\");\n \telse if (fspathcmp(backlink.buf, repo.buf))\n \t\trepair = _(\".git file incorrect\");\n+\telse if (use_relative_paths == is_absolute_path(dotgit_contents))\n+\t\trepair = _(\".git file absolute/relative path mismatch\");\n \n \tif (repair) {\n \t\tfn(0, wt->path, repair, cb_data);\n-\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, wt->path, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \t}\n \n done:\n \tfree(dotgit_contents);\n \tstrbuf_release(&repo);\n \tstrbuf_release(&dotgit);\n+\tstrbuf_release(&gitdir);\n \tstrbuf_release(&backlink);\n-\tstrbuf_release(&tmp);\n }\n \n static void repair_noop(int iserr UNUSED,\n@@ -633,7 +637,7 @@ static void repair_noop(int iserr UNUSED,\n \t/* nothing */\n }\n \n-void repair_worktrees(worktree_repair_fn fn, void *cb_data)\n+void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths)\n {\n \tstruct worktree **worktrees = get_worktrees_internal(1);\n \tstruct worktree **wt = worktrees + 1; /* +1 skips main worktree */\n@@ -641,7 +645,7 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)\n \tif (!fn)\n \t\tfn = repair_noop;\n \tfor (; *wt; wt++)\n-\t\trepair_gitfile(*wt, fn, cb_data);\n+\t\trepair_gitfile(*wt, fn, cb_data, use_relative_paths);\n \tfree_worktrees(worktrees);\n }\n \n@@ -757,16 +761,14 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n  * the worktree's path.\n  */\n void repair_worktree_at_path(const char *path,\n-\t\t\t     worktree_repair_fn fn, void *cb_data)\n+\t\t\t     worktree_repair_fn fn, void *cb_data,\n+\t\t\t     int use_relative_paths)\n {\n \tstruct strbuf dotgit = STRBUF_INIT;\n-\tstruct strbuf realdotgit = STRBUF_INIT;\n \tstruct strbuf backlink = STRBUF_INIT;\n \tstruct strbuf inferred_backlink = STRBUF_INIT;\n \tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf olddotgit = STRBUF_INIT;\n-\tstruct strbuf realolddotgit = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n \tchar *dotgit_contents = NULL;\n \tconst char *repair = NULL;\n \tint err;\n@@ -778,25 +780,25 @@ void repair_worktree_at_path(const char *path,\n \t\tgoto done;\n \n \tstrbuf_addf(&dotgit, \"%s/.git\", path);\n-\tif (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {\n+\tif (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {\n \t\tfn(1, path, _(\"not a valid path\"), cb_data);\n \t\tgoto done;\n \t}\n \n-\tinfer_backlink(realdotgit.buf, &inferred_backlink);\n+\tinfer_backlink(dotgit.buf, &inferred_backlink);\n \tstrbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);\n-\tdotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));\n+\tdotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));\n \tif (dotgit_contents) {\n \t\tif (is_absolute_path(dotgit_contents)) {\n \t\t\tstrbuf_addstr(&backlink, dotgit_contents);\n \t\t} else {\n-\t\t\tstrbuf_addbuf(&backlink, &realdotgit);\n+\t\t\tstrbuf_addbuf(&backlink, &dotgit);\n \t\t\tstrbuf_strip_suffix(&backlink, \".git\");\n \t\t\tstrbuf_addstr(&backlink, dotgit_contents);\n \t\t\tstrbuf_realpath_forgiving(&backlink, backlink.buf, 0);\n \t\t}\n \t} else if (err == READ_GITFILE_ERR_NOT_A_FILE) {\n-\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git is not a file\"), cb_data);\n+\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git is not a file\"), cb_data);\n \t\tgoto done;\n \t} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {\n \t\tif (inferred_backlink.len) {\n@@ -809,11 +811,11 @@ void repair_worktree_at_path(const char *path,\n \t\t\t */\n \t\t\tstrbuf_swap(&backlink, &inferred_backlink);\n \t\t} else {\n-\t\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git file does not reference a repository\"), cb_data);\n+\t\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git file does not reference a repository\"), cb_data);\n \t\t\tgoto done;\n \t\t}\n \t} else {\n-\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git file broken\"), cb_data);\n+\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git file broken\"), cb_data);\n \t\tgoto done;\n \t}\n \n@@ -835,39 +837,35 @@ void repair_worktree_at_path(const char *path,\n \t * in the \"copy\" repository. In this case, point the \"copy\" worktree's\n \t * .git file at the \"copy\" repository.\n \t */\n-\tif (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) {\n+\tif (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))\n \t\tstrbuf_swap(&backlink, &inferred_backlink);\n-\t}\n \n \tstrbuf_addf(&gitdir, \"%s/gitdir\", backlink.buf);\n \tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n \t\trepair = _(\"gitdir unreadable\");\n+\telse if (use_relative_paths == is_absolute_path(olddotgit.buf))\n+\t\trepair = _(\"gitdir absolute/relative path mismatch\");\n \telse {\n \t\tstrbuf_rtrim(&olddotgit);\n-\t\tif (is_absolute_path(olddotgit.buf)) {\n-\t\t\tstrbuf_addbuf(&realolddotgit, &olddotgit);\n-\t\t} else {\n-\t\t\tstrbuf_addf(&realolddotgit, \"%s/%s\", backlink.buf, olddotgit.buf);\n-\t\t\tstrbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0);\n+\t\tif (!is_absolute_path(olddotgit.buf)) {\n+\t\t\tstrbuf_insertf(&olddotgit, 0, \"%s/\", backlink.buf);\n+\t\t\tstrbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0);\n \t\t}\n-\t\tif (fspathcmp(realolddotgit.buf, realdotgit.buf))\n+\t\tif (fspathcmp(olddotgit.buf, dotgit.buf))\n \t\t\trepair = _(\"gitdir incorrect\");\n \t}\n \n \tif (repair) {\n \t\tfn(0, gitdir.buf, repair, cb_data);\n-\t\twrite_file(gitdir.buf, \"%s\", relative_path(realdotgit.buf, backlink.buf, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \t}\n done:\n \tfree(dotgit_contents);\n \tstrbuf_release(&olddotgit);\n-\tstrbuf_release(&realolddotgit);\n \tstrbuf_release(&backlink);\n \tstrbuf_release(&inferred_backlink);\n \tstrbuf_release(&gitdir);\n-\tstrbuf_release(&realdotgit);\n \tstrbuf_release(&dotgit);\n-\tstrbuf_release(&tmp);\n }\n \n int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)\ndiff --git a/worktree.h b/worktree.h\nindex 9c699d080d8ebf37712044136679db3821ee1f63..38145df80f41079f301d3aeaaffcc35b7f6760b9 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -129,7 +129,7 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,\n  * function, if non-NULL, is called with the path of the worktree and a\n  * description of the repair or error, along with the callback user-data.\n  */\n-void repair_worktrees(worktree_repair_fn, void *cb_data);\n+void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths);\n \n /*\n  * Repair the linked worktrees after the gitdir has been moved.\n@@ -151,7 +151,8 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path\n  * worktree and a description of the repair or error, along with the callback\n  * user-data.\n  */\n-void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);\n+void repair_worktree_at_path(const char *, worktree_repair_fn,\n+\t\t\t     void *cb_data, int use_relative_paths);\n \n /*\n  * Free up the memory for a worktree.\n\n-- \n2.47.0\n\n\n"},{"id":"508131","messageId":"20241125-wt_relative_options-v5-8-356d122ff3db@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v5 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T01:52:27Z","receivedAt":"2024-11-26T01:52:34Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This refactors `repair_worktree_after_gitdir_move()` to use the new\n`write_worktree_linking_files` function. It also preserves the\nrelativity of the linking files; e.g., if an existing worktree used\nabsolute paths then the repaired paths will be absolute (and visa-versa).\n`repair_worktree_after_gitdir_move()` is used to repair both sets of\nworktree linking files if the `.git` directory is moved during a\nre-initialization using `git init`.\n\nThis also adds a test case for reinitializing a repository that has\nrelative worktrees.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n t/t0001-init.sh | 22 ++++++++++++++++++----\n worktree.c      | 29 ++++++++---------------------\n 2 files changed, 26 insertions(+), 25 deletions(-)\n\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e394147b8480fb5784f1c23c0d2eae712eecc44b 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n sep_git_dir_worktree ()  {\n \ttest_when_finished \"rm -rf mainwt linkwt seprepo\" &&\n \tgit init mainwt &&\n+\tif test \"relative\" = $2\n+\tthen\n+\t\ttest_config -C mainwt worktree.useRelativePaths true\n+\telse\n+\t\ttest_config -C mainwt worktree.useRelativePaths false\n+\tfi\n \ttest_commit -C mainwt gumby &&\n \tgit -C mainwt worktree add --detach ../linkwt &&\n \tgit -C \"$1\" init --separate-git-dir ../seprepo &&\n@@ -442,12 +448,20 @@ sep_git_dir_worktree ()  {\n \ttest_cmp expect actual\n }\n \n-test_expect_success 're-init to move gitdir with linked worktrees' '\n-\tsep_git_dir_worktree mainwt\n+test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '\n+\tsep_git_dir_worktree mainwt absolute\n+'\n+\n+test_expect_success 're-init to move gitdir within linked worktree (absolute)' '\n+\tsep_git_dir_worktree linkwt absolute\n+'\n+\n+test_expect_success 're-init to move gitdir with linked worktrees (relative)' '\n+\tsep_git_dir_worktree mainwt relative\n '\n \n-test_expect_success 're-init to move gitdir within linked worktree' '\n-\tsep_git_dir_worktree linkwt\n+test_expect_success 're-init to move gitdir within linked worktree (relative)' '\n+\tsep_git_dir_worktree linkwt relative\n '\n \n test_expect_success MINGW '.git hidden' '\ndiff --git a/worktree.c b/worktree.c\nindex 2e76bbc1494afc125997f803dc39846a0b95a84f..af68b24f9d0917a99cd965347aad6466e9dbb152 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -651,45 +651,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat\n \n void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)\n {\n-\tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf repo = STRBUF_INIT;\n \tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf dotgit = STRBUF_INIT;\n-\tstruct strbuf olddotgit = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n+\tint is_relative_path;\n \n \tif (is_main_worktree(wt))\n \t\tgoto done;\n \n-\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n-\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n+\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n \n-\tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n+\tif (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)\n \t\tgoto done;\n \n-\tstrbuf_rtrim(&olddotgit);\n-\tif (is_absolute_path(olddotgit.buf)) {\n-\t\tstrbuf_addbuf(&dotgit, &olddotgit);\n-\t} else {\n-\t\tstrbuf_addf(&dotgit, \"%s/worktrees/%s/%s\", old_path, wt->id, olddotgit.buf);\n+\tstrbuf_rtrim(&dotgit);\n+\tis_relative_path = ! is_absolute_path(dotgit.buf);\n+\tif (is_relative_path) {\n+\t\tstrbuf_insertf(&dotgit, 0, \"%s/worktrees/%s/\", old_path, wt->id);\n \t\tstrbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);\n \t}\n \n \tif (!file_exists(dotgit.buf))\n \t\tgoto done;\n \n-\tstrbuf_addbuf(&path, &dotgit);\n-\tstrbuf_strip_suffix(&path, \"/.git\");\n-\n-\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n-\twrite_file(gitdir.buf, \"%s\", relative_path(dotgit.buf, repo.buf, &tmp));\n+\twrite_worktree_linking_files(dotgit, gitdir, is_relative_path);\n done:\n-\tstrbuf_release(&path);\n-\tstrbuf_release(&repo);\n \tstrbuf_release(&gitdir);\n \tstrbuf_release(&dotgit);\n-\tstrbuf_release(&olddotgit);\n-\tstrbuf_release(&tmp);\n }\n \n void repair_worktrees_after_gitdir_move(const char *old_path)\n\n-- \n2.47.0\n\n\n"},{"id":"508136","messageId":"xmqqv7wazfex.fsf@gitster.g","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-11-26T06:18:30Z","receivedAt":"2024-11-26T06:18:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Caleb White <cdwhite3@pm.me> writes:\n\n> Changes in v5:\n> - Added docs to `--relative-paths` option.\n\nYou already had doc on this, but the default was not described at\nall.\n\n --[no-]relative-paths::\n+       Link worktrees using relative paths or absolute paths (default).\n\n> - Added test coverage for `repair_worktrees()` and relative paths.\n> - Move `strbuf_reset` call in `infer_backlink()`.\n\nThis was more like \"revert the change in v4 that moved it\nunnecessarily\", no?\n\n> - Cleaned up tests.\n\nYup, there truely a lot of test changes between v4 and v5.  Many\ntests now use existing test helpers, which is good.\n\n\n> - Slight stylistic changes.\n\nI saw many changes like these (the diff is between v4 and v5)\n\n static void repair_gitfile(struct worktree *wt,\n-                          worktree_repair_fn fn,\n-                          void *cb_data,\n+                          worktree_repair_fn fn, void *cb_data,\n                           int use_relative_paths)\n\nwhich looked good (the original had fn and cb_data defined on the\nsame line).\n\n> - Tweaked commit messages.\n\nUpdates to the proposed log message for `repair` step [7/8] did not\nreally \"clarify\", other than helping readers to see how messy things\nare.  It said:\n\n    +    To simplify things, both linking files are written when one of the files\n    +    needs to be repaired. In some cases, this fixes the other file before it\n    +    is checked, in other cases this results in a correct file being written\n    +    with the same contents.\n\nwhich may describe what the code happens to do correctly, but does\nnot quite help building the confidence in what it does is correct.\n\nSuppose that the directory X has a repository, and the repository\nthinks that the directory W is its worktree.  But the worktree at\nthe directory W thinks that its repository is not X but Y, and there\nindeed is a repository at the directory Y.  That repository thinks W\nbelongs to it.\n\nIf we examine X first, would we end up updating W to point at X\n(because X thinks W is its worktree)?\n\nOr do we make W to point at Y (because Y thinks W is its, and W\nthinks it is Y's)\"?\n\nEither way, I think the comment is trying to say that, if we decide\nto make X and W belong to each other, we'd overwrite links from X to\nW and also W to X, even though the link from X was already pointing\nat W and the minimum fix we needed to make was to update the link\nfrom W to point at X.  Overwriting a link from X to W with a new\nlink from X to W is a no-op, so it does not seem to help greatly,\nsince `repair` is not at all performance critical.  The correctness\nis a lot more important.\n\n\n> - Updated base to 090d24e9af.\n\nThis made it harder than necessary to compare the two iterations, by\nthe way.\n\n\nThanks.\n"},{"id":"508190","messageId":"D5W9TF80TCN0.19T8NJTHY4VGW@pm.me","threadId":"62433","inReplyTo":"xmqqv7wazfex.fsf@gitster.g","subject":"Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-26T17:02:42Z","receivedAt":"2024-11-26T17:03:05Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Tue Nov 26, 2024 at 12:18 AM CST, Junio C Hamano wrote:\n> Caleb White <cdwhite3@pm.me> writes:\n>> Changes in v5:\n>> - Added docs to `--relative-paths` option.\n>\n> You already had doc on this, but the default was not described at\n> all.\n>\n>  --[no-]relative-paths::\n> +       Link worktrees using relative paths or absolute paths (default).\n\nI added a bit more explanation instead of just directing the user to the\nconfig variable (I originally had docs, but it was requested that\nI remove the duplication and just point to the config, however, I think\nthe changes describes it a bit better as well as gives the default).\n\n>> - Added test coverage for `repair_worktrees()` and relative paths.\n>> - Move `strbuf_reset` call in `infer_backlink()`.\n>\n> This was more like \"revert the change in v4 that moved it\n> unnecessarily\", no?\n\nYes, that is correct.\n\n>> - Cleaned up tests.\n>\n> Yup, there truely a lot of test changes between v4 and v5.  Many\n> tests now use existing test helpers, which is good.\n\nThis is the majority of the reroll. It seems like MacOS doesn't like the \n`test_config` helper inside of a subshell, so I had to stick with `git\nconfig` in those cases.\n\n>> - Slight stylistic changes.\n>\n> I saw many changes like these (the diff is between v4 and v5)\n>\n>  static void repair_gitfile(struct worktree *wt,\n> -                          worktree_repair_fn fn,\n> -                          void *cb_data,\n> +                          worktree_repair_fn fn, void *cb_data,\n>                            int use_relative_paths)\n>\n> which looked good (the original had fn and cb_data defined on the\n> same line).\n\nYes, this was brought up in the previous review and I decided to make\nthe change.\n\n>> - Tweaked commit messages.\n>\n> Updates to the proposed log message for `repair` step [7/8] did not\n> really \"clarify\", other than helping readers to see how messy things\n> are.  It said:\n>\n>     +    To simplify things, both linking files are written when one of the files\n>     +    needs to be repaired. In some cases, this fixes the other file before it\n>     +    is checked, in other cases this results in a correct file being written\n>     +    with the same contents.\n>\n> which may describe what the code happens to do correctly, but does\n> not quite help building the confidence in what it does is correct.\n>\n> Suppose that the directory X has a repository, and the repository\n> thinks that the directory W is its worktree.  But the worktree at\n> the directory W thinks that its repository is not X but Y, and there\n> indeed is a repository at the directory Y.  That repository thinks W\n> belongs to it.\n\nThat's a bit of a confusing scenario, but I think this is what you're\ntrying to describe:\n\n    Repository X ----> Worktree W <---> Repository Y (Case 0)\n\nwhich is not a normal case (but I'll get to that later).\nMost of the time, a repair would be performed with one of the following\ncases:\n\n    Repository X <---> Worktree W (Case 1)\n    Repository X ----> Worktree W (Case 2)\n    Repository X <---- Worktree W (Case 3)\n    Repository X       Worktree W (Case 4)\n\nthat is, a repository and worktree have valid links, have valid links in\none direction or the other, or they have no valid links at all.\n\nBefore I go on, I think it would be helpful to revisit how the repair\noperation works. There are two loops in the repair operation:\n1. the `repair()` function iterates over (via `repair_worktree_at_path()`)\n   the given worktrees/paths (or `.` if no paths are given) to potentially\n   repair the `<repo>/.git/worktrees/<worktree_id>/gitdir` files\n2. the `repair_worktrees()` function iterates over all the worktrees\n   defined at `<repo>/.git/worktrees/*` to potentially repair the\n   `.../<worktree_id>/.git` files\n\nIn Loop 1, a repair is performed if:\n- there's an absolute/relative path mismatch\n- the worktree `.git` file points to the repository, but the repository\n  `gitdir` file is unreadable or does not point back to the worktree\n- the worktree `.git` file does NOT point to the repository, but an\n  inferred backlink can be established (the worktree id in the `.git`\n  file matches a worktree id in the repository's `worktrees` directory),\n  and that inferred repository does not point to the worktree\n\nIn Loop 2, a repair is performed if:\n- there's an absolute/relative path mismatch\n- the worktree pointed to by the repository `gitdir` file does not point\n  back to the repository or the file is corrupted\n\nNow back to Cases 1--4:\n- In Case 1, the repair would not update any links (already valid).\n- Case 2 is most likely when using absolute paths and the repository is\n  moved, but the worktree is not. The worktree `.git` will be updated\n  during Loop 2, however, now the repository `gitdir` file will also be\n  written with the same contents (a no-op) to keep the code simple.\n- Case 3 is most likely when using absolute paths and the worktree is\n  moved, but the repository is not. The repository `gitdir` will be\n  updated during Loop 1, however, now the worktree `.git` file will also\n  be written with the same contents (a no-op) to keep the code simple.\n- Case 4 can occur when using absolute paths and both the repository and\n  worktree are moved, but it can also occur when using relative paths and\n  either the repository or worktree is moved. Both linking files need to\n  be updated. In the past, the repository `gitdir` file would be updated\n  during Loop 1 (from the inferred backlink), and the worktree `.git`\n  would not be updated until Loop 2. However, now both linking files are\n  updated during Loop 1 and the repair is complete by the time Loop 2\n  is reached.\n\n> If we examine X first, would we end up updating W to point at X\n> (because X thinks W is its worktree)?\n>\n> Or do we make W to point at Y (because Y thinks W is its, and W\n> thinks it is Y's)\"?\n\nA repair is always performed in the context of single repository,\ntherefore, if operating on repository X and the worktree W is found to\nbe a valid worktree for X, then yes, the repair would update the link\nfrom W to X so that Case 0 would now look like:\n\n    Repository X <---> Worktree W <---- Repository Y\n\nbut again, this is a very weird case---the most likely scenario that I\ncan think of is that a user copied a repository (with or without the\nworktree). The `es/worktree-repair-copied` topic added support for\nrepairing a worktree from such a copy scenario. However, I did note[1,2]\nthat the topic added the ability for a repository to \"take over\" a\nworktree from another repository if the worktree_id matched a worktree\ninside the current repository. This can happen if two repositories use\nthe same worktree name (I usually name my worktrees the same name as the\nbranch to keep things simple so this can happen if two repositories\ncreate a worktree for `master` for instance).\n\nI recommended that worktrees be created with a unique hash/identifier so\nthat the worktree_id is unique across all repositories even if they have\nthe same name. I was planning on creating a future topic to address this,\nfor example creating a worktree `develop` would look like:\n\n    foo/\n    ├── .git/worktrees/develop-6b3d7b/\n    └── develop/\n\nThe actual worktree directory name would still be `develop`, but the\nworktree_id would be unique and prevent the \"take over\" scenario.\n\n> Either way, I think the comment is trying to say that, if we decide\n> to make X and W belong to each other, we'd overwrite links from X to\n> W and also W to X, even though the link from X was already pointing\n> at W and the minimum fix we needed to make was to update the link\n> from W to point at X.  Overwriting a link from X to W with a new\n> link from X to W is a no-op, so it does not seem to help greatly,\n> since `repair` is not at all performance critical.  The correctness\n> is a lot more important.\n\nYes, you understand this correctly. The repair operation is not\nperformance critical, so I decided to keep the code simple and just\nalways update both linking files. The same `write_worktree_linking_files()\nis used for all operations (add, move, repair), some which require both\nfiles to be updated, and others which only require one file to be updated.\n\n>> - Updated base to 090d24e9af.\n>\n> This made it harder than necessary to compare the two iterations, by\n> the way.\n\nMy apologies for that. I wasn't sure what the procedure was when\na dependent topic was merged to master. I figured it would be best to\nrebase onto the latest master.\n\nBest,\n\nCaleb\n\n[1]: https://lore.kernel.org/git/20241008153035.71178-1-cdwhite3@pm.me/\n[2]: https://lore.kernel.org/git/r4zmcET41Skr_FMop47AKd7cms9E8bKPSvHuAUpnYavzKEY6JybJta0_7GfuYB0q-gD-XNcvh5VDTfiT3qthGKjqhS1sbT4M2lUABynOz2Q=@pm.me/\n\n"},{"id":"508271","messageId":"054b70c2-accd-4d85-a576-66910d35a26d@gmail.com","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-11-28T14:44:51Z","receivedAt":"2024-11-28T14:44:54Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 26/11/2024 01:51, Caleb White wrote:\n> Changes in v5:\n> - Added docs to `--relative-paths` option.\n> - Added test coverage for `repair_worktrees()` and relative paths.\n> - Move `strbuf_reset` call in `infer_backlink()`.\n> - Cleaned up tests.\n> - Slight stylistic changes.\n> - Tweaked commit messages.\n> - Updated base to 090d24e9af.\n\nThanks for re-rolling, these changes sound good. Below is the\nrange-diff of what is in seen today compared to last week. I've left\nit untrimmed so other people can check what's changed and I've added a\ncouple of comments. The only thing I'm worried about is the deletion\nof a check for setting extensions.relativeWorktrees in patch 5 the\nrest of the changes look good, thank you for the extra test checks and\nlog messages.\n\nBest Wishes\n\nPhillip\n\n1:  67a622c7f2 ! 1:  0986f98022 setup: correctly reinitialize repository version\n     @@ Commit message\n          config to ensure that the repository version is set correctly.\n      \n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## setup.c ##\n      @@ setup.c: void initialize_repository_version(int hash_algo,\n2:  85964909b0 ! 2:  c36e1a59fa worktree: add `relativeWorktrees` extension\n     @@ Commit message\n      \n          Suggested-by: Junio C Hamano <gitster@pobox.com>\n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## Documentation/config/extensions.txt ##\n      @@ Documentation/config/extensions.txt: Note that this setting should only be set by linkgit:git-init[1] or\n3:  a614c39333 ! 3:  5b19b63040 worktree: refactor infer_backlink return\n     @@ Commit message\n          [1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n      \n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## worktree.c ##\n      @@ worktree.c: struct worktree *get_linked_worktree(const char *id,\n     @@ worktree.c: static int is_main_worktree_path(const char *path)\n       {\n       \tstruct strbuf actual = STRBUF_INIT;\n       \tconst char *id;\n     -\n     -+\tstrbuf_reset(inferred);\n     - \tif (strbuf_read_file(&actual, gitfile, 0) < 0)\n     - \t\tgoto error;\n     - \tif (!starts_with(actual.buf, \"gitdir:\"))\n      @@ worktree.c: static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n     - \tid++; /* advance past '/' to point at <id> */\n     - \tif (!*id)\n     - \t\tgoto error;\n     --\tstrbuf_reset(inferred);\n     - \tstrbuf_git_common_path(inferred, the_repository, \"worktrees/%s\", id);\n     - \tif (!is_directory(inferred->buf))\n       \t\tgoto error;\n       \n       \tstrbuf_release(&actual);\n4:  83ea6d7ba0 ! 4:  ec143ae00e worktree: add `write_worktree_linking_files()` function\n     @@ Commit message\n          is linked with relative paths.\n      \n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## worktree.c ##\n      @@ worktree.c: int init_worktree_config(struct repository *r)\n       \tfree(main_worktree_file);\n       \treturn res;\n       }\n      +\n     -+void write_worktree_linking_files(struct strbuf dotgit,\n     -+\t\t\t\t  struct strbuf gitdir,\n     ++void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,\n      +\t\t\t\t  int use_relative_paths)\n      +{\n      +\tstruct strbuf path = STRBUF_INIT;\n     @@ worktree.h: void strbuf_worktree_ref(const struct worktree *wt,\n      + *  dotgit: \"/path/to/foo/.git\"\n      + *  gitdir: \"/path/to/repo/worktrees/foo/gitdir\"\n      + */\n     -+void write_worktree_linking_files(struct strbuf dotgit,\n     -+\t\t\t\t  struct strbuf gitdir,\n     ++void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,\n      +\t\t\t\t  int use_relative_paths);\n      +\n       #endif\n5:  36d01dca84 ! 5:  237206b08f worktree: add relative cli/config options to `add` command\n     @@ Commit message\n          written for the various worktree operations in their own files.\n      \n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## Documentation/config/worktree.txt ##\n      @@ Documentation/config/worktree.txt: worktree.guessRemote::\n     @@ Documentation/git-worktree.txt: To remove a locked worktree, specify `--force` t\n       `worktree.guessRemote` config option.\n       \n      +--[no-]relative-paths::\n     ++\tLink worktrees using relative paths or absolute paths (default).\n      +\tOverrides the `worktree.useRelativePaths` config option, see\n      +\tlinkgit:git-config[1].\n      +\n     @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" with initialized submodule,\n       \n      +test_expect_success 'can create worktrees with relative paths' '\n      +\ttest_when_finished \"git worktree remove relative\" &&\n     -+\tgit config worktree.useRelativePaths false &&\n     ++\ttest_config worktree.useRelativePaths false &&\n      +\tgit worktree add --relative-paths ./relative &&\n     -+\tcat relative/.git >actual &&\n      +\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n     -+\ttest_cmp expect actual &&\n     -+\tcat .git/worktrees/relative/gitdir >actual &&\n     ++\ttest_cmp expect relative/.git &&\n      +\techo \"../../../relative/.git\" >expect &&\n     -+\ttest_cmp expect actual\n     -+\n     ++\ttest_cmp expect .git/worktrees/relative/gitdir\n      +'\n      +\n      +test_expect_success 'can create worktrees with absolute paths' '\n     -+\tgit config worktree.useRelativePaths true &&\n     ++\ttest_config worktree.useRelativePaths true &&\n      +\tgit worktree add ./relative &&\n     -+\tcat relative/.git >actual &&\n      +\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n     -+\ttest_cmp expect actual &&\n     ++\ttest_cmp expect relative/.git &&\n      +\tgit worktree add --no-relative-paths ./absolute &&\n     -+\tcat absolute/.git >actual &&\n      +\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n     -+\ttest_cmp expect actual\n     ++\ttest_cmp expect absolute/.git &&\n     ++\techo \"$(pwd)/absolute/.git\" >expect &&\n     ++\ttest_cmp expect .git/worktrees/absolute/gitdir\n      +'\n      +\n      +test_expect_success 'move repo without breaking relative internal links' '\n      +\ttest_when_finished rm -rf repo moved &&\n      +\tgit init repo &&\n      +\t(\n      +\t\tcd repo &&\n     -+\t\tgit config worktree.useRelativePaths true &&\n      +\t\ttest_commit initial &&\n     -+\t\tgit worktree add wt1 &&\n     ++\t\tgit worktree add --relative-paths wt1 &&\n      +\t\tcd .. &&\n      +\t\tmv repo moved &&\n      +\t\tcd moved/wt1 &&\n     -+\t\tgit status >out 2>err &&\n     ++\t\tgit worktree list >out 2>err &&\n      +\t\ttest_must_be_empty err\n      +\t)\n      +'\n     @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" with initialized submodule,\n      +\tgit init repo &&\n      +\tgit -C repo commit --allow-empty -m base &&\n      +\tgit -C repo worktree add --relative-paths ./foo &&\n     -+\tgit -C repo config get core.repositoryformatversion >actual &&\n     -+\techo 1 >expected &&\n     -+\ttest_cmp expected actual &&\n     -+\tgit -C repo config get extensions.relativeworktrees >actual &&\n     -+\techo true >expected &&\n     -+\ttest_cmp expected actual\n     ++\ttest_cmp_config -C repo 1 core.repositoryformatversion\n      +'\n\nWe have lost the check for extensions.relativeworktrees\nhere. Although we don't set worktree.useRelativePaths anymore we\nshould still set the extension as we pase --relative-paths to \"git\nworktree add\"\n\n+\n       test_done\n6:  34401d680c ! 6:  8e4307f520 worktree: add relative cli/config options to `move` command\n     @@ Commit message\n          the new path will be relative (and visa-versa).\n      \n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## builtin/worktree.c ##\n      @@ builtin/worktree.c: static int move_worktree(int ac, const char **av, const char *prefix)\n     @@ t/t2403-worktree-move.sh: test_expect_success 'not remove a repo with initialize\n       '\n       \n      +test_expect_success 'move worktree with absolute path to relative path' '\n     -+\tgit config worktree.useRelativePaths false &&\n     ++\ttest_config worktree.useRelativePaths false &&\n      +\tgit worktree add ./absolute &&\n      +\tgit worktree move --relative-paths absolute relative &&\n     -+\tcat relative/.git >actual &&\n      +\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n     -+\ttest_cmp expect actual &&\n     -+\tgit config worktree.useRelativePaths true &&\n     ++\ttest_cmp expect relative/.git &&\n     ++\techo \"../../../relative/.git\" >expect &&\n     ++\ttest_cmp expect .git/worktrees/absolute/gitdir &&\n     ++\ttest_config worktree.useRelativePaths true &&\n      +\tgit worktree move relative relative2 &&\n     -+\tcat relative2/.git >actual &&\n      +\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n     -+\ttest_cmp expect actual\n     ++\ttest_cmp expect relative2/.git &&\n     ++\techo \"../../../relative2/.git\" >expect &&\n     ++\ttest_cmp expect .git/worktrees/absolute/gitdir\n      +'\n      +\n      +test_expect_success 'move worktree with relative path to absolute path' '\n     -+\tgit config worktree.useRelativePaths true &&\n     ++\ttest_config worktree.useRelativePaths true &&\n      +\tgit worktree move --no-relative-paths relative2 absolute &&\n     -+\tcat absolute/.git >actual &&\n      +\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n     -+\ttest_cmp expect actual\n     ++\ttest_cmp expect absolute/.git &&\n     ++\techo \"$(pwd)/absolute/.git\" >expect &&\n     ++\ttest_cmp expect .git/worktrees/absolute/gitdir\n      +'\n      +\n       test_done\n     @@ worktree.c: int validate_worktree(const struct worktree *wt, struct strbuf *errm\n       }\n       \n      -void update_worktree_location(struct worktree *wt, const char *path_)\n     -+void update_worktree_location(struct worktree *wt,\n     -+\t\t\t      const char *path_,\n     ++void update_worktree_location(struct worktree *wt, const char *path_,\n      +\t\t\t      int use_relative_paths)\n       {\n       \tstruct strbuf path = STRBUF_INIT;\n     @@ worktree.c: int validate_worktree(const struct worktree *wt, struct strbuf *errm\n      \n       ## worktree.h ##\n      @@ worktree.h: int validate_worktree(const struct worktree *wt,\n     + /*\n        * Update worktrees/xxx/gitdir with the new path.\n        */\n     - void update_worktree_location(struct worktree *wt,\n     +-void update_worktree_location(struct worktree *wt,\n      -\t\t\t      const char *path_);\n     -+\t\t\t      const char *path_,\n     ++void update_worktree_location(struct worktree *wt, const char *path_,\n      +\t\t\t      int use_relative_paths);\n       \n       typedef void (* worktree_repair_fn)(int iserr, const char *path,\n7:  30e9e41061 ! 7:  4f951f4088 worktree: add relative cli/config options to `repair` command\n     @@ Commit message\n          even if the original path was correct. This allows a user to covert\n          existing worktrees between absolute/relative as desired.\n      \n     +    To simplify things, both linking files are written when one of the files\n     +    needs to be repaired. In some cases, this fixes the other file before it\n     +    is checked, in other cases this results in a correct file being written\n     +    with the same contents.\n     +\n\nThanks for adding this, I think it will be really helpful for anyone\ntrying to understand this change in the future.\n\n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## Documentation/git-worktree.txt ##\n      @@ Documentation/git-worktree.txt: This can also be set up as the default behaviour by using the\n     - --[no-]relative-paths::\n     + \tLink worktrees using relative paths or absolute paths (default).\n       \tOverrides the `worktree.useRelativePaths` config option, see\n       \tlinkgit:git-config[1].\n      ++\n     @@ t/t2406-worktree-repair.sh: test_expect_success 'repair copied main and linked w\n       \ttest_cmp dup/linked.expect dup/linked/.git\n       '\n       \n     ++test_expect_success 'repair worktree with relative path with missing gitfile' '\n     ++\ttest_when_finished \"rm -rf main wt\" &&\n     ++\ttest_create_repo main &&\n     ++\tgit -C main config worktree.useRelativePaths true &&\n     ++\ttest_commit -C main init &&\n     ++\tgit -C main worktree add --detach ../wt &&\n     ++\trm wt/.git &&\n     ++\ttest_path_is_missing wt/.git &&\n     ++\tgit -C main worktree repair &&\n     ++\techo \"gitdir: ../main/.git/worktrees/wt\" >expect &&\n     ++\ttest_cmp expect wt/.git\n     ++'\n     ++\n      +test_expect_success 'repair absolute worktree to use relative paths' '\n      +\ttest_when_finished \"rm -rf main side sidemoved\" &&\n      +\ttest_create_repo main &&\n     @@ worktree.c: int other_head_refs(each_ref_fn fn, void *cb_data)\n        */\n       static void repair_gitfile(struct worktree *wt,\n      -\t\t\t   worktree_repair_fn fn, void *cb_data)\n     -+\t\t\t   worktree_repair_fn fn,\n     -+\t\t\t   void *cb_data,\n     ++\t\t\t   worktree_repair_fn fn, void *cb_data,\n      +\t\t\t   int use_relative_paths)\n       {\n       \tstruct strbuf dotgit = STRBUF_INIT;\n     @@ worktree.c: static ssize_t infer_backlink(const char *gitfile, struct strbuf *in\n        */\n       void repair_worktree_at_path(const char *path,\n      -\t\t\t     worktree_repair_fn fn, void *cb_data)\n     -+\t\t\t     worktree_repair_fn fn,\n     -+\t\t\t     void *cb_data,\n     ++\t\t\t     worktree_repair_fn fn, void *cb_data,\n      +\t\t\t     int use_relative_paths)\n       {\n       \tstruct strbuf dotgit = STRBUF_INIT;\n8:  298327f538 ! 8:  28eb7f66b2 worktree: refactor `repair_worktree_after_gitdir_move()`\n     @@ Commit message\n          `write_worktree_linking_files` function. It also preserves the\n          relativity of the linking files; e.g., if an existing worktree used\n          absolute paths then the repaired paths will be absolute (and visa-versa).\n     +    `repair_worktree_after_gitdir_move()` is used to repair both sets of\n     +    worktree linking files if the `.git` directory is moved during a\n     +    re-initialization using `git init`.\n\nThanks for the extra explanation\n\n          This also adds a test case for reinitializing a repository that has\n          relative worktrees.\n      \n          Signed-off-by: Caleb White <cdwhite3@pm.me>\n     -    Signed-off-by: Taylor Blau <me@ttaylorr.com>\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## t/t0001-init.sh ##\n      @@ t/t0001-init.sh: test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n     @@ t/t0001-init.sh: test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n       \tgit init mainwt &&\n      +\tif test \"relative\" = $2\n      +\tthen\n     -+\t\tgit -C mainwt config worktree.useRelativePaths true\n     ++\t\ttest_config -C mainwt worktree.useRelativePaths true\n      +\telse\n     -+\t\tgit -C mainwt config worktree.useRelativePaths false\n     ++\t\ttest_config -C mainwt worktree.useRelativePaths false\n      +\tfi\n       \ttest_commit -C mainwt gumby &&\n       \tgit -C mainwt worktree add --detach ../linkwt &&\n\n> - Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me\n> Changes in v4:\n> - Fixed failing test in ci\n> - Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me\n> Changes in v3:\n> - Split patches into smaller edits.\n> - Moved tests into the patches with the relevant code changes.\n> - Removed global `use_relative_paths` and instead pass parameter to functions.\n> - Changed `infer_backlink` return type from `int` to `ssize_t`.\n> - Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.\n> - Reordered patches\n> - Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me\n> Changes in v2:\n> - Fixed a bug where repositories with valid extensions would be downgraded\n>    to v0 during reinitialization, causing future operations to fail.\n> - Split patch [1/2] into three separate patches.\n> - Updated cover letter and commit messages.\n> - Updated documentation wording.\n> - Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me\n> \n> ---\n> Caleb White (8):\n>        setup: correctly reinitialize repository version\n>        worktree: add `relativeWorktrees` extension\n>        worktree: refactor infer_backlink return\n>        worktree: add `write_worktree_linking_files()` function\n>        worktree: add relative cli/config options to `add` command\n>        worktree: add relative cli/config options to `move` command\n>        worktree: add relative cli/config options to `repair` command\n>        worktree: refactor `repair_worktree_after_gitdir_move()`\n> \n>   Documentation/config/extensions.txt |   6 ++\n>   Documentation/config/worktree.txt   |  10 +++\n>   Documentation/git-worktree.txt      |   8 ++\n>   builtin/worktree.c                  |  29 ++++---\n>   repository.c                        |   1 +\n>   repository.h                        |   1 +\n>   setup.c                             |  39 ++++++---\n>   setup.h                             |   1 +\n>   t/t0001-init.sh                     |  22 ++++-\n>   t/t2400-worktree-add.sh             |  45 +++++++++++\n>   t/t2401-worktree-prune.sh           |   3 +-\n>   t/t2402-worktree-list.sh            |  22 +++++\n>   t/t2403-worktree-move.sh            |  25 ++++++\n>   t/t2406-worktree-repair.sh          |  39 +++++++++\n>   t/t2408-worktree-relative.sh        |  39 ---------\n>   t/t5504-fetch-receive-strict.sh     |   6 +-\n>   worktree.c                          | 157 ++++++++++++++++++++----------------\n>   worktree.h                          |  22 ++++-\n>   18 files changed, 333 insertions(+), 142 deletions(-)\n> ---\n> base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559\n> change-id: 20241025-wt_relative_options-afa41987bc32\n> \n> Best regards,\n\n"},{"id":"508303","messageId":"D5Y09IBPIL5V.2LHPVW07V4ZDR@pm.me","threadId":"62433","inReplyTo":"054b70c2-accd-4d85-a576-66910d35a26d@gmail.com","subject":"Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-28T17:58:49Z","receivedAt":"2024-11-28T17:58:55Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Thu Nov 28, 2024 at 8:44 AM CST, Phillip Wood wrote:\n> On 26/11/2024 01:51, Caleb White wrote:\n>> Changes in v5:\n>> - Added docs to `--relative-paths` option.\n>> - Added test coverage for `repair_worktrees()` and relative paths.\n>> - Move `strbuf_reset` call in `infer_backlink()`.\n>> - Cleaned up tests.\n>> - Slight stylistic changes.\n>> - Tweaked commit messages.\n>> - Updated base to 090d24e9af.\n>\n> Thanks for re-rolling, these changes sound good. Below is the\n> range-diff of what is in seen today compared to last week. I've left\n> it untrimmed so other people can check what's changed and I've added a\n> couple of comments. The only thing I'm worried about is the deletion\n> of a check for setting extensions.relativeWorktrees in patch 5 the\n> rest of the changes look good, thank you for the extra test checks and\n> log messages.\n>\n>      @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" with initialized submodule,\n>       +\tgit init repo &&\n>       +\tgit -C repo commit --allow-empty -m base &&\n>       +\tgit -C repo worktree add --relative-paths ./foo &&\n>      -+\tgit -C repo config get core.repositoryformatversion >actual &&\n>      -+\techo 1 >expected &&\n>      -+\ttest_cmp expected actual &&\n>      -+\tgit -C repo config get extensions.relativeworktrees >actual &&\n>      -+\techo true >expected &&\n>      -+\ttest_cmp expected actual\n>      ++\ttest_cmp_config -C repo 1 core.repositoryformatversion\n>       +'\n>\n> We have lost the check for extensions.relativeworktrees\n> here. Although we don't set worktree.useRelativePaths anymore we\n> should still set the extension as we pase --relative-paths to \"git\n> worktree add\"\n\nGood catch! This was removed unintentionally. I'll add this back in, but\nwill hold off on re-rolling until we're ready for the final re-roll (or\nthere's other changes requested).\n\nBest,\n\nCaleb\n\n"},{"id":"508348","messageId":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241125-wt_relative_options-v5-0-356d122ff3db@pm.me","subject":"[PATCH v6 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:22:19Z","receivedAt":"2024-11-29T22:22:31Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This series introduces the `--[no-]relative-paths` CLI option for\n`git worktree {add, move, repair}` commands, as well as the\n`worktree.useRelativePaths` configuration setting. When enabled,\nthese options allow worktrees to be linked using relative paths,\nenhancing portability across environments where absolute paths\nmay differ (e.g., containerized setups, shared network drives).\nGit still creates absolute paths by default, but these options allow\nusers to opt-in to relative paths if desired.\n\nUsing the `--relative-paths` option with `worktree {move, repair}`\nwill convert absolute paths to relative ones, while `--no-relative-paths`\ndoes the reverse. For cases where users want consistency in path handling,\nthe config option `worktree.useRelativePaths` provides a persistent setting.\n\nA new extension, `relativeWorktrees`, is added to indicate that at least\none worktree in the repository has been linked with relative paths. This\nextension is automatically set when a worktree is created or repaired\nusing the `--relative-paths` option, or when the\n`worktree.useRelativePaths` config is set to `true`.\n\nThe `relativeWorktrees` extension ensures older Git versions do not\nattempt to automatically prune worktrees with relative paths, as they\nwould not not recognize the paths as being valid.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\nThe base for this patch series is 090d24e9af.\n\nLink to original patch series:\nhttps://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n\n---\nChanges in v6:\n- Re-add test for extensions.relativeworktrees config setting\n- Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me\nChanges in v5:\n- Added docs to `--relative-paths` option.\n- Added test coverage for `repair_worktrees()` and relative paths.\n- Move `strbuf_reset` call in `infer_backlink()`.\n- Cleaned up tests.\n- Slight stylistic changes.\n- Tweaked commit messages.\n- Updated base to 090d24e9af.\n- Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me\nChanges in v4:\n- Fixed failing test in ci\n- Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me\nChanges in v3:\n- Split patches into smaller edits.\n- Moved tests into the patches with the relevant code changes.\n- Removed global `use_relative_paths` and instead pass parameter to functions.\n- Changed `infer_backlink` return type from `int` to `ssize_t`.\n- Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.\n- Reordered patches\n- Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me\nChanges in v2:\n- Fixed a bug where repositories with valid extensions would be downgraded\n  to v0 during reinitialization, causing future operations to fail.\n- Split patch [1/2] into three separate patches.\n- Updated cover letter and commit messages.\n- Updated documentation wording.\n- Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me\n\n---\nCaleb White (8):\n      setup: correctly reinitialize repository version\n      worktree: add `relativeWorktrees` extension\n      worktree: refactor infer_backlink return\n      worktree: add `write_worktree_linking_files()` function\n      worktree: add relative cli/config options to `add` command\n      worktree: add relative cli/config options to `move` command\n      worktree: add relative cli/config options to `repair` command\n      worktree: refactor `repair_worktree_after_gitdir_move()`\n\n Documentation/config/extensions.txt |   6 ++\n Documentation/config/worktree.txt   |  10 +++\n Documentation/git-worktree.txt      |   8 ++\n builtin/worktree.c                  |  29 ++++---\n repository.c                        |   1 +\n repository.h                        |   1 +\n setup.c                             |  39 ++++++---\n setup.h                             |   1 +\n t/t0001-init.sh                     |  22 ++++-\n t/t2400-worktree-add.sh             |  46 +++++++++++\n t/t2401-worktree-prune.sh           |   3 +-\n t/t2402-worktree-list.sh            |  22 +++++\n t/t2403-worktree-move.sh            |  25 ++++++\n t/t2406-worktree-repair.sh          |  39 +++++++++\n t/t2408-worktree-relative.sh        |  39 ---------\n t/t5504-fetch-receive-strict.sh     |   6 +-\n worktree.c                          | 157 ++++++++++++++++++++----------------\n worktree.h                          |  22 ++++-\n 18 files changed, 334 insertions(+), 142 deletions(-)\n---\nbase-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559\nchange-id: 20241025-wt_relative_options-afa41987bc32\n\nBest regards,\n-- \nCaleb White <cdwhite3@pm.me>\n\n\n"},{"id":"508349","messageId":"20241129-wt_relative_options-v6-1-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 1/8] setup: correctly reinitialize repository version","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:22:26Z","receivedAt":"2024-11-29T22:22:33Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"When reinitializing a repository, Git does not account for extensions\nother than `objectformat` and `refstorage` when determining the\nrepository version. This can lead to a repository being downgraded to\nversion 0 if extensions are set, causing Git future operations to fail.\n\nThis patch teaches Git to check if other extensions are defined in the\nconfig to ensure that the repository version is set correctly.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n setup.c                         | 32 +++++++++++++++++++++++---------\n t/t5504-fetch-receive-strict.sh |  6 +++---\n 2 files changed, 26 insertions(+), 12 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,\n \t\t\t\t   enum ref_storage_format ref_storage_format,\n \t\t\t\t   int reinit)\n {\n-\tchar repo_version_string[10];\n-\tint repo_version = GIT_REPO_VERSION;\n+\tstruct strbuf repo_version = STRBUF_INIT;\n+\tint target_version = GIT_REPO_VERSION;\n \n \t/*\n \t * Note that we initialize the repository version to 1 when the ref\n@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,\n \t */\n \tif (hash_algo != GIT_HASH_SHA1 ||\n \t    ref_storage_format != REF_STORAGE_FORMAT_FILES)\n-\t\trepo_version = GIT_REPO_VERSION_READ;\n-\n-\t/* This forces creation of new config file */\n-\txsnprintf(repo_version_string, sizeof(repo_version_string),\n-\t\t  \"%d\", repo_version);\n-\tgit_config_set(\"core.repositoryformatversion\", repo_version_string);\n+\t\ttarget_version = GIT_REPO_VERSION_READ;\n \n \tif (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)\n \t\tgit_config_set(\"extensions.objectformat\",\n@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,\n \t\t\t       ref_storage_format_to_name(ref_storage_format));\n \telse if (reinit)\n \t\tgit_config_set_gently(\"extensions.refstorage\", NULL);\n+\n+\tif (reinit) {\n+\t\tstruct strbuf config = STRBUF_INIT;\n+\t\tstruct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;\n+\n+\t\tstrbuf_git_common_path(&config, the_repository, \"config\");\n+\t\tread_repository_format(&repo_fmt, config.buf);\n+\n+\t\tif (repo_fmt.v1_only_extensions.nr)\n+\t\t\ttarget_version = GIT_REPO_VERSION_READ;\n+\n+\t\tstrbuf_release(&config);\n+\t\tclear_repository_format(&repo_fmt);\n+\t}\n+\n+\tstrbuf_addf(&repo_version, \"%d\", target_version);\n+\tgit_config_set(\"core.repositoryformatversion\", repo_version.buf);\n+\n+\tstrbuf_release(&repo_version);\n }\n \n static int is_reinit(void)\n@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,\n \t\tadjust_shared_perm(repo_get_git_dir(the_repository));\n \t}\n \n-\tinitialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);\n+\tinitialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);\n \n \t/* Check filemode trustability */\n \tpath = git_path_buf(&buf, \"config\");\ndiff --git a/t/t5504-fetch-receive-strict.sh b/t/t5504-fetch-receive-strict.sh\nindex 138e6778a477650ecbe2dc3e480c5fe83d4bb485..290d2a591adae02acf5bcf24dbbff2a0bbfceac8 100755\n--- a/t/t5504-fetch-receive-strict.sh\n+++ b/t/t5504-fetch-receive-strict.sh\n@@ -171,7 +171,7 @@ test_expect_success 'fsck with invalid or bogus skipList input' '\n \ttest_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \ttest_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err\n+\ttest_grep \"invalid object name: \" err\n '\n \n test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '\n@@ -234,7 +234,7 @@ test_expect_success 'push with receive.fsck.skipList' '\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \tgit --git-dir=dst/.git config receive.fsck.skipList config &&\n \ttest_must_fail git push --porcelain dst bogus 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err &&\n+\ttest_grep \"invalid object name: \" err &&\n \n \tgit --git-dir=dst/.git config receive.fsck.skipList SKIP &&\n \tgit push --porcelain dst bogus\n@@ -263,7 +263,7 @@ test_expect_success 'fetch with fetch.fsck.skipList' '\n \ttest_grep \"could not open.*: does-not-exist\" err &&\n \tgit --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&\n \ttest_must_fail git --git-dir=dst/.git fetch \"file://$(pwd)\" $refspec 2>err &&\n-\ttest_grep \"invalid object name: \\[core\\]\" err &&\n+\ttest_grep \"invalid object name: \" err &&\n \n \tgit --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&\n \tgit --git-dir=dst/.git fetch \"file://$(pwd)\" $refspec\n\n-- \n2.47.0\n\n\n"},{"id":"508350","messageId":"20241129-wt_relative_options-v6-2-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 2/8] worktree: add `relativeWorktrees` extension","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:22:34Z","receivedAt":"2024-11-29T22:22:41Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"A new extension, `relativeWorktrees`, is added to indicate that at least\none worktree in the repository has been linked with relative paths.\nThis ensures older Git versions do not attempt to automatically prune\nworktrees with relative paths, as they would not not recognize the\npaths as being valid.\n\nSuggested-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/config/extensions.txt | 6 ++++++\n repository.c                        | 1 +\n repository.h                        | 1 +\n setup.c                             | 7 +++++++\n setup.h                             | 1 +\n 5 files changed, 16 insertions(+)\n\ndiff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt\nindex 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644\n--- a/Documentation/config/extensions.txt\n+++ b/Documentation/config/extensions.txt\n@@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or\n linkgit:git-clone[1]. Trying to change it after initialization will not\n work and will produce hard-to-diagnose issues.\n \n+relativeWorktrees::\n+\tIf enabled, indicates at least one worktree has been linked with\n+\trelative paths. Automatically set if a worktree has been created or\n+\trepaired with either the `--relative-paths` option or with the\n+\t`worktree.useRelativePaths` config set to `true`.\n+\n worktreeConfig::\n \tIf enabled, then worktrees will load config settings from the\n \t`$GIT_DIR/config.worktree` file in addition to the\ndiff --git a/repository.c b/repository.c\nindex f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644\n--- a/repository.c\n+++ b/repository.c\n@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,\n \trepo_set_compat_hash_algo(repo, format.compat_hash_algo);\n \trepo_set_ref_storage_format(repo, format.ref_storage_format);\n \trepo->repository_format_worktree_config = format.worktree_config;\n+\trepo->repository_format_relative_worktrees = format.relative_worktrees;\n \n \t/* take ownership of format.partial_clone */\n \trepo->repository_format_partial_clone = format.partial_clone;\ndiff --git a/repository.h b/repository.h\nindex 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -150,6 +150,7 @@ struct repository {\n \n \t/* Configurations */\n \tint repository_format_worktree_config;\n+\tint repository_format_relative_worktrees;\n \n \t/* Indicate if a repository has a different 'commondir' from 'gitdir' */\n \tunsigned different_commondir:1;\ndiff --git a/setup.c b/setup.c\nindex 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var,\n \t\t\t\t     \"extensions.refstorage\", value);\n \t\tdata->ref_storage_format = format;\n \t\treturn EXTENSION_OK;\n+\t} else if (!strcmp(ext, \"relativeworktrees\")) {\n+\t\tdata->relative_worktrees = git_config_bool(var, value);\n+\t\treturn EXTENSION_OK;\n \t}\n \treturn EXTENSION_UNKNOWN;\n }\n@@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\t\t\t\t\t    repo_fmt.ref_storage_format);\n \t\t\tthe_repository->repository_format_worktree_config =\n \t\t\t\trepo_fmt.worktree_config;\n+\t\t\tthe_repository->repository_format_relative_worktrees =\n+\t\t\t\trepo_fmt.relative_worktrees;\n \t\t\t/* take ownership of repo_fmt.partial_clone */\n \t\t\tthe_repository->repository_format_partial_clone =\n \t\t\t\trepo_fmt.partial_clone;\n@@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt)\n \t\t\t\t    fmt->ref_storage_format);\n \tthe_repository->repository_format_worktree_config =\n \t\tfmt->worktree_config;\n+\tthe_repository->repository_format_relative_worktrees =\n+\t\tfmt->relative_worktrees;\n \tthe_repository->repository_format_partial_clone =\n \t\txstrdup_or_null(fmt->partial_clone);\n \tclear_repository_format(&repo_fmt);\ndiff --git a/setup.h b/setup.h\nindex e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644\n--- a/setup.h\n+++ b/setup.h\n@@ -129,6 +129,7 @@ struct repository_format {\n \tint precious_objects;\n \tchar *partial_clone; /* value of extensions.partialclone */\n \tint worktree_config;\n+\tint relative_worktrees;\n \tint is_bare;\n \tint hash_algo;\n \tint compat_hash_algo;\n\n-- \n2.47.0\n\n\n"},{"id":"508351","messageId":"20241129-wt_relative_options-v6-3-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 3/8] worktree: refactor infer_backlink return","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:22:41Z","receivedAt":"2024-11-29T22:22:47Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"The previous round[1] was merged a bit early before reviewer feedback\ncould be applied. This correctly indents a code block and updates the\n`infer_backlink` function to return `-1` on failure and strbuf.len on\nsuccess.\n\n[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n worktree.c | 15 ++++++++-------\n 1 file changed, 8 insertions(+), 7 deletions(-)\n\ndiff --git a/worktree.c b/worktree.c\nindex 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..afde5394c8760c328f12aa321be3ac5c199cc0f1 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id,\n \tstrbuf_strip_suffix(&worktree_path, \"/.git\");\n \n \tif (!is_absolute_path(worktree_path.buf)) {\n-\t    strbuf_strip_suffix(&path, \"gitdir\");\n-\t    strbuf_addbuf(&path, &worktree_path);\n-\t    strbuf_realpath_forgiving(&worktree_path, path.buf, 0);\n+\t\tstrbuf_strip_suffix(&path, \"gitdir\");\n+\t\tstrbuf_addbuf(&path, &worktree_path);\n+\t\tstrbuf_realpath_forgiving(&worktree_path, path.buf, 0);\n \t}\n \n \tCALLOC_ARRAY(worktree, 1);\n@@ -725,8 +725,10 @@ static int is_main_worktree_path(const char *path)\n  * won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may\n  * be able to infer the gitdir by manually reading /path/to/worktree/.git,\n  * extracting the <id>, and checking if <repo>/worktrees/<id> exists.\n+ *\n+ * Returns -1 on failure and strbuf.len on success.\n  */\n-static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n+static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n {\n \tstruct strbuf actual = STRBUF_INIT;\n \tconst char *id;\n@@ -747,12 +749,11 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)\n \t\tgoto error;\n \n \tstrbuf_release(&actual);\n-\treturn 1;\n-\n+\treturn inferred->len;\n error:\n \tstrbuf_release(&actual);\n \tstrbuf_reset(inferred); /* clear invalid path */\n-\treturn 0;\n+\treturn -1;\n }\n \n /*\n\n-- \n2.47.0\n\n\n"},{"id":"508352","messageId":"20241129-wt_relative_options-v6-4-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 4/8] worktree: add `write_worktree_linking_files()` function","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:22:47Z","receivedAt":"2024-11-29T22:22:52Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"A new helper function, `write_worktree_linking_files()`, centralizes\nthe logic for computing and writing either relative or absolute\npaths, based on the provided configuration. This function accepts\n`strbuf` pointers to both the worktree’s `.git` link and the\nrepository’s `gitdir`, and then writes the appropriate path to each.\nThe `relativeWorktrees` extension is automatically set when a worktree\nis linked with relative paths.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n worktree.c | 35 +++++++++++++++++++++++++++++++++++\n worktree.h | 13 +++++++++++++\n 2 files changed, 48 insertions(+)\n\ndiff --git a/worktree.c b/worktree.c\nindex afde5394c8760c328f12aa321be3ac5c199cc0f1..cf05045cc973f121a0a76b5ccfa731acf25d1a73 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -1032,3 +1032,38 @@ int init_worktree_config(struct repository *r)\n \tfree(main_worktree_file);\n \treturn res;\n }\n+\n+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,\n+\t\t\t\t  int use_relative_paths)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct strbuf repo = STRBUF_INIT;\n+\tstruct strbuf tmp = STRBUF_INIT;\n+\n+\tstrbuf_addbuf(&path, &dotgit);\n+\tstrbuf_strip_suffix(&path, \"/.git\");\n+\tstrbuf_realpath(&path, path.buf, 1);\n+\tstrbuf_addbuf(&repo, &gitdir);\n+\tstrbuf_strip_suffix(&repo, \"/gitdir\");\n+\tstrbuf_realpath(&repo, repo.buf, 1);\n+\n+\tif (use_relative_paths && !the_repository->repository_format_relative_worktrees) {\n+\t\tif (upgrade_repository_format(1) < 0)\n+\t\t\tdie(_(\"unable to upgrade repository format to support relative worktrees\"));\n+\t\tif (git_config_set_gently(\"extensions.relativeWorktrees\", \"true\"))\n+\t\t\tdie(_(\"unable to set extensions.relativeWorktrees setting\"));\n+\t\tthe_repository->repository_format_relative_worktrees = 1;\n+\t}\n+\n+\tif (use_relative_paths) {\n+\t\twrite_file(gitdir.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n+\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n+\t} else {\n+\t\twrite_file(gitdir.buf, \"%s/.git\", path.buf);\n+\t\twrite_file(dotgit.buf, \"gitdir: %s\", repo.buf);\n+\t}\n+\n+\tstrbuf_release(&path);\n+\tstrbuf_release(&repo);\n+\tstrbuf_release(&tmp);\n+}\ndiff --git a/worktree.h b/worktree.h\nindex e96118621638667d87c5d7e0452ed10bd1ddf606..fd040f5d999697b603df929679bdddd2ff7f6eea 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -215,4 +215,17 @@ void strbuf_worktree_ref(const struct worktree *wt,\n  */\n int init_worktree_config(struct repository *r);\n \n+/**\n+ * Write the .git file and gitdir file that links the worktree to the repository.\n+ *\n+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`\n+ * is the path to the repository's `gitdir` file.\n+ *\n+ * Example\n+ *  dotgit: \"/path/to/foo/.git\"\n+ *  gitdir: \"/path/to/repo/worktrees/foo/gitdir\"\n+ */\n+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,\n+\t\t\t\t  int use_relative_paths);\n+\n #endif\n\n-- \n2.47.0\n\n\n"},{"id":"508353","messageId":"20241129-wt_relative_options-v6-5-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 5/8] worktree: add relative cli/config options to `add` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:22:55Z","receivedAt":"2024-11-29T22:23:07Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This introduces the `--[no-]relative-paths` CLI option and\n`worktree.useRelativePaths` configuration setting to the `worktree add`\ncommand. When enabled these options allow worktrees to be linked using\nrelative paths, enhancing portability across environments where absolute\npaths may differ (e.g., containerized setups, shared network drives).\nGit still creates absolute paths by default, but these options allow\nusers to opt-in to relative paths if desired.\n\nThe t2408 test file is removed and more comprehensive tests are\nwritten for the various worktree operations in their own files.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/config/worktree.txt | 10 +++++++++\n Documentation/git-worktree.txt    |  5 +++++\n builtin/worktree.c                | 19 ++++++++--------\n t/t2400-worktree-add.sh           | 46 +++++++++++++++++++++++++++++++++++++++\n t/t2401-worktree-prune.sh         |  3 ++-\n t/t2402-worktree-list.sh          | 22 +++++++++++++++++++\n t/t2408-worktree-relative.sh      | 39 ---------------------------------\n 7 files changed, 95 insertions(+), 49 deletions(-)\n\ndiff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt\nindex 048e349482df6c892055720eb53cdcd6c327b6ed..5e35c7d018aecdedca0642b11e45df6d19024d42 100644\n--- a/Documentation/config/worktree.txt\n+++ b/Documentation/config/worktree.txt\n@@ -7,3 +7,13 @@ worktree.guessRemote::\n \tsuch a branch exists, it is checked out and set as \"upstream\"\n \tfor the new branch.  If no such match can be found, it falls\n \tback to creating a new branch from the current HEAD.\n+\n+worktree.useRelativePaths::\n+\tLink worktrees using relative paths (when \"true\") or absolute\n+\tpaths (when \"false\"). This is particularly useful for setups\n+\twhere the repository and worktrees may be moved between\n+\tdifferent locations or environments. Defaults to \"false\".\n++\n+Note that setting `worktree.useRelativePaths` to \"true\" implies enabling the\n+`extension.relativeWorktrees` config (see linkgit:git-config[1]),\n+thus making it incompatible with older versions of Git.\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 70437c815f13852bd2eb862176b8b933e6de0acf..60a671bbc255aa7763b4f77511c09f6a02783e00 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -216,6 +216,11 @@ To remove a locked worktree, specify `--force` twice.\n This can also be set up as the default behaviour by using the\n `worktree.guessRemote` config option.\n \n+--[no-]relative-paths::\n+\tLink worktrees using relative paths or absolute paths (default).\n+\tOverrides the `worktree.useRelativePaths` config option, see\n+\tlinkgit:git-config[1].\n+\n --[no-]track::\n \tWhen creating a new branch, if `<commit-ish>` is a branch,\n \tmark it as \"upstream\" from the new branch.  This is the\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -120,12 +120,14 @@ struct add_opts {\n \tint quiet;\n \tint checkout;\n \tint orphan;\n+\tint relative_paths;\n \tconst char *keep_locked;\n };\n \n static int show_only;\n static int verbose;\n static int guess_remote;\n+static int use_relative_paths;\n static timestamp_t expire;\n \n static int git_worktree_config(const char *var, const char *value,\n@@ -134,6 +136,9 @@ static int git_worktree_config(const char *var, const char *value,\n \tif (!strcmp(var, \"worktree.guessremote\")) {\n \t\tguess_remote = git_config_bool(var, value);\n \t\treturn 0;\n+\t} else if (!strcmp(var, \"worktree.userelativepaths\")) {\n+\t\tuse_relative_paths = git_config_bool(var, value);\n+\t\treturn 0;\n \t}\n \n \treturn git_default_config(var, value, ctx, cb);\n@@ -414,8 +419,7 @@ static int add_worktree(const char *path, const char *refname,\n \t\t\tconst struct add_opts *opts)\n {\n \tstruct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;\n-\tstruct strbuf sb = STRBUF_INIT, sb_tmp = STRBUF_INIT;\n-\tstruct strbuf sb_path_realpath = STRBUF_INIT, sb_repo_realpath = STRBUF_INIT;\n+\tstruct strbuf sb = STRBUF_INIT;\n \tconst char *name;\n \tstruct strvec child_env = STRVEC_INIT;\n \tunsigned int counter = 0;\n@@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname,\n \n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"%s/gitdir\", sb_repo.buf);\n-\tstrbuf_realpath(&sb_path_realpath, path, 1);\n-\tstrbuf_realpath(&sb_repo_realpath, sb_repo.buf, 1);\n-\twrite_file(sb.buf, \"%s/.git\", relative_path(sb_path_realpath.buf, sb_repo_realpath.buf, &sb_tmp));\n-\twrite_file(sb_git.buf, \"gitdir: %s\", relative_path(sb_repo_realpath.buf, sb_path_realpath.buf, &sb_tmp));\n+\twrite_worktree_linking_files(sb_git, sb, opts->relative_paths);\n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"%s/commondir\", sb_repo.buf);\n \twrite_file(sb.buf, \"../..\");\n@@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname,\n \n \tstrvec_clear(&child_env);\n \tstrbuf_release(&sb);\n-\tstrbuf_release(&sb_tmp);\n \tstrbuf_release(&symref);\n \tstrbuf_release(&sb_repo);\n-\tstrbuf_release(&sb_repo_realpath);\n \tstrbuf_release(&sb_git);\n-\tstrbuf_release(&sb_path_realpath);\n \tstrbuf_release(&sb_name);\n \tfree_worktree(wt);\n \treturn ret;\n@@ -796,12 +794,15 @@ static int add(int ac, const char **av, const char *prefix)\n \t\t\t     PARSE_OPT_NOARG | PARSE_OPT_OPTARG),\n \t\tOPT_BOOL(0, \"guess-remote\", &guess_remote,\n \t\t\t N_(\"try to match the new branch name with a remote-tracking branch\")),\n+\t\tOPT_BOOL(0, \"relative-paths\", &opts.relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tint ret;\n \n \tmemset(&opts, 0, sizeof(opts));\n \topts.checkout = 1;\n+\topts.relative_paths = use_relative_paths;\n \tac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);\n \tif (!!opts.detach + !!new_branch + !!new_branch_force > 1)\n \t\tdie(_(\"options '%s', '%s', and '%s' cannot be used together\"), \"-b\", \"-B\", \"--detach\");\ndiff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\nindex cfc4aeb1798c6d023909cec771e5b74e983af5ea..bc4f4e90d6ecfedbde9082bda6f9e4eec3e3575d 100755\n--- a/t/t2400-worktree-add.sh\n+++ b/t/t2400-worktree-add.sh\n@@ -1207,4 +1207,50 @@ test_expect_success '\"add\" with initialized submodule, with submodule.recurse se\n \tgit -C project-clone -c submodule.recurse worktree add ../project-5\n '\n \n+test_expect_success 'can create worktrees with relative paths' '\n+\ttest_when_finished \"git worktree remove relative\" &&\n+\ttest_config worktree.useRelativePaths false &&\n+\tgit worktree add --relative-paths ./relative &&\n+\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n+\ttest_cmp expect relative/.git &&\n+\techo \"../../../relative/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/relative/gitdir\n+'\n+\n+test_expect_success 'can create worktrees with absolute paths' '\n+\ttest_config worktree.useRelativePaths true &&\n+\tgit worktree add ./relative &&\n+\techo \"gitdir: ../.git/worktrees/relative\" >expect &&\n+\ttest_cmp expect relative/.git &&\n+\tgit worktree add --no-relative-paths ./absolute &&\n+\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect absolute/.git &&\n+\techo \"$(pwd)/absolute/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir\n+'\n+\n+test_expect_success 'move repo without breaking relative internal links' '\n+\ttest_when_finished rm -rf repo moved &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\tgit worktree add --relative-paths wt1 &&\n+\t\tcd .. &&\n+\t\tmv repo moved &&\n+\t\tcd moved/wt1 &&\n+\t\tgit worktree list >out 2>err &&\n+\t\ttest_must_be_empty err\n+\t)\n+'\n+\n+test_expect_success 'relative worktree sets extension config' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\tgit -C repo commit --allow-empty -m base &&\n+\tgit -C repo worktree add --relative-paths ./foo &&\n+\ttest_cmp_config -C repo 1 core.repositoryformatversion &&\n+\ttest_cmp_config -C repo true extensions.relativeworktrees\n+'\n+\n test_done\ndiff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh\nindex 976d048e3efc74be9cd909ce76d552b3944d2e10..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755\n--- a/t/t2401-worktree-prune.sh\n+++ b/t/t2401-worktree-prune.sh\n@@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' '\n \t! test -d .git/worktrees/wt\n '\n \n-test_expect_success 'not prune proper worktrees when run inside linked worktree' '\n+test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '\n \ttest_when_finished rm -rf repo wt_ext &&\n \tgit init repo &&\n \t(\n \t    cd repo &&\n+\t    git config worktree.useRelativePaths true &&\n \t    echo content >file &&\n \t    git add file &&\n \t    git commit -m msg &&\ndiff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh\nindex 33ea9cb21ba07c9563530b54da06753eaa993fe2..780daa6cd6351f8fa9434619cc212aade8f01420 100755\n--- a/t/t2402-worktree-list.sh\n+++ b/t/t2402-worktree-list.sh\n@@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' '\n '\n \n test_expect_success 'linked worktrees are sorted' '\n+\ttest_when_finished \"rm -rf sorted\" &&\n \tmkdir sorted &&\n \tgit init sorted/main &&\n \t(\n@@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' '\n \ttest_cmp expected sorted/main/actual\n '\n \n+test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '\n+\ttest_when_finished \"rm -rf sorted\" &&\n+\tmkdir sorted &&\n+\tgit init sorted/main &&\n+\t(\n+\t\tcd sorted/main &&\n+\t\ttest_tick &&\n+\t\ttest_commit new &&\n+\t\tgit worktree add --relative-paths ../first &&\n+\t\tgit worktree add ../second &&\n+\t\tgit worktree list --porcelain >out &&\n+\t\tgrep ^worktree out >actual\n+\t) &&\n+\tcat >expected <<-EOF &&\n+\tworktree $(pwd)/sorted/main\n+\tworktree $(pwd)/sorted/first\n+\tworktree $(pwd)/sorted/second\n+\tEOF\n+\ttest_cmp expected sorted/main/actual\n+'\n+\n test_expect_success 'worktree path when called in .git directory' '\n \tgit worktree list >list1 &&\n \tgit -C .git worktree list >list2 &&\ndiff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh\ndeleted file mode 100755\nindex a3136db7e28cb20926ff44211e246ce625a6e51a..0000000000000000000000000000000000000000\n--- a/t/t2408-worktree-relative.sh\n+++ /dev/null\n@@ -1,39 +0,0 @@\n-#!/bin/sh\n-\n-test_description='test worktrees linked with relative paths'\n-\n-TEST_PASSES_SANITIZE_LEAK=true\n-. ./test-lib.sh\n-\n-test_expect_success 'links worktrees with relative paths' '\n-\ttest_when_finished rm -rf repo &&\n-\tgit init repo &&\n-\t(\n-\t\tcd repo &&\n-\t\ttest_commit initial &&\n-\t\tgit worktree add wt1 &&\n-\t\techo \"../../../wt1/.git\" >expected_gitdir &&\n-\t\tcat .git/worktrees/wt1/gitdir >actual_gitdir &&\n-\t\techo \"gitdir: ../.git/worktrees/wt1\" >expected_git &&\n-\t\tcat wt1/.git >actual_git &&\n-\t\ttest_cmp expected_gitdir actual_gitdir &&\n-\t\ttest_cmp expected_git actual_git\n-\t)\n-'\n-\n-test_expect_success 'move repo without breaking relative internal links' '\n-\ttest_when_finished rm -rf repo moved &&\n-\tgit init repo &&\n-\t(\n-\t\tcd repo &&\n-\t\ttest_commit initial &&\n-\t\tgit worktree add wt1 &&\n-\t\tcd .. &&\n-\t\tmv repo moved &&\n-\t\tcd moved/wt1 &&\n-\t\tgit status >out 2>err &&\n-\t\ttest_must_be_empty err\n-\t)\n-'\n-\n-test_done\n\n-- \n2.47.0\n\n\n"},{"id":"508354","messageId":"20241129-wt_relative_options-v6-6-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 6/8] worktree: add relative cli/config options to `move` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:23:03Z","receivedAt":"2024-11-29T22:23:10Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This teaches the `worktree move` command to respect the\n`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\nconfig setting. If an existing worktree is moved with `--relative-paths`\nthe new path will be relative (and visa-versa).\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n builtin/worktree.c       |  4 +++-\n t/t2403-worktree-move.sh | 25 +++++++++++++++++++++++++\n worktree.c               | 22 +++++++++-------------\n worktree.h               |  4 ++--\n 4 files changed, 39 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n \t\tOPT__FORCE(&force,\n \t\t\t N_(\"force move even if worktree is dirty or locked\"),\n \t\t\t PARSE_OPT_NOCOMPLETE),\n+\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tstruct worktree **worktrees, *wt;\n@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)\n \tif (rename(wt->path, dst.buf) == -1)\n \t\tdie_errno(_(\"failed to move '%s' to '%s'\"), wt->path, dst.buf);\n \n-\tupdate_worktree_location(wt, dst.buf);\n+\tupdate_worktree_location(wt, dst.buf, use_relative_paths);\n \n \tstrbuf_release(&dst);\n \tfree_worktrees(worktrees);\ndiff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh\nindex 901342ea09b51a8e832f1109fbb737df84283aa2..422c1a05580057b18ab8bfdfe38da4d723749493 100755\n--- a/t/t2403-worktree-move.sh\n+++ b/t/t2403-worktree-move.sh\n@@ -247,4 +247,29 @@ test_expect_success 'not remove a repo with initialized submodule' '\n \t)\n '\n \n+test_expect_success 'move worktree with absolute path to relative path' '\n+\ttest_config worktree.useRelativePaths false &&\n+\tgit worktree add ./absolute &&\n+\tgit worktree move --relative-paths absolute relative &&\n+\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect relative/.git &&\n+\techo \"../../../relative/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir &&\n+\ttest_config worktree.useRelativePaths true &&\n+\tgit worktree move relative relative2 &&\n+\techo \"gitdir: ../.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect relative2/.git &&\n+\techo \"../../../relative2/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir\n+'\n+\n+test_expect_success 'move worktree with relative path to absolute path' '\n+\ttest_config worktree.useRelativePaths true &&\n+\tgit worktree move --no-relative-paths relative2 absolute &&\n+\techo \"gitdir: $(pwd)/.git/worktrees/absolute\" >expect &&\n+\ttest_cmp expect absolute/.git &&\n+\techo \"$(pwd)/absolute/.git\" >expect &&\n+\ttest_cmp expect .git/worktrees/absolute/gitdir\n+'\n+\n test_done\ndiff --git a/worktree.c b/worktree.c\nindex cf05045cc973f121a0a76b5ccfa731acf25d1a73..c749cb16994cf46ccccd4c2880ac917e497671b8 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -376,32 +376,28 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,\n \treturn ret;\n }\n \n-void update_worktree_location(struct worktree *wt, const char *path_)\n+void update_worktree_location(struct worktree *wt, const char *path_,\n+\t\t\t      int use_relative_paths)\n {\n \tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf repo = STRBUF_INIT;\n-\tstruct strbuf file = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n+\tstruct strbuf dotgit = STRBUF_INIT;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n \n \tif (is_main_worktree(wt))\n \t\tBUG(\"can't relocate main worktree\");\n \n-\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n+\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n \tstrbuf_realpath(&path, path_, 1);\n+\tstrbuf_addf(&dotgit, \"%s/.git\", path.buf);\n \tif (fspathcmp(wt->path, path.buf)) {\n-\t\tstrbuf_addf(&file, \"%s/gitdir\", repo.buf);\n-\t\twrite_file(file.buf, \"%s/.git\", relative_path(path.buf, repo.buf, &tmp));\n-\t\tstrbuf_reset(&file);\n-\t\tstrbuf_addf(&file, \"%s/.git\", path.buf);\n-\t\twrite_file(file.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \n \t\tfree(wt->path);\n \t\twt->path = strbuf_detach(&path, NULL);\n \t}\n \tstrbuf_release(&path);\n-\tstrbuf_release(&repo);\n-\tstrbuf_release(&file);\n-\tstrbuf_release(&tmp);\n+\tstrbuf_release(&dotgit);\n+\tstrbuf_release(&gitdir);\n }\n \n int is_worktree_being_rebased(const struct worktree *wt,\ndiff --git a/worktree.h b/worktree.h\nindex fd040f5d999697b603df929679bdddd2ff7f6eea..9c699d080d8ebf37712044136679db3821ee1f63 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -117,8 +117,8 @@ int validate_worktree(const struct worktree *wt,\n /*\n  * Update worktrees/xxx/gitdir with the new path.\n  */\n-void update_worktree_location(struct worktree *wt,\n-\t\t\t      const char *path_);\n+void update_worktree_location(struct worktree *wt, const char *path_,\n+\t\t\t      int use_relative_paths);\n \n typedef void (* worktree_repair_fn)(int iserr, const char *path,\n \t\t\t\t    const char *msg, void *cb_data);\n\n-- \n2.47.0\n\n\n"},{"id":"508355","messageId":"20241129-wt_relative_options-v6-7-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 7/8] worktree: add relative cli/config options to `repair` command","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:23:10Z","receivedAt":"2024-11-29T22:23:15Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This teaches the `worktree repair` command to respect the\n`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`\nconfig setting. If an existing worktree with an absolute path is repaired\nwith `--relative-paths`, the links will be replaced with relative paths,\neven if the original path was correct. This allows a user to covert\nexisting worktrees between absolute/relative as desired.\n\nTo simplify things, both linking files are written when one of the files\nneeds to be repaired. In some cases, this fixes the other file before it\nis checked, in other cases this results in a correct file being written\nwith the same contents.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n Documentation/git-worktree.txt |  3 +++\n builtin/worktree.c             |  6 +++--\n t/t2406-worktree-repair.sh     | 39 +++++++++++++++++++++++++++++\n worktree.c                     | 56 ++++++++++++++++++++----------------------\n worktree.h                     |  5 ++--\n 5 files changed, 76 insertions(+), 33 deletions(-)\n\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 60a671bbc255aa7763b4f77511c09f6a02783e00..8340b7f028e6c1c3bae3de0879e9754098466d14 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -220,6 +220,9 @@ This can also be set up as the default behaviour by using the\n \tLink worktrees using relative paths or absolute paths (default).\n \tOverrides the `worktree.useRelativePaths` config option, see\n \tlinkgit:git-config[1].\n++\n+With `repair`, the linking files will be updated if there's an absolute/relative\n+mismatch, even if the links are correct.\n \n --[no-]track::\n \tWhen creating a new branch, if `<commit-ish>` is a branch,\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 302151506981718658db1cd338cd9064688f5c14..fde9ff4dc9a734c655e95ccd62774282950cbba6 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -1385,6 +1385,8 @@ static int repair(int ac, const char **av, const char *prefix)\n \tconst char **p;\n \tconst char *self[] = { \".\", NULL };\n \tstruct option options[] = {\n+\t\tOPT_BOOL(0, \"relative-paths\", &use_relative_paths,\n+\t\t\t N_(\"use relative paths for worktrees\")),\n \t\tOPT_END()\n \t};\n \tint rc = 0;\n@@ -1392,8 +1394,8 @@ static int repair(int ac, const char **av, const char *prefix)\n \tac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);\n \tp = ac > 0 ? av : self;\n \tfor (; *p; p++)\n-\t\trepair_worktree_at_path(*p, report_repair, &rc);\n-\trepair_worktrees(report_repair, &rc);\n+\t\trepair_worktree_at_path(*p, report_repair, &rc, use_relative_paths);\n+\trepair_worktrees(report_repair, &rc, use_relative_paths);\n \treturn rc;\n }\n \ndiff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh\nindex 7686e60f6ad186519b275f11a5e14064c905b207..49b70b999518d47e1edd72a61a847b427f4c67a1 100755\n--- a/t/t2406-worktree-repair.sh\n+++ b/t/t2406-worktree-repair.sh\n@@ -216,4 +216,43 @@ test_expect_success 'repair copied main and linked worktrees' '\n \ttest_cmp dup/linked.expect dup/linked/.git\n '\n \n+test_expect_success 'repair worktree with relative path with missing gitfile' '\n+\ttest_when_finished \"rm -rf main wt\" &&\n+\ttest_create_repo main &&\n+\tgit -C main config worktree.useRelativePaths true &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --detach ../wt &&\n+\trm wt/.git &&\n+\ttest_path_is_missing wt/.git &&\n+\tgit -C main worktree repair &&\n+\techo \"gitdir: ../main/.git/worktrees/wt\" >expect &&\n+\ttest_cmp expect wt/.git\n+'\n+\n+test_expect_success 'repair absolute worktree to use relative paths' '\n+\ttest_when_finished \"rm -rf main side sidemoved\" &&\n+\ttest_create_repo main &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --detach ../side &&\n+\techo \"../../../../sidemoved/.git\" >expect-gitdir &&\n+\techo \"gitdir: ../main/.git/worktrees/side\" >expect-gitfile &&\n+\tmv side sidemoved &&\n+\tgit -C main worktree repair --relative-paths ../sidemoved &&\n+\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n+\ttest_cmp expect-gitfile sidemoved/.git\n+'\n+\n+test_expect_success 'repair relative worktree to use absolute paths' '\n+\ttest_when_finished \"rm -rf main side sidemoved\" &&\n+\ttest_create_repo main &&\n+\ttest_commit -C main init &&\n+\tgit -C main worktree add --relative-paths --detach ../side &&\n+\techo \"$(pwd)/sidemoved/.git\" >expect-gitdir &&\n+\techo \"gitdir: $(pwd)/main/.git/worktrees/side\" >expect-gitfile &&\n+\tmv side sidemoved &&\n+\tgit -C main worktree repair ../sidemoved &&\n+\ttest_cmp expect-gitdir main/.git/worktrees/side/gitdir &&\n+\ttest_cmp expect-gitfile sidemoved/.git\n+'\n+\n test_done\ndiff --git a/worktree.c b/worktree.c\nindex c749cb16994cf46ccccd4c2880ac917e497671b8..2e76bbc1494afc125997f803dc39846a0b95a84f 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -573,12 +573,13 @@ int other_head_refs(each_ref_fn fn, void *cb_data)\n  * pointing at <repo>/worktrees/<id>.\n  */\n static void repair_gitfile(struct worktree *wt,\n-\t\t\t   worktree_repair_fn fn, void *cb_data)\n+\t\t\t   worktree_repair_fn fn, void *cb_data,\n+\t\t\t   int use_relative_paths)\n {\n \tstruct strbuf dotgit = STRBUF_INIT;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf repo = STRBUF_INIT;\n \tstruct strbuf backlink = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n \tchar *dotgit_contents = NULL;\n \tconst char *repair = NULL;\n \tint err;\n@@ -594,6 +595,7 @@ static void repair_gitfile(struct worktree *wt,\n \n \tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n \tstrbuf_addf(&dotgit, \"%s/.git\", wt->path);\n+\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n \tdotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));\n \n \tif (dotgit_contents) {\n@@ -611,18 +613,20 @@ static void repair_gitfile(struct worktree *wt,\n \t\trepair = _(\".git file broken\");\n \telse if (fspathcmp(backlink.buf, repo.buf))\n \t\trepair = _(\".git file incorrect\");\n+\telse if (use_relative_paths == is_absolute_path(dotgit_contents))\n+\t\trepair = _(\".git file absolute/relative path mismatch\");\n \n \tif (repair) {\n \t\tfn(0, wt->path, repair, cb_data);\n-\t\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, wt->path, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \t}\n \n done:\n \tfree(dotgit_contents);\n \tstrbuf_release(&repo);\n \tstrbuf_release(&dotgit);\n+\tstrbuf_release(&gitdir);\n \tstrbuf_release(&backlink);\n-\tstrbuf_release(&tmp);\n }\n \n static void repair_noop(int iserr UNUSED,\n@@ -633,7 +637,7 @@ static void repair_noop(int iserr UNUSED,\n \t/* nothing */\n }\n \n-void repair_worktrees(worktree_repair_fn fn, void *cb_data)\n+void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths)\n {\n \tstruct worktree **worktrees = get_worktrees_internal(1);\n \tstruct worktree **wt = worktrees + 1; /* +1 skips main worktree */\n@@ -641,7 +645,7 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)\n \tif (!fn)\n \t\tfn = repair_noop;\n \tfor (; *wt; wt++)\n-\t\trepair_gitfile(*wt, fn, cb_data);\n+\t\trepair_gitfile(*wt, fn, cb_data, use_relative_paths);\n \tfree_worktrees(worktrees);\n }\n \n@@ -757,16 +761,14 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)\n  * the worktree's path.\n  */\n void repair_worktree_at_path(const char *path,\n-\t\t\t     worktree_repair_fn fn, void *cb_data)\n+\t\t\t     worktree_repair_fn fn, void *cb_data,\n+\t\t\t     int use_relative_paths)\n {\n \tstruct strbuf dotgit = STRBUF_INIT;\n-\tstruct strbuf realdotgit = STRBUF_INIT;\n \tstruct strbuf backlink = STRBUF_INIT;\n \tstruct strbuf inferred_backlink = STRBUF_INIT;\n \tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf olddotgit = STRBUF_INIT;\n-\tstruct strbuf realolddotgit = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n \tchar *dotgit_contents = NULL;\n \tconst char *repair = NULL;\n \tint err;\n@@ -778,25 +780,25 @@ void repair_worktree_at_path(const char *path,\n \t\tgoto done;\n \n \tstrbuf_addf(&dotgit, \"%s/.git\", path);\n-\tif (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {\n+\tif (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {\n \t\tfn(1, path, _(\"not a valid path\"), cb_data);\n \t\tgoto done;\n \t}\n \n-\tinfer_backlink(realdotgit.buf, &inferred_backlink);\n+\tinfer_backlink(dotgit.buf, &inferred_backlink);\n \tstrbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);\n-\tdotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));\n+\tdotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));\n \tif (dotgit_contents) {\n \t\tif (is_absolute_path(dotgit_contents)) {\n \t\t\tstrbuf_addstr(&backlink, dotgit_contents);\n \t\t} else {\n-\t\t\tstrbuf_addbuf(&backlink, &realdotgit);\n+\t\t\tstrbuf_addbuf(&backlink, &dotgit);\n \t\t\tstrbuf_strip_suffix(&backlink, \".git\");\n \t\t\tstrbuf_addstr(&backlink, dotgit_contents);\n \t\t\tstrbuf_realpath_forgiving(&backlink, backlink.buf, 0);\n \t\t}\n \t} else if (err == READ_GITFILE_ERR_NOT_A_FILE) {\n-\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git is not a file\"), cb_data);\n+\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git is not a file\"), cb_data);\n \t\tgoto done;\n \t} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {\n \t\tif (inferred_backlink.len) {\n@@ -809,11 +811,11 @@ void repair_worktree_at_path(const char *path,\n \t\t\t */\n \t\t\tstrbuf_swap(&backlink, &inferred_backlink);\n \t\t} else {\n-\t\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git file does not reference a repository\"), cb_data);\n+\t\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git file does not reference a repository\"), cb_data);\n \t\t\tgoto done;\n \t\t}\n \t} else {\n-\t\tfn(1, realdotgit.buf, _(\"unable to locate repository; .git file broken\"), cb_data);\n+\t\tfn(1, dotgit.buf, _(\"unable to locate repository; .git file broken\"), cb_data);\n \t\tgoto done;\n \t}\n \n@@ -835,39 +837,35 @@ void repair_worktree_at_path(const char *path,\n \t * in the \"copy\" repository. In this case, point the \"copy\" worktree's\n \t * .git file at the \"copy\" repository.\n \t */\n-\tif (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) {\n+\tif (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))\n \t\tstrbuf_swap(&backlink, &inferred_backlink);\n-\t}\n \n \tstrbuf_addf(&gitdir, \"%s/gitdir\", backlink.buf);\n \tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n \t\trepair = _(\"gitdir unreadable\");\n+\telse if (use_relative_paths == is_absolute_path(olddotgit.buf))\n+\t\trepair = _(\"gitdir absolute/relative path mismatch\");\n \telse {\n \t\tstrbuf_rtrim(&olddotgit);\n-\t\tif (is_absolute_path(olddotgit.buf)) {\n-\t\t\tstrbuf_addbuf(&realolddotgit, &olddotgit);\n-\t\t} else {\n-\t\t\tstrbuf_addf(&realolddotgit, \"%s/%s\", backlink.buf, olddotgit.buf);\n-\t\t\tstrbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0);\n+\t\tif (!is_absolute_path(olddotgit.buf)) {\n+\t\t\tstrbuf_insertf(&olddotgit, 0, \"%s/\", backlink.buf);\n+\t\t\tstrbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0);\n \t\t}\n-\t\tif (fspathcmp(realolddotgit.buf, realdotgit.buf))\n+\t\tif (fspathcmp(olddotgit.buf, dotgit.buf))\n \t\t\trepair = _(\"gitdir incorrect\");\n \t}\n \n \tif (repair) {\n \t\tfn(0, gitdir.buf, repair, cb_data);\n-\t\twrite_file(gitdir.buf, \"%s\", relative_path(realdotgit.buf, backlink.buf, &tmp));\n+\t\twrite_worktree_linking_files(dotgit, gitdir, use_relative_paths);\n \t}\n done:\n \tfree(dotgit_contents);\n \tstrbuf_release(&olddotgit);\n-\tstrbuf_release(&realolddotgit);\n \tstrbuf_release(&backlink);\n \tstrbuf_release(&inferred_backlink);\n \tstrbuf_release(&gitdir);\n-\tstrbuf_release(&realdotgit);\n \tstrbuf_release(&dotgit);\n-\tstrbuf_release(&tmp);\n }\n \n int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)\ndiff --git a/worktree.h b/worktree.h\nindex 9c699d080d8ebf37712044136679db3821ee1f63..38145df80f41079f301d3aeaaffcc35b7f6760b9 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -129,7 +129,7 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,\n  * function, if non-NULL, is called with the path of the worktree and a\n  * description of the repair or error, along with the callback user-data.\n  */\n-void repair_worktrees(worktree_repair_fn, void *cb_data);\n+void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths);\n \n /*\n  * Repair the linked worktrees after the gitdir has been moved.\n@@ -151,7 +151,8 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path\n  * worktree and a description of the repair or error, along with the callback\n  * user-data.\n  */\n-void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);\n+void repair_worktree_at_path(const char *, worktree_repair_fn,\n+\t\t\t     void *cb_data, int use_relative_paths);\n \n /*\n  * Free up the memory for a worktree.\n\n-- \n2.47.0\n\n\n"},{"id":"508356","messageId":"20241129-wt_relative_options-v6-8-44e4e0bec8c3@pm.me","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"[PATCH v6 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-11-29T22:23:16Z","receivedAt":"2024-11-29T22:23:23Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"This refactors `repair_worktree_after_gitdir_move()` to use the new\n`write_worktree_linking_files` function. It also preserves the\nrelativity of the linking files; e.g., if an existing worktree used\nabsolute paths then the repaired paths will be absolute (and visa-versa).\n`repair_worktree_after_gitdir_move()` is used to repair both sets of\nworktree linking files if the `.git` directory is moved during a\nre-initialization using `git init`.\n\nThis also adds a test case for reinitializing a repository that has\nrelative worktrees.\n\nSigned-off-by: Caleb White <cdwhite3@pm.me>\n---\n t/t0001-init.sh | 22 ++++++++++++++++++----\n worktree.c      | 29 ++++++++---------------------\n 2 files changed, 26 insertions(+), 25 deletions(-)\n\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e394147b8480fb5784f1c23c0d2eae712eecc44b 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '\n sep_git_dir_worktree ()  {\n \ttest_when_finished \"rm -rf mainwt linkwt seprepo\" &&\n \tgit init mainwt &&\n+\tif test \"relative\" = $2\n+\tthen\n+\t\ttest_config -C mainwt worktree.useRelativePaths true\n+\telse\n+\t\ttest_config -C mainwt worktree.useRelativePaths false\n+\tfi\n \ttest_commit -C mainwt gumby &&\n \tgit -C mainwt worktree add --detach ../linkwt &&\n \tgit -C \"$1\" init --separate-git-dir ../seprepo &&\n@@ -442,12 +448,20 @@ sep_git_dir_worktree ()  {\n \ttest_cmp expect actual\n }\n \n-test_expect_success 're-init to move gitdir with linked worktrees' '\n-\tsep_git_dir_worktree mainwt\n+test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '\n+\tsep_git_dir_worktree mainwt absolute\n+'\n+\n+test_expect_success 're-init to move gitdir within linked worktree (absolute)' '\n+\tsep_git_dir_worktree linkwt absolute\n+'\n+\n+test_expect_success 're-init to move gitdir with linked worktrees (relative)' '\n+\tsep_git_dir_worktree mainwt relative\n '\n \n-test_expect_success 're-init to move gitdir within linked worktree' '\n-\tsep_git_dir_worktree linkwt\n+test_expect_success 're-init to move gitdir within linked worktree (relative)' '\n+\tsep_git_dir_worktree linkwt relative\n '\n \n test_expect_success MINGW '.git hidden' '\ndiff --git a/worktree.c b/worktree.c\nindex 2e76bbc1494afc125997f803dc39846a0b95a84f..af68b24f9d0917a99cd965347aad6466e9dbb152 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -651,45 +651,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat\n \n void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)\n {\n-\tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf repo = STRBUF_INIT;\n \tstruct strbuf gitdir = STRBUF_INIT;\n \tstruct strbuf dotgit = STRBUF_INIT;\n-\tstruct strbuf olddotgit = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n+\tint is_relative_path;\n \n \tif (is_main_worktree(wt))\n \t\tgoto done;\n \n-\tstrbuf_realpath(&repo, git_common_path(\"worktrees/%s\", wt->id), 1);\n-\tstrbuf_addf(&gitdir, \"%s/gitdir\", repo.buf);\n+\tstrbuf_realpath(&gitdir, git_common_path(\"worktrees/%s/gitdir\", wt->id), 1);\n \n-\tif (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)\n+\tif (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)\n \t\tgoto done;\n \n-\tstrbuf_rtrim(&olddotgit);\n-\tif (is_absolute_path(olddotgit.buf)) {\n-\t\tstrbuf_addbuf(&dotgit, &olddotgit);\n-\t} else {\n-\t\tstrbuf_addf(&dotgit, \"%s/worktrees/%s/%s\", old_path, wt->id, olddotgit.buf);\n+\tstrbuf_rtrim(&dotgit);\n+\tis_relative_path = ! is_absolute_path(dotgit.buf);\n+\tif (is_relative_path) {\n+\t\tstrbuf_insertf(&dotgit, 0, \"%s/worktrees/%s/\", old_path, wt->id);\n \t\tstrbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);\n \t}\n \n \tif (!file_exists(dotgit.buf))\n \t\tgoto done;\n \n-\tstrbuf_addbuf(&path, &dotgit);\n-\tstrbuf_strip_suffix(&path, \"/.git\");\n-\n-\twrite_file(dotgit.buf, \"gitdir: %s\", relative_path(repo.buf, path.buf, &tmp));\n-\twrite_file(gitdir.buf, \"%s\", relative_path(dotgit.buf, repo.buf, &tmp));\n+\twrite_worktree_linking_files(dotgit, gitdir, is_relative_path);\n done:\n-\tstrbuf_release(&path);\n-\tstrbuf_release(&repo);\n \tstrbuf_release(&gitdir);\n \tstrbuf_release(&dotgit);\n-\tstrbuf_release(&olddotgit);\n-\tstrbuf_release(&tmp);\n }\n \n void repair_worktrees_after_gitdir_move(const char *old_path)\n\n-- \n2.47.0\n\n\n"},{"id":"508453","messageId":"103c7a4d-a74b-4381-a9c7-33a89d5db192@gmail.com","threadId":"62433","inReplyTo":"20241129-wt_relative_options-v6-0-44e4e0bec8c3@pm.me","subject":"Re: [PATCH v6 0/8] Allow relative worktree linking to be configured by the user","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-12-02T14:57:38Z","receivedAt":"2024-12-02T14:57:42Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Caleb\n\nOn 29/11/2024 22:22, Caleb White wrote:\n> Changes in v6:\n> - Re-add test for extensions.relativeworktrees config setting\n> - Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me\n\nThe range-diff below looks good, thanks for working on this\nI think it's ready for next\n\nBest Wishes\n\nPhillip\n\n1:  0986f98022 = 1:  d897f2c16d setup: correctly reinitialize repository version\n2:  c36e1a59fa = 2:  1860ba1a2a worktree: add `relativeWorktrees` extension\n3:  5b19b63040 = 3:  5976310916 worktree: refactor infer_backlink return\n4:  ec143ae00e = 4:  4dac9e3c01 worktree: add `write_worktree_linking_files()` function\n5:  237206b08f ! 5:  b7016344f1 worktree: add relative cli/config options to `add` command\n     @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" with initialized submodule,\n      +  git init repo &&\n      +  git -C repo commit --allow-empty -m base &&\n      +  git -C repo worktree add --relative-paths ./foo &&\n     -+  test_cmp_config -C repo 1 core.repositoryformatversion\n     ++  test_cmp_config -C repo 1 core.repositoryformatversion &&\n     ++  test_cmp_config -C repo true extensions.relativeworktrees\n      +'\n      +\n       test_done\n6:  8e4307f520 = 6:  298d2917e2 worktree: add relative cli/config options to `move` command\n7:  4f951f4088 = 7:  e6df1ee2c1 worktree: add relative cli/config options to `repair` command\n8:  28eb7f66b2 = 8:  2037ca85ad worktree: refactor `repair_worktree_after_gitdir_move()`\n\n> Changes in v5:\n> - Added docs to `--relative-paths` option.\n> - Added test coverage for `repair_worktrees()` and relative paths.\n> - Move `strbuf_reset` call in `infer_backlink()`.\n> - Cleaned up tests.\n> - Slight stylistic changes.\n> - Tweaked commit messages.\n> - Updated base to 090d24e9af.\n> - Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me\n> Changes in v4:\n> - Fixed failing test in ci\n> - Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me\n> Changes in v3:\n> - Split patches into smaller edits.\n> - Moved tests into the patches with the relevant code changes.\n> - Removed global `use_relative_paths` and instead pass parameter to functions.\n> - Changed `infer_backlink` return type from `int` to `ssize_t`.\n> - Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.\n> - Reordered patches\n> - Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me\n> Changes in v2:\n> - Fixed a bug where repositories with valid extensions would be downgraded\n>    to v0 during reinitialization, causing future operations to fail.\n> - Split patch [1/2] into three separate patches.\n> - Updated cover letter and commit messages.\n> - Updated documentation wording.\n> - Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me\n> \n> ---\n> Caleb White (8):\n>        setup: correctly reinitialize repository version\n>        worktree: add `relativeWorktrees` extension\n>        worktree: refactor infer_backlink return\n>        worktree: add `write_worktree_linking_files()` function\n>        worktree: add relative cli/config options to `add` command\n>        worktree: add relative cli/config options to `move` command\n>        worktree: add relative cli/config options to `repair` command\n>        worktree: refactor `repair_worktree_after_gitdir_move()`\n> \n>   Documentation/config/extensions.txt |   6 ++\n>   Documentation/config/worktree.txt   |  10 +++\n>   Documentation/git-worktree.txt      |   8 ++\n>   builtin/worktree.c                  |  29 ++++---\n>   repository.c                        |   1 +\n>   repository.h                        |   1 +\n>   setup.c                             |  39 ++++++---\n>   setup.h                             |   1 +\n>   t/t0001-init.sh                     |  22 ++++-\n>   t/t2400-worktree-add.sh             |  46 +++++++++++\n>   t/t2401-worktree-prune.sh           |   3 +-\n>   t/t2402-worktree-list.sh            |  22 +++++\n>   t/t2403-worktree-move.sh            |  25 ++++++\n>   t/t2406-worktree-repair.sh          |  39 +++++++++\n>   t/t2408-worktree-relative.sh        |  39 ---------\n>   t/t5504-fetch-receive-strict.sh     |   6 +-\n>   worktree.c                          | 157 ++++++++++++++++++++----------------\n>   worktree.h                          |  22 ++++-\n>   18 files changed, 334 insertions(+), 142 deletions(-)\n> ---\n> base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559\n> change-id: 20241025-wt_relative_options-afa41987bc32\n> \n> Best regards,\n\n"},{"id":"508512","messageId":"xmqq4j3le57m.fsf@gitster.g","threadId":"62433","inReplyTo":"103c7a4d-a74b-4381-a9c7-33a89d5db192@gmail.com","subject":"Re: [PATCH v6 0/8] Allow relative worktree linking to be configured by the user","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-12-03T04:54:53Z","receivedAt":"2024-12-03T04:54:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Hi Caleb\n>\n> On 29/11/2024 22:22, Caleb White wrote:\n>> Changes in v6:\n>> - Re-add test for extensions.relativeworktrees config setting\n>> - Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me\n>\n> The range-diff below looks good, thanks for working on this\n> I think it's ready for next\n>\n> Best Wishes\n>\n> Phillip\n\nThanks, both.\n\nLet me mark the topic for 'next'.\n"},{"id":"508513","messageId":"D61TAOKLO2ZI.3U0UMVRVO14EW@pm.me","threadId":"62433","inReplyTo":"xmqq4j3le57m.fsf@gitster.g","subject":"Re: [PATCH v6 0/8] Allow relative worktree linking to be configured by the user","fromName":"Caleb White","fromEmail":"cdwhite3@pm.me","sentAt":"2024-12-03T05:21:52Z","receivedAt":"2024-12-03T05:22:06Z","isPatch":true,"sender":{"key":"cdwhite3@pm.me","avatar":"https://avatars.githubusercontent.com/u/4176520?v=4"},"body":"On Mon Dec 2, 2024 at 10:54 PM CST, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n>> Hi Caleb\n>>\n>> On 29/11/2024 22:22, Caleb White wrote:\n>>> Changes in v6:\n>>> - Re-add test for extensions.relativeworktrees config setting\n>>> - Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me\n>>\n>> The range-diff below looks good, thanks for working on this\n>> I think it's ready for next\n>>\n>> Best Wishes\n>>\n>> Phillip\n>\n> Thanks, both.\n>\n> Let me mark the topic for 'next'.\n\nAwesome, thanks!\n\n"}]}