worktree: add relative cli/config options to `move` command

This teaches the `worktree move` command to respect the `--[no-]relative-paths` CLI option and `worktree.useRelativePaths` config setting. If an existing worktree is moved with `--relative-paths` the new path will be relative (and visa-versa). Signed-off-by: Caleb White <cdwhite3@pm.me> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Caleb White committed Nov 29, 2024 at 22:23 UTC 298d2917e26520791b47ba5d38c1866e21894cc7
4 files changed +39 -16
builtin/worktree.c
+3 -1
@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
1190 OPT__FORCE(&force,
1191 N_("force move even if worktree is dirty or locked"),
1192 PARSE_OPT_NOCOMPLETE),
1193 + OPT_BOOL(0, "relative-paths", &use_relative_paths,
1194 + N_("use relative paths for worktrees")),
1195 OPT_END()
1196 };
1197 struct worktree **worktrees, *wt;
@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
1244 if (rename(wt->path, dst.buf) == -1)
1245 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
1246
1245 - update_worktree_location(wt, dst.buf);
1247 + update_worktree_location(wt, dst.buf, use_relative_paths);
1248
1249 strbuf_release(&dst);
1250 free_worktrees(worktrees);
t/t2403-worktree-move.sh
+25
@@ -247,4 +247,29 @@ test_expect_success 'not remove a repo with initialized submodule' '
247 )
248 '
249
250 +test_expect_success 'move worktree with absolute path to relative path' '
251 + test_config worktree.useRelativePaths false &&
252 + git worktree add ./absolute &&
253 + git worktree move --relative-paths absolute relative &&
254 + echo "gitdir: ../.git/worktrees/absolute" >expect &&
255 + test_cmp expect relative/.git &&
256 + echo "../../../relative/.git" >expect &&
257 + test_cmp expect .git/worktrees/absolute/gitdir &&
258 + test_config worktree.useRelativePaths true &&
259 + git worktree move relative relative2 &&
260 + echo "gitdir: ../.git/worktrees/absolute" >expect &&
261 + test_cmp expect relative2/.git &&
262 + echo "../../../relative2/.git" >expect &&
263 + test_cmp expect .git/worktrees/absolute/gitdir
264 +'
265 +
266 +test_expect_success 'move worktree with relative path to absolute path' '
267 + test_config worktree.useRelativePaths true &&
268 + git worktree move --no-relative-paths relative2 absolute &&
269 + echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
270 + test_cmp expect absolute/.git &&
271 + echo "$(pwd)/absolute/.git" >expect &&
272 + test_cmp expect .git/worktrees/absolute/gitdir
273 +'
274 +
275 test_done
worktree.c
+9 -13
@@ -376,32 +376,28 @@ done:
376 return ret;
377 }
378
379 -void update_worktree_location(struct worktree *wt, const char *path_)
379 +void update_worktree_location(struct worktree *wt, const char *path_,
380 + int use_relative_paths)
381 {
382 struct strbuf path = STRBUF_INIT;
382 - struct strbuf repo = STRBUF_INIT;
383 - struct strbuf file = STRBUF_INIT;
384 - struct strbuf tmp = STRBUF_INIT;
383 + struct strbuf dotgit = STRBUF_INIT;
384 + struct strbuf gitdir = STRBUF_INIT;
385
386 if (is_main_worktree(wt))
387 BUG("can't relocate main worktree");
388
389 - strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
389 + strbuf_realpath(&gitdir, git_common_path("worktrees/%s/gitdir", wt->id), 1);
390 strbuf_realpath(&path, path_, 1);
391 + strbuf_addf(&dotgit, "%s/.git", path.buf);
392 if (fspathcmp(wt->path, path.buf)) {
392 - strbuf_addf(&file, "%s/gitdir", repo.buf);
393 - write_file(file.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
394 - strbuf_reset(&file);
395 - strbuf_addf(&file, "%s/.git", path.buf);
396 - write_file(file.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
393 + write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
394
395 free(wt->path);
396 wt->path = strbuf_detach(&path, NULL);
397 }
398 strbuf_release(&path);
402 - strbuf_release(&repo);
403 - strbuf_release(&file);
404 - strbuf_release(&tmp);
399 + strbuf_release(&dotgit);
400 + strbuf_release(&gitdir);
401 }
402
403 int is_worktree_being_rebased(const struct worktree *wt,
worktree.h
+2 -2
@@ -117,8 +117,8 @@ int validate_worktree(const struct worktree *wt,
117 /*
118 * Update worktrees/xxx/gitdir with the new path.
119 */
120 -void update_worktree_location(struct worktree *wt,
121 - const char *path_);
120 +void update_worktree_location(struct worktree *wt, const char *path_,
121 + int use_relative_paths);
122
123 typedef void (* worktree_repair_fn)(int iserr, const char *path,
124 const char *msg, void *cb_data);