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