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