6 #include "parse-options.h"
7 #include "argv-array.h"
10 #include "run-command.h"
16 static const char * const worktree_usage
[] = {
17 N_("git worktree add [<options>] <path> [<branch>]"),
18 N_("git worktree list [<options>]"),
19 N_("git worktree lock [<options>] <path>"),
20 N_("git worktree prune [<options>]"),
21 N_("git worktree unlock <path>"),
34 static int guess_remote
;
35 static timestamp_t expire
;
37 static int git_worktree_config(const char *var
, const char *value
, void *cb
)
39 if (!strcmp(var
, "worktree.guessremote")) {
40 guess_remote
= git_config_bool(var
, value
);
44 return git_default_config(var
, value
, cb
);
47 static int prune_worktree(const char *id
, struct strbuf
*reason
)
55 if (!is_directory(git_path("worktrees/%s", id
))) {
56 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
59 if (file_exists(git_path("worktrees/%s/locked", id
)))
61 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
62 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
65 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
67 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
71 len
= xsize_t(st
.st_size
);
74 read_result
= read_in_full(fd
, path
, len
);
75 if (read_result
< 0) {
76 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
84 if (read_result
!= len
) {
86 _("Removing worktrees/%s: short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
87 id
, (uintmax_t)len
, (uintmax_t)read_result
);
91 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
94 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
99 if (!file_exists(path
)) {
103 * the repo is moved manually and has not been
106 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
107 st_link
.st_nlink
> 1)
109 if (st
.st_mtime
<= expire
) {
110 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
120 static void prune_worktrees(void)
122 struct strbuf reason
= STRBUF_INIT
;
123 struct strbuf path
= STRBUF_INIT
;
124 DIR *dir
= opendir(git_path("worktrees"));
129 while ((d
= readdir(dir
)) != NULL
) {
130 if (is_dot_or_dotdot(d
->d_name
))
132 strbuf_reset(&reason
);
133 if (!prune_worktree(d
->d_name
, &reason
))
135 if (show_only
|| verbose
)
136 printf("%s\n", reason
.buf
);
139 git_path_buf(&path
, "worktrees/%s", d
->d_name
);
140 ret
= remove_dir_recursively(&path
, 0);
141 if (ret
< 0 && errno
== ENOTDIR
)
142 ret
= unlink(path
.buf
);
144 error_errno(_("failed to remove '%s'"), path
.buf
);
148 rmdir(git_path("worktrees"));
149 strbuf_release(&reason
);
150 strbuf_release(&path
);
153 static int prune(int ac
, const char **av
, const char *prefix
)
155 struct option options
[] = {
156 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
157 OPT__VERBOSE(&verbose
, N_("report pruned working trees")),
158 OPT_EXPIRY_DATE(0, "expire", &expire
,
159 N_("expire working trees older than <time>")),
164 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
166 usage_with_options(worktree_usage
, options
);
171 static char *junk_work_tree
;
172 static char *junk_git_dir
;
174 static pid_t junk_pid
;
176 static void remove_junk(void)
178 struct strbuf sb
= STRBUF_INIT
;
179 if (!is_junk
|| getpid() != junk_pid
)
182 strbuf_addstr(&sb
, junk_git_dir
);
183 remove_dir_recursively(&sb
, 0);
186 if (junk_work_tree
) {
187 strbuf_addstr(&sb
, junk_work_tree
);
188 remove_dir_recursively(&sb
, 0);
193 static void remove_junk_on_signal(int signo
)
200 static const char *worktree_basename(const char *path
, int *olen
)
206 while (len
&& is_dir_sep(path
[len
- 1]))
209 for (name
= path
+ len
- 1; name
> path
; name
--)
210 if (is_dir_sep(*name
)) {
219 static int add_worktree(const char *path
, const char *refname
,
220 const struct add_opts
*opts
)
222 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
223 struct strbuf sb
= STRBUF_INIT
;
226 struct child_process cp
= CHILD_PROCESS_INIT
;
227 struct argv_array child_env
= ARGV_ARRAY_INIT
;
228 int counter
= 0, len
, ret
;
229 struct strbuf symref
= STRBUF_INIT
;
230 struct commit
*commit
= NULL
;
233 if (file_exists(path
) && !is_empty_dir(path
))
234 die(_("'%s' already exists"), path
);
236 /* is 'refname' a branch or commit? */
237 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
238 ref_exists(symref
.buf
)) {
241 die_if_checked_out(symref
.buf
, 0);
243 commit
= lookup_commit_reference_by_name(refname
);
245 die(_("invalid reference: %s"), refname
);
247 name
= worktree_basename(path
, &len
);
248 git_path_buf(&sb_repo
, "worktrees/%.*s", (int)(path
+ len
- name
), name
);
250 if (safe_create_leading_directories_const(sb_repo
.buf
))
251 die_errno(_("could not create leading directories of '%s'"),
253 while (!stat(sb_repo
.buf
, &st
)) {
255 strbuf_setlen(&sb_repo
, len
);
256 strbuf_addf(&sb_repo
, "%d", counter
);
258 name
= strrchr(sb_repo
.buf
, '/') + 1;
262 sigchain_push_common(remove_junk_on_signal
);
264 if (mkdir(sb_repo
.buf
, 0777))
265 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
266 junk_git_dir
= xstrdup(sb_repo
.buf
);
270 * lock the incomplete repo so prune won't delete it, unlock
271 * after the preparation is over.
273 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
274 if (!opts
->keep_locked
)
275 write_file(sb
.buf
, "initializing");
277 write_file(sb
.buf
, "added with --lock");
279 strbuf_addf(&sb_git
, "%s/.git", path
);
280 if (safe_create_leading_directories_const(sb_git
.buf
))
281 die_errno(_("could not create leading directories of '%s'"),
283 junk_work_tree
= xstrdup(path
);
286 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
287 write_file(sb
.buf
, "%s", real_path(sb_git
.buf
));
288 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
289 real_path(get_git_common_dir()), name
);
291 * This is to keep resolve_ref() happy. We need a valid HEAD
292 * or is_git_directory() will reject the directory. Any value which
293 * looks like an object ID will do since it will be immediately
294 * replaced by the symbolic-ref or update-ref invocation in the new
298 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
299 write_file(sb
.buf
, "%s", sha1_to_hex(null_sha1
));
301 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
302 write_file(sb
.buf
, "../..");
304 argv_array_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
305 argv_array_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
309 argv_array_pushl(&cp
.args
, "update-ref", "HEAD",
310 oid_to_hex(&commit
->object
.oid
), NULL
);
312 argv_array_pushl(&cp
.args
, "symbolic-ref", "HEAD",
314 cp
.env
= child_env
.argv
;
315 ret
= run_command(&cp
);
319 if (opts
->checkout
) {
321 argv_array_clear(&cp
.args
);
322 argv_array_pushl(&cp
.args
, "reset", "--hard", NULL
);
323 cp
.env
= child_env
.argv
;
324 ret
= run_command(&cp
);
330 FREE_AND_NULL(junk_work_tree
);
331 FREE_AND_NULL(junk_git_dir
);
334 if (ret
|| !opts
->keep_locked
) {
336 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
337 unlink_or_warn(sb
.buf
);
341 * Hook failure does not warrant worktree deletion, so run hook after
342 * is_junk is cleared, but do return appropriate code when hook fails.
344 if (!ret
&& opts
->checkout
)
345 ret
= run_hook_le(NULL
, "post-checkout", oid_to_hex(&null_oid
),
346 oid_to_hex(&commit
->object
.oid
), "1", NULL
);
348 argv_array_clear(&child_env
);
350 strbuf_release(&symref
);
351 strbuf_release(&sb_repo
);
352 strbuf_release(&sb_git
);
356 static void print_preparing_worktree_line(int detach
,
358 const char *new_branch
,
359 int force_new_branch
)
361 if (force_new_branch
) {
362 struct commit
*commit
= lookup_commit_reference_by_name(new_branch
);
364 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
366 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
368 find_unique_abbrev(commit
->object
.oid
.hash
,
370 } else if (new_branch
) {
371 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch
);
373 struct strbuf s
= STRBUF_INIT
;
374 if (!detach
&& !strbuf_check_branch_ref(&s
, branch
) &&
376 printf_ln(_("Preparing worktree (checking out '%s')"),
379 struct commit
*commit
= lookup_commit_reference_by_name(branch
);
381 die(_("invalid reference: %s"), branch
);
382 printf_ln(_("Preparing worktree (detached HEAD %s)"),
383 find_unique_abbrev(commit
->object
.oid
.hash
,
390 static int add(int ac
, const char **av
, const char *prefix
)
392 struct add_opts opts
;
393 const char *new_branch_force
= NULL
;
396 const char *new_branch
= NULL
;
397 const char *opt_track
= NULL
;
398 struct option options
[] = {
399 OPT__FORCE(&opts
.force
, N_("checkout <branch> even if already checked out in other worktree")),
400 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
401 N_("create a new branch")),
402 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
403 N_("create or reset a branch")),
404 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
405 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
406 OPT_BOOL(0, "lock", &opts
.keep_locked
, N_("keep the new working tree locked")),
407 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
408 N_("set up tracking mode (see git-branch(1))"),
409 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
410 OPT_BOOL(0, "guess-remote", &guess_remote
,
411 N_("try to match the new branch name with a remote-tracking branch")),
415 memset(&opts
, 0, sizeof(opts
));
417 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
418 if (!!opts
.detach
+ !!new_branch
+ !!new_branch_force
> 1)
419 die(_("-b, -B, and --detach are mutually exclusive"));
420 if (ac
< 1 || ac
> 2)
421 usage_with_options(worktree_usage
, options
);
423 path
= prefix_filename(prefix
, av
[0]);
424 branch
= ac
< 2 ? "HEAD" : av
[1];
426 if (!strcmp(branch
, "-"))
429 if (new_branch_force
) {
430 struct strbuf symref
= STRBUF_INIT
;
432 new_branch
= new_branch_force
;
435 !strbuf_check_branch_ref(&symref
, new_branch
) &&
436 ref_exists(symref
.buf
))
437 die_if_checked_out(symref
.buf
, 0);
438 strbuf_release(&symref
);
441 if (ac
< 2 && !new_branch
&& !opts
.detach
) {
443 const char *s
= worktree_basename(path
, &n
);
444 new_branch
= xstrndup(s
, n
);
447 struct object_id oid
;
449 unique_tracking_name(new_branch
, &oid
);
455 if (ac
== 2 && !new_branch
&& !opts
.detach
) {
456 struct object_id oid
;
457 struct commit
*commit
;
460 commit
= lookup_commit_reference_by_name(branch
);
462 remote
= unique_tracking_name(branch
, &oid
);
470 print_preparing_worktree_line(opts
.detach
, branch
, new_branch
, !!new_branch_force
);
473 struct child_process cp
= CHILD_PROCESS_INIT
;
475 argv_array_push(&cp
.args
, "branch");
476 if (new_branch_force
)
477 argv_array_push(&cp
.args
, "--force");
478 argv_array_push(&cp
.args
, new_branch
);
479 argv_array_push(&cp
.args
, branch
);
481 argv_array_push(&cp
.args
, opt_track
);
482 if (run_command(&cp
))
485 } else if (opt_track
) {
486 die(_("--[no-]track can only be used if a new branch is created"));
491 return add_worktree(path
, branch
, &opts
);
494 static void show_worktree_porcelain(struct worktree
*wt
)
496 printf("worktree %s\n", wt
->path
);
500 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
502 printf("detached\n");
503 else if (wt
->head_ref
)
504 printf("branch %s\n", wt
->head_ref
);
509 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
511 struct strbuf sb
= STRBUF_INIT
;
512 int cur_path_len
= strlen(wt
->path
);
513 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
515 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
517 strbuf_addstr(&sb
, "(bare)");
519 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
520 find_unique_abbrev(wt
->head_oid
.hash
, DEFAULT_ABBREV
));
522 strbuf_addstr(&sb
, "(detached HEAD)");
523 else if (wt
->head_ref
) {
524 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
525 strbuf_addf(&sb
, "[%s]", ref
);
528 strbuf_addstr(&sb
, "(error)");
530 printf("%s\n", sb
.buf
);
535 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
539 for (i
= 0; wt
[i
]; i
++) {
541 int path_len
= strlen(wt
[i
]->path
);
543 if (path_len
> *maxlen
)
545 sha1_len
= strlen(find_unique_abbrev(wt
[i
]->head_oid
.hash
, *abbrev
));
546 if (sha1_len
> *abbrev
)
551 static int list(int ac
, const char **av
, const char *prefix
)
555 struct option options
[] = {
556 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
560 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
562 usage_with_options(worktree_usage
, options
);
564 struct worktree
**worktrees
= get_worktrees(GWT_SORT_LINKED
);
565 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
568 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
570 for (i
= 0; worktrees
[i
]; i
++) {
572 show_worktree_porcelain(worktrees
[i
]);
574 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
576 free_worktrees(worktrees
);
581 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
583 const char *reason
= "", *old_reason
;
584 struct option options
[] = {
585 OPT_STRING(0, "reason", &reason
, N_("string"),
586 N_("reason for locking")),
589 struct worktree
**worktrees
, *wt
;
591 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
593 usage_with_options(worktree_usage
, options
);
595 worktrees
= get_worktrees(0);
596 wt
= find_worktree(worktrees
, prefix
, av
[0]);
598 die(_("'%s' is not a working tree"), av
[0]);
599 if (is_main_worktree(wt
))
600 die(_("The main working tree cannot be locked or unlocked"));
602 old_reason
= is_worktree_locked(wt
);
605 die(_("'%s' is already locked, reason: %s"),
607 die(_("'%s' is already locked"), av
[0]);
610 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
612 free_worktrees(worktrees
);
616 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
618 struct option options
[] = {
621 struct worktree
**worktrees
, *wt
;
624 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
626 usage_with_options(worktree_usage
, options
);
628 worktrees
= get_worktrees(0);
629 wt
= find_worktree(worktrees
, prefix
, av
[0]);
631 die(_("'%s' is not a working tree"), av
[0]);
632 if (is_main_worktree(wt
))
633 die(_("The main working tree cannot be locked or unlocked"));
634 if (!is_worktree_locked(wt
))
635 die(_("'%s' is not locked"), av
[0]);
636 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
637 free_worktrees(worktrees
);
641 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
643 struct option options
[] = {
647 git_config(git_worktree_config
, NULL
);
650 usage_with_options(worktree_usage
, options
);
653 if (!strcmp(av
[1], "add"))
654 return add(ac
- 1, av
+ 1, prefix
);
655 if (!strcmp(av
[1], "prune"))
656 return prune(ac
- 1, av
+ 1, prefix
);
657 if (!strcmp(av
[1], "list"))
658 return list(ac
- 1, av
+ 1, prefix
);
659 if (!strcmp(av
[1], "lock"))
660 return lock_worktree(ac
- 1, av
+ 1, prefix
);
661 if (!strcmp(av
[1], "unlock"))
662 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
663 usage_with_options(worktree_usage
, options
);