]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/worktree.c
worktree: add_worktree: construct worktree-population command locally
[thirdparty/git.git] / builtin / worktree.c
CommitLineData
df0b6cfb
NTND
1#include "cache.h"
2#include "builtin.h"
3#include "dir.h"
4#include "parse-options.h"
fc56361f
ES
5#include "argv-array.h"
6#include "run-command.h"
b979d950 7#include "sigchain.h"
df0b6cfb
NTND
8
9static const char * const worktree_usage[] = {
f4325444 10 N_("git worktree add [<options>] <path> <branch>"),
df0b6cfb
NTND
11 N_("git worktree prune [<options>]"),
12 NULL
13};
14
5dd6e234
ES
15struct add_opts {
16 int force;
17 int detach;
18 const char *new_branch;
19 int force_new_branch;
20};
21
df0b6cfb
NTND
22static int show_only;
23static int verbose;
24static unsigned long expire;
25
26static int prune_worktree(const char *id, struct strbuf *reason)
27{
28 struct stat st;
29 char *path;
30 int fd, len;
31
32 if (!is_directory(git_path("worktrees/%s", id))) {
33 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
34 return 1;
35 }
36 if (file_exists(git_path("worktrees/%s/locked", id)))
37 return 0;
38 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
39 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
40 return 1;
41 }
42 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
43 if (fd < 0) {
44 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
45 id, strerror(errno));
46 return 1;
47 }
48 len = st.st_size;
49 path = xmalloc(len + 1);
50 read_in_full(fd, path, len);
51 close(fd);
52 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
53 len--;
54 if (!len) {
55 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
56 free(path);
57 return 1;
58 }
59 path[len] = '\0';
60 if (!file_exists(path)) {
61 struct stat st_link;
62 free(path);
63 /*
64 * the repo is moved manually and has not been
65 * accessed since?
66 */
67 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
68 st_link.st_nlink > 1)
69 return 0;
70 if (st.st_mtime <= expire) {
71 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
72 return 1;
73 } else {
74 return 0;
75 }
76 }
77 free(path);
78 return 0;
79}
80
81static void prune_worktrees(void)
82{
83 struct strbuf reason = STRBUF_INIT;
84 struct strbuf path = STRBUF_INIT;
85 DIR *dir = opendir(git_path("worktrees"));
86 struct dirent *d;
87 int ret;
88 if (!dir)
89 return;
90 while ((d = readdir(dir)) != NULL) {
91 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
92 continue;
93 strbuf_reset(&reason);
94 if (!prune_worktree(d->d_name, &reason))
95 continue;
96 if (show_only || verbose)
97 printf("%s\n", reason.buf);
98 if (show_only)
99 continue;
100 strbuf_reset(&path);
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);
105 if (ret)
106 error(_("failed to remove: %s"), strerror(errno));
107 }
108 closedir(dir);
109 if (!show_only)
110 rmdir(git_path("worktrees"));
111 strbuf_release(&reason);
112 strbuf_release(&path);
113}
114
115static int prune(int ac, const char **av, const char *prefix)
116{
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>")),
122 OPT_END()
123 };
124
125 expire = ULONG_MAX;
126 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
127 if (ac)
128 usage_with_options(worktree_usage, options);
129 prune_worktrees();
130 return 0;
131}
132
b979d950
ES
133static char *junk_work_tree;
134static char *junk_git_dir;
135static int is_junk;
136static pid_t junk_pid;
137
138static void remove_junk(void)
139{
140 struct strbuf sb = STRBUF_INIT;
141 if (!is_junk || getpid() != junk_pid)
142 return;
143 if (junk_git_dir) {
144 strbuf_addstr(&sb, junk_git_dir);
145 remove_dir_recursively(&sb, 0);
146 strbuf_reset(&sb);
147 }
148 if (junk_work_tree) {
149 strbuf_addstr(&sb, junk_work_tree);
150 remove_dir_recursively(&sb, 0);
151 }
152 strbuf_release(&sb);
153}
154
155static void remove_junk_on_signal(int signo)
156{
157 remove_junk();
158 sigchain_pop(signo);
159 raise(signo);
160}
161
f5682b2a
ES
162static const char *worktree_basename(const char *path, int *olen)
163{
164 const char *name;
165 int len;
166
167 len = strlen(path);
168 while (len && is_dir_sep(path[len - 1]))
169 len--;
170
171 for (name = path + len - 1; name > path; name--)
172 if (is_dir_sep(*name)) {
173 name++;
174 break;
175 }
176
177 *olen = len;
178 return name;
179}
180
80a0548f 181static int add_worktree(const char *path, const char *refname,
5dd6e234 182 const struct add_opts *opts)
b979d950
ES
183{
184 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
185 struct strbuf sb = STRBUF_INIT;
186 const char *name;
187 struct stat st;
188 struct child_process cp;
ae2a3827 189 struct argv_array child_env = ARGV_ARRAY_INIT;
b979d950
ES
190 int counter = 0, len, ret;
191 unsigned char rev[20];
192
193 if (file_exists(path) && !is_empty_dir(path))
194 die(_("'%s' already exists"), path);
195
f5682b2a 196 name = worktree_basename(path, &len);
b979d950
ES
197 strbuf_addstr(&sb_repo,
198 git_path("worktrees/%.*s", (int)(path + len - name), name));
199 len = sb_repo.len;
200 if (safe_create_leading_directories_const(sb_repo.buf))
201 die_errno(_("could not create leading directories of '%s'"),
202 sb_repo.buf);
203 while (!stat(sb_repo.buf, &st)) {
204 counter++;
205 strbuf_setlen(&sb_repo, len);
206 strbuf_addf(&sb_repo, "%d", counter);
207 }
208 name = strrchr(sb_repo.buf, '/') + 1;
209
210 junk_pid = getpid();
211 atexit(remove_junk);
212 sigchain_push_common(remove_junk_on_signal);
213
214 if (mkdir(sb_repo.buf, 0777))
215 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
216 junk_git_dir = xstrdup(sb_repo.buf);
217 is_junk = 1;
218
219 /*
220 * lock the incomplete repo so prune won't delete it, unlock
221 * after the preparation is over.
222 */
223 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
224 write_file(sb.buf, 1, "initializing\n");
225
226 strbuf_addf(&sb_git, "%s/.git", path);
227 if (safe_create_leading_directories_const(sb_git.buf))
228 die_errno(_("could not create leading directories of '%s'"),
229 sb_git.buf);
230 junk_work_tree = xstrdup(path);
231
232 strbuf_reset(&sb);
233 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
234 write_file(sb.buf, 1, "%s\n", real_path(sb_git.buf));
235 write_file(sb_git.buf, 1, "gitdir: %s/worktrees/%s\n",
236 real_path(get_git_common_dir()), name);
237 /*
238 * This is to keep resolve_ref() happy. We need a valid HEAD
239 * or is_git_directory() will reject the directory. Moreover, HEAD
240 * in the new worktree must resolve to the same value as HEAD in
241 * the current tree since the command invoked to populate the new
242 * worktree will be handed the branch/ref specified by the user.
243 * For instance, if the user asks for the new worktree to be based
244 * at HEAD~5, then the resolved HEAD~5 in the new worktree must
245 * match the resolved HEAD~5 in the current tree in order to match
246 * the user's expectation.
247 */
248 if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
249 die(_("unable to resolve HEAD"));
250 strbuf_reset(&sb);
251 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
252 write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
253 strbuf_reset(&sb);
254 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
255 write_file(sb.buf, 1, "../..\n");
256
cd2f4713 257 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
b979d950
ES
258
259 setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
ae2a3827
ES
260 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
261 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
b979d950
ES
262 memset(&cp, 0, sizeof(cp));
263 cp.git_cmd = 1;
80a0548f
ES
264 argv_array_push(&cp.args, "checkout");
265 if (opts->force)
266 argv_array_push(&cp.args, "--ignore-other-worktrees");
267 if (opts->detach)
268 argv_array_push(&cp.args, "--detach");
269 argv_array_push(&cp.args, refname);
ae2a3827 270 cp.env = child_env.argv;
b979d950
ES
271 ret = run_command(&cp);
272 if (!ret) {
273 is_junk = 0;
274 free(junk_work_tree);
275 free(junk_git_dir);
276 junk_work_tree = NULL;
277 junk_git_dir = NULL;
278 }
279 strbuf_reset(&sb);
280 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
281 unlink_or_warn(sb.buf);
ae2a3827 282 argv_array_clear(&child_env);
b979d950
ES
283 strbuf_release(&sb);
284 strbuf_release(&sb_repo);
285 strbuf_release(&sb_git);
286 return ret;
287}
288
fc56361f
ES
289static int add(int ac, const char **av, const char *prefix)
290{
5dd6e234
ES
291 struct add_opts opts;
292 const char *new_branch_force = NULL;
fc56361f 293 const char *path, *branch;
fc56361f 294 struct option options[] = {
5dd6e234
ES
295 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
296 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
cbdf60fa
ES
297 N_("create a new branch")),
298 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
299 N_("create or reset a branch")),
5dd6e234 300 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
fc56361f
ES
301 OPT_END()
302 };
303
5dd6e234 304 memset(&opts, 0, sizeof(opts));
fc56361f 305 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
ab0b2c53
ES
306 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
307 die(_("-b, -B, and --detach are mutually exclusive"));
0f4af3b9
ES
308 if (ac < 1 || ac > 2)
309 usage_with_options(worktree_usage, options);
fc56361f
ES
310
311 path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
0f4af3b9 312 branch = ac < 2 ? "HEAD" : av[1];
fc56361f 313
5dd6e234
ES
314 opts.force_new_branch = !!new_branch_force;
315 if (opts.force_new_branch)
316 opts.new_branch = new_branch_force;
eef005dc 317
5c942570 318 if (ac < 2 && !opts.new_branch && !opts.detach) {
1eb07d82
ES
319 int n;
320 const char *s = worktree_basename(path, &n);
5dd6e234 321 opts.new_branch = xstrndup(s, n);
1eb07d82
ES
322 }
323
c2842439
ES
324 if (opts.new_branch) {
325 struct child_process cp;
326 memset(&cp, 0, sizeof(cp));
327 cp.git_cmd = 1;
328 argv_array_push(&cp.args, "branch");
329 if (opts.force_new_branch)
330 argv_array_push(&cp.args, "--force");
331 argv_array_push(&cp.args, opts.new_branch);
332 argv_array_push(&cp.args, branch);
333 if (run_command(&cp))
334 return -1;
335 branch = opts.new_branch;
336 }
337
80a0548f 338 return add_worktree(path, branch, &opts);
fc56361f
ES
339}
340
df0b6cfb
NTND
341int cmd_worktree(int ac, const char **av, const char *prefix)
342{
343 struct option options[] = {
344 OPT_END()
345 };
346
347 if (ac < 2)
348 usage_with_options(worktree_usage, options);
fc56361f
ES
349 if (!strcmp(av[1], "add"))
350 return add(ac - 1, av + 1, prefix);
df0b6cfb
NTND
351 if (!strcmp(av[1], "prune"))
352 return prune(ac - 1, av + 1, prefix);
353 usage_with_options(worktree_usage, options);
354}