4 #include "parse-options.h"
5 #include "argv-array.h"
6 #include "run-command.h"
9 static const char * const worktree_usage
[] = {
10 N_("git worktree add [<options>] <path> <branch>"),
11 N_("git worktree prune [<options>]"),
18 const char *new_branch
;
24 static unsigned long expire
;
26 static int prune_worktree(const char *id
, struct strbuf
*reason
)
32 if (!is_directory(git_path("worktrees/%s", id
))) {
33 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
36 if (file_exists(git_path("worktrees/%s/locked", id
)))
38 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
39 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
42 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
44 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
49 path
= xmalloc(len
+ 1);
50 read_in_full(fd
, path
, len
);
52 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
55 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
60 if (!file_exists(path
)) {
64 * the repo is moved manually and has not been
67 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
70 if (st
.st_mtime
<= expire
) {
71 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
81 static void prune_worktrees(void)
83 struct strbuf reason
= STRBUF_INIT
;
84 struct strbuf path
= STRBUF_INIT
;
85 DIR *dir
= opendir(git_path("worktrees"));
90 while ((d
= readdir(dir
)) != NULL
) {
91 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
93 strbuf_reset(&reason
);
94 if (!prune_worktree(d
->d_name
, &reason
))
96 if (show_only
|| verbose
)
97 printf("%s\n", reason
.buf
);
101 strbuf_addstr(&path
, git_path("worktrees/%s", d
->d_name
));
102 ret
= remove_dir_recursively(&path
, 0);
103 if (ret
< 0 && errno
== ENOTDIR
)
104 ret
= unlink(path
.buf
);
106 error(_("failed to remove: %s"), strerror(errno
));
110 rmdir(git_path("worktrees"));
111 strbuf_release(&reason
);
112 strbuf_release(&path
);
115 static int prune(int ac
, const char **av
, const char *prefix
)
117 struct option options
[] = {
118 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
119 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
120 OPT_EXPIRY_DATE(0, "expire", &expire
,
121 N_("expire objects older than <time>")),
126 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
128 usage_with_options(worktree_usage
, options
);
133 static char *junk_work_tree
;
134 static char *junk_git_dir
;
136 static pid_t junk_pid
;
138 static void remove_junk(void)
140 struct strbuf sb
= STRBUF_INIT
;
141 if (!is_junk
|| getpid() != junk_pid
)
144 strbuf_addstr(&sb
, junk_git_dir
);
145 remove_dir_recursively(&sb
, 0);
148 if (junk_work_tree
) {
149 strbuf_addstr(&sb
, junk_work_tree
);
150 remove_dir_recursively(&sb
, 0);
155 static void remove_junk_on_signal(int signo
)
162 static const char *worktree_basename(const char *path
, int *olen
)
168 while (len
&& is_dir_sep(path
[len
- 1]))
171 for (name
= path
+ len
- 1; name
> path
; name
--)
172 if (is_dir_sep(*name
)) {
181 static int add_worktree(const char *path
, const char **child_argv
,
182 const struct add_opts
*opts
)
184 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
185 struct strbuf sb
= STRBUF_INIT
;
188 struct child_process cp
;
189 int counter
= 0, len
, ret
;
190 unsigned char rev
[20];
192 if (file_exists(path
) && !is_empty_dir(path
))
193 die(_("'%s' already exists"), path
);
195 name
= worktree_basename(path
, &len
);
196 strbuf_addstr(&sb_repo
,
197 git_path("worktrees/%.*s", (int)(path
+ len
- name
), name
));
199 if (safe_create_leading_directories_const(sb_repo
.buf
))
200 die_errno(_("could not create leading directories of '%s'"),
202 while (!stat(sb_repo
.buf
, &st
)) {
204 strbuf_setlen(&sb_repo
, len
);
205 strbuf_addf(&sb_repo
, "%d", counter
);
207 name
= strrchr(sb_repo
.buf
, '/') + 1;
211 sigchain_push_common(remove_junk_on_signal
);
213 if (mkdir(sb_repo
.buf
, 0777))
214 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
215 junk_git_dir
= xstrdup(sb_repo
.buf
);
219 * lock the incomplete repo so prune won't delete it, unlock
220 * after the preparation is over.
222 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
223 write_file(sb
.buf
, 1, "initializing\n");
225 strbuf_addf(&sb_git
, "%s/.git", path
);
226 if (safe_create_leading_directories_const(sb_git
.buf
))
227 die_errno(_("could not create leading directories of '%s'"),
229 junk_work_tree
= xstrdup(path
);
232 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
233 write_file(sb
.buf
, 1, "%s\n", real_path(sb_git
.buf
));
234 write_file(sb_git
.buf
, 1, "gitdir: %s/worktrees/%s\n",
235 real_path(get_git_common_dir()), name
);
237 * This is to keep resolve_ref() happy. We need a valid HEAD
238 * or is_git_directory() will reject the directory. Moreover, HEAD
239 * in the new worktree must resolve to the same value as HEAD in
240 * the current tree since the command invoked to populate the new
241 * worktree will be handed the branch/ref specified by the user.
242 * For instance, if the user asks for the new worktree to be based
243 * at HEAD~5, then the resolved HEAD~5 in the new worktree must
244 * match the resolved HEAD~5 in the current tree in order to match
245 * the user's expectation.
247 if (!resolve_ref_unsafe("HEAD", 0, rev
, NULL
))
248 die(_("unable to resolve HEAD"));
250 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
251 write_file(sb
.buf
, 1, "%s\n", sha1_to_hex(rev
));
253 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
254 write_file(sb
.buf
, 1, "../..\n");
256 fprintf_ln(stderr
, _("Preparing %s (identifier %s)"), path
, name
);
258 setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
259 setenv(GIT_DIR_ENVIRONMENT
, sb_git
.buf
, 1);
260 setenv(GIT_WORK_TREE_ENVIRONMENT
, path
, 1);
261 memset(&cp
, 0, sizeof(cp
));
263 cp
.argv
= child_argv
;
264 ret
= run_command(&cp
);
267 free(junk_work_tree
);
269 junk_work_tree
= NULL
;
273 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
274 unlink_or_warn(sb
.buf
);
276 strbuf_release(&sb_repo
);
277 strbuf_release(&sb_git
);
281 static int add(int ac
, const char **av
, const char *prefix
)
283 struct add_opts opts
;
284 const char *new_branch_force
= NULL
;
285 const char *path
, *branch
;
286 struct argv_array cmd
= ARGV_ARRAY_INIT
;
287 struct option options
[] = {
288 OPT__FORCE(&opts
.force
, N_("checkout <branch> even if already checked out in other worktree")),
289 OPT_STRING('b', NULL
, &opts
.new_branch
, N_("branch"),
290 N_("create a new branch")),
291 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
292 N_("create or reset a branch")),
293 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
297 memset(&opts
, 0, sizeof(opts
));
298 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
299 if (!!opts
.detach
+ !!opts
.new_branch
+ !!new_branch_force
> 1)
300 die(_("-b, -B, and --detach are mutually exclusive"));
301 if (ac
< 1 || ac
> 2)
302 usage_with_options(worktree_usage
, options
);
304 path
= prefix
? prefix_filename(prefix
, strlen(prefix
), av
[0]) : av
[0];
305 branch
= ac
< 2 ? "HEAD" : av
[1];
307 opts
.force_new_branch
= !!new_branch_force
;
308 if (opts
.force_new_branch
)
309 opts
.new_branch
= new_branch_force
;
311 if (ac
< 2 && !opts
.new_branch
) {
313 const char *s
= worktree_basename(path
, &n
);
314 opts
.new_branch
= xstrndup(s
, n
);
317 argv_array_push(&cmd
, "checkout");
319 argv_array_push(&cmd
, "--ignore-other-worktrees");
321 argv_array_pushl(&cmd
, opts
.force_new_branch
? "-B" : "-b",
322 opts
.new_branch
, NULL
);
324 argv_array_push(&cmd
, "--detach");
325 argv_array_push(&cmd
, branch
);
327 return add_worktree(path
, cmd
.argv
, &opts
);
330 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
332 struct option options
[] = {
337 usage_with_options(worktree_usage
, options
);
338 if (!strcmp(av
[1], "add"))
339 return add(ac
- 1, av
+ 1, prefix
);
340 if (!strcmp(av
[1], "prune"))
341 return prune(ac
- 1, av
+ 1, prefix
);
342 usage_with_options(worktree_usage
, options
);