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