* [PATCH v5 1/8] setup: correctly reinitialize repository version
2024-11-26 1:51 ` [PATCH v5 " Caleb White
@ 2024-11-26 1:51 ` Caleb White
2024-11-26 1:51 ` [PATCH v5 2/8] worktree: add `relativeWorktrees` extension Caleb White
` (9 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:51 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
When reinitializing a repository, Git does not account for extensions
other than `objectformat` and `refstorage` when determining the
repository version. This can lead to a repository being downgraded to
version 0 if extensions are set, causing Git future operations to fail.
This patch teaches Git to check if other extensions are defined in the
config to ensure that the repository version is set correctly.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
setup.c | 32 +++++++++++++++++++++++---------
t/t5504-fetch-receive-strict.sh | 6 +++---
2 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/setup.c b/setup.c
index 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644
--- a/setup.c
+++ b/setup.c
@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,
enum ref_storage_format ref_storage_format,
int reinit)
{
- char repo_version_string[10];
- int repo_version = GIT_REPO_VERSION;
+ struct strbuf repo_version = STRBUF_INIT;
+ int target_version = GIT_REPO_VERSION;
/*
* Note that we initialize the repository version to 1 when the ref
@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,
*/
if (hash_algo != GIT_HASH_SHA1 ||
ref_storage_format != REF_STORAGE_FORMAT_FILES)
- repo_version = GIT_REPO_VERSION_READ;
-
- /* This forces creation of new config file */
- xsnprintf(repo_version_string, sizeof(repo_version_string),
- "%d", repo_version);
- git_config_set("core.repositoryformatversion", repo_version_string);
+ target_version = GIT_REPO_VERSION_READ;
if (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)
git_config_set("extensions.objectformat",
@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,
ref_storage_format_to_name(ref_storage_format));
else if (reinit)
git_config_set_gently("extensions.refstorage", NULL);
+
+ if (reinit) {
+ struct strbuf config = STRBUF_INIT;
+ struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
+
+ strbuf_git_common_path(&config, the_repository, "config");
+ read_repository_format(&repo_fmt, config.buf);
+
+ if (repo_fmt.v1_only_extensions.nr)
+ target_version = GIT_REPO_VERSION_READ;
+
+ strbuf_release(&config);
+ clear_repository_format(&repo_fmt);
+ }
+
+ strbuf_addf(&repo_version, "%d", target_version);
+ git_config_set("core.repositoryformatversion", repo_version.buf);
+
+ strbuf_release(&repo_version);
}
static int is_reinit(void)
@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,
adjust_shared_perm(repo_get_git_dir(the_repository));
}
- initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);
+ initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);
/* Check filemode trustability */
path = git_path_buf(&buf, "config");
diff --git a/t/t5504-fetch-receive-strict.sh b/t/t5504-fetch-receive-strict.sh
index 138e6778a477650ecbe2dc3e480c5fe83d4bb485..290d2a591adae02acf5bcf24dbbff2a0bbfceac8 100755
--- a/t/t5504-fetch-receive-strict.sh
+++ b/t/t5504-fetch-receive-strict.sh
@@ -171,7 +171,7 @@ test_expect_success 'fsck with invalid or bogus skipList input' '
test_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&
test_grep "could not open.*: does-not-exist" err &&
test_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&
- test_grep "invalid object name: \[core\]" err
+ test_grep "invalid object name: " err
'
test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '
@@ -234,7 +234,7 @@ test_expect_success 'push with receive.fsck.skipList' '
test_grep "could not open.*: does-not-exist" err &&
git --git-dir=dst/.git config receive.fsck.skipList config &&
test_must_fail git push --porcelain dst bogus 2>err &&
- test_grep "invalid object name: \[core\]" err &&
+ test_grep "invalid object name: " err &&
git --git-dir=dst/.git config receive.fsck.skipList SKIP &&
git push --porcelain dst bogus
@@ -263,7 +263,7 @@ test_expect_success 'fetch with fetch.fsck.skipList' '
test_grep "could not open.*: does-not-exist" err &&
git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&
test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
- test_grep "invalid object name: \[core\]" err &&
+ test_grep "invalid object name: " err &&
git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&
git --git-dir=dst/.git fetch "file://$(pwd)" $refspec
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 2/8] worktree: add `relativeWorktrees` extension
2024-11-26 1:51 ` [PATCH v5 " Caleb White
2024-11-26 1:51 ` [PATCH v5 1/8] setup: correctly reinitialize repository version Caleb White
@ 2024-11-26 1:51 ` Caleb White
2024-11-26 1:51 ` [PATCH v5 3/8] worktree: refactor infer_backlink return Caleb White
` (8 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:51 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
A new extension, `relativeWorktrees`, is added to indicate that at least
one worktree in the repository has been linked with relative paths.
This ensures older Git versions do not attempt to automatically prune
worktrees with relative paths, as they would not not recognize the
paths as being valid.
Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/config/extensions.txt | 6 ++++++
repository.c | 1 +
repository.h | 1 +
setup.c | 7 +++++++
setup.h | 1 +
5 files changed, 16 insertions(+)
diff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt
index 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644
--- a/Documentation/config/extensions.txt
+++ b/Documentation/config/extensions.txt
@@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or
linkgit:git-clone[1]. Trying to change it after initialization will not
work and will produce hard-to-diagnose issues.
+relativeWorktrees::
+ If enabled, indicates at least one worktree has been linked with
+ relative paths. Automatically set if a worktree has been created or
+ repaired with either the `--relative-paths` option or with the
+ `worktree.useRelativePaths` config set to `true`.
+
worktreeConfig::
If enabled, then worktrees will load config settings from the
`$GIT_DIR/config.worktree` file in addition to the
diff --git a/repository.c b/repository.c
index f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644
--- a/repository.c
+++ b/repository.c
@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,
repo_set_compat_hash_algo(repo, format.compat_hash_algo);
repo_set_ref_storage_format(repo, format.ref_storage_format);
repo->repository_format_worktree_config = format.worktree_config;
+ repo->repository_format_relative_worktrees = format.relative_worktrees;
/* take ownership of format.partial_clone */
repo->repository_format_partial_clone = format.partial_clone;
diff --git a/repository.h b/repository.h
index 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644
--- a/repository.h
+++ b/repository.h
@@ -150,6 +150,7 @@ struct repository {
/* Configurations */
int repository_format_worktree_config;
+ int repository_format_relative_worktrees;
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
unsigned different_commondir:1;
diff --git a/setup.c b/setup.c
index 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 100644
--- a/setup.c
+++ b/setup.c
@@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var,
"extensions.refstorage", value);
data->ref_storage_format = format;
return EXTENSION_OK;
+ } else if (!strcmp(ext, "relativeworktrees")) {
+ data->relative_worktrees = git_config_bool(var, value);
+ return EXTENSION_OK;
}
return EXTENSION_UNKNOWN;
}
@@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
repo_fmt.ref_storage_format);
the_repository->repository_format_worktree_config =
repo_fmt.worktree_config;
+ the_repository->repository_format_relative_worktrees =
+ repo_fmt.relative_worktrees;
/* take ownership of repo_fmt.partial_clone */
the_repository->repository_format_partial_clone =
repo_fmt.partial_clone;
@@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt)
fmt->ref_storage_format);
the_repository->repository_format_worktree_config =
fmt->worktree_config;
+ the_repository->repository_format_relative_worktrees =
+ fmt->relative_worktrees;
the_repository->repository_format_partial_clone =
xstrdup_or_null(fmt->partial_clone);
clear_repository_format(&repo_fmt);
diff --git a/setup.h b/setup.h
index e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644
--- a/setup.h
+++ b/setup.h
@@ -129,6 +129,7 @@ struct repository_format {
int precious_objects;
char *partial_clone; /* value of extensions.partialclone */
int worktree_config;
+ int relative_worktrees;
int is_bare;
int hash_algo;
int compat_hash_algo;
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 3/8] worktree: refactor infer_backlink return
2024-11-26 1:51 ` [PATCH v5 " Caleb White
2024-11-26 1:51 ` [PATCH v5 1/8] setup: correctly reinitialize repository version Caleb White
2024-11-26 1:51 ` [PATCH v5 2/8] worktree: add `relativeWorktrees` extension Caleb White
@ 2024-11-26 1:51 ` Caleb White
2024-11-26 1:51 ` [PATCH v5 4/8] worktree: add `write_worktree_linking_files()` function Caleb White
` (7 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:51 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
The previous round[1] was merged a bit early before reviewer feedback
could be applied. This correctly indents a code block and updates the
`infer_backlink` function to return `-1` on failure and strbuf.len on
success.
[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
worktree.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/worktree.c b/worktree.c
index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..afde5394c8760c328f12aa321be3ac5c199cc0f1 100644
--- a/worktree.c
+++ b/worktree.c
@@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id,
strbuf_strip_suffix(&worktree_path, "/.git");
if (!is_absolute_path(worktree_path.buf)) {
- strbuf_strip_suffix(&path, "gitdir");
- strbuf_addbuf(&path, &worktree_path);
- strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
+ strbuf_strip_suffix(&path, "gitdir");
+ strbuf_addbuf(&path, &worktree_path);
+ strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
}
CALLOC_ARRAY(worktree, 1);
@@ -725,8 +725,10 @@ static int is_main_worktree_path(const char *path)
* won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
* be able to infer the gitdir by manually reading /path/to/worktree/.git,
* extracting the <id>, and checking if <repo>/worktrees/<id> exists.
+ *
+ * Returns -1 on failure and strbuf.len on success.
*/
-static int infer_backlink(const char *gitfile, struct strbuf *inferred)
+static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
{
struct strbuf actual = STRBUF_INIT;
const char *id;
@@ -747,12 +749,11 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)
goto error;
strbuf_release(&actual);
- return 1;
-
+ return inferred->len;
error:
strbuf_release(&actual);
strbuf_reset(inferred); /* clear invalid path */
- return 0;
+ return -1;
}
/*
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 4/8] worktree: add `write_worktree_linking_files()` function
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (2 preceding siblings ...)
2024-11-26 1:51 ` [PATCH v5 3/8] worktree: refactor infer_backlink return Caleb White
@ 2024-11-26 1:51 ` Caleb White
2024-11-26 1:52 ` [PATCH v5 5/8] worktree: add relative cli/config options to `add` command Caleb White
` (6 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:51 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
A new helper function, `write_worktree_linking_files()`, centralizes
the logic for computing and writing either relative or absolute
paths, based on the provided configuration. This function accepts
`strbuf` pointers to both the worktree’s `.git` link and the
repository’s `gitdir`, and then writes the appropriate path to each.
The `relativeWorktrees` extension is automatically set when a worktree
is linked with relative paths.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
worktree.c | 35 +++++++++++++++++++++++++++++++++++
worktree.h | 13 +++++++++++++
2 files changed, 48 insertions(+)
diff --git a/worktree.c b/worktree.c
index afde5394c8760c328f12aa321be3ac5c199cc0f1..cf05045cc973f121a0a76b5ccfa731acf25d1a73 100644
--- a/worktree.c
+++ b/worktree.c
@@ -1032,3 +1032,38 @@ int init_worktree_config(struct repository *r)
free(main_worktree_file);
return res;
}
+
+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
+ int use_relative_paths)
+{
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf repo = STRBUF_INIT;
+ struct strbuf tmp = STRBUF_INIT;
+
+ strbuf_addbuf(&path, &dotgit);
+ strbuf_strip_suffix(&path, "/.git");
+ strbuf_realpath(&path, path.buf, 1);
+ strbuf_addbuf(&repo, &gitdir);
+ strbuf_strip_suffix(&repo, "/gitdir");
+ strbuf_realpath(&repo, repo.buf, 1);
+
+ if (use_relative_paths && !the_repository->repository_format_relative_worktrees) {
+ if (upgrade_repository_format(1) < 0)
+ die(_("unable to upgrade repository format to support relative worktrees"));
+ if (git_config_set_gently("extensions.relativeWorktrees", "true"))
+ die(_("unable to set extensions.relativeWorktrees setting"));
+ the_repository->repository_format_relative_worktrees = 1;
+ }
+
+ if (use_relative_paths) {
+ write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
+ write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
+ } else {
+ write_file(gitdir.buf, "%s/.git", path.buf);
+ write_file(dotgit.buf, "gitdir: %s", repo.buf);
+ }
+
+ strbuf_release(&path);
+ strbuf_release(&repo);
+ strbuf_release(&tmp);
+}
diff --git a/worktree.h b/worktree.h
index e96118621638667d87c5d7e0452ed10bd1ddf606..fd040f5d999697b603df929679bdddd2ff7f6eea 100644
--- a/worktree.h
+++ b/worktree.h
@@ -215,4 +215,17 @@ void strbuf_worktree_ref(const struct worktree *wt,
*/
int init_worktree_config(struct repository *r);
+/**
+ * Write the .git file and gitdir file that links the worktree to the repository.
+ *
+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
+ * is the path to the repository's `gitdir` file.
+ *
+ * Example
+ * dotgit: "/path/to/foo/.git"
+ * gitdir: "/path/to/repo/worktrees/foo/gitdir"
+ */
+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
+ int use_relative_paths);
+
#endif
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 5/8] worktree: add relative cli/config options to `add` command
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (3 preceding siblings ...)
2024-11-26 1:51 ` [PATCH v5 4/8] worktree: add `write_worktree_linking_files()` function Caleb White
@ 2024-11-26 1:52 ` Caleb White
2024-11-26 1:52 ` [PATCH v5 6/8] worktree: add relative cli/config options to `move` command Caleb White
` (5 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:52 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This introduces the `--[no-]relative-paths` CLI option and
`worktree.useRelativePaths` configuration setting to the `worktree add`
command. When enabled these options allow worktrees to be linked using
relative paths, enhancing portability across environments where absolute
paths may differ (e.g., containerized setups, shared network drives).
Git still creates absolute paths by default, but these options allow
users to opt-in to relative paths if desired.
The t2408 test file is removed and more comprehensive tests are
written for the various worktree operations in their own files.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/config/worktree.txt | 10 +++++++++
Documentation/git-worktree.txt | 5 +++++
builtin/worktree.c | 19 +++++++++--------
t/t2400-worktree-add.sh | 45 +++++++++++++++++++++++++++++++++++++++
t/t2401-worktree-prune.sh | 3 ++-
t/t2402-worktree-list.sh | 22 +++++++++++++++++++
t/t2408-worktree-relative.sh | 39 ---------------------------------
7 files changed, 94 insertions(+), 49 deletions(-)
diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
index 048e349482df6c892055720eb53cdcd6c327b6ed..5e35c7d018aecdedca0642b11e45df6d19024d42 100644
--- a/Documentation/config/worktree.txt
+++ b/Documentation/config/worktree.txt
@@ -7,3 +7,13 @@ worktree.guessRemote::
such a branch exists, it is checked out and set as "upstream"
for the new branch. If no such match can be found, it falls
back to creating a new branch from the current HEAD.
+
+worktree.useRelativePaths::
+ Link worktrees using relative paths (when "true") or absolute
+ paths (when "false"). This is particularly useful for setups
+ where the repository and worktrees may be moved between
+ different locations or environments. Defaults to "false".
++
+Note that setting `worktree.useRelativePaths` to "true" implies enabling the
+`extension.relativeWorktrees` config (see linkgit:git-config[1]),
+thus making it incompatible with older versions of Git.
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 70437c815f13852bd2eb862176b8b933e6de0acf..60a671bbc255aa7763b4f77511c09f6a02783e00 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -216,6 +216,11 @@ To remove a locked worktree, specify `--force` twice.
This can also be set up as the default behaviour by using the
`worktree.guessRemote` config option.
+--[no-]relative-paths::
+ Link worktrees using relative paths or absolute paths (default).
+ Overrides the `worktree.useRelativePaths` config option, see
+ linkgit:git-config[1].
+
--[no-]track::
When creating a new branch, if `<commit-ish>` is a branch,
mark it as "upstream" from the new branch. This is the
diff --git a/builtin/worktree.c b/builtin/worktree.c
index dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -120,12 +120,14 @@ struct add_opts {
int quiet;
int checkout;
int orphan;
+ int relative_paths;
const char *keep_locked;
};
static int show_only;
static int verbose;
static int guess_remote;
+static int use_relative_paths;
static timestamp_t expire;
static int git_worktree_config(const char *var, const char *value,
@@ -134,6 +136,9 @@ static int git_worktree_config(const char *var, const char *value,
if (!strcmp(var, "worktree.guessremote")) {
guess_remote = git_config_bool(var, value);
return 0;
+ } else if (!strcmp(var, "worktree.userelativepaths")) {
+ use_relative_paths = git_config_bool(var, value);
+ return 0;
}
return git_default_config(var, value, ctx, cb);
@@ -414,8 +419,7 @@ static int add_worktree(const char *path, const char *refname,
const struct add_opts *opts)
{
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
- struct strbuf sb = STRBUF_INIT, sb_tmp = STRBUF_INIT;
- struct strbuf sb_path_realpath = STRBUF_INIT, sb_repo_realpath = STRBUF_INIT;
+ struct strbuf sb = STRBUF_INIT;
const char *name;
struct strvec child_env = STRVEC_INIT;
unsigned int counter = 0;
@@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname,
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
- strbuf_realpath(&sb_path_realpath, path, 1);
- strbuf_realpath(&sb_repo_realpath, sb_repo.buf, 1);
- write_file(sb.buf, "%s/.git", relative_path(sb_path_realpath.buf, sb_repo_realpath.buf, &sb_tmp));
- write_file(sb_git.buf, "gitdir: %s", relative_path(sb_repo_realpath.buf, sb_path_realpath.buf, &sb_tmp));
+ write_worktree_linking_files(sb_git, sb, opts->relative_paths);
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
write_file(sb.buf, "../..");
@@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname,
strvec_clear(&child_env);
strbuf_release(&sb);
- strbuf_release(&sb_tmp);
strbuf_release(&symref);
strbuf_release(&sb_repo);
- strbuf_release(&sb_repo_realpath);
strbuf_release(&sb_git);
- strbuf_release(&sb_path_realpath);
strbuf_release(&sb_name);
free_worktree(wt);
return ret;
@@ -796,12 +794,15 @@ static int add(int ac, const char **av, const char *prefix)
PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
OPT_BOOL(0, "guess-remote", &guess_remote,
N_("try to match the new branch name with a remote-tracking branch")),
+ OPT_BOOL(0, "relative-paths", &opts.relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
int ret;
memset(&opts, 0, sizeof(opts));
opts.checkout = 1;
+ opts.relative_paths = use_relative_paths;
ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);
if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index cfc4aeb1798c6d023909cec771e5b74e983af5ea..fc1a909123e7932194ac433d6b5e194a26ed8233 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -1207,4 +1207,49 @@ test_expect_success '"add" with initialized submodule, with submodule.recurse se
git -C project-clone -c submodule.recurse worktree add ../project-5
'
+test_expect_success 'can create worktrees with relative paths' '
+ test_when_finished "git worktree remove relative" &&
+ test_config worktree.useRelativePaths false &&
+ git worktree add --relative-paths ./relative &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
+ test_cmp expect relative/.git &&
+ echo "../../../relative/.git" >expect &&
+ test_cmp expect .git/worktrees/relative/gitdir
+'
+
+test_expect_success 'can create worktrees with absolute paths' '
+ test_config worktree.useRelativePaths true &&
+ git worktree add ./relative &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
+ test_cmp expect relative/.git &&
+ git worktree add --no-relative-paths ./absolute &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+ test_cmp expect absolute/.git &&
+ echo "$(pwd)/absolute/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
+test_expect_success 'move repo without breaking relative internal links' '
+ test_when_finished rm -rf repo moved &&
+ git init repo &&
+ (
+ cd repo &&
+ test_commit initial &&
+ git worktree add --relative-paths wt1 &&
+ cd .. &&
+ mv repo moved &&
+ cd moved/wt1 &&
+ git worktree list >out 2>err &&
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'relative worktree sets extension config' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ git -C repo commit --allow-empty -m base &&
+ git -C repo worktree add --relative-paths ./foo &&
+ test_cmp_config -C repo 1 core.repositoryformatversion
+'
+
test_done
diff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh
index 976d048e3efc74be9cd909ce76d552b3944d2e10..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755
--- a/t/t2401-worktree-prune.sh
+++ b/t/t2401-worktree-prune.sh
@@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' '
! test -d .git/worktrees/wt
'
-test_expect_success 'not prune proper worktrees when run inside linked worktree' '
+test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '
test_when_finished rm -rf repo wt_ext &&
git init repo &&
(
cd repo &&
+ git config worktree.useRelativePaths true &&
echo content >file &&
git add file &&
git commit -m msg &&
diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
index 33ea9cb21ba07c9563530b54da06753eaa993fe2..780daa6cd6351f8fa9434619cc212aade8f01420 100755
--- a/t/t2402-worktree-list.sh
+++ b/t/t2402-worktree-list.sh
@@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' '
'
test_expect_success 'linked worktrees are sorted' '
+ test_when_finished "rm -rf sorted" &&
mkdir sorted &&
git init sorted/main &&
(
@@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' '
test_cmp expected sorted/main/actual
'
+test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '
+ test_when_finished "rm -rf sorted" &&
+ mkdir sorted &&
+ git init sorted/main &&
+ (
+ cd sorted/main &&
+ test_tick &&
+ test_commit new &&
+ git worktree add --relative-paths ../first &&
+ git worktree add ../second &&
+ git worktree list --porcelain >out &&
+ grep ^worktree out >actual
+ ) &&
+ cat >expected <<-EOF &&
+ worktree $(pwd)/sorted/main
+ worktree $(pwd)/sorted/first
+ worktree $(pwd)/sorted/second
+ EOF
+ test_cmp expected sorted/main/actual
+'
+
test_expect_success 'worktree path when called in .git directory' '
git worktree list >list1 &&
git -C .git worktree list >list2 &&
diff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh
deleted file mode 100755
index a3136db7e28cb20926ff44211e246ce625a6e51a..0000000000000000000000000000000000000000
--- a/t/t2408-worktree-relative.sh
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/bin/sh
-
-test_description='test worktrees linked with relative paths'
-
-TEST_PASSES_SANITIZE_LEAK=true
-. ./test-lib.sh
-
-test_expect_success 'links worktrees with relative paths' '
- test_when_finished rm -rf repo &&
- git init repo &&
- (
- cd repo &&
- test_commit initial &&
- git worktree add wt1 &&
- echo "../../../wt1/.git" >expected_gitdir &&
- cat .git/worktrees/wt1/gitdir >actual_gitdir &&
- echo "gitdir: ../.git/worktrees/wt1" >expected_git &&
- cat wt1/.git >actual_git &&
- test_cmp expected_gitdir actual_gitdir &&
- test_cmp expected_git actual_git
- )
-'
-
-test_expect_success 'move repo without breaking relative internal links' '
- test_when_finished rm -rf repo moved &&
- git init repo &&
- (
- cd repo &&
- test_commit initial &&
- git worktree add wt1 &&
- cd .. &&
- mv repo moved &&
- cd moved/wt1 &&
- git status >out 2>err &&
- test_must_be_empty err
- )
-'
-
-test_done
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 6/8] worktree: add relative cli/config options to `move` command
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (4 preceding siblings ...)
2024-11-26 1:52 ` [PATCH v5 5/8] worktree: add relative cli/config options to `add` command Caleb White
@ 2024-11-26 1:52 ` Caleb White
2024-11-26 1:52 ` [PATCH v5 7/8] worktree: add relative cli/config options to `repair` command Caleb White
` (4 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:52 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This teaches the `worktree move` command to respect the
`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`
config setting. If an existing worktree is moved with `--relative-paths`
the new path will be relative (and visa-versa).
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
builtin/worktree.c | 4 +++-
t/t2403-worktree-move.sh | 25 +++++++++++++++++++++++++
worktree.c | 22 +++++++++-------------
worktree.h | 4 ++--
4 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
OPT__FORCE(&force,
N_("force move even if worktree is dirty or locked"),
PARSE_OPT_NOCOMPLETE),
+ OPT_BOOL(0, "relative-paths", &use_relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
struct worktree **worktrees, *wt;
@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
if (rename(wt->path, dst.buf) == -1)
die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
- update_worktree_location(wt, dst.buf);
+ update_worktree_location(wt, dst.buf, use_relative_paths);
strbuf_release(&dst);
free_worktrees(worktrees);
diff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh
index 901342ea09b51a8e832f1109fbb737df84283aa2..422c1a05580057b18ab8bfdfe38da4d723749493 100755
--- a/t/t2403-worktree-move.sh
+++ b/t/t2403-worktree-move.sh
@@ -247,4 +247,29 @@ test_expect_success 'not remove a repo with initialized submodule' '
)
'
+test_expect_success 'move worktree with absolute path to relative path' '
+ test_config worktree.useRelativePaths false &&
+ git worktree add ./absolute &&
+ git worktree move --relative-paths absolute relative &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
+ test_cmp expect relative/.git &&
+ echo "../../../relative/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir &&
+ test_config worktree.useRelativePaths true &&
+ git worktree move relative relative2 &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
+ test_cmp expect relative2/.git &&
+ echo "../../../relative2/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
+test_expect_success 'move worktree with relative path to absolute path' '
+ test_config worktree.useRelativePaths true &&
+ git worktree move --no-relative-paths relative2 absolute &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+ test_cmp expect absolute/.git &&
+ echo "$(pwd)/absolute/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
test_done
diff --git a/worktree.c b/worktree.c
index cf05045cc973f121a0a76b5ccfa731acf25d1a73..c749cb16994cf46ccccd4c2880ac917e497671b8 100644
--- a/worktree.c
+++ b/worktree.c
@@ -376,32 +376,28 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
return ret;
}
-void update_worktree_location(struct worktree *wt, const char *path_)
+void update_worktree_location(struct worktree *wt, const char *path_,
+ int use_relative_paths)
{
struct strbuf path = STRBUF_INIT;
- struct strbuf repo = STRBUF_INIT;
- struct strbuf file = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
+ struct strbuf dotgit = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
if (is_main_worktree(wt))
BUG("can't relocate main worktree");
- strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
+ strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
strbuf_realpath(&path, path_, 1);
+ strbuf_addf(&dotgit, "%s/.git", path.buf);
if (fspathcmp(wt->path, path.buf)) {
- strbuf_addf(&file, "%s/gitdir", repo.buf);
- write_file(file.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
- strbuf_reset(&file);
- strbuf_addf(&file, "%s/.git", path.buf);
- write_file(file.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
free(wt->path);
wt->path = strbuf_detach(&path, NULL);
}
strbuf_release(&path);
- strbuf_release(&repo);
- strbuf_release(&file);
- strbuf_release(&tmp);
+ strbuf_release(&dotgit);
+ strbuf_release(&gitdir);
}
int is_worktree_being_rebased(const struct worktree *wt,
diff --git a/worktree.h b/worktree.h
index fd040f5d999697b603df929679bdddd2ff7f6eea..9c699d080d8ebf37712044136679db3821ee1f63 100644
--- a/worktree.h
+++ b/worktree.h
@@ -117,8 +117,8 @@ int validate_worktree(const struct worktree *wt,
/*
* Update worktrees/xxx/gitdir with the new path.
*/
-void update_worktree_location(struct worktree *wt,
- const char *path_);
+void update_worktree_location(struct worktree *wt, const char *path_,
+ int use_relative_paths);
typedef void (* worktree_repair_fn)(int iserr, const char *path,
const char *msg, void *cb_data);
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 7/8] worktree: add relative cli/config options to `repair` command
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (5 preceding siblings ...)
2024-11-26 1:52 ` [PATCH v5 6/8] worktree: add relative cli/config options to `move` command Caleb White
@ 2024-11-26 1:52 ` Caleb White
2024-11-26 1:52 ` [PATCH v5 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Caleb White
` (3 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:52 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This teaches the `worktree repair` command to respect the
`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`
config setting. If an existing worktree with an absolute path is repaired
with `--relative-paths`, the links will be replaced with relative paths,
even if the original path was correct. This allows a user to covert
existing worktrees between absolute/relative as desired.
To simplify things, both linking files are written when one of the files
needs to be repaired. In some cases, this fixes the other file before it
is checked, in other cases this results in a correct file being written
with the same contents.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/git-worktree.txt | 3 +++
builtin/worktree.c | 6 +++--
t/t2406-worktree-repair.sh | 39 +++++++++++++++++++++++++++++
worktree.c | 56 ++++++++++++++++++++----------------------
worktree.h | 5 ++--
5 files changed, 76 insertions(+), 33 deletions(-)
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 60a671bbc255aa7763b4f77511c09f6a02783e00..8340b7f028e6c1c3bae3de0879e9754098466d14 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -220,6 +220,9 @@ This can also be set up as the default behaviour by using the
Link worktrees using relative paths or absolute paths (default).
Overrides the `worktree.useRelativePaths` config option, see
linkgit:git-config[1].
++
+With `repair`, the linking files will be updated if there's an absolute/relative
+mismatch, even if the links are correct.
--[no-]track::
When creating a new branch, if `<commit-ish>` is a branch,
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 302151506981718658db1cd338cd9064688f5c14..fde9ff4dc9a734c655e95ccd62774282950cbba6 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -1385,6 +1385,8 @@ static int repair(int ac, const char **av, const char *prefix)
const char **p;
const char *self[] = { ".", NULL };
struct option options[] = {
+ OPT_BOOL(0, "relative-paths", &use_relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
int rc = 0;
@@ -1392,8 +1394,8 @@ static int repair(int ac, const char **av, const char *prefix)
ac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);
p = ac > 0 ? av : self;
for (; *p; p++)
- repair_worktree_at_path(*p, report_repair, &rc);
- repair_worktrees(report_repair, &rc);
+ repair_worktree_at_path(*p, report_repair, &rc, use_relative_paths);
+ repair_worktrees(report_repair, &rc, use_relative_paths);
return rc;
}
diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
index 7686e60f6ad186519b275f11a5e14064c905b207..49b70b999518d47e1edd72a61a847b427f4c67a1 100755
--- a/t/t2406-worktree-repair.sh
+++ b/t/t2406-worktree-repair.sh
@@ -216,4 +216,43 @@ test_expect_success 'repair copied main and linked worktrees' '
test_cmp dup/linked.expect dup/linked/.git
'
+test_expect_success 'repair worktree with relative path with missing gitfile' '
+ test_when_finished "rm -rf main wt" &&
+ test_create_repo main &&
+ git -C main config worktree.useRelativePaths true &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../wt &&
+ rm wt/.git &&
+ test_path_is_missing wt/.git &&
+ git -C main worktree repair &&
+ echo "gitdir: ../main/.git/worktrees/wt" >expect &&
+ test_cmp expect wt/.git
+'
+
+test_expect_success 'repair absolute worktree to use relative paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../side &&
+ echo "../../../../sidemoved/.git" >expect-gitdir &&
+ echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
+ mv side sidemoved &&
+ git -C main worktree repair --relative-paths ../sidemoved &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
+test_expect_success 'repair relative worktree to use absolute paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --relative-paths --detach ../side &&
+ echo "$(pwd)/sidemoved/.git" >expect-gitdir &&
+ echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
+ mv side sidemoved &&
+ git -C main worktree repair ../sidemoved &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
test_done
diff --git a/worktree.c b/worktree.c
index c749cb16994cf46ccccd4c2880ac917e497671b8..2e76bbc1494afc125997f803dc39846a0b95a84f 100644
--- a/worktree.c
+++ b/worktree.c
@@ -573,12 +573,13 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
* pointing at <repo>/worktrees/<id>.
*/
static void repair_gitfile(struct worktree *wt,
- worktree_repair_fn fn, void *cb_data)
+ worktree_repair_fn fn, void *cb_data,
+ int use_relative_paths)
{
struct strbuf dotgit = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
struct strbuf repo = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -594,6 +595,7 @@ static void repair_gitfile(struct worktree *wt,
strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
strbuf_addf(&dotgit, "%s/.git", wt->path);
+ strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
if (dotgit_contents) {
@@ -611,18 +613,20 @@ static void repair_gitfile(struct worktree *wt,
repair = _(".git file broken");
else if (fspathcmp(backlink.buf, repo.buf))
repair = _(".git file incorrect");
+ else if (use_relative_paths == is_absolute_path(dotgit_contents))
+ repair = _(".git file absolute/relative path mismatch");
if (repair) {
fn(0, wt->path, repair, cb_data);
- write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, wt->path, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
}
done:
free(dotgit_contents);
strbuf_release(&repo);
strbuf_release(&dotgit);
+ strbuf_release(&gitdir);
strbuf_release(&backlink);
- strbuf_release(&tmp);
}
static void repair_noop(int iserr UNUSED,
@@ -633,7 +637,7 @@ static void repair_noop(int iserr UNUSED,
/* nothing */
}
-void repair_worktrees(worktree_repair_fn fn, void *cb_data)
+void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths)
{
struct worktree **worktrees = get_worktrees_internal(1);
struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
@@ -641,7 +645,7 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)
if (!fn)
fn = repair_noop;
for (; *wt; wt++)
- repair_gitfile(*wt, fn, cb_data);
+ repair_gitfile(*wt, fn, cb_data, use_relative_paths);
free_worktrees(worktrees);
}
@@ -757,16 +761,14 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
* the worktree's path.
*/
void repair_worktree_at_path(const char *path,
- worktree_repair_fn fn, void *cb_data)
+ worktree_repair_fn fn, void *cb_data,
+ int use_relative_paths)
{
struct strbuf dotgit = STRBUF_INIT;
- struct strbuf realdotgit = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
struct strbuf inferred_backlink = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
struct strbuf olddotgit = STRBUF_INIT;
- struct strbuf realolddotgit = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -778,25 +780,25 @@ void repair_worktree_at_path(const char *path,
goto done;
strbuf_addf(&dotgit, "%s/.git", path);
- if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
+ if (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {
fn(1, path, _("not a valid path"), cb_data);
goto done;
}
- infer_backlink(realdotgit.buf, &inferred_backlink);
+ infer_backlink(dotgit.buf, &inferred_backlink);
strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
- dotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
+ dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
if (dotgit_contents) {
if (is_absolute_path(dotgit_contents)) {
strbuf_addstr(&backlink, dotgit_contents);
} else {
- strbuf_addbuf(&backlink, &realdotgit);
+ strbuf_addbuf(&backlink, &dotgit);
strbuf_strip_suffix(&backlink, ".git");
strbuf_addstr(&backlink, dotgit_contents);
strbuf_realpath_forgiving(&backlink, backlink.buf, 0);
}
} else if (err == READ_GITFILE_ERR_NOT_A_FILE) {
- fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
+ fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
goto done;
} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
if (inferred_backlink.len) {
@@ -809,11 +811,11 @@ void repair_worktree_at_path(const char *path,
*/
strbuf_swap(&backlink, &inferred_backlink);
} else {
- fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
+ fn(1, dotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
goto done;
}
} else {
- fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
+ fn(1, dotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
goto done;
}
@@ -835,39 +837,35 @@ void repair_worktree_at_path(const char *path,
* in the "copy" repository. In this case, point the "copy" worktree's
* .git file at the "copy" repository.
*/
- if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) {
+ if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))
strbuf_swap(&backlink, &inferred_backlink);
- }
strbuf_addf(&gitdir, "%s/gitdir", backlink.buf);
if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
repair = _("gitdir unreadable");
+ else if (use_relative_paths == is_absolute_path(olddotgit.buf))
+ repair = _("gitdir absolute/relative path mismatch");
else {
strbuf_rtrim(&olddotgit);
- if (is_absolute_path(olddotgit.buf)) {
- strbuf_addbuf(&realolddotgit, &olddotgit);
- } else {
- strbuf_addf(&realolddotgit, "%s/%s", backlink.buf, olddotgit.buf);
- strbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0);
+ if (!is_absolute_path(olddotgit.buf)) {
+ strbuf_insertf(&olddotgit, 0, "%s/", backlink.buf);
+ strbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0);
}
- if (fspathcmp(realolddotgit.buf, realdotgit.buf))
+ if (fspathcmp(olddotgit.buf, dotgit.buf))
repair = _("gitdir incorrect");
}
if (repair) {
fn(0, gitdir.buf, repair, cb_data);
- write_file(gitdir.buf, "%s", relative_path(realdotgit.buf, backlink.buf, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
}
done:
free(dotgit_contents);
strbuf_release(&olddotgit);
- strbuf_release(&realolddotgit);
strbuf_release(&backlink);
strbuf_release(&inferred_backlink);
strbuf_release(&gitdir);
- strbuf_release(&realdotgit);
strbuf_release(&dotgit);
- strbuf_release(&tmp);
}
int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
diff --git a/worktree.h b/worktree.h
index 9c699d080d8ebf37712044136679db3821ee1f63..38145df80f41079f301d3aeaaffcc35b7f6760b9 100644
--- a/worktree.h
+++ b/worktree.h
@@ -129,7 +129,7 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,
* function, if non-NULL, is called with the path of the worktree and a
* description of the repair or error, along with the callback user-data.
*/
-void repair_worktrees(worktree_repair_fn, void *cb_data);
+void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths);
/*
* Repair the linked worktrees after the gitdir has been moved.
@@ -151,7 +151,8 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
* worktree and a description of the repair or error, along with the callback
* user-data.
*/
-void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
+void repair_worktree_at_path(const char *, worktree_repair_fn,
+ void *cb_data, int use_relative_paths);
/*
* Free up the memory for a worktree.
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v5 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (6 preceding siblings ...)
2024-11-26 1:52 ` [PATCH v5 7/8] worktree: add relative cli/config options to `repair` command Caleb White
@ 2024-11-26 1:52 ` Caleb White
2024-11-26 6:18 ` [PATCH v5 0/8] Allow relative worktree linking to be configured by the user Junio C Hamano
` (2 subsequent siblings)
10 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 1:52 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This refactors `repair_worktree_after_gitdir_move()` to use the new
`write_worktree_linking_files` function. It also preserves the
relativity of the linking files; e.g., if an existing worktree used
absolute paths then the repaired paths will be absolute (and visa-versa).
`repair_worktree_after_gitdir_move()` is used to repair both sets of
worktree linking files if the `.git` directory is moved during a
re-initialization using `git init`.
This also adds a test case for reinitializing a repository that has
relative worktrees.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
t/t0001-init.sh | 22 ++++++++++++++++++----
worktree.c | 29 ++++++++---------------------
2 files changed, 26 insertions(+), 25 deletions(-)
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e394147b8480fb5784f1c23c0d2eae712eecc44b 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
sep_git_dir_worktree () {
test_when_finished "rm -rf mainwt linkwt seprepo" &&
git init mainwt &&
+ if test "relative" = $2
+ then
+ test_config -C mainwt worktree.useRelativePaths true
+ else
+ test_config -C mainwt worktree.useRelativePaths false
+ fi
test_commit -C mainwt gumby &&
git -C mainwt worktree add --detach ../linkwt &&
git -C "$1" init --separate-git-dir ../seprepo &&
@@ -442,12 +448,20 @@ sep_git_dir_worktree () {
test_cmp expect actual
}
-test_expect_success 're-init to move gitdir with linked worktrees' '
- sep_git_dir_worktree mainwt
+test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '
+ sep_git_dir_worktree mainwt absolute
+'
+
+test_expect_success 're-init to move gitdir within linked worktree (absolute)' '
+ sep_git_dir_worktree linkwt absolute
+'
+
+test_expect_success 're-init to move gitdir with linked worktrees (relative)' '
+ sep_git_dir_worktree mainwt relative
'
-test_expect_success 're-init to move gitdir within linked worktree' '
- sep_git_dir_worktree linkwt
+test_expect_success 're-init to move gitdir within linked worktree (relative)' '
+ sep_git_dir_worktree linkwt relative
'
test_expect_success MINGW '.git hidden' '
diff --git a/worktree.c b/worktree.c
index 2e76bbc1494afc125997f803dc39846a0b95a84f..af68b24f9d0917a99cd965347aad6466e9dbb152 100644
--- a/worktree.c
+++ b/worktree.c
@@ -651,45 +651,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat
void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)
{
- struct strbuf path = STRBUF_INIT;
- struct strbuf repo = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
struct strbuf dotgit = STRBUF_INIT;
- struct strbuf olddotgit = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
+ int is_relative_path;
if (is_main_worktree(wt))
goto done;
- strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
- strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
+ strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
- if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
+ if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)
goto done;
- strbuf_rtrim(&olddotgit);
- if (is_absolute_path(olddotgit.buf)) {
- strbuf_addbuf(&dotgit, &olddotgit);
- } else {
- strbuf_addf(&dotgit, "%s/worktrees/%s/%s", old_path, wt->id, olddotgit.buf);
+ strbuf_rtrim(&dotgit);
+ is_relative_path = ! is_absolute_path(dotgit.buf);
+ if (is_relative_path) {
+ strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id);
strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
}
if (!file_exists(dotgit.buf))
goto done;
- strbuf_addbuf(&path, &dotgit);
- strbuf_strip_suffix(&path, "/.git");
-
- write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
- write_file(gitdir.buf, "%s", relative_path(dotgit.buf, repo.buf, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, is_relative_path);
done:
- strbuf_release(&path);
- strbuf_release(&repo);
strbuf_release(&gitdir);
strbuf_release(&dotgit);
- strbuf_release(&olddotgit);
- strbuf_release(&tmp);
}
void repair_worktrees_after_gitdir_move(const char *old_path)
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (7 preceding siblings ...)
2024-11-26 1:52 ` [PATCH v5 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Caleb White
@ 2024-11-26 6:18 ` Junio C Hamano
2024-11-26 17:02 ` Caleb White
2024-11-28 14:44 ` Phillip Wood
2024-11-29 22:22 ` [PATCH v6 " Caleb White
10 siblings, 1 reply; 60+ messages in thread
From: Junio C Hamano @ 2024-11-26 6:18 UTC (permalink / raw)
To: Caleb White; +Cc: git, Taylor Blau, Phillip Wood, Eric Sunshine
Caleb White <cdwhite3@pm.me> writes:
> Changes in v5:
> - Added docs to `--relative-paths` option.
You already had doc on this, but the default was not described at
all.
--[no-]relative-paths::
+ Link worktrees using relative paths or absolute paths (default).
> - Added test coverage for `repair_worktrees()` and relative paths.
> - Move `strbuf_reset` call in `infer_backlink()`.
This was more like "revert the change in v4 that moved it
unnecessarily", no?
> - Cleaned up tests.
Yup, there truely a lot of test changes between v4 and v5. Many
tests now use existing test helpers, which is good.
> - Slight stylistic changes.
I saw many changes like these (the diff is between v4 and v5)
static void repair_gitfile(struct worktree *wt,
- worktree_repair_fn fn,
- void *cb_data,
+ worktree_repair_fn fn, void *cb_data,
int use_relative_paths)
which looked good (the original had fn and cb_data defined on the
same line).
> - Tweaked commit messages.
Updates to the proposed log message for `repair` step [7/8] did not
really "clarify", other than helping readers to see how messy things
are. It said:
+ To simplify things, both linking files are written when one of the files
+ needs to be repaired. In some cases, this fixes the other file before it
+ is checked, in other cases this results in a correct file being written
+ with the same contents.
which may describe what the code happens to do correctly, but does
not quite help building the confidence in what it does is correct.
Suppose that the directory X has a repository, and the repository
thinks that the directory W is its worktree. But the worktree at
the directory W thinks that its repository is not X but Y, and there
indeed is a repository at the directory Y. That repository thinks W
belongs to it.
If we examine X first, would we end up updating W to point at X
(because X thinks W is its worktree)?
Or do we make W to point at Y (because Y thinks W is its, and W
thinks it is Y's)"?
Either way, I think the comment is trying to say that, if we decide
to make X and W belong to each other, we'd overwrite links from X to
W and also W to X, even though the link from X was already pointing
at W and the minimum fix we needed to make was to update the link
from W to point at X. Overwriting a link from X to W with a new
link from X to W is a no-op, so it does not seem to help greatly,
since `repair` is not at all performance critical. The correctness
is a lot more important.
> - Updated base to 090d24e9af.
This made it harder than necessary to compare the two iterations, by
the way.
Thanks.
^ permalink raw reply [flat|nested] 60+ messages in thread* Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user
2024-11-26 6:18 ` [PATCH v5 0/8] Allow relative worktree linking to be configured by the user Junio C Hamano
@ 2024-11-26 17:02 ` Caleb White
0 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-26 17:02 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Taylor Blau, Phillip Wood, Eric Sunshine
On Tue Nov 26, 2024 at 12:18 AM CST, Junio C Hamano wrote:
> Caleb White <cdwhite3@pm.me> writes:
>> Changes in v5:
>> - Added docs to `--relative-paths` option.
>
> You already had doc on this, but the default was not described at
> all.
>
> --[no-]relative-paths::
> + Link worktrees using relative paths or absolute paths (default).
I added a bit more explanation instead of just directing the user to the
config variable (I originally had docs, but it was requested that
I remove the duplication and just point to the config, however, I think
the changes describes it a bit better as well as gives the default).
>> - Added test coverage for `repair_worktrees()` and relative paths.
>> - Move `strbuf_reset` call in `infer_backlink()`.
>
> This was more like "revert the change in v4 that moved it
> unnecessarily", no?
Yes, that is correct.
>> - Cleaned up tests.
>
> Yup, there truely a lot of test changes between v4 and v5. Many
> tests now use existing test helpers, which is good.
This is the majority of the reroll. It seems like MacOS doesn't like the
`test_config` helper inside of a subshell, so I had to stick with `git
config` in those cases.
>> - Slight stylistic changes.
>
> I saw many changes like these (the diff is between v4 and v5)
>
> static void repair_gitfile(struct worktree *wt,
> - worktree_repair_fn fn,
> - void *cb_data,
> + worktree_repair_fn fn, void *cb_data,
> int use_relative_paths)
>
> which looked good (the original had fn and cb_data defined on the
> same line).
Yes, this was brought up in the previous review and I decided to make
the change.
>> - Tweaked commit messages.
>
> Updates to the proposed log message for `repair` step [7/8] did not
> really "clarify", other than helping readers to see how messy things
> are. It said:
>
> + To simplify things, both linking files are written when one of the files
> + needs to be repaired. In some cases, this fixes the other file before it
> + is checked, in other cases this results in a correct file being written
> + with the same contents.
>
> which may describe what the code happens to do correctly, but does
> not quite help building the confidence in what it does is correct.
>
> Suppose that the directory X has a repository, and the repository
> thinks that the directory W is its worktree. But the worktree at
> the directory W thinks that its repository is not X but Y, and there
> indeed is a repository at the directory Y. That repository thinks W
> belongs to it.
That's a bit of a confusing scenario, but I think this is what you're
trying to describe:
Repository X ----> Worktree W <---> Repository Y (Case 0)
which is not a normal case (but I'll get to that later).
Most of the time, a repair would be performed with one of the following
cases:
Repository X <---> Worktree W (Case 1)
Repository X ----> Worktree W (Case 2)
Repository X <---- Worktree W (Case 3)
Repository X Worktree W (Case 4)
that is, a repository and worktree have valid links, have valid links in
one direction or the other, or they have no valid links at all.
Before I go on, I think it would be helpful to revisit how the repair
operation works. There are two loops in the repair operation:
1. the `repair()` function iterates over (via `repair_worktree_at_path()`)
the given worktrees/paths (or `.` if no paths are given) to potentially
repair the `<repo>/.git/worktrees/<worktree_id>/gitdir` files
2. the `repair_worktrees()` function iterates over all the worktrees
defined at `<repo>/.git/worktrees/*` to potentially repair the
`.../<worktree_id>/.git` files
In Loop 1, a repair is performed if:
- there's an absolute/relative path mismatch
- the worktree `.git` file points to the repository, but the repository
`gitdir` file is unreadable or does not point back to the worktree
- the worktree `.git` file does NOT point to the repository, but an
inferred backlink can be established (the worktree id in the `.git`
file matches a worktree id in the repository's `worktrees` directory),
and that inferred repository does not point to the worktree
In Loop 2, a repair is performed if:
- there's an absolute/relative path mismatch
- the worktree pointed to by the repository `gitdir` file does not point
back to the repository or the file is corrupted
Now back to Cases 1--4:
- In Case 1, the repair would not update any links (already valid).
- Case 2 is most likely when using absolute paths and the repository is
moved, but the worktree is not. The worktree `.git` will be updated
during Loop 2, however, now the repository `gitdir` file will also be
written with the same contents (a no-op) to keep the code simple.
- Case 3 is most likely when using absolute paths and the worktree is
moved, but the repository is not. The repository `gitdir` will be
updated during Loop 1, however, now the worktree `.git` file will also
be written with the same contents (a no-op) to keep the code simple.
- Case 4 can occur when using absolute paths and both the repository and
worktree are moved, but it can also occur when using relative paths and
either the repository or worktree is moved. Both linking files need to
be updated. In the past, the repository `gitdir` file would be updated
during Loop 1 (from the inferred backlink), and the worktree `.git`
would not be updated until Loop 2. However, now both linking files are
updated during Loop 1 and the repair is complete by the time Loop 2
is reached.
> If we examine X first, would we end up updating W to point at X
> (because X thinks W is its worktree)?
>
> Or do we make W to point at Y (because Y thinks W is its, and W
> thinks it is Y's)"?
A repair is always performed in the context of single repository,
therefore, if operating on repository X and the worktree W is found to
be a valid worktree for X, then yes, the repair would update the link
from W to X so that Case 0 would now look like:
Repository X <---> Worktree W <---- Repository Y
but again, this is a very weird case---the most likely scenario that I
can think of is that a user copied a repository (with or without the
worktree). The `es/worktree-repair-copied` topic added support for
repairing a worktree from such a copy scenario. However, I did note[1,2]
that the topic added the ability for a repository to "take over" a
worktree from another repository if the worktree_id matched a worktree
inside the current repository. This can happen if two repositories use
the same worktree name (I usually name my worktrees the same name as the
branch to keep things simple so this can happen if two repositories
create a worktree for `master` for instance).
I recommended that worktrees be created with a unique hash/identifier so
that the worktree_id is unique across all repositories even if they have
the same name. I was planning on creating a future topic to address this,
for example creating a worktree `develop` would look like:
foo/
├── .git/worktrees/develop-6b3d7b/
└── develop/
The actual worktree directory name would still be `develop`, but the
worktree_id would be unique and prevent the "take over" scenario.
> Either way, I think the comment is trying to say that, if we decide
> to make X and W belong to each other, we'd overwrite links from X to
> W and also W to X, even though the link from X was already pointing
> at W and the minimum fix we needed to make was to update the link
> from W to point at X. Overwriting a link from X to W with a new
> link from X to W is a no-op, so it does not seem to help greatly,
> since `repair` is not at all performance critical. The correctness
> is a lot more important.
Yes, you understand this correctly. The repair operation is not
performance critical, so I decided to keep the code simple and just
always update both linking files. The same `write_worktree_linking_files()
is used for all operations (add, move, repair), some which require both
files to be updated, and others which only require one file to be updated.
>> - Updated base to 090d24e9af.
>
> This made it harder than necessary to compare the two iterations, by
> the way.
My apologies for that. I wasn't sure what the procedure was when
a dependent topic was merged to master. I figured it would be best to
rebase onto the latest master.
Best,
Caleb
[1]: https://lore.kernel.org/git/20241008153035.71178-1-cdwhite3@pm.me/
[2]: https://lore.kernel.org/git/r4zmcET41Skr_FMop47AKd7cms9E8bKPSvHuAUpnYavzKEY6JybJta0_7GfuYB0q-gD-XNcvh5VDTfiT3qthGKjqhS1sbT4M2lUABynOz2Q=@pm.me/
^ permalink raw reply [flat|nested] 60+ messages in thread
* Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (8 preceding siblings ...)
2024-11-26 6:18 ` [PATCH v5 0/8] Allow relative worktree linking to be configured by the user Junio C Hamano
@ 2024-11-28 14:44 ` Phillip Wood
2024-11-28 17:58 ` Caleb White
2024-11-29 22:22 ` [PATCH v6 " Caleb White
10 siblings, 1 reply; 60+ messages in thread
From: Phillip Wood @ 2024-11-28 14:44 UTC (permalink / raw)
To: Caleb White, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
Hi Caleb
On 26/11/2024 01:51, Caleb White wrote:
> Changes in v5:
> - Added docs to `--relative-paths` option.
> - Added test coverage for `repair_worktrees()` and relative paths.
> - Move `strbuf_reset` call in `infer_backlink()`.
> - Cleaned up tests.
> - Slight stylistic changes.
> - Tweaked commit messages.
> - Updated base to 090d24e9af.
Thanks for re-rolling, these changes sound good. Below is the
range-diff of what is in seen today compared to last week. I've left
it untrimmed so other people can check what's changed and I've added a
couple of comments. The only thing I'm worried about is the deletion
of a check for setting extensions.relativeWorktrees in patch 5 the
rest of the changes look good, thank you for the extra test checks and
log messages.
Best Wishes
Phillip
1: 67a622c7f2 ! 1: 0986f98022 setup: correctly reinitialize repository version
@@ Commit message
config to ensure that the repository version is set correctly.
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## setup.c ##
@@ setup.c: void initialize_repository_version(int hash_algo,
2: 85964909b0 ! 2: c36e1a59fa worktree: add `relativeWorktrees` extension
@@ Commit message
Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## Documentation/config/extensions.txt ##
@@ Documentation/config/extensions.txt: Note that this setting should only be set by linkgit:git-init[1] or
3: a614c39333 ! 3: 5b19b63040 worktree: refactor infer_backlink return
@@ Commit message
[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## worktree.c ##
@@ worktree.c: struct worktree *get_linked_worktree(const char *id,
@@ worktree.c: static int is_main_worktree_path(const char *path)
{
struct strbuf actual = STRBUF_INIT;
const char *id;
-
-+ strbuf_reset(inferred);
- if (strbuf_read_file(&actual, gitfile, 0) < 0)
- goto error;
- if (!starts_with(actual.buf, "gitdir:"))
@@ worktree.c: static int infer_backlink(const char *gitfile, struct strbuf *inferred)
- id++; /* advance past '/' to point at <id> */
- if (!*id)
- goto error;
-- strbuf_reset(inferred);
- strbuf_git_common_path(inferred, the_repository, "worktrees/%s", id);
- if (!is_directory(inferred->buf))
goto error;
strbuf_release(&actual);
4: 83ea6d7ba0 ! 4: ec143ae00e worktree: add `write_worktree_linking_files()` function
@@ Commit message
is linked with relative paths.
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## worktree.c ##
@@ worktree.c: int init_worktree_config(struct repository *r)
free(main_worktree_file);
return res;
}
+
-+void write_worktree_linking_files(struct strbuf dotgit,
-+ struct strbuf gitdir,
++void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
+ int use_relative_paths)
+{
+ struct strbuf path = STRBUF_INIT;
@@ worktree.h: void strbuf_worktree_ref(const struct worktree *wt,
+ * dotgit: "/path/to/foo/.git"
+ * gitdir: "/path/to/repo/worktrees/foo/gitdir"
+ */
-+void write_worktree_linking_files(struct strbuf dotgit,
-+ struct strbuf gitdir,
++void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
+ int use_relative_paths);
+
#endif
5: 36d01dca84 ! 5: 237206b08f worktree: add relative cli/config options to `add` command
@@ Commit message
written for the various worktree operations in their own files.
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## Documentation/config/worktree.txt ##
@@ Documentation/config/worktree.txt: worktree.guessRemote::
@@ Documentation/git-worktree.txt: To remove a locked worktree, specify `--force` t
`worktree.guessRemote` config option.
+--[no-]relative-paths::
++ Link worktrees using relative paths or absolute paths (default).
+ Overrides the `worktree.useRelativePaths` config option, see
+ linkgit:git-config[1].
+
@@ t/t2400-worktree-add.sh: test_expect_success '"add" with initialized submodule,
+test_expect_success 'can create worktrees with relative paths' '
+ test_when_finished "git worktree remove relative" &&
-+ git config worktree.useRelativePaths false &&
++ test_config worktree.useRelativePaths false &&
+ git worktree add --relative-paths ./relative &&
-+ cat relative/.git >actual &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
-+ test_cmp expect actual &&
-+ cat .git/worktrees/relative/gitdir >actual &&
++ test_cmp expect relative/.git &&
+ echo "../../../relative/.git" >expect &&
-+ test_cmp expect actual
-+
++ test_cmp expect .git/worktrees/relative/gitdir
+'
+
+test_expect_success 'can create worktrees with absolute paths' '
-+ git config worktree.useRelativePaths true &&
++ test_config worktree.useRelativePaths true &&
+ git worktree add ./relative &&
-+ cat relative/.git >actual &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
-+ test_cmp expect actual &&
++ test_cmp expect relative/.git &&
+ git worktree add --no-relative-paths ./absolute &&
-+ cat absolute/.git >actual &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
-+ test_cmp expect actual
++ test_cmp expect absolute/.git &&
++ echo "$(pwd)/absolute/.git" >expect &&
++ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
+test_expect_success 'move repo without breaking relative internal links' '
+ test_when_finished rm -rf repo moved &&
+ git init repo &&
+ (
+ cd repo &&
-+ git config worktree.useRelativePaths true &&
+ test_commit initial &&
-+ git worktree add wt1 &&
++ git worktree add --relative-paths wt1 &&
+ cd .. &&
+ mv repo moved &&
+ cd moved/wt1 &&
-+ git status >out 2>err &&
++ git worktree list >out 2>err &&
+ test_must_be_empty err
+ )
+'
@@ t/t2400-worktree-add.sh: test_expect_success '"add" with initialized submodule,
+ git init repo &&
+ git -C repo commit --allow-empty -m base &&
+ git -C repo worktree add --relative-paths ./foo &&
-+ git -C repo config get core.repositoryformatversion >actual &&
-+ echo 1 >expected &&
-+ test_cmp expected actual &&
-+ git -C repo config get extensions.relativeworktrees >actual &&
-+ echo true >expected &&
-+ test_cmp expected actual
++ test_cmp_config -C repo 1 core.repositoryformatversion
+'
We have lost the check for extensions.relativeworktrees
here. Although we don't set worktree.useRelativePaths anymore we
should still set the extension as we pase --relative-paths to "git
worktree add"
+
test_done
6: 34401d680c ! 6: 8e4307f520 worktree: add relative cli/config options to `move` command
@@ Commit message
the new path will be relative (and visa-versa).
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## builtin/worktree.c ##
@@ builtin/worktree.c: static int move_worktree(int ac, const char **av, const char *prefix)
@@ t/t2403-worktree-move.sh: test_expect_success 'not remove a repo with initialize
'
+test_expect_success 'move worktree with absolute path to relative path' '
-+ git config worktree.useRelativePaths false &&
++ test_config worktree.useRelativePaths false &&
+ git worktree add ./absolute &&
+ git worktree move --relative-paths absolute relative &&
-+ cat relative/.git >actual &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
-+ test_cmp expect actual &&
-+ git config worktree.useRelativePaths true &&
++ test_cmp expect relative/.git &&
++ echo "../../../relative/.git" >expect &&
++ test_cmp expect .git/worktrees/absolute/gitdir &&
++ test_config worktree.useRelativePaths true &&
+ git worktree move relative relative2 &&
-+ cat relative2/.git >actual &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
-+ test_cmp expect actual
++ test_cmp expect relative2/.git &&
++ echo "../../../relative2/.git" >expect &&
++ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
+test_expect_success 'move worktree with relative path to absolute path' '
-+ git config worktree.useRelativePaths true &&
++ test_config worktree.useRelativePaths true &&
+ git worktree move --no-relative-paths relative2 absolute &&
-+ cat absolute/.git >actual &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
-+ test_cmp expect actual
++ test_cmp expect absolute/.git &&
++ echo "$(pwd)/absolute/.git" >expect &&
++ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
test_done
@@ worktree.c: int validate_worktree(const struct worktree *wt, struct strbuf *errm
}
-void update_worktree_location(struct worktree *wt, const char *path_)
-+void update_worktree_location(struct worktree *wt,
-+ const char *path_,
++void update_worktree_location(struct worktree *wt, const char *path_,
+ int use_relative_paths)
{
struct strbuf path = STRBUF_INIT;
@@ worktree.c: int validate_worktree(const struct worktree *wt, struct strbuf *errm
## worktree.h ##
@@ worktree.h: int validate_worktree(const struct worktree *wt,
+ /*
* Update worktrees/xxx/gitdir with the new path.
*/
- void update_worktree_location(struct worktree *wt,
+-void update_worktree_location(struct worktree *wt,
- const char *path_);
-+ const char *path_,
++void update_worktree_location(struct worktree *wt, const char *path_,
+ int use_relative_paths);
typedef void (* worktree_repair_fn)(int iserr, const char *path,
7: 30e9e41061 ! 7: 4f951f4088 worktree: add relative cli/config options to `repair` command
@@ Commit message
even if the original path was correct. This allows a user to covert
existing worktrees between absolute/relative as desired.
+ To simplify things, both linking files are written when one of the files
+ needs to be repaired. In some cases, this fixes the other file before it
+ is checked, in other cases this results in a correct file being written
+ with the same contents.
+
Thanks for adding this, I think it will be really helpful for anyone
trying to understand this change in the future.
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## Documentation/git-worktree.txt ##
@@ Documentation/git-worktree.txt: This can also be set up as the default behaviour by using the
- --[no-]relative-paths::
+ Link worktrees using relative paths or absolute paths (default).
Overrides the `worktree.useRelativePaths` config option, see
linkgit:git-config[1].
++
@@ t/t2406-worktree-repair.sh: test_expect_success 'repair copied main and linked w
test_cmp dup/linked.expect dup/linked/.git
'
++test_expect_success 'repair worktree with relative path with missing gitfile' '
++ test_when_finished "rm -rf main wt" &&
++ test_create_repo main &&
++ git -C main config worktree.useRelativePaths true &&
++ test_commit -C main init &&
++ git -C main worktree add --detach ../wt &&
++ rm wt/.git &&
++ test_path_is_missing wt/.git &&
++ git -C main worktree repair &&
++ echo "gitdir: ../main/.git/worktrees/wt" >expect &&
++ test_cmp expect wt/.git
++'
++
+test_expect_success 'repair absolute worktree to use relative paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
@@ worktree.c: int other_head_refs(each_ref_fn fn, void *cb_data)
*/
static void repair_gitfile(struct worktree *wt,
- worktree_repair_fn fn, void *cb_data)
-+ worktree_repair_fn fn,
-+ void *cb_data,
++ worktree_repair_fn fn, void *cb_data,
+ int use_relative_paths)
{
struct strbuf dotgit = STRBUF_INIT;
@@ worktree.c: static ssize_t infer_backlink(const char *gitfile, struct strbuf *in
*/
void repair_worktree_at_path(const char *path,
- worktree_repair_fn fn, void *cb_data)
-+ worktree_repair_fn fn,
-+ void *cb_data,
++ worktree_repair_fn fn, void *cb_data,
+ int use_relative_paths)
{
struct strbuf dotgit = STRBUF_INIT;
8: 298327f538 ! 8: 28eb7f66b2 worktree: refactor `repair_worktree_after_gitdir_move()`
@@ Commit message
`write_worktree_linking_files` function. It also preserves the
relativity of the linking files; e.g., if an existing worktree used
absolute paths then the repaired paths will be absolute (and visa-versa).
+ `repair_worktree_after_gitdir_move()` is used to repair both sets of
+ worktree linking files if the `.git` directory is moved during a
+ re-initialization using `git init`.
Thanks for the extra explanation
This also adds a test case for reinitializing a repository that has
relative worktrees.
Signed-off-by: Caleb White <cdwhite3@pm.me>
- Signed-off-by: Taylor Blau <me@ttaylorr.com>
+ Signed-off-by: Junio C Hamano <gitster@pobox.com>
## t/t0001-init.sh ##
@@ t/t0001-init.sh: test_expect_success SYMLINKS 're-init to move gitdir symlink' '
@@ t/t0001-init.sh: test_expect_success SYMLINKS 're-init to move gitdir symlink' '
git init mainwt &&
+ if test "relative" = $2
+ then
-+ git -C mainwt config worktree.useRelativePaths true
++ test_config -C mainwt worktree.useRelativePaths true
+ else
-+ git -C mainwt config worktree.useRelativePaths false
++ test_config -C mainwt worktree.useRelativePaths false
+ fi
test_commit -C mainwt gumby &&
git -C mainwt worktree add --detach ../linkwt &&
> - Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me
> Changes in v4:
> - Fixed failing test in ci
> - Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me
> Changes in v3:
> - Split patches into smaller edits.
> - Moved tests into the patches with the relevant code changes.
> - Removed global `use_relative_paths` and instead pass parameter to functions.
> - Changed `infer_backlink` return type from `int` to `ssize_t`.
> - Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.
> - Reordered patches
> - Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me
> Changes in v2:
> - Fixed a bug where repositories with valid extensions would be downgraded
> to v0 during reinitialization, causing future operations to fail.
> - Split patch [1/2] into three separate patches.
> - Updated cover letter and commit messages.
> - Updated documentation wording.
> - Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me
>
> ---
> Caleb White (8):
> setup: correctly reinitialize repository version
> worktree: add `relativeWorktrees` extension
> worktree: refactor infer_backlink return
> worktree: add `write_worktree_linking_files()` function
> worktree: add relative cli/config options to `add` command
> worktree: add relative cli/config options to `move` command
> worktree: add relative cli/config options to `repair` command
> worktree: refactor `repair_worktree_after_gitdir_move()`
>
> Documentation/config/extensions.txt | 6 ++
> Documentation/config/worktree.txt | 10 +++
> Documentation/git-worktree.txt | 8 ++
> builtin/worktree.c | 29 ++++---
> repository.c | 1 +
> repository.h | 1 +
> setup.c | 39 ++++++---
> setup.h | 1 +
> t/t0001-init.sh | 22 ++++-
> t/t2400-worktree-add.sh | 45 +++++++++++
> t/t2401-worktree-prune.sh | 3 +-
> t/t2402-worktree-list.sh | 22 +++++
> t/t2403-worktree-move.sh | 25 ++++++
> t/t2406-worktree-repair.sh | 39 +++++++++
> t/t2408-worktree-relative.sh | 39 ---------
> t/t5504-fetch-receive-strict.sh | 6 +-
> worktree.c | 157 ++++++++++++++++++++----------------
> worktree.h | 22 ++++-
> 18 files changed, 333 insertions(+), 142 deletions(-)
> ---
> base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559
> change-id: 20241025-wt_relative_options-afa41987bc32
>
> Best regards,
^ permalink raw reply [flat|nested] 60+ messages in thread* Re: [PATCH v5 0/8] Allow relative worktree linking to be configured by the user
2024-11-28 14:44 ` Phillip Wood
@ 2024-11-28 17:58 ` Caleb White
0 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-28 17:58 UTC (permalink / raw)
To: phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On Thu Nov 28, 2024 at 8:44 AM CST, Phillip Wood wrote:
> On 26/11/2024 01:51, Caleb White wrote:
>> Changes in v5:
>> - Added docs to `--relative-paths` option.
>> - Added test coverage for `repair_worktrees()` and relative paths.
>> - Move `strbuf_reset` call in `infer_backlink()`.
>> - Cleaned up tests.
>> - Slight stylistic changes.
>> - Tweaked commit messages.
>> - Updated base to 090d24e9af.
>
> Thanks for re-rolling, these changes sound good. Below is the
> range-diff of what is in seen today compared to last week. I've left
> it untrimmed so other people can check what's changed and I've added a
> couple of comments. The only thing I'm worried about is the deletion
> of a check for setting extensions.relativeWorktrees in patch 5 the
> rest of the changes look good, thank you for the extra test checks and
> log messages.
>
> @@ t/t2400-worktree-add.sh: test_expect_success '"add" with initialized submodule,
> + git init repo &&
> + git -C repo commit --allow-empty -m base &&
> + git -C repo worktree add --relative-paths ./foo &&
> -+ git -C repo config get core.repositoryformatversion >actual &&
> -+ echo 1 >expected &&
> -+ test_cmp expected actual &&
> -+ git -C repo config get extensions.relativeworktrees >actual &&
> -+ echo true >expected &&
> -+ test_cmp expected actual
> ++ test_cmp_config -C repo 1 core.repositoryformatversion
> +'
>
> We have lost the check for extensions.relativeworktrees
> here. Although we don't set worktree.useRelativePaths anymore we
> should still set the extension as we pase --relative-paths to "git
> worktree add"
Good catch! This was removed unintentionally. I'll add this back in, but
will hold off on re-rolling until we're ready for the final re-roll (or
there's other changes requested).
Best,
Caleb
^ permalink raw reply [flat|nested] 60+ messages in thread
* [PATCH v6 0/8] Allow relative worktree linking to be configured by the user
2024-11-26 1:51 ` [PATCH v5 " Caleb White
` (9 preceding siblings ...)
2024-11-28 14:44 ` Phillip Wood
@ 2024-11-29 22:22 ` Caleb White
2024-11-29 22:22 ` [PATCH v6 1/8] setup: correctly reinitialize repository version Caleb White
` (8 more replies)
10 siblings, 9 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:22 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This series introduces the `--[no-]relative-paths` CLI option for
`git worktree {add, move, repair}` commands, as well as the
`worktree.useRelativePaths` configuration setting. When enabled,
these options allow worktrees to be linked using relative paths,
enhancing portability across environments where absolute paths
may differ (e.g., containerized setups, shared network drives).
Git still creates absolute paths by default, but these options allow
users to opt-in to relative paths if desired.
Using the `--relative-paths` option with `worktree {move, repair}`
will convert absolute paths to relative ones, while `--no-relative-paths`
does the reverse. For cases where users want consistency in path handling,
the config option `worktree.useRelativePaths` provides a persistent setting.
A new extension, `relativeWorktrees`, is added to indicate that at least
one worktree in the repository has been linked with relative paths. This
extension is automatically set when a worktree is created or repaired
using the `--relative-paths` option, or when the
`worktree.useRelativePaths` config is set to `true`.
The `relativeWorktrees` extension ensures older Git versions do not
attempt to automatically prune worktrees with relative paths, as they
would not not recognize the paths as being valid.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
The base for this patch series is 090d24e9af.
Link to original patch series:
https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me
---
Changes in v6:
- Re-add test for extensions.relativeworktrees config setting
- Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me
Changes in v5:
- Added docs to `--relative-paths` option.
- Added test coverage for `repair_worktrees()` and relative paths.
- Move `strbuf_reset` call in `infer_backlink()`.
- Cleaned up tests.
- Slight stylistic changes.
- Tweaked commit messages.
- Updated base to 090d24e9af.
- Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me
Changes in v4:
- Fixed failing test in ci
- Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me
Changes in v3:
- Split patches into smaller edits.
- Moved tests into the patches with the relevant code changes.
- Removed global `use_relative_paths` and instead pass parameter to functions.
- Changed `infer_backlink` return type from `int` to `ssize_t`.
- Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.
- Reordered patches
- Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me
Changes in v2:
- Fixed a bug where repositories with valid extensions would be downgraded
to v0 during reinitialization, causing future operations to fail.
- Split patch [1/2] into three separate patches.
- Updated cover letter and commit messages.
- Updated documentation wording.
- Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me
---
Caleb White (8):
setup: correctly reinitialize repository version
worktree: add `relativeWorktrees` extension
worktree: refactor infer_backlink return
worktree: add `write_worktree_linking_files()` function
worktree: add relative cli/config options to `add` command
worktree: add relative cli/config options to `move` command
worktree: add relative cli/config options to `repair` command
worktree: refactor `repair_worktree_after_gitdir_move()`
Documentation/config/extensions.txt | 6 ++
Documentation/config/worktree.txt | 10 +++
Documentation/git-worktree.txt | 8 ++
builtin/worktree.c | 29 ++++---
repository.c | 1 +
repository.h | 1 +
setup.c | 39 ++++++---
setup.h | 1 +
t/t0001-init.sh | 22 ++++-
t/t2400-worktree-add.sh | 46 +++++++++++
t/t2401-worktree-prune.sh | 3 +-
t/t2402-worktree-list.sh | 22 +++++
t/t2403-worktree-move.sh | 25 ++++++
t/t2406-worktree-repair.sh | 39 +++++++++
t/t2408-worktree-relative.sh | 39 ---------
t/t5504-fetch-receive-strict.sh | 6 +-
worktree.c | 157 ++++++++++++++++++++----------------
worktree.h | 22 ++++-
18 files changed, 334 insertions(+), 142 deletions(-)
---
base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559
change-id: 20241025-wt_relative_options-afa41987bc32
Best regards,
--
Caleb White <cdwhite3@pm.me>
^ permalink raw reply [flat|nested] 60+ messages in thread* [PATCH v6 1/8] setup: correctly reinitialize repository version
2024-11-29 22:22 ` [PATCH v6 " Caleb White
@ 2024-11-29 22:22 ` Caleb White
2024-11-29 22:22 ` [PATCH v6 2/8] worktree: add `relativeWorktrees` extension Caleb White
` (7 subsequent siblings)
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:22 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
When reinitializing a repository, Git does not account for extensions
other than `objectformat` and `refstorage` when determining the
repository version. This can lead to a repository being downgraded to
version 0 if extensions are set, causing Git future operations to fail.
This patch teaches Git to check if other extensions are defined in the
config to ensure that the repository version is set correctly.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
setup.c | 32 +++++++++++++++++++++++---------
t/t5504-fetch-receive-strict.sh | 6 +++---
2 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/setup.c b/setup.c
index 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644
--- a/setup.c
+++ b/setup.c
@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,
enum ref_storage_format ref_storage_format,
int reinit)
{
- char repo_version_string[10];
- int repo_version = GIT_REPO_VERSION;
+ struct strbuf repo_version = STRBUF_INIT;
+ int target_version = GIT_REPO_VERSION;
/*
* Note that we initialize the repository version to 1 when the ref
@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,
*/
if (hash_algo != GIT_HASH_SHA1 ||
ref_storage_format != REF_STORAGE_FORMAT_FILES)
- repo_version = GIT_REPO_VERSION_READ;
-
- /* This forces creation of new config file */
- xsnprintf(repo_version_string, sizeof(repo_version_string),
- "%d", repo_version);
- git_config_set("core.repositoryformatversion", repo_version_string);
+ target_version = GIT_REPO_VERSION_READ;
if (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)
git_config_set("extensions.objectformat",
@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,
ref_storage_format_to_name(ref_storage_format));
else if (reinit)
git_config_set_gently("extensions.refstorage", NULL);
+
+ if (reinit) {
+ struct strbuf config = STRBUF_INIT;
+ struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
+
+ strbuf_git_common_path(&config, the_repository, "config");
+ read_repository_format(&repo_fmt, config.buf);
+
+ if (repo_fmt.v1_only_extensions.nr)
+ target_version = GIT_REPO_VERSION_READ;
+
+ strbuf_release(&config);
+ clear_repository_format(&repo_fmt);
+ }
+
+ strbuf_addf(&repo_version, "%d", target_version);
+ git_config_set("core.repositoryformatversion", repo_version.buf);
+
+ strbuf_release(&repo_version);
}
static int is_reinit(void)
@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,
adjust_shared_perm(repo_get_git_dir(the_repository));
}
- initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);
+ initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);
/* Check filemode trustability */
path = git_path_buf(&buf, "config");
diff --git a/t/t5504-fetch-receive-strict.sh b/t/t5504-fetch-receive-strict.sh
index 138e6778a477650ecbe2dc3e480c5fe83d4bb485..290d2a591adae02acf5bcf24dbbff2a0bbfceac8 100755
--- a/t/t5504-fetch-receive-strict.sh
+++ b/t/t5504-fetch-receive-strict.sh
@@ -171,7 +171,7 @@ test_expect_success 'fsck with invalid or bogus skipList input' '
test_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&
test_grep "could not open.*: does-not-exist" err &&
test_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&
- test_grep "invalid object name: \[core\]" err
+ test_grep "invalid object name: " err
'
test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '
@@ -234,7 +234,7 @@ test_expect_success 'push with receive.fsck.skipList' '
test_grep "could not open.*: does-not-exist" err &&
git --git-dir=dst/.git config receive.fsck.skipList config &&
test_must_fail git push --porcelain dst bogus 2>err &&
- test_grep "invalid object name: \[core\]" err &&
+ test_grep "invalid object name: " err &&
git --git-dir=dst/.git config receive.fsck.skipList SKIP &&
git push --porcelain dst bogus
@@ -263,7 +263,7 @@ test_expect_success 'fetch with fetch.fsck.skipList' '
test_grep "could not open.*: does-not-exist" err &&
git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&
test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
- test_grep "invalid object name: \[core\]" err &&
+ test_grep "invalid object name: " err &&
git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&
git --git-dir=dst/.git fetch "file://$(pwd)" $refspec
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 2/8] worktree: add `relativeWorktrees` extension
2024-11-29 22:22 ` [PATCH v6 " Caleb White
2024-11-29 22:22 ` [PATCH v6 1/8] setup: correctly reinitialize repository version Caleb White
@ 2024-11-29 22:22 ` Caleb White
2024-11-29 22:22 ` [PATCH v6 3/8] worktree: refactor infer_backlink return Caleb White
` (6 subsequent siblings)
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:22 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
A new extension, `relativeWorktrees`, is added to indicate that at least
one worktree in the repository has been linked with relative paths.
This ensures older Git versions do not attempt to automatically prune
worktrees with relative paths, as they would not not recognize the
paths as being valid.
Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/config/extensions.txt | 6 ++++++
repository.c | 1 +
repository.h | 1 +
setup.c | 7 +++++++
setup.h | 1 +
5 files changed, 16 insertions(+)
diff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt
index 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644
--- a/Documentation/config/extensions.txt
+++ b/Documentation/config/extensions.txt
@@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or
linkgit:git-clone[1]. Trying to change it after initialization will not
work and will produce hard-to-diagnose issues.
+relativeWorktrees::
+ If enabled, indicates at least one worktree has been linked with
+ relative paths. Automatically set if a worktree has been created or
+ repaired with either the `--relative-paths` option or with the
+ `worktree.useRelativePaths` config set to `true`.
+
worktreeConfig::
If enabled, then worktrees will load config settings from the
`$GIT_DIR/config.worktree` file in addition to the
diff --git a/repository.c b/repository.c
index f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644
--- a/repository.c
+++ b/repository.c
@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,
repo_set_compat_hash_algo(repo, format.compat_hash_algo);
repo_set_ref_storage_format(repo, format.ref_storage_format);
repo->repository_format_worktree_config = format.worktree_config;
+ repo->repository_format_relative_worktrees = format.relative_worktrees;
/* take ownership of format.partial_clone */
repo->repository_format_partial_clone = format.partial_clone;
diff --git a/repository.h b/repository.h
index 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644
--- a/repository.h
+++ b/repository.h
@@ -150,6 +150,7 @@ struct repository {
/* Configurations */
int repository_format_worktree_config;
+ int repository_format_relative_worktrees;
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
unsigned different_commondir:1;
diff --git a/setup.c b/setup.c
index 1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631..39ff48d9dc5d67b16159c6cca66ff2663bbba6cf 100644
--- a/setup.c
+++ b/setup.c
@@ -683,6 +683,9 @@ static enum extension_result handle_extension(const char *var,
"extensions.refstorage", value);
data->ref_storage_format = format;
return EXTENSION_OK;
+ } else if (!strcmp(ext, "relativeworktrees")) {
+ data->relative_worktrees = git_config_bool(var, value);
+ return EXTENSION_OK;
}
return EXTENSION_UNKNOWN;
}
@@ -1854,6 +1857,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
repo_fmt.ref_storage_format);
the_repository->repository_format_worktree_config =
repo_fmt.worktree_config;
+ the_repository->repository_format_relative_worktrees =
+ repo_fmt.relative_worktrees;
/* take ownership of repo_fmt.partial_clone */
the_repository->repository_format_partial_clone =
repo_fmt.partial_clone;
@@ -1950,6 +1955,8 @@ void check_repository_format(struct repository_format *fmt)
fmt->ref_storage_format);
the_repository->repository_format_worktree_config =
fmt->worktree_config;
+ the_repository->repository_format_relative_worktrees =
+ fmt->relative_worktrees;
the_repository->repository_format_partial_clone =
xstrdup_or_null(fmt->partial_clone);
clear_repository_format(&repo_fmt);
diff --git a/setup.h b/setup.h
index e496ab3e4de580c2d9f95a7ea0eaf90e0d41b070..18dc3b73686ce28fac2fe04282ce95f8bf3e6b74 100644
--- a/setup.h
+++ b/setup.h
@@ -129,6 +129,7 @@ struct repository_format {
int precious_objects;
char *partial_clone; /* value of extensions.partialclone */
int worktree_config;
+ int relative_worktrees;
int is_bare;
int hash_algo;
int compat_hash_algo;
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 3/8] worktree: refactor infer_backlink return
2024-11-29 22:22 ` [PATCH v6 " Caleb White
2024-11-29 22:22 ` [PATCH v6 1/8] setup: correctly reinitialize repository version Caleb White
2024-11-29 22:22 ` [PATCH v6 2/8] worktree: add `relativeWorktrees` extension Caleb White
@ 2024-11-29 22:22 ` Caleb White
2024-11-29 22:22 ` [PATCH v6 4/8] worktree: add `write_worktree_linking_files()` function Caleb White
` (5 subsequent siblings)
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:22 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
The previous round[1] was merged a bit early before reviewer feedback
could be applied. This correctly indents a code block and updates the
`infer_backlink` function to return `-1` on failure and strbuf.len on
success.
[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
worktree.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/worktree.c b/worktree.c
index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..afde5394c8760c328f12aa321be3ac5c199cc0f1 100644
--- a/worktree.c
+++ b/worktree.c
@@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id,
strbuf_strip_suffix(&worktree_path, "/.git");
if (!is_absolute_path(worktree_path.buf)) {
- strbuf_strip_suffix(&path, "gitdir");
- strbuf_addbuf(&path, &worktree_path);
- strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
+ strbuf_strip_suffix(&path, "gitdir");
+ strbuf_addbuf(&path, &worktree_path);
+ strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
}
CALLOC_ARRAY(worktree, 1);
@@ -725,8 +725,10 @@ static int is_main_worktree_path(const char *path)
* won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
* be able to infer the gitdir by manually reading /path/to/worktree/.git,
* extracting the <id>, and checking if <repo>/worktrees/<id> exists.
+ *
+ * Returns -1 on failure and strbuf.len on success.
*/
-static int infer_backlink(const char *gitfile, struct strbuf *inferred)
+static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
{
struct strbuf actual = STRBUF_INIT;
const char *id;
@@ -747,12 +749,11 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)
goto error;
strbuf_release(&actual);
- return 1;
-
+ return inferred->len;
error:
strbuf_release(&actual);
strbuf_reset(inferred); /* clear invalid path */
- return 0;
+ return -1;
}
/*
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 4/8] worktree: add `write_worktree_linking_files()` function
2024-11-29 22:22 ` [PATCH v6 " Caleb White
` (2 preceding siblings ...)
2024-11-29 22:22 ` [PATCH v6 3/8] worktree: refactor infer_backlink return Caleb White
@ 2024-11-29 22:22 ` Caleb White
2024-11-29 22:22 ` [PATCH v6 5/8] worktree: add relative cli/config options to `add` command Caleb White
` (4 subsequent siblings)
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:22 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
A new helper function, `write_worktree_linking_files()`, centralizes
the logic for computing and writing either relative or absolute
paths, based on the provided configuration. This function accepts
`strbuf` pointers to both the worktree’s `.git` link and the
repository’s `gitdir`, and then writes the appropriate path to each.
The `relativeWorktrees` extension is automatically set when a worktree
is linked with relative paths.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
worktree.c | 35 +++++++++++++++++++++++++++++++++++
worktree.h | 13 +++++++++++++
2 files changed, 48 insertions(+)
diff --git a/worktree.c b/worktree.c
index afde5394c8760c328f12aa321be3ac5c199cc0f1..cf05045cc973f121a0a76b5ccfa731acf25d1a73 100644
--- a/worktree.c
+++ b/worktree.c
@@ -1032,3 +1032,38 @@ int init_worktree_config(struct repository *r)
free(main_worktree_file);
return res;
}
+
+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
+ int use_relative_paths)
+{
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf repo = STRBUF_INIT;
+ struct strbuf tmp = STRBUF_INIT;
+
+ strbuf_addbuf(&path, &dotgit);
+ strbuf_strip_suffix(&path, "/.git");
+ strbuf_realpath(&path, path.buf, 1);
+ strbuf_addbuf(&repo, &gitdir);
+ strbuf_strip_suffix(&repo, "/gitdir");
+ strbuf_realpath(&repo, repo.buf, 1);
+
+ if (use_relative_paths && !the_repository->repository_format_relative_worktrees) {
+ if (upgrade_repository_format(1) < 0)
+ die(_("unable to upgrade repository format to support relative worktrees"));
+ if (git_config_set_gently("extensions.relativeWorktrees", "true"))
+ die(_("unable to set extensions.relativeWorktrees setting"));
+ the_repository->repository_format_relative_worktrees = 1;
+ }
+
+ if (use_relative_paths) {
+ write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
+ write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
+ } else {
+ write_file(gitdir.buf, "%s/.git", path.buf);
+ write_file(dotgit.buf, "gitdir: %s", repo.buf);
+ }
+
+ strbuf_release(&path);
+ strbuf_release(&repo);
+ strbuf_release(&tmp);
+}
diff --git a/worktree.h b/worktree.h
index e96118621638667d87c5d7e0452ed10bd1ddf606..fd040f5d999697b603df929679bdddd2ff7f6eea 100644
--- a/worktree.h
+++ b/worktree.h
@@ -215,4 +215,17 @@ void strbuf_worktree_ref(const struct worktree *wt,
*/
int init_worktree_config(struct repository *r);
+/**
+ * Write the .git file and gitdir file that links the worktree to the repository.
+ *
+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
+ * is the path to the repository's `gitdir` file.
+ *
+ * Example
+ * dotgit: "/path/to/foo/.git"
+ * gitdir: "/path/to/repo/worktrees/foo/gitdir"
+ */
+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
+ int use_relative_paths);
+
#endif
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 5/8] worktree: add relative cli/config options to `add` command
2024-11-29 22:22 ` [PATCH v6 " Caleb White
` (3 preceding siblings ...)
2024-11-29 22:22 ` [PATCH v6 4/8] worktree: add `write_worktree_linking_files()` function Caleb White
@ 2024-11-29 22:22 ` Caleb White
2024-11-29 22:23 ` [PATCH v6 6/8] worktree: add relative cli/config options to `move` command Caleb White
` (3 subsequent siblings)
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:22 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This introduces the `--[no-]relative-paths` CLI option and
`worktree.useRelativePaths` configuration setting to the `worktree add`
command. When enabled these options allow worktrees to be linked using
relative paths, enhancing portability across environments where absolute
paths may differ (e.g., containerized setups, shared network drives).
Git still creates absolute paths by default, but these options allow
users to opt-in to relative paths if desired.
The t2408 test file is removed and more comprehensive tests are
written for the various worktree operations in their own files.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/config/worktree.txt | 10 +++++++++
Documentation/git-worktree.txt | 5 +++++
builtin/worktree.c | 19 ++++++++--------
t/t2400-worktree-add.sh | 46 +++++++++++++++++++++++++++++++++++++++
t/t2401-worktree-prune.sh | 3 ++-
t/t2402-worktree-list.sh | 22 +++++++++++++++++++
t/t2408-worktree-relative.sh | 39 ---------------------------------
7 files changed, 95 insertions(+), 49 deletions(-)
diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
index 048e349482df6c892055720eb53cdcd6c327b6ed..5e35c7d018aecdedca0642b11e45df6d19024d42 100644
--- a/Documentation/config/worktree.txt
+++ b/Documentation/config/worktree.txt
@@ -7,3 +7,13 @@ worktree.guessRemote::
such a branch exists, it is checked out and set as "upstream"
for the new branch. If no such match can be found, it falls
back to creating a new branch from the current HEAD.
+
+worktree.useRelativePaths::
+ Link worktrees using relative paths (when "true") or absolute
+ paths (when "false"). This is particularly useful for setups
+ where the repository and worktrees may be moved between
+ different locations or environments. Defaults to "false".
++
+Note that setting `worktree.useRelativePaths` to "true" implies enabling the
+`extension.relativeWorktrees` config (see linkgit:git-config[1]),
+thus making it incompatible with older versions of Git.
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 70437c815f13852bd2eb862176b8b933e6de0acf..60a671bbc255aa7763b4f77511c09f6a02783e00 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -216,6 +216,11 @@ To remove a locked worktree, specify `--force` twice.
This can also be set up as the default behaviour by using the
`worktree.guessRemote` config option.
+--[no-]relative-paths::
+ Link worktrees using relative paths or absolute paths (default).
+ Overrides the `worktree.useRelativePaths` config option, see
+ linkgit:git-config[1].
+
--[no-]track::
When creating a new branch, if `<commit-ish>` is a branch,
mark it as "upstream" from the new branch. This is the
diff --git a/builtin/worktree.c b/builtin/worktree.c
index dae63dedf4cac2621f51f95a39aa456b33acd894..e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -120,12 +120,14 @@ struct add_opts {
int quiet;
int checkout;
int orphan;
+ int relative_paths;
const char *keep_locked;
};
static int show_only;
static int verbose;
static int guess_remote;
+static int use_relative_paths;
static timestamp_t expire;
static int git_worktree_config(const char *var, const char *value,
@@ -134,6 +136,9 @@ static int git_worktree_config(const char *var, const char *value,
if (!strcmp(var, "worktree.guessremote")) {
guess_remote = git_config_bool(var, value);
return 0;
+ } else if (!strcmp(var, "worktree.userelativepaths")) {
+ use_relative_paths = git_config_bool(var, value);
+ return 0;
}
return git_default_config(var, value, ctx, cb);
@@ -414,8 +419,7 @@ static int add_worktree(const char *path, const char *refname,
const struct add_opts *opts)
{
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
- struct strbuf sb = STRBUF_INIT, sb_tmp = STRBUF_INIT;
- struct strbuf sb_path_realpath = STRBUF_INIT, sb_repo_realpath = STRBUF_INIT;
+ struct strbuf sb = STRBUF_INIT;
const char *name;
struct strvec child_env = STRVEC_INIT;
unsigned int counter = 0;
@@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname,
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
- strbuf_realpath(&sb_path_realpath, path, 1);
- strbuf_realpath(&sb_repo_realpath, sb_repo.buf, 1);
- write_file(sb.buf, "%s/.git", relative_path(sb_path_realpath.buf, sb_repo_realpath.buf, &sb_tmp));
- write_file(sb_git.buf, "gitdir: %s", relative_path(sb_repo_realpath.buf, sb_path_realpath.buf, &sb_tmp));
+ write_worktree_linking_files(sb_git, sb, opts->relative_paths);
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
write_file(sb.buf, "../..");
@@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname,
strvec_clear(&child_env);
strbuf_release(&sb);
- strbuf_release(&sb_tmp);
strbuf_release(&symref);
strbuf_release(&sb_repo);
- strbuf_release(&sb_repo_realpath);
strbuf_release(&sb_git);
- strbuf_release(&sb_path_realpath);
strbuf_release(&sb_name);
free_worktree(wt);
return ret;
@@ -796,12 +794,15 @@ static int add(int ac, const char **av, const char *prefix)
PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
OPT_BOOL(0, "guess-remote", &guess_remote,
N_("try to match the new branch name with a remote-tracking branch")),
+ OPT_BOOL(0, "relative-paths", &opts.relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
int ret;
memset(&opts, 0, sizeof(opts));
opts.checkout = 1;
+ opts.relative_paths = use_relative_paths;
ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);
if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index cfc4aeb1798c6d023909cec771e5b74e983af5ea..bc4f4e90d6ecfedbde9082bda6f9e4eec3e3575d 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -1207,4 +1207,50 @@ test_expect_success '"add" with initialized submodule, with submodule.recurse se
git -C project-clone -c submodule.recurse worktree add ../project-5
'
+test_expect_success 'can create worktrees with relative paths' '
+ test_when_finished "git worktree remove relative" &&
+ test_config worktree.useRelativePaths false &&
+ git worktree add --relative-paths ./relative &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
+ test_cmp expect relative/.git &&
+ echo "../../../relative/.git" >expect &&
+ test_cmp expect .git/worktrees/relative/gitdir
+'
+
+test_expect_success 'can create worktrees with absolute paths' '
+ test_config worktree.useRelativePaths true &&
+ git worktree add ./relative &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
+ test_cmp expect relative/.git &&
+ git worktree add --no-relative-paths ./absolute &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+ test_cmp expect absolute/.git &&
+ echo "$(pwd)/absolute/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
+test_expect_success 'move repo without breaking relative internal links' '
+ test_when_finished rm -rf repo moved &&
+ git init repo &&
+ (
+ cd repo &&
+ test_commit initial &&
+ git worktree add --relative-paths wt1 &&
+ cd .. &&
+ mv repo moved &&
+ cd moved/wt1 &&
+ git worktree list >out 2>err &&
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'relative worktree sets extension config' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ git -C repo commit --allow-empty -m base &&
+ git -C repo worktree add --relative-paths ./foo &&
+ test_cmp_config -C repo 1 core.repositoryformatversion &&
+ test_cmp_config -C repo true extensions.relativeworktrees
+'
+
test_done
diff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh
index 976d048e3efc74be9cd909ce76d552b3944d2e10..5eb52b9abbf29514dc082c260ebb7a5e8e63aae0 100755
--- a/t/t2401-worktree-prune.sh
+++ b/t/t2401-worktree-prune.sh
@@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' '
! test -d .git/worktrees/wt
'
-test_expect_success 'not prune proper worktrees when run inside linked worktree' '
+test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '
test_when_finished rm -rf repo wt_ext &&
git init repo &&
(
cd repo &&
+ git config worktree.useRelativePaths true &&
echo content >file &&
git add file &&
git commit -m msg &&
diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
index 33ea9cb21ba07c9563530b54da06753eaa993fe2..780daa6cd6351f8fa9434619cc212aade8f01420 100755
--- a/t/t2402-worktree-list.sh
+++ b/t/t2402-worktree-list.sh
@@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' '
'
test_expect_success 'linked worktrees are sorted' '
+ test_when_finished "rm -rf sorted" &&
mkdir sorted &&
git init sorted/main &&
(
@@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' '
test_cmp expected sorted/main/actual
'
+test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '
+ test_when_finished "rm -rf sorted" &&
+ mkdir sorted &&
+ git init sorted/main &&
+ (
+ cd sorted/main &&
+ test_tick &&
+ test_commit new &&
+ git worktree add --relative-paths ../first &&
+ git worktree add ../second &&
+ git worktree list --porcelain >out &&
+ grep ^worktree out >actual
+ ) &&
+ cat >expected <<-EOF &&
+ worktree $(pwd)/sorted/main
+ worktree $(pwd)/sorted/first
+ worktree $(pwd)/sorted/second
+ EOF
+ test_cmp expected sorted/main/actual
+'
+
test_expect_success 'worktree path when called in .git directory' '
git worktree list >list1 &&
git -C .git worktree list >list2 &&
diff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh
deleted file mode 100755
index a3136db7e28cb20926ff44211e246ce625a6e51a..0000000000000000000000000000000000000000
--- a/t/t2408-worktree-relative.sh
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/bin/sh
-
-test_description='test worktrees linked with relative paths'
-
-TEST_PASSES_SANITIZE_LEAK=true
-. ./test-lib.sh
-
-test_expect_success 'links worktrees with relative paths' '
- test_when_finished rm -rf repo &&
- git init repo &&
- (
- cd repo &&
- test_commit initial &&
- git worktree add wt1 &&
- echo "../../../wt1/.git" >expected_gitdir &&
- cat .git/worktrees/wt1/gitdir >actual_gitdir &&
- echo "gitdir: ../.git/worktrees/wt1" >expected_git &&
- cat wt1/.git >actual_git &&
- test_cmp expected_gitdir actual_gitdir &&
- test_cmp expected_git actual_git
- )
-'
-
-test_expect_success 'move repo without breaking relative internal links' '
- test_when_finished rm -rf repo moved &&
- git init repo &&
- (
- cd repo &&
- test_commit initial &&
- git worktree add wt1 &&
- cd .. &&
- mv repo moved &&
- cd moved/wt1 &&
- git status >out 2>err &&
- test_must_be_empty err
- )
-'
-
-test_done
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 6/8] worktree: add relative cli/config options to `move` command
2024-11-29 22:22 ` [PATCH v6 " Caleb White
` (4 preceding siblings ...)
2024-11-29 22:22 ` [PATCH v6 5/8] worktree: add relative cli/config options to `add` command Caleb White
@ 2024-11-29 22:23 ` Caleb White
2024-11-29 22:23 ` [PATCH v6 7/8] worktree: add relative cli/config options to `repair` command Caleb White
` (2 subsequent siblings)
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:23 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This teaches the `worktree move` command to respect the
`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`
config setting. If an existing worktree is moved with `--relative-paths`
the new path will be relative (and visa-versa).
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
builtin/worktree.c | 4 +++-
t/t2403-worktree-move.sh | 25 +++++++++++++++++++++++++
worktree.c | 22 +++++++++-------------
worktree.h | 4 ++--
4 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index e3b4a71ee0bc13d5e817cf7dcc398e9e2bd975de..302151506981718658db1cd338cd9064688f5c14 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
OPT__FORCE(&force,
N_("force move even if worktree is dirty or locked"),
PARSE_OPT_NOCOMPLETE),
+ OPT_BOOL(0, "relative-paths", &use_relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
struct worktree **worktrees, *wt;
@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
if (rename(wt->path, dst.buf) == -1)
die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
- update_worktree_location(wt, dst.buf);
+ update_worktree_location(wt, dst.buf, use_relative_paths);
strbuf_release(&dst);
free_worktrees(worktrees);
diff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh
index 901342ea09b51a8e832f1109fbb737df84283aa2..422c1a05580057b18ab8bfdfe38da4d723749493 100755
--- a/t/t2403-worktree-move.sh
+++ b/t/t2403-worktree-move.sh
@@ -247,4 +247,29 @@ test_expect_success 'not remove a repo with initialized submodule' '
)
'
+test_expect_success 'move worktree with absolute path to relative path' '
+ test_config worktree.useRelativePaths false &&
+ git worktree add ./absolute &&
+ git worktree move --relative-paths absolute relative &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
+ test_cmp expect relative/.git &&
+ echo "../../../relative/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir &&
+ test_config worktree.useRelativePaths true &&
+ git worktree move relative relative2 &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
+ test_cmp expect relative2/.git &&
+ echo "../../../relative2/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
+test_expect_success 'move worktree with relative path to absolute path' '
+ test_config worktree.useRelativePaths true &&
+ git worktree move --no-relative-paths relative2 absolute &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+ test_cmp expect absolute/.git &&
+ echo "$(pwd)/absolute/.git" >expect &&
+ test_cmp expect .git/worktrees/absolute/gitdir
+'
+
test_done
diff --git a/worktree.c b/worktree.c
index cf05045cc973f121a0a76b5ccfa731acf25d1a73..c749cb16994cf46ccccd4c2880ac917e497671b8 100644
--- a/worktree.c
+++ b/worktree.c
@@ -376,32 +376,28 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
return ret;
}
-void update_worktree_location(struct worktree *wt, const char *path_)
+void update_worktree_location(struct worktree *wt, const char *path_,
+ int use_relative_paths)
{
struct strbuf path = STRBUF_INIT;
- struct strbuf repo = STRBUF_INIT;
- struct strbuf file = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
+ struct strbuf dotgit = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
if (is_main_worktree(wt))
BUG("can't relocate main worktree");
- strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
+ strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
strbuf_realpath(&path, path_, 1);
+ strbuf_addf(&dotgit, "%s/.git", path.buf);
if (fspathcmp(wt->path, path.buf)) {
- strbuf_addf(&file, "%s/gitdir", repo.buf);
- write_file(file.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
- strbuf_reset(&file);
- strbuf_addf(&file, "%s/.git", path.buf);
- write_file(file.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
free(wt->path);
wt->path = strbuf_detach(&path, NULL);
}
strbuf_release(&path);
- strbuf_release(&repo);
- strbuf_release(&file);
- strbuf_release(&tmp);
+ strbuf_release(&dotgit);
+ strbuf_release(&gitdir);
}
int is_worktree_being_rebased(const struct worktree *wt,
diff --git a/worktree.h b/worktree.h
index fd040f5d999697b603df929679bdddd2ff7f6eea..9c699d080d8ebf37712044136679db3821ee1f63 100644
--- a/worktree.h
+++ b/worktree.h
@@ -117,8 +117,8 @@ int validate_worktree(const struct worktree *wt,
/*
* Update worktrees/xxx/gitdir with the new path.
*/
-void update_worktree_location(struct worktree *wt,
- const char *path_);
+void update_worktree_location(struct worktree *wt, const char *path_,
+ int use_relative_paths);
typedef void (* worktree_repair_fn)(int iserr, const char *path,
const char *msg, void *cb_data);
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 7/8] worktree: add relative cli/config options to `repair` command
2024-11-29 22:22 ` [PATCH v6 " Caleb White
` (5 preceding siblings ...)
2024-11-29 22:23 ` [PATCH v6 6/8] worktree: add relative cli/config options to `move` command Caleb White
@ 2024-11-29 22:23 ` Caleb White
2024-11-29 22:23 ` [PATCH v6 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Caleb White
2024-12-02 14:57 ` [PATCH v6 0/8] Allow relative worktree linking to be configured by the user Phillip Wood
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:23 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This teaches the `worktree repair` command to respect the
`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`
config setting. If an existing worktree with an absolute path is repaired
with `--relative-paths`, the links will be replaced with relative paths,
even if the original path was correct. This allows a user to covert
existing worktrees between absolute/relative as desired.
To simplify things, both linking files are written when one of the files
needs to be repaired. In some cases, this fixes the other file before it
is checked, in other cases this results in a correct file being written
with the same contents.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/git-worktree.txt | 3 +++
builtin/worktree.c | 6 +++--
t/t2406-worktree-repair.sh | 39 +++++++++++++++++++++++++++++
worktree.c | 56 ++++++++++++++++++++----------------------
worktree.h | 5 ++--
5 files changed, 76 insertions(+), 33 deletions(-)
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 60a671bbc255aa7763b4f77511c09f6a02783e00..8340b7f028e6c1c3bae3de0879e9754098466d14 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -220,6 +220,9 @@ This can also be set up as the default behaviour by using the
Link worktrees using relative paths or absolute paths (default).
Overrides the `worktree.useRelativePaths` config option, see
linkgit:git-config[1].
++
+With `repair`, the linking files will be updated if there's an absolute/relative
+mismatch, even if the links are correct.
--[no-]track::
When creating a new branch, if `<commit-ish>` is a branch,
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 302151506981718658db1cd338cd9064688f5c14..fde9ff4dc9a734c655e95ccd62774282950cbba6 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -1385,6 +1385,8 @@ static int repair(int ac, const char **av, const char *prefix)
const char **p;
const char *self[] = { ".", NULL };
struct option options[] = {
+ OPT_BOOL(0, "relative-paths", &use_relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
int rc = 0;
@@ -1392,8 +1394,8 @@ static int repair(int ac, const char **av, const char *prefix)
ac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);
p = ac > 0 ? av : self;
for (; *p; p++)
- repair_worktree_at_path(*p, report_repair, &rc);
- repair_worktrees(report_repair, &rc);
+ repair_worktree_at_path(*p, report_repair, &rc, use_relative_paths);
+ repair_worktrees(report_repair, &rc, use_relative_paths);
return rc;
}
diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
index 7686e60f6ad186519b275f11a5e14064c905b207..49b70b999518d47e1edd72a61a847b427f4c67a1 100755
--- a/t/t2406-worktree-repair.sh
+++ b/t/t2406-worktree-repair.sh
@@ -216,4 +216,43 @@ test_expect_success 'repair copied main and linked worktrees' '
test_cmp dup/linked.expect dup/linked/.git
'
+test_expect_success 'repair worktree with relative path with missing gitfile' '
+ test_when_finished "rm -rf main wt" &&
+ test_create_repo main &&
+ git -C main config worktree.useRelativePaths true &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../wt &&
+ rm wt/.git &&
+ test_path_is_missing wt/.git &&
+ git -C main worktree repair &&
+ echo "gitdir: ../main/.git/worktrees/wt" >expect &&
+ test_cmp expect wt/.git
+'
+
+test_expect_success 'repair absolute worktree to use relative paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../side &&
+ echo "../../../../sidemoved/.git" >expect-gitdir &&
+ echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
+ mv side sidemoved &&
+ git -C main worktree repair --relative-paths ../sidemoved &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
+test_expect_success 'repair relative worktree to use absolute paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --relative-paths --detach ../side &&
+ echo "$(pwd)/sidemoved/.git" >expect-gitdir &&
+ echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
+ mv side sidemoved &&
+ git -C main worktree repair ../sidemoved &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
test_done
diff --git a/worktree.c b/worktree.c
index c749cb16994cf46ccccd4c2880ac917e497671b8..2e76bbc1494afc125997f803dc39846a0b95a84f 100644
--- a/worktree.c
+++ b/worktree.c
@@ -573,12 +573,13 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
* pointing at <repo>/worktrees/<id>.
*/
static void repair_gitfile(struct worktree *wt,
- worktree_repair_fn fn, void *cb_data)
+ worktree_repair_fn fn, void *cb_data,
+ int use_relative_paths)
{
struct strbuf dotgit = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
struct strbuf repo = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -594,6 +595,7 @@ static void repair_gitfile(struct worktree *wt,
strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
strbuf_addf(&dotgit, "%s/.git", wt->path);
+ strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
if (dotgit_contents) {
@@ -611,18 +613,20 @@ static void repair_gitfile(struct worktree *wt,
repair = _(".git file broken");
else if (fspathcmp(backlink.buf, repo.buf))
repair = _(".git file incorrect");
+ else if (use_relative_paths == is_absolute_path(dotgit_contents))
+ repair = _(".git file absolute/relative path mismatch");
if (repair) {
fn(0, wt->path, repair, cb_data);
- write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, wt->path, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
}
done:
free(dotgit_contents);
strbuf_release(&repo);
strbuf_release(&dotgit);
+ strbuf_release(&gitdir);
strbuf_release(&backlink);
- strbuf_release(&tmp);
}
static void repair_noop(int iserr UNUSED,
@@ -633,7 +637,7 @@ static void repair_noop(int iserr UNUSED,
/* nothing */
}
-void repair_worktrees(worktree_repair_fn fn, void *cb_data)
+void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths)
{
struct worktree **worktrees = get_worktrees_internal(1);
struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
@@ -641,7 +645,7 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)
if (!fn)
fn = repair_noop;
for (; *wt; wt++)
- repair_gitfile(*wt, fn, cb_data);
+ repair_gitfile(*wt, fn, cb_data, use_relative_paths);
free_worktrees(worktrees);
}
@@ -757,16 +761,14 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
* the worktree's path.
*/
void repair_worktree_at_path(const char *path,
- worktree_repair_fn fn, void *cb_data)
+ worktree_repair_fn fn, void *cb_data,
+ int use_relative_paths)
{
struct strbuf dotgit = STRBUF_INIT;
- struct strbuf realdotgit = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
struct strbuf inferred_backlink = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
struct strbuf olddotgit = STRBUF_INIT;
- struct strbuf realolddotgit = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -778,25 +780,25 @@ void repair_worktree_at_path(const char *path,
goto done;
strbuf_addf(&dotgit, "%s/.git", path);
- if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
+ if (!strbuf_realpath(&dotgit, dotgit.buf, 0)) {
fn(1, path, _("not a valid path"), cb_data);
goto done;
}
- infer_backlink(realdotgit.buf, &inferred_backlink);
+ infer_backlink(dotgit.buf, &inferred_backlink);
strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
- dotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
+ dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
if (dotgit_contents) {
if (is_absolute_path(dotgit_contents)) {
strbuf_addstr(&backlink, dotgit_contents);
} else {
- strbuf_addbuf(&backlink, &realdotgit);
+ strbuf_addbuf(&backlink, &dotgit);
strbuf_strip_suffix(&backlink, ".git");
strbuf_addstr(&backlink, dotgit_contents);
strbuf_realpath_forgiving(&backlink, backlink.buf, 0);
}
} else if (err == READ_GITFILE_ERR_NOT_A_FILE) {
- fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
+ fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
goto done;
} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
if (inferred_backlink.len) {
@@ -809,11 +811,11 @@ void repair_worktree_at_path(const char *path,
*/
strbuf_swap(&backlink, &inferred_backlink);
} else {
- fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
+ fn(1, dotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
goto done;
}
} else {
- fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
+ fn(1, dotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
goto done;
}
@@ -835,39 +837,35 @@ void repair_worktree_at_path(const char *path,
* in the "copy" repository. In this case, point the "copy" worktree's
* .git file at the "copy" repository.
*/
- if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) {
+ if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))
strbuf_swap(&backlink, &inferred_backlink);
- }
strbuf_addf(&gitdir, "%s/gitdir", backlink.buf);
if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
repair = _("gitdir unreadable");
+ else if (use_relative_paths == is_absolute_path(olddotgit.buf))
+ repair = _("gitdir absolute/relative path mismatch");
else {
strbuf_rtrim(&olddotgit);
- if (is_absolute_path(olddotgit.buf)) {
- strbuf_addbuf(&realolddotgit, &olddotgit);
- } else {
- strbuf_addf(&realolddotgit, "%s/%s", backlink.buf, olddotgit.buf);
- strbuf_realpath_forgiving(&realolddotgit, realolddotgit.buf, 0);
+ if (!is_absolute_path(olddotgit.buf)) {
+ strbuf_insertf(&olddotgit, 0, "%s/", backlink.buf);
+ strbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0);
}
- if (fspathcmp(realolddotgit.buf, realdotgit.buf))
+ if (fspathcmp(olddotgit.buf, dotgit.buf))
repair = _("gitdir incorrect");
}
if (repair) {
fn(0, gitdir.buf, repair, cb_data);
- write_file(gitdir.buf, "%s", relative_path(realdotgit.buf, backlink.buf, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
}
done:
free(dotgit_contents);
strbuf_release(&olddotgit);
- strbuf_release(&realolddotgit);
strbuf_release(&backlink);
strbuf_release(&inferred_backlink);
strbuf_release(&gitdir);
- strbuf_release(&realdotgit);
strbuf_release(&dotgit);
- strbuf_release(&tmp);
}
int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
diff --git a/worktree.h b/worktree.h
index 9c699d080d8ebf37712044136679db3821ee1f63..38145df80f41079f301d3aeaaffcc35b7f6760b9 100644
--- a/worktree.h
+++ b/worktree.h
@@ -129,7 +129,7 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,
* function, if non-NULL, is called with the path of the worktree and a
* description of the repair or error, along with the callback user-data.
*/
-void repair_worktrees(worktree_repair_fn, void *cb_data);
+void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths);
/*
* Repair the linked worktrees after the gitdir has been moved.
@@ -151,7 +151,8 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
* worktree and a description of the repair or error, along with the callback
* user-data.
*/
-void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
+void repair_worktree_at_path(const char *, worktree_repair_fn,
+ void *cb_data, int use_relative_paths);
/*
* Free up the memory for a worktree.
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* [PATCH v6 8/8] worktree: refactor `repair_worktree_after_gitdir_move()`
2024-11-29 22:22 ` [PATCH v6 " Caleb White
` (6 preceding siblings ...)
2024-11-29 22:23 ` [PATCH v6 7/8] worktree: add relative cli/config options to `repair` command Caleb White
@ 2024-11-29 22:23 ` Caleb White
2024-12-02 14:57 ` [PATCH v6 0/8] Allow relative worktree linking to be configured by the user Phillip Wood
8 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-11-29 22:23 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Phillip Wood, Junio C Hamano, Eric Sunshine,
Caleb White
This refactors `repair_worktree_after_gitdir_move()` to use the new
`write_worktree_linking_files` function. It also preserves the
relativity of the linking files; e.g., if an existing worktree used
absolute paths then the repaired paths will be absolute (and visa-versa).
`repair_worktree_after_gitdir_move()` is used to repair both sets of
worktree linking files if the `.git` directory is moved during a
re-initialization using `git init`.
This also adds a test case for reinitializing a repository that has
relative worktrees.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
t/t0001-init.sh | 22 ++++++++++++++++++----
worktree.c | 29 ++++++++---------------------
2 files changed, 26 insertions(+), 25 deletions(-)
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e394147b8480fb5784f1c23c0d2eae712eecc44b 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -434,6 +434,12 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
sep_git_dir_worktree () {
test_when_finished "rm -rf mainwt linkwt seprepo" &&
git init mainwt &&
+ if test "relative" = $2
+ then
+ test_config -C mainwt worktree.useRelativePaths true
+ else
+ test_config -C mainwt worktree.useRelativePaths false
+ fi
test_commit -C mainwt gumby &&
git -C mainwt worktree add --detach ../linkwt &&
git -C "$1" init --separate-git-dir ../seprepo &&
@@ -442,12 +448,20 @@ sep_git_dir_worktree () {
test_cmp expect actual
}
-test_expect_success 're-init to move gitdir with linked worktrees' '
- sep_git_dir_worktree mainwt
+test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '
+ sep_git_dir_worktree mainwt absolute
+'
+
+test_expect_success 're-init to move gitdir within linked worktree (absolute)' '
+ sep_git_dir_worktree linkwt absolute
+'
+
+test_expect_success 're-init to move gitdir with linked worktrees (relative)' '
+ sep_git_dir_worktree mainwt relative
'
-test_expect_success 're-init to move gitdir within linked worktree' '
- sep_git_dir_worktree linkwt
+test_expect_success 're-init to move gitdir within linked worktree (relative)' '
+ sep_git_dir_worktree linkwt relative
'
test_expect_success MINGW '.git hidden' '
diff --git a/worktree.c b/worktree.c
index 2e76bbc1494afc125997f803dc39846a0b95a84f..af68b24f9d0917a99cd965347aad6466e9dbb152 100644
--- a/worktree.c
+++ b/worktree.c
@@ -651,45 +651,32 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_pat
void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path)
{
- struct strbuf path = STRBUF_INIT;
- struct strbuf repo = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
struct strbuf dotgit = STRBUF_INIT;
- struct strbuf olddotgit = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
+ int is_relative_path;
if (is_main_worktree(wt))
goto done;
- strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
- strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
+ strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
- if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
+ if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0)
goto done;
- strbuf_rtrim(&olddotgit);
- if (is_absolute_path(olddotgit.buf)) {
- strbuf_addbuf(&dotgit, &olddotgit);
- } else {
- strbuf_addf(&dotgit, "%s/worktrees/%s/%s", old_path, wt->id, olddotgit.buf);
+ strbuf_rtrim(&dotgit);
+ is_relative_path = ! is_absolute_path(dotgit.buf);
+ if (is_relative_path) {
+ strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id);
strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
}
if (!file_exists(dotgit.buf))
goto done;
- strbuf_addbuf(&path, &dotgit);
- strbuf_strip_suffix(&path, "/.git");
-
- write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
- write_file(gitdir.buf, "%s", relative_path(dotgit.buf, repo.buf, &tmp));
+ write_worktree_linking_files(dotgit, gitdir, is_relative_path);
done:
- strbuf_release(&path);
- strbuf_release(&repo);
strbuf_release(&gitdir);
strbuf_release(&dotgit);
- strbuf_release(&olddotgit);
- strbuf_release(&tmp);
}
void repair_worktrees_after_gitdir_move(const char *old_path)
--
2.47.0
^ permalink raw reply related [flat|nested] 60+ messages in thread* Re: [PATCH v6 0/8] Allow relative worktree linking to be configured by the user
2024-11-29 22:22 ` [PATCH v6 " Caleb White
` (7 preceding siblings ...)
2024-11-29 22:23 ` [PATCH v6 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Caleb White
@ 2024-12-02 14:57 ` Phillip Wood
2024-12-03 4:54 ` Junio C Hamano
8 siblings, 1 reply; 60+ messages in thread
From: Phillip Wood @ 2024-12-02 14:57 UTC (permalink / raw)
To: Caleb White, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
Hi Caleb
On 29/11/2024 22:22, Caleb White wrote:
> Changes in v6:
> - Re-add test for extensions.relativeworktrees config setting
> - Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me
The range-diff below looks good, thanks for working on this
I think it's ready for next
Best Wishes
Phillip
1: 0986f98022 = 1: d897f2c16d setup: correctly reinitialize repository version
2: c36e1a59fa = 2: 1860ba1a2a worktree: add `relativeWorktrees` extension
3: 5b19b63040 = 3: 5976310916 worktree: refactor infer_backlink return
4: ec143ae00e = 4: 4dac9e3c01 worktree: add `write_worktree_linking_files()` function
5: 237206b08f ! 5: b7016344f1 worktree: add relative cli/config options to `add` command
@@ t/t2400-worktree-add.sh: test_expect_success '"add" with initialized submodule,
+ git init repo &&
+ git -C repo commit --allow-empty -m base &&
+ git -C repo worktree add --relative-paths ./foo &&
-+ test_cmp_config -C repo 1 core.repositoryformatversion
++ test_cmp_config -C repo 1 core.repositoryformatversion &&
++ test_cmp_config -C repo true extensions.relativeworktrees
+'
+
test_done
6: 8e4307f520 = 6: 298d2917e2 worktree: add relative cli/config options to `move` command
7: 4f951f4088 = 7: e6df1ee2c1 worktree: add relative cli/config options to `repair` command
8: 28eb7f66b2 = 8: 2037ca85ad worktree: refactor `repair_worktree_after_gitdir_move()`
> Changes in v5:
> - Added docs to `--relative-paths` option.
> - Added test coverage for `repair_worktrees()` and relative paths.
> - Move `strbuf_reset` call in `infer_backlink()`.
> - Cleaned up tests.
> - Slight stylistic changes.
> - Tweaked commit messages.
> - Updated base to 090d24e9af.
> - Link to v4: https://lore.kernel.org/r/20241031-wt_relative_options-v4-0-07a3dc0f02a3@pm.me
> Changes in v4:
> - Fixed failing test in ci
> - Link to v3: https://lore.kernel.org/r/20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me
> Changes in v3:
> - Split patches into smaller edits.
> - Moved tests into the patches with the relevant code changes.
> - Removed global `use_relative_paths` and instead pass parameter to functions.
> - Changed `infer_backlink` return type from `int` to `ssize_t`.
> - Updated `worktree.useRelativePaths` and `--relative-paths` descriptions.
> - Reordered patches
> - Link to v2: https://lore.kernel.org/r/20241028-wt_relative_options-v2-0-33a5021bd7bb@pm.me
> Changes in v2:
> - Fixed a bug where repositories with valid extensions would be downgraded
> to v0 during reinitialization, causing future operations to fail.
> - Split patch [1/2] into three separate patches.
> - Updated cover letter and commit messages.
> - Updated documentation wording.
> - Link to v1: https://lore.kernel.org/r/20241025-wt_relative_options-v1-0-c3005df76bf9@pm.me
>
> ---
> Caleb White (8):
> setup: correctly reinitialize repository version
> worktree: add `relativeWorktrees` extension
> worktree: refactor infer_backlink return
> worktree: add `write_worktree_linking_files()` function
> worktree: add relative cli/config options to `add` command
> worktree: add relative cli/config options to `move` command
> worktree: add relative cli/config options to `repair` command
> worktree: refactor `repair_worktree_after_gitdir_move()`
>
> Documentation/config/extensions.txt | 6 ++
> Documentation/config/worktree.txt | 10 +++
> Documentation/git-worktree.txt | 8 ++
> builtin/worktree.c | 29 ++++---
> repository.c | 1 +
> repository.h | 1 +
> setup.c | 39 ++++++---
> setup.h | 1 +
> t/t0001-init.sh | 22 ++++-
> t/t2400-worktree-add.sh | 46 +++++++++++
> t/t2401-worktree-prune.sh | 3 +-
> t/t2402-worktree-list.sh | 22 +++++
> t/t2403-worktree-move.sh | 25 ++++++
> t/t2406-worktree-repair.sh | 39 +++++++++
> t/t2408-worktree-relative.sh | 39 ---------
> t/t5504-fetch-receive-strict.sh | 6 +-
> worktree.c | 157 ++++++++++++++++++++----------------
> worktree.h | 22 ++++-
> 18 files changed, 334 insertions(+), 142 deletions(-)
> ---
> base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559
> change-id: 20241025-wt_relative_options-afa41987bc32
>
> Best regards,
^ permalink raw reply [flat|nested] 60+ messages in thread* Re: [PATCH v6 0/8] Allow relative worktree linking to be configured by the user
2024-12-02 14:57 ` [PATCH v6 0/8] Allow relative worktree linking to be configured by the user Phillip Wood
@ 2024-12-03 4:54 ` Junio C Hamano
2024-12-03 5:21 ` Caleb White
0 siblings, 1 reply; 60+ messages in thread
From: Junio C Hamano @ 2024-12-03 4:54 UTC (permalink / raw)
To: Phillip Wood; +Cc: Caleb White, git, Taylor Blau, Eric Sunshine
Phillip Wood <phillip.wood123@gmail.com> writes:
> Hi Caleb
>
> On 29/11/2024 22:22, Caleb White wrote:
>> Changes in v6:
>> - Re-add test for extensions.relativeworktrees config setting
>> - Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me
>
> The range-diff below looks good, thanks for working on this
> I think it's ready for next
>
> Best Wishes
>
> Phillip
Thanks, both.
Let me mark the topic for 'next'.
^ permalink raw reply [flat|nested] 60+ messages in thread
* Re: [PATCH v6 0/8] Allow relative worktree linking to be configured by the user
2024-12-03 4:54 ` Junio C Hamano
@ 2024-12-03 5:21 ` Caleb White
0 siblings, 0 replies; 60+ messages in thread
From: Caleb White @ 2024-12-03 5:21 UTC (permalink / raw)
To: Junio C Hamano, Phillip Wood; +Cc: git, Taylor Blau, Eric Sunshine
On Mon Dec 2, 2024 at 10:54 PM CST, Junio C Hamano wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
>> Hi Caleb
>>
>> On 29/11/2024 22:22, Caleb White wrote:
>>> Changes in v6:
>>> - Re-add test for extensions.relativeworktrees config setting
>>> - Link to v5: https://lore.kernel.org/r/20241125-wt_relative_options-v5-0-356d122ff3db@pm.me
>>
>> The range-diff below looks good, thanks for working on this
>> I think it's ready for next
>>
>> Best Wishes
>>
>> Phillip
>
> Thanks, both.
>
> Let me mark the topic for 'next'.
Awesome, thanks!
^ permalink raw reply [flat|nested] 60+ messages in thread