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 fprintf_ln(stderr
, _("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 void copy_filtered_worktree_config(const char *worktree_git_dir
)
241 char *from_file
= git_pathdup("config.worktree");
242 char *to_file
= xstrfmt("%s/config.worktree", worktree_git_dir
);
244 if (file_exists(from_file
)) {
245 struct config_set cs
= { { 0 } };
246 const char *core_worktree
;
249 if (safe_create_leading_directories(to_file
) ||
250 copy_file(to_file
, from_file
, 0666)) {
251 error(_("failed to copy worktree config from '%s' to '%s'"),
253 goto worktree_copy_cleanup
;
256 git_configset_init(&cs
);
257 git_configset_add_file(&cs
, from_file
);
259 if (!git_configset_get_bool(&cs
, "core.bare", &bare
) &&
261 git_config_set_multivar_in_file_gently(
262 to_file
, "core.bare", NULL
, "true", 0))
263 error(_("failed to unset '%s' in '%s'"),
264 "core.bare", to_file
);
265 if (!git_configset_get_value(&cs
, "core.worktree", &core_worktree
) &&
266 git_config_set_in_file_gently(to_file
,
267 "core.worktree", NULL
))
268 error(_("failed to unset '%s' in '%s'"),
269 "core.worktree", to_file
);
271 git_configset_clear(&cs
);
274 worktree_copy_cleanup
:
279 static int add_worktree(const char *path
, const char *refname
,
280 const struct add_opts
*opts
)
282 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
283 struct strbuf sb
= STRBUF_INIT
, realpath
= STRBUF_INIT
;
285 struct child_process cp
= CHILD_PROCESS_INIT
;
286 struct strvec child_env
= STRVEC_INIT
;
287 unsigned int counter
= 0;
289 struct strbuf symref
= STRBUF_INIT
;
290 struct commit
*commit
= NULL
;
292 struct strbuf sb_name
= STRBUF_INIT
;
293 struct worktree
**worktrees
;
295 worktrees
= get_worktrees();
296 check_candidate_path(path
, opts
->force
, worktrees
, "add");
297 free_worktrees(worktrees
);
300 /* is 'refname' a branch or commit? */
301 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
302 ref_exists(symref
.buf
)) {
305 die_if_checked_out(symref
.buf
, 0);
307 commit
= lookup_commit_reference_by_name(refname
);
309 die(_("invalid reference: %s"), refname
);
311 name
= worktree_basename(path
, &len
);
312 strbuf_add(&sb
, name
, path
+ len
- name
);
313 sanitize_refname_component(sb
.buf
, &sb_name
);
315 BUG("How come '%s' becomes empty after sanitization?", sb
.buf
);
318 git_path_buf(&sb_repo
, "worktrees/%s", name
);
320 if (safe_create_leading_directories_const(sb_repo
.buf
))
321 die_errno(_("could not create leading directories of '%s'"),
324 while (mkdir(sb_repo
.buf
, 0777)) {
326 if ((errno
!= EEXIST
) || !counter
/* overflow */)
327 die_errno(_("could not create directory of '%s'"),
329 strbuf_setlen(&sb_repo
, len
);
330 strbuf_addf(&sb_repo
, "%d", counter
);
332 name
= strrchr(sb_repo
.buf
, '/') + 1;
336 sigchain_push_common(remove_junk_on_signal
);
338 junk_git_dir
= xstrdup(sb_repo
.buf
);
342 * lock the incomplete repo so prune won't delete it, unlock
343 * after the preparation is over.
345 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
346 if (opts
->keep_locked
)
347 write_file(sb
.buf
, "%s", opts
->keep_locked
);
349 write_file(sb
.buf
, _("initializing"));
351 strbuf_addf(&sb_git
, "%s/.git", path
);
352 if (safe_create_leading_directories_const(sb_git
.buf
))
353 die_errno(_("could not create leading directories of '%s'"),
355 junk_work_tree
= xstrdup(path
);
358 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
359 strbuf_realpath(&realpath
, sb_git
.buf
, 1);
360 write_file(sb
.buf
, "%s", realpath
.buf
);
361 strbuf_realpath(&realpath
, get_git_common_dir(), 1);
362 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
365 * This is to keep resolve_ref() happy. We need a valid HEAD
366 * or is_git_directory() will reject the directory. Any value which
367 * looks like an object ID will do since it will be immediately
368 * replaced by the symbolic-ref or update-ref invocation in the new
372 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
373 write_file(sb
.buf
, "%s", oid_to_hex(null_oid()));
375 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
376 write_file(sb
.buf
, "../..");
379 * If the current worktree has sparse-checkout enabled, then copy
380 * the sparse-checkout patterns from the current worktree.
382 if (core_apply_sparse_checkout
) {
383 char *from_file
= git_pathdup("info/sparse-checkout");
384 char *to_file
= xstrfmt("%s/info/sparse-checkout",
387 if (file_exists(from_file
)) {
388 if (safe_create_leading_directories(to_file
) ||
389 copy_file(to_file
, from_file
, 0666))
390 error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
399 * If we are using worktree config, then copy all current config
400 * values from the current worktree into the new one, that way the
401 * new worktree behaves the same as this one.
403 if (repository_format_worktree_config
)
404 copy_filtered_worktree_config(sb_repo
.buf
);
406 strvec_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
407 strvec_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
411 strvec_pushl(&cp
.args
, "update-ref", "HEAD",
412 oid_to_hex(&commit
->object
.oid
), NULL
);
414 strvec_pushl(&cp
.args
, "symbolic-ref", "HEAD",
417 strvec_push(&cp
.args
, "--quiet");
420 strvec_pushv(&cp
.env_array
, child_env
.v
);
421 ret
= run_command(&cp
);
425 if (opts
->checkout
) {
426 struct child_process cp
= CHILD_PROCESS_INIT
;
428 strvec_pushl(&cp
.args
, "reset", "--hard", "--no-recurse-submodules", NULL
);
430 strvec_push(&cp
.args
, "--quiet");
431 strvec_pushv(&cp
.env_array
, child_env
.v
);
432 ret
= run_command(&cp
);
438 FREE_AND_NULL(junk_work_tree
);
439 FREE_AND_NULL(junk_git_dir
);
442 if (ret
|| !opts
->keep_locked
) {
444 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
445 unlink_or_warn(sb
.buf
);
449 * Hook failure does not warrant worktree deletion, so run hook after
450 * is_junk is cleared, but do return appropriate code when hook fails.
452 if (!ret
&& opts
->checkout
) {
453 const char *hook
= find_hook("post-checkout");
455 const char *env
[] = { "GIT_DIR", "GIT_WORK_TREE", NULL
};
456 struct child_process cp
= CHILD_PROCESS_INIT
;
458 cp
.stdout_to_stderr
= 1;
460 strvec_pushv(&cp
.env_array
, env
);
461 cp
.trace2_hook_name
= "post-checkout";
462 strvec_pushl(&cp
.args
, absolute_path(hook
),
463 oid_to_hex(null_oid()),
464 oid_to_hex(&commit
->object
.oid
),
466 ret
= run_command(&cp
);
470 strvec_clear(&child_env
);
472 strbuf_release(&symref
);
473 strbuf_release(&sb_repo
);
474 strbuf_release(&sb_git
);
475 strbuf_release(&sb_name
);
476 strbuf_release(&realpath
);
480 static void print_preparing_worktree_line(int detach
,
482 const char *new_branch
,
483 int force_new_branch
)
485 if (force_new_branch
) {
486 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
488 fprintf_ln(stderr
, _("Preparing worktree (new branch '%s')"), new_branch
);
490 fprintf_ln(stderr
, _("Preparing worktree (resetting branch '%s'; was at %s)"),
492 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
493 } else if (new_branch
) {
494 fprintf_ln(stderr
, _("Preparing worktree (new branch '%s')"), new_branch
);
496 struct strbuf s
= STRBUF_INIT
;
497 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
499 fprintf_ln(stderr
, _("Preparing worktree (checking out '%s')"),
502 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
504 die(_("invalid reference: %s"), branch
);
505 fprintf_ln(stderr
, _("Preparing worktree (detached HEAD %s)"),
506 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
));
512 static const char *dwim_branch(const char *path
, const char **new_branch
)
516 const char *s
= worktree_basename(path
, &n
);
517 const char *branchname
= xstrndup(s
, n
);
518 struct strbuf ref
= STRBUF_INIT
;
522 branch_exists
= !strbuf_check_branch_ref(&ref
, branchname
) &&
524 strbuf_release(&ref
);
528 *new_branch
= branchname
;
530 struct object_id oid
;
532 unique_tracking_name(*new_branch
, &oid
, NULL
);
538 static int add(int ac
, const char **av
, const char *prefix
)
540 struct add_opts opts
;
541 const char *new_branch_force
= NULL
;
544 const char *new_branch
= NULL
;
545 const char *opt_track
= NULL
;
546 const char *lock_reason
= NULL
;
548 struct option options
[] = {
549 OPT__FORCE(&opts
.force
,
550 N_("checkout <branch> even if already checked out in other worktree"),
551 PARSE_OPT_NOCOMPLETE
),
552 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
553 N_("create a new branch")),
554 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
555 N_("create or reset a branch")),
556 OPT_BOOL('d', "detach", &opts
.detach
, N_("detach HEAD at named commit")),
557 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
558 OPT_BOOL(0, "lock", &keep_locked
, N_("keep the new working tree locked")),
559 OPT_STRING(0, "reason", &lock_reason
, N_("string"),
560 N_("reason for locking")),
561 OPT__QUIET(&opts
.quiet
, N_("suppress progress reporting")),
562 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
563 N_("set up tracking mode (see git-branch(1))"),
564 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
565 OPT_BOOL(0, "guess-remote", &guess_remote
,
566 N_("try to match the new branch name with a remote-tracking branch")),
570 memset(&opts
, 0, sizeof(opts
));
572 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
573 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
574 die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
575 if (lock_reason
&& !keep_locked
)
576 die(_("the option '%s' requires '%s'"), "--reason", "--lock");
578 opts
.keep_locked
= lock_reason
;
579 else if (keep_locked
)
580 opts
.keep_locked
= _("added with --lock");
582 if (ac
< 1 || ac
> 2)
583 usage_with_options(worktree_usage
, options
);
585 path
= prefix_filename(prefix
, av
[0]);
586 branch
= ac
< 2 ? "HEAD" : av
[1];
588 if (!strcmp(branch
, "-"))
591 if (new_branch_force
) {
592 struct strbuf symref
= STRBUF_INIT
;
594 new_branch
= new_branch_force
;
597 !strbuf_check_branch_ref(&symref
, new_branch
) &&
598 ref_exists(symref
.buf
))
599 die_if_checked_out(symref
.buf
, 0);
600 strbuf_release(&symref
);
603 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
604 const char *s
= dwim_branch(path
, &new_branch
);
609 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
610 struct object_id oid
;
611 struct commit
*commit
;
614 commit
= lookup_commit_reference_by_name(branch
);
616 remote
= unique_tracking_name(branch
, &oid
, NULL
);
624 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
627 struct child_process cp
= CHILD_PROCESS_INIT
;
629 strvec_push(&cp
.args
, "branch");
630 if (new_branch_force
)
631 strvec_push(&cp
.args
, "--force");
633 strvec_push(&cp
.args
, "--quiet");
634 strvec_push(&cp
.args
, new_branch
);
635 strvec_push(&cp
.args
, branch
);
637 strvec_push(&cp
.args
, opt_track
);
638 if (run_command(&cp
))
641 } else if (opt_track
) {
642 die(_("--[no-]track can only be used if a new branch is created"));
647 return add_worktree(path
, branch
, &opts
);
650 static void show_worktree_porcelain(struct worktree
*wt
)
654 printf("worktree %s\n", wt
->path
);
658 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
660 printf("detached\n");
661 else if (wt
->head_ref
)
662 printf("branch %s\n", wt
->head_ref
);
665 reason
= worktree_lock_reason(wt
);
666 if (reason
&& *reason
) {
667 struct strbuf sb
= STRBUF_INIT
;
668 quote_c_style(reason
, &sb
, NULL
, 0);
669 printf("locked %s\n", sb
.buf
);
674 reason
= worktree_prune_reason(wt
, expire
);
676 printf("prunable %s\n", reason
);
681 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
683 struct strbuf sb
= STRBUF_INIT
;
684 int cur_path_len
= strlen(wt
->path
);
685 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
688 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
690 strbuf_addstr(&sb
, "(bare)");
692 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
693 find_unique_abbrev(&wt
->head_oid
, DEFAULT_ABBREV
));
695 strbuf_addstr(&sb
, "(detached HEAD)");
696 else if (wt
->head_ref
) {
697 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
698 strbuf_addf(&sb
, "[%s]", ref
);
701 strbuf_addstr(&sb
, "(error)");
704 reason
= worktree_lock_reason(wt
);
705 if (verbose
&& reason
&& *reason
)
706 strbuf_addf(&sb
, "\n\tlocked: %s", reason
);
708 strbuf_addstr(&sb
, " locked");
710 reason
= worktree_prune_reason(wt
, expire
);
711 if (verbose
&& reason
)
712 strbuf_addf(&sb
, "\n\tprunable: %s", reason
);
714 strbuf_addstr(&sb
, " prunable");
716 printf("%s\n", sb
.buf
);
720 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
724 for (i
= 0; wt
[i
]; i
++) {
726 int path_len
= strlen(wt
[i
]->path
);
728 if (path_len
> *maxlen
)
730 sha1_len
= strlen(find_unique_abbrev(&wt
[i
]->head_oid
, *abbrev
));
731 if (sha1_len
> *abbrev
)
736 static int pathcmp(const void *a_
, const void *b_
)
738 const struct worktree
*const *a
= a_
;
739 const struct worktree
*const *b
= b_
;
740 return fspathcmp((*a
)->path
, (*b
)->path
);
743 static void pathsort(struct worktree
**wt
)
746 struct worktree
**p
= wt
;
750 QSORT(wt
, n
, pathcmp
);
753 static int list(int ac
, const char **av
, const char *prefix
)
757 struct option options
[] = {
758 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
759 OPT__VERBOSE(&verbose
, N_("show extended annotations and reasons, if available")),
760 OPT_EXPIRY_DATE(0, "expire", &expire
,
761 N_("add 'prunable' annotation to worktrees older than <time>")),
766 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
768 usage_with_options(worktree_usage
, options
);
769 else if (verbose
&& porcelain
)
770 die(_("options '%s' and '%s' cannot be used together"), "--verbose", "--porcelain");
772 struct worktree
**worktrees
= get_worktrees();
773 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
775 /* sort worktrees by path but keep main worktree at top */
776 pathsort(worktrees
+ 1);
779 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
781 for (i
= 0; worktrees
[i
]; i
++) {
783 show_worktree_porcelain(worktrees
[i
]);
785 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
787 free_worktrees(worktrees
);
792 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
794 const char *reason
= "", *old_reason
;
795 struct option options
[] = {
796 OPT_STRING(0, "reason", &reason
, N_("string"),
797 N_("reason for locking")),
800 struct worktree
**worktrees
, *wt
;
802 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
804 usage_with_options(worktree_usage
, options
);
806 worktrees
= get_worktrees();
807 wt
= find_worktree(worktrees
, prefix
, av
[0]);
809 die(_("'%s' is not a working tree"), av
[0]);
810 if (is_main_worktree(wt
))
811 die(_("The main working tree cannot be locked or unlocked"));
813 old_reason
= worktree_lock_reason(wt
);
816 die(_("'%s' is already locked, reason: %s"),
818 die(_("'%s' is already locked"), av
[0]);
821 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
823 free_worktrees(worktrees
);
827 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
829 struct option options
[] = {
832 struct worktree
**worktrees
, *wt
;
835 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
837 usage_with_options(worktree_usage
, options
);
839 worktrees
= get_worktrees();
840 wt
= find_worktree(worktrees
, prefix
, av
[0]);
842 die(_("'%s' is not a working tree"), av
[0]);
843 if (is_main_worktree(wt
))
844 die(_("The main working tree cannot be locked or unlocked"));
845 if (!worktree_lock_reason(wt
))
846 die(_("'%s' is not locked"), av
[0]);
847 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
848 free_worktrees(worktrees
);
852 static void validate_no_submodules(const struct worktree
*wt
)
854 struct index_state istate
= { NULL
};
855 struct strbuf path
= STRBUF_INIT
;
856 int i
, found_submodules
= 0;
858 if (is_directory(worktree_git_path(wt
, "modules"))) {
860 * There could be false positives, e.g. the "modules"
861 * directory exists but is empty. But it's a rare case and
862 * this simpler check is probably good enough for now.
864 found_submodules
= 1;
865 } else if (read_index_from(&istate
, worktree_git_path(wt
, "index"),
866 get_worktree_git_dir(wt
)) > 0) {
867 for (i
= 0; i
< istate
.cache_nr
; i
++) {
868 struct cache_entry
*ce
= istate
.cache
[i
];
871 if (!S_ISGITLINK(ce
->ce_mode
))
875 strbuf_addf(&path
, "%s/%s", wt
->path
, ce
->name
);
876 if (!is_submodule_populated_gently(path
.buf
, &err
))
879 found_submodules
= 1;
883 discard_index(&istate
);
884 strbuf_release(&path
);
886 if (found_submodules
)
887 die(_("working trees containing submodules cannot be moved or removed"));
890 static int move_worktree(int ac
, const char **av
, const char *prefix
)
893 struct option options
[] = {
895 N_("force move even if worktree is dirty or locked"),
896 PARSE_OPT_NOCOMPLETE
),
899 struct worktree
**worktrees
, *wt
;
900 struct strbuf dst
= STRBUF_INIT
;
901 struct strbuf errmsg
= STRBUF_INIT
;
902 const char *reason
= NULL
;
905 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
907 usage_with_options(worktree_usage
, options
);
909 path
= prefix_filename(prefix
, av
[1]);
910 strbuf_addstr(&dst
, path
);
913 worktrees
= get_worktrees();
914 wt
= find_worktree(worktrees
, prefix
, av
[0]);
916 die(_("'%s' is not a working tree"), av
[0]);
917 if (is_main_worktree(wt
))
918 die(_("'%s' is a main working tree"), av
[0]);
919 if (is_directory(dst
.buf
)) {
920 const char *sep
= find_last_dir_sep(wt
->path
);
923 die(_("could not figure out destination name from '%s'"),
925 strbuf_trim_trailing_dir_sep(&dst
);
926 strbuf_addstr(&dst
, sep
);
928 check_candidate_path(dst
.buf
, force
, worktrees
, "move");
930 validate_no_submodules(wt
);
933 reason
= worktree_lock_reason(wt
);
936 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
938 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
940 if (validate_worktree(wt
, &errmsg
, 0))
941 die(_("validation failed, cannot move working tree: %s"),
943 strbuf_release(&errmsg
);
945 if (rename(wt
->path
, dst
.buf
) == -1)
946 die_errno(_("failed to move '%s' to '%s'"), wt
->path
, dst
.buf
);
948 update_worktree_location(wt
, dst
.buf
);
950 strbuf_release(&dst
);
951 free_worktrees(worktrees
);
956 * Note, "git status --porcelain" is used to determine if it's safe to
957 * delete a whole worktree. "git status" does not ignore user
958 * configuration, so if a normal "git status" shows "clean" for the
959 * user, then it's ok to remove it.
961 * This assumption may be a bad one. We may want to ignore
962 * (potentially bad) user settings and only delete a worktree when
963 * it's absolutely safe to do so from _our_ point of view because we
966 static void check_clean_worktree(struct worktree
*wt
,
967 const char *original_path
)
969 struct child_process cp
;
974 * Until we sort this out, all submodules are "dirty" and
975 * will abort this function.
977 validate_no_submodules(wt
);
979 child_process_init(&cp
);
980 strvec_pushf(&cp
.env_array
, "%s=%s/.git",
981 GIT_DIR_ENVIRONMENT
, wt
->path
);
982 strvec_pushf(&cp
.env_array
, "%s=%s",
983 GIT_WORK_TREE_ENVIRONMENT
, wt
->path
);
984 strvec_pushl(&cp
.args
, "status",
985 "--porcelain", "--ignore-submodules=none",
990 ret
= start_command(&cp
);
992 die_errno(_("failed to run 'git status' on '%s'"),
994 ret
= xread(cp
.out
, buf
, sizeof(buf
));
996 die(_("'%s' contains modified or untracked files, use --force to delete it"),
999 ret
= finish_command(&cp
);
1001 die_errno(_("failed to run 'git status' on '%s', code %d"),
1002 original_path
, ret
);
1005 static int delete_git_work_tree(struct worktree
*wt
)
1007 struct strbuf sb
= STRBUF_INIT
;
1010 strbuf_addstr(&sb
, wt
->path
);
1011 if (remove_dir_recursively(&sb
, 0)) {
1012 error_errno(_("failed to delete '%s'"), sb
.buf
);
1015 strbuf_release(&sb
);
1019 static int remove_worktree(int ac
, const char **av
, const char *prefix
)
1022 struct option options
[] = {
1024 N_("force removal even if worktree is dirty or locked"),
1025 PARSE_OPT_NOCOMPLETE
),
1028 struct worktree
**worktrees
, *wt
;
1029 struct strbuf errmsg
= STRBUF_INIT
;
1030 const char *reason
= NULL
;
1033 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
1035 usage_with_options(worktree_usage
, options
);
1037 worktrees
= get_worktrees();
1038 wt
= find_worktree(worktrees
, prefix
, av
[0]);
1040 die(_("'%s' is not a working tree"), av
[0]);
1041 if (is_main_worktree(wt
))
1042 die(_("'%s' is a main working tree"), av
[0]);
1044 reason
= worktree_lock_reason(wt
);
1047 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
1049 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
1051 if (validate_worktree(wt
, &errmsg
, WT_VALIDATE_WORKTREE_MISSING_OK
))
1052 die(_("validation failed, cannot remove working tree: %s"),
1054 strbuf_release(&errmsg
);
1056 if (file_exists(wt
->path
)) {
1058 check_clean_worktree(wt
, av
[0]);
1060 ret
|= delete_git_work_tree(wt
);
1063 * continue on even if ret is non-zero, there's no going back
1066 ret
|= delete_git_dir(wt
->id
);
1067 delete_worktrees_dir_if_empty();
1069 free_worktrees(worktrees
);
1073 static void report_repair(int iserr
, const char *path
, const char *msg
, void *cb_data
)
1076 fprintf_ln(stderr
, _("repair: %s: %s"), msg
, path
);
1078 int *exit_status
= (int *)cb_data
;
1079 fprintf_ln(stderr
, _("error: %s: %s"), msg
, path
);
1084 static int repair(int ac
, const char **av
, const char *prefix
)
1087 const char *self
[] = { ".", NULL
};
1088 struct option options
[] = {
1093 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
1094 p
= ac
> 0 ? av
: self
;
1096 repair_worktree_at_path(*p
, report_repair
, &rc
);
1097 repair_worktrees(report_repair
, &rc
);
1101 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
1103 struct option options
[] = {
1107 git_config(git_worktree_config
, NULL
);
1110 usage_with_options(worktree_usage
, options
);
1113 if (!strcmp(av
[1], "add"))
1114 return add(ac
- 1, av
+ 1, prefix
);
1115 if (!strcmp(av
[1], "prune"))
1116 return prune(ac
- 1, av
+ 1, prefix
);
1117 if (!strcmp(av
[1], "list"))
1118 return list(ac
- 1, av
+ 1, prefix
);
1119 if (!strcmp(av
[1], "lock"))
1120 return lock_worktree(ac
- 1, av
+ 1, prefix
);
1121 if (!strcmp(av
[1], "unlock"))
1122 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
1123 if (!strcmp(av
[1], "move"))
1124 return move_worktree(ac
- 1, av
+ 1, prefix
);
1125 if (!strcmp(av
[1], "remove"))
1126 return remove_worktree(ac
- 1, av
+ 1, prefix
);
1127 if (!strcmp(av
[1], "repair"))
1128 return repair(ac
- 1, av
+ 1, prefix
);
1129 usage_with_options(worktree_usage
, options
);