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)
43 if (!is_directory(git_path("worktrees/%s", id))) {
44 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
47 if (file_exists(git_path("worktrees/%s/locked", id)))
49 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
50 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
53 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
55 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
61 read_in_full(fd, path, len);
63 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
66 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
71 if (!file_exists(path)) {
75 * the repo is moved manually and has not been
78 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
81 if (st.st_mtime <= expire) {
82 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
92 static void prune_worktrees(void)
94 struct strbuf reason = STRBUF_INIT;
95 struct strbuf path = STRBUF_INIT;
96 DIR *dir = opendir(git_path("worktrees"));
101 while ((d = readdir(dir)) != NULL) {
102 if (is_dot_or_dotdot(d->d_name))
104 strbuf_reset(&reason);
105 if (!prune_worktree(d->d_name, &reason))
107 if (show_only || verbose)
108 printf("%s\n", reason.buf);
111 git_path_buf(&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 git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name);
221 if (safe_create_leading_directories_const(sb_repo.buf))
222 die_errno(_("could not create leading directories of '%s'"),
224 while (!stat(sb_repo.buf, &st)) {
226 strbuf_setlen(&sb_repo, len);
227 strbuf_addf(&sb_repo, "%d", counter);
229 name = strrchr(sb_repo.buf, '/') + 1;
233 sigchain_push_common(remove_junk_on_signal);
235 if (mkdir(sb_repo.buf, 0777))
236 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
237 junk_git_dir = xstrdup(sb_repo.buf);
241 * lock the incomplete repo so prune won't delete it, unlock
242 * after the preparation is over.
244 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
245 if (!opts->keep_locked)
246 write_file(sb.buf, "initializing");
248 write_file(sb.buf, "added with --lock");
250 strbuf_addf(&sb_git, "%s/.git", path);
251 if (safe_create_leading_directories_const(sb_git.buf))
252 die_errno(_("could not create leading directories of '%s'"),
254 junk_work_tree = xstrdup(path);
257 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
258 write_file(sb.buf, "%s", real_path(sb_git.buf));
259 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
260 real_path(get_git_common_dir()), name);
262 * This is to keep resolve_ref() happy. We need a valid HEAD
263 * or is_git_directory() will reject the directory. Any value which
264 * looks like an object ID will do since it will be immediately
265 * replaced by the symbolic-ref or update-ref invocation in the new
269 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
270 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
272 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
273 write_file(sb.buf, "../..");
275 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
277 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
278 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
282 argv_array_pushl(&cp.args, "update-ref", "HEAD",
283 oid_to_hex(&commit->object.oid), NULL);
285 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
287 cp.env = child_env.argv;
288 ret = run_command(&cp);
292 if (opts->checkout) {
294 argv_array_clear(&cp.args);
295 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
296 cp.env = child_env.argv;
297 ret = run_command(&cp);
303 FREE_AND_NULL(junk_work_tree);
304 FREE_AND_NULL(junk_git_dir);
307 if (ret || !opts->keep_locked) {
309 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
310 unlink_or_warn(sb.buf);
312 argv_array_clear(&child_env);
314 strbuf_release(&symref);
315 strbuf_release(&sb_repo);
316 strbuf_release(&sb_git);
320 static int add(int ac, const char **av, const char *prefix)
322 struct add_opts opts;
323 const char *new_branch_force = NULL;
326 struct option options[] = {
327 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
328 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
329 N_("create a new branch")),
330 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
331 N_("create or reset a branch")),
332 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
333 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
334 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
338 memset(&opts, 0, sizeof(opts));
340 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
341 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
342 die(_("-b, -B, and --detach are mutually exclusive"));
343 if (ac < 1 || ac > 2)
344 usage_with_options(worktree_usage, options);
346 path = prefix_filename(prefix, av[0]);
347 branch = ac < 2 ? "HEAD" : av[1];
349 if (!strcmp(branch, "-"))
352 opts.force_new_branch = !!new_branch_force;
353 if (opts.force_new_branch) {
354 struct strbuf symref = STRBUF_INIT;
356 opts.new_branch = new_branch_force;
359 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
360 ref_exists(symref.buf))
361 die_if_checked_out(symref.buf, 0);
362 strbuf_release(&symref);
365 if (ac < 2 && !opts.new_branch && !opts.detach) {
367 const char *s = worktree_basename(path, &n);
368 opts.new_branch = xstrndup(s, n);
371 if (opts.new_branch) {
372 struct child_process cp = CHILD_PROCESS_INIT;
374 argv_array_push(&cp.args, "branch");
375 if (opts.force_new_branch)
376 argv_array_push(&cp.args, "--force");
377 argv_array_push(&cp.args, opts.new_branch);
378 argv_array_push(&cp.args, branch);
379 if (run_command(&cp))
381 branch = opts.new_branch;
384 return add_worktree(path, branch, &opts);
387 static void show_worktree_porcelain(struct worktree *wt)
389 printf("worktree %s\n", wt->path);
393 printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
395 printf("detached\n");
396 else if (wt->head_ref)
397 printf("branch %s\n", wt->head_ref);
402 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
404 struct strbuf sb = STRBUF_INIT;
405 int cur_path_len = strlen(wt->path);
406 int path_adj = cur_path_len - utf8_strwidth(wt->path);
408 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
410 strbuf_addstr(&sb, "(bare)");
412 strbuf_addf(&sb, "%-*s ", abbrev_len,
413 find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
415 strbuf_addstr(&sb, "(detached HEAD)");
416 else if (wt->head_ref) {
417 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
418 strbuf_addf(&sb, "[%s]", ref);
421 strbuf_addstr(&sb, "(error)");
423 printf("%s\n", sb.buf);
428 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
432 for (i = 0; wt[i]; i++) {
434 int path_len = strlen(wt[i]->path);
436 if (path_len > *maxlen)
438 sha1_len = strlen(find_unique_abbrev(wt[i]->head_sha1, *abbrev));
439 if (sha1_len > *abbrev)
444 static int list(int ac, const char **av, const char *prefix)
448 struct option options[] = {
449 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
453 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
455 usage_with_options(worktree_usage, options);
457 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
458 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
461 measure_widths(worktrees, &abbrev, &path_maxlen);
463 for (i = 0; worktrees[i]; i++) {
465 show_worktree_porcelain(worktrees[i]);
467 show_worktree(worktrees[i], path_maxlen, abbrev);
469 free_worktrees(worktrees);
474 static int lock_worktree(int ac, const char **av, const char *prefix)
476 const char *reason = "", *old_reason;
477 struct option options[] = {
478 OPT_STRING(0, "reason", &reason, N_("string"),
479 N_("reason for locking")),
482 struct worktree **worktrees, *wt;
484 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
486 usage_with_options(worktree_usage, options);
488 worktrees = get_worktrees(0);
489 wt = find_worktree(worktrees, prefix, av[0]);
491 die(_("'%s' is not a working tree"), av[0]);
492 if (is_main_worktree(wt))
493 die(_("The main working tree cannot be locked or unlocked"));
495 old_reason = is_worktree_locked(wt);
498 die(_("'%s' is already locked, reason: %s"),
500 die(_("'%s' is already locked"), av[0]);
503 write_file(git_common_path("worktrees/%s/locked", wt->id),
505 free_worktrees(worktrees);
509 static int unlock_worktree(int ac, const char **av, const char *prefix)
511 struct option options[] = {
514 struct worktree **worktrees, *wt;
517 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
519 usage_with_options(worktree_usage, options);
521 worktrees = get_worktrees(0);
522 wt = find_worktree(worktrees, prefix, av[0]);
524 die(_("'%s' is not a working tree"), av[0]);
525 if (is_main_worktree(wt))
526 die(_("The main working tree cannot be locked or unlocked"));
527 if (!is_worktree_locked(wt))
528 die(_("'%s' is not locked"), av[0]);
529 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
530 free_worktrees(worktrees);
534 int cmd_worktree(int ac, const char **av, const char *prefix)
536 struct option options[] = {
540 git_config(git_default_config, NULL);
543 usage_with_options(worktree_usage, options);
546 if (!strcmp(av[1], "add"))
547 return add(ac - 1, av + 1, prefix);
548 if (!strcmp(av[1], "prune"))
549 return prune(ac - 1, av + 1, prefix);
550 if (!strcmp(av[1], "list"))
551 return list(ac - 1, av + 1, prefix);
552 if (!strcmp(av[1], "lock"))
553 return lock_worktree(ac - 1, av + 1, prefix);
554 if (!strcmp(av[1], "unlock"))
555 return unlock_worktree(ac - 1, av + 1, prefix);
556 usage_with_options(worktree_usage, options);