6 #include "parse-options.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 */
71 * Return true if worktree entry should be pruned, along with the reason for
72 * pruning. Otherwise, return false and the worktree's path, or NULL if it
73 * cannot be determined. Caller is responsible for freeing returned path.
75 static int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath)
84 if (!is_directory(git_path("worktrees/%s", id))) {
85 strbuf_addstr(reason, _("not a valid directory"));
88 if (file_exists(git_path("worktrees/%s/locked", id)))
90 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
91 strbuf_addstr(reason, _("gitdir file does not exist"));
94 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
96 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
100 len = xsize_t(st.st_size);
101 path = xmallocz(len);
103 read_result = read_in_full(fd, path, len);
104 if (read_result < 0) {
105 strbuf_addf(reason, _("unable to read gitdir file (%s)"),
113 if (read_result != len) {
115 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
116 (uintmax_t)len, (uintmax_t)read_result);
120 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
123 strbuf_addstr(reason, _("invalid gitdir file"));
128 if (!file_exists(path)) {
129 if (stat(git_path("worktrees/%s/index", id), &st) ||
130 st.st_mtime <= expire) {
131 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
143 static void prune_worktree(const char *id, const char *reason)
145 if (show_only || verbose)
146 printf_ln(_("Removing %s/%s: %s"), "worktrees", id, reason);
151 static int prune_cmp(const void *a, const void *b)
153 const struct string_list_item *x = a;
154 const struct string_list_item *y = b;
157 if ((c = fspathcmp(x->string, y->string)))
160 * paths same; prune_dupes() removes all but the first worktree entry
161 * having the same path, so sort main worktree ('util' is NULL) above
162 * linked worktrees ('util' not NULL) since main worktree can't be
169 /* paths same; sort by .git/worktrees/<id> */
170 return strcmp(x->util, y->util);
173 static void prune_dups(struct string_list *l)
177 QSORT(l->items, l->nr, prune_cmp);
178 for (i = 1; i < l->nr; i++) {
179 if (!fspathcmp(l->items[i].string, l->items[i - 1].string))
180 prune_worktree(l->items[i].util, "duplicate entry");
184 static void prune_worktrees(void)
186 struct strbuf reason = STRBUF_INIT;
187 struct strbuf main_path = STRBUF_INIT;
188 struct string_list kept = STRING_LIST_INIT_NODUP;
189 DIR *dir = opendir(git_path("worktrees"));
193 while ((d = readdir(dir)) != NULL) {
195 if (is_dot_or_dotdot(d->d_name))
197 strbuf_reset(&reason);
198 if (should_prune_worktree(d->d_name, &reason, &path))
199 prune_worktree(d->d_name, reason.buf);
201 string_list_append(&kept, path)->util = xstrdup(d->d_name);
205 strbuf_add_absolute_path(&main_path, get_git_common_dir());
206 /* massage main worktree absolute path to match 'gitdir' content */
207 strbuf_strip_suffix(&main_path, "/.");
208 string_list_append(&kept, strbuf_detach(&main_path, NULL));
210 string_list_clear(&kept, 1);
213 delete_worktrees_dir_if_empty();
214 strbuf_release(&reason);
217 static int prune(int ac, const char **av, const char *prefix)
219 struct option options[] = {
220 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
221 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
222 OPT_EXPIRY_DATE(0, "expire", &expire,
223 N_("expire working trees older than <time>")),
228 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
230 usage_with_options(worktree_usage, options);
235 static char *junk_work_tree;
236 static char *junk_git_dir;
238 static pid_t junk_pid;
240 static void remove_junk(void)
242 struct strbuf sb = STRBUF_INIT;
243 if (!is_junk || getpid() != junk_pid)
246 strbuf_addstr(&sb, junk_git_dir);
247 remove_dir_recursively(&sb, 0);
250 if (junk_work_tree) {
251 strbuf_addstr(&sb, junk_work_tree);
252 remove_dir_recursively(&sb, 0);
257 static void remove_junk_on_signal(int signo)
264 static const char *worktree_basename(const char *path, int *olen)
270 while (len && is_dir_sep(path[len - 1]))
273 for (name = path + len - 1; name > path; name--)
274 if (is_dir_sep(*name)) {
283 /* check that path is viable location for worktree */
284 static void check_candidate_path(const char *path,
286 struct worktree **worktrees,
292 if (file_exists(path) && !is_empty_dir(path))
293 die(_("'%s' already exists"), path);
295 wt = find_worktree_by_path(worktrees, path);
299 locked = !!worktree_lock_reason(wt);
300 if ((!locked && force) || (locked && force > 1)) {
301 if (delete_git_dir(wt->id))
302 die(_("unusable worktree destination '%s'"), path);
307 die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), cmd, path);
309 die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), cmd, path);
312 static int add_worktree(const char *path, const char *refname,
313 const struct add_opts *opts)
315 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
316 struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT;
318 struct child_process cp = CHILD_PROCESS_INIT;
319 struct strvec child_env = STRVEC_INIT;
320 unsigned int counter = 0;
322 struct strbuf symref = STRBUF_INIT;
323 struct commit *commit = NULL;
325 struct strbuf sb_name = STRBUF_INIT;
326 struct worktree **worktrees;
328 worktrees = get_worktrees();
329 check_candidate_path(path, opts->force, worktrees, "add");
330 free_worktrees(worktrees);
333 /* is 'refname' a branch or commit? */
334 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
335 ref_exists(symref.buf)) {
338 die_if_checked_out(symref.buf, 0);
340 commit = lookup_commit_reference_by_name(refname);
342 die(_("invalid reference: %s"), refname);
344 name = worktree_basename(path, &len);
345 strbuf_add(&sb, name, path + len - name);
346 sanitize_refname_component(sb.buf, &sb_name);
348 BUG("How come '%s' becomes empty after sanitization?", sb.buf);
351 git_path_buf(&sb_repo, "worktrees/%s", name);
353 if (safe_create_leading_directories_const(sb_repo.buf))
354 die_errno(_("could not create leading directories of '%s'"),
357 while (mkdir(sb_repo.buf, 0777)) {
359 if ((errno != EEXIST) || !counter /* overflow */)
360 die_errno(_("could not create directory of '%s'"),
362 strbuf_setlen(&sb_repo, len);
363 strbuf_addf(&sb_repo, "%d", counter);
365 name = strrchr(sb_repo.buf, '/') + 1;
369 sigchain_push_common(remove_junk_on_signal);
371 junk_git_dir = xstrdup(sb_repo.buf);
375 * lock the incomplete repo so prune won't delete it, unlock
376 * after the preparation is over.
378 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
379 if (!opts->keep_locked)
380 write_file(sb.buf, "initializing");
382 write_file(sb.buf, "added with --lock");
384 strbuf_addf(&sb_git, "%s/.git", path);
385 if (safe_create_leading_directories_const(sb_git.buf))
386 die_errno(_("could not create leading directories of '%s'"),
388 junk_work_tree = xstrdup(path);
391 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
392 strbuf_realpath(&realpath, sb_git.buf, 1);
393 write_file(sb.buf, "%s", realpath.buf);
394 strbuf_realpath(&realpath, get_git_common_dir(), 1);
395 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
398 * This is to keep resolve_ref() happy. We need a valid HEAD
399 * or is_git_directory() will reject the directory. Any value which
400 * looks like an object ID will do since it will be immediately
401 * replaced by the symbolic-ref or update-ref invocation in the new
405 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
406 write_file(sb.buf, "%s", oid_to_hex(&null_oid));
408 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
409 write_file(sb.buf, "../..");
411 strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
412 strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
416 strvec_pushl(&cp.args, "update-ref", "HEAD",
417 oid_to_hex(&commit->object.oid), NULL);
419 strvec_pushl(&cp.args, "symbolic-ref", "HEAD",
422 strvec_push(&cp.args, "--quiet");
425 cp.env = child_env.v;
426 ret = run_command(&cp);
430 if (opts->checkout) {
432 strvec_clear(&cp.args);
433 strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
435 strvec_push(&cp.args, "--quiet");
436 cp.env = child_env.v;
437 ret = run_command(&cp);
443 FREE_AND_NULL(junk_work_tree);
444 FREE_AND_NULL(junk_git_dir);
447 if (ret || !opts->keep_locked) {
449 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
450 unlink_or_warn(sb.buf);
454 * Hook failure does not warrant worktree deletion, so run hook after
455 * is_junk is cleared, but do return appropriate code when hook fails.
457 if (!ret && opts->checkout) {
458 const char *hook = find_hook("post-checkout");
460 const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL };
463 cp.stdout_to_stderr = 1;
467 cp.trace2_hook_name = "post-checkout";
468 strvec_pushl(&cp.args, absolute_path(hook),
469 oid_to_hex(&null_oid),
470 oid_to_hex(&commit->object.oid),
472 ret = run_command(&cp);
476 strvec_clear(&child_env);
478 strbuf_release(&symref);
479 strbuf_release(&sb_repo);
480 strbuf_release(&sb_git);
481 strbuf_release(&sb_name);
482 strbuf_release(&realpath);
486 static void print_preparing_worktree_line(int detach,
488 const char *new_branch,
489 int force_new_branch)
491 if (force_new_branch) {
492 struct commit *commit = lookup_commit_reference_by_name(new_branch);
494 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
496 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
498 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
499 } else if (new_branch) {
500 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
502 struct strbuf s = STRBUF_INIT;
503 if (!detach && !strbuf_check_branch_ref(&s, branch) &&
505 printf_ln(_("Preparing worktree (checking out '%s')"),
508 struct commit *commit = lookup_commit_reference_by_name(branch);
510 die(_("invalid reference: %s"), branch);
511 printf_ln(_("Preparing worktree (detached HEAD %s)"),
512 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
518 static const char *dwim_branch(const char *path, const char **new_branch)
521 const char *s = worktree_basename(path, &n);
522 const char *branchname = xstrndup(s, n);
523 struct strbuf ref = STRBUF_INIT;
526 if (!strbuf_check_branch_ref(&ref, branchname) &&
527 ref_exists(ref.buf)) {
528 strbuf_release(&ref);
532 *new_branch = branchname;
534 struct object_id oid;
536 unique_tracking_name(*new_branch, &oid, NULL);
542 static int add(int ac, const char **av, const char *prefix)
544 struct add_opts opts;
545 const char *new_branch_force = NULL;
548 const char *new_branch = NULL;
549 const char *opt_track = NULL;
550 struct option options[] = {
551 OPT__FORCE(&opts.force,
552 N_("checkout <branch> even if already checked out in other worktree"),
553 PARSE_OPT_NOCOMPLETE),
554 OPT_STRING('b', NULL, &new_branch, N_("branch"),
555 N_("create a new branch")),
556 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
557 N_("create or reset a branch")),
558 OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")),
559 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
560 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
561 OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
562 OPT_PASSTHRU(0, "track", &opt_track, NULL,
563 N_("set up tracking mode (see git-branch(1))"),
564 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
565 OPT_BOOL(0, "guess-remote", &guess_remote,
566 N_("try to match the new branch name with a remote-tracking branch")),
570 memset(&opts, 0, sizeof(opts));
572 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
573 if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
574 die(_("-b, -B, and --detach are mutually exclusive"));
575 if (ac < 1 || ac > 2)
576 usage_with_options(worktree_usage, options);
578 path = prefix_filename(prefix, av[0]);
579 branch = ac < 2 ? "HEAD" : av[1];
581 if (!strcmp(branch, "-"))
584 if (new_branch_force) {
585 struct strbuf symref = STRBUF_INIT;
587 new_branch = new_branch_force;
590 !strbuf_check_branch_ref(&symref, new_branch) &&
591 ref_exists(symref.buf))
592 die_if_checked_out(symref.buf, 0);
593 strbuf_release(&symref);
596 if (ac < 2 && !new_branch && !opts.detach) {
597 const char *s = dwim_branch(path, &new_branch);
602 if (ac == 2 && !new_branch && !opts.detach) {
603 struct object_id oid;
604 struct commit *commit;
607 commit = lookup_commit_reference_by_name(branch);
609 remote = unique_tracking_name(branch, &oid, NULL);
617 print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
620 struct child_process cp = CHILD_PROCESS_INIT;
622 strvec_push(&cp.args, "branch");
623 if (new_branch_force)
624 strvec_push(&cp.args, "--force");
626 strvec_push(&cp.args, "--quiet");
627 strvec_push(&cp.args, new_branch);
628 strvec_push(&cp.args, branch);
630 strvec_push(&cp.args, opt_track);
631 if (run_command(&cp))
634 } else if (opt_track) {
635 die(_("--[no-]track can only be used if a new branch is created"));
640 return add_worktree(path, branch, &opts);
643 static void show_worktree_porcelain(struct worktree *wt)
645 printf("worktree %s\n", wt->path);
649 printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
651 printf("detached\n");
652 else if (wt->head_ref)
653 printf("branch %s\n", wt->head_ref);
658 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
660 struct strbuf sb = STRBUF_INIT;
661 int cur_path_len = strlen(wt->path);
662 int path_adj = cur_path_len - utf8_strwidth(wt->path);
664 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
666 strbuf_addstr(&sb, "(bare)");
668 strbuf_addf(&sb, "%-*s ", abbrev_len,
669 find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV));
671 strbuf_addstr(&sb, "(detached HEAD)");
672 else if (wt->head_ref) {
673 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
674 strbuf_addf(&sb, "[%s]", ref);
677 strbuf_addstr(&sb, "(error)");
679 printf("%s\n", sb.buf);
684 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
688 for (i = 0; wt[i]; i++) {
690 int path_len = strlen(wt[i]->path);
692 if (path_len > *maxlen)
694 sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev));
695 if (sha1_len > *abbrev)
700 static int pathcmp(const void *a_, const void *b_)
702 const struct worktree *const *a = a_;
703 const struct worktree *const *b = b_;
704 return fspathcmp((*a)->path, (*b)->path);
707 static void pathsort(struct worktree **wt)
710 struct worktree **p = wt;
714 QSORT(wt, n, pathcmp);
717 static int list(int ac, const char **av, const char *prefix)
721 struct option options[] = {
722 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
726 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
728 usage_with_options(worktree_usage, options);
730 struct worktree **worktrees = get_worktrees();
731 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
733 /* sort worktrees by path but keep main worktree at top */
734 pathsort(worktrees + 1);
737 measure_widths(worktrees, &abbrev, &path_maxlen);
739 for (i = 0; worktrees[i]; i++) {
741 show_worktree_porcelain(worktrees[i]);
743 show_worktree(worktrees[i], path_maxlen, abbrev);
745 free_worktrees(worktrees);
750 static int lock_worktree(int ac, const char **av, const char *prefix)
752 const char *reason = "", *old_reason;
753 struct option options[] = {
754 OPT_STRING(0, "reason", &reason, N_("string"),
755 N_("reason for locking")),
758 struct worktree **worktrees, *wt;
760 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
762 usage_with_options(worktree_usage, options);
764 worktrees = get_worktrees();
765 wt = find_worktree(worktrees, prefix, av[0]);
767 die(_("'%s' is not a working tree"), av[0]);
768 if (is_main_worktree(wt))
769 die(_("The main working tree cannot be locked or unlocked"));
771 old_reason = worktree_lock_reason(wt);
774 die(_("'%s' is already locked, reason: %s"),
776 die(_("'%s' is already locked"), av[0]);
779 write_file(git_common_path("worktrees/%s/locked", wt->id),
781 free_worktrees(worktrees);
785 static int unlock_worktree(int ac, const char **av, const char *prefix)
787 struct option options[] = {
790 struct worktree **worktrees, *wt;
793 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
795 usage_with_options(worktree_usage, options);
797 worktrees = get_worktrees();
798 wt = find_worktree(worktrees, prefix, av[0]);
800 die(_("'%s' is not a working tree"), av[0]);
801 if (is_main_worktree(wt))
802 die(_("The main working tree cannot be locked or unlocked"));
803 if (!worktree_lock_reason(wt))
804 die(_("'%s' is not locked"), av[0]);
805 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
806 free_worktrees(worktrees);
810 static void validate_no_submodules(const struct worktree *wt)
812 struct index_state istate = { NULL };
813 struct strbuf path = STRBUF_INIT;
814 int i, found_submodules = 0;
816 if (is_directory(worktree_git_path(wt, "modules"))) {
818 * There could be false positives, e.g. the "modules"
819 * directory exists but is empty. But it's a rare case and
820 * this simpler check is probably good enough for now.
822 found_submodules = 1;
823 } else if (read_index_from(&istate, worktree_git_path(wt, "index"),
824 get_worktree_git_dir(wt)) > 0) {
825 for (i = 0; i < istate.cache_nr; i++) {
826 struct cache_entry *ce = istate.cache[i];
829 if (!S_ISGITLINK(ce->ce_mode))
833 strbuf_addf(&path, "%s/%s", wt->path, ce->name);
834 if (!is_submodule_populated_gently(path.buf, &err))
837 found_submodules = 1;
841 discard_index(&istate);
842 strbuf_release(&path);
844 if (found_submodules)
845 die(_("working trees containing submodules cannot be moved or removed"));
848 static int move_worktree(int ac, const char **av, const char *prefix)
851 struct option options[] = {
853 N_("force move even if worktree is dirty or locked"),
854 PARSE_OPT_NOCOMPLETE),
857 struct worktree **worktrees, *wt;
858 struct strbuf dst = STRBUF_INIT;
859 struct strbuf errmsg = STRBUF_INIT;
860 const char *reason = NULL;
863 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
865 usage_with_options(worktree_usage, options);
867 path = prefix_filename(prefix, av[1]);
868 strbuf_addstr(&dst, path);
871 worktrees = get_worktrees();
872 wt = find_worktree(worktrees, prefix, av[0]);
874 die(_("'%s' is not a working tree"), av[0]);
875 if (is_main_worktree(wt))
876 die(_("'%s' is a main working tree"), av[0]);
877 if (is_directory(dst.buf)) {
878 const char *sep = find_last_dir_sep(wt->path);
881 die(_("could not figure out destination name from '%s'"),
883 strbuf_trim_trailing_dir_sep(&dst);
884 strbuf_addstr(&dst, sep);
886 check_candidate_path(dst.buf, force, worktrees, "move");
888 validate_no_submodules(wt);
891 reason = worktree_lock_reason(wt);
894 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
896 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
898 if (validate_worktree(wt, &errmsg, 0))
899 die(_("validation failed, cannot move working tree: %s"),
901 strbuf_release(&errmsg);
903 if (rename(wt->path, dst.buf) == -1)
904 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
906 update_worktree_location(wt, dst.buf);
908 strbuf_release(&dst);
909 free_worktrees(worktrees);
914 * Note, "git status --porcelain" is used to determine if it's safe to
915 * delete a whole worktree. "git status" does not ignore user
916 * configuration, so if a normal "git status" shows "clean" for the
917 * user, then it's ok to remove it.
919 * This assumption may be a bad one. We may want to ignore
920 * (potentially bad) user settings and only delete a worktree when
921 * it's absolutely safe to do so from _our_ point of view because we
924 static void check_clean_worktree(struct worktree *wt,
925 const char *original_path)
927 struct child_process cp;
932 * Until we sort this out, all submodules are "dirty" and
933 * will abort this function.
935 validate_no_submodules(wt);
937 child_process_init(&cp);
938 strvec_pushf(&cp.env_array, "%s=%s/.git",
939 GIT_DIR_ENVIRONMENT, wt->path);
940 strvec_pushf(&cp.env_array, "%s=%s",
941 GIT_WORK_TREE_ENVIRONMENT, wt->path);
942 strvec_pushl(&cp.args, "status",
943 "--porcelain", "--ignore-submodules=none",
948 ret = start_command(&cp);
950 die_errno(_("failed to run 'git status' on '%s'"),
952 ret = xread(cp.out, buf, sizeof(buf));
954 die(_("'%s' contains modified or untracked files, use --force to delete it"),
957 ret = finish_command(&cp);
959 die_errno(_("failed to run 'git status' on '%s', code %d"),
963 static int delete_git_work_tree(struct worktree *wt)
965 struct strbuf sb = STRBUF_INIT;
968 strbuf_addstr(&sb, wt->path);
969 if (remove_dir_recursively(&sb, 0)) {
970 error_errno(_("failed to delete '%s'"), sb.buf);
977 static int remove_worktree(int ac, const char **av, const char *prefix)
980 struct option options[] = {
982 N_("force removal even if worktree is dirty or locked"),
983 PARSE_OPT_NOCOMPLETE),
986 struct worktree **worktrees, *wt;
987 struct strbuf errmsg = STRBUF_INIT;
988 const char *reason = NULL;
991 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
993 usage_with_options(worktree_usage, options);
995 worktrees = get_worktrees();
996 wt = find_worktree(worktrees, prefix, av[0]);
998 die(_("'%s' is not a working tree"), av[0]);
999 if (is_main_worktree(wt))
1000 die(_("'%s' is a main working tree"), av[0]);
1002 reason = worktree_lock_reason(wt);
1005 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
1007 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
1009 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
1010 die(_("validation failed, cannot remove working tree: %s"),
1012 strbuf_release(&errmsg);
1014 if (file_exists(wt->path)) {
1016 check_clean_worktree(wt, av[0]);
1018 ret |= delete_git_work_tree(wt);
1021 * continue on even if ret is non-zero, there's no going back
1024 ret |= delete_git_dir(wt->id);
1025 delete_worktrees_dir_if_empty();
1027 free_worktrees(worktrees);
1031 static void report_repair(int iserr, const char *path, const char *msg, void *cb_data)
1034 printf_ln(_("repair: %s: %s"), msg, path);
1036 int *exit_status = (int *)cb_data;
1037 fprintf_ln(stderr, _("error: %s: %s"), msg, path);
1042 static int repair(int ac, const char **av, const char *prefix)
1045 const char *self[] = { ".", NULL };
1046 struct option options[] = {
1051 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1052 repair_worktrees(report_repair, &rc);
1053 p = ac > 0 ? av : self;
1055 repair_worktree_at_path(*p, report_repair, &rc);
1059 int cmd_worktree(int ac, const char **av, const char *prefix)
1061 struct option options[] = {
1065 git_config(git_worktree_config, NULL);
1068 usage_with_options(worktree_usage, options);
1071 if (!strcmp(av[1], "add"))
1072 return add(ac - 1, av + 1, prefix);
1073 if (!strcmp(av[1], "prune"))
1074 return prune(ac - 1, av + 1, prefix);
1075 if (!strcmp(av[1], "list"))
1076 return list(ac - 1, av + 1, prefix);
1077 if (!strcmp(av[1], "lock"))
1078 return lock_worktree(ac - 1, av + 1, prefix);
1079 if (!strcmp(av[1], "unlock"))
1080 return unlock_worktree(ac - 1, av + 1, prefix);
1081 if (!strcmp(av[1], "move"))
1082 return move_worktree(ac - 1, av + 1, prefix);
1083 if (!strcmp(av[1], "remove"))
1084 return remove_worktree(ac - 1, av + 1, prefix);
1085 if (!strcmp(av[1], "repair"))
1086 return repair(ac - 1, av + 1, prefix);
1087 usage_with_options(worktree_usage, options);