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