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 prune [<options>]"),
19 N_("git worktree unlock <path>"),
27 const char *new_branch;
33 static unsigned long expire;
35 static int prune_worktree(const char *id, struct strbuf *reason)
41 if (!is_directory(git_path("worktrees/%s", id))) {
42 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
45 if (file_exists(git_path("worktrees/%s/locked", id)))
47 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
48 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
51 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
53 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
59 read_in_full(fd, path, len);
61 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
64 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
69 if (!file_exists(path)) {
73 * the repo is moved manually and has not been
76 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
79 if (st.st_mtime <= expire) {
80 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
90 static void prune_worktrees(void)
92 struct strbuf reason = STRBUF_INIT;
93 struct strbuf path = STRBUF_INIT;
94 DIR *dir = opendir(git_path("worktrees"));
99 while ((d = readdir(dir)) != NULL) {
100 if (is_dot_or_dotdot(d->d_name))
102 strbuf_reset(&reason);
103 if (!prune_worktree(d->d_name, &reason))
105 if (show_only || verbose)
106 printf("%s\n", reason.buf);
109 git_path_buf(&path, "worktrees/%s", d->d_name);
110 ret = remove_dir_recursively(&path, 0);
111 if (ret < 0 && errno == ENOTDIR)
112 ret = unlink(path.buf);
114 error_errno(_("failed to remove '%s'"), path.buf);
118 rmdir(git_path("worktrees"));
119 strbuf_release(&reason);
120 strbuf_release(&path);
123 static int prune(int ac, const char **av, const char *prefix)
125 struct option options[] = {
126 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
127 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
128 OPT_EXPIRY_DATE(0, "expire", &expire,
129 N_("expire working trees older than <time>")),
134 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
136 usage_with_options(worktree_usage, options);
141 static char *junk_work_tree;
142 static char *junk_git_dir;
144 static pid_t junk_pid;
146 static void remove_junk(void)
148 struct strbuf sb = STRBUF_INIT;
149 if (!is_junk || getpid() != junk_pid)
152 strbuf_addstr(&sb, junk_git_dir);
153 remove_dir_recursively(&sb, 0);
156 if (junk_work_tree) {
157 strbuf_addstr(&sb, junk_work_tree);
158 remove_dir_recursively(&sb, 0);
163 static void remove_junk_on_signal(int signo)
170 static const char *worktree_basename(const char *path, int *olen)
176 while (len && is_dir_sep(path[len - 1]))
179 for (name = path + len - 1; name > path; name--)
180 if (is_dir_sep(*name)) {
189 static int add_worktree(const char *path, const char *refname,
190 const struct add_opts *opts)
192 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
193 struct strbuf sb = STRBUF_INIT;
196 struct child_process cp = CHILD_PROCESS_INIT;
197 struct argv_array child_env = ARGV_ARRAY_INIT;
198 int counter = 0, len, ret;
199 struct strbuf symref = STRBUF_INIT;
200 struct commit *commit = NULL;
202 if (file_exists(path) && !is_empty_dir(path))
203 die(_("'%s' already exists"), path);
205 /* is 'refname' a branch or commit? */
206 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
207 ref_exists(symref.buf)) { /* it's a branch */
209 die_if_checked_out(symref.buf, 0);
210 } else { /* must be a commit */
211 commit = lookup_commit_reference_by_name(refname);
213 die(_("invalid reference: %s"), refname);
216 name = worktree_basename(path, &len);
217 git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name);
219 if (safe_create_leading_directories_const(sb_repo.buf))
220 die_errno(_("could not create leading directories of '%s'"),
222 while (!stat(sb_repo.buf, &st)) {
224 strbuf_setlen(&sb_repo, len);
225 strbuf_addf(&sb_repo, "%d", counter);
227 name = strrchr(sb_repo.buf, '/') + 1;
231 sigchain_push_common(remove_junk_on_signal);
233 if (mkdir(sb_repo.buf, 0777))
234 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
235 junk_git_dir = xstrdup(sb_repo.buf);
239 * lock the incomplete repo so prune won't delete it, unlock
240 * after the preparation is over.
242 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
243 write_file(sb.buf, "initializing");
245 strbuf_addf(&sb_git, "%s/.git", path);
246 if (safe_create_leading_directories_const(sb_git.buf))
247 die_errno(_("could not create leading directories of '%s'"),
249 junk_work_tree = xstrdup(path);
252 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
253 write_file(sb.buf, "%s", real_path(sb_git.buf));
254 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
255 real_path(get_git_common_dir()), name);
257 * This is to keep resolve_ref() happy. We need a valid HEAD
258 * or is_git_directory() will reject the directory. Any value which
259 * looks like an object ID will do since it will be immediately
260 * replaced by the symbolic-ref or update-ref invocation in the new
264 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
265 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
267 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
268 write_file(sb.buf, "../..");
270 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
272 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
273 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
277 argv_array_pushl(&cp.args, "update-ref", "HEAD",
278 oid_to_hex(&commit->object.oid), NULL);
280 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
282 cp.env = child_env.argv;
283 ret = run_command(&cp);
287 if (opts->checkout) {
289 argv_array_clear(&cp.args);
290 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
291 cp.env = child_env.argv;
292 ret = run_command(&cp);
298 free(junk_work_tree);
300 junk_work_tree = NULL;
305 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
306 unlink_or_warn(sb.buf);
307 argv_array_clear(&child_env);
309 strbuf_release(&symref);
310 strbuf_release(&sb_repo);
311 strbuf_release(&sb_git);
315 static int add(int ac, const char **av, const char *prefix)
317 struct add_opts opts;
318 const char *new_branch_force = NULL;
321 struct option options[] = {
322 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
323 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
324 N_("create a new branch")),
325 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
326 N_("create or reset a branch")),
327 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
328 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
332 memset(&opts, 0, sizeof(opts));
334 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
335 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
336 die(_("-b, -B, and --detach are mutually exclusive"));
337 if (ac < 1 || ac > 2)
338 usage_with_options(worktree_usage, options);
340 path = prefix_filename(prefix, av[0]);
341 branch = ac < 2 ? "HEAD" : av[1];
343 if (!strcmp(branch, "-"))
346 opts.force_new_branch = !!new_branch_force;
347 if (opts.force_new_branch) {
348 struct strbuf symref = STRBUF_INIT;
350 opts.new_branch = new_branch_force;
353 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
354 ref_exists(symref.buf))
355 die_if_checked_out(symref.buf, 0);
356 strbuf_release(&symref);
359 if (ac < 2 && !opts.new_branch && !opts.detach) {
361 const char *s = worktree_basename(path, &n);
362 opts.new_branch = xstrndup(s, n);
365 if (opts.new_branch) {
366 struct child_process cp = CHILD_PROCESS_INIT;
368 argv_array_push(&cp.args, "branch");
369 if (opts.force_new_branch)
370 argv_array_push(&cp.args, "--force");
371 argv_array_push(&cp.args, opts.new_branch);
372 argv_array_push(&cp.args, branch);
373 if (run_command(&cp))
375 branch = opts.new_branch;
378 return add_worktree(path, branch, &opts);
381 static void show_worktree_porcelain(struct worktree *wt)
383 printf("worktree %s\n", wt->path);
387 printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
389 printf("detached\n");
390 else if (wt->head_ref)
391 printf("branch %s\n", wt->head_ref);
396 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
398 struct strbuf sb = STRBUF_INIT;
399 int cur_path_len = strlen(wt->path);
400 int path_adj = cur_path_len - utf8_strwidth(wt->path);
402 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
404 strbuf_addstr(&sb, "(bare)");
406 strbuf_addf(&sb, "%-*s ", abbrev_len,
407 find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
409 strbuf_addstr(&sb, "(detached HEAD)");
410 else if (wt->head_ref)
411 strbuf_addf(&sb, "[%s]", shorten_unambiguous_ref(wt->head_ref, 0));
413 strbuf_addstr(&sb, "(error)");
415 printf("%s\n", sb.buf);
420 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
424 for (i = 0; wt[i]; i++) {
426 int path_len = strlen(wt[i]->path);
428 if (path_len > *maxlen)
430 sha1_len = strlen(find_unique_abbrev(wt[i]->head_sha1, *abbrev));
431 if (sha1_len > *abbrev)
436 static int list(int ac, const char **av, const char *prefix)
440 struct option options[] = {
441 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
445 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
447 usage_with_options(worktree_usage, options);
449 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
450 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
453 measure_widths(worktrees, &abbrev, &path_maxlen);
455 for (i = 0; worktrees[i]; i++) {
457 show_worktree_porcelain(worktrees[i]);
459 show_worktree(worktrees[i], path_maxlen, abbrev);
461 free_worktrees(worktrees);
466 static int lock_worktree(int ac, const char **av, const char *prefix)
468 const char *reason = "", *old_reason;
469 struct option options[] = {
470 OPT_STRING(0, "reason", &reason, N_("string"),
471 N_("reason for locking")),
474 struct worktree **worktrees, *wt;
476 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
478 usage_with_options(worktree_usage, options);
480 worktrees = get_worktrees(0);
481 wt = find_worktree(worktrees, prefix, av[0]);
483 die(_("'%s' is not a working tree"), av[0]);
484 if (is_main_worktree(wt))
485 die(_("The main working tree cannot be locked or unlocked"));
487 old_reason = is_worktree_locked(wt);
490 die(_("'%s' is already locked, reason: %s"),
492 die(_("'%s' is already locked"), av[0]);
495 write_file(git_common_path("worktrees/%s/locked", wt->id),
497 free_worktrees(worktrees);
501 static int unlock_worktree(int ac, const char **av, const char *prefix)
503 struct option options[] = {
506 struct worktree **worktrees, *wt;
509 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
511 usage_with_options(worktree_usage, options);
513 worktrees = get_worktrees(0);
514 wt = find_worktree(worktrees, prefix, av[0]);
516 die(_("'%s' is not a working tree"), av[0]);
517 if (is_main_worktree(wt))
518 die(_("The main working tree cannot be locked or unlocked"));
519 if (!is_worktree_locked(wt))
520 die(_("'%s' is not locked"), av[0]);
521 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
522 free_worktrees(worktrees);
526 int cmd_worktree(int ac, const char **av, const char *prefix)
528 struct option options[] = {
532 git_config(git_default_config, NULL);
535 usage_with_options(worktree_usage, options);
538 if (!strcmp(av[1], "add"))
539 return add(ac - 1, av + 1, prefix);
540 if (!strcmp(av[1], "prune"))
541 return prune(ac - 1, av + 1, prefix);
542 if (!strcmp(av[1], "list"))
543 return list(ac - 1, av + 1, prefix);
544 if (!strcmp(av[1], "lock"))
545 return lock_worktree(ac - 1, av + 1, prefix);
546 if (!strcmp(av[1], "unlock"))
547 return unlock_worktree(ac - 1, av + 1, prefix);
548 usage_with_options(worktree_usage, options);