6 #include "parse-options.h"
10 #include "run-command.h"
12 #include "submodule.h"
17 static const char * const worktree_usage
[] = {
18 N_("git worktree add [<options>] <path> [<commit-ish>]"),
19 N_("git worktree list [<options>]"),
20 N_("git worktree lock [<options>] <path>"),
21 N_("git worktree move <worktree> <new-path>"),
22 N_("git worktree prune [<options>]"),
23 N_("git worktree remove [<options>] <worktree>"),
24 N_("git worktree unlock <path>"),
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
);
48 return git_default_config(var
, value
, cb
);
51 static int delete_git_dir(const char *id
)
53 struct strbuf sb
= STRBUF_INIT
;
56 strbuf_addstr(&sb
, git_common_path("worktrees/%s", id
));
57 ret
= remove_dir_recursively(&sb
, 0);
58 if (ret
< 0 && errno
== ENOTDIR
)
61 error_errno(_("failed to delete '%s'"), sb
.buf
);
66 static void delete_worktrees_dir_if_empty(void)
68 rmdir(git_path("worktrees")); /* ignore failed removal */
71 static void prune_worktree(const char *id
, const char *reason
)
73 if (show_only
|| verbose
)
74 printf_ln(_("Removing %s/%s: %s"), "worktrees", id
, reason
);
79 static int prune_cmp(const void *a
, const void *b
)
81 const struct string_list_item
*x
= a
;
82 const struct string_list_item
*y
= b
;
85 if ((c
= fspathcmp(x
->string
, y
->string
)))
88 * paths same; prune_dupes() removes all but the first worktree entry
89 * having the same path, so sort main worktree ('util' is NULL) above
90 * linked worktrees ('util' not NULL) since main worktree can't be
97 /* paths same; sort by .git/worktrees/<id> */
98 return strcmp(x
->util
, y
->util
);
101 static void prune_dups(struct string_list
*l
)
105 QSORT(l
->items
, l
->nr
, prune_cmp
);
106 for (i
= 1; i
< l
->nr
; i
++) {
107 if (!fspathcmp(l
->items
[i
].string
, l
->items
[i
- 1].string
))
108 prune_worktree(l
->items
[i
].util
, "duplicate entry");
112 static void prune_worktrees(void)
114 struct strbuf reason
= STRBUF_INIT
;
115 struct strbuf main_path
= STRBUF_INIT
;
116 struct string_list kept
= STRING_LIST_INIT_NODUP
;
117 DIR *dir
= opendir(git_path("worktrees"));
121 while ((d
= readdir(dir
)) != NULL
) {
123 if (is_dot_or_dotdot(d
->d_name
))
125 strbuf_reset(&reason
);
126 if (should_prune_worktree(d
->d_name
, &reason
, &path
, expire
))
127 prune_worktree(d
->d_name
, reason
.buf
);
129 string_list_append(&kept
, path
)->util
= xstrdup(d
->d_name
);
133 strbuf_add_absolute_path(&main_path
, get_git_common_dir());
134 /* massage main worktree absolute path to match 'gitdir' content */
135 strbuf_strip_suffix(&main_path
, "/.");
136 string_list_append(&kept
, strbuf_detach(&main_path
, NULL
));
138 string_list_clear(&kept
, 1);
141 delete_worktrees_dir_if_empty();
142 strbuf_release(&reason
);
145 static int prune(int ac
, const char **av
, const char *prefix
)
147 struct option options
[] = {
148 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
149 OPT__VERBOSE(&verbose
, N_("report pruned working trees")),
150 OPT_EXPIRY_DATE(0, "expire", &expire
,
151 N_("expire working trees older than <time>")),
156 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
158 usage_with_options(worktree_usage
, options
);
163 static char *junk_work_tree
;
164 static char *junk_git_dir
;
166 static pid_t junk_pid
;
168 static void remove_junk(void)
170 struct strbuf sb
= STRBUF_INIT
;
171 if (!is_junk
|| getpid() != junk_pid
)
174 strbuf_addstr(&sb
, junk_git_dir
);
175 remove_dir_recursively(&sb
, 0);
178 if (junk_work_tree
) {
179 strbuf_addstr(&sb
, junk_work_tree
);
180 remove_dir_recursively(&sb
, 0);
185 static void remove_junk_on_signal(int signo
)
192 static const char *worktree_basename(const char *path
, int *olen
)
198 while (len
&& is_dir_sep(path
[len
- 1]))
201 for (name
= path
+ len
- 1; name
> path
; name
--)
202 if (is_dir_sep(*name
)) {
211 /* check that path is viable location for worktree */
212 static void check_candidate_path(const char *path
,
214 struct worktree
**worktrees
,
220 if (file_exists(path
) && !is_empty_dir(path
))
221 die(_("'%s' already exists"), path
);
223 wt
= find_worktree_by_path(worktrees
, path
);
227 locked
= !!worktree_lock_reason(wt
);
228 if ((!locked
&& force
) || (locked
&& force
> 1)) {
229 if (delete_git_dir(wt
->id
))
230 die(_("unusable worktree destination '%s'"), path
);
235 die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path
, cmd
);
237 die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path
, cmd
);
240 static int add_worktree(const char *path
, const char *refname
,
241 const struct add_opts
*opts
)
243 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
244 struct strbuf sb
= STRBUF_INIT
, realpath
= STRBUF_INIT
;
246 struct child_process cp
= CHILD_PROCESS_INIT
;
247 struct strvec child_env
= STRVEC_INIT
;
248 unsigned int counter
= 0;
250 struct strbuf symref
= STRBUF_INIT
;
251 struct commit
*commit
= NULL
;
253 struct strbuf sb_name
= STRBUF_INIT
;
254 struct worktree
**worktrees
;
256 worktrees
= get_worktrees();
257 check_candidate_path(path
, opts
->force
, worktrees
, "add");
258 free_worktrees(worktrees
);
261 /* is 'refname' a branch or commit? */
262 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
263 ref_exists(symref
.buf
)) {
266 die_if_checked_out(symref
.buf
, 0);
268 commit
= lookup_commit_reference_by_name(refname
);
270 die(_("invalid reference: %s"), refname
);
272 name
= worktree_basename(path
, &len
);
273 strbuf_add(&sb
, name
, path
+ len
- name
);
274 sanitize_refname_component(sb
.buf
, &sb_name
);
276 BUG("How come '%s' becomes empty after sanitization?", sb
.buf
);
279 git_path_buf(&sb_repo
, "worktrees/%s", name
);
281 if (safe_create_leading_directories_const(sb_repo
.buf
))
282 die_errno(_("could not create leading directories of '%s'"),
285 while (mkdir(sb_repo
.buf
, 0777)) {
287 if ((errno
!= EEXIST
) || !counter
/* overflow */)
288 die_errno(_("could not create directory of '%s'"),
290 strbuf_setlen(&sb_repo
, len
);
291 strbuf_addf(&sb_repo
, "%d", counter
);
293 name
= strrchr(sb_repo
.buf
, '/') + 1;
297 sigchain_push_common(remove_junk_on_signal
);
299 junk_git_dir
= xstrdup(sb_repo
.buf
);
303 * lock the incomplete repo so prune won't delete it, unlock
304 * after the preparation is over.
306 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
307 if (!opts
->keep_locked
)
308 write_file(sb
.buf
, "initializing");
310 write_file(sb
.buf
, "added with --lock");
312 strbuf_addf(&sb_git
, "%s/.git", path
);
313 if (safe_create_leading_directories_const(sb_git
.buf
))
314 die_errno(_("could not create leading directories of '%s'"),
316 junk_work_tree
= xstrdup(path
);
319 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
320 strbuf_realpath(&realpath
, sb_git
.buf
, 1);
321 write_file(sb
.buf
, "%s", realpath
.buf
);
322 strbuf_realpath(&realpath
, get_git_common_dir(), 1);
323 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
326 * This is to keep resolve_ref() happy. We need a valid HEAD
327 * or is_git_directory() will reject the directory. Any value which
328 * looks like an object ID will do since it will be immediately
329 * replaced by the symbolic-ref or update-ref invocation in the new
333 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
334 write_file(sb
.buf
, "%s", oid_to_hex(&null_oid
));
336 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
337 write_file(sb
.buf
, "../..");
339 strvec_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
340 strvec_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
344 strvec_pushl(&cp
.args
, "update-ref", "HEAD",
345 oid_to_hex(&commit
->object
.oid
), NULL
);
347 strvec_pushl(&cp
.args
, "symbolic-ref", "HEAD",
350 strvec_push(&cp
.args
, "--quiet");
353 cp
.env
= child_env
.v
;
354 ret
= run_command(&cp
);
358 if (opts
->checkout
) {
360 strvec_clear(&cp
.args
);
361 strvec_pushl(&cp
.args
, "reset", "--hard", "--no-recurse-submodules", NULL
);
363 strvec_push(&cp
.args
, "--quiet");
364 cp
.env
= child_env
.v
;
365 ret
= run_command(&cp
);
371 FREE_AND_NULL(junk_work_tree
);
372 FREE_AND_NULL(junk_git_dir
);
375 if (ret
|| !opts
->keep_locked
) {
377 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
378 unlink_or_warn(sb
.buf
);
382 * Hook failure does not warrant worktree deletion, so run hook after
383 * is_junk is cleared, but do return appropriate code when hook fails.
385 if (!ret
&& opts
->checkout
) {
386 const char *hook
= find_hook("post-checkout");
388 const char *env
[] = { "GIT_DIR", "GIT_WORK_TREE", NULL
};
391 cp
.stdout_to_stderr
= 1;
395 cp
.trace2_hook_name
= "post-checkout";
396 strvec_pushl(&cp
.args
, absolute_path(hook
),
397 oid_to_hex(&null_oid
),
398 oid_to_hex(&commit
->object
.oid
),
400 ret
= run_command(&cp
);
404 strvec_clear(&child_env
);
406 strbuf_release(&symref
);
407 strbuf_release(&sb_repo
);
408 strbuf_release(&sb_git
);
409 strbuf_release(&sb_name
);
410 strbuf_release(&realpath
);
414 static void print_preparing_worktree_line(int detach
,
416 const char *new_branch
,
417 int force_new_branch
)
419 if (force_new_branch
) {
420 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
422 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
424 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
426 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
427 } else if (new_branch
) {
428 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
430 struct strbuf s
= STRBUF_INIT
;
431 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
433 printf_ln(_("Preparing worktree (checking out '%s')"),
436 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
438 die(_("invalid reference: %s"), branch
);
439 printf_ln(_("Preparing worktree (detached HEAD %s)"),
440 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
446 static const char *dwim_branch(const char *path
, const char **new_branch
)
450 const char *s
= worktree_basename(path
, &n
);
451 const char *branchname
= xstrndup(s
, n
);
452 struct strbuf ref
= STRBUF_INIT
;
456 branch_exists
= !strbuf_check_branch_ref(&ref
, branchname
) &&
458 strbuf_release(&ref
);
462 *new_branch
= branchname
;
464 struct object_id oid
;
466 unique_tracking_name(*new_branch
, &oid
, NULL
);
472 static int add(int ac
, const char **av
, const char *prefix
)
474 struct add_opts opts
;
475 const char *new_branch_force
= NULL
;
478 const char *new_branch
= NULL
;
479 const char *opt_track
= NULL
;
480 struct option options
[] = {
481 OPT__FORCE(&opts
.force
,
482 N_("checkout <branch> even if already checked out in other worktree"),
483 PARSE_OPT_NOCOMPLETE
),
484 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
485 N_("create a new branch")),
486 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
487 N_("create or reset a branch")),
488 OPT_BOOL('d', "detach", &opts
.detach
, N_("detach HEAD at named commit")),
489 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
490 OPT_BOOL(0, "lock", &opts
.keep_locked
, N_("keep the new working tree locked")),
491 OPT__QUIET(&opts
.quiet
, N_("suppress progress reporting")),
492 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
493 N_("set up tracking mode (see git-branch(1))"),
494 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
495 OPT_BOOL(0, "guess-remote", &guess_remote
,
496 N_("try to match the new branch name with a remote-tracking branch")),
500 memset(&opts
, 0, sizeof(opts
));
502 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
503 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
504 die(_("-b, -B, and --detach are mutually exclusive"));
505 if (ac
< 1 || ac
> 2)
506 usage_with_options(worktree_usage
, options
);
508 path
= prefix_filename(prefix
, av
[0]);
509 branch
= ac
< 2 ? "HEAD" : av
[1];
511 if (!strcmp(branch
, "-"))
514 if (new_branch_force
) {
515 struct strbuf symref
= STRBUF_INIT
;
517 new_branch
= new_branch_force
;
520 !strbuf_check_branch_ref(&symref
, new_branch
) &&
521 ref_exists(symref
.buf
))
522 die_if_checked_out(symref
.buf
, 0);
523 strbuf_release(&symref
);
526 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
527 const char *s
= dwim_branch(path
, &new_branch
);
532 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
533 struct object_id oid
;
534 struct commit
*commit
;
537 commit
= lookup_commit_reference_by_name(branch
);
539 remote
= unique_tracking_name(branch
, &oid
, NULL
);
547 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
550 struct child_process cp
= CHILD_PROCESS_INIT
;
552 strvec_push(&cp
.args
, "branch");
553 if (new_branch_force
)
554 strvec_push(&cp
.args
, "--force");
556 strvec_push(&cp
.args
, "--quiet");
557 strvec_push(&cp
.args
, new_branch
);
558 strvec_push(&cp
.args
, branch
);
560 strvec_push(&cp
.args
, opt_track
);
561 if (run_command(&cp
))
564 } else if (opt_track
) {
565 die(_("--[no-]track can only be used if a new branch is created"));
570 return add_worktree(path
, branch
, &opts
);
573 static void show_worktree_porcelain(struct worktree
*wt
)
577 printf("worktree %s\n", wt
->path
);
581 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
583 printf("detached\n");
584 else if (wt
->head_ref
)
585 printf("branch %s\n", wt
->head_ref
);
588 reason
= worktree_lock_reason(wt
);
589 if (reason
&& *reason
) {
590 struct strbuf sb
= STRBUF_INIT
;
591 quote_c_style(reason
, &sb
, NULL
, 0);
592 printf("locked %s\n", sb
.buf
);
597 reason
= worktree_prune_reason(wt
, expire
);
599 printf("prunable %s\n", reason
);
604 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
606 struct strbuf sb
= STRBUF_INIT
;
607 int cur_path_len
= strlen(wt
->path
);
608 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
611 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
613 strbuf_addstr(&sb
, "(bare)");
615 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
616 find_unique_abbrev(&wt
->head_oid
, DEFAULT_ABBREV
));
618 strbuf_addstr(&sb
, "(detached HEAD)");
619 else if (wt
->head_ref
) {
620 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
621 strbuf_addf(&sb
, "[%s]", ref
);
624 strbuf_addstr(&sb
, "(error)");
627 reason
= worktree_lock_reason(wt
);
628 if (verbose
&& reason
&& *reason
)
629 strbuf_addf(&sb
, "\n\tlocked: %s", reason
);
631 strbuf_addstr(&sb
, " locked");
633 reason
= worktree_prune_reason(wt
, expire
);
634 if (verbose
&& reason
)
635 strbuf_addf(&sb
, "\n\tprunable: %s", reason
);
637 strbuf_addstr(&sb
, " prunable");
639 printf("%s\n", sb
.buf
);
643 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
647 for (i
= 0; wt
[i
]; i
++) {
649 int path_len
= strlen(wt
[i
]->path
);
651 if (path_len
> *maxlen
)
653 sha1_len
= strlen(find_unique_abbrev(&wt
[i
]->head_oid
, *abbrev
));
654 if (sha1_len
> *abbrev
)
659 static int pathcmp(const void *a_
, const void *b_
)
661 const struct worktree
*const *a
= a_
;
662 const struct worktree
*const *b
= b_
;
663 return fspathcmp((*a
)->path
, (*b
)->path
);
666 static void pathsort(struct worktree
**wt
)
669 struct worktree
**p
= wt
;
673 QSORT(wt
, n
, pathcmp
);
676 static int list(int ac
, const char **av
, const char *prefix
)
680 struct option options
[] = {
681 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
682 OPT__VERBOSE(&verbose
, N_("show extended annotations and reasons, if available")),
683 OPT_EXPIRY_DATE(0, "expire", &expire
,
684 N_("add 'prunable' annotation to worktrees older than <time>")),
689 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
691 usage_with_options(worktree_usage
, options
);
692 else if (verbose
&& porcelain
)
693 die(_("--verbose and --porcelain are mutually exclusive"));
695 struct worktree
**worktrees
= get_worktrees();
696 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
698 /* sort worktrees by path but keep main worktree at top */
699 pathsort(worktrees
+ 1);
702 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
704 for (i
= 0; worktrees
[i
]; i
++) {
706 show_worktree_porcelain(worktrees
[i
]);
708 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
710 free_worktrees(worktrees
);
715 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
717 const char *reason
= "", *old_reason
;
718 struct option options
[] = {
719 OPT_STRING(0, "reason", &reason
, N_("string"),
720 N_("reason for locking")),
723 struct worktree
**worktrees
, *wt
;
725 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
727 usage_with_options(worktree_usage
, options
);
729 worktrees
= get_worktrees();
730 wt
= find_worktree(worktrees
, prefix
, av
[0]);
732 die(_("'%s' is not a working tree"), av
[0]);
733 if (is_main_worktree(wt
))
734 die(_("The main working tree cannot be locked or unlocked"));
736 old_reason
= worktree_lock_reason(wt
);
739 die(_("'%s' is already locked, reason: %s"),
741 die(_("'%s' is already locked"), av
[0]);
744 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
746 free_worktrees(worktrees
);
750 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
752 struct option options
[] = {
755 struct worktree
**worktrees
, *wt
;
758 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
760 usage_with_options(worktree_usage
, options
);
762 worktrees
= get_worktrees();
763 wt
= find_worktree(worktrees
, prefix
, av
[0]);
765 die(_("'%s' is not a working tree"), av
[0]);
766 if (is_main_worktree(wt
))
767 die(_("The main working tree cannot be locked or unlocked"));
768 if (!worktree_lock_reason(wt
))
769 die(_("'%s' is not locked"), av
[0]);
770 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
771 free_worktrees(worktrees
);
775 static void validate_no_submodules(const struct worktree
*wt
)
777 struct index_state istate
= { NULL
};
778 struct strbuf path
= STRBUF_INIT
;
779 int i
, found_submodules
= 0;
781 if (is_directory(worktree_git_path(wt
, "modules"))) {
783 * There could be false positives, e.g. the "modules"
784 * directory exists but is empty. But it's a rare case and
785 * this simpler check is probably good enough for now.
787 found_submodules
= 1;
788 } else if (read_index_from(&istate
, worktree_git_path(wt
, "index"),
789 get_worktree_git_dir(wt
)) > 0) {
790 for (i
= 0; i
< istate
.cache_nr
; i
++) {
791 struct cache_entry
*ce
= istate
.cache
[i
];
794 if (!S_ISGITLINK(ce
->ce_mode
))
798 strbuf_addf(&path
, "%s/%s", wt
->path
, ce
->name
);
799 if (!is_submodule_populated_gently(path
.buf
, &err
))
802 found_submodules
= 1;
806 discard_index(&istate
);
807 strbuf_release(&path
);
809 if (found_submodules
)
810 die(_("working trees containing submodules cannot be moved or removed"));
813 static int move_worktree(int ac
, const char **av
, const char *prefix
)
816 struct option options
[] = {
818 N_("force move even if worktree is dirty or locked"),
819 PARSE_OPT_NOCOMPLETE
),
822 struct worktree
**worktrees
, *wt
;
823 struct strbuf dst
= STRBUF_INIT
;
824 struct strbuf errmsg
= STRBUF_INIT
;
825 const char *reason
= NULL
;
828 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
830 usage_with_options(worktree_usage
, options
);
832 path
= prefix_filename(prefix
, av
[1]);
833 strbuf_addstr(&dst
, path
);
836 worktrees
= get_worktrees();
837 wt
= find_worktree(worktrees
, prefix
, av
[0]);
839 die(_("'%s' is not a working tree"), av
[0]);
840 if (is_main_worktree(wt
))
841 die(_("'%s' is a main working tree"), av
[0]);
842 if (is_directory(dst
.buf
)) {
843 const char *sep
= find_last_dir_sep(wt
->path
);
846 die(_("could not figure out destination name from '%s'"),
848 strbuf_trim_trailing_dir_sep(&dst
);
849 strbuf_addstr(&dst
, sep
);
851 check_candidate_path(dst
.buf
, force
, worktrees
, "move");
853 validate_no_submodules(wt
);
856 reason
= worktree_lock_reason(wt
);
859 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
861 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
863 if (validate_worktree(wt
, &errmsg
, 0))
864 die(_("validation failed, cannot move working tree: %s"),
866 strbuf_release(&errmsg
);
868 if (rename(wt
->path
, dst
.buf
) == -1)
869 die_errno(_("failed to move '%s' to '%s'"), wt
->path
, dst
.buf
);
871 update_worktree_location(wt
, dst
.buf
);
873 strbuf_release(&dst
);
874 free_worktrees(worktrees
);
879 * Note, "git status --porcelain" is used to determine if it's safe to
880 * delete a whole worktree. "git status" does not ignore user
881 * configuration, so if a normal "git status" shows "clean" for the
882 * user, then it's ok to remove it.
884 * This assumption may be a bad one. We may want to ignore
885 * (potentially bad) user settings and only delete a worktree when
886 * it's absolutely safe to do so from _our_ point of view because we
889 static void check_clean_worktree(struct worktree
*wt
,
890 const char *original_path
)
892 struct child_process cp
;
897 * Until we sort this out, all submodules are "dirty" and
898 * will abort this function.
900 validate_no_submodules(wt
);
902 child_process_init(&cp
);
903 strvec_pushf(&cp
.env_array
, "%s=%s/.git",
904 GIT_DIR_ENVIRONMENT
, wt
->path
);
905 strvec_pushf(&cp
.env_array
, "%s=%s",
906 GIT_WORK_TREE_ENVIRONMENT
, wt
->path
);
907 strvec_pushl(&cp
.args
, "status",
908 "--porcelain", "--ignore-submodules=none",
913 ret
= start_command(&cp
);
915 die_errno(_("failed to run 'git status' on '%s'"),
917 ret
= xread(cp
.out
, buf
, sizeof(buf
));
919 die(_("'%s' contains modified or untracked files, use --force to delete it"),
922 ret
= finish_command(&cp
);
924 die_errno(_("failed to run 'git status' on '%s', code %d"),
928 static int delete_git_work_tree(struct worktree
*wt
)
930 struct strbuf sb
= STRBUF_INIT
;
933 strbuf_addstr(&sb
, wt
->path
);
934 if (remove_dir_recursively(&sb
, 0)) {
935 error_errno(_("failed to delete '%s'"), sb
.buf
);
942 static int remove_worktree(int ac
, const char **av
, const char *prefix
)
945 struct option options
[] = {
947 N_("force removal even if worktree is dirty or locked"),
948 PARSE_OPT_NOCOMPLETE
),
951 struct worktree
**worktrees
, *wt
;
952 struct strbuf errmsg
= STRBUF_INIT
;
953 const char *reason
= NULL
;
956 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
958 usage_with_options(worktree_usage
, options
);
960 worktrees
= get_worktrees();
961 wt
= find_worktree(worktrees
, prefix
, av
[0]);
963 die(_("'%s' is not a working tree"), av
[0]);
964 if (is_main_worktree(wt
))
965 die(_("'%s' is a main working tree"), av
[0]);
967 reason
= worktree_lock_reason(wt
);
970 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
972 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
974 if (validate_worktree(wt
, &errmsg
, WT_VALIDATE_WORKTREE_MISSING_OK
))
975 die(_("validation failed, cannot remove working tree: %s"),
977 strbuf_release(&errmsg
);
979 if (file_exists(wt
->path
)) {
981 check_clean_worktree(wt
, av
[0]);
983 ret
|= delete_git_work_tree(wt
);
986 * continue on even if ret is non-zero, there's no going back
989 ret
|= delete_git_dir(wt
->id
);
990 delete_worktrees_dir_if_empty();
992 free_worktrees(worktrees
);
996 static void report_repair(int iserr
, const char *path
, const char *msg
, void *cb_data
)
999 printf_ln(_("repair: %s: %s"), msg
, path
);
1001 int *exit_status
= (int *)cb_data
;
1002 fprintf_ln(stderr
, _("error: %s: %s"), msg
, path
);
1007 static int repair(int ac
, const char **av
, const char *prefix
)
1010 const char *self
[] = { ".", NULL
};
1011 struct option options
[] = {
1016 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
1017 p
= ac
> 0 ? av
: self
;
1019 repair_worktree_at_path(*p
, report_repair
, &rc
);
1020 repair_worktrees(report_repair
, &rc
);
1024 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
1026 struct option options
[] = {
1030 git_config(git_worktree_config
, NULL
);
1033 usage_with_options(worktree_usage
, options
);
1036 if (!strcmp(av
[1], "add"))
1037 return add(ac
- 1, av
+ 1, prefix
);
1038 if (!strcmp(av
[1], "prune"))
1039 return prune(ac
- 1, av
+ 1, prefix
);
1040 if (!strcmp(av
[1], "list"))
1041 return list(ac
- 1, av
+ 1, prefix
);
1042 if (!strcmp(av
[1], "lock"))
1043 return lock_worktree(ac
- 1, av
+ 1, prefix
);
1044 if (!strcmp(av
[1], "unlock"))
1045 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
1046 if (!strcmp(av
[1], "move"))
1047 return move_worktree(ac
- 1, av
+ 1, prefix
);
1048 if (!strcmp(av
[1], "remove"))
1049 return remove_worktree(ac
- 1, av
+ 1, prefix
);
1050 if (!strcmp(av
[1], "repair"))
1051 return repair(ac
- 1, av
+ 1, prefix
);
1052 usage_with_options(worktree_usage
, options
);