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 prune [<options>]"),
17 N_("git worktree list [<options>]"),
24 const char *new_branch;
30 static unsigned long expire;
32 static int prune_worktree(const char *id, struct strbuf *reason)
38 if (!is_directory(git_path("worktrees/%s", id))) {
39 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
42 if (file_exists(git_path("worktrees/%s/locked", id)))
44 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
45 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
48 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
50 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
55 path = xmalloc(len + 1);
56 read_in_full(fd, path, len);
58 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
61 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
66 if (!file_exists(path)) {
70 * the repo is moved manually and has not been
73 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
76 if (st.st_mtime <= expire) {
77 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
87 static void prune_worktrees(void)
89 struct strbuf reason = STRBUF_INIT;
90 struct strbuf path = STRBUF_INIT;
91 DIR *dir = opendir(git_path("worktrees"));
96 while ((d = readdir(dir)) != NULL) {
97 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
99 strbuf_reset(&reason);
100 if (!prune_worktree(d->d_name, &reason))
102 if (show_only || verbose)
103 printf("%s\n", reason.buf);
107 strbuf_addstr(&path, git_path("worktrees/%s", d->d_name));
108 ret = remove_dir_recursively(&path, 0);
109 if (ret < 0 && errno == ENOTDIR)
110 ret = unlink(path.buf);
112 error(_("failed to remove: %s"), strerror(errno));
116 rmdir(git_path("worktrees"));
117 strbuf_release(&reason);
118 strbuf_release(&path);
121 static int prune(int ac, const char **av, const char *prefix)
123 struct option options[] = {
124 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
125 OPT__VERBOSE(&verbose, N_("report pruned objects")),
126 OPT_EXPIRY_DATE(0, "expire", &expire,
127 N_("expire objects older than <time>")),
132 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
134 usage_with_options(worktree_usage, options);
139 static char *junk_work_tree;
140 static char *junk_git_dir;
142 static pid_t junk_pid;
144 static void remove_junk(void)
146 struct strbuf sb = STRBUF_INIT;
147 if (!is_junk || getpid() != junk_pid)
150 strbuf_addstr(&sb, junk_git_dir);
151 remove_dir_recursively(&sb, 0);
154 if (junk_work_tree) {
155 strbuf_addstr(&sb, junk_work_tree);
156 remove_dir_recursively(&sb, 0);
161 static void remove_junk_on_signal(int signo)
168 static const char *worktree_basename(const char *path, int *olen)
174 while (len && is_dir_sep(path[len - 1]))
177 for (name = path + len - 1; name > path; name--)
178 if (is_dir_sep(*name)) {
187 static int add_worktree(const char *path, const char *refname,
188 const struct add_opts *opts)
190 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
191 struct strbuf sb = STRBUF_INIT;
194 struct child_process cp;
195 struct argv_array child_env = ARGV_ARRAY_INIT;
196 int counter = 0, len, ret;
197 struct strbuf symref = STRBUF_INIT;
198 struct commit *commit = NULL;
200 if (file_exists(path) && !is_empty_dir(path))
201 die(_("'%s' already exists"), path);
203 /* is 'refname' a branch or commit? */
204 if (opts->force_new_branch) /* definitely a branch */
206 else 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);
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 strbuf_addstr(&sb_repo,
218 git_path("worktrees/%.*s", (int)(path + len - name), name));
220 if (safe_create_leading_directories_const(sb_repo.buf))
221 die_errno(_("could not create leading directories of '%s'"),
223 while (!stat(sb_repo.buf, &st)) {
225 strbuf_setlen(&sb_repo, len);
226 strbuf_addf(&sb_repo, "%d", counter);
228 name = strrchr(sb_repo.buf, '/') + 1;
232 sigchain_push_common(remove_junk_on_signal);
234 if (mkdir(sb_repo.buf, 0777))
235 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
236 junk_git_dir = xstrdup(sb_repo.buf);
240 * lock the incomplete repo so prune won't delete it, unlock
241 * after the preparation is over.
243 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
244 write_file(sb.buf, "initializing");
246 strbuf_addf(&sb_git, "%s/.git", path);
247 if (safe_create_leading_directories_const(sb_git.buf))
248 die_errno(_("could not create leading directories of '%s'"),
250 junk_work_tree = xstrdup(path);
253 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
254 write_file(sb.buf, "%s", real_path(sb_git.buf));
255 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
256 real_path(get_git_common_dir()), name);
258 * This is to keep resolve_ref() happy. We need a valid HEAD
259 * or is_git_directory() will reject the directory. Any value which
260 * looks like an object ID will do since it will be immediately
261 * replaced by the symbolic-ref or update-ref invocation in the new
265 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
266 write_file(sb.buf, "0000000000000000000000000000000000000000");
268 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
269 write_file(sb.buf, "../..");
271 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
273 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
274 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
275 memset(&cp, 0, sizeof(cp));
279 argv_array_pushl(&cp.args, "update-ref", "HEAD",
280 oid_to_hex(&commit->object.oid), NULL);
282 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
284 cp.env = child_env.argv;
285 ret = run_command(&cp);
290 argv_array_clear(&cp.args);
291 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
292 cp.env = child_env.argv;
293 ret = run_command(&cp);
296 free(junk_work_tree);
298 junk_work_tree = NULL;
303 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
304 unlink_or_warn(sb.buf);
305 argv_array_clear(&child_env);
307 strbuf_release(&symref);
308 strbuf_release(&sb_repo);
309 strbuf_release(&sb_git);
313 static int add(int ac, const char **av, const char *prefix)
315 struct add_opts opts;
316 const char *new_branch_force = NULL;
317 const char *path, *branch;
318 struct option options[] = {
319 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
320 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
321 N_("create a new branch")),
322 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
323 N_("create or reset a branch")),
324 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
328 memset(&opts, 0, sizeof(opts));
329 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
330 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
331 die(_("-b, -B, and --detach are mutually exclusive"));
332 if (ac < 1 || ac > 2)
333 usage_with_options(worktree_usage, options);
335 path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
336 branch = ac < 2 ? "HEAD" : av[1];
338 opts.force_new_branch = !!new_branch_force;
339 if (opts.force_new_branch)
340 opts.new_branch = new_branch_force;
342 if (ac < 2 && !opts.new_branch && !opts.detach) {
344 const char *s = worktree_basename(path, &n);
345 opts.new_branch = xstrndup(s, n);
348 if (opts.new_branch) {
349 struct child_process cp;
350 memset(&cp, 0, sizeof(cp));
352 argv_array_push(&cp.args, "branch");
353 if (opts.force_new_branch)
354 argv_array_push(&cp.args, "--force");
355 argv_array_push(&cp.args, opts.new_branch);
356 argv_array_push(&cp.args, branch);
357 if (run_command(&cp))
359 branch = opts.new_branch;
362 return add_worktree(path, branch, &opts);
365 static void show_worktree_porcelain(struct worktree *wt)
367 printf("worktree %s\n", wt->path);
371 printf("HEAD %s\n", sha1_to_hex(wt->head_sha1));
373 printf("detached\n");
375 printf("branch %s\n", wt->head_ref);
380 static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
382 struct strbuf sb = STRBUF_INIT;
383 int cur_path_len = strlen(wt->path);
384 int path_adj = cur_path_len - utf8_strwidth(wt->path);
386 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
388 strbuf_addstr(&sb, "(bare)");
390 strbuf_addf(&sb, "%-*s ", abbrev_len,
391 find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
392 if (!wt->is_detached)
393 strbuf_addf(&sb, "[%s]", shorten_unambiguous_ref(wt->head_ref, 0));
395 strbuf_addstr(&sb, "(detached HEAD)");
397 printf("%s\n", sb.buf);
402 static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
406 for (i = 0; wt[i]; i++) {
408 int path_len = strlen(wt[i]->path);
410 if (path_len > *maxlen)
412 sha1_len = strlen(find_unique_abbrev(wt[i]->head_sha1, *abbrev));
413 if (sha1_len > *abbrev)
418 static int list(int ac, const char **av, const char *prefix)
422 struct option options[] = {
423 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
427 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
429 usage_with_options(worktree_usage, options);
431 struct worktree **worktrees = get_worktrees();
432 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
435 measure_widths(worktrees, &abbrev, &path_maxlen);
437 for (i = 0; worktrees[i]; i++) {
439 show_worktree_porcelain(worktrees[i]);
441 show_worktree(worktrees[i], path_maxlen, abbrev);
443 free_worktrees(worktrees);
448 int cmd_worktree(int ac, const char **av, const char *prefix)
450 struct option options[] = {
455 usage_with_options(worktree_usage, options);
456 if (!strcmp(av[1], "add"))
457 return add(ac - 1, av + 1, prefix);
458 if (!strcmp(av[1], "prune"))
459 return prune(ac - 1, av + 1, prefix);
460 if (!strcmp(av[1], "list"))
461 return list(ac - 1, av + 1, prefix);
462 usage_with_options(worktree_usage, options);