6 #include "parse-options.h"
10 #include "run-command.h"
13 #include "submodule.h"
18 static const char * const worktree_usage
[] = {
19 N_("git worktree add [<options>] <path> [<commit-ish>]"),
20 N_("git worktree list [<options>]"),
21 N_("git worktree lock [<options>] <path>"),
22 N_("git worktree move <worktree> <new-path>"),
23 N_("git worktree prune [<options>]"),
24 N_("git worktree remove [<options>] <worktree>"),
25 N_("git worktree unlock <path>"),
34 const char *keep_locked
;
39 static int guess_remote
;
40 static timestamp_t expire
;
42 static int git_worktree_config(const char *var
, const char *value
, void *cb
)
44 if (!strcmp(var
, "worktree.guessremote")) {
45 guess_remote
= git_config_bool(var
, value
);
49 return git_default_config(var
, value
, cb
);
52 static int delete_git_dir(const char *id
)
54 struct strbuf sb
= STRBUF_INIT
;
57 strbuf_addstr(&sb
, git_common_path("worktrees/%s", id
));
58 ret
= remove_dir_recursively(&sb
, 0);
59 if (ret
< 0 && errno
== ENOTDIR
)
62 error_errno(_("failed to delete '%s'"), sb
.buf
);
67 static void delete_worktrees_dir_if_empty(void)
69 rmdir(git_path("worktrees")); /* ignore failed removal */
72 static void prune_worktree(const char *id
, const char *reason
)
74 if (show_only
|| verbose
)
75 printf_ln(_("Removing %s/%s: %s"), "worktrees", id
, reason
);
80 static int prune_cmp(const void *a
, const void *b
)
82 const struct string_list_item
*x
= a
;
83 const struct string_list_item
*y
= b
;
86 if ((c
= fspathcmp(x
->string
, y
->string
)))
89 * paths same; prune_dupes() removes all but the first worktree entry
90 * having the same path, so sort main worktree ('util' is NULL) above
91 * linked worktrees ('util' not NULL) since main worktree can't be
98 /* paths same; sort by .git/worktrees/<id> */
99 return strcmp(x
->util
, y
->util
);
102 static void prune_dups(struct string_list
*l
)
106 QSORT(l
->items
, l
->nr
, prune_cmp
);
107 for (i
= 1; i
< l
->nr
; i
++) {
108 if (!fspathcmp(l
->items
[i
].string
, l
->items
[i
- 1].string
))
109 prune_worktree(l
->items
[i
].util
, "duplicate entry");
113 static void prune_worktrees(void)
115 struct strbuf reason
= STRBUF_INIT
;
116 struct strbuf main_path
= STRBUF_INIT
;
117 struct string_list kept
= STRING_LIST_INIT_NODUP
;
118 DIR *dir
= opendir(git_path("worktrees"));
122 while ((d
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
124 strbuf_reset(&reason
);
125 if (should_prune_worktree(d
->d_name
, &reason
, &path
, expire
))
126 prune_worktree(d
->d_name
, reason
.buf
);
128 string_list_append(&kept
, path
)->util
= xstrdup(d
->d_name
);
132 strbuf_add_absolute_path(&main_path
, get_git_common_dir());
133 /* massage main worktree absolute path to match 'gitdir' content */
134 strbuf_strip_suffix(&main_path
, "/.");
135 string_list_append(&kept
, strbuf_detach(&main_path
, NULL
));
137 string_list_clear(&kept
, 1);
140 delete_worktrees_dir_if_empty();
141 strbuf_release(&reason
);
144 static int prune(int ac
, const char **av
, const char *prefix
)
146 struct option options
[] = {
147 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
148 OPT__VERBOSE(&verbose
, N_("report pruned working trees")),
149 OPT_EXPIRY_DATE(0, "expire", &expire
,
150 N_("expire working trees older than <time>")),
155 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
157 usage_with_options(worktree_usage
, options
);
162 static char *junk_work_tree
;
163 static char *junk_git_dir
;
165 static pid_t junk_pid
;
167 static void remove_junk(void)
169 struct strbuf sb
= STRBUF_INIT
;
170 if (!is_junk
|| getpid() != junk_pid
)
173 strbuf_addstr(&sb
, junk_git_dir
);
174 remove_dir_recursively(&sb
, 0);
177 if (junk_work_tree
) {
178 strbuf_addstr(&sb
, junk_work_tree
);
179 remove_dir_recursively(&sb
, 0);
184 static void remove_junk_on_signal(int signo
)
191 static const char *worktree_basename(const char *path
, int *olen
)
197 while (len
&& is_dir_sep(path
[len
- 1]))
200 for (name
= path
+ len
- 1; name
> path
; name
--)
201 if (is_dir_sep(*name
)) {
210 /* check that path is viable location for worktree */
211 static void check_candidate_path(const char *path
,
213 struct worktree
**worktrees
,
219 if (file_exists(path
) && !is_empty_dir(path
))
220 die(_("'%s' already exists"), path
);
222 wt
= find_worktree_by_path(worktrees
, path
);
226 locked
= !!worktree_lock_reason(wt
);
227 if ((!locked
&& force
) || (locked
&& force
> 1)) {
228 if (delete_git_dir(wt
->id
))
229 die(_("unusable worktree destination '%s'"), path
);
234 die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path
, cmd
);
236 die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path
, cmd
);
239 static int add_worktree(const char *path
, const char *refname
,
240 const struct add_opts
*opts
)
242 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
243 struct strbuf sb
= STRBUF_INIT
, realpath
= STRBUF_INIT
;
245 struct child_process cp
= CHILD_PROCESS_INIT
;
246 struct strvec child_env
= STRVEC_INIT
;
247 unsigned int counter
= 0;
249 struct strbuf symref
= STRBUF_INIT
;
250 struct commit
*commit
= NULL
;
252 struct strbuf sb_name
= STRBUF_INIT
;
253 struct worktree
**worktrees
;
255 worktrees
= get_worktrees();
256 check_candidate_path(path
, opts
->force
, worktrees
, "add");
257 free_worktrees(worktrees
);
260 /* is 'refname' a branch or commit? */
261 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
262 ref_exists(symref
.buf
)) {
265 die_if_checked_out(symref
.buf
, 0);
267 commit
= lookup_commit_reference_by_name(refname
);
269 die(_("invalid reference: %s"), refname
);
271 name
= worktree_basename(path
, &len
);
272 strbuf_add(&sb
, name
, path
+ len
- name
);
273 sanitize_refname_component(sb
.buf
, &sb_name
);
275 BUG("How come '%s' becomes empty after sanitization?", sb
.buf
);
278 git_path_buf(&sb_repo
, "worktrees/%s", name
);
280 if (safe_create_leading_directories_const(sb_repo
.buf
))
281 die_errno(_("could not create leading directories of '%s'"),
284 while (mkdir(sb_repo
.buf
, 0777)) {
286 if ((errno
!= EEXIST
) || !counter
/* overflow */)
287 die_errno(_("could not create directory of '%s'"),
289 strbuf_setlen(&sb_repo
, len
);
290 strbuf_addf(&sb_repo
, "%d", counter
);
292 name
= strrchr(sb_repo
.buf
, '/') + 1;
296 sigchain_push_common(remove_junk_on_signal
);
298 junk_git_dir
= xstrdup(sb_repo
.buf
);
302 * lock the incomplete repo so prune won't delete it, unlock
303 * after the preparation is over.
305 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
306 if (opts
->keep_locked
)
307 write_file(sb
.buf
, "%s", opts
->keep_locked
);
309 write_file(sb
.buf
, _("initializing"));
311 strbuf_addf(&sb_git
, "%s/.git", path
);
312 if (safe_create_leading_directories_const(sb_git
.buf
))
313 die_errno(_("could not create leading directories of '%s'"),
315 junk_work_tree
= xstrdup(path
);
318 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
319 strbuf_realpath(&realpath
, sb_git
.buf
, 1);
320 write_file(sb
.buf
, "%s", realpath
.buf
);
321 strbuf_realpath(&realpath
, get_git_common_dir(), 1);
322 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
325 * This is to keep resolve_ref() happy. We need a valid HEAD
326 * or is_git_directory() will reject the directory. Any value which
327 * looks like an object ID will do since it will be immediately
328 * replaced by the symbolic-ref or update-ref invocation in the new
332 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
333 write_file(sb
.buf
, "%s", oid_to_hex(null_oid()));
335 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
336 write_file(sb
.buf
, "../..");
338 strvec_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
339 strvec_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
343 strvec_pushl(&cp
.args
, "update-ref", "HEAD",
344 oid_to_hex(&commit
->object
.oid
), NULL
);
346 strvec_pushl(&cp
.args
, "symbolic-ref", "HEAD",
349 strvec_push(&cp
.args
, "--quiet");
352 cp
.env
= child_env
.v
;
353 ret
= run_command(&cp
);
357 if (opts
->checkout
) {
359 strvec_clear(&cp
.args
);
360 strvec_pushl(&cp
.args
, "reset", "--hard", "--no-recurse-submodules", NULL
);
362 strvec_push(&cp
.args
, "--quiet");
363 cp
.env
= child_env
.v
;
364 ret
= run_command(&cp
);
370 FREE_AND_NULL(junk_work_tree
);
371 FREE_AND_NULL(junk_git_dir
);
374 if (ret
|| !opts
->keep_locked
) {
376 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
377 unlink_or_warn(sb
.buf
);
381 * Hook failure does not warrant worktree deletion, so run hook after
382 * is_junk is cleared, but do return appropriate code when hook fails.
384 if (!ret
&& opts
->checkout
) {
385 const char *hook
= find_hook("post-checkout");
387 const char *env
[] = { "GIT_DIR", "GIT_WORK_TREE", NULL
};
390 cp
.stdout_to_stderr
= 1;
394 cp
.trace2_hook_name
= "post-checkout";
395 strvec_pushl(&cp
.args
, absolute_path(hook
),
396 oid_to_hex(null_oid()),
397 oid_to_hex(&commit
->object
.oid
),
399 ret
= run_command(&cp
);
403 strvec_clear(&child_env
);
405 strbuf_release(&symref
);
406 strbuf_release(&sb_repo
);
407 strbuf_release(&sb_git
);
408 strbuf_release(&sb_name
);
409 strbuf_release(&realpath
);
413 static void print_preparing_worktree_line(int detach
,
415 const char *new_branch
,
416 int force_new_branch
)
418 if (force_new_branch
) {
419 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
421 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
423 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
425 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
426 } else if (new_branch
) {
427 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
429 struct strbuf s
= STRBUF_INIT
;
430 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
432 printf_ln(_("Preparing worktree (checking out '%s')"),
435 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
437 die(_("invalid reference: %s"), branch
);
438 printf_ln(_("Preparing worktree (detached HEAD %s)"),
439 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
445 static const char *dwim_branch(const char *path
, const char **new_branch
)
449 const char *s
= worktree_basename(path
, &n
);
450 const char *branchname
= xstrndup(s
, n
);
451 struct strbuf ref
= STRBUF_INIT
;
455 branch_exists
= !strbuf_check_branch_ref(&ref
, branchname
) &&
457 strbuf_release(&ref
);
461 *new_branch
= branchname
;
463 struct object_id oid
;
465 unique_tracking_name(*new_branch
, &oid
, NULL
);
471 static int add(int ac
, const char **av
, const char *prefix
)
473 struct add_opts opts
;
474 const char *new_branch_force
= NULL
;
477 const char *new_branch
= NULL
;
478 const char *opt_track
= NULL
;
479 const char *lock_reason
= NULL
;
481 struct option options
[] = {
482 OPT__FORCE(&opts
.force
,
483 N_("checkout <branch> even if already checked out in other worktree"),
484 PARSE_OPT_NOCOMPLETE
),
485 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
486 N_("create a new branch")),
487 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
488 N_("create or reset a branch")),
489 OPT_BOOL('d', "detach", &opts
.detach
, N_("detach HEAD at named commit")),
490 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
491 OPT_BOOL(0, "lock", &keep_locked
, N_("keep the new working tree locked")),
492 OPT_STRING(0, "reason", &lock_reason
, N_("string"),
493 N_("reason for locking")),
494 OPT__QUIET(&opts
.quiet
, N_("suppress progress reporting")),
495 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
496 N_("set up tracking mode (see git-branch(1))"),
497 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
498 OPT_BOOL(0, "guess-remote", &guess_remote
,
499 N_("try to match the new branch name with a remote-tracking branch")),
503 memset(&opts
, 0, sizeof(opts
));
505 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
506 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
507 die(_("-b, -B, and --detach are mutually exclusive"));
508 if (lock_reason
&& !keep_locked
)
509 die(_("--reason requires --lock"));
511 opts
.keep_locked
= lock_reason
;
512 else if (keep_locked
)
513 opts
.keep_locked
= _("added with --lock");
515 if (ac
< 1 || ac
> 2)
516 usage_with_options(worktree_usage
, options
);
518 path
= prefix_filename(prefix
, av
[0]);
519 branch
= ac
< 2 ? "HEAD" : av
[1];
521 if (!strcmp(branch
, "-"))
524 if (new_branch_force
) {
525 struct strbuf symref
= STRBUF_INIT
;
527 new_branch
= new_branch_force
;
530 !strbuf_check_branch_ref(&symref
, new_branch
) &&
531 ref_exists(symref
.buf
))
532 die_if_checked_out(symref
.buf
, 0);
533 strbuf_release(&symref
);
536 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
537 const char *s
= dwim_branch(path
, &new_branch
);
542 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
543 struct object_id oid
;
544 struct commit
*commit
;
547 commit
= lookup_commit_reference_by_name(branch
);
549 remote
= unique_tracking_name(branch
, &oid
, NULL
);
557 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
560 struct child_process cp
= CHILD_PROCESS_INIT
;
562 strvec_push(&cp
.args
, "branch");
563 if (new_branch_force
)
564 strvec_push(&cp
.args
, "--force");
566 strvec_push(&cp
.args
, "--quiet");
567 strvec_push(&cp
.args
, new_branch
);
568 strvec_push(&cp
.args
, branch
);
570 strvec_push(&cp
.args
, opt_track
);
571 if (run_command(&cp
))
574 } else if (opt_track
) {
575 die(_("--[no-]track can only be used if a new branch is created"));
580 return add_worktree(path
, branch
, &opts
);
583 static void show_worktree_porcelain(struct worktree
*wt
)
587 printf("worktree %s\n", wt
->path
);
591 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
593 printf("detached\n");
594 else if (wt
->head_ref
)
595 printf("branch %s\n", wt
->head_ref
);
598 reason
= worktree_lock_reason(wt
);
599 if (reason
&& *reason
) {
600 struct strbuf sb
= STRBUF_INIT
;
601 quote_c_style(reason
, &sb
, NULL
, 0);
602 printf("locked %s\n", sb
.buf
);
607 reason
= worktree_prune_reason(wt
, expire
);
609 printf("prunable %s\n", reason
);
614 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
616 struct strbuf sb
= STRBUF_INIT
;
617 int cur_path_len
= strlen(wt
->path
);
618 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
621 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
623 strbuf_addstr(&sb
, "(bare)");
625 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
626 find_unique_abbrev(&wt
->head_oid
, DEFAULT_ABBREV
));
628 strbuf_addstr(&sb
, "(detached HEAD)");
629 else if (wt
->head_ref
) {
630 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
631 strbuf_addf(&sb
, "[%s]", ref
);
634 strbuf_addstr(&sb
, "(error)");
637 reason
= worktree_lock_reason(wt
);
638 if (verbose
&& reason
&& *reason
)
639 strbuf_addf(&sb
, "\n\tlocked: %s", reason
);
641 strbuf_addstr(&sb
, " locked");
643 reason
= worktree_prune_reason(wt
, expire
);
644 if (verbose
&& reason
)
645 strbuf_addf(&sb
, "\n\tprunable: %s", reason
);
647 strbuf_addstr(&sb
, " prunable");
649 printf("%s\n", sb
.buf
);
653 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
657 for (i
= 0; wt
[i
]; i
++) {
659 int path_len
= strlen(wt
[i
]->path
);
661 if (path_len
> *maxlen
)
663 sha1_len
= strlen(find_unique_abbrev(&wt
[i
]->head_oid
, *abbrev
));
664 if (sha1_len
> *abbrev
)
669 static int pathcmp(const void *a_
, const void *b_
)
671 const struct worktree
*const *a
= a_
;
672 const struct worktree
*const *b
= b_
;
673 return fspathcmp((*a
)->path
, (*b
)->path
);
676 static void pathsort(struct worktree
**wt
)
679 struct worktree
**p
= wt
;
683 QSORT(wt
, n
, pathcmp
);
686 static int list(int ac
, const char **av
, const char *prefix
)
690 struct option options
[] = {
691 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
692 OPT__VERBOSE(&verbose
, N_("show extended annotations and reasons, if available")),
693 OPT_EXPIRY_DATE(0, "expire", &expire
,
694 N_("add 'prunable' annotation to worktrees older than <time>")),
699 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
701 usage_with_options(worktree_usage
, options
);
702 else if (verbose
&& porcelain
)
703 die(_("--verbose and --porcelain are mutually exclusive"));
705 struct worktree
**worktrees
= get_worktrees();
706 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
708 /* sort worktrees by path but keep main worktree at top */
709 pathsort(worktrees
+ 1);
712 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
714 for (i
= 0; worktrees
[i
]; i
++) {
716 show_worktree_porcelain(worktrees
[i
]);
718 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
720 free_worktrees(worktrees
);
725 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
727 const char *reason
= "", *old_reason
;
728 struct option options
[] = {
729 OPT_STRING(0, "reason", &reason
, N_("string"),
730 N_("reason for locking")),
733 struct worktree
**worktrees
, *wt
;
735 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
737 usage_with_options(worktree_usage
, options
);
739 worktrees
= get_worktrees();
740 wt
= find_worktree(worktrees
, prefix
, av
[0]);
742 die(_("'%s' is not a working tree"), av
[0]);
743 if (is_main_worktree(wt
))
744 die(_("The main working tree cannot be locked or unlocked"));
746 old_reason
= worktree_lock_reason(wt
);
749 die(_("'%s' is already locked, reason: %s"),
751 die(_("'%s' is already locked"), av
[0]);
754 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
756 free_worktrees(worktrees
);
760 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
762 struct option options
[] = {
765 struct worktree
**worktrees
, *wt
;
768 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
770 usage_with_options(worktree_usage
, options
);
772 worktrees
= get_worktrees();
773 wt
= find_worktree(worktrees
, prefix
, av
[0]);
775 die(_("'%s' is not a working tree"), av
[0]);
776 if (is_main_worktree(wt
))
777 die(_("The main working tree cannot be locked or unlocked"));
778 if (!worktree_lock_reason(wt
))
779 die(_("'%s' is not locked"), av
[0]);
780 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
781 free_worktrees(worktrees
);
785 static void validate_no_submodules(const struct worktree
*wt
)
787 struct index_state istate
= { NULL
};
788 struct strbuf path
= STRBUF_INIT
;
789 int i
, found_submodules
= 0;
791 if (is_directory(worktree_git_path(wt
, "modules"))) {
793 * There could be false positives, e.g. the "modules"
794 * directory exists but is empty. But it's a rare case and
795 * this simpler check is probably good enough for now.
797 found_submodules
= 1;
798 } else if (read_index_from(&istate
, worktree_git_path(wt
, "index"),
799 get_worktree_git_dir(wt
)) > 0) {
800 for (i
= 0; i
< istate
.cache_nr
; i
++) {
801 struct cache_entry
*ce
= istate
.cache
[i
];
804 if (!S_ISGITLINK(ce
->ce_mode
))
808 strbuf_addf(&path
, "%s/%s", wt
->path
, ce
->name
);
809 if (!is_submodule_populated_gently(path
.buf
, &err
))
812 found_submodules
= 1;
816 discard_index(&istate
);
817 strbuf_release(&path
);
819 if (found_submodules
)
820 die(_("working trees containing submodules cannot be moved or removed"));
823 static int move_worktree(int ac
, const char **av
, const char *prefix
)
826 struct option options
[] = {
828 N_("force move even if worktree is dirty or locked"),
829 PARSE_OPT_NOCOMPLETE
),
832 struct worktree
**worktrees
, *wt
;
833 struct strbuf dst
= STRBUF_INIT
;
834 struct strbuf errmsg
= STRBUF_INIT
;
835 const char *reason
= NULL
;
838 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
840 usage_with_options(worktree_usage
, options
);
842 path
= prefix_filename(prefix
, av
[1]);
843 strbuf_addstr(&dst
, path
);
846 worktrees
= get_worktrees();
847 wt
= find_worktree(worktrees
, prefix
, av
[0]);
849 die(_("'%s' is not a working tree"), av
[0]);
850 if (is_main_worktree(wt
))
851 die(_("'%s' is a main working tree"), av
[0]);
852 if (is_directory(dst
.buf
)) {
853 const char *sep
= find_last_dir_sep(wt
->path
);
856 die(_("could not figure out destination name from '%s'"),
858 strbuf_trim_trailing_dir_sep(&dst
);
859 strbuf_addstr(&dst
, sep
);
861 check_candidate_path(dst
.buf
, force
, worktrees
, "move");
863 validate_no_submodules(wt
);
866 reason
= worktree_lock_reason(wt
);
869 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
871 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
873 if (validate_worktree(wt
, &errmsg
, 0))
874 die(_("validation failed, cannot move working tree: %s"),
876 strbuf_release(&errmsg
);
878 if (rename(wt
->path
, dst
.buf
) == -1)
879 die_errno(_("failed to move '%s' to '%s'"), wt
->path
, dst
.buf
);
881 update_worktree_location(wt
, dst
.buf
);
883 strbuf_release(&dst
);
884 free_worktrees(worktrees
);
889 * Note, "git status --porcelain" is used to determine if it's safe to
890 * delete a whole worktree. "git status" does not ignore user
891 * configuration, so if a normal "git status" shows "clean" for the
892 * user, then it's ok to remove it.
894 * This assumption may be a bad one. We may want to ignore
895 * (potentially bad) user settings and only delete a worktree when
896 * it's absolutely safe to do so from _our_ point of view because we
899 static void check_clean_worktree(struct worktree
*wt
,
900 const char *original_path
)
902 struct child_process cp
;
907 * Until we sort this out, all submodules are "dirty" and
908 * will abort this function.
910 validate_no_submodules(wt
);
912 child_process_init(&cp
);
913 strvec_pushf(&cp
.env_array
, "%s=%s/.git",
914 GIT_DIR_ENVIRONMENT
, wt
->path
);
915 strvec_pushf(&cp
.env_array
, "%s=%s",
916 GIT_WORK_TREE_ENVIRONMENT
, wt
->path
);
917 strvec_pushl(&cp
.args
, "status",
918 "--porcelain", "--ignore-submodules=none",
923 ret
= start_command(&cp
);
925 die_errno(_("failed to run 'git status' on '%s'"),
927 ret
= xread(cp
.out
, buf
, sizeof(buf
));
929 die(_("'%s' contains modified or untracked files, use --force to delete it"),
932 ret
= finish_command(&cp
);
934 die_errno(_("failed to run 'git status' on '%s', code %d"),
938 static int delete_git_work_tree(struct worktree
*wt
)
940 struct strbuf sb
= STRBUF_INIT
;
943 strbuf_addstr(&sb
, wt
->path
);
944 if (remove_dir_recursively(&sb
, 0)) {
945 error_errno(_("failed to delete '%s'"), sb
.buf
);
952 static int remove_worktree(int ac
, const char **av
, const char *prefix
)
955 struct option options
[] = {
957 N_("force removal even if worktree is dirty or locked"),
958 PARSE_OPT_NOCOMPLETE
),
961 struct worktree
**worktrees
, *wt
;
962 struct strbuf errmsg
= STRBUF_INIT
;
963 const char *reason
= NULL
;
966 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
968 usage_with_options(worktree_usage
, options
);
970 worktrees
= get_worktrees();
971 wt
= find_worktree(worktrees
, prefix
, av
[0]);
973 die(_("'%s' is not a working tree"), av
[0]);
974 if (is_main_worktree(wt
))
975 die(_("'%s' is a main working tree"), av
[0]);
977 reason
= worktree_lock_reason(wt
);
980 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
982 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
984 if (validate_worktree(wt
, &errmsg
, WT_VALIDATE_WORKTREE_MISSING_OK
))
985 die(_("validation failed, cannot remove working tree: %s"),
987 strbuf_release(&errmsg
);
989 if (file_exists(wt
->path
)) {
991 check_clean_worktree(wt
, av
[0]);
993 ret
|= delete_git_work_tree(wt
);
996 * continue on even if ret is non-zero, there's no going back
999 ret
|= delete_git_dir(wt
->id
);
1000 delete_worktrees_dir_if_empty();
1002 free_worktrees(worktrees
);
1006 static void report_repair(int iserr
, const char *path
, const char *msg
, void *cb_data
)
1009 printf_ln(_("repair: %s: %s"), msg
, path
);
1011 int *exit_status
= (int *)cb_data
;
1012 fprintf_ln(stderr
, _("error: %s: %s"), msg
, path
);
1017 static int repair(int ac
, const char **av
, const char *prefix
)
1020 const char *self
[] = { ".", NULL
};
1021 struct option options
[] = {
1026 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
1027 p
= ac
> 0 ? av
: self
;
1029 repair_worktree_at_path(*p
, report_repair
, &rc
);
1030 repair_worktrees(report_repair
, &rc
);
1034 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
1036 struct option options
[] = {
1040 git_config(git_worktree_config
, NULL
);
1043 usage_with_options(worktree_usage
, options
);
1046 if (!strcmp(av
[1], "add"))
1047 return add(ac
- 1, av
+ 1, prefix
);
1048 if (!strcmp(av
[1], "prune"))
1049 return prune(ac
- 1, av
+ 1, prefix
);
1050 if (!strcmp(av
[1], "list"))
1051 return list(ac
- 1, av
+ 1, prefix
);
1052 if (!strcmp(av
[1], "lock"))
1053 return lock_worktree(ac
- 1, av
+ 1, prefix
);
1054 if (!strcmp(av
[1], "unlock"))
1055 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
1056 if (!strcmp(av
[1], "move"))
1057 return move_worktree(ac
- 1, av
+ 1, prefix
);
1058 if (!strcmp(av
[1], "remove"))
1059 return remove_worktree(ac
- 1, av
+ 1, prefix
);
1060 if (!strcmp(av
[1], "repair"))
1061 return repair(ac
- 1, av
+ 1, prefix
);
1062 usage_with_options(worktree_usage
, options
);