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