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