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);
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;
270 struct child_process cp = CHILD_PROCESS_INIT;
271 struct argv_array child_env = ARGV_ARRAY_INIT;
272 unsigned int counter = 0;
274 struct strbuf symref = STRBUF_INIT;
275 struct commit *commit = NULL;
277 struct strbuf sb_name = STRBUF_INIT;
279 validate_worktree_add(path, opts);
281 /* is 'refname' a branch or commit? */
282 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
283 ref_exists(symref.buf)) {
286 die_if_checked_out(symref.buf, 0);
288 commit = lookup_commit_reference_by_name(refname);
290 die(_("invalid reference: %s"), refname);
292 name = worktree_basename(path, &len);
293 strbuf_add(&sb, name, path + len - name);
294 sanitize_refname_component(sb.buf, &sb_name);
296 BUG("How come '%s' becomes empty after sanitization?", sb.buf);
299 git_path_buf(&sb_repo, "worktrees/%s", name);
301 if (safe_create_leading_directories_const(sb_repo.buf))
302 die_errno(_("could not create leading directories of '%s'"),
305 while (mkdir(sb_repo.buf, 0777)) {
307 if ((errno != EEXIST) || !counter /* overflow */)
308 die_errno(_("could not create directory of '%s'"),
310 strbuf_setlen(&sb_repo, len);
311 strbuf_addf(&sb_repo, "%d", counter);
313 name = strrchr(sb_repo.buf, '/') + 1;
317 sigchain_push_common(remove_junk_on_signal);
319 junk_git_dir = xstrdup(sb_repo.buf);
323 * lock the incomplete repo so prune won't delete it, unlock
324 * after the preparation is over.
326 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
327 if (!opts->keep_locked)
328 write_file(sb.buf, "initializing");
330 write_file(sb.buf, "added with --lock");
332 strbuf_addf(&sb_git, "%s/.git", path);
333 if (safe_create_leading_directories_const(sb_git.buf))
334 die_errno(_("could not create leading directories of '%s'"),
336 junk_work_tree = xstrdup(path);
339 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
340 write_file(sb.buf, "%s", real_path(sb_git.buf));
341 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
342 real_path(get_git_common_dir()), name);
344 * This is to keep resolve_ref() happy. We need a valid HEAD
345 * or is_git_directory() will reject the directory. Any value which
346 * looks like an object ID will do since it will be immediately
347 * replaced by the symbolic-ref or update-ref invocation in the new
351 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
352 write_file(sb.buf, "%s", oid_to_hex(&null_oid));
354 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
355 write_file(sb.buf, "../..");
357 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
358 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
362 argv_array_pushl(&cp.args, "update-ref", "HEAD",
363 oid_to_hex(&commit->object.oid), NULL);
365 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
368 argv_array_push(&cp.args, "--quiet");
371 cp.env = child_env.argv;
372 ret = run_command(&cp);
376 if (opts->checkout) {
378 argv_array_clear(&cp.args);
379 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
381 argv_array_push(&cp.args, "--quiet");
382 cp.env = child_env.argv;
383 ret = run_command(&cp);
389 FREE_AND_NULL(junk_work_tree);
390 FREE_AND_NULL(junk_git_dir);
393 if (ret || !opts->keep_locked) {
395 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
396 unlink_or_warn(sb.buf);
400 * Hook failure does not warrant worktree deletion, so run hook after
401 * is_junk is cleared, but do return appropriate code when hook fails.
403 if (!ret && opts->checkout) {
404 const char *hook = find_hook("post-checkout");
406 const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL };
409 cp.stdout_to_stderr = 1;
413 cp.trace2_hook_name = "post-checkout";
414 argv_array_pushl(&cp.args, absolute_path(hook),
415 oid_to_hex(&null_oid),
416 oid_to_hex(&commit->object.oid),
418 ret = run_command(&cp);
422 argv_array_clear(&child_env);
424 strbuf_release(&symref);
425 strbuf_release(&sb_repo);
426 strbuf_release(&sb_git);
427 strbuf_release(&sb_name);
431 static void print_preparing_worktree_line(int detach,
433 const char *new_branch,
434 int force_new_branch)
436 if (force_new_branch) {
437 struct commit *commit = lookup_commit_reference_by_name(new_branch);
439 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
441 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
443 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
444 } else if (new_branch) {
445 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
447 struct strbuf s = STRBUF_INIT;
448 if (!detach && !strbuf_check_branch_ref(&s, branch) &&
450 printf_ln(_("Preparing worktree (checking out '%s')"),
453 struct commit *commit = lookup_commit_reference_by_name(branch);
455 die(_("invalid reference: %s"), branch);
456 printf_ln(_("Preparing worktree (detached HEAD %s)"),
457 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
463 static const char *dwim_branch(const char *path, const char **new_branch)
466 const char *s = worktree_basename(path, &n);
467 const char *branchname = xstrndup(s, n);
468 struct strbuf ref = STRBUF_INIT;
471 if (!strbuf_check_branch_ref(&ref, branchname) &&
472 ref_exists(ref.buf)) {
473 strbuf_release(&ref);
477 *new_branch = branchname;
479 struct object_id oid;
481 unique_tracking_name(*new_branch, &oid, NULL);
487 static int add(int ac, const char **av, const char *prefix)
489 struct add_opts opts;
490 const char *new_branch_force = NULL;
493 const char *new_branch = NULL;
494 const char *opt_track = NULL;
495 struct option options[] = {
496 OPT__FORCE(&opts.force,
497 N_("checkout <branch> even if already checked out in other worktree"),
498 PARSE_OPT_NOCOMPLETE),
499 OPT_STRING('b', NULL, &new_branch, N_("branch"),
500 N_("create a new branch")),
501 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
502 N_("create or reset a branch")),
503 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
504 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
505 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
506 OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
507 OPT_PASSTHRU(0, "track", &opt_track, NULL,
508 N_("set up tracking mode (see git-branch(1))"),
509 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
510 OPT_BOOL(0, "guess-remote", &guess_remote,
511 N_("try to match the new branch name with a remote-tracking branch")),
515 memset(&opts, 0, sizeof(opts));
517 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
518 if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
519 die(_("-b, -B, and --detach are mutually exclusive"));
520 if (ac < 1 || ac > 2)
521 usage_with_options(worktree_usage, options);
523 path = prefix_filename(prefix, av[0]);
524 branch = ac < 2 ? "HEAD" : av[1];
526 if (!strcmp(branch, "-"))
529 if (new_branch_force) {
530 struct strbuf symref = STRBUF_INIT;
532 new_branch = new_branch_force;
535 !strbuf_check_branch_ref(&symref, new_branch) &&
536 ref_exists(symref.buf))
537 die_if_checked_out(symref.buf, 0);
538 strbuf_release(&symref);
541 if (ac < 2 && !new_branch && !opts.detach) {
542 const char *s = dwim_branch(path, &new_branch);
547 if (ac == 2 && !new_branch && !opts.detach) {
548 struct object_id oid;
549 struct commit *commit;
552 commit = lookup_commit_reference_by_name(branch);
554 remote = unique_tracking_name(branch, &oid, NULL);
562 print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
565 struct child_process cp = CHILD_PROCESS_INIT;
567 argv_array_push(&cp.args, "branch");
568 if (new_branch_force)
569 argv_array_push(&cp.args, "--force");
571 argv_array_push(&cp.args, "--quiet");
572 argv_array_push(&cp.args, new_branch);
573 argv_array_push(&cp.args, branch);
575 argv_array_push(&cp.args, opt_track);
576 if (run_command(&cp))
579 } else if (opt_track) {
580 die(_("--[no-]track can only be used if a new branch is created"));
585 return add_worktree(path, branch, &opts);
588 static void show_worktree_porcelain(struct worktree *wt)
590 printf("worktree %s\n", wt->path);
594 printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
596 printf("detached\n");
597 else if (wt->head_ref)
598 printf("branch %s\n", wt->head_ref);
603 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
605 struct strbuf sb = STRBUF_INIT;
606 int cur_path_len = strlen(wt->path);
607 int path_adj = cur_path_len - utf8_strwidth(wt->path);
609 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
611 strbuf_addstr(&sb, "(bare)");
613 strbuf_addf(&sb, "%-*s ", abbrev_len,
614 find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV));
616 strbuf_addstr(&sb, "(detached HEAD)");
617 else if (wt->head_ref) {
618 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
619 strbuf_addf(&sb, "[%s]", ref);
622 strbuf_addstr(&sb, "(error)");
624 printf("%s\n", sb.buf);
629 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
633 for (i = 0; wt[i]; i++) {
635 int path_len = strlen(wt[i]->path);
637 if (path_len > *maxlen)
639 sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev));
640 if (sha1_len > *abbrev)
645 static int list(int ac, const char **av, const char *prefix)
649 struct option options[] = {
650 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
654 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
656 usage_with_options(worktree_usage, options);
658 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
659 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
662 measure_widths(worktrees, &abbrev, &path_maxlen);
664 for (i = 0; worktrees[i]; i++) {
666 show_worktree_porcelain(worktrees[i]);
668 show_worktree(worktrees[i], path_maxlen, abbrev);
670 free_worktrees(worktrees);
675 static int lock_worktree(int ac, const char **av, const char *prefix)
677 const char *reason = "", *old_reason;
678 struct option options[] = {
679 OPT_STRING(0, "reason", &reason, N_("string"),
680 N_("reason for locking")),
683 struct worktree **worktrees, *wt;
685 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
687 usage_with_options(worktree_usage, options);
689 worktrees = get_worktrees(0);
690 wt = find_worktree(worktrees, prefix, av[0]);
692 die(_("'%s' is not a working tree"), av[0]);
693 if (is_main_worktree(wt))
694 die(_("The main working tree cannot be locked or unlocked"));
696 old_reason = worktree_lock_reason(wt);
699 die(_("'%s' is already locked, reason: %s"),
701 die(_("'%s' is already locked"), av[0]);
704 write_file(git_common_path("worktrees/%s/locked", wt->id),
706 free_worktrees(worktrees);
710 static int unlock_worktree(int ac, const char **av, const char *prefix)
712 struct option options[] = {
715 struct worktree **worktrees, *wt;
718 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
720 usage_with_options(worktree_usage, options);
722 worktrees = get_worktrees(0);
723 wt = find_worktree(worktrees, prefix, av[0]);
725 die(_("'%s' is not a working tree"), av[0]);
726 if (is_main_worktree(wt))
727 die(_("The main working tree cannot be locked or unlocked"));
728 if (!worktree_lock_reason(wt))
729 die(_("'%s' is not locked"), av[0]);
730 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
731 free_worktrees(worktrees);
735 static void validate_no_submodules(const struct worktree *wt)
737 struct index_state istate = { NULL };
738 struct strbuf path = STRBUF_INIT;
739 int i, found_submodules = 0;
741 if (is_directory(worktree_git_path(wt, "modules"))) {
743 * There could be false positives, e.g. the "modules"
744 * directory exists but is empty. But it's a rare case and
745 * this simpler check is probably good enough for now.
747 found_submodules = 1;
748 } else if (read_index_from(&istate, worktree_git_path(wt, "index"),
749 get_worktree_git_dir(wt)) > 0) {
750 for (i = 0; i < istate.cache_nr; i++) {
751 struct cache_entry *ce = istate.cache[i];
754 if (!S_ISGITLINK(ce->ce_mode))
758 strbuf_addf(&path, "%s/%s", wt->path, ce->name);
759 if (!is_submodule_populated_gently(path.buf, &err))
762 found_submodules = 1;
766 discard_index(&istate);
767 strbuf_release(&path);
769 if (found_submodules)
770 die(_("working trees containing submodules cannot be moved or removed"));
773 static int move_worktree(int ac, const char **av, const char *prefix)
776 struct option options[] = {
778 N_("force move even if worktree is dirty or locked"),
779 PARSE_OPT_NOCOMPLETE),
782 struct worktree **worktrees, *wt;
783 struct strbuf dst = STRBUF_INIT;
784 struct strbuf errmsg = STRBUF_INIT;
785 const char *reason = NULL;
788 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
790 usage_with_options(worktree_usage, options);
792 path = prefix_filename(prefix, av[1]);
793 strbuf_addstr(&dst, path);
796 worktrees = get_worktrees(0);
797 wt = find_worktree(worktrees, prefix, av[0]);
799 die(_("'%s' is not a working tree"), av[0]);
800 if (is_main_worktree(wt))
801 die(_("'%s' is a main working tree"), av[0]);
802 if (is_directory(dst.buf)) {
803 const char *sep = find_last_dir_sep(wt->path);
806 die(_("could not figure out destination name from '%s'"),
808 strbuf_trim_trailing_dir_sep(&dst);
809 strbuf_addstr(&dst, sep);
811 if (file_exists(dst.buf))
812 die(_("target '%s' already exists"), dst.buf);
814 validate_no_submodules(wt);
817 reason = worktree_lock_reason(wt);
820 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
822 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
824 if (validate_worktree(wt, &errmsg, 0))
825 die(_("validation failed, cannot move working tree: %s"),
827 strbuf_release(&errmsg);
829 if (rename(wt->path, dst.buf) == -1)
830 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
832 update_worktree_location(wt, dst.buf);
834 strbuf_release(&dst);
835 free_worktrees(worktrees);
840 * Note, "git status --porcelain" is used to determine if it's safe to
841 * delete a whole worktree. "git status" does not ignore user
842 * configuration, so if a normal "git status" shows "clean" for the
843 * user, then it's ok to remove it.
845 * This assumption may be a bad one. We may want to ignore
846 * (potentially bad) user settings and only delete a worktree when
847 * it's absolutely safe to do so from _our_ point of view because we
850 static void check_clean_worktree(struct worktree *wt,
851 const char *original_path)
853 struct argv_array child_env = ARGV_ARRAY_INIT;
854 struct child_process cp;
859 * Until we sort this out, all submodules are "dirty" and
860 * will abort this function.
862 validate_no_submodules(wt);
864 argv_array_pushf(&child_env, "%s=%s/.git",
865 GIT_DIR_ENVIRONMENT, wt->path);
866 argv_array_pushf(&child_env, "%s=%s",
867 GIT_WORK_TREE_ENVIRONMENT, wt->path);
868 memset(&cp, 0, sizeof(cp));
869 argv_array_pushl(&cp.args, "status",
870 "--porcelain", "--ignore-submodules=none",
872 cp.env = child_env.argv;
876 ret = start_command(&cp);
878 die_errno(_("failed to run 'git status' on '%s'"),
880 ret = xread(cp.out, buf, sizeof(buf));
882 die(_("'%s' contains modified or untracked files, use --force to delete it"),
885 ret = finish_command(&cp);
887 die_errno(_("failed to run 'git status' on '%s', code %d"),
891 static int delete_git_work_tree(struct worktree *wt)
893 struct strbuf sb = STRBUF_INIT;
896 strbuf_addstr(&sb, wt->path);
897 if (remove_dir_recursively(&sb, 0)) {
898 error_errno(_("failed to delete '%s'"), sb.buf);
905 static int remove_worktree(int ac, const char **av, const char *prefix)
908 struct option options[] = {
910 N_("force removal even if worktree is dirty or locked"),
911 PARSE_OPT_NOCOMPLETE),
914 struct worktree **worktrees, *wt;
915 struct strbuf errmsg = STRBUF_INIT;
916 const char *reason = NULL;
919 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
921 usage_with_options(worktree_usage, options);
923 worktrees = get_worktrees(0);
924 wt = find_worktree(worktrees, prefix, av[0]);
926 die(_("'%s' is not a working tree"), av[0]);
927 if (is_main_worktree(wt))
928 die(_("'%s' is a main working tree"), av[0]);
930 reason = worktree_lock_reason(wt);
933 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
935 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
937 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
938 die(_("validation failed, cannot remove working tree: %s"),
940 strbuf_release(&errmsg);
942 if (file_exists(wt->path)) {
944 check_clean_worktree(wt, av[0]);
946 ret |= delete_git_work_tree(wt);
949 * continue on even if ret is non-zero, there's no going back
952 ret |= delete_git_dir(wt->id);
953 delete_worktrees_dir_if_empty();
955 free_worktrees(worktrees);
959 int cmd_worktree(int ac, const char **av, const char *prefix)
961 struct option options[] = {
965 git_config(git_worktree_config, NULL);
968 usage_with_options(worktree_usage, options);
971 if (!strcmp(av[1], "add"))
972 return add(ac - 1, av + 1, prefix);
973 if (!strcmp(av[1], "prune"))
974 return prune(ac - 1, av + 1, prefix);
975 if (!strcmp(av[1], "list"))
976 return list(ac - 1, av + 1, prefix);
977 if (!strcmp(av[1], "lock"))
978 return lock_worktree(ac - 1, av + 1, prefix);
979 if (!strcmp(av[1], "unlock"))
980 return unlock_worktree(ac - 1, av + 1, prefix);
981 if (!strcmp(av[1], "move"))
982 return move_worktree(ac - 1, av + 1, prefix);
983 if (!strcmp(av[1], "remove"))
984 return remove_worktree(ac - 1, av + 1, prefix);
985 usage_with_options(worktree_usage, options);