* [PATCH v2 0/5] Allow relative worktree linking to be configured by the user
@ 2024-10-28 19:09 Caleb White
2024-10-28 19:09 ` [PATCH v2 1/5] worktree: add CLI/config options for relative path linking Caleb White
` (5 more replies)
0 siblings, 6 replies; 26+ messages in thread
From: Caleb White @ 2024-10-28 19:09 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine, Caleb White
This patch 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 obtained by applying the following
patch onto 6a11438f43:
- cw/config-extensions topic (doc: consolidate extensions in git-config
documentation, 2024-10-22, <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>)
Link to original patch series:
https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@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 (5):
worktree: add CLI/config options for relative path linking
worktree: add `write_worktree_linking_files` function
worktree: add tests for worktrees with relative paths
setup: correctly reinitialize repository version
worktree: add `relativeWorktrees` extension
Documentation/config/extensions.txt | 6 ++
Documentation/config/worktree.txt | 5 ++
Documentation/git-worktree.txt | 12 +++
builtin/worktree.c | 20 ++---
repository.c | 1 +
repository.h | 1 +
setup.c | 39 +++++++---
setup.h | 1 +
t/t0001-init.sh | 17 ++++-
t/t2400-worktree-add.sh | 54 ++++++++++++++
t/t2401-worktree-prune.sh | 3 +-
t/t2402-worktree-list.sh | 22 ++++++
t/t2403-worktree-move.sh | 22 ++++++
t/t2406-worktree-repair.sh | 26 +++++++
t/t2408-worktree-relative.sh | 39 ----------
worktree.c | 143 ++++++++++++++++++++----------------
worktree.h | 14 ++++
17 files changed, 298 insertions(+), 127 deletions(-)
---
base-commit: 6a11438f43469f3815f2f0fc997bd45792ff04c0
change-id: 20241025-wt_relative_options-afa41987bc32
prerequisite-change-id: 20241020-cleanup-extension-docs-f365868711bf:v1
prerequisite-patch-id: 60a443b24e92938b9b6f4a016a7bab87e13bf3ea
Best regards,
--
Caleb White <cdwhite3@pm.me>
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
@ 2024-10-28 19:09 ` Caleb White
2024-10-29 14:52 ` Phillip Wood
2024-10-29 18:42 ` Taylor Blau
2024-10-28 19:09 ` [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function Caleb White
` (4 subsequent siblings)
5 siblings, 2 replies; 26+ messages in thread
From: Caleb White @ 2024-10-28 19:09 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine, Caleb White
This patch 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.
In response to reviewer feedback from the initial patch series[1],
this revision includes slight refactoring for improved
maintainability and clarity in the code base.
[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me
Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
Documentation/config/worktree.txt | 5 +++++
Documentation/git-worktree.txt | 12 ++++++++++++
builtin/worktree.c | 9 +++++++++
t/t2408-worktree-relative.sh | 39 ---------------------------------------
worktree.c | 17 ++++++++++-------
worktree.h | 2 ++
6 files changed, 38 insertions(+), 46 deletions(-)
diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
index 048e349482df6c892055720eb53cdcd6c327b6ed..44b783c2774dc5ff65e3fa232b0c25cd5254876b 100644
--- a/Documentation/config/worktree.txt
+++ b/Documentation/config/worktree.txt
@@ -7,3 +7,8 @@ worktree.guessRemote::
such a branch exists, it is checked out and set as "upstream"
for the new branch. If no such match can be found, it falls
back to creating a new branch from the current HEAD.
+worktree.useRelativePaths::
+ If set to `true`, worktrees will be linked to the repository using
+ relative paths rather than using absolute paths. This is particularly
+ useful for setups where the repository and worktrees may be moved between
+ different locations or environments.
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 70437c815f13852bd2eb862176b8b933e6de0acf..975dc3c46d480480457ec4857988a6b8bc67b647 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -216,6 +216,18 @@ 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::
+ Worktrees will be linked to the repository using relative paths
+ rather than using absolute paths. This is particularly useful for setups
+ where the repository and worktrees may be moved between different
+ locations or environments.
++
+With `repair`, the linking files will be updated if there's an absolute/relative
+mismatch, even if the links are correct.
++
+This can also be set up as the default behaviour by using the
+`worktree.useRelativePaths` config option.
+
--[no-]track::
When creating a new branch, if `<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..c1130be5890c905c0b648782a834eb8dfcd79ba5 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value,
if (!strcmp(var, "worktree.guessremote")) {
guess_remote = git_config_bool(var, value);
return 0;
+ } else if (!strcmp(var, "worktree.userelativepaths")) {
+ use_relative_paths = git_config_bool(var, value);
+ return 0;
}
return git_default_config(var, value, ctx, cb);
@@ -796,6 +799,8 @@ static int add(int ac, const char **av, const char *prefix)
PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
OPT_BOOL(0, "guess-remote", &guess_remote,
N_("try to match the new branch name with a remote-tracking branch")),
+ OPT_BOOL(0, "relative-paths", &use_relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
int ret;
@@ -1189,6 +1194,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;
@@ -1382,6 +1389,8 @@ static int repair(int ac, const char **av, const char *prefix)
const char **p;
const char *self[] = { ".", NULL };
struct option options[] = {
+ OPT_BOOL(0, "relative-paths", &use_relative_paths,
+ N_("use relative paths for worktrees")),
OPT_END()
};
int rc = 0;
diff --git a/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
diff --git a/worktree.c b/worktree.c
index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..de5c5e53a5f2a758ddf470b5d6a9ad6c66247181 100644
--- a/worktree.c
+++ b/worktree.c
@@ -14,6 +14,8 @@
#include "wt-status.h"
#include "config.h"
+int use_relative_paths = 0;
+
void free_worktree(struct worktree *worktree)
{
if (!worktree)
@@ -111,9 +113,9 @@ struct worktree *get_linked_worktree(const char *id,
strbuf_strip_suffix(&worktree_path, "/.git");
if (!is_absolute_path(worktree_path.buf)) {
- strbuf_strip_suffix(&path, "gitdir");
- strbuf_addbuf(&path, &worktree_path);
- strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
+ strbuf_strip_suffix(&path, "gitdir");
+ strbuf_addbuf(&path, &worktree_path);
+ strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
}
CALLOC_ARRAY(worktree, 1);
@@ -725,12 +727,15 @@ 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)
{
struct strbuf actual = STRBUF_INIT;
const char *id;
+ strbuf_reset(inferred);
if (strbuf_read_file(&actual, gitfile, 0) < 0)
goto error;
if (!starts_with(actual.buf, "gitdir:"))
@@ -741,18 +746,16 @@ 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);
- return 1;
-
+ return inferred->len;
error:
strbuf_release(&actual);
strbuf_reset(inferred); /* clear invalid path */
- return 0;
+ return -1;
}
/*
diff --git a/worktree.h b/worktree.h
index e96118621638667d87c5d7e0452ed10bd1ddf606..37e65d508ed23d3e7a29850bb938285072a3aaa6 100644
--- a/worktree.h
+++ b/worktree.h
@@ -5,6 +5,8 @@
struct strbuf;
+extern int use_relative_paths;
+
struct worktree {
/* The repository this worktree belongs to. */
struct repository *repo;
--
2.47.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
2024-10-28 19:09 ` [PATCH v2 1/5] worktree: add CLI/config options for relative path linking Caleb White
@ 2024-10-28 19:09 ` Caleb White
2024-10-29 14:52 ` Phillip Wood
2024-10-28 19:09 ` [PATCH v2 3/5] worktree: add tests for worktrees with relative paths Caleb White
` (3 subsequent siblings)
5 siblings, 1 reply; 26+ messages in thread
From: Caleb White @ 2024-10-28 19:09 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, 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.
This also teachs `git worktree repair` to fix the linking files if
there is an absolute/relative paths but the links are correct. E.g.,
`git worktree repair` can be used to convert a valid worktree between
absolute and relative paths.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
builtin/worktree.c | 11 +----
worktree.c | 118 +++++++++++++++++++++++++++--------------------------
worktree.h | 12 ++++++
3 files changed, 74 insertions(+), 67 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index c1130be5890c905c0b648782a834eb8dfcd79ba5..bb06830d6fe82aa97833c6e87f034115dfaa23bd 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -417,8 +417,7 @@ static int add_worktree(const char *path, const char *refname,
const struct add_opts *opts)
{
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
- struct strbuf sb = STRBUF_INIT, 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;
@@ -494,10 +493,7 @@ static int add_worktree(const char *path, const char *refname,
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
- strbuf_realpath(&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);
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
write_file(sb.buf, "../..");
@@ -581,12 +577,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;
diff --git a/worktree.c b/worktree.c
index de5c5e53a5f2a758ddf470b5d6a9ad6c66247181..f4cee73d7a1edecafdff30b6d5e2d9dd1365b93e 100644
--- a/worktree.c
+++ b/worktree.c
@@ -381,29 +381,24 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
void update_worktree_location(struct worktree *wt, const char *path_)
{
struct strbuf path = STRBUF_INIT;
- struct strbuf 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);
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,
@@ -582,9 +577,9 @@ static void repair_gitfile(struct worktree *wt,
worktree_repair_fn fn, void *cb_data)
{
struct strbuf dotgit = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
struct strbuf repo = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -600,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) {
@@ -617,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);
}
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,
@@ -653,45 +651,30 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)
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;
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);
+ if (!is_absolute_path(dotgit.buf)) {
+ strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id);
strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
}
if (!file_exists(dotgit.buf))
goto done;
- 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);
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)
@@ -766,13 +749,10 @@ void repair_worktree_at_path(const char *path,
worktree_repair_fn fn, void *cb_data)
{
struct strbuf dotgit = STRBUF_INIT;
- struct strbuf realdotgit = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
struct strbuf inferred_backlink = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
struct strbuf olddotgit = STRBUF_INIT;
- struct strbuf realolddotgit = STRBUF_INIT;
- struct strbuf tmp = STRBUF_INIT;
char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -784,25 +764,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) {
@@ -815,11 +795,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;
}
@@ -841,39 +821,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);
}
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)
@@ -1034,3 +1010,29 @@ int init_worktree_config(struct repository *r)
free(main_worktree_file);
return res;
}
+
+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir)
+{
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf repo = STRBUF_INIT;
+ struct strbuf tmp = STRBUF_INIT;
+
+ strbuf_addbuf(&path, &dotgit);
+ strbuf_strip_suffix(&path, "/.git");
+ strbuf_realpath(&path, path.buf, 1);
+ strbuf_addbuf(&repo, &gitdir);
+ strbuf_strip_suffix(&repo, "/gitdir");
+ strbuf_realpath(&repo, repo.buf, 1);
+
+ if (use_relative_paths) {
+ write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
+ write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
+ } else {
+ write_file(gitdir.buf, "%s/.git", path.buf);
+ write_file(dotgit.buf, "gitdir: %s", repo.buf);
+ }
+
+ strbuf_release(&path);
+ strbuf_release(&repo);
+ strbuf_release(&tmp);
+}
diff --git a/worktree.h b/worktree.h
index 37e65d508ed23d3e7a29850bb938285072a3aaa6..5929089891c97318a8f5329f7938264c717050d5 100644
--- a/worktree.h
+++ b/worktree.h
@@ -217,4 +217,16 @@ void strbuf_worktree_ref(const struct worktree *wt,
*/
int init_worktree_config(struct repository *r);
+/**
+ * Write the .git file and gitdir file that links the worktree to the repository.
+ *
+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
+ * is the path to the repository's `gitdir` file.
+ *
+ * Example
+ * dotgit: "/path/to/foo/.git"
+ * gitdir: "/path/to/repo/worktrees/foo/gitdir"
+ */
+void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir);
+
#endif
--
2.47.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
2024-10-28 19:09 ` [PATCH v2 1/5] worktree: add CLI/config options for relative path linking Caleb White
2024-10-28 19:09 ` [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function Caleb White
@ 2024-10-28 19:09 ` Caleb White
2024-10-29 14:52 ` Phillip Wood
2024-10-29 23:00 ` Taylor Blau
2024-10-28 19:10 ` [PATCH v2 4/5] setup: correctly reinitialize repository version Caleb White
` (2 subsequent siblings)
5 siblings, 2 replies; 26+ messages in thread
From: Caleb White @ 2024-10-28 19:09 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine, Caleb White
This patch expands the test coverage by adding cases that specifically
handle relative paths. These tests verify correct behavior in a variety
of operations, including: adding, listing, pruning, moving, and
repairing worktrees with relative paths configured.
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 | 17 +++++++++++++----
t/t2400-worktree-add.sh | 41 +++++++++++++++++++++++++++++++++++++++++
t/t2401-worktree-prune.sh | 3 ++-
t/t2402-worktree-list.sh | 22 ++++++++++++++++++++++
t/t2403-worktree-move.sh | 22 ++++++++++++++++++++++
t/t2406-worktree-repair.sh | 26 ++++++++++++++++++++++++++
6 files changed, 126 insertions(+), 5 deletions(-)
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e21b9aa5e7f4599af8b20165b50896c9a49ba7ea 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -435,6 +435,7 @@ sep_git_dir_worktree () {
test_when_finished "rm -rf mainwt linkwt seprepo" &&
git init mainwt &&
test_commit -C mainwt gumby &&
+ git -C mainwt config worktree.useRelativePaths "$([ "$2" = "relative" ] && echo true || echo false)" &&
git -C mainwt worktree add --detach ../linkwt &&
git -C "$1" init --separate-git-dir ../seprepo &&
git -C mainwt rev-parse --git-common-dir >expect &&
@@ -442,12 +443,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' '
- sep_git_dir_worktree linkwt
+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 (relative)' '
+ sep_git_dir_worktree linkwt relative
'
test_expect_success MINGW '.git hidden' '
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index cfc4aeb1798c6d023909cec771e5b74e983af5ea..630c13230b3cc762ce8d943e22be8891aa2b1871 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -1207,4 +1207,45 @@ test_expect_success '"add" with initialized submodule, with submodule.recurse se
git -C project-clone -c submodule.recurse worktree add ../project-5
'
+test_expect_success 'can create worktrees with relative paths' '
+ test_when_finished "git worktree remove relative" &&
+ git config worktree.useRelativePaths false &&
+ git worktree add --relative-paths ./relative &&
+ cat relative/.git >actual &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
+ test_cmp expect actual &&
+ cat .git/worktrees/relative/gitdir >actual &&
+ echo "../../../relative/.git" >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'can create worktrees with absolute paths' '
+ git config worktree.useRelativePaths true &&
+ git worktree add ./relative &&
+ cat relative/.git >actual &&
+ echo "gitdir: ../.git/worktrees/relative" >expect &&
+ test_cmp expect actual &&
+ git worktree add --no-relative-paths ./absolute &&
+ cat absolute/.git >actual &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'move repo without breaking relative internal links' '
+ test_when_finished rm -rf repo moved &&
+ git init repo &&
+ (
+ cd repo &&
+ git config worktree.useRelativePaths true &&
+ test_commit initial &&
+ git worktree add wt1 &&
+ cd .. &&
+ mv repo moved &&
+ cd moved/wt1 &&
+ git status >out 2>err &&
+ test_must_be_empty err
+ )
+'
+
test_done
diff --git a/t/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/t2403-worktree-move.sh b/t/t2403-worktree-move.sh
index 901342ea09b51a8e832f1109fbb737df84283aa2..6ce9ed3f1e6b3f73d2a290e770233eec30221fe5 100755
--- a/t/t2403-worktree-move.sh
+++ b/t/t2403-worktree-move.sh
@@ -247,4 +247,26 @@ test_expect_success 'not remove a repo with initialized submodule' '
)
'
+test_expect_success 'move worktree with absolute path to relative path' '
+ git config worktree.useRelativePaths false &&
+ git worktree add ./absolute &&
+ git worktree move --relative-paths absolute relative &&
+ cat relative/.git >actual &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
+ test_cmp expect actual &&
+ git config worktree.useRelativePaths true &&
+ git worktree move relative relative2 &&
+ cat relative2/.git >actual &&
+ echo "gitdir: ../.git/worktrees/absolute" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'move worktree with relative path to absolute path' '
+ git config worktree.useRelativePaths true &&
+ git worktree move --no-relative-paths relative2 absolute &&
+ cat absolute/.git >actual &&
+ echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+ test_cmp expect actual
+'
+
test_done
diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
index 7686e60f6ad186519b275f11a5e14064c905b207..84451e903b2ef3c645c0311faf055c846588baf6 100755
--- a/t/t2406-worktree-repair.sh
+++ b/t/t2406-worktree-repair.sh
@@ -216,4 +216,30 @@ test_expect_success 'repair copied main and linked worktrees' '
test_cmp dup/linked.expect dup/linked/.git
'
+test_expect_success 'repair absolute worktree to use relative paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../side &&
+ echo "../../../../sidemoved/.git" >expect-gitdir &&
+ echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
+ mv side sidemoved &&
+ git -C main worktree repair --relative-paths ../sidemoved &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
+test_expect_success 'repair relative worktree to use absolute paths' '
+ test_when_finished "rm -rf main side sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --relative-paths --detach ../side &&
+ echo "$(pwd)/sidemoved/.git" >expect-gitdir &&
+ echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
+ mv side sidemoved &&
+ git -C main worktree repair ../sidemoved &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
test_done
--
2.47.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v2 4/5] setup: correctly reinitialize repository version
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
` (2 preceding siblings ...)
2024-10-28 19:09 ` [PATCH v2 3/5] worktree: add tests for worktrees with relative paths Caleb White
@ 2024-10-28 19:10 ` Caleb White
2024-10-28 19:10 ` [PATCH v2 5/5] worktree: add `relativeWorktrees` extension Caleb White
2024-10-29 14:55 ` [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Phillip Wood
5 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-28 19:10 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, 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 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/setup.c b/setup.c
index 7b648de0279116b381eea46800ad130606926103..1e5c2eacb19eb6b230d7c9954f66fc7ae0b05631 100644
--- a/setup.c
+++ b/setup.c
@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,
enum ref_storage_format ref_storage_format,
int reinit)
{
- char repo_version_string[10];
- int repo_version = GIT_REPO_VERSION;
+ struct strbuf repo_version = STRBUF_INIT;
+ int target_version = GIT_REPO_VERSION;
/*
* Note that we initialize the repository version to 1 when the ref
@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,
*/
if (hash_algo != GIT_HASH_SHA1 ||
ref_storage_format != REF_STORAGE_FORMAT_FILES)
- repo_version = GIT_REPO_VERSION_READ;
-
- /* This forces creation of new config file */
- xsnprintf(repo_version_string, sizeof(repo_version_string),
- "%d", repo_version);
- git_config_set("core.repositoryformatversion", repo_version_string);
+ target_version = GIT_REPO_VERSION_READ;
if (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)
git_config_set("extensions.objectformat",
@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,
ref_storage_format_to_name(ref_storage_format));
else if (reinit)
git_config_set_gently("extensions.refstorage", NULL);
+
+ if (reinit) {
+ struct strbuf config = STRBUF_INIT;
+ struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
+
+ strbuf_git_common_path(&config, the_repository, "config");
+ read_repository_format(&repo_fmt, config.buf);
+
+ if (repo_fmt.v1_only_extensions.nr)
+ target_version = GIT_REPO_VERSION_READ;
+
+ strbuf_release(&config);
+ clear_repository_format(&repo_fmt);
+ }
+
+ strbuf_addf(&repo_version, "%d", target_version);
+ git_config_set("core.repositoryformatversion", repo_version.buf);
+
+ strbuf_release(&repo_version);
}
static int is_reinit(void)
@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,
adjust_shared_perm(repo_get_git_dir(the_repository));
}
- initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);
+ initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);
/* Check filemode trustability */
path = git_path_buf(&buf, "config");
--
2.47.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v2 5/5] worktree: add `relativeWorktrees` extension
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
` (3 preceding siblings ...)
2024-10-28 19:10 ` [PATCH v2 4/5] setup: correctly reinitialize repository version Caleb White
@ 2024-10-28 19:10 ` Caleb White
2024-10-29 14:55 ` [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Phillip Wood
5 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-28 19:10 UTC (permalink / raw)
To: git; +Cc: Taylor Blau, 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
extension is automatically set when a worktree is created or repaired
using the `--relative-paths` option, or when the
`worktree.useRelativePaths` config is set to `true`.
The `relativeWorktrees` extension ensures older Git versions do not
attempt to automatically prune worktrees with relative paths, as they
would not not recognize the paths as being valid.
Suggested-by: Junio C Hamano <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 +
t/t2400-worktree-add.sh | 13 +++++++++++++
worktree.c | 8 ++++++++
7 files changed, 37 insertions(+)
diff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt
index 5dc569d1c9c77c15e32441493289f9c9dd5e7f0b..5cb4721a0e0ae1ed64f90492c0dc18b96473cb33 100644
--- a/Documentation/config/extensions.txt
+++ b/Documentation/config/extensions.txt
@@ -63,6 +63,12 @@ Note that this setting should only be set by linkgit:git-init[1] or
linkgit:git-clone[1]. Trying to change it after initialization will not
work and will produce hard-to-diagnose issues.
+relativeWorktrees::
+ If enabled, indicates at least one worktree has been linked with
+ relative paths. Automatically set if a worktree has been created or
+ repaired with either the `--relative-paths` option or with the
+ `worktree.useRelativePaths` config set to `true`.
+
worktreeConfig::
If enabled, then worktrees will load config settings from the
`$GIT_DIR/config.worktree` file in addition to the
diff --git a/repository.c b/repository.c
index f988b8ae68a6a29792e7f2c980a02bd0e388a3b9..1a6a62bbd03a5dc4fdade3eb45ea2696968abc23 100644
--- a/repository.c
+++ b/repository.c
@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,
repo_set_compat_hash_algo(repo, format.compat_hash_algo);
repo_set_ref_storage_format(repo, format.ref_storage_format);
repo->repository_format_worktree_config = format.worktree_config;
+ repo->repository_format_relative_worktrees = format.relative_worktrees;
/* take ownership of format.partial_clone */
repo->repository_format_partial_clone = format.partial_clone;
diff --git a/repository.h b/repository.h
index 24a66a496a6ff516ce06d47b7329b3d36eb701ca..c4c92b2ab9c9e3b425dc2974636e33d1f4089c69 100644
--- a/repository.h
+++ b/repository.h
@@ -150,6 +150,7 @@ struct repository {
/* Configurations */
int repository_format_worktree_config;
+ int repository_format_relative_worktrees;
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
unsigned different_commondir:1;
diff --git a/setup.c b/setup.c
index 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;
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index 630c13230b3cc762ce8d943e22be8891aa2b1871..d36d8a4db0e924877787697579544f10f92dc0cf 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -1248,4 +1248,17 @@ test_expect_success 'move repo without breaking relative internal links' '
)
'
+test_expect_success 'relative worktree sets extension config' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ git -C repo commit --allow-empty -m base &&
+ git -C repo worktree add --relative-paths ./foo &&
+ git -C repo config get core.repositoryformatversion >actual &&
+ echo 1 >expected &&
+ test_cmp expected actual &&
+ git -C repo config get extensions.relativeworktrees >actual &&
+ echo true >expected &&
+ test_cmp expected actual
+'
+
test_done
diff --git a/worktree.c b/worktree.c
index f4cee73d7a1edecafdff30b6d5e2d9dd1365b93e..0ae4a5f796f10c017ef90413e969e1622b54a897 100644
--- a/worktree.c
+++ b/worktree.c
@@ -1024,6 +1024,14 @@ void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir)
strbuf_strip_suffix(&repo, "/gitdir");
strbuf_realpath(&repo, repo.buf, 1);
+ if (use_relative_paths && !the_repository->repository_format_relative_worktrees) {
+ if (upgrade_repository_format(1) < 0)
+ die(_("unable to upgrade repository format to support relative worktrees"));
+ if (git_config_set_gently("extensions.relativeWorktrees", "true"))
+ die(_("unable to set extensions.relativeWorktrees setting"));
+ the_repository->repository_format_relative_worktrees = 1;
+ }
+
if (use_relative_paths) {
write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
--
2.47.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-28 19:09 ` [PATCH v2 1/5] worktree: add CLI/config options for relative path linking Caleb White
@ 2024-10-29 14:52 ` Phillip Wood
2024-10-30 5:27 ` Caleb White
2024-10-29 18:42 ` Taylor Blau
1 sibling, 1 reply; 26+ messages in thread
From: Phillip Wood @ 2024-10-29 14:52 UTC (permalink / raw)
To: Caleb White, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
Hi Caleb
Thanks for working on this.
On 28/10/2024 19:09, Caleb White wrote:
> This patch introduces the `--[no-]relative-paths` CLI option for
"This patch" is a bit redundant, I'd say "Introduce a
`--[no-]-relative-paths ..`
> `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.
This sounds good, I'm not sure the patch actually implements anything
other than the option parsing though. I think it would make sense to add
these options at the end of the patch series once the implementation has
been changed to support them. I'd start with patches 4 and 5 to add the
new extension setting first, then refactor worktree.c to handle creating
worktrees with relative or absolute paths and set the extension if
appropriate, then add the --relative-path option to "git worktree"
> 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.
>
> In response to reviewer feedback from the initial patch series[1],
> this revision includes slight refactoring for improved
> maintainability and clarity in the code base.
Please don't mix cleanups with other code changes as it makes it hard to
check that the cleanups don't change the behavior.
> [1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me
>
> Suggested-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Caleb White <cdwhite3@pm.me>
> ---
> Documentation/config/worktree.txt | 5 +++++
> Documentation/git-worktree.txt | 12 ++++++++++++
> builtin/worktree.c | 9 +++++++++
> t/t2408-worktree-relative.sh | 39 ---------------------------------------
> worktree.c | 17 ++++++++++-------
> worktree.h | 2 ++
> 6 files changed, 38 insertions(+), 46 deletions(-)
>
> diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
> index 048e349482df6c892055720eb53cdcd6c327b6ed..44b783c2774dc5ff65e3fa232b0c25cd5254876b 100644
> --- a/Documentation/config/worktree.txt
> +++ b/Documentation/config/worktree.txt
> @@ -7,3 +7,8 @@ worktree.guessRemote::
> such a branch exists, it is checked out and set as "upstream"
> for the new branch. If no such match can be found, it falls
> back to creating a new branch from the current HEAD.
> +worktree.useRelativePaths::
> + If set to `true`, worktrees will be linked to the repository using
> + relative paths rather than using absolute paths. This is particularly
> + useful for setups where the repository and worktrees may be moved between
> + different locations or environments.
I think it would be helpful to spell out the implications of this to the
user - namely that you cannot use older versions of git on a repository
with worktrees using relative paths and it may break third-party
software as well.
> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> index 70437c815f13852bd2eb862176b8b933e6de0acf..975dc3c46d480480457ec4857988a6b8bc67b647 100644
> --- a/Documentation/git-worktree.txt
> +++ b/Documentation/git-worktree.txt
> @@ -216,6 +216,18 @@ 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::
> + Worktrees will be linked to the repository using relative paths
> + rather than using absolute paths. This is particularly useful for setups
> + where the repository and worktrees may be moved between different
> + locations or environments.
Again we should spell out the implications of using relative paths.
> +With `repair`, the linking files will be updated if there's an absolute/relative
> +mismatch, even if the links are correct.
> ++
> +This can also be set up as the default behaviour by using the
> +`worktree.useRelativePaths` config option.
> +
> --[no-]track::
> When creating a new branch, if `<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..c1130be5890c905c0b648782a834eb8dfcd79ba5 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value,
> if (!strcmp(var, "worktree.guessremote")) {
> guess_remote = git_config_bool(var, value);
> return 0;
> + } else if (!strcmp(var, "worktree.userelativepaths")) {
> + use_relative_paths = git_config_bool(var, value);
As we're trying to remove global variables from libgit.a as part of the
libification effort I'd be much happier if "use_relative_paths" was
declared as a "static int" in this file and then passed down to the
functions that need it rather than declaring it as a global in "worktree.c".
> diff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh
> deleted file mode 100755
There's no explanation for this change in the commit message
Best Wishes
Phillip
> 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
> diff --git a/worktree.c b/worktree.c
> index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..de5c5e53a5f2a758ddf470b5d6a9ad6c66247181 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -14,6 +14,8 @@
> #include "wt-status.h"
> #include "config.h"
>
> +int use_relative_paths = 0;
> +
> void free_worktree(struct worktree *worktree)
> {
> if (!worktree)
> @@ -111,9 +113,9 @@ struct worktree *get_linked_worktree(const char *id,
> strbuf_strip_suffix(&worktree_path, "/.git");
>
> if (!is_absolute_path(worktree_path.buf)) {
> - strbuf_strip_suffix(&path, "gitdir");
> - strbuf_addbuf(&path, &worktree_path);
> - strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
> + strbuf_strip_suffix(&path, "gitdir");
> + strbuf_addbuf(&path, &worktree_path);
> + strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
> }
>
> CALLOC_ARRAY(worktree, 1);
> @@ -725,12 +727,15 @@ 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)
> {
> struct strbuf actual = STRBUF_INIT;
> const char *id;
>
> + strbuf_reset(inferred);
> if (strbuf_read_file(&actual, gitfile, 0) < 0)
> goto error;
> if (!starts_with(actual.buf, "gitdir:"))
> @@ -741,18 +746,16 @@ 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);
> - return 1;
> -
> + return inferred->len;
> error:
> strbuf_release(&actual);
> strbuf_reset(inferred); /* clear invalid path */
> - return 0;
> + return -1;
> }
>
> /*
> diff --git a/worktree.h b/worktree.h
> index e96118621638667d87c5d7e0452ed10bd1ddf606..37e65d508ed23d3e7a29850bb938285072a3aaa6 100644
> --- a/worktree.h
> +++ b/worktree.h
> @@ -5,6 +5,8 @@
>
> struct strbuf;
>
> +extern int use_relative_paths;
> +
> struct worktree {
> /* The repository this worktree belongs to. */
> struct repository *repo;
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function
2024-10-28 19:09 ` [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function Caleb White
@ 2024-10-29 14:52 ` Phillip Wood
2024-10-29 22:55 ` Taylor Blau
2024-10-30 5:30 ` Caleb White
0 siblings, 2 replies; 26+ messages in thread
From: Phillip Wood @ 2024-10-29 14:52 UTC (permalink / raw)
To: Caleb White, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
Hi Caleb
On 28/10/2024 19:09, Caleb White wrote:
> 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.
That sounds like a useful change. I think it would be better to pass an
extra parameter "use_relative_paths" rather than relying on a global
varibale in worktree.c. Thank you for adding some documentaion for the
new function.
> This also teachs `git worktree repair` to fix the linking files if
> there is an absolute/relative paths but the links are correct. E.g.,
> `git worktree repair` can be used to convert a valid worktree between
> absolute and relative paths.
This might be better as a separate step so that reviewers can
concentrate on the correctness of write_werktree_linking_files() when
reviewing this patch.
Best Wishes
Phillip
> Signed-off-by: Caleb White <cdwhite3@pm.me>
> ---
> builtin/worktree.c | 11 +----
> worktree.c | 118 +++++++++++++++++++++++++++--------------------------
> worktree.h | 12 ++++++
> 3 files changed, 74 insertions(+), 67 deletions(-)
>
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index c1130be5890c905c0b648782a834eb8dfcd79ba5..bb06830d6fe82aa97833c6e87f034115dfaa23bd 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -417,8 +417,7 @@ static int add_worktree(const char *path, const char *refname,
> const struct add_opts *opts)
> {
> struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
> - struct strbuf sb = STRBUF_INIT, 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;
> @@ -494,10 +493,7 @@ static int add_worktree(const char *path, const char *refname,
>
> strbuf_reset(&sb);
> strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
> - strbuf_realpath(&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);
> strbuf_reset(&sb);
> strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
> write_file(sb.buf, "../..");
> @@ -581,12 +577,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;
> diff --git a/worktree.c b/worktree.c
> index de5c5e53a5f2a758ddf470b5d6a9ad6c66247181..f4cee73d7a1edecafdff30b6d5e2d9dd1365b93e 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -381,29 +381,24 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
> void update_worktree_location(struct worktree *wt, const char *path_)
> {
> struct strbuf path = STRBUF_INIT;
> - struct strbuf 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);
>
> 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,
> @@ -582,9 +577,9 @@ static void repair_gitfile(struct worktree *wt,
> worktree_repair_fn fn, void *cb_data)
> {
> struct strbuf dotgit = STRBUF_INIT;
> + struct strbuf gitdir = STRBUF_INIT;
> struct strbuf repo = STRBUF_INIT;
> struct strbuf backlink = STRBUF_INIT;
> - struct strbuf tmp = STRBUF_INIT;
> char *dotgit_contents = NULL;
> const char *repair = NULL;
> int err;
> @@ -600,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) {
> @@ -617,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);
> }
>
> 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,
> @@ -653,45 +651,30 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)
>
> 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;
>
> 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);
> + if (!is_absolute_path(dotgit.buf)) {
> + strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id);
> strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
> }
>
> if (!file_exists(dotgit.buf))
> goto done;
>
> - 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);
> 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)
> @@ -766,13 +749,10 @@ void repair_worktree_at_path(const char *path,
> worktree_repair_fn fn, void *cb_data)
> {
> struct strbuf dotgit = STRBUF_INIT;
> - struct strbuf realdotgit = STRBUF_INIT;
> struct strbuf backlink = STRBUF_INIT;
> struct strbuf inferred_backlink = STRBUF_INIT;
> struct strbuf gitdir = STRBUF_INIT;
> struct strbuf olddotgit = STRBUF_INIT;
> - struct strbuf realolddotgit = STRBUF_INIT;
> - struct strbuf tmp = STRBUF_INIT;
> char *dotgit_contents = NULL;
> const char *repair = NULL;
> int err;
> @@ -784,25 +764,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) {
> @@ -815,11 +795,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;
> }
>
> @@ -841,39 +821,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);
> }
> 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)
> @@ -1034,3 +1010,29 @@ int init_worktree_config(struct repository *r)
> free(main_worktree_file);
> return res;
> }
> +
> +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir)
> +{
> + struct strbuf path = STRBUF_INIT;
> + struct strbuf repo = STRBUF_INIT;
> + struct strbuf tmp = STRBUF_INIT;
> +
> + strbuf_addbuf(&path, &dotgit);
> + strbuf_strip_suffix(&path, "/.git");
> + strbuf_realpath(&path, path.buf, 1);
> + strbuf_addbuf(&repo, &gitdir);
> + strbuf_strip_suffix(&repo, "/gitdir");
> + strbuf_realpath(&repo, repo.buf, 1);
> +
> + if (use_relative_paths) {
> + write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
> + write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
> + } else {
> + write_file(gitdir.buf, "%s/.git", path.buf);
> + write_file(dotgit.buf, "gitdir: %s", repo.buf);
> + }
> +
> + strbuf_release(&path);
> + strbuf_release(&repo);
> + strbuf_release(&tmp);
> +}
> diff --git a/worktree.h b/worktree.h
> index 37e65d508ed23d3e7a29850bb938285072a3aaa6..5929089891c97318a8f5329f7938264c717050d5 100644
> --- a/worktree.h
> +++ b/worktree.h
> @@ -217,4 +217,16 @@ void strbuf_worktree_ref(const struct worktree *wt,
> */
> int init_worktree_config(struct repository *r);
>
> +/**
> + * Write the .git file and gitdir file that links the worktree to the repository.
> + *
> + * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
> + * is the path to the repository's `gitdir` file.
> + *
> + * Example
> + * dotgit: "/path/to/foo/.git"
> + * gitdir: "/path/to/repo/worktrees/foo/gitdir"
> + */
> +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir);
> +
> #endif
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-28 19:09 ` [PATCH v2 3/5] worktree: add tests for worktrees with relative paths Caleb White
@ 2024-10-29 14:52 ` Phillip Wood
2024-10-29 14:58 ` Caleb White
2024-10-29 23:00 ` Taylor Blau
1 sibling, 1 reply; 26+ messages in thread
From: Phillip Wood @ 2024-10-29 14:52 UTC (permalink / raw)
To: Caleb White, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
Hi Caleb
On 28/10/2024 19:09, Caleb White wrote:
> This patch expands the test coverage by adding cases that specifically
> handle relative paths. These tests verify correct behavior in a variety
> of operations, including: adding, listing, pruning, moving, and
> repairing worktrees with relative paths configured.
>
> This also adds a test case for reinitializing a repository that has
> relative worktrees.
It's nice to see new tests being added. If they were added with the code
changes they test that would help reader understand the changes being
made I think.
Best Wishes
Phillip
> Signed-off-by: Caleb White <cdwhite3@pm.me>
> ---
> t/t0001-init.sh | 17 +++++++++++++----
> t/t2400-worktree-add.sh | 41 +++++++++++++++++++++++++++++++++++++++++
> t/t2401-worktree-prune.sh | 3 ++-
> t/t2402-worktree-list.sh | 22 ++++++++++++++++++++++
> t/t2403-worktree-move.sh | 22 ++++++++++++++++++++++
> t/t2406-worktree-repair.sh | 26 ++++++++++++++++++++++++++
> 6 files changed, 126 insertions(+), 5 deletions(-)
>
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e21b9aa5e7f4599af8b20165b50896c9a49ba7ea 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -435,6 +435,7 @@ sep_git_dir_worktree () {
> test_when_finished "rm -rf mainwt linkwt seprepo" &&
> git init mainwt &&
> test_commit -C mainwt gumby &&
> + git -C mainwt config worktree.useRelativePaths "$([ "$2" = "relative" ] && echo true || echo false)" &&
> git -C mainwt worktree add --detach ../linkwt &&
> git -C "$1" init --separate-git-dir ../seprepo &&
> git -C mainwt rev-parse --git-common-dir >expect &&
> @@ -442,12 +443,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' '
> - sep_git_dir_worktree linkwt
> +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 (relative)' '
> + sep_git_dir_worktree linkwt relative
> '
>
> test_expect_success MINGW '.git hidden' '
> diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
> index cfc4aeb1798c6d023909cec771e5b74e983af5ea..630c13230b3cc762ce8d943e22be8891aa2b1871 100755
> --- a/t/t2400-worktree-add.sh
> +++ b/t/t2400-worktree-add.sh
> @@ -1207,4 +1207,45 @@ test_expect_success '"add" with initialized submodule, with submodule.recurse se
> git -C project-clone -c submodule.recurse worktree add ../project-5
> '
>
> +test_expect_success 'can create worktrees with relative paths' '
> + test_when_finished "git worktree remove relative" &&
> + git config worktree.useRelativePaths false &&
> + git worktree add --relative-paths ./relative &&
> + cat relative/.git >actual &&
> + echo "gitdir: ../.git/worktrees/relative" >expect &&
> + test_cmp expect actual &&
> + cat .git/worktrees/relative/gitdir >actual &&
> + echo "../../../relative/.git" >expect &&
> + test_cmp expect actual
> +
> +'
> +
> +test_expect_success 'can create worktrees with absolute paths' '
> + git config worktree.useRelativePaths true &&
> + git worktree add ./relative &&
> + cat relative/.git >actual &&
> + echo "gitdir: ../.git/worktrees/relative" >expect &&
> + test_cmp expect actual &&
> + git worktree add --no-relative-paths ./absolute &&
> + cat absolute/.git >actual &&
> + echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
> + test_cmp expect actual
> +'
> +
> +test_expect_success 'move repo without breaking relative internal links' '
> + test_when_finished rm -rf repo moved &&
> + git init repo &&
> + (
> + cd repo &&
> + git config worktree.useRelativePaths true &&
> + test_commit initial &&
> + git worktree add wt1 &&
> + cd .. &&
> + mv repo moved &&
> + cd moved/wt1 &&
> + git status >out 2>err &&
> + test_must_be_empty err
> + )
> +'
> +
> test_done
> diff --git a/t/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/t2403-worktree-move.sh b/t/t2403-worktree-move.sh
> index 901342ea09b51a8e832f1109fbb737df84283aa2..6ce9ed3f1e6b3f73d2a290e770233eec30221fe5 100755
> --- a/t/t2403-worktree-move.sh
> +++ b/t/t2403-worktree-move.sh
> @@ -247,4 +247,26 @@ test_expect_success 'not remove a repo with initialized submodule' '
> )
> '
>
> +test_expect_success 'move worktree with absolute path to relative path' '
> + git config worktree.useRelativePaths false &&
> + git worktree add ./absolute &&
> + git worktree move --relative-paths absolute relative &&
> + cat relative/.git >actual &&
> + echo "gitdir: ../.git/worktrees/absolute" >expect &&
> + test_cmp expect actual &&
> + git config worktree.useRelativePaths true &&
> + git worktree move relative relative2 &&
> + cat relative2/.git >actual &&
> + echo "gitdir: ../.git/worktrees/absolute" >expect &&
> + test_cmp expect actual
> +'
> +
> +test_expect_success 'move worktree with relative path to absolute path' '
> + git config worktree.useRelativePaths true &&
> + git worktree move --no-relative-paths relative2 absolute &&
> + cat absolute/.git >actual &&
> + echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
> + test_cmp expect actual
> +'
> +
> test_done
> diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
> index 7686e60f6ad186519b275f11a5e14064c905b207..84451e903b2ef3c645c0311faf055c846588baf6 100755
> --- a/t/t2406-worktree-repair.sh
> +++ b/t/t2406-worktree-repair.sh
> @@ -216,4 +216,30 @@ test_expect_success 'repair copied main and linked worktrees' '
> test_cmp dup/linked.expect dup/linked/.git
> '
>
> +test_expect_success 'repair absolute worktree to use relative paths' '
> + test_when_finished "rm -rf main side sidemoved" &&
> + test_create_repo main &&
> + test_commit -C main init &&
> + git -C main worktree add --detach ../side &&
> + echo "../../../../sidemoved/.git" >expect-gitdir &&
> + echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
> + mv side sidemoved &&
> + git -C main worktree repair --relative-paths ../sidemoved &&
> + test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
> + test_cmp expect-gitfile sidemoved/.git
> +'
> +
> +test_expect_success 'repair relative worktree to use absolute paths' '
> + test_when_finished "rm -rf main side sidemoved" &&
> + test_create_repo main &&
> + test_commit -C main init &&
> + git -C main worktree add --relative-paths --detach ../side &&
> + echo "$(pwd)/sidemoved/.git" >expect-gitdir &&
> + echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
> + mv side sidemoved &&
> + git -C main worktree repair ../sidemoved &&
> + test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
> + test_cmp expect-gitfile sidemoved/.git
> +'
> +
> test_done
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 0/5] Allow relative worktree linking to be configured by the user
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
` (4 preceding siblings ...)
2024-10-28 19:10 ` [PATCH v2 5/5] worktree: add `relativeWorktrees` extension Caleb White
@ 2024-10-29 14:55 ` Phillip Wood
2024-10-30 5:13 ` Caleb White
5 siblings, 1 reply; 26+ messages in thread
From: Phillip Wood @ 2024-10-29 14:55 UTC (permalink / raw)
To: Caleb White, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
Hi Caleb
Thanks for working on this. I've left so high-level comments on the
first three patches. As I said in my comments on patch 1 I think it
would be helpful to re-order the changes so that the extension is
implementation first and then adjust the implementation to support both
relative and absolute paths before adding "--no-relative-paths" to "git
worktree add/move/repair". I'd also like to avoid adding a new global
variable
Best Wishes
Phillip
On 28/10/2024 19:09, Caleb White wrote:
> This patch 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 obtained by applying the following
> patch onto 6a11438f43:
> - cw/config-extensions topic (doc: consolidate extensions in git-config
> documentation, 2024-10-22, <20241021-cleanup-extension-docs-v1-1-ab02cece3132@pm.me>)
>
> Link to original patch series:
> https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@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 (5):
> worktree: add CLI/config options for relative path linking
> worktree: add `write_worktree_linking_files` function
> worktree: add tests for worktrees with relative paths
> setup: correctly reinitialize repository version
> worktree: add `relativeWorktrees` extension
>
> Documentation/config/extensions.txt | 6 ++
> Documentation/config/worktree.txt | 5 ++
> Documentation/git-worktree.txt | 12 +++
> builtin/worktree.c | 20 ++---
> repository.c | 1 +
> repository.h | 1 +
> setup.c | 39 +++++++---
> setup.h | 1 +
> t/t0001-init.sh | 17 ++++-
> t/t2400-worktree-add.sh | 54 ++++++++++++++
> t/t2401-worktree-prune.sh | 3 +-
> t/t2402-worktree-list.sh | 22 ++++++
> t/t2403-worktree-move.sh | 22 ++++++
> t/t2406-worktree-repair.sh | 26 +++++++
> t/t2408-worktree-relative.sh | 39 ----------
> worktree.c | 143 ++++++++++++++++++++----------------
> worktree.h | 14 ++++
> 17 files changed, 298 insertions(+), 127 deletions(-)
> ---
> base-commit: 6a11438f43469f3815f2f0fc997bd45792ff04c0
> change-id: 20241025-wt_relative_options-afa41987bc32
> prerequisite-change-id: 20241020-cleanup-extension-docs-f365868711bf:v1
> prerequisite-patch-id: 60a443b24e92938b9b6f4a016a7bab87e13bf3ea
>
> Best regards,
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-29 14:52 ` Phillip Wood
@ 2024-10-29 14:58 ` Caleb White
2024-10-29 15:43 ` phillip.wood123
0 siblings, 1 reply; 26+ messages in thread
From: Caleb White @ 2024-10-29 14:58 UTC (permalink / raw)
To: phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 9:52 AM CDT, Phillip Wood wrote:
> Hi Caleb
>
> On 28/10/2024 19:09, Caleb White wrote:
>> This patch expands the test coverage by adding cases that specifically
>> handle relative paths. These tests verify correct behavior in a variety
>> of operations, including: adding, listing, pruning, moving, and
>> repairing worktrees with relative paths configured.
>>
>> This also adds a test case for reinitializing a repository that has
>> relative worktrees.
>
> It's nice to see new tests being added. If they were added with the code
> changes they test that would help reader understand the changes being
> made I think.
I had received feedback that the original patch was too large, so I
was trying to split it up into smaller, more digestible pieces. I could
go either way so it's really what the reviewers would prefer.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-29 14:58 ` Caleb White
@ 2024-10-29 15:43 ` phillip.wood123
2024-10-30 5:10 ` Caleb White
0 siblings, 1 reply; 26+ messages in thread
From: phillip.wood123 @ 2024-10-29 15:43 UTC (permalink / raw)
To: Caleb White, phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On 29/10/2024 14:58, Caleb White wrote:
> On Tue Oct 29, 2024 at 9:52 AM CDT, Phillip Wood wrote:
>> Hi Caleb
>>
>> On 28/10/2024 19:09, Caleb White wrote:
>>> This patch expands the test coverage by adding cases that specifically
>>> handle relative paths. These tests verify correct behavior in a variety
>>> of operations, including: adding, listing, pruning, moving, and
>>> repairing worktrees with relative paths configured.
>>>
>>> This also adds a test case for reinitializing a repository that has
>>> relative worktrees.
>>
>> It's nice to see new tests being added. If they were added with the code
>> changes they test that would help reader understand the changes being
>> made I think.
>
> I had received feedback that the original patch was too large, so I
> was trying to split it up into smaller, more digestible pieces.
One way to do that would be to convert the "add", "move" and "repair"
subcommands in separate patches changing option handling and appropriate
tests in each one rather than changing all subcommands and their tests
at once.
Best Wishes
Phillip
I could
> go either way so it's really what the reviewers would prefer.
>
> Best,
> Caleb
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-28 19:09 ` [PATCH v2 1/5] worktree: add CLI/config options for relative path linking Caleb White
2024-10-29 14:52 ` Phillip Wood
@ 2024-10-29 18:42 ` Taylor Blau
2024-10-30 5:07 ` Caleb White
1 sibling, 1 reply; 26+ messages in thread
From: Taylor Blau @ 2024-10-29 18:42 UTC (permalink / raw)
To: Caleb White; +Cc: git, Junio C Hamano, Eric Sunshine
On Mon, Oct 28, 2024 at 07:09:37PM +0000, Caleb White wrote:
> This patch 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.
This is great. This addresses the main concerns that you and I discussed
in the earlier round of this series, which was making sure that the new
behavior be opt-in, as it breaks backwards compatibility and thus
requires a new extension to quarantine older Git versions from touching
repositories that list their worktrees out with relative paths.
This approach makes the most sense to me because it doesn't impose such
a breakage between Git versions when the user doesn't explicitly opt-in
to the new behavior, which is the right approach to take here IMO.
> In response to reviewer feedback from the initial patch series[1],
> this revision includes slight refactoring for improved
> maintainability and clarity in the code base.
Great :-).
> diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
> index 048e349482df6c892055720eb53cdcd6c327b6ed..44b783c2774dc5ff65e3fa232b0c25cd5254876b 100644
> --- a/Documentation/config/worktree.txt
> +++ b/Documentation/config/worktree.txt
> @@ -7,3 +7,8 @@ worktree.guessRemote::
> such a branch exists, it is checked out and set as "upstream"
> for the new branch. If no such match can be found, it falls
> back to creating a new branch from the current HEAD.
I would have thought there would be a blank line in between this and the
section on worktree.guessRemote. ASCIIDoc doesn't require it because
this is a labeled list, but it does improve the readability of the raw
ASCIIDoc itself.
So not a big deal, but if you end up sending out another version of this
series it would be nice to include.
> +worktree.useRelativePaths::
> + If set to `true`, worktrees will be linked to the repository using
> + relative paths rather than using absolute paths. This is particularly
> + useful for setups where the repository and worktrees may be moved between
> + different locations or environments.
This is a good start, but I have a few suggestions on top that I'm
curious of your thoughts on. First: what is the default? Users
should have some insight into what the default is. Likewise, they should
know that that the default behavior does not introduce the repository
extension, but that setting this configuration to 'true' does.
Maybe something like the following on top?
--- 8< ---
diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
index 44b783c277..666cb3c190 100644
--- a/Documentation/config/worktree.txt
+++ b/Documentation/config/worktree.txt
@@ -7,8 +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::
- If set to `true`, worktrees will be linked to the repository using
- relative paths rather than using absolute paths. This is particularly
- useful for setups where the repository and worktrees may be moved between
- different locations or environments.
+ 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 "relativeWorktrees" repository extension, thus making it
+incompatible with older versions of Git.
--- >8 ---
> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> index 70437c815f13852bd2eb862176b8b933e6de0acf..975dc3c46d480480457ec4857988a6b8bc67b647 100644
> --- a/Documentation/git-worktree.txt
> +++ b/Documentation/git-worktree.txt
> @@ -216,6 +216,18 @@ 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::
> + Worktrees will be linked to the repository using relative paths
> + rather than using absolute paths. This is particularly useful for setups
> + where the repository and worktrees may be moved between different
> + locations or environments.
This paragraph is redundant with what you wrote in git-config(1). I
think all we want to say is that it overrides the setting of that
configuration variable, and refer users there with linkgit.
> ++
> +With `repair`, the linking files will be updated if there's an absolute/relative
> +mismatch, even if the links are correct.
This is worth keeping.
> +This can also be set up as the default behaviour by using the
> +`worktree.useRelativePaths` config option.
> +
This should get folded into my suggestion above.
> 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 @@
Was removing t2408 intentional here? I don't see the tests being
re-added elsewhere in this patch (though they may be introduced
elsewhere later in the series, I haven't read that far yet). Either way,
it may be worth mentioning in the commit message to avoid confusing
readers.
> diff --git a/worktree.c b/worktree.c
> index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..de5c5e53a5f2a758ddf470b5d6a9ad6c66247181 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -14,6 +14,8 @@
> #include "wt-status.h"
> #include "config.h"
>
> +int use_relative_paths = 0;
I wondered whether 'use_relative_paths' should be static, or if we need to extern it in
from somewhere else in the tree. But we do, from worktree.[ch], which
seems reasonable. It would be nice if there was some way to thread that
into the worktree.h API, but I think that this is a reasonable measure
to take for now.
> +
> void free_worktree(struct worktree *worktree)
> {
> if (!worktree)
> @@ -111,9 +113,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);
Whitespace change?
> }
>
> CALLOC_ARRAY(worktree, 1);
> @@ -725,12 +727,15 @@ 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)
Should this return an ssize_t instead, then? I don't think we're going
to have worktree paths that are actually larger than INT_MAX, but it
seems hygienic and good to prevent any accidental overflow issues.
Thanks,
Taylor
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function
2024-10-29 14:52 ` Phillip Wood
@ 2024-10-29 22:55 ` Taylor Blau
2024-10-30 5:38 ` Caleb White
2024-10-30 5:30 ` Caleb White
1 sibling, 1 reply; 26+ messages in thread
From: Taylor Blau @ 2024-10-29 22:55 UTC (permalink / raw)
To: phillip.wood; +Cc: Caleb White, git, Junio C Hamano, Eric Sunshine
On Tue, Oct 29, 2024 at 02:52:36PM +0000, Phillip Wood wrote:
> Hi Caleb
>
> On 28/10/2024 19:09, Caleb White wrote:
> > 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.
>
> That sounds like a useful change. I think it would be better to pass an
> extra parameter "use_relative_paths" rather than relying on a global
> varibale in worktree.c. Thank you for adding some documentaion for the new
> function.
Good suggestion. I definitely agree that this is a useful direction.
> > This also teachs `git worktree repair` to fix the linking files if
> > there is an absolute/relative paths but the links are correct. E.g.,
> > `git worktree repair` can be used to convert a valid worktree between
> > absolute and relative paths.
>
> This might be better as a separate step so that reviewers can concentrate on
> the correctness of write_werktree_linking_files() when reviewing this patch.
Indeed. This patch (even though the diffstat isn't overly large) is
somewhat noisy just because of the number of spots that needed to be
adjusted here.
I wonder if another way to split this up (in addition to what you wrote
above) might be to introduce the new function and convert one single
caller in the first patch. Then subsequent patches can go one callsite
at a time and convert them to use the new function.
That way, each patch is easy-ish to verify in isolation. I know that
results in some more patches, but I think that the additional clarity I
imagine we'll get is worth doing so.
Thanks,
Taylor
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-28 19:09 ` [PATCH v2 3/5] worktree: add tests for worktrees with relative paths Caleb White
2024-10-29 14:52 ` Phillip Wood
@ 2024-10-29 23:00 ` Taylor Blau
2024-10-30 4:16 ` Caleb White
1 sibling, 1 reply; 26+ messages in thread
From: Taylor Blau @ 2024-10-29 23:00 UTC (permalink / raw)
To: Caleb White; +Cc: git, Junio C Hamano, Eric Sunshine
On Mon, Oct 28, 2024 at 07:09:52PM +0000, Caleb White wrote:
> This patch expands the test coverage by adding cases that specifically
> handle relative paths. These tests verify correct behavior in a variety
> of operations, including: adding, listing, pruning, moving, and
> repairing worktrees with relative paths configured.
>
> 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 | 17 +++++++++++++----
> t/t2400-worktree-add.sh | 41 +++++++++++++++++++++++++++++++++++++++++
> t/t2401-worktree-prune.sh | 3 ++-
> t/t2402-worktree-list.sh | 22 ++++++++++++++++++++++
> t/t2403-worktree-move.sh | 22 ++++++++++++++++++++++
> t/t2406-worktree-repair.sh | 26 ++++++++++++++++++++++++++
> 6 files changed, 126 insertions(+), 5 deletions(-)
>
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e21b9aa5e7f4599af8b20165b50896c9a49ba7ea 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -435,6 +435,7 @@ sep_git_dir_worktree () {
> test_when_finished "rm -rf mainwt linkwt seprepo" &&
> git init mainwt &&
> test_commit -C mainwt gumby &&
> + git -C mainwt config worktree.useRelativePaths "$([ "$2" = "relative" ] && echo true || echo false)" &&
Can we avoid using '[' and perhaps split this out a little further.
Perhaps:
if test "relative" = $2
then
git -C mainwt config worktree.useRelativePaths true
else
git -C mainwt config worktree.useRelativePaths false
fi
I think the duplication is more than worth the clarity here.
Thanks,
Taylor
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-29 23:00 ` Taylor Blau
@ 2024-10-30 4:16 ` Caleb White
0 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 4:16 UTC (permalink / raw)
To: Taylor Blau; +Cc: git, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 6:00 PM CDT, Taylor Blau wrote:
>> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
>> index 0178aa62a41f1606f2382a4a10ab593ccf11e0e8..e21b9aa5e7f4599af8b20165b50896c9a49ba7ea 100755
>> --- a/t/t0001-init.sh
>> +++ b/t/t0001-init.sh
>> @@ -435,6 +435,7 @@ sep_git_dir_worktree () {
>> test_when_finished "rm -rf mainwt linkwt seprepo" &&
>> git init mainwt &&
>> test_commit -C mainwt gumby &&
>> + git -C mainwt config worktree.useRelativePaths "$([ "$2" = "relative" ] && echo true || echo false)" &&
>
> Can we avoid using '[' and perhaps split this out a little further.
> Perhaps:
>
> if test "relative" = $2
> then
> git -C mainwt config worktree.useRelativePaths true
> else
> git -C mainwt config worktree.useRelativePaths false
> fi
>
> I think the duplication is more than worth the clarity here.
Sounds good, I'll make that change.
Best,
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-29 18:42 ` Taylor Blau
@ 2024-10-30 5:07 ` Caleb White
0 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 5:07 UTC (permalink / raw)
To: Taylor Blau; +Cc: git, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 1:42 PM CDT, Taylor Blau wrote:
> On Mon, Oct 28, 2024 at 07:09:37PM +0000, Caleb White wrote:
>> diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
>> index 048e349482df6c892055720eb53cdcd6c327b6ed..44b783c2774dc5ff65e3fa232b0c25cd5254876b 100644
>> --- a/Documentation/config/worktree.txt
>> +++ b/Documentation/config/worktree.txt
>> @@ -7,3 +7,8 @@ worktree.guessRemote::
>> such a branch exists, it is checked out and set as "upstream"
>> for the new branch. If no such match can be found, it falls
>> back to creating a new branch from the current HEAD.
>
> I would have thought there would be a blank line in between this and the
> section on worktree.guessRemote. ASCIIDoc doesn't require it because
> this is a labeled list, but it does improve the readability of the raw
> ASCIIDoc itself.
>
> So not a big deal, but if you end up sending out another version of this
> series it would be nice to include.
I'll add the blank line in the next version of the patch.
>> +worktree.useRelativePaths::
>> + If set to `true`, worktrees will be linked to the repository using
>> + relative paths rather than using absolute paths. This is particularly
>> + useful for setups where the repository and worktrees may be moved between
>> + different locations or environments.
>
> This is a good start, but I have a few suggestions on top that I'm
> curious of your thoughts on. First: what is the default? Users
> should have some insight into what the default is. Likewise, they should
> know that that the default behavior does not introduce the repository
> extension, but that setting this configuration to 'true' does.
>
> Maybe something like the following on top?
>
> --- 8< ---
> diff --git a/Documentation/config/worktree.txt b/Documentation/config/worktree.txt
> index 44b783c277..666cb3c190 100644
> --- a/Documentation/config/worktree.txt
> +++ b/Documentation/config/worktree.txt
> @@ -7,8 +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::
> - If set to `true`, worktrees will be linked to the repository using
> - relative paths rather than using absolute paths. This is particularly
> - useful for setups where the repository and worktrees may be moved between
> - different locations or environments.
> + 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 "relativeWorktrees" repository extension, thus making it
> +incompatible with older versions of Git.
Sounds good to me. I'll update.
>> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
>> index 70437c815f13852bd2eb862176b8b933e6de0acf..975dc3c46d480480457ec4857988a6b8bc67b647 100644
>> --- a/Documentation/git-worktree.txt
>> +++ b/Documentation/git-worktree.txt
>> @@ -216,6 +216,18 @@ 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::
>> + Worktrees will be linked to the repository using relative paths
>> + rather than using absolute paths. This is particularly useful for setups
>> + where the repository and worktrees may be moved between different
>> + locations or environments.
>
> This paragraph is redundant with what you wrote in git-config(1). I
> think all we want to say is that it overrides the setting of that
> configuration variable, and refer users there with linkgit.
I agree. I'll update this paragraph to refer to the config documentation.
>> ++
>> +With `repair`, the linking files will be updated if there's an absolute/relative
>> +mismatch, even if the links are correct.
>
> This is worth keeping.
Keeping this as is.
>> +This can also be set up as the default behaviour by using the
>> +`worktree.useRelativePaths` config option.
>> +
>
> This should get folded into my suggestion above.
Done.
>> 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 @@
>
> Was removing t2408 intentional here? I don't see the tests being
> re-added elsewhere in this patch (though they may be introduced
> elsewhere later in the series, I haven't read that far yet). Either way,
> it may be worth mentioning in the commit message to avoid confusing
> readers.
Yes, this was intentional. This was added in the original round when the
default was changed to use relative paths. I added more comprehensive
tests in the various worktree files to cover the new functionality. I
will make sure to mention this in the commit message.
>> diff --git a/worktree.c b/worktree.c
>> index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..de5c5e53a5f2a758ddf470b5d6a9ad6c66247181 100644
>> --- a/worktree.c
>> +++ b/worktree.c
>> @@ -14,6 +14,8 @@
>> #include "wt-status.h"
>> #include "config.h"
>>
>> +int use_relative_paths = 0;
>
> I wondered whether 'use_relative_paths' should be static, or if we need to extern it in
> from somewhere else in the tree. But we do, from worktree.[ch], which
> seems reasonable. It would be nice if there was some way to thread that
> into the worktree.h API, but I think that this is a reasonable measure
> to take for now.
I can add a getter/setter to the worktree API so we're not using a
global variable.
>> +
>> void free_worktree(struct worktree *worktree)
>> {
>> if (!worktree)
>> @@ -111,9 +113,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);
>
> Whitespace change?
Yes, this was added in the original round, I didn't notice that it used
4 spaces instead of a tab. I had fixed this in v4 of the original round,
but this was merged in v3 so I just went ahead and fixed it here.
>> CALLOC_ARRAY(worktree, 1);
>> @@ -725,12 +727,15 @@ 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)
>
> Should this return an ssize_t instead, then? I don't think we're going
> to have worktree paths that are actually larger than INT_MAX, but it
> seems hygienic and good to prevent any accidental overflow issues.
I thought about this, but you'll run into OS limits long before you hit
INT_MAX. However, I can make this change to be hygienic.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 3/5] worktree: add tests for worktrees with relative paths
2024-10-29 15:43 ` phillip.wood123
@ 2024-10-30 5:10 ` Caleb White
0 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 5:10 UTC (permalink / raw)
To: phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 10:43 AM CDT, phillip.wood123 wrote:
>> I had received feedback that the original patch was too large, so I
>> was trying to split it up into smaller, more digestible pieces.
>
> One way to do that would be to convert the "add", "move" and "repair"
> subcommands in separate patches changing option handling and appropriate
> tests in each one rather than changing all subcommands and their tests
> at once.
That makes sense. I'll implement that.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 0/5] Allow relative worktree linking to be configured by the user
2024-10-29 14:55 ` [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Phillip Wood
@ 2024-10-30 5:13 ` Caleb White
0 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 5:13 UTC (permalink / raw)
To: phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 9:55 AM CDT, Phillip Wood wrote:
> Hi Caleb
>
> Thanks for working on this. I've left so high-level comments on the
> first three patches. As I said in my comments on patch 1 I think it
> would be helpful to re-order the changes so that the extension is
> implementation first and then adjust the implementation to support both
> relative and absolute paths before adding "--no-relative-paths" to "git
> worktree add/move/repair". I'd also like to avoid adding a new global
> variable
Thanks for your review! I appreciate it :).
I'll address your comments on the first three patches and get back to
you with a revised series.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-29 14:52 ` Phillip Wood
@ 2024-10-30 5:27 ` Caleb White
2024-10-30 20:16 ` Taylor Blau
0 siblings, 1 reply; 26+ messages in thread
From: Caleb White @ 2024-10-30 5:27 UTC (permalink / raw)
To: phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 9:52 AM CDT, Phillip Wood wrote:
>> This patch introduces the `--[no-]relative-paths` CLI option for
>
> "This patch" is a bit redundant, I'd say "Introduce a
> `--[no-]-relative-paths ..`
I removed "patch".
>> `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.
>
> This sounds good, I'm not sure the patch actually implements anything
> other than the option parsing though. I think it would make sense to add
> these options at the end of the patch series once the implementation has
> been changed to support them. I'd start with patches 4 and 5 to add the
> new extension setting first, then refactor worktree.c to handle creating
> worktrees with relative or absolute paths and set the extension if
> appropriate, then add the --relative-path option to "git worktree"
That makes sense, I can reorder the patches as you described.
>> 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.
>>
>> In response to reviewer feedback from the initial patch series[1],
>> this revision includes slight refactoring for improved
>> maintainability and clarity in the code base.
>
> Please don't mix cleanups with other code changes as it makes it hard to
> check that the cleanups don't change the behavior.
Fair enough, I'll separate the cleanups into their own patch.
>> +worktree.useRelativePaths::
>> + If set to `true`, worktrees will be linked to the repository using
>> + relative paths rather than using absolute paths. This is particularly
>> + useful for setups where the repository and worktrees may be moved between
>> + different locations or environments.
>
> I think it would be helpful to spell out the implications of this to the
> user - namely that you cannot use older versions of git on a repository
> with worktrees using relative paths and it may break third-party
> software as well.
I've added a note that you cannot use older versions of Git with
worktrees when using relative paths.
>> +--[no-]relative-paths::
>> + Worktrees will be linked to the repository using relative paths
>> + rather than using absolute paths. This is particularly useful for setups
>> + where the repository and worktrees may be moved between different
>> + locations or environments.
>
> Again we should spell out the implications of using relative paths.
This has been revised to simply point to the config docs.
>> +With `repair`, the linking files will be updated if there's an absolute/relative
>> +mismatch, even if the links are correct.
>> ++
>> +This can also be set up as the default behaviour by using the
>> +`worktree.useRelativePaths` config option.
>> +
>> --[no-]track::
>> When creating a new branch, if `<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..c1130be5890c905c0b648782a834eb8dfcd79ba5 100644
>> --- a/builtin/worktree.c
>> +++ b/builtin/worktree.c
>> @@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value,
>> if (!strcmp(var, "worktree.guessremote")) {
>> guess_remote = git_config_bool(var, value);
>> return 0;
>> + } else if (!strcmp(var, "worktree.userelativepaths")) {
>> + use_relative_paths = git_config_bool(var, value);
>
> As we're trying to remove global variables from libgit.a as part of the
> libification effort I'd be much happier if "use_relative_paths" was
> declared as a "static int" in this file and then passed down to the
> functions that need it rather than declaring it as a global in "worktree.c".
I can create a getter/setter in the worktree API to handle this, but
I'd rather not pass it as an argument to every function that needs it as
that would be a lot of changes. All of these functions would need their
signatures updated to include the new parameter:
- `add_worktree()`
- `update_worktree_location()`
- `repair_worktree_at_path()`
- `repair_worktrees()`
- `repair_worktree()`
- `write_worktree_linking_files()`
>> diff --git a/t/t2408-worktree-relative.sh b/t/t2408-worktree-relative.sh
>> deleted file mode 100755
>
> There's no explanation for this change in the commit message
I added an explanation for this deletion.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function
2024-10-29 14:52 ` Phillip Wood
2024-10-29 22:55 ` Taylor Blau
@ 2024-10-30 5:30 ` Caleb White
1 sibling, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 5:30 UTC (permalink / raw)
To: phillip.wood, git; +Cc: Taylor Blau, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 9:52 AM CDT, Phillip Wood wrote:
> On 28/10/2024 19:09, Caleb White wrote:
>> This also teachs `git worktree repair` to fix the linking files if
>> there is an absolute/relative paths but the links are correct. E.g.,
>> `git worktree repair` can be used to convert a valid worktree between
>> absolute and relative paths.
>
> This might be better as a separate step so that reviewers can
> concentrate on the correctness of write_werktree_linking_files() when
> reviewing this patch.
I'm planning on splitting this patch into separate patches for `add`,
`move`, and `repair` commands. This can likely be done in the same
patch as the `repair` command as it shouldn't be too difficult to
follow at that point.
Best,
Caleb
>> Signed-off-by: Caleb White <cdwhite3@pm.me>
>> ---
>> builtin/worktree.c | 11 +----
>> worktree.c | 118 +++++++++++++++++++++++++++--------------------------
>> worktree.h | 12 ++++++
>> 3 files changed, 74 insertions(+), 67 deletions(-)
>>
>> diff --git a/builtin/worktree.c b/builtin/worktree.c
>> index c1130be5890c905c0b648782a834eb8dfcd79ba5..bb06830d6fe82aa97833c6e87f034115dfaa23bd 100644
>> --- a/builtin/worktree.c
>> +++ b/builtin/worktree.c
>> @@ -417,8 +417,7 @@ static int add_worktree(const char *path, const char *refname,
>> const struct add_opts *opts)
>> {
>> struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
>> - struct strbuf sb = STRBUF_INIT, 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;
>> @@ -494,10 +493,7 @@ static int add_worktree(const char *path, const char *refname,
>>
>> strbuf_reset(&sb);
>> strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
>> - strbuf_realpath(&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);
>> strbuf_reset(&sb);
>> strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
>> write_file(sb.buf, "../..");
>> @@ -581,12 +577,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;
>> diff --git a/worktree.c b/worktree.c
>> index de5c5e53a5f2a758ddf470b5d6a9ad6c66247181..f4cee73d7a1edecafdff30b6d5e2d9dd1365b93e 100644
>> --- a/worktree.c
>> +++ b/worktree.c
>> @@ -381,29 +381,24 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
>> void update_worktree_location(struct worktree *wt, const char *path_)
>> {
>> struct strbuf path = STRBUF_INIT;
>> - struct strbuf 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);
>>
>> 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,
>> @@ -582,9 +577,9 @@ static void repair_gitfile(struct worktree *wt,
>> worktree_repair_fn fn, void *cb_data)
>> {
>> struct strbuf dotgit = STRBUF_INIT;
>> + struct strbuf gitdir = STRBUF_INIT;
>> struct strbuf repo = STRBUF_INIT;
>> struct strbuf backlink = STRBUF_INIT;
>> - struct strbuf tmp = STRBUF_INIT;
>> char *dotgit_contents = NULL;
>> const char *repair = NULL;
>> int err;
>> @@ -600,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) {
>> @@ -617,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);
>> }
>>
>> 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,
>> @@ -653,45 +651,30 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)
>>
>> 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;
>>
>> 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);
>> + if (!is_absolute_path(dotgit.buf)) {
>> + strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id);
>> strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0);
>> }
>>
>> if (!file_exists(dotgit.buf))
>> goto done;
>>
>> - 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);
>> 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)
>> @@ -766,13 +749,10 @@ void repair_worktree_at_path(const char *path,
>> worktree_repair_fn fn, void *cb_data)
>> {
>> struct strbuf dotgit = STRBUF_INIT;
>> - struct strbuf realdotgit = STRBUF_INIT;
>> struct strbuf backlink = STRBUF_INIT;
>> struct strbuf inferred_backlink = STRBUF_INIT;
>> struct strbuf gitdir = STRBUF_INIT;
>> struct strbuf olddotgit = STRBUF_INIT;
>> - struct strbuf realolddotgit = STRBUF_INIT;
>> - struct strbuf tmp = STRBUF_INIT;
>> char *dotgit_contents = NULL;
>> const char *repair = NULL;
>> int err;
>> @@ -784,25 +764,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) {
>> @@ -815,11 +795,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;
>> }
>>
>> @@ -841,39 +821,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);
>> }
>> 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)
>> @@ -1034,3 +1010,29 @@ int init_worktree_config(struct repository *r)
>> free(main_worktree_file);
>> return res;
>> }
>> +
>> +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir)
>> +{
>> + struct strbuf path = STRBUF_INIT;
>> + struct strbuf repo = STRBUF_INIT;
>> + struct strbuf tmp = STRBUF_INIT;
>> +
>> + strbuf_addbuf(&path, &dotgit);
>> + strbuf_strip_suffix(&path, "/.git");
>> + strbuf_realpath(&path, path.buf, 1);
>> + strbuf_addbuf(&repo, &gitdir);
>> + strbuf_strip_suffix(&repo, "/gitdir");
>> + strbuf_realpath(&repo, repo.buf, 1);
>> +
>> + if (use_relative_paths) {
>> + write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
>> + write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
>> + } else {
>> + write_file(gitdir.buf, "%s/.git", path.buf);
>> + write_file(dotgit.buf, "gitdir: %s", repo.buf);
>> + }
>> +
>> + strbuf_release(&path);
>> + strbuf_release(&repo);
>> + strbuf_release(&tmp);
>> +}
>> diff --git a/worktree.h b/worktree.h
>> index 37e65d508ed23d3e7a29850bb938285072a3aaa6..5929089891c97318a8f5329f7938264c717050d5 100644
>> --- a/worktree.h
>> +++ b/worktree.h
>> @@ -217,4 +217,16 @@ void strbuf_worktree_ref(const struct worktree *wt,
>> */
>> int init_worktree_config(struct repository *r);
>>
>> +/**
>> + * Write the .git file and gitdir file that links the worktree to the repository.
>> + *
>> + * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
>> + * is the path to the repository's `gitdir` file.
>> + *
>> + * Example
>> + * dotgit: "/path/to/foo/.git"
>> + * gitdir: "/path/to/repo/worktrees/foo/gitdir"
>> + */
>> +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir);
>> +
>> #endif
>>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function
2024-10-29 22:55 ` Taylor Blau
@ 2024-10-30 5:38 ` Caleb White
0 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 5:38 UTC (permalink / raw)
To: Taylor Blau, phillip.wood; +Cc: git, Junio C Hamano, Eric Sunshine
On Tue Oct 29, 2024 at 5:55 PM CDT, Taylor Blau wrote:
> On Tue, Oct 29, 2024 at 02:52:36PM +0000, Phillip Wood wrote:
>> On 28/10/2024 19:09, Caleb White wrote:
>> > 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.
>>
>> That sounds like a useful change. I think it would be better to pass an
>> extra parameter "use_relative_paths" rather than relying on a global
>> varibale in worktree.c. Thank you for adding some documentaion for the new
>> function.
>
> Good suggestion. I definitely agree that this is a useful direction.
I didn't particularly care for the global variable either, but that was
the easiest way to get up and running. I don't want to just pass this
parameter around as that will result in changing about 6-7 function
signatures. What are some other ways settings have been propagated
into the internals? Right now, I'm thinking of just using
a getter/setter in the worktree API, which would remove the global
variable.
>> This might be better as a separate step so that reviewers can concentrate on
>> the correctness of write_werktree_linking_files() when reviewing this patch.
>
> Indeed. This patch (even though the diffstat isn't overly large) is
> somewhat noisy just because of the number of spots that needed to be
> adjusted here.
>
> I wonder if another way to split this up (in addition to what you wrote
> above) might be to introduce the new function and convert one single
> caller in the first patch. Then subsequent patches can go one callsite
> at a time and convert them to use the new function.
>
> That way, each patch is easy-ish to verify in isolation. I know that
> results in some more patches, but I think that the additional clarity I
> imagine we'll get is worth doing so.
Sounds good, I'll split this up into multiple patches for each of the
subcommands. I'll also add the tests for that subcommand in the same
patch for context.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-30 5:27 ` Caleb White
@ 2024-10-30 20:16 ` Taylor Blau
2024-10-30 20:21 ` Caleb White
0 siblings, 1 reply; 26+ messages in thread
From: Taylor Blau @ 2024-10-30 20:16 UTC (permalink / raw)
To: Caleb White; +Cc: phillip.wood, git, Junio C Hamano, Eric Sunshine
On Wed, Oct 30, 2024 at 05:27:33AM +0000, Caleb White wrote:
> >> diff --git a/builtin/worktree.c b/builtin/worktree.c
> >> index dae63dedf4cac2621f51f95a39aa456b33acd894..c1130be5890c905c0b648782a834eb8dfcd79ba5 100644
> >> --- a/builtin/worktree.c
> >> +++ b/builtin/worktree.c
> >> @@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value,
> >> if (!strcmp(var, "worktree.guessremote")) {
> >> guess_remote = git_config_bool(var, value);
> >> return 0;
> >> + } else if (!strcmp(var, "worktree.userelativepaths")) {
> >> + use_relative_paths = git_config_bool(var, value);
> >
> > As we're trying to remove global variables from libgit.a as part of the
> > libification effort I'd be much happier if "use_relative_paths" was
> > declared as a "static int" in this file and then passed down to the
> > functions that need it rather than declaring it as a global in "worktree.c".
>
> I can create a getter/setter in the worktree API to handle this, but
> I'd rather not pass it as an argument to every function that needs it as
> that would be a lot of changes. All of these functions would need their
> signatures updated to include the new parameter:
>
> - `add_worktree()`
> - `update_worktree_location()`
> - `repair_worktree_at_path()`
> - `repair_worktrees()`
> - `repair_worktree()`
> - `write_worktree_linking_files()`
There is no reason to have a "getter" and "setter" for a extern'd
variable.
I agree that it would be preferable to have use_relative_paths be a
static int within this compilation unit and to pass it to the above
functions.
Thanks,
Taylor
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-30 20:16 ` Taylor Blau
@ 2024-10-30 20:21 ` Caleb White
2024-10-30 20:30 ` phillip.wood123
0 siblings, 1 reply; 26+ messages in thread
From: Caleb White @ 2024-10-30 20:21 UTC (permalink / raw)
To: Taylor Blau; +Cc: phillip.wood, git, Junio C Hamano, Eric Sunshine
On Wed Oct 30, 2024 at 3:16 PM CDT, Taylor Blau wrote:
> On Wed, Oct 30, 2024 at 05:27:33AM +0000, Caleb White wrote:
>> >> diff --git a/builtin/worktree.c b/builtin/worktree.c
>> >> index dae63dedf4cac2621f51f95a39aa456b33acd894..c1130be5890c905c0b648782a834eb8dfcd79ba5 100644
>> >> --- a/builtin/worktree.c
>> >> +++ b/builtin/worktree.c
>> >> @@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value,
>> >> if (!strcmp(var, "worktree.guessremote")) {
>> >> guess_remote = git_config_bool(var, value);
>> >> return 0;
>> >> + } else if (!strcmp(var, "worktree.userelativepaths")) {
>> >> + use_relative_paths = git_config_bool(var, value);
>> >
>> > As we're trying to remove global variables from libgit.a as part of the
>> > libification effort I'd be much happier if "use_relative_paths" was
>> > declared as a "static int" in this file and then passed down to the
>> > functions that need it rather than declaring it as a global in "worktree.c".
>>
>> I can create a getter/setter in the worktree API to handle this, but
>> I'd rather not pass it as an argument to every function that needs it as
>> that would be a lot of changes. All of these functions would need their
>> signatures updated to include the new parameter:
>>
>> - `add_worktree()`
>> - `update_worktree_location()`
>> - `repair_worktree_at_path()`
>> - `repair_worktrees()`
>> - `repair_worktree()`
>> - `write_worktree_linking_files()`
>
> There is no reason to have a "getter" and "setter" for a extern'd
> variable.
>
> I agree that it would be preferable to have use_relative_paths be a
> static int within this compilation unit and to pass it to the above
> functions.
If I created a getter/setter then the variable would no longer be
extern'd.
To be clear, you're advocating that I change the function signature
for all of the functions listed above to include the new parameter? That
seems like a lot of parameter bloat when I could just set the variable
in this compilation unit and access it directly in the
`write_worktree_linking_files()` function.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-30 20:21 ` Caleb White
@ 2024-10-30 20:30 ` phillip.wood123
2024-10-30 20:36 ` Caleb White
0 siblings, 1 reply; 26+ messages in thread
From: phillip.wood123 @ 2024-10-30 20:30 UTC (permalink / raw)
To: Caleb White, Taylor Blau; +Cc: phillip.wood, git, Junio C Hamano, Eric Sunshine
Hi Caleb
On 30/10/2024 20:21, Caleb White wrote:
> On Wed Oct 30, 2024 at 3:16 PM CDT, Taylor Blau wrote:
>> On Wed, Oct 30, 2024 at 05:27:33AM +0000, Caleb White wrote:
>>>>> diff --git a/builtin/worktree.c b/builtin/worktree.c
>>>>> index dae63dedf4cac2621f51f95a39aa456b33acd894..c1130be5890c905c0b648782a834eb8dfcd79ba5 100644
>>>>> --- a/builtin/worktree.c
>>>>> +++ b/builtin/worktree.c
>>>>> @@ -134,6 +134,9 @@ static int git_worktree_config(const char *var, const char *value,
>>>>> if (!strcmp(var, "worktree.guessremote")) {
>>>>> guess_remote = git_config_bool(var, value);
>>>>> return 0;
>>>>> + } else if (!strcmp(var, "worktree.userelativepaths")) {
>>>>> + use_relative_paths = git_config_bool(var, value);
>>>>
>>>> As we're trying to remove global variables from libgit.a as part of the
>>>> libification effort I'd be much happier if "use_relative_paths" was
>>>> declared as a "static int" in this file and then passed down to the
>>>> functions that need it rather than declaring it as a global in "worktree.c".
>>>
>>> I can create a getter/setter in the worktree API to handle this, but
>>> I'd rather not pass it as an argument to every function that needs it as
>>> that would be a lot of changes. All of these functions would need their
>>> signatures updated to include the new parameter:
>>>
>>> - `add_worktree()`
>>> - `update_worktree_location()`
>>> - `repair_worktree_at_path()`
>>> - `repair_worktrees()`
>>> - `repair_worktree()`
>>> - `write_worktree_linking_files()`
>>
>> There is no reason to have a "getter" and "setter" for a extern'd
>> variable.
>>
>> I agree that it would be preferable to have use_relative_paths be a
>> static int within this compilation unit and to pass it to the above
>> functions.
>
> If I created a getter/setter then the variable would no longer be
> extern'd.
>
> To be clear, you're advocating that I change the function signature
> for all of the functions listed above to include the new parameter? That
> seems like a lot of parameter bloat
It's a bit of a pain to have to change the function signatures and pass
the parameter down but it's not difficult to do.
> when I could just set the variable
> in this compilation unit and access it directly in the
> `write_worktree_linking_files()` function.
The problem with that is that the variable is still effectively global.
The aim of the libification work is to be able to work on more than one
repository from a single process while respecting each repository's
config settings.
Best Wishes
Phillip
> Best,
> Caleb
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v2 1/5] worktree: add CLI/config options for relative path linking
2024-10-30 20:30 ` phillip.wood123
@ 2024-10-30 20:36 ` Caleb White
0 siblings, 0 replies; 26+ messages in thread
From: Caleb White @ 2024-10-30 20:36 UTC (permalink / raw)
To: phillip.wood, Taylor Blau; +Cc: git, Junio C Hamano, Eric Sunshine
On Wed Oct 30, 2024 at 3:30 PM CDT, phillip.wood123 wrote:
> On 30/10/2024 20:21, Caleb White wrote:
>> If I created a getter/setter then the variable would no longer be
>> extern'd.
>>
>> To be clear, you're advocating that I change the function signature
>> for all of the functions listed above to include the new parameter? That
>> seems like a lot of parameter bloat
>
> It's a bit of a pain to have to change the function signatures and pass
> the parameter down but it's not difficult to do.
>
>> when I could just set the variable
>> in this compilation unit and access it directly in the
>> `write_worktree_linking_files()` function.
>
> The problem with that is that the variable is still effectively global.
> The aim of the libification work is to be able to work on more than one
> repository from a single process while respecting each repository's
> config settings.
>
I see your point. I'll make the changes you've suggested. Thanks for the
feedback.
Best,
Caleb
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2024-10-30 20:36 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-28 19:09 [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Caleb White
2024-10-28 19:09 ` [PATCH v2 1/5] worktree: add CLI/config options for relative path linking Caleb White
2024-10-29 14:52 ` Phillip Wood
2024-10-30 5:27 ` Caleb White
2024-10-30 20:16 ` Taylor Blau
2024-10-30 20:21 ` Caleb White
2024-10-30 20:30 ` phillip.wood123
2024-10-30 20:36 ` Caleb White
2024-10-29 18:42 ` Taylor Blau
2024-10-30 5:07 ` Caleb White
2024-10-28 19:09 ` [PATCH v2 2/5] worktree: add `write_worktree_linking_files` function Caleb White
2024-10-29 14:52 ` Phillip Wood
2024-10-29 22:55 ` Taylor Blau
2024-10-30 5:38 ` Caleb White
2024-10-30 5:30 ` Caleb White
2024-10-28 19:09 ` [PATCH v2 3/5] worktree: add tests for worktrees with relative paths Caleb White
2024-10-29 14:52 ` Phillip Wood
2024-10-29 14:58 ` Caleb White
2024-10-29 15:43 ` phillip.wood123
2024-10-30 5:10 ` Caleb White
2024-10-29 23:00 ` Taylor Blau
2024-10-30 4:16 ` Caleb White
2024-10-28 19:10 ` [PATCH v2 4/5] setup: correctly reinitialize repository version Caleb White
2024-10-28 19:10 ` [PATCH v2 5/5] worktree: add `relativeWorktrees` extension Caleb White
2024-10-29 14:55 ` [PATCH v2 0/5] Allow relative worktree linking to be configured by the user Phillip Wood
2024-10-30 5:13 ` Caleb White
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).