Skip to content

Commit 44ac722

Browse files
committed
Merge branch 'nd/worktree-move' into pu
"git worktree" learned move and remove subcommands. * nd/worktree-move: fixup! worktree move: new command worktree remove: new command worktree move: refuse to move worktrees with submodules worktree move: accept destination as directory worktree move: new command worktree.c: add update_worktree_location() worktree.c: add validate_worktree()
2 parents aa8e08e + a8122c8 commit 44ac722

File tree

6 files changed

+336
-11
lines changed

6 files changed

+336
-11
lines changed

Documentation/git-worktree.txt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ SYNOPSIS
1212
'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
1313
'git worktree list' [--porcelain]
1414
'git worktree lock' [--reason <string>] <worktree>
15+
'git worktree move' <worktree> <new-path>
1516
'git worktree prune' [-n] [-v] [--expire <expire>]
17+
'git worktree remove' [--force] <worktree>
1618
'git worktree unlock' <worktree>
1719

1820
DESCRIPTION
@@ -71,10 +73,22 @@ files from being pruned automatically. This also prevents it from
7173
being moved or deleted. Optionally, specify a reason for the lock
7274
with `--reason`.
7375

76+
move::
77+
78+
Move a working tree to a new location. Note that the main working tree
79+
cannot be moved yet.
80+
7481
prune::
7582

7683
Prune working tree information in $GIT_DIR/worktrees.
7784

85+
remove::
86+
87+
Remove a working tree. Only clean working trees (no untracked files
88+
and no modification in tracked files) can be removed. Unclean working
89+
trees can be removed with `--force`. The main working tree cannot be
90+
removed.
91+
7892
unlock::
7993

8094
Unlock a working tree, allowing it to be pruned, moved or deleted.
@@ -84,9 +98,10 @@ OPTIONS
8498

8599
-f::
86100
--force::
87-
By default, `add` refuses to create a new working tree when `<branch>`
88-
is already checked out by another working tree. This option overrides
89-
that safeguard.
101+
By default, `add` refuses to create a new working tree when
102+
`<branch>` is already checked out by another working tree and
103+
`remove` refuses to remove an unclean working tree. This option
104+
overrides that safeguard.
90105

91106
-b <new-branch>::
92107
-B <new-branch>::
@@ -247,13 +262,6 @@ Multiple checkout in general is still experimental, and the support
247262
for submodules is incomplete. It is NOT recommended to make multiple
248263
checkouts of a superproject.
249264

250-
git-worktree could provide more automation for tasks currently
251-
performed manually, such as:
252-
253-
- `remove` to remove a linked working tree and its administrative files (and
254-
warn if the working tree is dirty)
255-
- `mv` to move or rename a working tree and update its administrative files
256-
257265
GIT
258266
---
259267
Part of the linkgit:git[1] suite

builtin/worktree.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ static const char * const worktree_usage[] = {
1515
N_("git worktree add [<options>] <path> [<branch>]"),
1616
N_("git worktree list [<options>]"),
1717
N_("git worktree lock [<options>] <path>"),
18+
N_("git worktree move <worktree> <new-path>"),
1819
N_("git worktree prune [<options>]"),
20+
N_("git worktree remove [<options>] <worktree>"),
1921
N_("git worktree unlock <path>"),
2022
NULL
2123
};
@@ -524,6 +526,162 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
524526
return ret;
525527
}
526528

529+
static void validate_no_submodules(const struct worktree *wt)
530+
{
531+
struct index_state istate = {0};
532+
int i, found_submodules = 0;
533+
534+
if (read_index_from(&istate, worktree_git_path(wt, "index")) > 0) {
535+
for (i = 0; i < istate.cache_nr; i++) {
536+
struct cache_entry *ce = istate.cache[i];
537+
538+
if (S_ISGITLINK(ce->ce_mode)) {
539+
found_submodules = 1;
540+
break;
541+
}
542+
}
543+
}
544+
discard_index(&istate);
545+
546+
if (found_submodules)
547+
die(_("This working tree contains submodules and cannot be moved yet"));
548+
}
549+
550+
static int move_worktree(int ac, const char **av, const char *prefix)
551+
{
552+
struct option options[] = {
553+
OPT_END()
554+
};
555+
struct worktree **worktrees, *wt;
556+
struct strbuf dst = STRBUF_INIT;
557+
const char *reason;
558+
559+
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
560+
if (ac != 2)
561+
usage_with_options(worktree_usage, options);
562+
563+
strbuf_addstr(&dst, prefix_filename(prefix,
564+
strlen(prefix),
565+
av[1]));
566+
if (is_directory(dst.buf))
567+
/*
568+
* keep going, dst will be appended after we get the
569+
* source's absolute path
570+
*/
571+
;
572+
else if (file_exists(dst.buf))
573+
die(_("target '%s' already exists"), av[1]);
574+
575+
worktrees = get_worktrees(0);
576+
wt = find_worktree(worktrees, prefix, av[0]);
577+
if (!wt)
578+
die(_("'%s' is not a working directory"), av[0]);
579+
if (is_main_worktree(wt))
580+
die(_("'%s' is a main working directory"), av[0]);
581+
reason = is_worktree_locked(wt);
582+
if (reason) {
583+
if (*reason)
584+
die(_("already locked, reason: %s"), reason);
585+
die(_("already locked, no reason"));
586+
}
587+
if (validate_worktree(wt, 0))
588+
return -1;
589+
590+
validate_no_submodules(wt);
591+
592+
if (is_directory(dst.buf)) {
593+
const char *sep = find_last_dir_sep(wt->path);
594+
595+
if (!sep)
596+
die(_("could not figure out destination name from '%s'"),
597+
wt->path);
598+
strbuf_addstr(&dst, sep);
599+
if (file_exists(dst.buf))
600+
die(_("target '%s' already exists"), dst.buf);
601+
}
602+
603+
if (rename(wt->path, dst.buf) == -1)
604+
die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
605+
606+
return update_worktree_location(wt, dst.buf);
607+
}
608+
609+
static int remove_worktree(int ac, const char **av, const char *prefix)
610+
{
611+
int force = 0;
612+
struct option options[] = {
613+
OPT_BOOL(0, "force", &force,
614+
N_("force removing even if the worktree is dirty")),
615+
OPT_END()
616+
};
617+
struct worktree **worktrees, *wt;
618+
struct strbuf sb = STRBUF_INIT;
619+
const char *reason;
620+
int ret = 0;
621+
622+
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
623+
if (ac != 1)
624+
usage_with_options(worktree_usage, options);
625+
626+
worktrees = get_worktrees(0);
627+
wt = find_worktree(worktrees, prefix, av[0]);
628+
if (!wt)
629+
die(_("'%s' is not a working directory"), av[0]);
630+
if (is_main_worktree(wt))
631+
die(_("'%s' is a main working directory"), av[0]);
632+
reason = is_worktree_locked(wt);
633+
if (reason) {
634+
if (*reason)
635+
die(_("already locked, reason: %s"), reason);
636+
die(_("already locked, no reason"));
637+
}
638+
if (validate_worktree(wt, 0))
639+
return -1;
640+
641+
if (!force) {
642+
struct argv_array child_env = ARGV_ARRAY_INIT;
643+
struct child_process cp;
644+
char buf[1];
645+
646+
argv_array_pushf(&child_env, "%s=%s/.git",
647+
GIT_DIR_ENVIRONMENT, wt->path);
648+
argv_array_pushf(&child_env, "%s=%s",
649+
GIT_WORK_TREE_ENVIRONMENT, wt->path);
650+
memset(&cp, 0, sizeof(cp));
651+
argv_array_pushl(&cp.args, "status", "--porcelain", NULL);
652+
cp.env = child_env.argv;
653+
cp.git_cmd = 1;
654+
cp.dir = wt->path;
655+
cp.out = -1;
656+
ret = start_command(&cp);
657+
if (ret)
658+
die_errno(_("failed to run git-status on '%s', code %d"),
659+
av[0], ret);
660+
ret = xread(cp.out, buf, sizeof(buf));
661+
if (ret)
662+
die(_("'%s' is dirty, use --force to delete it"), av[0]);
663+
close(cp.out);
664+
ret = finish_command(&cp);
665+
if (ret)
666+
die_errno(_("failed to run git-status on '%s', code %d"),
667+
av[0], ret);
668+
}
669+
strbuf_addstr(&sb, wt->path);
670+
if (remove_dir_recursively(&sb, 0)) {
671+
error_errno(_("failed to delete '%s'"), sb.buf);
672+
ret = -1;
673+
}
674+
strbuf_reset(&sb);
675+
strbuf_addstr(&sb, git_common_path("worktrees/%s", wt->id));
676+
if (remove_dir_recursively(&sb, 0)) {
677+
error_errno(_("failed to delete '%s'"), sb.buf);
678+
ret = -1;
679+
}
680+
strbuf_release(&sb);
681+
free_worktrees(worktrees);
682+
return ret;
683+
}
684+
527685
int cmd_worktree(int ac, const char **av, const char *prefix)
528686
{
529687
struct option options[] = {
@@ -546,5 +704,9 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
546704
return lock_worktree(ac - 1, av + 1, prefix);
547705
if (!strcmp(av[1], "unlock"))
548706
return unlock_worktree(ac - 1, av + 1, prefix);
707+
if (!strcmp(av[1], "move"))
708+
return move_worktree(ac - 1, av + 1, prefix);
709+
if (!strcmp(av[1], "remove"))
710+
return remove_worktree(ac - 1, av + 1, prefix);
549711
usage_with_options(worktree_usage, options);
550712
}

contrib/completion/git-completion.bash

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2716,7 +2716,7 @@ _git_whatchanged ()
27162716

27172717
_git_worktree ()
27182718
{
2719-
local subcommands="add list lock prune unlock"
2719+
local subcommands="add list lock move prune remove unlock"
27202720
local subcommand="$(__git_find_on_cmdline "$subcommands")"
27212721
if [ -z "$subcommand" ]; then
27222722
__gitcomp "$subcommands"
@@ -2734,6 +2734,9 @@ _git_worktree ()
27342734
prune,--*)
27352735
__gitcomp "--dry-run --expire --verbose"
27362736
;;
2737+
remove,--*)
2738+
__gitcomp "--force"
2739+
;;
27372740
*)
27382741
;;
27392742
esac

t/t2028-worktree-move.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,61 @@ test_expect_success 'unlock worktree twice' '
5959
test_path_is_missing .git/worktrees/source/locked
6060
'
6161

62+
test_expect_success 'move non-worktree' '
63+
mkdir abc &&
64+
test_must_fail git worktree move abc def
65+
'
66+
67+
test_expect_success 'move locked worktree' '
68+
git worktree lock source &&
69+
test_must_fail git worktree move source destination &&
70+
git worktree unlock source
71+
'
72+
73+
test_expect_success 'move worktree' '
74+
toplevel="$(pwd)" &&
75+
git worktree move source destination &&
76+
test_path_is_missing source &&
77+
git worktree list --porcelain | grep "^worktree" >actual &&
78+
cat <<-EOF >expected &&
79+
worktree $toplevel
80+
worktree $toplevel/destination
81+
worktree $toplevel/elsewhere
82+
EOF
83+
test_cmp expected actual &&
84+
git -C destination log --format=%s >actual2 &&
85+
echo init >expected2 &&
86+
test_cmp expected2 actual2
87+
'
88+
89+
test_expect_success 'move main worktree' '
90+
test_must_fail git worktree move . def
91+
'
92+
93+
test_expect_success 'remove main worktree' '
94+
test_must_fail git worktree remove .
95+
'
96+
97+
test_expect_success 'remove locked worktree' '
98+
git worktree lock destination &&
99+
test_must_fail git worktree remove destination &&
100+
git worktree unlock destination
101+
'
102+
103+
test_expect_success 'remove worktree with dirty tracked file' '
104+
echo dirty >>destination/init.t &&
105+
test_must_fail git worktree remove destination
106+
'
107+
108+
test_expect_success 'remove worktree with untracked file' '
109+
git -C destination checkout init.t &&
110+
: >destination/untracked &&
111+
test_must_fail git worktree remove destination
112+
'
113+
114+
test_expect_success 'force remove worktree with untracked file' '
115+
git worktree remove --force destination &&
116+
test_path_is_missing destination
117+
'
118+
62119
test_done

0 commit comments

Comments
 (0)