Skip to content

Commit e5441ba

Browse files
pcloudsgitster
authored andcommitted
worktree remove: new command
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 62985f7 commit e5441ba

File tree

4 files changed

+121
-10
lines changed

4 files changed

+121
-10
lines changed

Documentation/git-worktree.txt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SYNOPSIS
1414
'git worktree lock' [--reason <string>] <worktree>
1515
'git worktree move' <worktree> <new-path>
1616
'git worktree prune' [-n] [-v] [--expire <expire>]
17+
'git worktree remove' [--force] <worktree>
1718
'git worktree unlock' <worktree>
1819

1920
DESCRIPTION
@@ -81,6 +82,13 @@ prune::
8182

8283
Prune working tree information in $GIT_DIR/worktrees.
8384

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+
8492
unlock::
8593

8694
Unlock a working tree, allowing it to be pruned, moved or deleted.
@@ -90,9 +98,10 @@ OPTIONS
9098

9199
-f::
92100
--force::
93-
By default, `add` refuses to create a new working tree when `<branch>`
94-
is already checked out by another working tree. This option overrides
95-
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.
96105

97106
-b <new-branch>::
98107
-B <new-branch>::
@@ -253,12 +262,6 @@ Multiple checkout in general is still experimental, and the support
253262
for submodules is incomplete. It is NOT recommended to make multiple
254263
checkouts of a superproject.
255264

256-
git-worktree could provide more automation for tasks currently
257-
performed manually, such as:
258-
259-
- `remove` to remove a linked working tree and its administrative files (and
260-
warn if the working tree is dirty)
261-
262265
GIT
263266
---
264267
Part of the linkgit:git[1] suite

builtin/worktree.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static const char * const worktree_usage[] = {
1717
N_("git worktree lock [<options>] <path>"),
1818
N_("git worktree move <worktree> <new-path>"),
1919
N_("git worktree prune [<options>]"),
20+
N_("git worktree remove [<options>] <worktree>"),
2021
N_("git worktree unlock <path>"),
2122
NULL
2223
};
@@ -605,6 +606,82 @@ static int move_worktree(int ac, const char **av, const char *prefix)
605606
return update_worktree_location(wt, dst.buf);
606607
}
607608

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+
608685
int cmd_worktree(int ac, const char **av, const char *prefix)
609686
{
610687
struct option options[] = {
@@ -629,5 +706,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
629706
return unlock_worktree(ac - 1, av + 1, prefix);
630707
if (!strcmp(av[1], "move"))
631708
return move_worktree(ac - 1, av + 1, prefix);
709+
if (!strcmp(av[1], "remove"))
710+
return remove_worktree(ac - 1, av + 1, prefix);
632711
usage_with_options(worktree_usage, options);
633712
}

contrib/completion/git-completion.bash

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

27162716
_git_worktree ()
27172717
{
2718-
local subcommands="add list lock move prune unlock"
2718+
local subcommands="add list lock move prune remove unlock"
27192719
local subcommand="$(__git_find_on_cmdline "$subcommands")"
27202720
if [ -z "$subcommand" ]; then
27212721
__gitcomp "$subcommands"
@@ -2733,6 +2733,9 @@ _git_worktree ()
27332733
prune,--*)
27342734
__gitcomp "--dry-run --expire --verbose"
27352735
;;
2736+
remove,--*)
2737+
__gitcomp "--force"
2738+
;;
27362739
*)
27372740
;;
27382741
esac

t/t2028-worktree-move.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,30 @@ test_expect_success 'move main worktree' '
8989
test_must_fail git worktree move . def
9090
'
9191

92+
test_expect_success 'remove main worktree' '
93+
test_must_fail git worktree remove .
94+
'
95+
96+
test_expect_success 'remove locked worktree' '
97+
git worktree lock destination &&
98+
test_must_fail git worktree remove destination &&
99+
git worktree unlock destination
100+
'
101+
102+
test_expect_success 'remove worktree with dirty tracked file' '
103+
echo dirty >>destination/init.t &&
104+
test_must_fail git worktree remove destination
105+
'
106+
107+
test_expect_success 'remove worktree with untracked file' '
108+
git -C destination checkout init.t &&
109+
: >destination/untracked &&
110+
test_must_fail git worktree remove destination
111+
'
112+
113+
test_expect_success 'force remove worktree with untracked file' '
114+
git worktree remove --force destination &&
115+
test_path_is_missing destination
116+
'
117+
92118
test_done

0 commit comments

Comments
 (0)