4 #include "parse-options.h"
5 #include "argv-array.h"
8 #include "run-command.h"
14 static const char * const worktree_usage[] = {
15 N_("git worktree add [<options>] <path> [<branch>]"),
16 N_("git worktree list [<options>]"),
17 N_("git worktree lock [<options>] <path>"),
18 N_("git worktree move [<options>] <oldpath> <newpath>"),
19 N_("git worktree prune [<options>]"),
20 N_("git worktree unlock <path>"),
28 const char *new_branch;
34 static unsigned long expire;
36 static int prune_worktree(const char *id, struct strbuf *reason)
42 if (!is_directory(git_path("worktrees/%s", id))) {
43 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
46 if (file_exists(git_path("worktrees/%s/locked", id)))
48 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
49 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
52 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
54 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
60 read_in_full(fd, path, len);
62 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
65 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
70 if (!file_exists(path)) {
74 * the repo is moved manually and has not been
77 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
80 if (st.st_mtime <= expire) {
81 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
91 static void prune_worktrees(void)
93 struct strbuf reason = STRBUF_INIT;
94 struct strbuf path = STRBUF_INIT;
95 DIR *dir = opendir(git_path("worktrees"));
100 while ((d = readdir(dir)) != NULL) {
101 if (is_dot_or_dotdot(d->d_name))
103 strbuf_reset(&reason);
104 if (!prune_worktree(d->d_name, &reason))
106 if (show_only || verbose)
107 printf("%s\n", reason.buf);
111 strbuf_addstr(&path, git_path("worktrees/%s", d->d_name));
112 ret = remove_dir_recursively(&path, 0);
113 if (ret < 0 && errno == ENOTDIR)
114 ret = unlink(path.buf);
116 error_errno(_("failed to remove '%s'"), path.buf);
120 rmdir(git_path("worktrees"));
121 strbuf_release(&reason);
122 strbuf_release(&path);
125 static int prune(int ac, const char **av, const char *prefix)
127 struct option options[] = {
128 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
129 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
130 OPT_EXPIRY_DATE(0, "expire", &expire,
131 N_("expire working trees older than <time>")),
136 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
138 usage_with_options(worktree_usage, options);
143 static char *junk_work_tree;
144 static char *junk_git_dir;
146 static pid_t junk_pid;
148 static void remove_junk(void)
150 struct strbuf sb = STRBUF_INIT;
151 if (!is_junk || getpid() != junk_pid)
154 strbuf_addstr(&sb, junk_git_dir);
155 remove_dir_recursively(&sb, 0);
158 if (junk_work_tree) {
159 strbuf_addstr(&sb, junk_work_tree);
160 remove_dir_recursively(&sb, 0);
165 static void remove_junk_on_signal(int signo)
172 static const char *worktree_basename(const char *path, int *olen)
178 while (len && is_dir_sep(path[len - 1]))
181 for (name = path + len - 1; name > path; name--)
182 if (is_dir_sep(*name)) {
191 static int add_worktree(const char *path, const char *refname,
192 const struct add_opts *opts)
194 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
195 struct strbuf sb = STRBUF_INIT;
198 struct child_process cp = CHILD_PROCESS_INIT;
199 struct argv_array child_env = ARGV_ARRAY_INIT;
200 int counter = 0, len, ret;
201 struct strbuf symref = STRBUF_INIT;
202 struct commit *commit = NULL;
204 if (file_exists(path) && !is_empty_dir(path))
205 die(_("'%s' already exists"), path);
207 /* is 'refname' a branch or commit? */
208 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
209 ref_exists(symref.buf)) { /* it's a branch */
211 die_if_checked_out(symref.buf, 0);
212 } else { /* must be a commit */
213 commit = lookup_commit_reference_by_name(refname);
215 die(_("invalid reference: %s"), refname);
218 name = worktree_basename(path, &len);
219 strbuf_addstr(&sb_repo,
220 git_path("worktrees/%.*s", (int)(path + len - name), name));
222 if (safe_create_leading_directories_const(sb_repo.buf))
223 die_errno(_("could not create leading directories of '%s'"),
225 while (!stat(sb_repo.buf, &st)) {
227 strbuf_setlen(&sb_repo, len);
228 strbuf_addf(&sb_repo, "%d", counter);
230 name = strrchr(sb_repo.buf, '/') + 1;
234 sigchain_push_common(remove_junk_on_signal);
236 if (mkdir(sb_repo.buf, 0777))
237 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
238 junk_git_dir = xstrdup(sb_repo.buf);
242 * lock the incomplete repo so prune won't delete it, unlock
243 * after the preparation is over.
245 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
246 write_file(sb.buf, "initializing");
248 strbuf_addf(&sb_git, "%s/.git", path);
249 if (safe_create_leading_directories_const(sb_git.buf))
250 die_errno(_("could not create leading directories of '%s'"),
252 junk_work_tree = xstrdup(path);
255 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
256 write_file(sb.buf, "%s", real_path(sb_git.buf));
257 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
258 real_path(get_git_common_dir()), name);
260 * This is to keep resolve_ref() happy. We need a valid HEAD
261 * or is_git_directory() will reject the directory. Any value which
262 * looks like an object ID will do since it will be immediately
263 * replaced by the symbolic-ref or update-ref invocation in the new
267 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
268 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
270 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
271 write_file(sb.buf, "../..");
273 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
275 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
276 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
280 argv_array_pushl(&cp.args, "update-ref", "HEAD",
281 oid_to_hex(&commit->object.oid), NULL);
283 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
285 cp.env = child_env.argv;
286 ret = run_command(&cp);
290 if (opts->checkout) {
292 argv_array_clear(&cp.args);
293 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
294 cp.env = child_env.argv;
295 ret = run_command(&cp);
301 free(junk_work_tree);
303 junk_work_tree = NULL;
308 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
309 unlink_or_warn(sb.buf);
310 argv_array_clear(&child_env);
312 strbuf_release(&symref);
313 strbuf_release(&sb_repo);
314 strbuf_release(&sb_git);
318 static int add(int ac, const char **av, const char *prefix)
320 struct add_opts opts;
321 const char *new_branch_force = NULL;
324 struct option options[] = {
325 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
326 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
327 N_("create a new branch")),
328 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
329 N_("create or reset a branch")),
330 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
331 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
335 memset(&opts, 0, sizeof(opts));
337 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
338 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
339 die(_("-b, -B, and --detach are mutually exclusive"));
340 if (ac < 1 || ac > 2)
341 usage_with_options(worktree_usage, options);
343 path = prefix_filename(prefix, av[0]);
344 branch = ac < 2 ? "HEAD" : av[1];
346 if (!strcmp(branch, "-"))
349 opts.force_new_branch = !!new_branch_force;
350 if (opts.force_new_branch) {
351 struct strbuf symref = STRBUF_INIT;
353 opts.new_branch = new_branch_force;
356 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
357 ref_exists(symref.buf))
358 die_if_checked_out(symref.buf, 0);
359 strbuf_release(&symref);
362 if (ac < 2 && !opts.new_branch && !opts.detach) {
364 const char *s = worktree_basename(path, &n);
365 opts.new_branch = xstrndup(s, n);
368 if (opts.new_branch) {
369 struct child_process cp = CHILD_PROCESS_INIT;
371 argv_array_push(&cp.args, "branch");
372 if (opts.force_new_branch)
373 argv_array_push(&cp.args, "--force");
374 argv_array_push(&cp.args, opts.new_branch);
375 argv_array_push(&cp.args, branch);
376 if (run_command(&cp))
378 branch = opts.new_branch;
381 return add_worktree(path, branch, &opts);
384 static void show_worktree_porcelain(struct worktree *wt)
386 printf("worktree %s\n", wt->path);
390 printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
392 printf("detached\n");
393 else if (wt->head_ref)
394 printf("branch %s\n", wt->head_ref);
399 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
401 struct strbuf sb = STRBUF_INIT;
402 int cur_path_len = strlen(wt->path);
403 int path_adj = cur_path_len - utf8_strwidth(wt->path);
405 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
407 strbuf_addstr(&sb, "(bare)");
409 strbuf_addf(&sb, "%-*s ", abbrev_len,
410 find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
412 strbuf_addstr(&sb, "(detached HEAD)");
413 else if (wt->head_ref)
414 strbuf_addf(&sb, "[%s]", shorten_unambiguous_ref(wt->head_ref, 0));
416 strbuf_addstr(&sb, "(error)");
418 printf("%s\n", sb.buf);
423 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
427 for (i = 0; wt[i]; i++) {
429 int path_len = strlen(wt[i]->path);
431 if (path_len > *maxlen)
433 sha1_len = strlen(find_unique_abbrev(wt[i]->head_sha1, *abbrev));
434 if (sha1_len > *abbrev)
439 static int list(int ac, const char **av, const char *prefix)
443 struct option options[] = {
444 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
448 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
450 usage_with_options(worktree_usage, options);
452 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
453 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
456 measure_widths(worktrees, &abbrev, &path_maxlen);
458 for (i = 0; worktrees[i]; i++) {
460 show_worktree_porcelain(worktrees[i]);
462 show_worktree(worktrees[i], path_maxlen, abbrev);
464 free_worktrees(worktrees);
469 static int lock_worktree(int ac, const char **av, const char *prefix)
471 const char *reason = "", *old_reason;
472 struct option options[] = {
473 OPT_STRING(0, "reason", &reason, N_("string"),
474 N_("reason for locking")),
477 struct worktree **worktrees, *wt;
479 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
481 usage_with_options(worktree_usage, options);
483 worktrees = get_worktrees(0);
484 wt = find_worktree(worktrees, prefix, av[0]);
486 die(_("'%s' is not a working tree"), av[0]);
487 if (is_main_worktree(wt))
488 die(_("The main working tree cannot be locked or unlocked"));
490 old_reason = is_worktree_locked(wt);
493 die(_("'%s' is already locked, reason: %s"),
495 die(_("'%s' is already locked"), av[0]);
498 write_file(git_common_path("worktrees/%s/locked", wt->id),
500 free_worktrees(worktrees);
504 static int unlock_worktree(int ac, const char **av, const char *prefix)
506 struct option options[] = {
509 struct worktree **worktrees, *wt;
512 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
514 usage_with_options(worktree_usage, options);
516 worktrees = get_worktrees(0);
517 wt = find_worktree(worktrees, prefix, av[0]);
519 die(_("'%s' is not a working tree"), av[0]);
520 if (is_main_worktree(wt))
521 die(_("The main working tree cannot be locked or unlocked"));
522 if (!is_worktree_locked(wt))
523 die(_("'%s' is not locked"), av[0]);
524 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
525 free_worktrees(worktrees);
529 static int move_worktree(int ac, const char **av, const char *prefix)
531 const char *lock_reason;
532 struct option options[] = {
533 OPT__DRY_RUN(&show_only, N_("do not move, show only")),
536 struct worktree **worktrees, *wt;
539 struct strbuf sb_path = STRBUF_INIT;
540 int len, ret = 0, ret2 = 0;
542 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
544 usage_with_options(worktree_usage, options);
546 worktrees = get_worktrees(0);
548 wt = find_worktree(worktrees, prefix, av[1]);
550 die(_("worktree '%s' exists already"), av[1]);
552 path = prefix_filename(prefix, av[1]);
553 if (file_exists(path))
554 die(_("'%s' already exists"), path);
556 name = worktree_basename(path, &len);
557 strbuf_add_real_path(&sb_path, path);
559 wt = find_worktree(worktrees, prefix, av[0]);
561 die(_("'%s' is not a working tree"), av[0]);
562 /* Moving the main worktree could potentially be doable by changing all
563 * references from the associated worktrees too, but we don't support
565 if (is_main_worktree(wt))
566 die(_("The main working tree cannot be moved"));
568 lock_reason = is_worktree_locked(wt);
571 die(_("'%s' is locked, reason: %s"), av[0], lock_reason);
572 die(_("'%s' is locked"), av[0]);
576 printf("would lock %s\n", wt->id);
578 struct strbuf sb_lock = STRBUF_INIT;
579 strbuf_git_common_path(&sb_lock, "worktrees/%s/locked", wt->id);
580 write_file(sb_lock.buf, "moving\nfrom %s\nto %s", wt->path, sb_path.buf);
581 strbuf_reset(&sb_lock);
585 printf("would move %s to %s\n", wt->path, sb_path.buf);
587 ret = rename(wt->path, sb_path.buf);
593 printf("would move worktrees/%s to worktrees/%s\n", wt->id, name);
596 struct strbuf wtold = STRBUF_INIT, wtnew = STRBUF_INIT;
597 strbuf_git_common_path(&wtold, "worktrees/%s", wt->id);
598 strbuf_git_common_path(&wtnew, "worktrees/%s", name);
599 ret = rename(wtold.buf, wtnew.buf);
604 wt->id = xstrdup(name);
606 strbuf_reset(&wtold);
607 strbuf_reset(&wtnew);
612 printf("would fixup gitdir for %s", wt->id);
615 struct strbuf sb_gitdir = STRBUF_INIT,
616 sb_git = STRBUF_INIT,
617 sb_real_git = STRBUF_INIT;
618 strbuf_git_common_path(&sb_gitdir, "worktrees/%s/gitdir", wt->id);
619 strbuf_addf(&sb_git, "%s/.git", sb_path.buf);
620 strbuf_add_real_path(&sb_real_git, sb_gitdir.buf);
621 strbuf_remove(&sb_real_git, sb_real_git.len - 7, 7);
623 write_file(sb_gitdir.buf, "%s", sb_git.buf);
624 write_file(sb_git.buf, "gitdir: %s", sb_real_git.buf);
626 strbuf_reset(&sb_real_git);
627 strbuf_reset(&sb_git);
628 strbuf_reset(&sb_gitdir);
633 printf("would unlock %s\n", wt->id);
635 struct strbuf sb_lock = STRBUF_INIT;
636 strbuf_git_common_path(&sb_lock, "worktrees/%s/locked", wt->id);
637 ret2 = unlink_or_warn(sb_lock.buf);
638 strbuf_reset(&sb_lock);
641 free_worktrees(worktrees);
644 /* return the errno from the failed rename, if any, else the one from the
646 return ret ? ret : ret2;
650 int cmd_worktree(int ac, const char **av, const char *prefix)
652 struct option options[] = {
656 git_config(git_default_config, NULL);
659 usage_with_options(worktree_usage, options);
662 if (!strcmp(av[1], "add"))
663 return add(ac - 1, av + 1, prefix);
664 if (!strcmp(av[1], "prune"))
665 return prune(ac - 1, av + 1, prefix);
666 if (!strcmp(av[1], "list"))
667 return list(ac - 1, av + 1, prefix);
668 if (!strcmp(av[1], "lock"))
669 return lock_worktree(ac - 1, av + 1, prefix);
670 if (!strcmp(av[1], "move"))
671 return move_worktree(ac - 1, av + 1, prefix);
672 if (!strcmp(av[1], "unlock"))
673 return unlock_worktree(ac - 1, av + 1, prefix);
674 usage_with_options(worktree_usage, options);