6 #include "parse-options.h"
7 #include "argv-array.h"
10 #include "run-command.h"
16 static const char * const worktree_usage
[] = {
17 N_("git worktree add [<options>] <path> [<commit-ish>]"),
18 N_("git worktree list [<options>]"),
19 N_("git worktree lock [<options>] <path>"),
20 N_("git worktree move <worktree> <new-path>"),
21 N_("git worktree prune [<options>]"),
22 N_("git worktree remove [<options>] <worktree>"),
23 N_("git worktree unlock <path>"),
37 static int guess_remote
;
38 static timestamp_t expire
;
40 static int git_worktree_config(const char *var
, const char *value
, void *cb
)
42 if (!strcmp(var
, "worktree.guessremote")) {
43 guess_remote
= git_config_bool(var
, value
);
47 return git_default_config(var
, value
, cb
);
50 static int delete_git_dir(const char *id
)
52 struct strbuf sb
= STRBUF_INIT
;
55 strbuf_addstr(&sb
, git_common_path("worktrees/%s", id
));
56 ret
= remove_dir_recursively(&sb
, 0);
57 if (ret
< 0 && errno
== ENOTDIR
)
60 error_errno(_("failed to delete '%s'"), sb
.buf
);
65 static void delete_worktrees_dir_if_empty(void)
67 rmdir(git_path("worktrees")); /* ignore failed removal */
70 static int prune_worktree(const char *id
, struct strbuf
*reason
)
78 if (!is_directory(git_path("worktrees/%s", id
))) {
79 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
82 if (file_exists(git_path("worktrees/%s/locked", id
)))
84 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
85 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
88 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
90 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
94 len
= xsize_t(st
.st_size
);
97 read_result
= read_in_full(fd
, path
, len
);
98 if (read_result
< 0) {
99 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
100 id
, strerror(errno
));
107 if (read_result
!= len
) {
109 _("Removing worktrees/%s: short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
110 id
, (uintmax_t)len
, (uintmax_t)read_result
);
114 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
117 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
122 if (!file_exists(path
)) {
124 if (stat(git_path("worktrees/%s/index", id
), &st
) ||
125 st
.st_mtime
<= expire
) {
126 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
136 static void prune_worktrees(void)
138 struct strbuf reason
= STRBUF_INIT
;
139 DIR *dir
= opendir(git_path("worktrees"));
143 while ((d
= readdir(dir
)) != NULL
) {
144 if (is_dot_or_dotdot(d
->d_name
))
146 strbuf_reset(&reason
);
147 if (!prune_worktree(d
->d_name
, &reason
))
149 if (show_only
|| verbose
)
150 printf("%s\n", reason
.buf
);
153 delete_git_dir(d
->d_name
);
157 delete_worktrees_dir_if_empty();
158 strbuf_release(&reason
);
161 static int prune(int ac
, const char **av
, const char *prefix
)
163 struct option options
[] = {
164 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
165 OPT__VERBOSE(&verbose
, N_("report pruned working trees")),
166 OPT_EXPIRY_DATE(0, "expire", &expire
,
167 N_("expire working trees older than <time>")),
172 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
174 usage_with_options(worktree_usage
, options
);
179 static char *junk_work_tree
;
180 static char *junk_git_dir
;
182 static pid_t junk_pid
;
184 static void remove_junk(void)
186 struct strbuf sb
= STRBUF_INIT
;
187 if (!is_junk
|| getpid() != junk_pid
)
190 strbuf_addstr(&sb
, junk_git_dir
);
191 remove_dir_recursively(&sb
, 0);
194 if (junk_work_tree
) {
195 strbuf_addstr(&sb
, junk_work_tree
);
196 remove_dir_recursively(&sb
, 0);
201 static void remove_junk_on_signal(int signo
)
208 static const char *worktree_basename(const char *path
, int *olen
)
214 while (len
&& is_dir_sep(path
[len
- 1]))
217 for (name
= path
+ len
- 1; name
> path
; name
--)
218 if (is_dir_sep(*name
)) {
227 static void validate_worktree_add(const char *path
, const struct add_opts
*opts
)
229 struct worktree
**worktrees
;
233 if (file_exists(path
) && !is_empty_dir(path
))
234 die(_("'%s' already exists"), path
);
236 worktrees
= get_worktrees(0);
238 * find_worktree()'s suffix matching may undesirably find the main
239 * rather than a linked worktree (for instance, when the basenames
240 * of the main worktree and the one being created are the same).
241 * We're only interested in linked worktrees, so skip the main
244 wt
= find_worktree(worktrees
+ 1, NULL
, path
);
248 locked
= !!worktree_lock_reason(wt
);
249 if ((!locked
&& opts
->force
) || (locked
&& opts
->force
> 1)) {
250 if (delete_git_dir(wt
->id
))
251 die(_("unable to re-add worktree '%s'"), path
);
256 die(_("'%s' is a missing but locked worktree;\nuse 'add -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path
);
258 die(_("'%s' is a missing but already registered worktree;\nuse 'add -f' to override, or 'prune' or 'remove' to clear"), path
);
261 free_worktrees(worktrees
);
264 static int add_worktree(const char *path
, const char *refname
,
265 const struct add_opts
*opts
)
267 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
268 struct strbuf sb
= STRBUF_INIT
;
271 struct child_process cp
= CHILD_PROCESS_INIT
;
272 struct argv_array child_env
= ARGV_ARRAY_INIT
;
273 int counter
= 0, len
, ret
;
274 struct strbuf symref
= STRBUF_INIT
;
275 struct commit
*commit
= NULL
;
278 validate_worktree_add(path
, opts
);
280 /* is 'refname' a branch or commit? */
281 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
282 ref_exists(symref
.buf
)) {
285 die_if_checked_out(symref
.buf
, 0);
287 commit
= lookup_commit_reference_by_name(refname
);
289 die(_("invalid reference: %s"), refname
);
291 name
= worktree_basename(path
, &len
);
292 git_path_buf(&sb_repo
, "worktrees/%.*s", (int)(path
+ len
- name
), name
);
294 if (safe_create_leading_directories_const(sb_repo
.buf
))
295 die_errno(_("could not create leading directories of '%s'"),
297 while (!stat(sb_repo
.buf
, &st
)) {
299 strbuf_setlen(&sb_repo
, len
);
300 strbuf_addf(&sb_repo
, "%d", counter
);
302 name
= strrchr(sb_repo
.buf
, '/') + 1;
306 sigchain_push_common(remove_junk_on_signal
);
308 if (mkdir(sb_repo
.buf
, 0777))
309 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
310 junk_git_dir
= xstrdup(sb_repo
.buf
);
314 * lock the incomplete repo so prune won't delete it, unlock
315 * after the preparation is over.
317 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
318 if (!opts
->keep_locked
)
319 write_file(sb
.buf
, "initializing");
321 write_file(sb
.buf
, "added with --lock");
323 strbuf_addf(&sb_git
, "%s/.git", path
);
324 if (safe_create_leading_directories_const(sb_git
.buf
))
325 die_errno(_("could not create leading directories of '%s'"),
327 junk_work_tree
= xstrdup(path
);
330 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
331 write_file(sb
.buf
, "%s", real_path(sb_git
.buf
));
332 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
333 real_path(get_git_common_dir()), name
);
335 * This is to keep resolve_ref() happy. We need a valid HEAD
336 * or is_git_directory() will reject the directory. Any value which
337 * looks like an object ID will do since it will be immediately
338 * replaced by the symbolic-ref or update-ref invocation in the new
342 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
343 write_file(sb
.buf
, "%s", sha1_to_hex(null_sha1
));
345 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
346 write_file(sb
.buf
, "../..");
348 argv_array_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
349 argv_array_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
353 argv_array_pushl(&cp
.args
, "update-ref", "HEAD",
354 oid_to_hex(&commit
->object
.oid
), NULL
);
356 argv_array_pushl(&cp
.args
, "symbolic-ref", "HEAD",
359 argv_array_push(&cp
.args
, "--quiet");
362 cp
.env
= child_env
.argv
;
363 ret
= run_command(&cp
);
367 if (opts
->checkout
) {
369 argv_array_clear(&cp
.args
);
370 argv_array_pushl(&cp
.args
, "reset", "--hard", NULL
);
372 argv_array_push(&cp
.args
, "--quiet");
373 cp
.env
= child_env
.argv
;
374 ret
= run_command(&cp
);
380 FREE_AND_NULL(junk_work_tree
);
381 FREE_AND_NULL(junk_git_dir
);
384 if (ret
|| !opts
->keep_locked
) {
386 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
387 unlink_or_warn(sb
.buf
);
391 * Hook failure does not warrant worktree deletion, so run hook after
392 * is_junk is cleared, but do return appropriate code when hook fails.
394 if (!ret
&& opts
->checkout
) {
395 const char *hook
= find_hook("post-checkout");
397 const char *env
[] = { "GIT_DIR", "GIT_WORK_TREE", NULL
};
400 cp
.stdout_to_stderr
= 1;
404 argv_array_pushl(&cp
.args
, absolute_path(hook
),
405 oid_to_hex(&null_oid
),
406 oid_to_hex(&commit
->object
.oid
),
408 ret
= run_command(&cp
);
412 argv_array_clear(&child_env
);
414 strbuf_release(&symref
);
415 strbuf_release(&sb_repo
);
416 strbuf_release(&sb_git
);
420 static void print_preparing_worktree_line(int detach
,
422 const char *new_branch
,
423 int force_new_branch
)
425 if (force_new_branch
) {
426 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
428 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
430 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
432 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
433 } else if (new_branch
) {
434 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
436 struct strbuf s
= STRBUF_INIT
;
437 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
439 printf_ln(_("Preparing worktree (checking out '%s')"),
442 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
444 die(_("invalid reference: %s"), branch
);
445 printf_ln(_("Preparing worktree (detached HEAD %s)"),
446 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
452 static const char *dwim_branch(const char *path
, const char **new_branch
)
455 const char *s
= worktree_basename(path
, &n
);
456 const char *branchname
= xstrndup(s
, n
);
457 struct strbuf ref
= STRBUF_INIT
;
460 if (!strbuf_check_branch_ref(&ref
, branchname
) &&
461 ref_exists(ref
.buf
)) {
462 strbuf_release(&ref
);
466 *new_branch
= branchname
;
468 struct object_id oid
;
470 unique_tracking_name(*new_branch
, &oid
, NULL
);
476 static int add(int ac
, const char **av
, const char *prefix
)
478 struct add_opts opts
;
479 const char *new_branch_force
= NULL
;
482 const char *new_branch
= NULL
;
483 const char *opt_track
= NULL
;
484 struct option options
[] = {
485 OPT__FORCE(&opts
.force
,
486 N_("checkout <branch> even if already checked out in other worktree"),
487 PARSE_OPT_NOCOMPLETE
),
488 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
489 N_("create a new branch")),
490 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
491 N_("create or reset a branch")),
492 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
493 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
494 OPT_BOOL(0, "lock", &opts
.keep_locked
, N_("keep the new working tree locked")),
495 OPT__QUIET(&opts
.quiet
, N_("suppress progress reporting")),
496 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
497 N_("set up tracking mode (see git-branch(1))"),
498 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
499 OPT_BOOL(0, "guess-remote", &guess_remote
,
500 N_("try to match the new branch name with a remote-tracking branch")),
504 memset(&opts
, 0, sizeof(opts
));
506 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
507 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
508 die(_("-b, -B, and --detach are mutually exclusive"));
509 if (ac
< 1 || ac
> 2)
510 usage_with_options(worktree_usage
, options
);
512 path
= prefix_filename(prefix
, av
[0]);
513 branch
= ac
< 2 ? "HEAD" : av
[1];
515 if (!strcmp(branch
, "-"))
518 if (new_branch_force
) {
519 struct strbuf symref
= STRBUF_INIT
;
521 new_branch
= new_branch_force
;
524 !strbuf_check_branch_ref(&symref
, new_branch
) &&
525 ref_exists(symref
.buf
))
526 die_if_checked_out(symref
.buf
, 0);
527 strbuf_release(&symref
);
530 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
531 const char *s
= dwim_branch(path
, &new_branch
);
536 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
537 struct object_id oid
;
538 struct commit
*commit
;
541 commit
= lookup_commit_reference_by_name(branch
);
543 remote
= unique_tracking_name(branch
, &oid
, NULL
);
551 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
554 struct child_process cp
= CHILD_PROCESS_INIT
;
556 argv_array_push(&cp
.args
, "branch");
557 if (new_branch_force
)
558 argv_array_push(&cp
.args
, "--force");
560 argv_array_push(&cp
.args
, "--quiet");
561 argv_array_push(&cp
.args
, new_branch
);
562 argv_array_push(&cp
.args
, branch
);
564 argv_array_push(&cp
.args
, opt_track
);
565 if (run_command(&cp
))
568 } else if (opt_track
) {
569 die(_("--[no-]track can only be used if a new branch is created"));
574 return add_worktree(path
, branch
, &opts
);
577 static void show_worktree_porcelain(struct worktree
*wt
)
579 printf("worktree %s\n", wt
->path
);
583 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
585 printf("detached\n");
586 else if (wt
->head_ref
)
587 printf("branch %s\n", wt
->head_ref
);
592 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
594 struct strbuf sb
= STRBUF_INIT
;
595 int cur_path_len
= strlen(wt
->path
);
596 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
598 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
600 strbuf_addstr(&sb
, "(bare)");
602 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
603 find_unique_abbrev(&wt
->head_oid
, DEFAULT_ABBREV
));
605 strbuf_addstr(&sb
, "(detached HEAD)");
606 else if (wt
->head_ref
) {
607 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
608 strbuf_addf(&sb
, "[%s]", ref
);
611 strbuf_addstr(&sb
, "(error)");
613 printf("%s\n", sb
.buf
);
618 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
622 for (i
= 0; wt
[i
]; i
++) {
624 int path_len
= strlen(wt
[i
]->path
);
626 if (path_len
> *maxlen
)
628 sha1_len
= strlen(find_unique_abbrev(&wt
[i
]->head_oid
, *abbrev
));
629 if (sha1_len
> *abbrev
)
634 static int list(int ac
, const char **av
, const char *prefix
)
638 struct option options
[] = {
639 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
643 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
645 usage_with_options(worktree_usage
, options
);
647 struct worktree
**worktrees
= get_worktrees(GWT_SORT_LINKED
);
648 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
651 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
653 for (i
= 0; worktrees
[i
]; i
++) {
655 show_worktree_porcelain(worktrees
[i
]);
657 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
659 free_worktrees(worktrees
);
664 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
666 const char *reason
= "", *old_reason
;
667 struct option options
[] = {
668 OPT_STRING(0, "reason", &reason
, N_("string"),
669 N_("reason for locking")),
672 struct worktree
**worktrees
, *wt
;
674 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
676 usage_with_options(worktree_usage
, options
);
678 worktrees
= get_worktrees(0);
679 wt
= find_worktree(worktrees
, prefix
, av
[0]);
681 die(_("'%s' is not a working tree"), av
[0]);
682 if (is_main_worktree(wt
))
683 die(_("The main working tree cannot be locked or unlocked"));
685 old_reason
= worktree_lock_reason(wt
);
688 die(_("'%s' is already locked, reason: %s"),
690 die(_("'%s' is already locked"), av
[0]);
693 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
695 free_worktrees(worktrees
);
699 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
701 struct option options
[] = {
704 struct worktree
**worktrees
, *wt
;
707 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
709 usage_with_options(worktree_usage
, options
);
711 worktrees
= get_worktrees(0);
712 wt
= find_worktree(worktrees
, prefix
, av
[0]);
714 die(_("'%s' is not a working tree"), av
[0]);
715 if (is_main_worktree(wt
))
716 die(_("The main working tree cannot be locked or unlocked"));
717 if (!worktree_lock_reason(wt
))
718 die(_("'%s' is not locked"), av
[0]);
719 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
720 free_worktrees(worktrees
);
724 static void validate_no_submodules(const struct worktree
*wt
)
726 struct index_state istate
= { NULL
};
727 int i
, found_submodules
= 0;
729 if (read_index_from(&istate
, worktree_git_path(wt
, "index"),
730 get_worktree_git_dir(wt
)) > 0) {
731 for (i
= 0; i
< istate
.cache_nr
; i
++) {
732 struct cache_entry
*ce
= istate
.cache
[i
];
734 if (S_ISGITLINK(ce
->ce_mode
)) {
735 found_submodules
= 1;
740 discard_index(&istate
);
742 if (found_submodules
)
743 die(_("working trees containing submodules cannot be moved or removed"));
746 static int move_worktree(int ac
, const char **av
, const char *prefix
)
749 struct option options
[] = {
751 N_("force move even if worktree is dirty or locked"),
752 PARSE_OPT_NOCOMPLETE
),
755 struct worktree
**worktrees
, *wt
;
756 struct strbuf dst
= STRBUF_INIT
;
757 struct strbuf errmsg
= STRBUF_INIT
;
758 const char *reason
= NULL
;
761 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
763 usage_with_options(worktree_usage
, options
);
765 path
= prefix_filename(prefix
, av
[1]);
766 strbuf_addstr(&dst
, path
);
769 worktrees
= get_worktrees(0);
770 wt
= find_worktree(worktrees
, prefix
, av
[0]);
772 die(_("'%s' is not a working tree"), av
[0]);
773 if (is_main_worktree(wt
))
774 die(_("'%s' is a main working tree"), av
[0]);
775 if (is_directory(dst
.buf
)) {
776 const char *sep
= find_last_dir_sep(wt
->path
);
779 die(_("could not figure out destination name from '%s'"),
781 strbuf_trim_trailing_dir_sep(&dst
);
782 strbuf_addstr(&dst
, sep
);
784 if (file_exists(dst
.buf
))
785 die(_("target '%s' already exists"), dst
.buf
);
787 validate_no_submodules(wt
);
790 reason
= worktree_lock_reason(wt
);
793 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
795 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
797 if (validate_worktree(wt
, &errmsg
, 0))
798 die(_("validation failed, cannot move working tree: %s"),
800 strbuf_release(&errmsg
);
802 if (rename(wt
->path
, dst
.buf
) == -1)
803 die_errno(_("failed to move '%s' to '%s'"), wt
->path
, dst
.buf
);
805 update_worktree_location(wt
, dst
.buf
);
807 strbuf_release(&dst
);
808 free_worktrees(worktrees
);
813 * Note, "git status --porcelain" is used to determine if it's safe to
814 * delete a whole worktree. "git status" does not ignore user
815 * configuration, so if a normal "git status" shows "clean" for the
816 * user, then it's ok to remove it.
818 * This assumption may be a bad one. We may want to ignore
819 * (potentially bad) user settings and only delete a worktree when
820 * it's absolutely safe to do so from _our_ point of view because we
823 static void check_clean_worktree(struct worktree
*wt
,
824 const char *original_path
)
826 struct argv_array child_env
= ARGV_ARRAY_INIT
;
827 struct child_process cp
;
832 * Until we sort this out, all submodules are "dirty" and
833 * will abort this function.
835 validate_no_submodules(wt
);
837 argv_array_pushf(&child_env
, "%s=%s/.git",
838 GIT_DIR_ENVIRONMENT
, wt
->path
);
839 argv_array_pushf(&child_env
, "%s=%s",
840 GIT_WORK_TREE_ENVIRONMENT
, wt
->path
);
841 memset(&cp
, 0, sizeof(cp
));
842 argv_array_pushl(&cp
.args
, "status",
843 "--porcelain", "--ignore-submodules=none",
845 cp
.env
= child_env
.argv
;
849 ret
= start_command(&cp
);
851 die_errno(_("failed to run 'git status' on '%s'"),
853 ret
= xread(cp
.out
, buf
, sizeof(buf
));
855 die(_("'%s' is dirty, use --force to delete it"),
858 ret
= finish_command(&cp
);
860 die_errno(_("failed to run 'git status' on '%s', code %d"),
864 static int delete_git_work_tree(struct worktree
*wt
)
866 struct strbuf sb
= STRBUF_INIT
;
869 strbuf_addstr(&sb
, wt
->path
);
870 if (remove_dir_recursively(&sb
, 0)) {
871 error_errno(_("failed to delete '%s'"), sb
.buf
);
878 static int remove_worktree(int ac
, const char **av
, const char *prefix
)
881 struct option options
[] = {
883 N_("force removal even if worktree is dirty or locked"),
884 PARSE_OPT_NOCOMPLETE
),
887 struct worktree
**worktrees
, *wt
;
888 struct strbuf errmsg
= STRBUF_INIT
;
889 const char *reason
= NULL
;
892 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
894 usage_with_options(worktree_usage
, options
);
896 worktrees
= get_worktrees(0);
897 wt
= find_worktree(worktrees
, prefix
, av
[0]);
899 die(_("'%s' is not a working tree"), av
[0]);
900 if (is_main_worktree(wt
))
901 die(_("'%s' is a main working tree"), av
[0]);
903 reason
= worktree_lock_reason(wt
);
906 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
908 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
910 if (validate_worktree(wt
, &errmsg
, WT_VALIDATE_WORKTREE_MISSING_OK
))
911 die(_("validation failed, cannot remove working tree: %s"),
913 strbuf_release(&errmsg
);
915 if (file_exists(wt
->path
)) {
917 check_clean_worktree(wt
, av
[0]);
919 ret
|= delete_git_work_tree(wt
);
922 * continue on even if ret is non-zero, there's no going back
925 ret
|= delete_git_dir(wt
->id
);
926 delete_worktrees_dir_if_empty();
928 free_worktrees(worktrees
);
932 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
934 struct option options
[] = {
938 git_config(git_worktree_config
, NULL
);
941 usage_with_options(worktree_usage
, options
);
944 if (!strcmp(av
[1], "add"))
945 return add(ac
- 1, av
+ 1, prefix
);
946 if (!strcmp(av
[1], "prune"))
947 return prune(ac
- 1, av
+ 1, prefix
);
948 if (!strcmp(av
[1], "list"))
949 return list(ac
- 1, av
+ 1, prefix
);
950 if (!strcmp(av
[1], "lock"))
951 return lock_worktree(ac
- 1, av
+ 1, prefix
);
952 if (!strcmp(av
[1], "unlock"))
953 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
954 if (!strcmp(av
[1], "move"))
955 return move_worktree(ac
- 1, av
+ 1, prefix
);
956 if (!strcmp(av
[1], "remove"))
957 return remove_worktree(ac
- 1, av
+ 1, prefix
);
958 usage_with_options(worktree_usage
, options
);