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