5 #include "parse-options.h"
6 #include "argv-array.h"
9 #include "run-command.h"
15 static const char * const worktree_usage[] = {
16 N_("git worktree add [<options>] <path> [<branch>]"),
17 N_("git worktree list [<options>]"),
18 N_("git worktree lock [<options>] <path>"),
19 N_("git worktree prune [<options>]"),
20 N_("git worktree unlock <path>"),
29 const char *new_branch;
35 static timestamp_t expire;
37 static int prune_worktree(const char *id, struct strbuf *reason)
44 if (!is_directory(git_path("worktrees/%s", id))) {
45 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
48 if (file_exists(git_path("worktrees/%s/locked", id)))
50 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
51 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
54 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
56 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
60 len = xsize_t(st.st_size);
62 read_in_full(fd, path, len);
64 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
67 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
72 if (!file_exists(path)) {
76 * the repo is moved manually and has not been
79 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
82 if (st.st_mtime <= expire) {
83 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
93 static void prune_worktrees(void)
95 struct strbuf reason = STRBUF_INIT;
96 struct strbuf path = STRBUF_INIT;
97 DIR *dir = opendir(git_path("worktrees"));
102 while ((d = readdir(dir)) != NULL) {
103 if (is_dot_or_dotdot(d->d_name))
105 strbuf_reset(&reason);
106 if (!prune_worktree(d->d_name, &reason))
108 if (show_only || verbose)
109 printf("%s\n", reason.buf);
112 git_path_buf(&path, "worktrees/%s", d->d_name);
113 ret = remove_dir_recursively(&path, 0);
114 if (ret < 0 && errno == ENOTDIR)
115 ret = unlink(path.buf);
117 error_errno(_("failed to remove '%s'"), path.buf);
121 rmdir(git_path("worktrees"));
122 strbuf_release(&reason);
123 strbuf_release(&path);
126 static int prune(int ac, const char **av, const char *prefix)
128 struct option options[] = {
129 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
130 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
131 OPT_EXPIRY_DATE(0, "expire", &expire,
132 N_("expire working trees older than <time>")),
137 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
139 usage_with_options(worktree_usage, options);
144 static char *junk_work_tree;
145 static char *junk_git_dir;
147 static pid_t junk_pid;
149 static void remove_junk(void)
151 struct strbuf sb = STRBUF_INIT;
152 if (!is_junk || getpid() != junk_pid)
155 strbuf_addstr(&sb, junk_git_dir);
156 remove_dir_recursively(&sb, 0);
159 if (junk_work_tree) {
160 strbuf_addstr(&sb, junk_work_tree);
161 remove_dir_recursively(&sb, 0);
166 static void remove_junk_on_signal(int signo)
173 static const char *worktree_basename(const char *path, int *olen)
179 while (len && is_dir_sep(path[len - 1]))
182 for (name = path + len - 1; name > path; name--)
183 if (is_dir_sep(*name)) {
192 static int add_worktree(const char *path, const char *refname,
193 const struct add_opts *opts)
195 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
196 struct strbuf sb = STRBUF_INIT;
199 struct child_process cp = CHILD_PROCESS_INIT;
200 struct argv_array child_env = ARGV_ARRAY_INIT;
201 int counter = 0, len, ret;
202 struct strbuf symref = STRBUF_INIT;
203 struct commit *commit = NULL;
205 if (file_exists(path) && !is_empty_dir(path))
206 die(_("'%s' already exists"), path);
208 /* is 'refname' a branch or commit? */
209 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
210 ref_exists(symref.buf)) { /* it's a branch */
212 die_if_checked_out(symref.buf, 0);
213 } else { /* must be a commit */
214 commit = lookup_commit_reference_by_name(refname);
216 die(_("invalid reference: %s"), refname);
219 name = worktree_basename(path, &len);
220 git_path_buf(&sb_repo, "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 if (!opts->keep_locked)
247 write_file(sb.buf, "initializing");
249 write_file(sb.buf, "added with --lock");
251 strbuf_addf(&sb_git, "%s/.git", path);
252 if (safe_create_leading_directories_const(sb_git.buf))
253 die_errno(_("could not create leading directories of '%s'"),
255 junk_work_tree = xstrdup(path);
258 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
259 write_file(sb.buf, "%s", real_path(sb_git.buf));
260 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
261 real_path(get_git_common_dir()), name);
263 * This is to keep resolve_ref() happy. We need a valid HEAD
264 * or is_git_directory() will reject the directory. Any value which
265 * looks like an object ID will do since it will be immediately
266 * replaced by the symbolic-ref or update-ref invocation in the new
270 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
271 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
273 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
274 write_file(sb.buf, "../..");
276 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
278 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
279 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
283 argv_array_pushl(&cp.args, "update-ref", "HEAD",
284 oid_to_hex(&commit->object.oid), NULL);
286 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
288 cp.env = child_env.argv;
289 ret = run_command(&cp);
293 if (opts->checkout) {
295 argv_array_clear(&cp.args);
296 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
297 cp.env = child_env.argv;
298 ret = run_command(&cp);
304 FREE_AND_NULL(junk_work_tree);
305 FREE_AND_NULL(junk_git_dir);
308 if (ret || !opts->keep_locked) {
310 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
311 unlink_or_warn(sb.buf);
313 argv_array_clear(&child_env);
315 strbuf_release(&symref);
316 strbuf_release(&sb_repo);
317 strbuf_release(&sb_git);
321 static int add(int ac, const char **av, const char *prefix)
323 struct add_opts opts;
324 const char *new_branch_force = NULL;
327 struct option options[] = {
328 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
329 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
330 N_("create a new branch")),
331 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
332 N_("create or reset a branch")),
333 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
334 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
335 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
339 memset(&opts, 0, sizeof(opts));
341 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
342 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
343 die(_("-b, -B, and --detach are mutually exclusive"));
344 if (ac < 1 || ac > 2)
345 usage_with_options(worktree_usage, options);
347 path = prefix_filename(prefix, av[0]);
348 branch = ac < 2 ? "HEAD" : av[1];
350 if (!strcmp(branch, "-"))
353 opts.force_new_branch = !!new_branch_force;
354 if (opts.force_new_branch) {
355 struct strbuf symref = STRBUF_INIT;
357 opts.new_branch = new_branch_force;
360 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
361 ref_exists(symref.buf))
362 die_if_checked_out(symref.buf, 0);
363 strbuf_release(&symref);
366 if (ac < 2 && !opts.new_branch && !opts.detach) {
368 const char *s = worktree_basename(path, &n);
369 opts.new_branch = xstrndup(s, n);
372 if (opts.new_branch) {
373 struct child_process cp = CHILD_PROCESS_INIT;
375 argv_array_push(&cp.args, "branch");
376 if (opts.force_new_branch)
377 argv_array_push(&cp.args, "--force");
378 argv_array_push(&cp.args, opts.new_branch);
379 argv_array_push(&cp.args, branch);
380 if (run_command(&cp))
382 branch = opts.new_branch;
387 return add_worktree(path, branch, &opts);
390 static void show_worktree_porcelain(struct worktree *wt)
392 printf("worktree %s\n", wt->path);
396 printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
398 printf("detached\n");
399 else if (wt->head_ref)
400 printf("branch %s\n", wt->head_ref);
405 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
407 struct strbuf sb = STRBUF_INIT;
408 int cur_path_len = strlen(wt->path);
409 int path_adj = cur_path_len - utf8_strwidth(wt->path);
411 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
413 strbuf_addstr(&sb, "(bare)");
415 strbuf_addf(&sb, "%-*s ", abbrev_len,
416 find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
418 strbuf_addstr(&sb, "(detached HEAD)");
419 else if (wt->head_ref) {
420 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
421 strbuf_addf(&sb, "[%s]", ref);
424 strbuf_addstr(&sb, "(error)");
426 printf("%s\n", sb.buf);
431 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
435 for (i = 0; wt[i]; i++) {
437 int path_len = strlen(wt[i]->path);
439 if (path_len > *maxlen)
441 sha1_len = strlen(find_unique_abbrev(wt[i]->head_sha1, *abbrev));
442 if (sha1_len > *abbrev)
447 static int list(int ac, const char **av, const char *prefix)
451 struct option options[] = {
452 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
456 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
458 usage_with_options(worktree_usage, options);
460 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
461 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
464 measure_widths(worktrees, &abbrev, &path_maxlen);
466 for (i = 0; worktrees[i]; i++) {
468 show_worktree_porcelain(worktrees[i]);
470 show_worktree(worktrees[i], path_maxlen, abbrev);
472 free_worktrees(worktrees);
477 static int lock_worktree(int ac, const char **av, const char *prefix)
479 const char *reason = "", *old_reason;
480 struct option options[] = {
481 OPT_STRING(0, "reason", &reason, N_("string"),
482 N_("reason for locking")),
485 struct worktree **worktrees, *wt;
487 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
489 usage_with_options(worktree_usage, options);
491 worktrees = get_worktrees(0);
492 wt = find_worktree(worktrees, prefix, av[0]);
494 die(_("'%s' is not a working tree"), av[0]);
495 if (is_main_worktree(wt))
496 die(_("The main working tree cannot be locked or unlocked"));
498 old_reason = is_worktree_locked(wt);
501 die(_("'%s' is already locked, reason: %s"),
503 die(_("'%s' is already locked"), av[0]);
506 write_file(git_common_path("worktrees/%s/locked", wt->id),
508 free_worktrees(worktrees);
512 static int unlock_worktree(int ac, const char **av, const char *prefix)
514 struct option options[] = {
517 struct worktree **worktrees, *wt;
520 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
522 usage_with_options(worktree_usage, options);
524 worktrees = get_worktrees(0);
525 wt = find_worktree(worktrees, prefix, av[0]);
527 die(_("'%s' is not a working tree"), av[0]);
528 if (is_main_worktree(wt))
529 die(_("The main working tree cannot be locked or unlocked"));
530 if (!is_worktree_locked(wt))
531 die(_("'%s' is not locked"), av[0]);
532 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
533 free_worktrees(worktrees);
537 int cmd_worktree(int ac, const char **av, const char *prefix)
539 struct option options[] = {
543 git_config(git_default_config, NULL);
546 usage_with_options(worktree_usage, options);
549 if (!strcmp(av[1], "add"))
550 return add(ac - 1, av + 1, prefix);
551 if (!strcmp(av[1], "prune"))
552 return prune(ac - 1, av + 1, prefix);
553 if (!strcmp(av[1], "list"))
554 return list(ac - 1, av + 1, prefix);
555 if (!strcmp(av[1], "lock"))
556 return lock_worktree(ac - 1, av + 1, prefix);
557 if (!strcmp(av[1], "unlock"))
558 return unlock_worktree(ac - 1, av + 1, prefix);
559 usage_with_options(worktree_usage, options);