]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/worktree.c
Convert find_unique_abbrev* to struct object_id
[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"
fc56361f 7#include "argv-array.h"
f7c9dac1
ES
8#include "branch.h"
9#include "refs.h"
fc56361f 10#include "run-command.h"
b979d950 11#include "sigchain.h"
799767cc 12#include "refs.h"
bb9c03b8
MR
13#include "utf8.h"
14#include "worktree.h"
df0b6cfb
NTND
15
16static const char * const worktree_usage[] = {
b780e440 17 N_("git worktree add [<options>] <path> [<commit-ish>]"),
bb9c03b8 18 N_("git worktree list [<options>]"),
58142c09 19 N_("git worktree lock [<options>] <path>"),
7b722d90 20 N_("git worktree prune [<options>]"),
6d308627 21 N_("git worktree unlock <path>"),
df0b6cfb
NTND
22 NULL
23};
24
5dd6e234
ES
25struct add_opts {
26 int force;
27 int detach;
ef2a0ac9 28 int checkout;
507e6e9e 29 int keep_locked;
5dd6e234
ES
30 const char *new_branch;
31 int force_new_branch;
32};
33
df0b6cfb
NTND
34static int show_only;
35static int verbose;
e92445a7 36static int guess_remote;
dddbad72 37static timestamp_t expire;
df0b6cfb 38
e92445a7
TG
39static int git_worktree_config(const char *var, const char *value, void *cb)
40{
41 if (!strcmp(var, "worktree.guessremote")) {
42 guess_remote = git_config_bool(var, value);
43 return 0;
44 }
45
46 return git_default_config(var, value, cb);
47}
48
df0b6cfb
NTND
49static int prune_worktree(const char *id, struct strbuf *reason)
50{
51 struct stat st;
52 char *path;
228740b6
JK
53 int fd;
54 size_t len;
8a1a8d2a 55 ssize_t read_result;
df0b6cfb
NTND
56
57 if (!is_directory(git_path("worktrees/%s", id))) {
58 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
59 return 1;
60 }
61 if (file_exists(git_path("worktrees/%s/locked", id)))
62 return 0;
63 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
64 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
65 return 1;
66 }
67 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
68 if (fd < 0) {
69 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
70 id, strerror(errno));
71 return 1;
72 }
228740b6 73 len = xsize_t(st.st_size);
3733e694 74 path = xmallocz(len);
8a1a8d2a
JK
75
76 read_result = read_in_full(fd, path, len);
77 if (read_result < 0) {
78 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
79 id, strerror(errno));
80 close(fd);
81 free(path);
82 return 1;
83 }
df0b6cfb 84 close(fd);
8a1a8d2a
JK
85
86 if (read_result != len) {
87 strbuf_addf(reason,
88 _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
89 id, (uintmax_t)len, (uintmax_t)read_result);
90 free(path);
91 return 1;
92 }
df0b6cfb
NTND
93 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
94 len--;
95 if (!len) {
96 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
97 free(path);
98 return 1;
99 }
100 path[len] = '\0';
101 if (!file_exists(path)) {
102 struct stat st_link;
103 free(path);
104 /*
105 * the repo is moved manually and has not been
106 * accessed since?
107 */
108 if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
109 st_link.st_nlink > 1)
110 return 0;
111 if (st.st_mtime <= expire) {
112 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
113 return 1;
114 } else {
115 return 0;
116 }
117 }
118 free(path);
119 return 0;
120}
121
122static void prune_worktrees(void)
123{
124 struct strbuf reason = STRBUF_INIT;
125 struct strbuf path = STRBUF_INIT;
126 DIR *dir = opendir(git_path("worktrees"));
127 struct dirent *d;
128 int ret;
129 if (!dir)
130 return;
131 while ((d = readdir(dir)) != NULL) {
afb9e30b 132 if (is_dot_or_dotdot(d->d_name))
df0b6cfb
NTND
133 continue;
134 strbuf_reset(&reason);
135 if (!prune_worktree(d->d_name, &reason))
136 continue;
137 if (show_only || verbose)
138 printf("%s\n", reason.buf);
139 if (show_only)
140 continue;
8c2ca3a6 141 git_path_buf(&path, "worktrees/%s", d->d_name);
df0b6cfb
NTND
142 ret = remove_dir_recursively(&path, 0);
143 if (ret < 0 && errno == ENOTDIR)
144 ret = unlink(path.buf);
145 if (ret)
8d19e930 146 error_errno(_("failed to remove '%s'"), path.buf);
df0b6cfb
NTND
147 }
148 closedir(dir);
149 if (!show_only)
150 rmdir(git_path("worktrees"));
151 strbuf_release(&reason);
152 strbuf_release(&path);
153}
154
155static int prune(int ac, const char **av, const char *prefix)
156{
157 struct option options[] = {
158 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
2488dcab 159 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
df0b6cfb 160 OPT_EXPIRY_DATE(0, "expire", &expire,
2488dcab 161 N_("expire working trees older than <time>")),
df0b6cfb
NTND
162 OPT_END()
163 };
164
dddbad72 165 expire = TIME_MAX;
df0b6cfb
NTND
166 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
167 if (ac)
168 usage_with_options(worktree_usage, options);
169 prune_worktrees();
170 return 0;
171}
172
b979d950
ES
173static char *junk_work_tree;
174static char *junk_git_dir;
175static int is_junk;
176static pid_t junk_pid;
177
178static void remove_junk(void)
179{
180 struct strbuf sb = STRBUF_INIT;
181 if (!is_junk || getpid() != junk_pid)
182 return;
183 if (junk_git_dir) {
184 strbuf_addstr(&sb, junk_git_dir);
185 remove_dir_recursively(&sb, 0);
186 strbuf_reset(&sb);
187 }
188 if (junk_work_tree) {
189 strbuf_addstr(&sb, junk_work_tree);
190 remove_dir_recursively(&sb, 0);
191 }
192 strbuf_release(&sb);
193}
194
195static void remove_junk_on_signal(int signo)
196{
197 remove_junk();
198 sigchain_pop(signo);
199 raise(signo);
200}
201
f5682b2a
ES
202static const char *worktree_basename(const char *path, int *olen)
203{
204 const char *name;
205 int len;
206
207 len = strlen(path);
208 while (len && is_dir_sep(path[len - 1]))
209 len--;
210
211 for (name = path + len - 1; name > path; name--)
212 if (is_dir_sep(*name)) {
213 name++;
214 break;
215 }
216
217 *olen = len;
218 return name;
219}
220
80a0548f 221static int add_worktree(const char *path, const char *refname,
5dd6e234 222 const struct add_opts *opts)
b979d950
ES
223{
224 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
225 struct strbuf sb = STRBUF_INIT;
226 const char *name;
227 struct stat st;
542aa25d 228 struct child_process cp = CHILD_PROCESS_INIT;
ae2a3827 229 struct argv_array child_env = ARGV_ARRAY_INIT;
b979d950 230 int counter = 0, len, ret;
f7c9dac1
ES
231 struct strbuf symref = STRBUF_INIT;
232 struct commit *commit = NULL;
ade546be 233 int is_branch = 0;
b979d950
ES
234
235 if (file_exists(path) && !is_empty_dir(path))
236 die(_("'%s' already exists"), path);
237
f7c9dac1 238 /* is 'refname' a branch or commit? */
0ebf4a2a 239 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
ade546be
ES
240 ref_exists(symref.buf)) {
241 is_branch = 1;
f7c9dac1 242 if (!opts->force)
8d9fdd70 243 die_if_checked_out(symref.buf, 0);
f7c9dac1 244 }
ade546be
ES
245 commit = lookup_commit_reference_by_name(refname);
246 if (!commit)
247 die(_("invalid reference: %s"), refname);
f7c9dac1 248
f5682b2a 249 name = worktree_basename(path, &len);
8c2ca3a6 250 git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name);
b979d950
ES
251 len = sb_repo.len;
252 if (safe_create_leading_directories_const(sb_repo.buf))
253 die_errno(_("could not create leading directories of '%s'"),
254 sb_repo.buf);
255 while (!stat(sb_repo.buf, &st)) {
256 counter++;
257 strbuf_setlen(&sb_repo, len);
258 strbuf_addf(&sb_repo, "%d", counter);
259 }
260 name = strrchr(sb_repo.buf, '/') + 1;
261
262 junk_pid = getpid();
263 atexit(remove_junk);
264 sigchain_push_common(remove_junk_on_signal);
265
266 if (mkdir(sb_repo.buf, 0777))
267 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
268 junk_git_dir = xstrdup(sb_repo.buf);
269 is_junk = 1;
270
271 /*
272 * lock the incomplete repo so prune won't delete it, unlock
273 * after the preparation is over.
274 */
275 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
507e6e9e
NTND
276 if (!opts->keep_locked)
277 write_file(sb.buf, "initializing");
278 else
279 write_file(sb.buf, "added with --lock");
b979d950
ES
280
281 strbuf_addf(&sb_git, "%s/.git", path);
282 if (safe_create_leading_directories_const(sb_git.buf))
283 die_errno(_("could not create leading directories of '%s'"),
284 sb_git.buf);
285 junk_work_tree = xstrdup(path);
286
287 strbuf_reset(&sb);
288 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
1f76a10b
JH
289 write_file(sb.buf, "%s", real_path(sb_git.buf));
290 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
b979d950
ES
291 real_path(get_git_common_dir()), name);
292 /*
293 * This is to keep resolve_ref() happy. We need a valid HEAD
ed197a6a
ES
294 * or is_git_directory() will reject the directory. Any value which
295 * looks like an object ID will do since it will be immediately
296 * replaced by the symbolic-ref or update-ref invocation in the new
297 * worktree.
b979d950 298 */
b979d950
ES
299 strbuf_reset(&sb);
300 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
dabd35f4 301 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
b979d950
ES
302 strbuf_reset(&sb);
303 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
1f76a10b 304 write_file(sb.buf, "../..");
b979d950 305
cd2f4713 306 fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
b979d950 307
ae2a3827
ES
308 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
309 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
b979d950 310 cp.git_cmd = 1;
7f44e3d1 311
ade546be 312 if (!is_branch)
7f44e3d1 313 argv_array_pushl(&cp.args, "update-ref", "HEAD",
f2fd0760 314 oid_to_hex(&commit->object.oid), NULL);
7f44e3d1
ES
315 else
316 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
317 symref.buf, NULL);
318 cp.env = child_env.argv;
319 ret = run_command(&cp);
320 if (ret)
321 goto done;
322
ef2a0ac9
RZ
323 if (opts->checkout) {
324 cp.argv = NULL;
325 argv_array_clear(&cp.args);
326 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
327 cp.env = child_env.argv;
328 ret = run_command(&cp);
329 if (ret)
330 goto done;
b979d950 331 }
ef2a0ac9
RZ
332
333 is_junk = 0;
88ce3ef6
ÆAB
334 FREE_AND_NULL(junk_work_tree);
335 FREE_AND_NULL(junk_git_dir);
ef2a0ac9 336
7f44e3d1 337done:
507e6e9e
NTND
338 if (ret || !opts->keep_locked) {
339 strbuf_reset(&sb);
340 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
341 unlink_or_warn(sb.buf);
342 }
ade546be
ES
343
344 /*
345 * Hook failure does not warrant worktree deletion, so run hook after
346 * is_junk is cleared, but do return appropriate code when hook fails.
347 */
a4bf1e3c
ES
348 if (!ret && opts->checkout) {
349 const char *hook = find_hook("post-checkout");
350 if (hook) {
351 const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL };
352 cp.git_cmd = 0;
353 cp.no_stdin = 1;
354 cp.stdout_to_stderr = 1;
355 cp.dir = path;
356 cp.env = env;
357 cp.argv = NULL;
358 argv_array_pushl(&cp.args, absolute_path(hook),
359 oid_to_hex(&null_oid),
360 oid_to_hex(&commit->object.oid),
361 "1", NULL);
362 ret = run_command(&cp);
363 }
364 }
ade546be 365
ae2a3827 366 argv_array_clear(&child_env);
b979d950 367 strbuf_release(&sb);
f7c9dac1 368 strbuf_release(&symref);
b979d950
ES
369 strbuf_release(&sb_repo);
370 strbuf_release(&sb_git);
371 return ret;
372}
373
fc56361f
ES
374static int add(int ac, const char **av, const char *prefix)
375{
5dd6e234
ES
376 struct add_opts opts;
377 const char *new_branch_force = NULL;
e4da43b1
JK
378 char *path;
379 const char *branch;
e284e892 380 const char *opt_track = NULL;
fc56361f 381 struct option options[] = {
5dd6e234
ES
382 OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
383 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
cbdf60fa
ES
384 N_("create a new branch")),
385 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
386 N_("create or reset a branch")),
5dd6e234 387 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
ef2a0ac9 388 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
507e6e9e 389 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
e284e892
TG
390 OPT_PASSTHRU(0, "track", &opt_track, NULL,
391 N_("set up tracking mode (see git-branch(1))"),
392 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
71d6682d
TG
393 OPT_BOOL(0, "guess-remote", &guess_remote,
394 N_("try to match the new branch name with a remote-tracking branch")),
fc56361f
ES
395 OPT_END()
396 };
397
5dd6e234 398 memset(&opts, 0, sizeof(opts));
ef2a0ac9 399 opts.checkout = 1;
fc56361f 400 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
ab0b2c53
ES
401 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
402 die(_("-b, -B, and --detach are mutually exclusive"));
0f4af3b9
ES
403 if (ac < 1 || ac > 2)
404 usage_with_options(worktree_usage, options);
fc56361f 405
116fb64e 406 path = prefix_filename(prefix, av[0]);
0f4af3b9 407 branch = ac < 2 ? "HEAD" : av[1];
fc56361f 408
1a450e2f
JDG
409 if (!strcmp(branch, "-"))
410 branch = "@{-1}";
411
5dd6e234 412 opts.force_new_branch = !!new_branch_force;
beb6f24b
NTND
413 if (opts.force_new_branch) {
414 struct strbuf symref = STRBUF_INIT;
415
5dd6e234 416 opts.new_branch = new_branch_force;
eef005dc 417
beb6f24b
NTND
418 if (!opts.force &&
419 !strbuf_check_branch_ref(&symref, opts.new_branch) &&
420 ref_exists(symref.buf))
8d9fdd70 421 die_if_checked_out(symref.buf, 0);
beb6f24b
NTND
422 strbuf_release(&symref);
423 }
424
5c942570 425 if (ac < 2 && !opts.new_branch && !opts.detach) {
1eb07d82
ES
426 int n;
427 const char *s = worktree_basename(path, &n);
5dd6e234 428 opts.new_branch = xstrndup(s, n);
71d6682d
TG
429 if (guess_remote) {
430 struct object_id oid;
431 const char *remote =
432 unique_tracking_name(opts.new_branch, &oid);
433 if (remote)
434 branch = remote;
435 }
1eb07d82
ES
436 }
437
4e853331
TG
438 if (ac == 2 && !opts.new_branch && !opts.detach) {
439 struct object_id oid;
440 struct commit *commit;
441 const char *remote;
442
443 commit = lookup_commit_reference_by_name(branch);
444 if (!commit) {
445 remote = unique_tracking_name(branch, &oid);
446 if (remote) {
447 opts.new_branch = branch;
448 branch = remote;
449 }
450 }
451 }
452
c2842439 453 if (opts.new_branch) {
542aa25d 454 struct child_process cp = CHILD_PROCESS_INIT;
c2842439
ES
455 cp.git_cmd = 1;
456 argv_array_push(&cp.args, "branch");
457 if (opts.force_new_branch)
458 argv_array_push(&cp.args, "--force");
459 argv_array_push(&cp.args, opts.new_branch);
460 argv_array_push(&cp.args, branch);
e284e892
TG
461 if (opt_track)
462 argv_array_push(&cp.args, opt_track);
c2842439
ES
463 if (run_command(&cp))
464 return -1;
465 branch = opts.new_branch;
e284e892
TG
466 } else if (opt_track) {
467 die(_("--[no-]track can only be used if a new branch is created"));
1eb07d82
ES
468 }
469
0e5bba53
JK
470 UNLEAK(path);
471 UNLEAK(opts);
80a0548f 472 return add_worktree(path, branch, &opts);
fc56361f
ES
473}
474
bb9c03b8
MR
475static void show_worktree_porcelain(struct worktree *wt)
476{
477 printf("worktree %s\n", wt->path);
478 if (wt->is_bare)
479 printf("bare\n");
480 else {
0f05154c 481 printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
bb9c03b8
MR
482 if (wt->is_detached)
483 printf("detached\n");
a234563a 484 else if (wt->head_ref)
bb9c03b8
MR
485 printf("branch %s\n", wt->head_ref);
486 }
487 printf("\n");
488}
489
490static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
491{
492 struct strbuf sb = STRBUF_INIT;
493 int cur_path_len = strlen(wt->path);
494 int path_adj = cur_path_len - utf8_strwidth(wt->path);
495
496 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
497 if (wt->is_bare)
498 strbuf_addstr(&sb, "(bare)");
499 else {
500 strbuf_addf(&sb, "%-*s ", abbrev_len,
aab9583f 501 find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV));
96f09e2a 502 if (wt->is_detached)
bb9c03b8 503 strbuf_addstr(&sb, "(detached HEAD)");
2e11f58f
JS
504 else if (wt->head_ref) {
505 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
506 strbuf_addf(&sb, "[%s]", ref);
507 free(ref);
508 } else
a234563a 509 strbuf_addstr(&sb, "(error)");
bb9c03b8
MR
510 }
511 printf("%s\n", sb.buf);
512
513 strbuf_release(&sb);
514}
515
516static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
517{
518 int i;
519
520 for (i = 0; wt[i]; i++) {
521 int sha1_len;
522 int path_len = strlen(wt[i]->path);
523
524 if (path_len > *maxlen)
525 *maxlen = path_len;
aab9583f 526 sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev));
bb9c03b8
MR
527 if (sha1_len > *abbrev)
528 *abbrev = sha1_len;
529 }
530}
531
532static int list(int ac, const char **av, const char *prefix)
533{
534 int porcelain = 0;
535
536 struct option options[] = {
537 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
538 OPT_END()
539 };
540
541 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
542 if (ac)
543 usage_with_options(worktree_usage, options);
544 else {
4df1d4d4 545 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
bb9c03b8
MR
546 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
547
548 if (!porcelain)
549 measure_widths(worktrees, &abbrev, &path_maxlen);
550
551 for (i = 0; worktrees[i]; i++) {
552 if (porcelain)
553 show_worktree_porcelain(worktrees[i]);
554 else
555 show_worktree(worktrees[i], path_maxlen, abbrev);
556 }
557 free_worktrees(worktrees);
558 }
559 return 0;
560}
561
58142c09
NTND
562static int lock_worktree(int ac, const char **av, const char *prefix)
563{
564 const char *reason = "", *old_reason;
565 struct option options[] = {
566 OPT_STRING(0, "reason", &reason, N_("string"),
567 N_("reason for locking")),
568 OPT_END()
569 };
570 struct worktree **worktrees, *wt;
571
572 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
573 if (ac != 1)
574 usage_with_options(worktree_usage, options);
575
4fff1ef7 576 worktrees = get_worktrees(0);
58142c09
NTND
577 wt = find_worktree(worktrees, prefix, av[0]);
578 if (!wt)
579 die(_("'%s' is not a working tree"), av[0]);
580 if (is_main_worktree(wt))
581 die(_("The main working tree cannot be locked or unlocked"));
582
583 old_reason = is_worktree_locked(wt);
584 if (old_reason) {
585 if (*old_reason)
586 die(_("'%s' is already locked, reason: %s"),
587 av[0], old_reason);
588 die(_("'%s' is already locked"), av[0]);
589 }
590
591 write_file(git_common_path("worktrees/%s/locked", wt->id),
592 "%s", reason);
593 free_worktrees(worktrees);
594 return 0;
595}
596
6d308627
NTND
597static int unlock_worktree(int ac, const char **av, const char *prefix)
598{
599 struct option options[] = {
600 OPT_END()
601 };
602 struct worktree **worktrees, *wt;
603 int ret;
604
605 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
606 if (ac != 1)
607 usage_with_options(worktree_usage, options);
608
4fff1ef7 609 worktrees = get_worktrees(0);
6d308627
NTND
610 wt = find_worktree(worktrees, prefix, av[0]);
611 if (!wt)
612 die(_("'%s' is not a working tree"), av[0]);
613 if (is_main_worktree(wt))
614 die(_("The main working tree cannot be locked or unlocked"));
615 if (!is_worktree_locked(wt))
616 die(_("'%s' is not locked"), av[0]);
617 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
618 free_worktrees(worktrees);
619 return ret;
620}
621
df0b6cfb
NTND
622int cmd_worktree(int ac, const char **av, const char *prefix)
623{
624 struct option options[] = {
625 OPT_END()
626 };
627
e92445a7 628 git_config(git_worktree_config, NULL);
d49028e6 629
df0b6cfb
NTND
630 if (ac < 2)
631 usage_with_options(worktree_usage, options);
0409e0b6
NTND
632 if (!prefix)
633 prefix = "";
fc56361f
ES
634 if (!strcmp(av[1], "add"))
635 return add(ac - 1, av + 1, prefix);
df0b6cfb
NTND
636 if (!strcmp(av[1], "prune"))
637 return prune(ac - 1, av + 1, prefix);
bb9c03b8
MR
638 if (!strcmp(av[1], "list"))
639 return list(ac - 1, av + 1, prefix);
58142c09
NTND
640 if (!strcmp(av[1], "lock"))
641 return lock_worktree(ac - 1, av + 1, prefix);
6d308627
NTND
642 if (!strcmp(av[1], "unlock"))
643 return unlock_worktree(ac - 1, av + 1, prefix);
df0b6cfb
NTND
644 usage_with_options(worktree_usage, options);
645}