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>"),
30 const char *new_branch
;
36 static int guess_remote
;
37 static timestamp_t expire
;
39 static int git_worktree_config(const char *var
, const char *value
, void *cb
)
41 if (!strcmp(var
, "worktree.guessremote")) {
42 guess_remote
= git_config_bool(var
, value
);
46 return git_default_config(var
, value
, cb
);
49 static int prune_worktree(const char *id
, struct strbuf
*reason
)
57 if (!is_directory(git_path("worktrees/%s", id
))) {
58 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
61 if (file_exists(git_path("worktrees/%s/locked", id
)))
63 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
64 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
67 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
69 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
73 len
= xsize_t(st
.st_size
);
76 read_result
= read_in_full(fd
, path
, len
);
77 if (read_result
< 0) {
78 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
86 if (read_result
!= len
) {
88 _("Removing worktrees/%s: short read (expected %"PRIuMAX
" bytes, read %"PRIuMAX
")"),
89 id
, (uintmax_t)len
, (uintmax_t)read_result
);
93 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
96 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
101 if (!file_exists(path
)) {
105 * the repo is moved manually and has not been
108 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
109 st_link
.st_nlink
> 1)
111 if (st
.st_mtime
<= expire
) {
112 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
122 static void prune_worktrees(void)
124 struct strbuf reason
= STRBUF_INIT
;
125 struct strbuf path
= STRBUF_INIT
;
126 DIR *dir
= opendir(git_path("worktrees"));
131 while ((d
= readdir(dir
)) != NULL
) {
132 if (is_dot_or_dotdot(d
->d_name
))
134 strbuf_reset(&reason
);
135 if (!prune_worktree(d
->d_name
, &reason
))
137 if (show_only
|| verbose
)
138 printf("%s\n", reason
.buf
);
141 git_path_buf(&path
, "worktrees/%s", d
->d_name
);
142 ret
= remove_dir_recursively(&path
, 0);
143 if (ret
< 0 && errno
== ENOTDIR
)
144 ret
= unlink(path
.buf
);
146 error_errno(_("failed to remove '%s'"), path
.buf
);
150 rmdir(git_path("worktrees"));
151 strbuf_release(&reason
);
152 strbuf_release(&path
);
155 static int prune(int ac
, const char **av
, const char *prefix
)
157 struct option options
[] = {
158 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
159 OPT__VERBOSE(&verbose
, N_("report pruned working trees")),
160 OPT_EXPIRY_DATE(0, "expire", &expire
,
161 N_("expire working trees older than <time>")),
166 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
168 usage_with_options(worktree_usage
, options
);
173 static char *junk_work_tree
;
174 static char *junk_git_dir
;
176 static pid_t junk_pid
;
178 static void remove_junk(void)
180 struct strbuf sb
= STRBUF_INIT
;
181 if (!is_junk
|| getpid() != junk_pid
)
184 strbuf_addstr(&sb
, junk_git_dir
);
185 remove_dir_recursively(&sb
, 0);
188 if (junk_work_tree
) {
189 strbuf_addstr(&sb
, junk_work_tree
);
190 remove_dir_recursively(&sb
, 0);
195 static void remove_junk_on_signal(int signo
)
202 static const char *worktree_basename(const char *path
, int *olen
)
208 while (len
&& is_dir_sep(path
[len
- 1]))
211 for (name
= path
+ len
- 1; name
> path
; name
--)
212 if (is_dir_sep(*name
)) {
221 static int add_worktree(const char *path
, const char *refname
,
222 const struct add_opts
*opts
)
224 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
225 struct strbuf sb
= STRBUF_INIT
;
228 struct child_process cp
= CHILD_PROCESS_INIT
;
229 struct argv_array child_env
= ARGV_ARRAY_INIT
;
230 int counter
= 0, len
, ret
;
231 struct strbuf symref
= STRBUF_INIT
;
232 struct commit
*commit
= NULL
;
235 if (file_exists(path
) && !is_empty_dir(path
))
236 die(_("'%s' already exists"), path
);
238 /* is 'refname' a branch or commit? */
239 if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
240 ref_exists(symref
.buf
)) {
243 die_if_checked_out(symref
.buf
, 0);
245 commit
= lookup_commit_reference_by_name(refname
);
247 die(_("invalid reference: %s"), refname
);
249 name
= worktree_basename(path
, &len
);
250 git_path_buf(&sb_repo
, "worktrees/%.*s", (int)(path
+ len
- name
), name
);
252 if (safe_create_leading_directories_const(sb_repo
.buf
))
253 die_errno(_("could not create leading directories of '%s'"),
255 while (!stat(sb_repo
.buf
, &st
)) {
257 strbuf_setlen(&sb_repo
, len
);
258 strbuf_addf(&sb_repo
, "%d", counter
);
260 name
= strrchr(sb_repo
.buf
, '/') + 1;
264 sigchain_push_common(remove_junk_on_signal
);
266 if (mkdir(sb_repo
.buf
, 0777))
267 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
268 junk_git_dir
= xstrdup(sb_repo
.buf
);
272 * lock the incomplete repo so prune won't delete it, unlock
273 * after the preparation is over.
275 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
276 if (!opts
->keep_locked
)
277 write_file(sb
.buf
, "initializing");
279 write_file(sb
.buf
, "added with --lock");
281 strbuf_addf(&sb_git
, "%s/.git", path
);
282 if (safe_create_leading_directories_const(sb_git
.buf
))
283 die_errno(_("could not create leading directories of '%s'"),
285 junk_work_tree
= xstrdup(path
);
288 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
289 write_file(sb
.buf
, "%s", real_path(sb_git
.buf
));
290 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
291 real_path(get_git_common_dir()), name
);
293 * This is to keep resolve_ref() happy. We need a valid HEAD
294 * or is_git_directory() will reject the directory. Any value which
295 * looks like an object ID will do since it will be immediately
296 * replaced by the symbolic-ref or update-ref invocation in the new
300 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
301 write_file(sb
.buf
, "%s", sha1_to_hex(null_sha1
));
303 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
304 write_file(sb
.buf
, "../..");
306 fprintf_ln(stderr
, _("Preparing %s (identifier %s)"), path
, name
);
308 argv_array_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
309 argv_array_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
313 argv_array_pushl(&cp
.args
, "update-ref", "HEAD",
314 oid_to_hex(&commit
->object
.oid
), NULL
);
316 argv_array_pushl(&cp
.args
, "symbolic-ref", "HEAD",
318 cp
.env
= child_env
.argv
;
319 ret
= run_command(&cp
);
323 if (opts
->checkout
) {
325 argv_array_clear(&cp
.args
);
326 argv_array_pushl(&cp
.args
, "reset", "--hard", NULL
);
327 cp
.env
= child_env
.argv
;
328 ret
= run_command(&cp
);
334 FREE_AND_NULL(junk_work_tree
);
335 FREE_AND_NULL(junk_git_dir
);
338 if (ret
|| !opts
->keep_locked
) {
340 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
341 unlink_or_warn(sb
.buf
);
345 * Hook failure does not warrant worktree deletion, so run hook after
346 * is_junk is cleared, but do return appropriate code when hook fails.
348 if (!ret
&& opts
->checkout
)
349 ret
= run_hook_le(NULL
, "post-checkout", oid_to_hex(&null_oid
),
350 oid_to_hex(&commit
->object
.oid
), "1", NULL
);
352 argv_array_clear(&child_env
);
354 strbuf_release(&symref
);
355 strbuf_release(&sb_repo
);
356 strbuf_release(&sb_git
);
360 static int add(int ac
, const char **av
, const char *prefix
)
362 struct add_opts opts
;
363 const char *new_branch_force
= NULL
;
366 const char *opt_track
= NULL
;
367 struct option options
[] = {
368 OPT__FORCE(&opts
.force
,
369 N_("checkout <branch> even if already checked out in other worktree"),
371 OPT_STRING('b', NULL
, &opts
.new_branch
, N_("branch"),
372 N_("create a new branch")),
373 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
374 N_("create or reset a branch")),
375 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
376 OPT_BOOL(0, "checkout", &opts
.checkout
, N_("populate the new working tree")),
377 OPT_BOOL(0, "lock", &opts
.keep_locked
, N_("keep the new working tree locked")),
378 OPT_PASSTHRU(0, "track", &opt_track
, NULL
,
379 N_("set up tracking mode (see git-branch(1))"),
380 PARSE_OPT_NOARG
| PARSE_OPT_OPTARG
),
381 OPT_BOOL(0, "guess-remote", &guess_remote
,
382 N_("try to match the new branch name with a remote-tracking branch")),
386 memset(&opts
, 0, sizeof(opts
));
388 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
389 if (!!opts
.detach
+ !!opts
.new_branch
+ !!new_branch_force
> 1)
390 die(_("-b, -B, and --detach are mutually exclusive"));
391 if (ac
< 1 || ac
> 2)
392 usage_with_options(worktree_usage
, options
);
394 path
= prefix_filename(prefix
, av
[0]);
395 branch
= ac
< 2 ? "HEAD" : av
[1];
397 if (!strcmp(branch
, "-"))
400 opts
.force_new_branch
= !!new_branch_force
;
401 if (opts
.force_new_branch
) {
402 struct strbuf symref
= STRBUF_INIT
;
404 opts
.new_branch
= new_branch_force
;
407 !strbuf_check_branch_ref(&symref
, opts
.new_branch
) &&
408 ref_exists(symref
.buf
))
409 die_if_checked_out(symref
.buf
, 0);
410 strbuf_release(&symref
);
413 if (ac
< 2 && !opts
.new_branch
&& !opts
.detach
) {
415 const char *s
= worktree_basename(path
, &n
);
416 opts
.new_branch
= xstrndup(s
, n
);
418 struct object_id oid
;
420 unique_tracking_name(opts
.new_branch
, &oid
);
426 if (ac
== 2 && !opts
.new_branch
&& !opts
.detach
) {
427 struct object_id oid
;
428 struct commit
*commit
;
431 commit
= lookup_commit_reference_by_name(branch
);
433 remote
= unique_tracking_name(branch
, &oid
);
435 opts
.new_branch
= branch
;
441 if (opts
.new_branch
) {
442 struct child_process cp
= CHILD_PROCESS_INIT
;
444 argv_array_push(&cp
.args
, "branch");
445 if (opts
.force_new_branch
)
446 argv_array_push(&cp
.args
, "--force");
447 argv_array_push(&cp
.args
, opts
.new_branch
);
448 argv_array_push(&cp
.args
, branch
);
450 argv_array_push(&cp
.args
, opt_track
);
451 if (run_command(&cp
))
453 branch
= opts
.new_branch
;
454 } else if (opt_track
) {
455 die(_("--[no-]track can only be used if a new branch is created"));
460 return add_worktree(path
, branch
, &opts
);
463 static void show_worktree_porcelain(struct worktree
*wt
)
465 printf("worktree %s\n", wt
->path
);
469 printf("HEAD %s\n", oid_to_hex(&wt
->head_oid
));
471 printf("detached\n");
472 else if (wt
->head_ref
)
473 printf("branch %s\n", wt
->head_ref
);
478 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
480 struct strbuf sb
= STRBUF_INIT
;
481 int cur_path_len
= strlen(wt
->path
);
482 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
484 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
486 strbuf_addstr(&sb
, "(bare)");
488 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
489 find_unique_abbrev(wt
->head_oid
.hash
, DEFAULT_ABBREV
));
491 strbuf_addstr(&sb
, "(detached HEAD)");
492 else if (wt
->head_ref
) {
493 char *ref
= shorten_unambiguous_ref(wt
->head_ref
, 0);
494 strbuf_addf(&sb
, "[%s]", ref
);
497 strbuf_addstr(&sb
, "(error)");
499 printf("%s\n", sb
.buf
);
504 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
508 for (i
= 0; wt
[i
]; i
++) {
510 int path_len
= strlen(wt
[i
]->path
);
512 if (path_len
> *maxlen
)
514 sha1_len
= strlen(find_unique_abbrev(wt
[i
]->head_oid
.hash
, *abbrev
));
515 if (sha1_len
> *abbrev
)
520 static int list(int ac
, const char **av
, const char *prefix
)
524 struct option options
[] = {
525 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
529 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
531 usage_with_options(worktree_usage
, options
);
533 struct worktree
**worktrees
= get_worktrees(GWT_SORT_LINKED
);
534 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
537 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
539 for (i
= 0; worktrees
[i
]; i
++) {
541 show_worktree_porcelain(worktrees
[i
]);
543 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
545 free_worktrees(worktrees
);
550 static int lock_worktree(int ac
, const char **av
, const char *prefix
)
552 const char *reason
= "", *old_reason
;
553 struct option options
[] = {
554 OPT_STRING(0, "reason", &reason
, N_("string"),
555 N_("reason for locking")),
558 struct worktree
**worktrees
, *wt
;
560 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
562 usage_with_options(worktree_usage
, options
);
564 worktrees
= get_worktrees(0);
565 wt
= find_worktree(worktrees
, prefix
, av
[0]);
567 die(_("'%s' is not a working tree"), av
[0]);
568 if (is_main_worktree(wt
))
569 die(_("The main working tree cannot be locked or unlocked"));
571 old_reason
= is_worktree_locked(wt
);
574 die(_("'%s' is already locked, reason: %s"),
576 die(_("'%s' is already locked"), av
[0]);
579 write_file(git_common_path("worktrees/%s/locked", wt
->id
),
581 free_worktrees(worktrees
);
585 static int unlock_worktree(int ac
, const char **av
, const char *prefix
)
587 struct option options
[] = {
590 struct worktree
**worktrees
, *wt
;
593 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
595 usage_with_options(worktree_usage
, options
);
597 worktrees
= get_worktrees(0);
598 wt
= find_worktree(worktrees
, prefix
, av
[0]);
600 die(_("'%s' is not a working tree"), av
[0]);
601 if (is_main_worktree(wt
))
602 die(_("The main working tree cannot be locked or unlocked"));
603 if (!is_worktree_locked(wt
))
604 die(_("'%s' is not locked"), av
[0]);
605 ret
= unlink_or_warn(git_common_path("worktrees/%s/locked", wt
->id
));
606 free_worktrees(worktrees
);
610 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
612 struct option options
[] = {
616 git_config(git_worktree_config
, NULL
);
619 usage_with_options(worktree_usage
, options
);
622 if (!strcmp(av
[1], "add"))
623 return add(ac
- 1, av
+ 1, prefix
);
624 if (!strcmp(av
[1], "prune"))
625 return prune(ac
- 1, av
+ 1, prefix
);
626 if (!strcmp(av
[1], "list"))
627 return list(ac
- 1, av
+ 1, prefix
);
628 if (!strcmp(av
[1], "lock"))
629 return lock_worktree(ac
- 1, av
+ 1, prefix
);
630 if (!strcmp(av
[1], "unlock"))
631 return unlock_worktree(ac
- 1, av
+ 1, prefix
);
632 usage_with_options(worktree_usage
, options
);