]>
Commit | Line | Data |
---|---|---|
df0b6cfb | 1 | #include "cache.h" |
4e853331 | 2 | #include "checkout.h" |
b2141fc1 | 3 | #include "config.h" |
df0b6cfb NTND |
4 | #include "builtin.h" |
5 | #include "dir.h" | |
6 | #include "parse-options.h" | |
fc56361f | 7 | #include "argv-array.h" |
f7c9dac1 ES |
8 | #include "branch.h" |
9 | #include "refs.h" | |
fc56361f | 10 | #include "run-command.h" |
b979d950 | 11 | #include "sigchain.h" |
799767cc | 12 | #include "refs.h" |
bb9c03b8 MR |
13 | #include "utf8.h" |
14 | #include "worktree.h" | |
df0b6cfb NTND |
15 | |
16 | static const char * const worktree_usage[] = { | |
b780e440 | 17 | N_("git worktree add [<options>] <path> [<commit-ish>]"), |
bb9c03b8 | 18 | N_("git worktree list [<options>]"), |
58142c09 | 19 | N_("git worktree lock [<options>] <path>"), |
9f792bb4 | 20 | N_("git worktree move <worktree> <new-path>"), |
7b722d90 | 21 | N_("git worktree prune [<options>]"), |
cc73385c | 22 | N_("git worktree remove [<options>] <worktree>"), |
6d308627 | 23 | N_("git worktree unlock <path>"), |
df0b6cfb NTND |
24 | NULL |
25 | }; | |
26 | ||
5dd6e234 ES |
27 | struct add_opts { |
28 | int force; | |
29 | int detach; | |
371979c2 | 30 | int quiet; |
ef2a0ac9 | 31 | int checkout; |
507e6e9e | 32 | int keep_locked; |
5dd6e234 ES |
33 | }; |
34 | ||
df0b6cfb NTND |
35 | static int show_only; |
36 | static int verbose; | |
e92445a7 | 37 | static int guess_remote; |
dddbad72 | 38 | static timestamp_t expire; |
df0b6cfb | 39 | |
e92445a7 TG |
40 | static int git_worktree_config(const char *var, const char *value, void *cb) |
41 | { | |
42 | if (!strcmp(var, "worktree.guessremote")) { | |
43 | guess_remote = git_config_bool(var, value); | |
44 | return 0; | |
45 | } | |
46 | ||
47 | return git_default_config(var, value, cb); | |
48 | } | |
49 | ||
602aaed0 | 50 | static int delete_git_dir(const char *id) |
e5353bef ES |
51 | { |
52 | struct strbuf sb = STRBUF_INIT; | |
602aaed0 | 53 | int ret; |
e5353bef | 54 | |
602aaed0 ES |
55 | strbuf_addstr(&sb, git_common_path("worktrees/%s", id)); |
56 | ret = remove_dir_recursively(&sb, 0); | |
57 | if (ret < 0 && errno == ENOTDIR) | |
58 | ret = unlink(sb.buf); | |
59 | if (ret) | |
e5353bef | 60 | error_errno(_("failed to delete '%s'"), sb.buf); |
e5353bef ES |
61 | strbuf_release(&sb); |
62 | return ret; | |
63 | } | |
64 | ||
3a540433 ES |
65 | static void delete_worktrees_dir_if_empty(void) |
66 | { | |
67 | rmdir(git_path("worktrees")); /* ignore failed removal */ | |
68 | } | |
69 | ||
df0b6cfb NTND |
70 | static int prune_worktree(const char *id, struct strbuf *reason) |
71 | { | |
72 | struct stat st; | |
73 | char *path; | |
228740b6 JK |
74 | int fd; |
75 | size_t len; | |
8a1a8d2a | 76 | ssize_t read_result; |
df0b6cfb NTND |
77 | |
78 | if (!is_directory(git_path("worktrees/%s", id))) { | |
79 | strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id); | |
80 | return 1; | |
81 | } | |
82 | if (file_exists(git_path("worktrees/%s/locked", id))) | |
83 | return 0; | |
84 | if (stat(git_path("worktrees/%s/gitdir", id), &st)) { | |
85 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id); | |
86 | return 1; | |
87 | } | |
88 | fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY); | |
89 | if (fd < 0) { | |
90 | strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"), | |
91 | id, strerror(errno)); | |
92 | return 1; | |
93 | } | |
228740b6 | 94 | len = xsize_t(st.st_size); |
3733e694 | 95 | path = xmallocz(len); |
8a1a8d2a JK |
96 | |
97 | read_result = read_in_full(fd, path, len); | |
98 | if (read_result < 0) { | |
99 | strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"), | |
100 | id, strerror(errno)); | |
101 | close(fd); | |
102 | free(path); | |
103 | return 1; | |
104 | } | |
df0b6cfb | 105 | close(fd); |
8a1a8d2a JK |
106 | |
107 | if (read_result != len) { | |
108 | strbuf_addf(reason, | |
109 | _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), | |
110 | id, (uintmax_t)len, (uintmax_t)read_result); | |
111 | free(path); | |
112 | return 1; | |
113 | } | |
df0b6cfb NTND |
114 | while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) |
115 | len--; | |
116 | if (!len) { | |
117 | strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id); | |
118 | free(path); | |
119 | return 1; | |
120 | } | |
121 | path[len] = '\0'; | |
122 | if (!file_exists(path)) { | |
df0b6cfb | 123 | free(path); |
327864aa NTND |
124 | if (stat(git_path("worktrees/%s/index", id), &st) || |
125 | st.st_mtime <= expire) { | |
df0b6cfb NTND |
126 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id); |
127 | return 1; | |
128 | } else { | |
129 | return 0; | |
130 | } | |
131 | } | |
132 | free(path); | |
133 | return 0; | |
134 | } | |
135 | ||
136 | static void prune_worktrees(void) | |
137 | { | |
138 | struct strbuf reason = STRBUF_INIT; | |
df0b6cfb NTND |
139 | DIR *dir = opendir(git_path("worktrees")); |
140 | struct dirent *d; | |
df0b6cfb NTND |
141 | if (!dir) |
142 | return; | |
143 | while ((d = readdir(dir)) != NULL) { | |
afb9e30b | 144 | if (is_dot_or_dotdot(d->d_name)) |
df0b6cfb NTND |
145 | continue; |
146 | strbuf_reset(&reason); | |
147 | if (!prune_worktree(d->d_name, &reason)) | |
148 | continue; | |
149 | if (show_only || verbose) | |
150 | printf("%s\n", reason.buf); | |
151 | if (show_only) | |
152 | continue; | |
602aaed0 | 153 | delete_git_dir(d->d_name); |
df0b6cfb NTND |
154 | } |
155 | closedir(dir); | |
156 | if (!show_only) | |
3a540433 | 157 | delete_worktrees_dir_if_empty(); |
df0b6cfb | 158 | strbuf_release(&reason); |
df0b6cfb NTND |
159 | } |
160 | ||
161 | static int prune(int ac, const char **av, const char *prefix) | |
162 | { | |
163 | struct option options[] = { | |
164 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), | |
2488dcab | 165 | OPT__VERBOSE(&verbose, N_("report pruned working trees")), |
df0b6cfb | 166 | OPT_EXPIRY_DATE(0, "expire", &expire, |
2488dcab | 167 | N_("expire working trees older than <time>")), |
df0b6cfb NTND |
168 | OPT_END() |
169 | }; | |
170 | ||
dddbad72 | 171 | expire = TIME_MAX; |
df0b6cfb NTND |
172 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
173 | if (ac) | |
174 | usage_with_options(worktree_usage, options); | |
175 | prune_worktrees(); | |
176 | return 0; | |
177 | } | |
178 | ||
b979d950 ES |
179 | static char *junk_work_tree; |
180 | static char *junk_git_dir; | |
181 | static int is_junk; | |
182 | static pid_t junk_pid; | |
183 | ||
184 | static void remove_junk(void) | |
185 | { | |
186 | struct strbuf sb = STRBUF_INIT; | |
187 | if (!is_junk || getpid() != junk_pid) | |
188 | return; | |
189 | if (junk_git_dir) { | |
190 | strbuf_addstr(&sb, junk_git_dir); | |
191 | remove_dir_recursively(&sb, 0); | |
192 | strbuf_reset(&sb); | |
193 | } | |
194 | if (junk_work_tree) { | |
195 | strbuf_addstr(&sb, junk_work_tree); | |
196 | remove_dir_recursively(&sb, 0); | |
197 | } | |
198 | strbuf_release(&sb); | |
199 | } | |
200 | ||
201 | static void remove_junk_on_signal(int signo) | |
202 | { | |
203 | remove_junk(); | |
204 | sigchain_pop(signo); | |
205 | raise(signo); | |
206 | } | |
207 | ||
f5682b2a ES |
208 | static const char *worktree_basename(const char *path, int *olen) |
209 | { | |
210 | const char *name; | |
211 | int len; | |
212 | ||
213 | len = strlen(path); | |
214 | while (len && is_dir_sep(path[len - 1])) | |
215 | len--; | |
216 | ||
217 | for (name = path + len - 1; name > path; name--) | |
218 | if (is_dir_sep(*name)) { | |
219 | name++; | |
220 | break; | |
221 | } | |
222 | ||
223 | *olen = len; | |
224 | return name; | |
225 | } | |
226 | ||
45059e64 ES |
227 | static void validate_worktree_add(const char *path, const struct add_opts *opts) |
228 | { | |
cb56f55c ES |
229 | struct worktree **worktrees; |
230 | struct worktree *wt; | |
231 | int locked; | |
232 | ||
45059e64 ES |
233 | if (file_exists(path) && !is_empty_dir(path)) |
234 | die(_("'%s' already exists"), path); | |
cb56f55c ES |
235 | |
236 | worktrees = get_worktrees(0); | |
237 | /* | |
238 | * find_worktree()'s suffix matching may undesirably find the main | |
239 | * rather than a linked worktree (for instance, when the basenames | |
240 | * of the main worktree and the one being created are the same). | |
241 | * We're only interested in linked worktrees, so skip the main | |
242 | * worktree with +1. | |
243 | */ | |
244 | wt = find_worktree(worktrees + 1, NULL, path); | |
245 | if (!wt) | |
246 | goto done; | |
247 | ||
d236f12b | 248 | locked = !!worktree_lock_reason(wt); |
e19831c9 ES |
249 | if ((!locked && opts->force) || (locked && opts->force > 1)) { |
250 | if (delete_git_dir(wt->id)) | |
251 | die(_("unable to re-add worktree '%s'"), path); | |
252 | goto done; | |
253 | } | |
254 | ||
cb56f55c | 255 | if (locked) |
e19831c9 | 256 | die(_("'%s' is a missing but locked worktree;\nuse 'add -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path); |
cb56f55c | 257 | else |
e19831c9 | 258 | die(_("'%s' is a missing but already registered worktree;\nuse 'add -f' to override, or 'prune' or 'remove' to clear"), path); |
cb56f55c ES |
259 | |
260 | done: | |
261 | free_worktrees(worktrees); | |
45059e64 ES |
262 | } |
263 | ||
80a0548f | 264 | static int add_worktree(const char *path, const char *refname, |
5dd6e234 | 265 | const struct add_opts *opts) |
b979d950 ES |
266 | { |
267 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; | |
268 | struct strbuf sb = STRBUF_INIT; | |
269 | const char *name; | |
270 | struct stat st; | |
542aa25d | 271 | struct child_process cp = CHILD_PROCESS_INIT; |
ae2a3827 | 272 | struct argv_array child_env = ARGV_ARRAY_INIT; |
b979d950 | 273 | int counter = 0, len, ret; |
f7c9dac1 ES |
274 | struct strbuf symref = STRBUF_INIT; |
275 | struct commit *commit = NULL; | |
ade546be | 276 | int is_branch = 0; |
b979d950 | 277 | |
45059e64 | 278 | validate_worktree_add(path, opts); |
b979d950 | 279 | |
f7c9dac1 | 280 | /* is 'refname' a branch or commit? */ |
0ebf4a2a | 281 | if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && |
ade546be ES |
282 | ref_exists(symref.buf)) { |
283 | is_branch = 1; | |
f7c9dac1 | 284 | if (!opts->force) |
8d9fdd70 | 285 | die_if_checked_out(symref.buf, 0); |
f7c9dac1 | 286 | } |
ade546be ES |
287 | commit = lookup_commit_reference_by_name(refname); |
288 | if (!commit) | |
289 | die(_("invalid reference: %s"), refname); | |
f7c9dac1 | 290 | |
f5682b2a | 291 | name = worktree_basename(path, &len); |
8c2ca3a6 | 292 | git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name); |
b979d950 ES |
293 | len = sb_repo.len; |
294 | if (safe_create_leading_directories_const(sb_repo.buf)) | |
295 | die_errno(_("could not create leading directories of '%s'"), | |
296 | sb_repo.buf); | |
297 | while (!stat(sb_repo.buf, &st)) { | |
298 | counter++; | |
299 | strbuf_setlen(&sb_repo, len); | |
300 | strbuf_addf(&sb_repo, "%d", counter); | |
301 | } | |
302 | name = strrchr(sb_repo.buf, '/') + 1; | |
303 | ||
304 | junk_pid = getpid(); | |
305 | atexit(remove_junk); | |
306 | sigchain_push_common(remove_junk_on_signal); | |
307 | ||
308 | if (mkdir(sb_repo.buf, 0777)) | |
309 | die_errno(_("could not create directory of '%s'"), sb_repo.buf); | |
310 | junk_git_dir = xstrdup(sb_repo.buf); | |
311 | is_junk = 1; | |
312 | ||
313 | /* | |
314 | * lock the incomplete repo so prune won't delete it, unlock | |
315 | * after the preparation is over. | |
316 | */ | |
317 | strbuf_addf(&sb, "%s/locked", sb_repo.buf); | |
507e6e9e NTND |
318 | if (!opts->keep_locked) |
319 | write_file(sb.buf, "initializing"); | |
320 | else | |
321 | write_file(sb.buf, "added with --lock"); | |
b979d950 ES |
322 | |
323 | strbuf_addf(&sb_git, "%s/.git", path); | |
324 | if (safe_create_leading_directories_const(sb_git.buf)) | |
325 | die_errno(_("could not create leading directories of '%s'"), | |
326 | sb_git.buf); | |
327 | junk_work_tree = xstrdup(path); | |
328 | ||
329 | strbuf_reset(&sb); | |
330 | strbuf_addf(&sb, "%s/gitdir", sb_repo.buf); | |
1f76a10b JH |
331 | write_file(sb.buf, "%s", real_path(sb_git.buf)); |
332 | write_file(sb_git.buf, "gitdir: %s/worktrees/%s", | |
b979d950 ES |
333 | real_path(get_git_common_dir()), name); |
334 | /* | |
335 | * This is to keep resolve_ref() happy. We need a valid HEAD | |
ed197a6a ES |
336 | * or is_git_directory() will reject the directory. Any value which |
337 | * looks like an object ID will do since it will be immediately | |
338 | * replaced by the symbolic-ref or update-ref invocation in the new | |
339 | * worktree. | |
b979d950 | 340 | */ |
b979d950 ES |
341 | strbuf_reset(&sb); |
342 | strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); | |
dabd35f4 | 343 | write_file(sb.buf, "%s", sha1_to_hex(null_sha1)); |
b979d950 ES |
344 | strbuf_reset(&sb); |
345 | strbuf_addf(&sb, "%s/commondir", sb_repo.buf); | |
1f76a10b | 346 | write_file(sb.buf, "../.."); |
b979d950 | 347 | |
ae2a3827 ES |
348 | argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); |
349 | argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); | |
b979d950 | 350 | cp.git_cmd = 1; |
7f44e3d1 | 351 | |
ade546be | 352 | if (!is_branch) |
7f44e3d1 | 353 | argv_array_pushl(&cp.args, "update-ref", "HEAD", |
f2fd0760 | 354 | oid_to_hex(&commit->object.oid), NULL); |
371979c2 | 355 | else { |
7f44e3d1 ES |
356 | argv_array_pushl(&cp.args, "symbolic-ref", "HEAD", |
357 | symref.buf, NULL); | |
371979c2 EP |
358 | if (opts->quiet) |
359 | argv_array_push(&cp.args, "--quiet"); | |
360 | } | |
361 | ||
7f44e3d1 ES |
362 | cp.env = child_env.argv; |
363 | ret = run_command(&cp); | |
364 | if (ret) | |
365 | goto done; | |
366 | ||
ef2a0ac9 RZ |
367 | if (opts->checkout) { |
368 | cp.argv = NULL; | |
369 | argv_array_clear(&cp.args); | |
370 | argv_array_pushl(&cp.args, "reset", "--hard", NULL); | |
371979c2 EP |
371 | if (opts->quiet) |
372 | argv_array_push(&cp.args, "--quiet"); | |
ef2a0ac9 RZ |
373 | cp.env = child_env.argv; |
374 | ret = run_command(&cp); | |
375 | if (ret) | |
376 | goto done; | |
b979d950 | 377 | } |
ef2a0ac9 RZ |
378 | |
379 | is_junk = 0; | |
88ce3ef6 ÆAB |
380 | FREE_AND_NULL(junk_work_tree); |
381 | FREE_AND_NULL(junk_git_dir); | |
ef2a0ac9 | 382 | |
7f44e3d1 | 383 | done: |
507e6e9e NTND |
384 | if (ret || !opts->keep_locked) { |
385 | strbuf_reset(&sb); | |
386 | strbuf_addf(&sb, "%s/locked", sb_repo.buf); | |
387 | unlink_or_warn(sb.buf); | |
388 | } | |
ade546be ES |
389 | |
390 | /* | |
391 | * Hook failure does not warrant worktree deletion, so run hook after | |
392 | * is_junk is cleared, but do return appropriate code when hook fails. | |
393 | */ | |
a4bf1e3c ES |
394 | if (!ret && opts->checkout) { |
395 | const char *hook = find_hook("post-checkout"); | |
396 | if (hook) { | |
397 | const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL }; | |
398 | cp.git_cmd = 0; | |
399 | cp.no_stdin = 1; | |
400 | cp.stdout_to_stderr = 1; | |
401 | cp.dir = path; | |
402 | cp.env = env; | |
403 | cp.argv = NULL; | |
404 | argv_array_pushl(&cp.args, absolute_path(hook), | |
405 | oid_to_hex(&null_oid), | |
406 | oid_to_hex(&commit->object.oid), | |
407 | "1", NULL); | |
408 | ret = run_command(&cp); | |
409 | } | |
410 | } | |
ade546be | 411 | |
ae2a3827 | 412 | argv_array_clear(&child_env); |
b979d950 | 413 | strbuf_release(&sb); |
f7c9dac1 | 414 | strbuf_release(&symref); |
b979d950 ES |
415 | strbuf_release(&sb_repo); |
416 | strbuf_release(&sb_git); | |
417 | return ret; | |
418 | } | |
419 | ||
2c27002a TG |
420 | static void print_preparing_worktree_line(int detach, |
421 | const char *branch, | |
422 | const char *new_branch, | |
423 | int force_new_branch) | |
424 | { | |
425 | if (force_new_branch) { | |
426 | struct commit *commit = lookup_commit_reference_by_name(new_branch); | |
427 | if (!commit) | |
428 | printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); | |
429 | else | |
430 | printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"), | |
431 | new_branch, | |
10174da9 | 432 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
2c27002a TG |
433 | } else if (new_branch) { |
434 | printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); | |
435 | } else { | |
436 | struct strbuf s = STRBUF_INIT; | |
437 | if (!detach && !strbuf_check_branch_ref(&s, branch) && | |
438 | ref_exists(s.buf)) | |
439 | printf_ln(_("Preparing worktree (checking out '%s')"), | |
440 | branch); | |
441 | else { | |
442 | struct commit *commit = lookup_commit_reference_by_name(branch); | |
443 | if (!commit) | |
444 | die(_("invalid reference: %s"), branch); | |
445 | printf_ln(_("Preparing worktree (detached HEAD %s)"), | |
10174da9 | 446 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
2c27002a TG |
447 | } |
448 | strbuf_release(&s); | |
449 | } | |
450 | } | |
451 | ||
6427f871 TG |
452 | static const char *dwim_branch(const char *path, const char **new_branch) |
453 | { | |
454 | int n; | |
455 | const char *s = worktree_basename(path, &n); | |
f60a7b76 TG |
456 | const char *branchname = xstrndup(s, n); |
457 | struct strbuf ref = STRBUF_INIT; | |
458 | ||
459 | UNLEAK(branchname); | |
460 | if (!strbuf_check_branch_ref(&ref, branchname) && | |
461 | ref_exists(ref.buf)) { | |
462 | strbuf_release(&ref); | |
463 | return branchname; | |
464 | } | |
465 | ||
466 | *new_branch = branchname; | |
6427f871 TG |
467 | if (guess_remote) { |
468 | struct object_id oid; | |
469 | const char *remote = | |
3c87aa94 | 470 | unique_tracking_name(*new_branch, &oid, NULL); |
6427f871 TG |
471 | return remote; |
472 | } | |
473 | return NULL; | |
474 | } | |
475 | ||
fc56361f ES |
476 | static int add(int ac, const char **av, const char *prefix) |
477 | { | |
5dd6e234 ES |
478 | struct add_opts opts; |
479 | const char *new_branch_force = NULL; | |
e4da43b1 JK |
480 | char *path; |
481 | const char *branch; | |
d861d34a | 482 | const char *new_branch = NULL; |
e284e892 | 483 | const char *opt_track = NULL; |
fc56361f | 484 | struct option options[] = { |
1224781d NTND |
485 | OPT__FORCE(&opts.force, |
486 | N_("checkout <branch> even if already checked out in other worktree"), | |
fc3d4e0c | 487 | PARSE_OPT_NOCOMPLETE), |
d861d34a | 488 | OPT_STRING('b', NULL, &new_branch, N_("branch"), |
cbdf60fa ES |
489 | N_("create a new branch")), |
490 | OPT_STRING('B', NULL, &new_branch_force, N_("branch"), | |
491 | N_("create or reset a branch")), | |
5dd6e234 | 492 | OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")), |
ef2a0ac9 | 493 | OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")), |
507e6e9e | 494 | OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")), |
371979c2 | 495 | OPT__QUIET(&opts.quiet, N_("suppress progress reporting")), |
e284e892 TG |
496 | OPT_PASSTHRU(0, "track", &opt_track, NULL, |
497 | N_("set up tracking mode (see git-branch(1))"), | |
498 | PARSE_OPT_NOARG | PARSE_OPT_OPTARG), | |
71d6682d TG |
499 | OPT_BOOL(0, "guess-remote", &guess_remote, |
500 | N_("try to match the new branch name with a remote-tracking branch")), | |
fc56361f ES |
501 | OPT_END() |
502 | }; | |
503 | ||
5dd6e234 | 504 | memset(&opts, 0, sizeof(opts)); |
ef2a0ac9 | 505 | opts.checkout = 1; |
fc56361f | 506 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
d861d34a | 507 | if (!!opts.detach + !!new_branch + !!new_branch_force > 1) |
ab0b2c53 | 508 | die(_("-b, -B, and --detach are mutually exclusive")); |
0f4af3b9 ES |
509 | if (ac < 1 || ac > 2) |
510 | usage_with_options(worktree_usage, options); | |
fc56361f | 511 | |
116fb64e | 512 | path = prefix_filename(prefix, av[0]); |
0f4af3b9 | 513 | branch = ac < 2 ? "HEAD" : av[1]; |
fc56361f | 514 | |
1a450e2f JDG |
515 | if (!strcmp(branch, "-")) |
516 | branch = "@{-1}"; | |
517 | ||
d861d34a | 518 | if (new_branch_force) { |
beb6f24b NTND |
519 | struct strbuf symref = STRBUF_INIT; |
520 | ||
d861d34a | 521 | new_branch = new_branch_force; |
eef005dc | 522 | |
beb6f24b | 523 | if (!opts.force && |
d861d34a | 524 | !strbuf_check_branch_ref(&symref, new_branch) && |
beb6f24b | 525 | ref_exists(symref.buf)) |
8d9fdd70 | 526 | die_if_checked_out(symref.buf, 0); |
beb6f24b NTND |
527 | strbuf_release(&symref); |
528 | } | |
529 | ||
d861d34a | 530 | if (ac < 2 && !new_branch && !opts.detach) { |
6427f871 TG |
531 | const char *s = dwim_branch(path, &new_branch); |
532 | if (s) | |
533 | branch = s; | |
1eb07d82 ES |
534 | } |
535 | ||
d861d34a | 536 | if (ac == 2 && !new_branch && !opts.detach) { |
4e853331 TG |
537 | struct object_id oid; |
538 | struct commit *commit; | |
539 | const char *remote; | |
540 | ||
541 | commit = lookup_commit_reference_by_name(branch); | |
542 | if (!commit) { | |
3c87aa94 | 543 | remote = unique_tracking_name(branch, &oid, NULL); |
4e853331 | 544 | if (remote) { |
d861d34a | 545 | new_branch = branch; |
4e853331 TG |
546 | branch = remote; |
547 | } | |
548 | } | |
549 | } | |
371979c2 EP |
550 | if (!opts.quiet) |
551 | print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force); | |
2c27002a | 552 | |
d861d34a | 553 | if (new_branch) { |
542aa25d | 554 | struct child_process cp = CHILD_PROCESS_INIT; |
c2842439 ES |
555 | cp.git_cmd = 1; |
556 | argv_array_push(&cp.args, "branch"); | |
d861d34a | 557 | if (new_branch_force) |
c2842439 | 558 | argv_array_push(&cp.args, "--force"); |
371979c2 EP |
559 | if (opts.quiet) |
560 | argv_array_push(&cp.args, "--quiet"); | |
d861d34a | 561 | argv_array_push(&cp.args, new_branch); |
c2842439 | 562 | argv_array_push(&cp.args, branch); |
e284e892 TG |
563 | if (opt_track) |
564 | argv_array_push(&cp.args, opt_track); | |
c2842439 ES |
565 | if (run_command(&cp)) |
566 | return -1; | |
d861d34a | 567 | branch = new_branch; |
e284e892 TG |
568 | } else if (opt_track) { |
569 | die(_("--[no-]track can only be used if a new branch is created")); | |
1eb07d82 ES |
570 | } |
571 | ||
0e5bba53 JK |
572 | UNLEAK(path); |
573 | UNLEAK(opts); | |
80a0548f | 574 | return add_worktree(path, branch, &opts); |
fc56361f ES |
575 | } |
576 | ||
bb9c03b8 MR |
577 | static void show_worktree_porcelain(struct worktree *wt) |
578 | { | |
579 | printf("worktree %s\n", wt->path); | |
580 | if (wt->is_bare) | |
581 | printf("bare\n"); | |
582 | else { | |
0f05154c | 583 | printf("HEAD %s\n", oid_to_hex(&wt->head_oid)); |
bb9c03b8 MR |
584 | if (wt->is_detached) |
585 | printf("detached\n"); | |
a234563a | 586 | else if (wt->head_ref) |
bb9c03b8 MR |
587 | printf("branch %s\n", wt->head_ref); |
588 | } | |
589 | printf("\n"); | |
590 | } | |
591 | ||
592 | static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) | |
593 | { | |
594 | struct strbuf sb = STRBUF_INIT; | |
595 | int cur_path_len = strlen(wt->path); | |
596 | int path_adj = cur_path_len - utf8_strwidth(wt->path); | |
597 | ||
598 | strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path); | |
599 | if (wt->is_bare) | |
600 | strbuf_addstr(&sb, "(bare)"); | |
601 | else { | |
602 | strbuf_addf(&sb, "%-*s ", abbrev_len, | |
aab9583f | 603 | find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV)); |
96f09e2a | 604 | if (wt->is_detached) |
bb9c03b8 | 605 | strbuf_addstr(&sb, "(detached HEAD)"); |
2e11f58f JS |
606 | else if (wt->head_ref) { |
607 | char *ref = shorten_unambiguous_ref(wt->head_ref, 0); | |
608 | strbuf_addf(&sb, "[%s]", ref); | |
609 | free(ref); | |
610 | } else | |
a234563a | 611 | strbuf_addstr(&sb, "(error)"); |
bb9c03b8 MR |
612 | } |
613 | printf("%s\n", sb.buf); | |
614 | ||
615 | strbuf_release(&sb); | |
616 | } | |
617 | ||
618 | static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen) | |
619 | { | |
620 | int i; | |
621 | ||
622 | for (i = 0; wt[i]; i++) { | |
623 | int sha1_len; | |
624 | int path_len = strlen(wt[i]->path); | |
625 | ||
626 | if (path_len > *maxlen) | |
627 | *maxlen = path_len; | |
aab9583f | 628 | sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev)); |
bb9c03b8 MR |
629 | if (sha1_len > *abbrev) |
630 | *abbrev = sha1_len; | |
631 | } | |
632 | } | |
633 | ||
634 | static int list(int ac, const char **av, const char *prefix) | |
635 | { | |
636 | int porcelain = 0; | |
637 | ||
638 | struct option options[] = { | |
639 | OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")), | |
640 | OPT_END() | |
641 | }; | |
642 | ||
643 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
644 | if (ac) | |
645 | usage_with_options(worktree_usage, options); | |
646 | else { | |
4df1d4d4 | 647 | struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED); |
bb9c03b8 MR |
648 | int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i; |
649 | ||
650 | if (!porcelain) | |
651 | measure_widths(worktrees, &abbrev, &path_maxlen); | |
652 | ||
653 | for (i = 0; worktrees[i]; i++) { | |
654 | if (porcelain) | |
655 | show_worktree_porcelain(worktrees[i]); | |
656 | else | |
657 | show_worktree(worktrees[i], path_maxlen, abbrev); | |
658 | } | |
659 | free_worktrees(worktrees); | |
660 | } | |
661 | return 0; | |
662 | } | |
663 | ||
58142c09 NTND |
664 | static int lock_worktree(int ac, const char **av, const char *prefix) |
665 | { | |
666 | const char *reason = "", *old_reason; | |
667 | struct option options[] = { | |
668 | OPT_STRING(0, "reason", &reason, N_("string"), | |
669 | N_("reason for locking")), | |
670 | OPT_END() | |
671 | }; | |
672 | struct worktree **worktrees, *wt; | |
673 | ||
674 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
675 | if (ac != 1) | |
676 | usage_with_options(worktree_usage, options); | |
677 | ||
4fff1ef7 | 678 | worktrees = get_worktrees(0); |
58142c09 NTND |
679 | wt = find_worktree(worktrees, prefix, av[0]); |
680 | if (!wt) | |
681 | die(_("'%s' is not a working tree"), av[0]); | |
682 | if (is_main_worktree(wt)) | |
683 | die(_("The main working tree cannot be locked or unlocked")); | |
684 | ||
d236f12b | 685 | old_reason = worktree_lock_reason(wt); |
58142c09 NTND |
686 | if (old_reason) { |
687 | if (*old_reason) | |
688 | die(_("'%s' is already locked, reason: %s"), | |
689 | av[0], old_reason); | |
690 | die(_("'%s' is already locked"), av[0]); | |
691 | } | |
692 | ||
693 | write_file(git_common_path("worktrees/%s/locked", wt->id), | |
694 | "%s", reason); | |
695 | free_worktrees(worktrees); | |
696 | return 0; | |
697 | } | |
698 | ||
6d308627 NTND |
699 | static int unlock_worktree(int ac, const char **av, const char *prefix) |
700 | { | |
701 | struct option options[] = { | |
702 | OPT_END() | |
703 | }; | |
704 | struct worktree **worktrees, *wt; | |
705 | int ret; | |
706 | ||
707 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
708 | if (ac != 1) | |
709 | usage_with_options(worktree_usage, options); | |
710 | ||
4fff1ef7 | 711 | worktrees = get_worktrees(0); |
6d308627 NTND |
712 | wt = find_worktree(worktrees, prefix, av[0]); |
713 | if (!wt) | |
714 | die(_("'%s' is not a working tree"), av[0]); | |
715 | if (is_main_worktree(wt)) | |
716 | die(_("The main working tree cannot be locked or unlocked")); | |
d236f12b | 717 | if (!worktree_lock_reason(wt)) |
6d308627 NTND |
718 | die(_("'%s' is not locked"), av[0]); |
719 | ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id)); | |
720 | free_worktrees(worktrees); | |
721 | return ret; | |
722 | } | |
723 | ||
78d986b2 NTND |
724 | static void validate_no_submodules(const struct worktree *wt) |
725 | { | |
726 | struct index_state istate = { NULL }; | |
727 | int i, found_submodules = 0; | |
728 | ||
bd0f7943 JH |
729 | if (read_index_from(&istate, worktree_git_path(wt, "index"), |
730 | get_worktree_git_dir(wt)) > 0) { | |
78d986b2 NTND |
731 | for (i = 0; i < istate.cache_nr; i++) { |
732 | struct cache_entry *ce = istate.cache[i]; | |
733 | ||
734 | if (S_ISGITLINK(ce->ce_mode)) { | |
735 | found_submodules = 1; | |
736 | break; | |
737 | } | |
738 | } | |
739 | } | |
740 | discard_index(&istate); | |
741 | ||
742 | if (found_submodules) | |
cc73385c | 743 | die(_("working trees containing submodules cannot be moved or removed")); |
78d986b2 NTND |
744 | } |
745 | ||
9f792bb4 NTND |
746 | static int move_worktree(int ac, const char **av, const char *prefix) |
747 | { | |
68a6b3a1 | 748 | int force = 0; |
9f792bb4 | 749 | struct option options[] = { |
68a6b3a1 ES |
750 | OPT__FORCE(&force, |
751 | N_("force move even if worktree is dirty or locked"), | |
752 | PARSE_OPT_NOCOMPLETE), | |
9f792bb4 NTND |
753 | OPT_END() |
754 | }; | |
755 | struct worktree **worktrees, *wt; | |
756 | struct strbuf dst = STRBUF_INIT; | |
757 | struct strbuf errmsg = STRBUF_INIT; | |
68a6b3a1 | 758 | const char *reason = NULL; |
9f792bb4 NTND |
759 | char *path; |
760 | ||
761 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
762 | if (ac != 2) | |
763 | usage_with_options(worktree_usage, options); | |
764 | ||
765 | path = prefix_filename(prefix, av[1]); | |
766 | strbuf_addstr(&dst, path); | |
767 | free(path); | |
768 | ||
769 | worktrees = get_worktrees(0); | |
770 | wt = find_worktree(worktrees, prefix, av[0]); | |
771 | if (!wt) | |
772 | die(_("'%s' is not a working tree"), av[0]); | |
773 | if (is_main_worktree(wt)) | |
774 | die(_("'%s' is a main working tree"), av[0]); | |
c64a8d20 NTND |
775 | if (is_directory(dst.buf)) { |
776 | const char *sep = find_last_dir_sep(wt->path); | |
777 | ||
778 | if (!sep) | |
779 | die(_("could not figure out destination name from '%s'"), | |
780 | wt->path); | |
781 | strbuf_trim_trailing_dir_sep(&dst); | |
782 | strbuf_addstr(&dst, sep); | |
783 | } | |
9f792bb4 | 784 | if (file_exists(dst.buf)) |
c64a8d20 | 785 | die(_("target '%s' already exists"), dst.buf); |
9f792bb4 | 786 | |
78d986b2 NTND |
787 | validate_no_submodules(wt); |
788 | ||
68a6b3a1 | 789 | if (force < 2) |
d236f12b | 790 | reason = worktree_lock_reason(wt); |
9f792bb4 NTND |
791 | if (reason) { |
792 | if (*reason) | |
68a6b3a1 | 793 | die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"), |
9f792bb4 | 794 | reason); |
68a6b3a1 | 795 | die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first")); |
9f792bb4 | 796 | } |
ee6763af | 797 | if (validate_worktree(wt, &errmsg, 0)) |
9f792bb4 NTND |
798 | die(_("validation failed, cannot move working tree: %s"), |
799 | errmsg.buf); | |
800 | strbuf_release(&errmsg); | |
801 | ||
802 | if (rename(wt->path, dst.buf) == -1) | |
803 | die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf); | |
804 | ||
805 | update_worktree_location(wt, dst.buf); | |
806 | ||
807 | strbuf_release(&dst); | |
808 | free_worktrees(worktrees); | |
809 | return 0; | |
810 | } | |
811 | ||
cc73385c NTND |
812 | /* |
813 | * Note, "git status --porcelain" is used to determine if it's safe to | |
814 | * delete a whole worktree. "git status" does not ignore user | |
815 | * configuration, so if a normal "git status" shows "clean" for the | |
816 | * user, then it's ok to remove it. | |
817 | * | |
818 | * This assumption may be a bad one. We may want to ignore | |
819 | * (potentially bad) user settings and only delete a worktree when | |
820 | * it's absolutely safe to do so from _our_ point of view because we | |
821 | * know better. | |
822 | */ | |
823 | static void check_clean_worktree(struct worktree *wt, | |
824 | const char *original_path) | |
825 | { | |
826 | struct argv_array child_env = ARGV_ARRAY_INIT; | |
827 | struct child_process cp; | |
828 | char buf[1]; | |
829 | int ret; | |
830 | ||
831 | /* | |
832 | * Until we sort this out, all submodules are "dirty" and | |
833 | * will abort this function. | |
834 | */ | |
835 | validate_no_submodules(wt); | |
836 | ||
837 | argv_array_pushf(&child_env, "%s=%s/.git", | |
838 | GIT_DIR_ENVIRONMENT, wt->path); | |
839 | argv_array_pushf(&child_env, "%s=%s", | |
840 | GIT_WORK_TREE_ENVIRONMENT, wt->path); | |
841 | memset(&cp, 0, sizeof(cp)); | |
842 | argv_array_pushl(&cp.args, "status", | |
843 | "--porcelain", "--ignore-submodules=none", | |
844 | NULL); | |
845 | cp.env = child_env.argv; | |
846 | cp.git_cmd = 1; | |
847 | cp.dir = wt->path; | |
848 | cp.out = -1; | |
849 | ret = start_command(&cp); | |
850 | if (ret) | |
851 | die_errno(_("failed to run 'git status' on '%s'"), | |
852 | original_path); | |
853 | ret = xread(cp.out, buf, sizeof(buf)); | |
854 | if (ret) | |
855 | die(_("'%s' is dirty, use --force to delete it"), | |
856 | original_path); | |
857 | close(cp.out); | |
858 | ret = finish_command(&cp); | |
859 | if (ret) | |
860 | die_errno(_("failed to run 'git status' on '%s', code %d"), | |
861 | original_path, ret); | |
862 | } | |
863 | ||
864 | static int delete_git_work_tree(struct worktree *wt) | |
865 | { | |
866 | struct strbuf sb = STRBUF_INIT; | |
867 | int ret = 0; | |
868 | ||
869 | strbuf_addstr(&sb, wt->path); | |
870 | if (remove_dir_recursively(&sb, 0)) { | |
871 | error_errno(_("failed to delete '%s'"), sb.buf); | |
872 | ret = -1; | |
873 | } | |
874 | strbuf_release(&sb); | |
875 | return ret; | |
876 | } | |
877 | ||
cc73385c NTND |
878 | static int remove_worktree(int ac, const char **av, const char *prefix) |
879 | { | |
880 | int force = 0; | |
881 | struct option options[] = { | |
d228eea5 | 882 | OPT__FORCE(&force, |
f4143101 | 883 | N_("force removal even if worktree is dirty or locked"), |
d228eea5 | 884 | PARSE_OPT_NOCOMPLETE), |
cc73385c NTND |
885 | OPT_END() |
886 | }; | |
887 | struct worktree **worktrees, *wt; | |
888 | struct strbuf errmsg = STRBUF_INIT; | |
f4143101 | 889 | const char *reason = NULL; |
cc73385c NTND |
890 | int ret = 0; |
891 | ||
892 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
893 | if (ac != 1) | |
894 | usage_with_options(worktree_usage, options); | |
895 | ||
896 | worktrees = get_worktrees(0); | |
897 | wt = find_worktree(worktrees, prefix, av[0]); | |
898 | if (!wt) | |
899 | die(_("'%s' is not a working tree"), av[0]); | |
900 | if (is_main_worktree(wt)) | |
901 | die(_("'%s' is a main working tree"), av[0]); | |
f4143101 | 902 | if (force < 2) |
d236f12b | 903 | reason = worktree_lock_reason(wt); |
cc73385c NTND |
904 | if (reason) { |
905 | if (*reason) | |
f4143101 | 906 | die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"), |
cc73385c | 907 | reason); |
f4143101 | 908 | die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first")); |
cc73385c | 909 | } |
ee6763af | 910 | if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK)) |
cc73385c NTND |
911 | die(_("validation failed, cannot remove working tree: %s"), |
912 | errmsg.buf); | |
913 | strbuf_release(&errmsg); | |
914 | ||
ee6763af NTND |
915 | if (file_exists(wt->path)) { |
916 | if (!force) | |
917 | check_clean_worktree(wt, av[0]); | |
cc73385c | 918 | |
ee6763af NTND |
919 | ret |= delete_git_work_tree(wt); |
920 | } | |
cc73385c NTND |
921 | /* |
922 | * continue on even if ret is non-zero, there's no going back | |
923 | * from here. | |
924 | */ | |
602aaed0 | 925 | ret |= delete_git_dir(wt->id); |
3a540433 | 926 | delete_worktrees_dir_if_empty(); |
cc73385c NTND |
927 | |
928 | free_worktrees(worktrees); | |
929 | return ret; | |
930 | } | |
931 | ||
df0b6cfb NTND |
932 | int cmd_worktree(int ac, const char **av, const char *prefix) |
933 | { | |
934 | struct option options[] = { | |
935 | OPT_END() | |
936 | }; | |
937 | ||
e92445a7 | 938 | git_config(git_worktree_config, NULL); |
d49028e6 | 939 | |
df0b6cfb NTND |
940 | if (ac < 2) |
941 | usage_with_options(worktree_usage, options); | |
0409e0b6 NTND |
942 | if (!prefix) |
943 | prefix = ""; | |
fc56361f ES |
944 | if (!strcmp(av[1], "add")) |
945 | return add(ac - 1, av + 1, prefix); | |
df0b6cfb NTND |
946 | if (!strcmp(av[1], "prune")) |
947 | return prune(ac - 1, av + 1, prefix); | |
bb9c03b8 MR |
948 | if (!strcmp(av[1], "list")) |
949 | return list(ac - 1, av + 1, prefix); | |
58142c09 NTND |
950 | if (!strcmp(av[1], "lock")) |
951 | return lock_worktree(ac - 1, av + 1, prefix); | |
6d308627 NTND |
952 | if (!strcmp(av[1], "unlock")) |
953 | return unlock_worktree(ac - 1, av + 1, prefix); | |
9f792bb4 NTND |
954 | if (!strcmp(av[1], "move")) |
955 | return move_worktree(ac - 1, av + 1, prefix); | |
cc73385c NTND |
956 | if (!strcmp(av[1], "remove")) |
957 | return remove_worktree(ac - 1, av + 1, prefix); | |
df0b6cfb NTND |
958 | usage_with_options(worktree_usage, options); |
959 | } |