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)"),
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
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
205 ref_exists(symref
.buf
)) { /* it's a branch */
207 die_if_checked_out(symref
.buf
);
208 } else { /* must be a commit */
209 commit
= lookup_commit_reference_by_name(refname
);
211 die(_("invalid reference: %s"), refname
);
214 name
= worktree_basename(path
, &len
);
215 strbuf_addstr(&sb_repo
,
216 git_path("worktrees/%.*s", (int)(path
+ len
- name
), name
));
218 if (safe_create_leading_directories_const(sb_repo
.buf
))
219 die_errno(_("could not create leading directories of '%s'"),
221 while (!stat(sb_repo
.buf
, &st
)) {
223 strbuf_setlen(&sb_repo
, len
);
224 strbuf_addf(&sb_repo
, "%d", counter
);
226 name
= strrchr(sb_repo
.buf
, '/') + 1;
230 sigchain_push_common(remove_junk_on_signal
);
232 if (mkdir(sb_repo
.buf
, 0777))
233 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
234 junk_git_dir
= xstrdup(sb_repo
.buf
);
238 * lock the incomplete repo so prune won't delete it, unlock
239 * after the preparation is over.
241 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
242 write_file(sb
.buf
, "initializing");
244 strbuf_addf(&sb_git
, "%s/.git", path
);
245 if (safe_create_leading_directories_const(sb_git
.buf
))
246 die_errno(_("could not create leading directories of '%s'"),
248 junk_work_tree
= xstrdup(path
);
251 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
252 write_file(sb
.buf
, "%s", real_path(sb_git
.buf
));
253 write_file(sb_git
.buf
, "gitdir: %s/worktrees/%s",
254 real_path(get_git_common_dir()), name
);
256 * This is to keep resolve_ref() happy. We need a valid HEAD
257 * or is_git_directory() will reject the directory. Any value which
258 * looks like an object ID will do since it will be immediately
259 * replaced by the symbolic-ref or update-ref invocation in the new
263 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
264 write_file(sb
.buf
, "0000000000000000000000000000000000000000");
266 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
267 write_file(sb
.buf
, "../..");
269 fprintf_ln(stderr
, _("Preparing %s (identifier %s)"), path
, name
);
271 argv_array_pushf(&child_env
, "%s=%s", GIT_DIR_ENVIRONMENT
, sb_git
.buf
);
272 argv_array_pushf(&child_env
, "%s=%s", GIT_WORK_TREE_ENVIRONMENT
, path
);
273 memset(&cp
, 0, sizeof(cp
));
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
);
288 argv_array_clear(&cp
.args
);
289 argv_array_pushl(&cp
.args
, "reset", "--hard", NULL
);
290 cp
.env
= child_env
.argv
;
291 ret
= run_command(&cp
);
294 free(junk_work_tree
);
296 junk_work_tree
= NULL
;
301 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
302 unlink_or_warn(sb
.buf
);
303 argv_array_clear(&child_env
);
305 strbuf_release(&symref
);
306 strbuf_release(&sb_repo
);
307 strbuf_release(&sb_git
);
311 static int add(int ac
, const char **av
, const char *prefix
)
313 struct add_opts opts
;
314 const char *new_branch_force
= NULL
;
315 const char *path
, *branch
;
316 struct option options
[] = {
317 OPT__FORCE(&opts
.force
, N_("checkout <branch> even if already checked out in other worktree")),
318 OPT_STRING('b', NULL
, &opts
.new_branch
, N_("branch"),
319 N_("create a new branch")),
320 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
321 N_("create or reset a branch")),
322 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
326 memset(&opts
, 0, sizeof(opts
));
327 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
328 if (!!opts
.detach
+ !!opts
.new_branch
+ !!new_branch_force
> 1)
329 die(_("-b, -B, and --detach are mutually exclusive"));
330 if (ac
< 1 || ac
> 2)
331 usage_with_options(worktree_usage
, options
);
333 path
= prefix
? prefix_filename(prefix
, strlen(prefix
), av
[0]) : av
[0];
334 branch
= ac
< 2 ? "HEAD" : av
[1];
336 opts
.force_new_branch
= !!new_branch_force
;
337 if (opts
.force_new_branch
) {
338 struct strbuf symref
= STRBUF_INIT
;
340 opts
.new_branch
= new_branch_force
;
343 !strbuf_check_branch_ref(&symref
, opts
.new_branch
) &&
344 ref_exists(symref
.buf
))
345 die_if_checked_out(symref
.buf
);
346 strbuf_release(&symref
);
349 if (ac
< 2 && !opts
.new_branch
&& !opts
.detach
) {
351 const char *s
= worktree_basename(path
, &n
);
352 opts
.new_branch
= xstrndup(s
, n
);
355 if (opts
.new_branch
) {
356 struct child_process cp
;
357 memset(&cp
, 0, sizeof(cp
));
359 argv_array_push(&cp
.args
, "branch");
360 if (opts
.force_new_branch
)
361 argv_array_push(&cp
.args
, "--force");
362 argv_array_push(&cp
.args
, opts
.new_branch
);
363 argv_array_push(&cp
.args
, branch
);
364 if (run_command(&cp
))
366 branch
= opts
.new_branch
;
369 return add_worktree(path
, branch
, &opts
);
372 static void show_worktree_porcelain(struct worktree
*wt
)
374 printf("worktree %s\n", wt
->path
);
378 printf("HEAD %s\n", sha1_to_hex(wt
->head_sha1
));
380 printf("detached\n");
382 printf("branch %s\n", wt
->head_ref
);
387 static void show_worktree(struct worktree
*wt
, int path_maxlen
, int abbrev_len
)
389 struct strbuf sb
= STRBUF_INIT
;
390 int cur_path_len
= strlen(wt
->path
);
391 int path_adj
= cur_path_len
- utf8_strwidth(wt
->path
);
393 strbuf_addf(&sb
, "%-*s ", 1 + path_maxlen
+ path_adj
, wt
->path
);
395 strbuf_addstr(&sb
, "(bare)");
397 strbuf_addf(&sb
, "%-*s ", abbrev_len
,
398 find_unique_abbrev(wt
->head_sha1
, DEFAULT_ABBREV
));
399 if (!wt
->is_detached
)
400 strbuf_addf(&sb
, "[%s]", shorten_unambiguous_ref(wt
->head_ref
, 0));
402 strbuf_addstr(&sb
, "(detached HEAD)");
404 printf("%s\n", sb
.buf
);
409 static void measure_widths(struct worktree
**wt
, int *abbrev
, int *maxlen
)
413 for (i
= 0; wt
[i
]; i
++) {
415 int path_len
= strlen(wt
[i
]->path
);
417 if (path_len
> *maxlen
)
419 sha1_len
= strlen(find_unique_abbrev(wt
[i
]->head_sha1
, *abbrev
));
420 if (sha1_len
> *abbrev
)
425 static int list(int ac
, const char **av
, const char *prefix
)
429 struct option options
[] = {
430 OPT_BOOL(0, "porcelain", &porcelain
, N_("machine-readable output")),
434 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
436 usage_with_options(worktree_usage
, options
);
438 struct worktree
**worktrees
= get_worktrees();
439 int path_maxlen
= 0, abbrev
= DEFAULT_ABBREV
, i
;
442 measure_widths(worktrees
, &abbrev
, &path_maxlen
);
444 for (i
= 0; worktrees
[i
]; i
++) {
446 show_worktree_porcelain(worktrees
[i
]);
448 show_worktree(worktrees
[i
], path_maxlen
, abbrev
);
450 free_worktrees(worktrees
);
455 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
457 struct option options
[] = {
462 usage_with_options(worktree_usage
, options
);
463 if (!strcmp(av
[1], "add"))
464 return add(ac
- 1, av
+ 1, prefix
);
465 if (!strcmp(av
[1], "prune"))
466 return prune(ac
- 1, av
+ 1, prefix
);
467 if (!strcmp(av
[1], "list"))
468 return list(ac
- 1, av
+ 1, prefix
);
469 usage_with_options(worktree_usage
, options
);