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