6 #include "parse-options.h"
7 #include "argv-array.h"
10 #include "run-command.h"
12 #include "submodule.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);
237 wt
= find_worktree_by_path(worktrees
, path
);
241 locked
= !!worktree_lock_reason(wt
);
242 if ((!locked
&& opts
->force
) || (locked
&& opts
->force
> 1)) {
243 if (delete_git_dir(wt
->id
))
244 die(_("unable to re-add worktree '%s'"), path
);
249 die(_("'%s' is a missing but locked worktree;\nuse 'add -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path
);
251 die(_("'%s' is a missing but already registered worktree;\nuse 'add -f' to override, or 'prune' or 'remove' to clear"), path
);
254 free_worktrees(worktrees
);
257 static int add_worktree(const char *path
, const char *refname
,
258 const struct add_opts
*opts
)
260 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
261 struct strbuf sb
= STRBUF_INIT
;
263 struct child_process cp
= CHILD_PROCESS_INIT
;
264 struct argv_array child_env
= ARGV_ARRAY_INIT
;
265 unsigned int counter
= 0;
267 struct strbuf symref
= STRBUF_INIT
;
268 struct commit
*commit
= NULL
;
270 struct strbuf sb_name
= STRBUF_INIT
;
272 validate_worktree_add(path
, opts
);
274 /* is 'refname' a branch or commit? */
275 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
276 ref_exists(symref
.buf
)) {
279 die_if_checked_out(symref
.buf
, 0);
281 commit
= lookup_commit_reference_by_name(refname
);
283 die(_("invalid reference: %s"), refname
);
285 name
= worktree_basename(path
, &len
);
286 strbuf_add(&sb
, name
, path
+ len
- name
);
287 sanitize_refname_component(sb
.buf
, &sb_name
);
289 BUG("How come '%s' becomes empty after sanitization?", sb
.buf
);
292 git_path_buf(&sb_repo
, "worktrees/%s", name
);
294 if (safe_create_leading_directories_const(sb_repo
.buf
))
295 die_errno(_("could not create leading directories of '%s'"),
298 while (mkdir(sb_repo
.buf
, 0777)) {
300 if ((errno
!= EEXIST
) || !counter
/* overflow */)
301 die_errno(_("could not create directory of '%s'"),
303 strbuf_setlen(&sb_repo
, len
);
304 strbuf_addf(&sb_repo
, "%d", counter
);
306 name
= strrchr(sb_repo
.buf
, '/') + 1;
310 sigchain_push_common(remove_junk_on_signal
);
312 junk_git_dir
= xstrdup(sb_repo
.buf
);
316 * lock the incomplete repo so prune won't delete it, unlock
317 * after the preparation is over.
319 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
320 if (!opts
->keep_locked
)
321 write_file(sb
.buf
, "initializing");
323 write_file(sb
.buf
, "added with --lock");
325 strbuf_addf(&sb_git
, "%s/.git", path
);
326 if (safe_create_leading_directories_const(sb_git
.buf
))
327 die_errno(_("could not create leading directories of '%s'"),
329 junk_work_tree
= xstrdup(path
);
332 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
333 write_file(sb
.buf
, "%s", real_path(sb_git
.buf
));
334 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
335 real_path(get_git_common_dir()), name
);
337 * This is to keep resolve_ref() happy. We need a valid HEAD
338 * or is_git_directory() will reject the directory. Any value which
339 * looks like an object ID will do since it will be immediately
340 * replaced by the symbolic-ref or update-ref invocation in the new
344 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
345 write_file(sb
.buf
, "%s", oid_to_hex(&null_oid
));
347 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
348 write_file(sb
.buf
, "../..");
350 argv_array_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
351 argv_array_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
355 argv_array_pushl(&cp
.args
, "update-ref", "HEAD",
356 oid_to_hex(&commit
->object
.oid
), NULL
);
358 argv_array_pushl(&cp
.args
, "symbolic-ref", "HEAD",
361 argv_array_push(&cp
.args
, "--quiet");
364 cp
.env
= child_env
.argv
;
365 ret
= run_command(&cp
);
369 if (opts
->checkout
) {
371 argv_array_clear(&cp
.args
);
372 argv_array_pushl(&cp
.args
, "reset", "--hard", "--no-recurse-submodules", NULL
);
374 argv_array_push(&cp
.args
, "--quiet");
375 cp
.env
= child_env
.argv
;
376 ret
= run_command(&cp
);
382 FREE_AND_NULL(junk_work_tree
);
383 FREE_AND_NULL(junk_git_dir
);
386 if (ret
|| !opts
->keep_locked
) {
388 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
389 unlink_or_warn(sb
.buf
);
393 * Hook failure does not warrant worktree deletion, so run hook after
394 * is_junk is cleared, but do return appropriate code when hook fails.
396 if (!ret
&& opts
->checkout
) {
397 const char *hook
= find_hook("post-checkout");
399 const char *env
[] = { "GIT_DIR", "GIT_WORK_TREE", NULL
};
402 cp
.stdout_to_stderr
= 1;
406 cp
.trace2_hook_name
= "post-checkout";
407 argv_array_pushl(&cp
.args
, absolute_path(hook
),
408 oid_to_hex(&null_oid
),
409 oid_to_hex(&commit
->object
.oid
),
411 ret
= run_command(&cp
);
415 argv_array_clear(&child_env
);
417 strbuf_release(&symref
);
418 strbuf_release(&sb_repo
);
419 strbuf_release(&sb_git
);
420 strbuf_release(&sb_name
);
424 static void print_preparing_worktree_line(int detach
,
426 const char *new_branch
,
427 int force_new_branch
)
429 if (force_new_branch
) {
430 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
432 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
434 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
436 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
437 } else if (new_branch
) {
438 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
440 struct strbuf s
= STRBUF_INIT
;
441 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
443 printf_ln(_("Preparing worktree (checking out '%s')"),
446 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
448 die(_("invalid reference: %s"), branch
);
449 printf_ln(_("Preparing worktree (detached HEAD %s)"),
450 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
456 static const char *dwim_branch(const char *path
, const char **new_branch
)
459 const char *s
= worktree_basename(path
, &n
);
460 const char *branchname
= xstrndup(s
, n
);
461 struct strbuf ref
= STRBUF_INIT
;
464 if (!strbuf_check_branch_ref(&ref
, branchname
) &&
465 ref_exists(ref
.buf
)) {
466 strbuf_release(&ref
);
470 *new_branch
= branchname
;
472 struct object_id oid
;
474 unique_tracking_name(*new_branch
, &oid
, NULL
);
480 static int add(int ac
, const char **av
, const char *prefix
)
482 struct add_opts opts
;
483 const char *new_branch_force
= NULL
;
486 const char *new_branch
= NULL
;
487 const char *opt_track
= NULL
;
488 struct option options
[] = {
489 OPT__FORCE(&opts
.force
,
490 N_("checkout <branch> even if already checked out in other worktree"),
491 PARSE_OPT_NOCOMPLETE
),
492 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
493 N_("create a new branch")),
494 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
495 N_("create or reset a branch")),
496 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
497 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
498 OPT_BOOL(0, "lock", &opts
.keep_locked
, N_("keep the new working tree locked")),
499 OPT__QUIET(&opts
.quiet
, N_("suppress progress reporting")),
500 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
501 N_("set up tracking mode (see git-branch(1))"),
502 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
503 OPT_BOOL(0, "guess-remote", &guess_remote
,
504 N_("try to match the new branch name with a remote-tracking branch")),
508 memset(&opts
, 0, sizeof(opts
));
510 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
511 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
512 die(_("-b, -B, and --detach are mutually exclusive"));
513 if (ac
< 1 || ac
> 2)
514 usage_with_options(worktree_usage
, options
);
516 path
= prefix_filename(prefix
, av
[0]);
517 branch
= ac
< 2 ? "HEAD" : av
[1];
519 if (!strcmp(branch
, "-"))
522 if (new_branch_force
) {
523 struct strbuf symref
= STRBUF_INIT
;
525 new_branch
= new_branch_force
;
528 !strbuf_check_branch_ref(&symref
, new_branch
) &&
529 ref_exists(symref
.buf
))
530 die_if_checked_out(symref
.buf
, 0);
531 strbuf_release(&symref
);
534 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
535 const char *s
= dwim_branch(path
, &new_branch
);
540 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
541 struct object_id oid
;
542 struct commit
*commit
;
545 commit
= lookup_commit_reference_by_name(branch
);
547 remote
= unique_tracking_name(branch
, &oid
, NULL
);
555 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
558 struct child_process cp
= CHILD_PROCESS_INIT
;
560 argv_array_push(&cp
.args
, "branch");
561 if (new_branch_force
)
562 argv_array_push(&cp
.args
, "--force");
564 argv_array_push(&cp
.args
, "--quiet");
565 argv_array_push(&cp
.args
, new_branch
);
566 argv_array_push(&cp
.args
, branch
);
568 argv_array_push(&cp
.args
, opt_track
);
569 if (run_command(&cp
))
572 } else if (opt_track
) {
573 die(_("--[no-]track can only be used if a new branch is created"));
578 return add_worktree(path
, branch
, &opts
);
581 static void show_worktree_porcelain(struct worktree
*wt
)
583 printf("worktree %s\n", wt
->path
);
587 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
589 printf("detached\n");
590 else if (wt
->head_ref
)
591 printf("branch %s\n", wt
->head_ref
);
596 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
598 struct strbuf sb
= STRBUF_INIT
;
599 int cur_path_len
= strlen(wt
->path
);
600 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
602 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
604 strbuf_addstr(&sb
, "(bare)");
606 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
607 find_unique_abbrev(&wt
->head_oid
, DEFAULT_ABBREV
));
609 strbuf_addstr(&sb
, "(detached HEAD)");
610 else if (wt
->head_ref
) {
611 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
612 strbuf_addf(&sb
, "[%s]", ref
);
615 strbuf_addstr(&sb
, "(error)");
617 printf("%s\n", sb
.buf
);
622 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
626 for (i
= 0; wt
[i
]; i
++) {
628 int path_len
= strlen(wt
[i
]->path
);
630 if (path_len
> *maxlen
)
632 sha1_len
= strlen(find_unique_abbrev(&wt
[i
]->head_oid
, *abbrev
));
633 if (sha1_len
> *abbrev
)
638 static int list(int ac
, const char **av
, const char *prefix
)
642 struct option options
[] = {
643 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
647 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
649 usage_with_options(worktree_usage
, options
);
651 struct worktree
**worktrees
= get_worktrees(GWT_SORT_LINKED
);
652 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
655 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
657 for (i
= 0; worktrees
[i
]; i
++) {
659 show_worktree_porcelain(worktrees
[i
]);
661 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
663 free_worktrees(worktrees
);
668 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
670 const char *reason
= "", *old_reason
;
671 struct option options
[] = {
672 OPT_STRING(0, "reason", &reason
, N_("string"),
673 N_("reason for locking")),
676 struct worktree
**worktrees
, *wt
;
678 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
680 usage_with_options(worktree_usage
, options
);
682 worktrees
= get_worktrees(0);
683 wt
= find_worktree(worktrees
, prefix
, av
[0]);
685 die(_("'%s' is not a working tree"), av
[0]);
686 if (is_main_worktree(wt
))
687 die(_("The main working tree cannot be locked or unlocked"));
689 old_reason
= worktree_lock_reason(wt
);
692 die(_("'%s' is already locked, reason: %s"),
694 die(_("'%s' is already locked"), av
[0]);
697 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
699 free_worktrees(worktrees
);
703 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
705 struct option options
[] = {
708 struct worktree
**worktrees
, *wt
;
711 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
713 usage_with_options(worktree_usage
, options
);
715 worktrees
= get_worktrees(0);
716 wt
= find_worktree(worktrees
, prefix
, av
[0]);
718 die(_("'%s' is not a working tree"), av
[0]);
719 if (is_main_worktree(wt
))
720 die(_("The main working tree cannot be locked or unlocked"));
721 if (!worktree_lock_reason(wt
))
722 die(_("'%s' is not locked"), av
[0]);
723 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
724 free_worktrees(worktrees
);
728 static void validate_no_submodules(const struct worktree
*wt
)
730 struct index_state istate
= { NULL
};
731 struct strbuf path
= STRBUF_INIT
;
732 int i
, found_submodules
= 0;
734 if (is_directory(worktree_git_path(wt
, "modules"))) {
736 * There could be false positives, e.g. the "modules"
737 * directory exists but is empty. But it's a rare case and
738 * this simpler check is probably good enough for now.
740 found_submodules
= 1;
741 } else if (read_index_from(&istate
, worktree_git_path(wt
, "index"),
742 get_worktree_git_dir(wt
)) > 0) {
743 for (i
= 0; i
< istate
.cache_nr
; i
++) {
744 struct cache_entry
*ce
= istate
.cache
[i
];
747 if (!S_ISGITLINK(ce
->ce_mode
))
751 strbuf_addf(&path
, "%s/%s", wt
->path
, ce
->name
);
752 if (!is_submodule_populated_gently(path
.buf
, &err
))
755 found_submodules
= 1;
759 discard_index(&istate
);
760 strbuf_release(&path
);
762 if (found_submodules
)
763 die(_("working trees containing submodules cannot be moved or removed"));
766 static int move_worktree(int ac
, const char **av
, const char *prefix
)
769 struct option options
[] = {
771 N_("force move even if worktree is dirty or locked"),
772 PARSE_OPT_NOCOMPLETE
),
775 struct worktree
**worktrees
, *wt
;
776 struct strbuf dst
= STRBUF_INIT
;
777 struct strbuf errmsg
= STRBUF_INIT
;
778 const char *reason
= NULL
;
781 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
783 usage_with_options(worktree_usage
, options
);
785 path
= prefix_filename(prefix
, av
[1]);
786 strbuf_addstr(&dst
, path
);
789 worktrees
= get_worktrees(0);
790 wt
= find_worktree(worktrees
, prefix
, av
[0]);
792 die(_("'%s' is not a working tree"), av
[0]);
793 if (is_main_worktree(wt
))
794 die(_("'%s' is a main working tree"), av
[0]);
795 if (is_directory(dst
.buf
)) {
796 const char *sep
= find_last_dir_sep(wt
->path
);
799 die(_("could not figure out destination name from '%s'"),
801 strbuf_trim_trailing_dir_sep(&dst
);
802 strbuf_addstr(&dst
, sep
);
804 if (file_exists(dst
.buf
))
805 die(_("target '%s' already exists"), dst
.buf
);
807 validate_no_submodules(wt
);
810 reason
= worktree_lock_reason(wt
);
813 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
815 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
817 if (validate_worktree(wt
, &errmsg
, 0))
818 die(_("validation failed, cannot move working tree: %s"),
820 strbuf_release(&errmsg
);
822 if (rename(wt
->path
, dst
.buf
) == -1)
823 die_errno(_("failed to move '%s' to '%s'"), wt
->path
, dst
.buf
);
825 update_worktree_location(wt
, dst
.buf
);
827 strbuf_release(&dst
);
828 free_worktrees(worktrees
);
833 * Note, "git status --porcelain" is used to determine if it's safe to
834 * delete a whole worktree. "git status" does not ignore user
835 * configuration, so if a normal "git status" shows "clean" for the
836 * user, then it's ok to remove it.
838 * This assumption may be a bad one. We may want to ignore
839 * (potentially bad) user settings and only delete a worktree when
840 * it's absolutely safe to do so from _our_ point of view because we
843 static void check_clean_worktree(struct worktree
*wt
,
844 const char *original_path
)
846 struct argv_array child_env
= ARGV_ARRAY_INIT
;
847 struct child_process cp
;
852 * Until we sort this out, all submodules are "dirty" and
853 * will abort this function.
855 validate_no_submodules(wt
);
857 argv_array_pushf(&child_env
, "%s=%s/.git",
858 GIT_DIR_ENVIRONMENT
, wt
->path
);
859 argv_array_pushf(&child_env
, "%s=%s",
860 GIT_WORK_TREE_ENVIRONMENT
, wt
->path
);
861 memset(&cp
, 0, sizeof(cp
));
862 argv_array_pushl(&cp
.args
, "status",
863 "--porcelain", "--ignore-submodules=none",
865 cp
.env
= child_env
.argv
;
869 ret
= start_command(&cp
);
871 die_errno(_("failed to run 'git status' on '%s'"),
873 ret
= xread(cp
.out
, buf
, sizeof(buf
));
875 die(_("'%s' contains modified or untracked files, use --force to delete it"),
878 ret
= finish_command(&cp
);
880 die_errno(_("failed to run 'git status' on '%s', code %d"),
884 static int delete_git_work_tree(struct worktree
*wt
)
886 struct strbuf sb
= STRBUF_INIT
;
889 strbuf_addstr(&sb
, wt
->path
);
890 if (remove_dir_recursively(&sb
, 0)) {
891 error_errno(_("failed to delete '%s'"), sb
.buf
);
898 static int remove_worktree(int ac
, const char **av
, const char *prefix
)
901 struct option options
[] = {
903 N_("force removal even if worktree is dirty or locked"),
904 PARSE_OPT_NOCOMPLETE
),
907 struct worktree
**worktrees
, *wt
;
908 struct strbuf errmsg
= STRBUF_INIT
;
909 const char *reason
= NULL
;
912 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
914 usage_with_options(worktree_usage
, options
);
916 worktrees
= get_worktrees(0);
917 wt
= find_worktree(worktrees
, prefix
, av
[0]);
919 die(_("'%s' is not a working tree"), av
[0]);
920 if (is_main_worktree(wt
))
921 die(_("'%s' is a main working tree"), av
[0]);
923 reason
= worktree_lock_reason(wt
);
926 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
928 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
930 if (validate_worktree(wt
, &errmsg
, WT_VALIDATE_WORKTREE_MISSING_OK
))
931 die(_("validation failed, cannot remove working tree: %s"),
933 strbuf_release(&errmsg
);
935 if (file_exists(wt
->path
)) {
937 check_clean_worktree(wt
, av
[0]);
939 ret
|= delete_git_work_tree(wt
);
942 * continue on even if ret is non-zero, there's no going back
945 ret
|= delete_git_dir(wt
->id
);
946 delete_worktrees_dir_if_empty();
948 free_worktrees(worktrees
);
952 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
954 struct option options
[] = {
958 git_config(git_worktree_config
, NULL
);
961 usage_with_options(worktree_usage
, options
);
964 if (!strcmp(av
[1], "add"))
965 return add(ac
- 1, av
+ 1, prefix
);
966 if (!strcmp(av
[1], "prune"))
967 return prune(ac
- 1, av
+ 1, prefix
);
968 if (!strcmp(av
[1], "list"))
969 return list(ac
- 1, av
+ 1, prefix
);
970 if (!strcmp(av
[1], "lock"))
971 return lock_worktree(ac
- 1, av
+ 1, prefix
);
972 if (!strcmp(av
[1], "unlock"))
973 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
974 if (!strcmp(av
[1], "move"))
975 return move_worktree(ac
- 1, av
+ 1, prefix
);
976 if (!strcmp(av
[1], "remove"))
977 return remove_worktree(ac
- 1, av
+ 1, prefix
);
978 usage_with_options(worktree_usage
, options
);