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