Merge branch 'nd/worktree-move'
[git.git] / builtin / worktree.c
blob1c1e576fdb01ef1adc4207799d2d7ac97932a8cf
1 #include "cache.h"
2 #include "checkout.h"
3 #include "config.h"
4 #include "builtin.h"
5 #include "dir.h"
6 #include "parse-options.h"
7 #include "argv-array.h"
8 #include "branch.h"
9 #include "refs.h"
10 #include "run-command.h"
11 #include "sigchain.h"
12 #include "refs.h"
13 #include "utf8.h"
14 #include "worktree.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>"),
24 NULL
27 struct add_opts {
28 int force;
29 int detach;
30 int checkout;
31 int keep_locked;
32 const char *new_branch;
33 int force_new_branch;
36 static int show_only;
37 static int verbose;
38 static int guess_remote;
39 static timestamp_t expire;
41 static int git_worktree_config(const char *var, const char *value, void *cb)
43 if (!strcmp(var, "worktree.guessremote")) {
44 guess_remote = git_config_bool(var, value);
45 return 0;
48 return git_default_config(var, value, cb);
51 static int prune_worktree(const char *id, struct strbuf *reason)
53 struct stat st;
54 char *path;
55 int fd;
56 size_t len;
57 ssize_t read_result;
59 if (!is_directory(git_path("worktrees/%s", id))) {
60 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
61 return 1;
63 if (file_exists(git_path("worktrees/%s/locked", id)))
64 return 0;
65 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
66 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
67 return 1;
69 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
70 if (fd < 0) {
71 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
72 id, strerror(errno));
73 return 1;
75 len = xsize_t(st.st_size);
76 path = xmallocz(len);
78 read_result = read_in_full(fd, path, len);
79 if (read_result < 0) {
80 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
81 id, strerror(errno));
82 close(fd);
83 free(path);
84 return 1;
86 close(fd);
88 if (read_result != len) {
89 strbuf_addf(reason,
90 _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
91 id, (uintmax_t)len, (uintmax_t)read_result);
92 free(path);
93 return 1;
95 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
96 len--;
97 if (!len) {
98 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
99 free(path);
100 return 1;
102 path[len] = '\0';
103 if (!file_exists(path)) {
104 struct stat st_link;
105 free(path);
107 * the repo is moved manually and has not been
108 * accessed since?
110 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
111 st_link.st_nlink > 1)
112 return 0;
113 if (st.st_mtime <= expire) {
114 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
115 return 1;
116 } else {
117 return 0;
120 free(path);
121 return 0;
124 static void prune_worktrees(void)
126 struct strbuf reason = STRBUF_INIT;
127 struct strbuf path = STRBUF_INIT;
128 DIR *dir = opendir(git_path("worktrees"));
129 struct dirent *d;
130 int ret;
131 if (!dir)
132 return;
133 while ((d = readdir(dir)) != NULL) {
134 if (is_dot_or_dotdot(d->d_name))
135 continue;
136 strbuf_reset(&reason);
137 if (!prune_worktree(d->d_name, &reason))
138 continue;
139 if (show_only || verbose)
140 printf("%s\n", reason.buf);
141 if (show_only)
142 continue;
143 git_path_buf(&path, "worktrees/%s", d->d_name);
144 ret = remove_dir_recursively(&path, 0);
145 if (ret < 0 && errno == ENOTDIR)
146 ret = unlink(path.buf);
147 if (ret)
148 error_errno(_("failed to remove '%s'"), path.buf);
150 closedir(dir);
151 if (!show_only)
152 rmdir(git_path("worktrees"));
153 strbuf_release(&reason);
154 strbuf_release(&path);
157 static int prune(int ac, const char **av, const char *prefix)
159 struct option options[] = {
160 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
161 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
162 OPT_EXPIRY_DATE(0, "expire", &expire,
163 N_("expire working trees older than <time>")),
164 OPT_END()
167 expire = TIME_MAX;
168 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
169 if (ac)
170 usage_with_options(worktree_usage, options);
171 prune_worktrees();
172 return 0;
175 static char *junk_work_tree;
176 static char *junk_git_dir;
177 static int is_junk;
178 static pid_t junk_pid;
180 static void remove_junk(void)
182 struct strbuf sb = STRBUF_INIT;
183 if (!is_junk || getpid() != junk_pid)
184 return;
185 if (junk_git_dir) {
186 strbuf_addstr(&sb, junk_git_dir);
187 remove_dir_recursively(&sb, 0);
188 strbuf_reset(&sb);
190 if (junk_work_tree) {
191 strbuf_addstr(&sb, junk_work_tree);
192 remove_dir_recursively(&sb, 0);
194 strbuf_release(&sb);
197 static void remove_junk_on_signal(int signo)
199 remove_junk();
200 sigchain_pop(signo);
201 raise(signo);
204 static const char *worktree_basename(const char *path, int *olen)
206 const char *name;
207 int len;
209 len = strlen(path);
210 while (len && is_dir_sep(path[len - 1]))
211 len--;
213 for (name = path + len - 1; name > path; name--)
214 if (is_dir_sep(*name)) {
215 name++;
216 break;
219 *olen = len;
220 return name;
223 static int add_worktree(const char *path, const char *refname,
224 const struct add_opts *opts)
226 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
227 struct strbuf sb = STRBUF_INIT;
228 const char *name;
229 struct stat st;
230 struct child_process cp = CHILD_PROCESS_INIT;
231 struct argv_array child_env = ARGV_ARRAY_INIT;
232 int counter = 0, len, ret;
233 struct strbuf symref = STRBUF_INIT;
234 struct commit *commit = NULL;
235 int is_branch = 0;
237 if (file_exists(path) && !is_empty_dir(path))
238 die(_("'%s' already exists"), path);
240 /* is 'refname' a branch or commit? */
241 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
242 ref_exists(symref.buf)) {
243 is_branch = 1;
244 if (!opts->force)
245 die_if_checked_out(symref.buf, 0);
247 commit = lookup_commit_reference_by_name(refname);
248 if (!commit)
249 die(_("invalid reference: %s"), refname);
251 name = worktree_basename(path, &len);
252 git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name);
253 len = sb_repo.len;
254 if (safe_create_leading_directories_const(sb_repo.buf))
255 die_errno(_("could not create leading directories of '%s'"),
256 sb_repo.buf);
257 while (!stat(sb_repo.buf, &st)) {
258 counter++;
259 strbuf_setlen(&sb_repo, len);
260 strbuf_addf(&sb_repo, "%d", counter);
262 name = strrchr(sb_repo.buf, '/') + 1;
264 junk_pid = getpid();
265 atexit(remove_junk);
266 sigchain_push_common(remove_junk_on_signal);
268 if (mkdir(sb_repo.buf, 0777))
269 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
270 junk_git_dir = xstrdup(sb_repo.buf);
271 is_junk = 1;
274 * lock the incomplete repo so prune won't delete it, unlock
275 * after the preparation is over.
277 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
278 if (!opts->keep_locked)
279 write_file(sb.buf, "initializing");
280 else
281 write_file(sb.buf, "added with --lock");
283 strbuf_addf(&sb_git, "%s/.git", path);
284 if (safe_create_leading_directories_const(sb_git.buf))
285 die_errno(_("could not create leading directories of '%s'"),
286 sb_git.buf);
287 junk_work_tree = xstrdup(path);
289 strbuf_reset(&sb);
290 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
291 write_file(sb.buf, "%s", real_path(sb_git.buf));
292 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
293 real_path(get_git_common_dir()), name);
295 * This is to keep resolve_ref() happy. We need a valid HEAD
296 * or is_git_directory() will reject the directory. Any value which
297 * looks like an object ID will do since it will be immediately
298 * replaced by the symbolic-ref or update-ref invocation in the new
299 * worktree.
301 strbuf_reset(&sb);
302 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
303 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
304 strbuf_reset(&sb);
305 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
306 write_file(sb.buf, "../..");
308 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
310 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
311 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
312 cp.git_cmd = 1;
314 if (!is_branch)
315 argv_array_pushl(&cp.args, "update-ref", "HEAD",
316 oid_to_hex(&commit->object.oid), NULL);
317 else
318 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
319 symref.buf, NULL);
320 cp.env = child_env.argv;
321 ret = run_command(&cp);
322 if (ret)
323 goto done;
325 if (opts->checkout) {
326 cp.argv = NULL;
327 argv_array_clear(&cp.args);
328 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
329 cp.env = child_env.argv;
330 ret = run_command(&cp);
331 if (ret)
332 goto done;
335 is_junk = 0;
336 FREE_AND_NULL(junk_work_tree);
337 FREE_AND_NULL(junk_git_dir);
339 done:
340 if (ret || !opts->keep_locked) {
341 strbuf_reset(&sb);
342 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
343 unlink_or_warn(sb.buf);
347 * Hook failure does not warrant worktree deletion, so run hook after
348 * is_junk is cleared, but do return appropriate code when hook fails.
350 if (!ret && opts->checkout) {
351 const char *hook = find_hook("post-checkout");
352 if (hook) {
353 const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL };
354 cp.git_cmd = 0;
355 cp.no_stdin = 1;
356 cp.stdout_to_stderr = 1;
357 cp.dir = path;
358 cp.env = env;
359 cp.argv = NULL;
360 argv_array_pushl(&cp.args, absolute_path(hook),
361 oid_to_hex(&null_oid),
362 oid_to_hex(&commit->object.oid),
363 "1", NULL);
364 ret = run_command(&cp);
368 argv_array_clear(&child_env);
369 strbuf_release(&sb);
370 strbuf_release(&symref);
371 strbuf_release(&sb_repo);
372 strbuf_release(&sb_git);
373 return ret;
376 static int add(int ac, const char **av, const char *prefix)
378 struct add_opts opts;
379 const char *new_branch_force = NULL;
380 char *path;
381 const char *branch;
382 const char *opt_track = NULL;
383 struct option options[] = {
384 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
385 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
386 N_("create a new branch")),
387 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
388 N_("create or reset a branch")),
389 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
390 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
391 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
392 OPT_PASSTHRU(0, "track", &opt_track, NULL,
393 N_("set up tracking mode (see git-branch(1))"),
394 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
395 OPT_BOOL(0, "guess-remote", &guess_remote,
396 N_("try to match the new branch name with a remote-tracking branch")),
397 OPT_END()
400 memset(&opts, 0, sizeof(opts));
401 opts.checkout = 1;
402 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
403 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
404 die(_("-b, -B, and --detach are mutually exclusive"));
405 if (ac < 1 || ac > 2)
406 usage_with_options(worktree_usage, options);
408 path = prefix_filename(prefix, av[0]);
409 branch = ac < 2 ? "HEAD" : av[1];
411 if (!strcmp(branch, "-"))
412 branch = "@{-1}";
414 opts.force_new_branch = !!new_branch_force;
415 if (opts.force_new_branch) {
416 struct strbuf symref = STRBUF_INIT;
418 opts.new_branch = new_branch_force;
420 if (!opts.force &&
421 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
422 ref_exists(symref.buf))
423 die_if_checked_out(symref.buf, 0);
424 strbuf_release(&symref);
427 if (ac < 2 && !opts.new_branch && !opts.detach) {
428 int n;
429 const char *s = worktree_basename(path, &n);
430 opts.new_branch = xstrndup(s, n);
431 if (guess_remote) {
432 struct object_id oid;
433 const char *remote =
434 unique_tracking_name(opts.new_branch, &oid);
435 if (remote)
436 branch = remote;
440 if (ac == 2 && !opts.new_branch && !opts.detach) {
441 struct object_id oid;
442 struct commit *commit;
443 const char *remote;
445 commit = lookup_commit_reference_by_name(branch);
446 if (!commit) {
447 remote = unique_tracking_name(branch, &oid);
448 if (remote) {
449 opts.new_branch = branch;
450 branch = remote;
455 if (opts.new_branch) {
456 struct child_process cp = CHILD_PROCESS_INIT;
457 cp.git_cmd = 1;
458 argv_array_push(&cp.args, "branch");
459 if (opts.force_new_branch)
460 argv_array_push(&cp.args, "--force");
461 argv_array_push(&cp.args, opts.new_branch);
462 argv_array_push(&cp.args, branch);
463 if (opt_track)
464 argv_array_push(&cp.args, opt_track);
465 if (run_command(&cp))
466 return -1;
467 branch = opts.new_branch;
468 } else if (opt_track) {
469 die(_("--[no-]track can only be used if a new branch is created"));
472 UNLEAK(path);
473 UNLEAK(opts);
474 return add_worktree(path, branch, &opts);
477 static void show_worktree_porcelain(struct worktree *wt)
479 printf("worktree %s\n", wt->path);
480 if (wt->is_bare)
481 printf("bare\n");
482 else {
483 printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
484 if (wt->is_detached)
485 printf("detached\n");
486 else if (wt->head_ref)
487 printf("branch %s\n", wt->head_ref);
489 printf("\n");
492 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
494 struct strbuf sb = STRBUF_INIT;
495 int cur_path_len = strlen(wt->path);
496 int path_adj = cur_path_len - utf8_strwidth(wt->path);
498 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
499 if (wt->is_bare)
500 strbuf_addstr(&sb, "(bare)");
501 else {
502 strbuf_addf(&sb, "%-*s ", abbrev_len,
503 find_unique_abbrev(wt->head_oid.hash, DEFAULT_ABBREV));
504 if (wt->is_detached)
505 strbuf_addstr(&sb, "(detached HEAD)");
506 else if (wt->head_ref) {
507 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
508 strbuf_addf(&sb, "[%s]", ref);
509 free(ref);
510 } else
511 strbuf_addstr(&sb, "(error)");
513 printf("%s\n", sb.buf);
515 strbuf_release(&sb);
518 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
520 int i;
522 for (i = 0; wt[i]; i++) {
523 int sha1_len;
524 int path_len = strlen(wt[i]->path);
526 if (path_len > *maxlen)
527 *maxlen = path_len;
528 sha1_len = strlen(find_unique_abbrev(wt[i]->head_oid.hash, *abbrev));
529 if (sha1_len > *abbrev)
530 *abbrev = sha1_len;
534 static int list(int ac, const char **av, const char *prefix)
536 int porcelain = 0;
538 struct option options[] = {
539 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
540 OPT_END()
543 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
544 if (ac)
545 usage_with_options(worktree_usage, options);
546 else {
547 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
548 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
550 if (!porcelain)
551 measure_widths(worktrees, &abbrev, &path_maxlen);
553 for (i = 0; worktrees[i]; i++) {
554 if (porcelain)
555 show_worktree_porcelain(worktrees[i]);
556 else
557 show_worktree(worktrees[i], path_maxlen, abbrev);
559 free_worktrees(worktrees);
561 return 0;
564 static int lock_worktree(int ac, const char **av, const char *prefix)
566 const char *reason = "", *old_reason;
567 struct option options[] = {
568 OPT_STRING(0, "reason", &reason, N_("string"),
569 N_("reason for locking")),
570 OPT_END()
572 struct worktree **worktrees, *wt;
574 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
575 if (ac != 1)
576 usage_with_options(worktree_usage, options);
578 worktrees = get_worktrees(0);
579 wt = find_worktree(worktrees, prefix, av[0]);
580 if (!wt)
581 die(_("'%s' is not a working tree"), av[0]);
582 if (is_main_worktree(wt))
583 die(_("The main working tree cannot be locked or unlocked"));
585 old_reason = is_worktree_locked(wt);
586 if (old_reason) {
587 if (*old_reason)
588 die(_("'%s' is already locked, reason: %s"),
589 av[0], old_reason);
590 die(_("'%s' is already locked"), av[0]);
593 write_file(git_common_path("worktrees/%s/locked", wt->id),
594 "%s", reason);
595 free_worktrees(worktrees);
596 return 0;
599 static int unlock_worktree(int ac, const char **av, const char *prefix)
601 struct option options[] = {
602 OPT_END()
604 struct worktree **worktrees, *wt;
605 int ret;
607 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
608 if (ac != 1)
609 usage_with_options(worktree_usage, options);
611 worktrees = get_worktrees(0);
612 wt = find_worktree(worktrees, prefix, av[0]);
613 if (!wt)
614 die(_("'%s' is not a working tree"), av[0]);
615 if (is_main_worktree(wt))
616 die(_("The main working tree cannot be locked or unlocked"));
617 if (!is_worktree_locked(wt))
618 die(_("'%s' is not locked"), av[0]);
619 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
620 free_worktrees(worktrees);
621 return ret;
624 static void validate_no_submodules(const struct worktree *wt)
626 struct index_state istate = { NULL };
627 int i, found_submodules = 0;
629 if (read_index_from(&istate, worktree_git_path(wt, "index"),
630 get_worktree_git_dir(wt)) > 0) {
631 for (i = 0; i < istate.cache_nr; i++) {
632 struct cache_entry *ce = istate.cache[i];
634 if (S_ISGITLINK(ce->ce_mode)) {
635 found_submodules = 1;
636 break;
640 discard_index(&istate);
642 if (found_submodules)
643 die(_("working trees containing submodules cannot be moved or removed"));
646 static int move_worktree(int ac, const char **av, const char *prefix)
648 struct option options[] = {
649 OPT_END()
651 struct worktree **worktrees, *wt;
652 struct strbuf dst = STRBUF_INIT;
653 struct strbuf errmsg = STRBUF_INIT;
654 const char *reason;
655 char *path;
657 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
658 if (ac != 2)
659 usage_with_options(worktree_usage, options);
661 path = prefix_filename(prefix, av[1]);
662 strbuf_addstr(&dst, path);
663 free(path);
665 worktrees = get_worktrees(0);
666 wt = find_worktree(worktrees, prefix, av[0]);
667 if (!wt)
668 die(_("'%s' is not a working tree"), av[0]);
669 if (is_main_worktree(wt))
670 die(_("'%s' is a main working tree"), av[0]);
671 if (is_directory(dst.buf)) {
672 const char *sep = find_last_dir_sep(wt->path);
674 if (!sep)
675 die(_("could not figure out destination name from '%s'"),
676 wt->path);
677 strbuf_trim_trailing_dir_sep(&dst);
678 strbuf_addstr(&dst, sep);
680 if (file_exists(dst.buf))
681 die(_("target '%s' already exists"), dst.buf);
683 validate_no_submodules(wt);
685 reason = is_worktree_locked(wt);
686 if (reason) {
687 if (*reason)
688 die(_("cannot move a locked working tree, lock reason: %s"),
689 reason);
690 die(_("cannot move a locked working tree"));
692 if (validate_worktree(wt, &errmsg, 0))
693 die(_("validation failed, cannot move working tree: %s"),
694 errmsg.buf);
695 strbuf_release(&errmsg);
697 if (rename(wt->path, dst.buf) == -1)
698 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
700 update_worktree_location(wt, dst.buf);
702 strbuf_release(&dst);
703 free_worktrees(worktrees);
704 return 0;
708 * Note, "git status --porcelain" is used to determine if it's safe to
709 * delete a whole worktree. "git status" does not ignore user
710 * configuration, so if a normal "git status" shows "clean" for the
711 * user, then it's ok to remove it.
713 * This assumption may be a bad one. We may want to ignore
714 * (potentially bad) user settings and only delete a worktree when
715 * it's absolutely safe to do so from _our_ point of view because we
716 * know better.
718 static void check_clean_worktree(struct worktree *wt,
719 const char *original_path)
721 struct argv_array child_env = ARGV_ARRAY_INIT;
722 struct child_process cp;
723 char buf[1];
724 int ret;
727 * Until we sort this out, all submodules are "dirty" and
728 * will abort this function.
730 validate_no_submodules(wt);
732 argv_array_pushf(&child_env, "%s=%s/.git",
733 GIT_DIR_ENVIRONMENT, wt->path);
734 argv_array_pushf(&child_env, "%s=%s",
735 GIT_WORK_TREE_ENVIRONMENT, wt->path);
736 memset(&cp, 0, sizeof(cp));
737 argv_array_pushl(&cp.args, "status",
738 "--porcelain", "--ignore-submodules=none",
739 NULL);
740 cp.env = child_env.argv;
741 cp.git_cmd = 1;
742 cp.dir = wt->path;
743 cp.out = -1;
744 ret = start_command(&cp);
745 if (ret)
746 die_errno(_("failed to run 'git status' on '%s'"),
747 original_path);
748 ret = xread(cp.out, buf, sizeof(buf));
749 if (ret)
750 die(_("'%s' is dirty, use --force to delete it"),
751 original_path);
752 close(cp.out);
753 ret = finish_command(&cp);
754 if (ret)
755 die_errno(_("failed to run 'git status' on '%s', code %d"),
756 original_path, ret);
759 static int delete_git_work_tree(struct worktree *wt)
761 struct strbuf sb = STRBUF_INIT;
762 int ret = 0;
764 strbuf_addstr(&sb, wt->path);
765 if (remove_dir_recursively(&sb, 0)) {
766 error_errno(_("failed to delete '%s'"), sb.buf);
767 ret = -1;
769 strbuf_release(&sb);
770 return ret;
773 static int delete_git_dir(struct worktree *wt)
775 struct strbuf sb = STRBUF_INIT;
776 int ret = 0;
778 strbuf_addstr(&sb, git_common_path("worktrees/%s", wt->id));
779 if (remove_dir_recursively(&sb, 0)) {
780 error_errno(_("failed to delete '%s'"), sb.buf);
781 ret = -1;
783 strbuf_release(&sb);
784 return ret;
787 static int remove_worktree(int ac, const char **av, const char *prefix)
789 int force = 0;
790 struct option options[] = {
791 OPT_BOOL(0, "force", &force,
792 N_("force removing even if the worktree is dirty")),
793 OPT_END()
795 struct worktree **worktrees, *wt;
796 struct strbuf errmsg = STRBUF_INIT;
797 const char *reason;
798 int ret = 0;
800 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
801 if (ac != 1)
802 usage_with_options(worktree_usage, options);
804 worktrees = get_worktrees(0);
805 wt = find_worktree(worktrees, prefix, av[0]);
806 if (!wt)
807 die(_("'%s' is not a working tree"), av[0]);
808 if (is_main_worktree(wt))
809 die(_("'%s' is a main working tree"), av[0]);
810 reason = is_worktree_locked(wt);
811 if (reason) {
812 if (*reason)
813 die(_("cannot remove a locked working tree, lock reason: %s"),
814 reason);
815 die(_("cannot remove a locked working tree"));
817 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
818 die(_("validation failed, cannot remove working tree: %s"),
819 errmsg.buf);
820 strbuf_release(&errmsg);
822 if (file_exists(wt->path)) {
823 if (!force)
824 check_clean_worktree(wt, av[0]);
826 ret |= delete_git_work_tree(wt);
829 * continue on even if ret is non-zero, there's no going back
830 * from here.
832 ret |= delete_git_dir(wt);
834 free_worktrees(worktrees);
835 return ret;
838 int cmd_worktree(int ac, const char **av, const char *prefix)
840 struct option options[] = {
841 OPT_END()
844 git_config(git_worktree_config, NULL);
846 if (ac < 2)
847 usage_with_options(worktree_usage, options);
848 if (!prefix)
849 prefix = "";
850 if (!strcmp(av[1], "add"))
851 return add(ac - 1, av + 1, prefix);
852 if (!strcmp(av[1], "prune"))
853 return prune(ac - 1, av + 1, prefix);
854 if (!strcmp(av[1], "list"))
855 return list(ac - 1, av + 1, prefix);
856 if (!strcmp(av[1], "lock"))
857 return lock_worktree(ac - 1, av + 1, prefix);
858 if (!strcmp(av[1], "unlock"))
859 return unlock_worktree(ac - 1, av + 1, prefix);
860 if (!strcmp(av[1], "move"))
861 return move_worktree(ac - 1, av + 1, prefix);
862 if (!strcmp(av[1], "remove"))
863 return remove_worktree(ac - 1, av + 1, prefix);
864 usage_with_options(worktree_usage, options);