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>"),
33 const char *keep_locked
;
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_skip_dot_and_dotdot(dir
)) != NULL
) {
123 strbuf_reset(&reason
);
124 if (should_prune_worktree(d
->d_name
, &reason
, &path
, expire
))
125 prune_worktree(d
->d_name
, reason
.buf
);
127 string_list_append(&kept
, path
)->util
= xstrdup(d
->d_name
);
131 strbuf_add_absolute_path(&main_path
, get_git_common_dir());
132 /* massage main worktree absolute path to match 'gitdir' content */
133 strbuf_strip_suffix(&main_path
, "/.");
134 string_list_append(&kept
, strbuf_detach(&main_path
, NULL
));
136 string_list_clear(&kept
, 1);
139 delete_worktrees_dir_if_empty();
140 strbuf_release(&reason
);
143 static int prune(int ac
, const char **av
, const char *prefix
)
145 struct option options
[] = {
146 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
147 OPT__VERBOSE(&verbose
, N_("report pruned working trees")),
148 OPT_EXPIRY_DATE(0, "expire", &expire
,
149 N_("expire working trees older than <time>")),
154 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
156 usage_with_options(worktree_usage
, options
);
161 static char *junk_work_tree
;
162 static char *junk_git_dir
;
164 static pid_t junk_pid
;
166 static void remove_junk(void)
168 struct strbuf sb
= STRBUF_INIT
;
169 if (!is_junk
|| getpid() != junk_pid
)
172 strbuf_addstr(&sb
, junk_git_dir
);
173 remove_dir_recursively(&sb
, 0);
176 if (junk_work_tree
) {
177 strbuf_addstr(&sb
, junk_work_tree
);
178 remove_dir_recursively(&sb
, 0);
183 static void remove_junk_on_signal(int signo
)
190 static const char *worktree_basename(const char *path
, int *olen
)
196 while (len
&& is_dir_sep(path
[len
- 1]))
199 for (name
= path
+ len
- 1; name
> path
; name
--)
200 if (is_dir_sep(*name
)) {
209 /* check that path is viable location for worktree */
210 static void check_candidate_path(const char *path
,
212 struct worktree
**worktrees
,
218 if (file_exists(path
) && !is_empty_dir(path
))
219 die(_("'%s' already exists"), path
);
221 wt
= find_worktree_by_path(worktrees
, path
);
225 locked
= !!worktree_lock_reason(wt
);
226 if ((!locked
&& force
) || (locked
&& force
> 1)) {
227 if (delete_git_dir(wt
->id
))
228 die(_("unusable worktree destination '%s'"), path
);
233 die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path
, cmd
);
235 die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path
, cmd
);
238 static int add_worktree(const char *path
, const char *refname
,
239 const struct add_opts
*opts
)
241 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
242 struct strbuf sb
= STRBUF_INIT
, realpath
= STRBUF_INIT
;
244 struct child_process cp
= CHILD_PROCESS_INIT
;
245 struct strvec child_env
= STRVEC_INIT
;
246 unsigned int counter
= 0;
248 struct strbuf symref
= STRBUF_INIT
;
249 struct commit
*commit
= NULL
;
251 struct strbuf sb_name
= STRBUF_INIT
;
252 struct worktree
**worktrees
;
254 worktrees
= get_worktrees();
255 check_candidate_path(path
, opts
->force
, worktrees
, "add");
256 free_worktrees(worktrees
);
259 /* is 'refname' a branch or commit? */
260 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
261 ref_exists(symref
.buf
)) {
264 die_if_checked_out(symref
.buf
, 0);
266 commit
= lookup_commit_reference_by_name(refname
);
268 die(_("invalid reference: %s"), refname
);
270 name
= worktree_basename(path
, &len
);
271 strbuf_add(&sb
, name
, path
+ len
- name
);
272 sanitize_refname_component(sb
.buf
, &sb_name
);
274 BUG("How come '%s' becomes empty after sanitization?", sb
.buf
);
277 git_path_buf(&sb_repo
, "worktrees/%s", name
);
279 if (safe_create_leading_directories_const(sb_repo
.buf
))
280 die_errno(_("could not create leading directories of '%s'"),
283 while (mkdir(sb_repo
.buf
, 0777)) {
285 if ((errno
!= EEXIST
) || !counter
/* overflow */)
286 die_errno(_("could not create directory of '%s'"),
288 strbuf_setlen(&sb_repo
, len
);
289 strbuf_addf(&sb_repo
, "%d", counter
);
291 name
= strrchr(sb_repo
.buf
, '/') + 1;
295 sigchain_push_common(remove_junk_on_signal
);
297 junk_git_dir
= xstrdup(sb_repo
.buf
);
301 * lock the incomplete repo so prune won't delete it, unlock
302 * after the preparation is over.
304 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
305 if (opts
->keep_locked
)
306 write_file(sb
.buf
, "%s", opts
->keep_locked
);
308 write_file(sb
.buf
, _("initializing"));
310 strbuf_addf(&sb_git
, "%s/.git", path
);
311 if (safe_create_leading_directories_const(sb_git
.buf
))
312 die_errno(_("could not create leading directories of '%s'"),
314 junk_work_tree
= xstrdup(path
);
317 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
318 strbuf_realpath(&realpath
, sb_git
.buf
, 1);
319 write_file(sb
.buf
, "%s", realpath
.buf
);
320 strbuf_realpath(&realpath
, get_git_common_dir(), 1);
321 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
324 * This is to keep resolve_ref() happy. We need a valid HEAD
325 * or is_git_directory() will reject the directory. Any value which
326 * looks like an object ID will do since it will be immediately
327 * replaced by the symbolic-ref or update-ref invocation in the new
331 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
332 write_file(sb
.buf
, "%s", oid_to_hex(null_oid()));
334 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
335 write_file(sb
.buf
, "../..");
337 strvec_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
338 strvec_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
342 strvec_pushl(&cp
.args
, "update-ref", "HEAD",
343 oid_to_hex(&commit
->object
.oid
), NULL
);
345 strvec_pushl(&cp
.args
, "symbolic-ref", "HEAD",
348 strvec_push(&cp
.args
, "--quiet");
351 cp
.env
= child_env
.v
;
352 ret
= run_command(&cp
);
356 if (opts
->checkout
) {
358 strvec_clear(&cp
.args
);
359 strvec_pushl(&cp
.args
, "reset", "--hard", "--no-recurse-submodules", NULL
);
361 strvec_push(&cp
.args
, "--quiet");
362 cp
.env
= child_env
.v
;
363 ret
= run_command(&cp
);
369 FREE_AND_NULL(junk_work_tree
);
370 FREE_AND_NULL(junk_git_dir
);
373 if (ret
|| !opts
->keep_locked
) {
375 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
376 unlink_or_warn(sb
.buf
);
380 * Hook failure does not warrant worktree deletion, so run hook after
381 * is_junk is cleared, but do return appropriate code when hook fails.
383 if (!ret
&& opts
->checkout
) {
384 const char *hook
= find_hook("post-checkout");
386 const char *env
[] = { "GIT_DIR", "GIT_WORK_TREE", NULL
};
389 cp
.stdout_to_stderr
= 1;
393 cp
.trace2_hook_name
= "post-checkout";
394 strvec_pushl(&cp
.args
, absolute_path(hook
),
395 oid_to_hex(null_oid()),
396 oid_to_hex(&commit
->object
.oid
),
398 ret
= run_command(&cp
);
402 strvec_clear(&child_env
);
404 strbuf_release(&symref
);
405 strbuf_release(&sb_repo
);
406 strbuf_release(&sb_git
);
407 strbuf_release(&sb_name
);
408 strbuf_release(&realpath
);
412 static void print_preparing_worktree_line(int detach
,
414 const char *new_branch
,
415 int force_new_branch
)
417 if (force_new_branch
) {
418 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
420 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
422 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
424 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
425 } else if (new_branch
) {
426 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
428 struct strbuf s
= STRBUF_INIT
;
429 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
431 printf_ln(_("Preparing worktree (checking out '%s')"),
434 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
436 die(_("invalid reference: %s"), branch
);
437 printf_ln(_("Preparing worktree (detached HEAD %s)"),
438 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
444 static const char *dwim_branch(const char *path
, const char **new_branch
)
448 const char *s
= worktree_basename(path
, &n
);
449 const char *branchname
= xstrndup(s
, n
);
450 struct strbuf ref
= STRBUF_INIT
;
454 branch_exists
= !strbuf_check_branch_ref(&ref
, branchname
) &&
456 strbuf_release(&ref
);
460 *new_branch
= branchname
;
462 struct object_id oid
;
464 unique_tracking_name(*new_branch
, &oid
, NULL
);
470 static int add(int ac
, const char **av
, const char *prefix
)
472 struct add_opts opts
;
473 const char *new_branch_force
= NULL
;
476 const char *new_branch
= NULL
;
477 const char *opt_track
= NULL
;
478 const char *lock_reason
= 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", &keep_locked
, N_("keep the new working tree locked")),
491 OPT_STRING(0, "reason", &lock_reason
, N_("string"),
492 N_("reason for locking")),
493 OPT__QUIET(&opts
.quiet
, N_("suppress progress reporting")),
494 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
495 N_("set up tracking mode (see git-branch(1))"),
496 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
497 OPT_BOOL(0, "guess-remote", &guess_remote
,
498 N_("try to match the new branch name with a remote-tracking branch")),
502 memset(&opts
, 0, sizeof(opts
));
504 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
505 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
506 die(_("-b, -B, and --detach are mutually exclusive"));
507 if (lock_reason
&& !keep_locked
)
508 die(_("--reason requires --lock"));
510 opts
.keep_locked
= lock_reason
;
511 else if (keep_locked
)
512 opts
.keep_locked
= _("added with --lock");
514 if (ac
< 1 || ac
> 2)
515 usage_with_options(worktree_usage
, options
);
517 path
= prefix_filename(prefix
, av
[0]);
518 branch
= ac
< 2 ? "HEAD" : av
[1];
520 if (!strcmp(branch
, "-"))
523 if (new_branch_force
) {
524 struct strbuf symref
= STRBUF_INIT
;
526 new_branch
= new_branch_force
;
529 !strbuf_check_branch_ref(&symref
, new_branch
) &&
530 ref_exists(symref
.buf
))
531 die_if_checked_out(symref
.buf
, 0);
532 strbuf_release(&symref
);
535 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
536 const char *s
= dwim_branch(path
, &new_branch
);
541 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
542 struct object_id oid
;
543 struct commit
*commit
;
546 commit
= lookup_commit_reference_by_name(branch
);
548 remote
= unique_tracking_name(branch
, &oid
, NULL
);
556 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
559 struct child_process cp
= CHILD_PROCESS_INIT
;
561 strvec_push(&cp
.args
, "branch");
562 if (new_branch_force
)
563 strvec_push(&cp
.args
, "--force");
565 strvec_push(&cp
.args
, "--quiet");
566 strvec_push(&cp
.args
, new_branch
);
567 strvec_push(&cp
.args
, branch
);
569 strvec_push(&cp
.args
, opt_track
);
570 if (run_command(&cp
))
573 } else if (opt_track
) {
574 die(_("--[no-]track can only be used if a new branch is created"));
579 return add_worktree(path
, branch
, &opts
);
582 static void show_worktree_porcelain(struct worktree
*wt
)
586 printf("worktree %s\n", wt
->path
);
590 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
592 printf("detached\n");
593 else if (wt
->head_ref
)
594 printf("branch %s\n", wt
->head_ref
);
597 reason
= worktree_lock_reason(wt
);
598 if (reason
&& *reason
) {
599 struct strbuf sb
= STRBUF_INIT
;
600 quote_c_style(reason
, &sb
, NULL
, 0);
601 printf("locked %s\n", sb
.buf
);
606 reason
= worktree_prune_reason(wt
, expire
);
608 printf("prunable %s\n", reason
);
613 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
615 struct strbuf sb
= STRBUF_INIT
;
616 int cur_path_len
= strlen(wt
->path
);
617 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
620 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
622 strbuf_addstr(&sb
, "(bare)");
624 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
625 find_unique_abbrev(&wt
->head_oid
, DEFAULT_ABBREV
));
627 strbuf_addstr(&sb
, "(detached HEAD)");
628 else if (wt
->head_ref
) {
629 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
630 strbuf_addf(&sb
, "[%s]", ref
);
633 strbuf_addstr(&sb
, "(error)");
636 reason
= worktree_lock_reason(wt
);
637 if (verbose
&& reason
&& *reason
)
638 strbuf_addf(&sb
, "\n\tlocked: %s", reason
);
640 strbuf_addstr(&sb
, " locked");
642 reason
= worktree_prune_reason(wt
, expire
);
643 if (verbose
&& reason
)
644 strbuf_addf(&sb
, "\n\tprunable: %s", reason
);
646 strbuf_addstr(&sb
, " prunable");
648 printf("%s\n", sb
.buf
);
652 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
656 for (i
= 0; wt
[i
]; i
++) {
658 int path_len
= strlen(wt
[i
]->path
);
660 if (path_len
> *maxlen
)
662 sha1_len
= strlen(find_unique_abbrev(&wt
[i
]->head_oid
, *abbrev
));
663 if (sha1_len
> *abbrev
)
668 static int pathcmp(const void *a_
, const void *b_
)
670 const struct worktree
*const *a
= a_
;
671 const struct worktree
*const *b
= b_
;
672 return fspathcmp((*a
)->path
, (*b
)->path
);
675 static void pathsort(struct worktree
**wt
)
678 struct worktree
**p
= wt
;
682 QSORT(wt
, n
, pathcmp
);
685 static int list(int ac
, const char **av
, const char *prefix
)
689 struct option options
[] = {
690 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
691 OPT__VERBOSE(&verbose
, N_("show extended annotations and reasons, if available")),
692 OPT_EXPIRY_DATE(0, "expire", &expire
,
693 N_("add 'prunable' annotation to worktrees older than <time>")),
698 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
700 usage_with_options(worktree_usage
, options
);
701 else if (verbose
&& porcelain
)
702 die(_("--verbose and --porcelain are mutually exclusive"));
704 struct worktree
**worktrees
= get_worktrees();
705 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
707 /* sort worktrees by path but keep main worktree at top */
708 pathsort(worktrees
+ 1);
711 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
713 for (i
= 0; worktrees
[i
]; i
++) {
715 show_worktree_porcelain(worktrees
[i
]);
717 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
719 free_worktrees(worktrees
);
724 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
726 const char *reason
= "", *old_reason
;
727 struct option options
[] = {
728 OPT_STRING(0, "reason", &reason
, N_("string"),
729 N_("reason for locking")),
732 struct worktree
**worktrees
, *wt
;
734 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
736 usage_with_options(worktree_usage
, options
);
738 worktrees
= get_worktrees();
739 wt
= find_worktree(worktrees
, prefix
, av
[0]);
741 die(_("'%s' is not a working tree"), av
[0]);
742 if (is_main_worktree(wt
))
743 die(_("The main working tree cannot be locked or unlocked"));
745 old_reason
= worktree_lock_reason(wt
);
748 die(_("'%s' is already locked, reason: %s"),
750 die(_("'%s' is already locked"), av
[0]);
753 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
755 free_worktrees(worktrees
);
759 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
761 struct option options
[] = {
764 struct worktree
**worktrees
, *wt
;
767 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
769 usage_with_options(worktree_usage
, options
);
771 worktrees
= get_worktrees();
772 wt
= find_worktree(worktrees
, prefix
, av
[0]);
774 die(_("'%s' is not a working tree"), av
[0]);
775 if (is_main_worktree(wt
))
776 die(_("The main working tree cannot be locked or unlocked"));
777 if (!worktree_lock_reason(wt
))
778 die(_("'%s' is not locked"), av
[0]);
779 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
780 free_worktrees(worktrees
);
784 static void validate_no_submodules(const struct worktree
*wt
)
786 struct index_state istate
= { NULL
};
787 struct strbuf path
= STRBUF_INIT
;
788 int i
, found_submodules
= 0;
790 if (is_directory(worktree_git_path(wt
, "modules"))) {
792 * There could be false positives, e.g. the "modules"
793 * directory exists but is empty. But it's a rare case and
794 * this simpler check is probably good enough for now.
796 found_submodules
= 1;
797 } else if (read_index_from(&istate
, worktree_git_path(wt
, "index"),
798 get_worktree_git_dir(wt
)) > 0) {
799 for (i
= 0; i
< istate
.cache_nr
; i
++) {
800 struct cache_entry
*ce
= istate
.cache
[i
];
803 if (!S_ISGITLINK(ce
->ce_mode
))
807 strbuf_addf(&path
, "%s/%s", wt
->path
, ce
->name
);
808 if (!is_submodule_populated_gently(path
.buf
, &err
))
811 found_submodules
= 1;
815 discard_index(&istate
);
816 strbuf_release(&path
);
818 if (found_submodules
)
819 die(_("working trees containing submodules cannot be moved or removed"));
822 static int move_worktree(int ac
, const char **av
, const char *prefix
)
825 struct option options
[] = {
827 N_("force move even if worktree is dirty or locked"),
828 PARSE_OPT_NOCOMPLETE
),
831 struct worktree
**worktrees
, *wt
;
832 struct strbuf dst
= STRBUF_INIT
;
833 struct strbuf errmsg
= STRBUF_INIT
;
834 const char *reason
= NULL
;
837 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
839 usage_with_options(worktree_usage
, options
);
841 path
= prefix_filename(prefix
, av
[1]);
842 strbuf_addstr(&dst
, path
);
845 worktrees
= get_worktrees();
846 wt
= find_worktree(worktrees
, prefix
, av
[0]);
848 die(_("'%s' is not a working tree"), av
[0]);
849 if (is_main_worktree(wt
))
850 die(_("'%s' is a main working tree"), av
[0]);
851 if (is_directory(dst
.buf
)) {
852 const char *sep
= find_last_dir_sep(wt
->path
);
855 die(_("could not figure out destination name from '%s'"),
857 strbuf_trim_trailing_dir_sep(&dst
);
858 strbuf_addstr(&dst
, sep
);
860 check_candidate_path(dst
.buf
, force
, worktrees
, "move");
862 validate_no_submodules(wt
);
865 reason
= worktree_lock_reason(wt
);
868 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
870 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
872 if (validate_worktree(wt
, &errmsg
, 0))
873 die(_("validation failed, cannot move working tree: %s"),
875 strbuf_release(&errmsg
);
877 if (rename(wt
->path
, dst
.buf
) == -1)
878 die_errno(_("failed to move '%s' to '%s'"), wt
->path
, dst
.buf
);
880 update_worktree_location(wt
, dst
.buf
);
882 strbuf_release(&dst
);
883 free_worktrees(worktrees
);
888 * Note, "git status --porcelain" is used to determine if it's safe to
889 * delete a whole worktree. "git status" does not ignore user
890 * configuration, so if a normal "git status" shows "clean" for the
891 * user, then it's ok to remove it.
893 * This assumption may be a bad one. We may want to ignore
894 * (potentially bad) user settings and only delete a worktree when
895 * it's absolutely safe to do so from _our_ point of view because we
898 static void check_clean_worktree(struct worktree
*wt
,
899 const char *original_path
)
901 struct child_process cp
;
906 * Until we sort this out, all submodules are "dirty" and
907 * will abort this function.
909 validate_no_submodules(wt
);
911 child_process_init(&cp
);
912 strvec_pushf(&cp
.env_array
, "%s=%s/.git",
913 GIT_DIR_ENVIRONMENT
, wt
->path
);
914 strvec_pushf(&cp
.env_array
, "%s=%s",
915 GIT_WORK_TREE_ENVIRONMENT
, wt
->path
);
916 strvec_pushl(&cp
.args
, "status",
917 "--porcelain", "--ignore-submodules=none",
922 ret
= start_command(&cp
);
924 die_errno(_("failed to run 'git status' on '%s'"),
926 ret
= xread(cp
.out
, buf
, sizeof(buf
));
928 die(_("'%s' contains modified or untracked files, use --force to delete it"),
931 ret
= finish_command(&cp
);
933 die_errno(_("failed to run 'git status' on '%s', code %d"),
937 static int delete_git_work_tree(struct worktree
*wt
)
939 struct strbuf sb
= STRBUF_INIT
;
942 strbuf_addstr(&sb
, wt
->path
);
943 if (remove_dir_recursively(&sb
, 0)) {
944 error_errno(_("failed to delete '%s'"), sb
.buf
);
951 static int remove_worktree(int ac
, const char **av
, const char *prefix
)
954 struct option options
[] = {
956 N_("force removal even if worktree is dirty or locked"),
957 PARSE_OPT_NOCOMPLETE
),
960 struct worktree
**worktrees
, *wt
;
961 struct strbuf errmsg
= STRBUF_INIT
;
962 const char *reason
= NULL
;
965 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
967 usage_with_options(worktree_usage
, options
);
969 worktrees
= get_worktrees();
970 wt
= find_worktree(worktrees
, prefix
, av
[0]);
972 die(_("'%s' is not a working tree"), av
[0]);
973 if (is_main_worktree(wt
))
974 die(_("'%s' is a main working tree"), av
[0]);
976 reason
= worktree_lock_reason(wt
);
979 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
981 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
983 if (validate_worktree(wt
, &errmsg
, WT_VALIDATE_WORKTREE_MISSING_OK
))
984 die(_("validation failed, cannot remove working tree: %s"),
986 strbuf_release(&errmsg
);
988 if (file_exists(wt
->path
)) {
990 check_clean_worktree(wt
, av
[0]);
992 ret
|= delete_git_work_tree(wt
);
995 * continue on even if ret is non-zero, there's no going back
998 ret
|= delete_git_dir(wt
->id
);
999 delete_worktrees_dir_if_empty();
1001 free_worktrees(worktrees
);
1005 static void report_repair(int iserr
, const char *path
, const char *msg
, void *cb_data
)
1008 printf_ln(_("repair: %s: %s"), msg
, path
);
1010 int *exit_status
= (int *)cb_data
;
1011 fprintf_ln(stderr
, _("error: %s: %s"), msg
, path
);
1016 static int repair(int ac
, const char **av
, const char *prefix
)
1019 const char *self
[] = { ".", NULL
};
1020 struct option options
[] = {
1025 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
1026 p
= ac
> 0 ? av
: self
;
1028 repair_worktree_at_path(*p
, report_repair
, &rc
);
1029 repair_worktrees(report_repair
, &rc
);
1033 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
1035 struct option options
[] = {
1039 git_config(git_worktree_config
, NULL
);
1042 usage_with_options(worktree_usage
, options
);
1045 if (!strcmp(av
[1], "add"))
1046 return add(ac
- 1, av
+ 1, prefix
);
1047 if (!strcmp(av
[1], "prune"))
1048 return prune(ac
- 1, av
+ 1, prefix
);
1049 if (!strcmp(av
[1], "list"))
1050 return list(ac
- 1, av
+ 1, prefix
);
1051 if (!strcmp(av
[1], "lock"))
1052 return lock_worktree(ac
- 1, av
+ 1, prefix
);
1053 if (!strcmp(av
[1], "unlock"))
1054 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
1055 if (!strcmp(av
[1], "move"))
1056 return move_worktree(ac
- 1, av
+ 1, prefix
);
1057 if (!strcmp(av
[1], "remove"))
1058 return remove_worktree(ac
- 1, av
+ 1, prefix
);
1059 if (!strcmp(av
[1], "repair"))
1060 return repair(ac
- 1, av
+ 1, prefix
);
1061 usage_with_options(worktree_usage
, options
);