4 #include "parse-options.h"
5 #include "argv-array.h"
8 #include "run-command.h"
11 static const char * const worktree_usage
[] = {
12 N_("git worktree add [<options>] <path> <branch>"),
13 N_("git worktree prune [<options>]"),
20 const char *new_branch
;
26 static unsigned long expire
;
28 static int prune_worktree(const char *id
, struct strbuf
*reason
)
34 if (!is_directory(git_path("worktrees/%s", id
))) {
35 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
38 if (file_exists(git_path("worktrees/%s/locked", id
)))
40 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
41 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
44 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
46 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
51 path
= xmalloc(len
+ 1);
52 read_in_full(fd
, path
, len
);
54 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
57 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
62 if (!file_exists(path
)) {
66 * the repo is moved manually and has not been
69 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
72 if (st
.st_mtime
<= expire
) {
73 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
83 static void prune_worktrees(void)
85 struct strbuf reason
= STRBUF_INIT
;
86 struct strbuf path
= STRBUF_INIT
;
87 DIR *dir
= opendir(git_path("worktrees"));
92 while ((d
= readdir(dir
)) != NULL
) {
93 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
95 strbuf_reset(&reason
);
96 if (!prune_worktree(d
->d_name
, &reason
))
98 if (show_only
|| verbose
)
99 printf("%s\n", reason
.buf
);
103 strbuf_addstr(&path
, git_path("worktrees/%s", d
->d_name
));
104 ret
= remove_dir_recursively(&path
, 0);
105 if (ret
< 0 && errno
== ENOTDIR
)
106 ret
= unlink(path
.buf
);
108 error(_("failed to remove: %s"), strerror(errno
));
112 rmdir(git_path("worktrees"));
113 strbuf_release(&reason
);
114 strbuf_release(&path
);
117 static int prune(int ac
, const char **av
, const char *prefix
)
119 struct option options
[] = {
120 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
121 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
122 OPT_EXPIRY_DATE(0, "expire", &expire
,
123 N_("expire objects older than <time>")),
128 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
130 usage_with_options(worktree_usage
, options
);
135 static char *junk_work_tree
;
136 static char *junk_git_dir
;
138 static pid_t junk_pid
;
140 static void remove_junk(void)
142 struct strbuf sb
= STRBUF_INIT
;
143 if (!is_junk
|| getpid() != junk_pid
)
146 strbuf_addstr(&sb
, junk_git_dir
);
147 remove_dir_recursively(&sb
, 0);
150 if (junk_work_tree
) {
151 strbuf_addstr(&sb
, junk_work_tree
);
152 remove_dir_recursively(&sb
, 0);
157 static void remove_junk_on_signal(int signo
)
164 static const char *worktree_basename(const char *path
, int *olen
)
170 while (len
&& is_dir_sep(path
[len
- 1]))
173 for (name
= path
+ len
- 1; name
> path
; name
--)
174 if (is_dir_sep(*name
)) {
183 static int add_worktree(const char *path
, const char *refname
,
184 const struct add_opts
*opts
)
186 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
187 struct strbuf sb
= STRBUF_INIT
;
190 struct child_process cp
;
191 struct argv_array child_env
= ARGV_ARRAY_INIT
;
192 int counter
= 0, len
, ret
;
193 struct strbuf symref
= STRBUF_INIT
;
194 struct commit
*commit
= NULL
;
195 unsigned char rev
[20];
197 if (file_exists(path
) && !is_empty_dir(path
))
198 die(_("'%s' already exists"), path
);
200 /* is 'refname' a branch or commit? */
201 if (opts
->force_new_branch
) /* definitely a branch */
203 else if (!opts
->detach
&& !strbuf_check_branch_ref(&symref
, refname
) &&
204 ref_exists(symref
.buf
)) { /* it's a branch */
206 die_if_checked_out(symref
.buf
);
207 } else { /* must be a commit */
208 commit
= lookup_commit_reference_by_name(refname
);
210 die(_("invalid reference: %s"), refname
);
213 name
= worktree_basename(path
, &len
);
214 strbuf_addstr(&sb_repo
,
215 git_path("worktrees/%.*s", (int)(path
+ len
- name
), name
));
217 if (safe_create_leading_directories_const(sb_repo
.buf
))
218 die_errno(_("could not create leading directories of '%s'"),
220 while (!stat(sb_repo
.buf
, &st
)) {
222 strbuf_setlen(&sb_repo
, len
);
223 strbuf_addf(&sb_repo
, "%d", counter
);
225 name
= strrchr(sb_repo
.buf
, '/') + 1;
229 sigchain_push_common(remove_junk_on_signal
);
231 if (mkdir(sb_repo
.buf
, 0777))
232 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
233 junk_git_dir
= xstrdup(sb_repo
.buf
);
237 * lock the incomplete repo so prune won't delete it, unlock
238 * after the preparation is over.
240 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
241 write_file(sb
.buf
, 1, "initializing\n");
243 strbuf_addf(&sb_git
, "%s/.git", path
);
244 if (safe_create_leading_directories_const(sb_git
.buf
))
245 die_errno(_("could not create leading directories of '%s'"),
247 junk_work_tree
= xstrdup(path
);
250 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
251 write_file(sb
.buf
, 1, "%s\n", real_path(sb_git
.buf
));
252 write_file(sb_git
.buf
, 1, "gitdir: %s/worktrees/%s\n",
253 real_path(get_git_common_dir()), name
);
255 * This is to keep resolve_ref() happy. We need a valid HEAD
256 * or is_git_directory() will reject the directory. Moreover, HEAD
257 * in the new worktree must resolve to the same value as HEAD in
258 * the current tree since the command invoked to populate the new
259 * worktree will be handed the branch/ref specified by the user.
260 * For instance, if the user asks for the new worktree to be based
261 * at HEAD~5, then the resolved HEAD~5 in the new worktree must
262 * match the resolved HEAD~5 in the current tree in order to match
263 * the user's expectation.
265 if (!resolve_ref_unsafe("HEAD", 0, rev
, NULL
))
266 die(_("unable to resolve HEAD"));
268 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
269 write_file(sb
.buf
, 1, "%s\n", sha1_to_hex(rev
));
271 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
272 write_file(sb
.buf
, 1, "../..\n");
274 fprintf_ln(stderr
, _("Preparing %s (identifier %s)"), path
, name
);
276 setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
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
);
279 memset(&cp
, 0, sizeof(cp
));
283 argv_array_pushl(&cp
.args
, "update-ref", "HEAD",
284 sha1_to_hex(commit
->object
.sha1
), NULL
);
286 argv_array_pushl(&cp
.args
, "symbolic-ref", "HEAD",
288 cp
.env
= child_env
.argv
;
289 ret
= run_command(&cp
);
294 argv_array_clear(&cp
.args
);
295 argv_array_push(&cp
.args
, "checkout");
296 cp
.env
= child_env
.argv
;
297 ret
= run_command(&cp
);
300 free(junk_work_tree
);
302 junk_work_tree
= NULL
;
307 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
308 unlink_or_warn(sb
.buf
);
309 argv_array_clear(&child_env
);
311 strbuf_release(&symref
);
312 strbuf_release(&sb_repo
);
313 strbuf_release(&sb_git
);
317 static int add(int ac
, const char **av
, const char *prefix
)
319 struct add_opts opts
;
320 const char *new_branch_force
= NULL
;
321 const char *path
, *branch
;
322 struct option options
[] = {
323 OPT__FORCE(&opts
.force
, N_("checkout <branch> even if already checked out in other worktree")),
324 OPT_STRING('b', NULL
, &opts
.new_branch
, N_("branch"),
325 N_("create a new branch")),
326 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
327 N_("create or reset a branch")),
328 OPT_BOOL(0, "detach", &opts
.detach
, N_("detach HEAD at named commit")),
332 memset(&opts
, 0, sizeof(opts
));
333 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
334 if (!!opts
.detach
+ !!opts
.new_branch
+ !!new_branch_force
> 1)
335 die(_("-b, -B, and --detach are mutually exclusive"));
336 if (ac
< 1 || ac
> 2)
337 usage_with_options(worktree_usage
, options
);
339 path
= prefix
? prefix_filename(prefix
, strlen(prefix
), av
[0]) : av
[0];
340 branch
= ac
< 2 ? "HEAD" : av
[1];
342 opts
.force_new_branch
= !!new_branch_force
;
343 if (opts
.force_new_branch
)
344 opts
.new_branch
= new_branch_force
;
346 if (ac
< 2 && !opts
.new_branch
&& !opts
.detach
) {
348 const char *s
= worktree_basename(path
, &n
);
349 opts
.new_branch
= xstrndup(s
, n
);
352 if (opts
.new_branch
) {
353 struct child_process cp
;
354 memset(&cp
, 0, sizeof(cp
));
356 argv_array_push(&cp
.args
, "branch");
357 if (opts
.force_new_branch
)
358 argv_array_push(&cp
.args
, "--force");
359 argv_array_push(&cp
.args
, opts
.new_branch
);
360 argv_array_push(&cp
.args
, branch
);
361 if (run_command(&cp
))
363 branch
= opts
.new_branch
;
366 return add_worktree(path
, branch
, &opts
);
369 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
371 struct option options
[] = {
376 usage_with_options(worktree_usage
, options
);
377 if (!strcmp(av
[1], "add"))
378 return add(ac
- 1, av
+ 1, prefix
);
379 if (!strcmp(av
[1], "prune"))
380 return prune(ac
- 1, av
+ 1, prefix
);
381 usage_with_options(worktree_usage
, options
);