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>]"),
17 static unsigned long expire
;
19 static int prune_worktree(const char *id
, struct strbuf
*reason
)
25 if (!is_directory(git_path("worktrees/%s", id
))) {
26 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
29 if (file_exists(git_path("worktrees/%s/locked", id
)))
31 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
32 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
35 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
37 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
42 path
= xmalloc(len
+ 1);
43 read_in_full(fd
, path
, len
);
45 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
48 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
53 if (!file_exists(path
)) {
57 * the repo is moved manually and has not been
60 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
63 if (st
.st_mtime
<= expire
) {
64 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
74 static void prune_worktrees(void)
76 struct strbuf reason
= STRBUF_INIT
;
77 struct strbuf path
= STRBUF_INIT
;
78 DIR *dir
= opendir(git_path("worktrees"));
83 while ((d
= readdir(dir
)) != NULL
) {
84 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
86 strbuf_reset(&reason
);
87 if (!prune_worktree(d
->d_name
, &reason
))
89 if (show_only
|| verbose
)
90 printf("%s\n", reason
.buf
);
94 strbuf_addstr(&path
, git_path("worktrees/%s", d
->d_name
));
95 ret
= remove_dir_recursively(&path
, 0);
96 if (ret
< 0 && errno
== ENOTDIR
)
97 ret
= unlink(path
.buf
);
99 error(_("failed to remove: %s"), strerror(errno
));
103 rmdir(git_path("worktrees"));
104 strbuf_release(&reason
);
105 strbuf_release(&path
);
108 static int prune(int ac
, const char **av
, const char *prefix
)
110 struct option options
[] = {
111 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
112 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
113 OPT_EXPIRY_DATE(0, "expire", &expire
,
114 N_("expire objects older than <time>")),
119 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
121 usage_with_options(worktree_usage
, options
);
126 static char *junk_work_tree
;
127 static char *junk_git_dir
;
129 static pid_t junk_pid
;
131 static void remove_junk(void)
133 struct strbuf sb
= STRBUF_INIT
;
134 if (!is_junk
|| getpid() != junk_pid
)
137 strbuf_addstr(&sb
, junk_git_dir
);
138 remove_dir_recursively(&sb
, 0);
141 if (junk_work_tree
) {
142 strbuf_addstr(&sb
, junk_work_tree
);
143 remove_dir_recursively(&sb
, 0);
148 static void remove_junk_on_signal(int signo
)
155 static int add_worktree(const char *path
, const char **child_argv
)
157 struct strbuf sb_git
= STRBUF_INIT
, sb_repo
= STRBUF_INIT
;
158 struct strbuf sb
= STRBUF_INIT
;
161 struct child_process cp
;
162 int counter
= 0, len
, ret
;
163 unsigned char rev
[20];
165 if (file_exists(path
) && !is_empty_dir(path
))
166 die(_("'%s' already exists"), path
);
169 while (len
&& is_dir_sep(path
[len
- 1]))
172 for (name
= path
+ len
- 1; name
> path
; name
--)
173 if (is_dir_sep(*name
)) {
177 strbuf_addstr(&sb_repo
,
178 git_path("worktrees/%.*s", (int)(path
+ len
- name
), name
));
180 if (safe_create_leading_directories_const(sb_repo
.buf
))
181 die_errno(_("could not create leading directories of '%s'"),
183 while (!stat(sb_repo
.buf
, &st
)) {
185 strbuf_setlen(&sb_repo
, len
);
186 strbuf_addf(&sb_repo
, "%d", counter
);
188 name
= strrchr(sb_repo
.buf
, '/') + 1;
192 sigchain_push_common(remove_junk_on_signal
);
194 if (mkdir(sb_repo
.buf
, 0777))
195 die_errno(_("could not create directory of '%s'"), sb_repo
.buf
);
196 junk_git_dir
= xstrdup(sb_repo
.buf
);
200 * lock the incomplete repo so prune won't delete it, unlock
201 * after the preparation is over.
203 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
204 write_file(sb
.buf
, 1, "initializing\n");
206 strbuf_addf(&sb_git
, "%s/.git", path
);
207 if (safe_create_leading_directories_const(sb_git
.buf
))
208 die_errno(_("could not create leading directories of '%s'"),
210 junk_work_tree
= xstrdup(path
);
213 strbuf_addf(&sb
, "%s/gitdir", sb_repo
.buf
);
214 write_file(sb
.buf
, 1, "%s\n", real_path(sb_git
.buf
));
215 write_file(sb_git
.buf
, 1, "gitdir: %s/worktrees/%s\n",
216 real_path(get_git_common_dir()), name
);
218 * This is to keep resolve_ref() happy. We need a valid HEAD
219 * or is_git_directory() will reject the directory. Moreover, HEAD
220 * in the new worktree must resolve to the same value as HEAD in
221 * the current tree since the command invoked to populate the new
222 * worktree will be handed the branch/ref specified by the user.
223 * For instance, if the user asks for the new worktree to be based
224 * at HEAD~5, then the resolved HEAD~5 in the new worktree must
225 * match the resolved HEAD~5 in the current tree in order to match
226 * the user's expectation.
228 if (!resolve_ref_unsafe("HEAD", 0, rev
, NULL
))
229 die(_("unable to resolve HEAD"));
231 strbuf_addf(&sb
, "%s/HEAD", sb_repo
.buf
);
232 write_file(sb
.buf
, 1, "%s\n", sha1_to_hex(rev
));
234 strbuf_addf(&sb
, "%s/commondir", sb_repo
.buf
);
235 write_file(sb
.buf
, 1, "../..\n");
237 fprintf_ln(stderr
, _("Enter %s (identifier %s)"), path
, name
);
239 setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
240 setenv(GIT_DIR_ENVIRONMENT
, sb_git
.buf
, 1);
241 setenv(GIT_WORK_TREE_ENVIRONMENT
, path
, 1);
242 memset(&cp
, 0, sizeof(cp
));
244 cp
.argv
= child_argv
;
245 ret
= run_command(&cp
);
248 free(junk_work_tree
);
250 junk_work_tree
= NULL
;
254 strbuf_addf(&sb
, "%s/locked", sb_repo
.buf
);
255 unlink_or_warn(sb
.buf
);
257 strbuf_release(&sb_repo
);
258 strbuf_release(&sb_git
);
262 static int add(int ac
, const char **av
, const char *prefix
)
264 int force
= 0, detach
= 0;
265 const char *new_branch
= NULL
, *new_branch_force
= NULL
;
266 const char *path
, *branch
;
267 struct argv_array cmd
= ARGV_ARRAY_INIT
;
268 struct option options
[] = {
269 OPT__FORCE(&force
, N_("checkout <branch> even if already checked out in other worktree")),
270 OPT_STRING('b', NULL
, &new_branch
, N_("branch"),
271 N_("create a new branch")),
272 OPT_STRING('B', NULL
, &new_branch_force
, N_("branch"),
273 N_("create or reset a branch")),
274 OPT_BOOL(0, "detach", &detach
, N_("detach HEAD at named commit")),
278 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
279 if (new_branch
&& new_branch_force
)
280 die(_("-b and -B are mutually exclusive"));
282 usage_with_options(worktree_usage
, options
);
284 path
= prefix
? prefix_filename(prefix
, strlen(prefix
), av
[0]) : av
[0];
287 argv_array_push(&cmd
, "checkout");
289 argv_array_push(&cmd
, "--ignore-other-worktrees");
291 argv_array_pushl(&cmd
, "-b", new_branch
, NULL
);
292 if (new_branch_force
)
293 argv_array_pushl(&cmd
, "-B", new_branch_force
, NULL
);
295 argv_array_push(&cmd
, "--detach");
296 argv_array_push(&cmd
, branch
);
298 return add_worktree(path
, cmd
.argv
);
301 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
303 struct option options
[] = {
308 usage_with_options(worktree_usage
, options
);
309 if (!strcmp(av
[1], "add"))
310 return add(ac
- 1, av
+ 1, prefix
);
311 if (!strcmp(av
[1], "prune"))
312 return prune(ac
- 1, av
+ 1, prefix
);
313 usage_with_options(worktree_usage
, options
);