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