Skip to content

Commit 298d291

Browse files
calebdwgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b701634 commit 298d291

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

builtin/worktree.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
11901190
OPT__FORCE(&force,
11911191
N_("force move even if worktree is dirty or locked"),
11921192
PARSE_OPT_NOCOMPLETE),
1193+
OPT_BOOL(0, "relative-paths", &use_relative_paths,
1194+
N_("use relative paths for worktrees")),
11931195
OPT_END()
11941196
};
11951197
struct worktree **worktrees, *wt;
@@ -1242,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
12421244
if (rename(wt->path, dst.buf) == -1)
12431245
die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
12441246

1245-
update_worktree_location(wt, dst.buf);
1247+
update_worktree_location(wt, dst.buf, use_relative_paths);
12461248

12471249
strbuf_release(&dst);
12481250
free_worktrees(worktrees);

t/t2403-worktree-move.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,29 @@ test_expect_success 'not remove a repo with initialized submodule' '
247247
)
248248
'
249249

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+
250275
test_done

worktree.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,32 +376,28 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
376376
return ret;
377377
}
378378

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)
380381
{
381382
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;
385385

386386
if (is_main_worktree(wt))
387387
BUG("can't relocate main worktree");
388388

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);
390390
strbuf_realpath(&path, path_, 1);
391+
strbuf_addf(&dotgit, "%s/.git", path.buf);
391392
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);
397394

398395
free(wt->path);
399396
wt->path = strbuf_detach(&path, NULL);
400397
}
401398
strbuf_release(&path);
402-
strbuf_release(&repo);
403-
strbuf_release(&file);
404-
strbuf_release(&tmp);
399+
strbuf_release(&dotgit);
400+
strbuf_release(&gitdir);
405401
}
406402

407403
int is_worktree_being_rebased(const struct worktree *wt,

worktree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ int validate_worktree(const struct worktree *wt,
117117
/*
118118
* Update worktrees/xxx/gitdir with the new path.
119119
*/
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);
122122

123123
typedef void (* worktree_repair_fn)(int iserr, const char *path,
124124
const char *msg, void *cb_data);

0 commit comments

Comments
 (0)