]>
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" | |
dbbcd44f | 7 | #include "strvec.h" |
f7c9dac1 ES |
8 | #include "branch.h" |
9 | #include "refs.h" | |
fc56361f | 10 | #include "run-command.h" |
b979d950 | 11 | #include "sigchain.h" |
00a6d4d1 | 12 | #include "submodule.h" |
bb9c03b8 MR |
13 | #include "utf8.h" |
14 | #include "worktree.h" | |
862c723d | 15 | #include "quote.h" |
df0b6cfb NTND |
16 | |
17 | static const char * const worktree_usage[] = { | |
b780e440 | 18 | N_("git worktree add [<options>] <path> [<commit-ish>]"), |
bb9c03b8 | 19 | N_("git worktree list [<options>]"), |
58142c09 | 20 | N_("git worktree lock [<options>] <path>"), |
9f792bb4 | 21 | N_("git worktree move <worktree> <new-path>"), |
7b722d90 | 22 | N_("git worktree prune [<options>]"), |
cc73385c | 23 | N_("git worktree remove [<options>] <worktree>"), |
6d308627 | 24 | N_("git worktree unlock <path>"), |
df0b6cfb NTND |
25 | NULL |
26 | }; | |
27 | ||
5dd6e234 ES |
28 | struct add_opts { |
29 | int force; | |
30 | int detach; | |
371979c2 | 31 | int quiet; |
ef2a0ac9 | 32 | int checkout; |
507e6e9e | 33 | int keep_locked; |
5dd6e234 ES |
34 | }; |
35 | ||
df0b6cfb NTND |
36 | static int show_only; |
37 | static int verbose; | |
e92445a7 | 38 | static int guess_remote; |
dddbad72 | 39 | static timestamp_t expire; |
df0b6cfb | 40 | |
e92445a7 TG |
41 | static int git_worktree_config(const char *var, const char *value, void *cb) |
42 | { | |
43 | if (!strcmp(var, "worktree.guessremote")) { | |
44 | guess_remote = git_config_bool(var, value); | |
45 | return 0; | |
46 | } | |
47 | ||
48 | return git_default_config(var, value, cb); | |
49 | } | |
50 | ||
602aaed0 | 51 | static int delete_git_dir(const char *id) |
e5353bef ES |
52 | { |
53 | struct strbuf sb = STRBUF_INIT; | |
602aaed0 | 54 | int ret; |
e5353bef | 55 | |
602aaed0 ES |
56 | strbuf_addstr(&sb, git_common_path("worktrees/%s", id)); |
57 | ret = remove_dir_recursively(&sb, 0); | |
58 | if (ret < 0 && errno == ENOTDIR) | |
59 | ret = unlink(sb.buf); | |
60 | if (ret) | |
e5353bef | 61 | error_errno(_("failed to delete '%s'"), sb.buf); |
e5353bef ES |
62 | strbuf_release(&sb); |
63 | return ret; | |
64 | } | |
65 | ||
3a540433 ES |
66 | static void delete_worktrees_dir_if_empty(void) |
67 | { | |
68 | rmdir(git_path("worktrees")); /* ignore failed removal */ | |
69 | } | |
70 | ||
dd9609a1 ES |
71 | static void prune_worktree(const char *id, const char *reason) |
72 | { | |
73 | if (show_only || verbose) | |
74 | printf_ln(_("Removing %s/%s: %s"), "worktrees", id, reason); | |
75 | if (!show_only) | |
76 | delete_git_dir(id); | |
77 | } | |
78 | ||
4a3ce479 ES |
79 | static int prune_cmp(const void *a, const void *b) |
80 | { | |
81 | const struct string_list_item *x = a; | |
82 | const struct string_list_item *y = b; | |
83 | int c; | |
84 | ||
85 | if ((c = fspathcmp(x->string, y->string))) | |
86 | return c; | |
916133ef ES |
87 | /* |
88 | * paths same; prune_dupes() removes all but the first worktree entry | |
89 | * having the same path, so sort main worktree ('util' is NULL) above | |
90 | * linked worktrees ('util' not NULL) since main worktree can't be | |
91 | * removed | |
92 | */ | |
93 | if (!x->util) | |
94 | return -1; | |
95 | if (!y->util) | |
96 | return 1; | |
4a3ce479 ES |
97 | /* paths same; sort by .git/worktrees/<id> */ |
98 | return strcmp(x->util, y->util); | |
99 | } | |
100 | ||
101 | static void prune_dups(struct string_list *l) | |
102 | { | |
103 | int i; | |
104 | ||
105 | QSORT(l->items, l->nr, prune_cmp); | |
106 | for (i = 1; i < l->nr; i++) { | |
107 | if (!fspathcmp(l->items[i].string, l->items[i - 1].string)) | |
108 | prune_worktree(l->items[i].util, "duplicate entry"); | |
109 | } | |
110 | } | |
111 | ||
df0b6cfb NTND |
112 | static void prune_worktrees(void) |
113 | { | |
114 | struct strbuf reason = STRBUF_INIT; | |
916133ef | 115 | struct strbuf main_path = STRBUF_INIT; |
4a3ce479 | 116 | struct string_list kept = STRING_LIST_INIT_NODUP; |
df0b6cfb NTND |
117 | DIR *dir = opendir(git_path("worktrees")); |
118 | struct dirent *d; | |
df0b6cfb NTND |
119 | if (!dir) |
120 | return; | |
b548f0f1 | 121 | while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) { |
4a3ce479 | 122 | char *path; |
df0b6cfb | 123 | strbuf_reset(&reason); |
a29a8b75 | 124 | if (should_prune_worktree(d->d_name, &reason, &path, expire)) |
4a3ce479 ES |
125 | prune_worktree(d->d_name, reason.buf); |
126 | else if (path) | |
127 | string_list_append(&kept, path)->util = xstrdup(d->d_name); | |
df0b6cfb NTND |
128 | } |
129 | closedir(dir); | |
4a3ce479 | 130 | |
916133ef ES |
131 | strbuf_add_absolute_path(&main_path, get_git_common_dir()); |
132 | /* massage main worktree absolute path to match 'gitdir' content */ | |
133 | strbuf_strip_suffix(&main_path, "/."); | |
134 | string_list_append(&kept, strbuf_detach(&main_path, NULL)); | |
4a3ce479 ES |
135 | prune_dups(&kept); |
136 | string_list_clear(&kept, 1); | |
137 | ||
df0b6cfb | 138 | if (!show_only) |
3a540433 | 139 | delete_worktrees_dir_if_empty(); |
df0b6cfb | 140 | strbuf_release(&reason); |
df0b6cfb NTND |
141 | } |
142 | ||
143 | static int prune(int ac, const char **av, const char *prefix) | |
144 | { | |
145 | struct option options[] = { | |
146 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), | |
2488dcab | 147 | OPT__VERBOSE(&verbose, N_("report pruned working trees")), |
df0b6cfb | 148 | OPT_EXPIRY_DATE(0, "expire", &expire, |
2488dcab | 149 | N_("expire working trees older than <time>")), |
df0b6cfb NTND |
150 | OPT_END() |
151 | }; | |
152 | ||
dddbad72 | 153 | expire = TIME_MAX; |
df0b6cfb NTND |
154 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
155 | if (ac) | |
156 | usage_with_options(worktree_usage, options); | |
157 | prune_worktrees(); | |
158 | return 0; | |
159 | } | |
160 | ||
b979d950 ES |
161 | static char *junk_work_tree; |
162 | static char *junk_git_dir; | |
163 | static int is_junk; | |
164 | static pid_t junk_pid; | |
165 | ||
166 | static void remove_junk(void) | |
167 | { | |
168 | struct strbuf sb = STRBUF_INIT; | |
169 | if (!is_junk || getpid() != junk_pid) | |
170 | return; | |
171 | if (junk_git_dir) { | |
172 | strbuf_addstr(&sb, junk_git_dir); | |
173 | remove_dir_recursively(&sb, 0); | |
174 | strbuf_reset(&sb); | |
175 | } | |
176 | if (junk_work_tree) { | |
177 | strbuf_addstr(&sb, junk_work_tree); | |
178 | remove_dir_recursively(&sb, 0); | |
179 | } | |
180 | strbuf_release(&sb); | |
181 | } | |
182 | ||
183 | static void remove_junk_on_signal(int signo) | |
184 | { | |
185 | remove_junk(); | |
186 | sigchain_pop(signo); | |
187 | raise(signo); | |
188 | } | |
189 | ||
f5682b2a ES |
190 | static const char *worktree_basename(const char *path, int *olen) |
191 | { | |
192 | const char *name; | |
193 | int len; | |
194 | ||
195 | len = strlen(path); | |
196 | while (len && is_dir_sep(path[len - 1])) | |
197 | len--; | |
198 | ||
199 | for (name = path + len - 1; name > path; name--) | |
200 | if (is_dir_sep(*name)) { | |
201 | name++; | |
202 | break; | |
203 | } | |
204 | ||
205 | *olen = len; | |
206 | return name; | |
207 | } | |
208 | ||
d179af67 ES |
209 | /* check that path is viable location for worktree */ |
210 | static void check_candidate_path(const char *path, | |
211 | int force, | |
212 | struct worktree **worktrees, | |
213 | const char *cmd) | |
45059e64 | 214 | { |
cb56f55c ES |
215 | struct worktree *wt; |
216 | int locked; | |
217 | ||
45059e64 ES |
218 | if (file_exists(path) && !is_empty_dir(path)) |
219 | die(_("'%s' already exists"), path); | |
cb56f55c | 220 | |
bb69b3b0 | 221 | wt = find_worktree_by_path(worktrees, path); |
cb56f55c | 222 | if (!wt) |
d179af67 | 223 | return; |
cb56f55c | 224 | |
d236f12b | 225 | locked = !!worktree_lock_reason(wt); |
d179af67 | 226 | if ((!locked && force) || (locked && force > 1)) { |
e19831c9 | 227 | if (delete_git_dir(wt->id)) |
d179af67 ES |
228 | die(_("unusable worktree destination '%s'"), path); |
229 | return; | |
e19831c9 ES |
230 | } |
231 | ||
cb56f55c | 232 | if (locked) |
b86339b1 | 233 | die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path, cmd); |
cb56f55c | 234 | else |
b86339b1 | 235 | die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path, cmd); |
45059e64 ES |
236 | } |
237 | ||
80a0548f | 238 | static int add_worktree(const char *path, const char *refname, |
5dd6e234 | 239 | const struct add_opts *opts) |
b979d950 ES |
240 | { |
241 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; | |
3d7747e3 | 242 | struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT; |
b979d950 | 243 | const char *name; |
542aa25d | 244 | struct child_process cp = CHILD_PROCESS_INIT; |
22f9b7f3 | 245 | struct strvec child_env = STRVEC_INIT; |
7af01f23 MS |
246 | unsigned int counter = 0; |
247 | int len, ret; | |
f7c9dac1 ES |
248 | struct strbuf symref = STRBUF_INIT; |
249 | struct commit *commit = NULL; | |
ade546be | 250 | int is_branch = 0; |
1de16aec | 251 | struct strbuf sb_name = STRBUF_INIT; |
d179af67 | 252 | struct worktree **worktrees; |
b979d950 | 253 | |
03f2465b | 254 | worktrees = get_worktrees(); |
d179af67 ES |
255 | check_candidate_path(path, opts->force, worktrees, "add"); |
256 | free_worktrees(worktrees); | |
257 | worktrees = NULL; | |
b979d950 | 258 | |
f7c9dac1 | 259 | /* is 'refname' a branch or commit? */ |
0ebf4a2a | 260 | if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && |
ade546be ES |
261 | ref_exists(symref.buf)) { |
262 | is_branch = 1; | |
f7c9dac1 | 263 | if (!opts->force) |
8d9fdd70 | 264 | die_if_checked_out(symref.buf, 0); |
f7c9dac1 | 265 | } |
ade546be ES |
266 | commit = lookup_commit_reference_by_name(refname); |
267 | if (!commit) | |
268 | die(_("invalid reference: %s"), refname); | |
f7c9dac1 | 269 | |
f5682b2a | 270 | name = worktree_basename(path, &len); |
1de16aec NTND |
271 | strbuf_add(&sb, name, path + len - name); |
272 | sanitize_refname_component(sb.buf, &sb_name); | |
273 | if (!sb_name.len) | |
274 | BUG("How come '%s' becomes empty after sanitization?", sb.buf); | |
275 | strbuf_reset(&sb); | |
276 | name = sb_name.buf; | |
277 | git_path_buf(&sb_repo, "worktrees/%s", name); | |
b979d950 ES |
278 | len = sb_repo.len; |
279 | if (safe_create_leading_directories_const(sb_repo.buf)) | |
280 | die_errno(_("could not create leading directories of '%s'"), | |
281 | sb_repo.buf); | |
7af01f23 MS |
282 | |
283 | while (mkdir(sb_repo.buf, 0777)) { | |
b979d950 | 284 | counter++; |
7af01f23 MS |
285 | if ((errno != EEXIST) || !counter /* overflow */) |
286 | die_errno(_("could not create directory of '%s'"), | |
287 | sb_repo.buf); | |
b979d950 ES |
288 | strbuf_setlen(&sb_repo, len); |
289 | strbuf_addf(&sb_repo, "%d", counter); | |
290 | } | |
291 | name = strrchr(sb_repo.buf, '/') + 1; | |
292 | ||
293 | junk_pid = getpid(); | |
294 | atexit(remove_junk); | |
295 | sigchain_push_common(remove_junk_on_signal); | |
296 | ||
b979d950 ES |
297 | junk_git_dir = xstrdup(sb_repo.buf); |
298 | is_junk = 1; | |
299 | ||
300 | /* | |
301 | * lock the incomplete repo so prune won't delete it, unlock | |
302 | * after the preparation is over. | |
303 | */ | |
304 | strbuf_addf(&sb, "%s/locked", sb_repo.buf); | |
507e6e9e | 305 | if (!opts->keep_locked) |
f7c35ea2 | 306 | write_file(sb.buf, _("initializing")); |
507e6e9e | 307 | else |
f7c35ea2 | 308 | write_file(sb.buf, _("added with --lock")); |
b979d950 ES |
309 | |
310 | strbuf_addf(&sb_git, "%s/.git", path); | |
311 | if (safe_create_leading_directories_const(sb_git.buf)) | |
312 | die_errno(_("could not create leading directories of '%s'"), | |
313 | sb_git.buf); | |
314 | junk_work_tree = xstrdup(path); | |
315 | ||
316 | strbuf_reset(&sb); | |
317 | strbuf_addf(&sb, "%s/gitdir", sb_repo.buf); | |
3d7747e3 AM |
318 | strbuf_realpath(&realpath, sb_git.buf, 1); |
319 | write_file(sb.buf, "%s", realpath.buf); | |
320 | strbuf_realpath(&realpath, get_git_common_dir(), 1); | |
1f76a10b | 321 | write_file(sb_git.buf, "gitdir: %s/worktrees/%s", |
3d7747e3 | 322 | realpath.buf, name); |
b979d950 ES |
323 | /* |
324 | * This is to keep resolve_ref() happy. We need a valid HEAD | |
ed197a6a ES |
325 | * or is_git_directory() will reject the directory. Any value which |
326 | * looks like an object ID will do since it will be immediately | |
327 | * replaced by the symbolic-ref or update-ref invocation in the new | |
328 | * worktree. | |
b979d950 | 329 | */ |
b979d950 ES |
330 | strbuf_reset(&sb); |
331 | strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); | |
14228447 | 332 | write_file(sb.buf, "%s", oid_to_hex(null_oid())); |
b979d950 ES |
333 | strbuf_reset(&sb); |
334 | strbuf_addf(&sb, "%s/commondir", sb_repo.buf); | |
1f76a10b | 335 | write_file(sb.buf, "../.."); |
b979d950 | 336 | |
22f9b7f3 JK |
337 | strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); |
338 | strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); | |
b979d950 | 339 | cp.git_cmd = 1; |
7f44e3d1 | 340 | |
ade546be | 341 | if (!is_branch) |
22f9b7f3 | 342 | strvec_pushl(&cp.args, "update-ref", "HEAD", |
f6d8942b | 343 | oid_to_hex(&commit->object.oid), NULL); |
371979c2 | 344 | else { |
22f9b7f3 | 345 | strvec_pushl(&cp.args, "symbolic-ref", "HEAD", |
f6d8942b | 346 | symref.buf, NULL); |
371979c2 | 347 | if (opts->quiet) |
22f9b7f3 | 348 | strvec_push(&cp.args, "--quiet"); |
371979c2 EP |
349 | } |
350 | ||
d70a9eb6 | 351 | cp.env = child_env.v; |
7f44e3d1 ES |
352 | ret = run_command(&cp); |
353 | if (ret) | |
354 | goto done; | |
355 | ||
ef2a0ac9 RZ |
356 | if (opts->checkout) { |
357 | cp.argv = NULL; | |
22f9b7f3 JK |
358 | strvec_clear(&cp.args); |
359 | strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL); | |
371979c2 | 360 | if (opts->quiet) |
22f9b7f3 | 361 | strvec_push(&cp.args, "--quiet"); |
d70a9eb6 | 362 | cp.env = child_env.v; |
ef2a0ac9 RZ |
363 | ret = run_command(&cp); |
364 | if (ret) | |
365 | goto done; | |
b979d950 | 366 | } |
ef2a0ac9 RZ |
367 | |
368 | is_junk = 0; | |
88ce3ef6 ÆAB |
369 | FREE_AND_NULL(junk_work_tree); |
370 | FREE_AND_NULL(junk_git_dir); | |
ef2a0ac9 | 371 | |
7f44e3d1 | 372 | done: |
507e6e9e NTND |
373 | if (ret || !opts->keep_locked) { |
374 | strbuf_reset(&sb); | |
375 | strbuf_addf(&sb, "%s/locked", sb_repo.buf); | |
376 | unlink_or_warn(sb.buf); | |
377 | } | |
ade546be ES |
378 | |
379 | /* | |
380 | * Hook failure does not warrant worktree deletion, so run hook after | |
381 | * is_junk is cleared, but do return appropriate code when hook fails. | |
382 | */ | |
a4bf1e3c ES |
383 | if (!ret && opts->checkout) { |
384 | const char *hook = find_hook("post-checkout"); | |
385 | if (hook) { | |
386 | const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL }; | |
387 | cp.git_cmd = 0; | |
388 | cp.no_stdin = 1; | |
389 | cp.stdout_to_stderr = 1; | |
390 | cp.dir = path; | |
391 | cp.env = env; | |
392 | cp.argv = NULL; | |
6206286e | 393 | cp.trace2_hook_name = "post-checkout"; |
22f9b7f3 | 394 | strvec_pushl(&cp.args, absolute_path(hook), |
14228447 | 395 | oid_to_hex(null_oid()), |
f6d8942b JK |
396 | oid_to_hex(&commit->object.oid), |
397 | "1", NULL); | |
a4bf1e3c ES |
398 | ret = run_command(&cp); |
399 | } | |
400 | } | |
ade546be | 401 | |
22f9b7f3 | 402 | strvec_clear(&child_env); |
b979d950 | 403 | strbuf_release(&sb); |
f7c9dac1 | 404 | strbuf_release(&symref); |
b979d950 ES |
405 | strbuf_release(&sb_repo); |
406 | strbuf_release(&sb_git); | |
1de16aec | 407 | strbuf_release(&sb_name); |
3d7747e3 | 408 | strbuf_release(&realpath); |
b979d950 ES |
409 | return ret; |
410 | } | |
411 | ||
2c27002a TG |
412 | static void print_preparing_worktree_line(int detach, |
413 | const char *branch, | |
414 | const char *new_branch, | |
415 | int force_new_branch) | |
416 | { | |
417 | if (force_new_branch) { | |
418 | struct commit *commit = lookup_commit_reference_by_name(new_branch); | |
419 | if (!commit) | |
420 | printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); | |
421 | else | |
422 | printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"), | |
423 | new_branch, | |
10174da9 | 424 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
2c27002a TG |
425 | } else if (new_branch) { |
426 | printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); | |
427 | } else { | |
428 | struct strbuf s = STRBUF_INIT; | |
429 | if (!detach && !strbuf_check_branch_ref(&s, branch) && | |
430 | ref_exists(s.buf)) | |
431 | printf_ln(_("Preparing worktree (checking out '%s')"), | |
432 | branch); | |
433 | else { | |
434 | struct commit *commit = lookup_commit_reference_by_name(branch); | |
435 | if (!commit) | |
436 | die(_("invalid reference: %s"), branch); | |
437 | printf_ln(_("Preparing worktree (detached HEAD %s)"), | |
10174da9 | 438 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
2c27002a TG |
439 | } |
440 | strbuf_release(&s); | |
441 | } | |
442 | } | |
443 | ||
6427f871 TG |
444 | static const char *dwim_branch(const char *path, const char **new_branch) |
445 | { | |
446 | int n; | |
aa1b6397 | 447 | int branch_exists; |
6427f871 | 448 | const char *s = worktree_basename(path, &n); |
f60a7b76 TG |
449 | const char *branchname = xstrndup(s, n); |
450 | struct strbuf ref = STRBUF_INIT; | |
451 | ||
452 | UNLEAK(branchname); | |
aa1b6397 AH |
453 | |
454 | branch_exists = !strbuf_check_branch_ref(&ref, branchname) && | |
455 | ref_exists(ref.buf); | |
456 | strbuf_release(&ref); | |
457 | if (branch_exists) | |
f60a7b76 | 458 | return branchname; |
f60a7b76 TG |
459 | |
460 | *new_branch = branchname; | |
6427f871 TG |
461 | if (guess_remote) { |
462 | struct object_id oid; | |
463 | const char *remote = | |
3c87aa94 | 464 | unique_tracking_name(*new_branch, &oid, NULL); |
6427f871 TG |
465 | return remote; |
466 | } | |
467 | return NULL; | |
468 | } | |
469 | ||
fc56361f ES |
470 | static int add(int ac, const char **av, const char *prefix) |
471 | { | |
5dd6e234 ES |
472 | struct add_opts opts; |
473 | const char *new_branch_force = NULL; | |
e4da43b1 JK |
474 | char *path; |
475 | const char *branch; | |
d861d34a | 476 | const char *new_branch = NULL; |
e284e892 | 477 | const char *opt_track = NULL; |
fc56361f | 478 | struct option options[] = { |
1224781d NTND |
479 | OPT__FORCE(&opts.force, |
480 | N_("checkout <branch> even if already checked out in other worktree"), | |
fc3d4e0c | 481 | PARSE_OPT_NOCOMPLETE), |
d861d34a | 482 | OPT_STRING('b', NULL, &new_branch, N_("branch"), |
cbdf60fa ES |
483 | N_("create a new branch")), |
484 | OPT_STRING('B', NULL, &new_branch_force, N_("branch"), | |
485 | N_("create or reset a branch")), | |
c670aa47 | 486 | OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")), |
ef2a0ac9 | 487 | OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")), |
507e6e9e | 488 | OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")), |
371979c2 | 489 | OPT__QUIET(&opts.quiet, N_("suppress progress reporting")), |
e284e892 TG |
490 | OPT_PASSTHRU(0, "track", &opt_track, NULL, |
491 | N_("set up tracking mode (see git-branch(1))"), | |
492 | PARSE_OPT_NOARG | PARSE_OPT_OPTARG), | |
71d6682d TG |
493 | OPT_BOOL(0, "guess-remote", &guess_remote, |
494 | N_("try to match the new branch name with a remote-tracking branch")), | |
fc56361f ES |
495 | OPT_END() |
496 | }; | |
497 | ||
5dd6e234 | 498 | memset(&opts, 0, sizeof(opts)); |
ef2a0ac9 | 499 | opts.checkout = 1; |
fc56361f | 500 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
d861d34a | 501 | if (!!opts.detach + !!new_branch + !!new_branch_force > 1) |
ab0b2c53 | 502 | die(_("-b, -B, and --detach are mutually exclusive")); |
0f4af3b9 ES |
503 | if (ac < 1 || ac > 2) |
504 | usage_with_options(worktree_usage, options); | |
fc56361f | 505 | |
116fb64e | 506 | path = prefix_filename(prefix, av[0]); |
0f4af3b9 | 507 | branch = ac < 2 ? "HEAD" : av[1]; |
fc56361f | 508 | |
1a450e2f JDG |
509 | if (!strcmp(branch, "-")) |
510 | branch = "@{-1}"; | |
511 | ||
d861d34a | 512 | if (new_branch_force) { |
beb6f24b NTND |
513 | struct strbuf symref = STRBUF_INIT; |
514 | ||
d861d34a | 515 | new_branch = new_branch_force; |
eef005dc | 516 | |
beb6f24b | 517 | if (!opts.force && |
d861d34a | 518 | !strbuf_check_branch_ref(&symref, new_branch) && |
beb6f24b | 519 | ref_exists(symref.buf)) |
8d9fdd70 | 520 | die_if_checked_out(symref.buf, 0); |
beb6f24b NTND |
521 | strbuf_release(&symref); |
522 | } | |
523 | ||
d861d34a | 524 | if (ac < 2 && !new_branch && !opts.detach) { |
6427f871 TG |
525 | const char *s = dwim_branch(path, &new_branch); |
526 | if (s) | |
527 | branch = s; | |
1eb07d82 ES |
528 | } |
529 | ||
d861d34a | 530 | if (ac == 2 && !new_branch && !opts.detach) { |
4e853331 TG |
531 | struct object_id oid; |
532 | struct commit *commit; | |
533 | const char *remote; | |
534 | ||
535 | commit = lookup_commit_reference_by_name(branch); | |
536 | if (!commit) { | |
3c87aa94 | 537 | remote = unique_tracking_name(branch, &oid, NULL); |
4e853331 | 538 | if (remote) { |
d861d34a | 539 | new_branch = branch; |
4e853331 TG |
540 | branch = remote; |
541 | } | |
542 | } | |
543 | } | |
371979c2 EP |
544 | if (!opts.quiet) |
545 | print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force); | |
2c27002a | 546 | |
d861d34a | 547 | if (new_branch) { |
542aa25d | 548 | struct child_process cp = CHILD_PROCESS_INIT; |
c2842439 | 549 | cp.git_cmd = 1; |
22f9b7f3 | 550 | strvec_push(&cp.args, "branch"); |
d861d34a | 551 | if (new_branch_force) |
22f9b7f3 | 552 | strvec_push(&cp.args, "--force"); |
371979c2 | 553 | if (opts.quiet) |
22f9b7f3 JK |
554 | strvec_push(&cp.args, "--quiet"); |
555 | strvec_push(&cp.args, new_branch); | |
556 | strvec_push(&cp.args, branch); | |
e284e892 | 557 | if (opt_track) |
22f9b7f3 | 558 | strvec_push(&cp.args, opt_track); |
c2842439 ES |
559 | if (run_command(&cp)) |
560 | return -1; | |
d861d34a | 561 | branch = new_branch; |
e284e892 TG |
562 | } else if (opt_track) { |
563 | die(_("--[no-]track can only be used if a new branch is created")); | |
1eb07d82 ES |
564 | } |
565 | ||
0e5bba53 JK |
566 | UNLEAK(path); |
567 | UNLEAK(opts); | |
80a0548f | 568 | return add_worktree(path, branch, &opts); |
fc56361f ES |
569 | } |
570 | ||
bb9c03b8 MR |
571 | static void show_worktree_porcelain(struct worktree *wt) |
572 | { | |
862c723d RS |
573 | const char *reason; |
574 | ||
bb9c03b8 MR |
575 | printf("worktree %s\n", wt->path); |
576 | if (wt->is_bare) | |
577 | printf("bare\n"); | |
578 | else { | |
0f05154c | 579 | printf("HEAD %s\n", oid_to_hex(&wt->head_oid)); |
bb9c03b8 MR |
580 | if (wt->is_detached) |
581 | printf("detached\n"); | |
a234563a | 582 | else if (wt->head_ref) |
bb9c03b8 MR |
583 | printf("branch %s\n", wt->head_ref); |
584 | } | |
862c723d RS |
585 | |
586 | reason = worktree_lock_reason(wt); | |
587 | if (reason && *reason) { | |
588 | struct strbuf sb = STRBUF_INIT; | |
589 | quote_c_style(reason, &sb, NULL, 0); | |
590 | printf("locked %s\n", sb.buf); | |
591 | strbuf_release(&sb); | |
592 | } else if (reason) | |
593 | printf("locked\n"); | |
594 | ||
9b19a58f RS |
595 | reason = worktree_prune_reason(wt, expire); |
596 | if (reason) | |
597 | printf("prunable %s\n", reason); | |
598 | ||
bb9c03b8 MR |
599 | printf("\n"); |
600 | } | |
601 | ||
602 | static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) | |
603 | { | |
604 | struct strbuf sb = STRBUF_INIT; | |
605 | int cur_path_len = strlen(wt->path); | |
606 | int path_adj = cur_path_len - utf8_strwidth(wt->path); | |
076b444a | 607 | const char *reason; |
bb9c03b8 MR |
608 | |
609 | strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path); | |
610 | if (wt->is_bare) | |
611 | strbuf_addstr(&sb, "(bare)"); | |
612 | else { | |
613 | strbuf_addf(&sb, "%-*s ", abbrev_len, | |
aab9583f | 614 | find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV)); |
96f09e2a | 615 | if (wt->is_detached) |
bb9c03b8 | 616 | strbuf_addstr(&sb, "(detached HEAD)"); |
2e11f58f JS |
617 | else if (wt->head_ref) { |
618 | char *ref = shorten_unambiguous_ref(wt->head_ref, 0); | |
619 | strbuf_addf(&sb, "[%s]", ref); | |
620 | free(ref); | |
621 | } else | |
a234563a | 622 | strbuf_addstr(&sb, "(error)"); |
bb9c03b8 | 623 | } |
bb9c03b8 | 624 | |
076b444a RS |
625 | reason = worktree_lock_reason(wt); |
626 | if (verbose && reason && *reason) | |
627 | strbuf_addf(&sb, "\n\tlocked: %s", reason); | |
628 | else if (reason) | |
c57b3367 RS |
629 | strbuf_addstr(&sb, " locked"); |
630 | ||
076b444a RS |
631 | reason = worktree_prune_reason(wt, expire); |
632 | if (verbose && reason) | |
633 | strbuf_addf(&sb, "\n\tprunable: %s", reason); | |
634 | else if (reason) | |
9b19a58f RS |
635 | strbuf_addstr(&sb, " prunable"); |
636 | ||
c57b3367 | 637 | printf("%s\n", sb.buf); |
bb9c03b8 MR |
638 | strbuf_release(&sb); |
639 | } | |
640 | ||
641 | static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen) | |
642 | { | |
643 | int i; | |
644 | ||
645 | for (i = 0; wt[i]; i++) { | |
646 | int sha1_len; | |
647 | int path_len = strlen(wt[i]->path); | |
648 | ||
649 | if (path_len > *maxlen) | |
650 | *maxlen = path_len; | |
aab9583f | 651 | sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev)); |
bb9c03b8 MR |
652 | if (sha1_len > *abbrev) |
653 | *abbrev = sha1_len; | |
654 | } | |
655 | } | |
656 | ||
d9c54c2b ES |
657 | static int pathcmp(const void *a_, const void *b_) |
658 | { | |
659 | const struct worktree *const *a = a_; | |
660 | const struct worktree *const *b = b_; | |
661 | return fspathcmp((*a)->path, (*b)->path); | |
662 | } | |
663 | ||
664 | static void pathsort(struct worktree **wt) | |
665 | { | |
666 | int n = 0; | |
667 | struct worktree **p = wt; | |
668 | ||
669 | while (*p++) | |
670 | n++; | |
671 | QSORT(wt, n, pathcmp); | |
672 | } | |
673 | ||
bb9c03b8 MR |
674 | static int list(int ac, const char **av, const char *prefix) |
675 | { | |
676 | int porcelain = 0; | |
677 | ||
678 | struct option options[] = { | |
679 | OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")), | |
076b444a | 680 | OPT__VERBOSE(&verbose, N_("show extended annotations and reasons, if available")), |
9b19a58f RS |
681 | OPT_EXPIRY_DATE(0, "expire", &expire, |
682 | N_("add 'prunable' annotation to worktrees older than <time>")), | |
bb9c03b8 MR |
683 | OPT_END() |
684 | }; | |
685 | ||
9b19a58f | 686 | expire = TIME_MAX; |
bb9c03b8 MR |
687 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
688 | if (ac) | |
689 | usage_with_options(worktree_usage, options); | |
076b444a RS |
690 | else if (verbose && porcelain) |
691 | die(_("--verbose and --porcelain are mutually exclusive")); | |
bb9c03b8 | 692 | else { |
03f2465b | 693 | struct worktree **worktrees = get_worktrees(); |
bb9c03b8 MR |
694 | int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i; |
695 | ||
d9c54c2b ES |
696 | /* sort worktrees by path but keep main worktree at top */ |
697 | pathsort(worktrees + 1); | |
698 | ||
bb9c03b8 MR |
699 | if (!porcelain) |
700 | measure_widths(worktrees, &abbrev, &path_maxlen); | |
701 | ||
702 | for (i = 0; worktrees[i]; i++) { | |
703 | if (porcelain) | |
704 | show_worktree_porcelain(worktrees[i]); | |
705 | else | |
706 | show_worktree(worktrees[i], path_maxlen, abbrev); | |
707 | } | |
708 | free_worktrees(worktrees); | |
709 | } | |
710 | return 0; | |
711 | } | |
712 | ||
58142c09 NTND |
713 | static int lock_worktree(int ac, const char **av, const char *prefix) |
714 | { | |
715 | const char *reason = "", *old_reason; | |
716 | struct option options[] = { | |
717 | OPT_STRING(0, "reason", &reason, N_("string"), | |
718 | N_("reason for locking")), | |
719 | OPT_END() | |
720 | }; | |
721 | struct worktree **worktrees, *wt; | |
722 | ||
723 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
724 | if (ac != 1) | |
725 | usage_with_options(worktree_usage, options); | |
726 | ||
03f2465b | 727 | worktrees = get_worktrees(); |
58142c09 NTND |
728 | wt = find_worktree(worktrees, prefix, av[0]); |
729 | if (!wt) | |
730 | die(_("'%s' is not a working tree"), av[0]); | |
731 | if (is_main_worktree(wt)) | |
732 | die(_("The main working tree cannot be locked or unlocked")); | |
733 | ||
d236f12b | 734 | old_reason = worktree_lock_reason(wt); |
58142c09 NTND |
735 | if (old_reason) { |
736 | if (*old_reason) | |
737 | die(_("'%s' is already locked, reason: %s"), | |
738 | av[0], old_reason); | |
739 | die(_("'%s' is already locked"), av[0]); | |
740 | } | |
741 | ||
742 | write_file(git_common_path("worktrees/%s/locked", wt->id), | |
743 | "%s", reason); | |
744 | free_worktrees(worktrees); | |
745 | return 0; | |
746 | } | |
747 | ||
6d308627 NTND |
748 | static int unlock_worktree(int ac, const char **av, const char *prefix) |
749 | { | |
750 | struct option options[] = { | |
751 | OPT_END() | |
752 | }; | |
753 | struct worktree **worktrees, *wt; | |
754 | int ret; | |
755 | ||
756 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
757 | if (ac != 1) | |
758 | usage_with_options(worktree_usage, options); | |
759 | ||
03f2465b | 760 | worktrees = get_worktrees(); |
6d308627 NTND |
761 | wt = find_worktree(worktrees, prefix, av[0]); |
762 | if (!wt) | |
763 | die(_("'%s' is not a working tree"), av[0]); | |
764 | if (is_main_worktree(wt)) | |
765 | die(_("The main working tree cannot be locked or unlocked")); | |
d236f12b | 766 | if (!worktree_lock_reason(wt)) |
6d308627 NTND |
767 | die(_("'%s' is not locked"), av[0]); |
768 | ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id)); | |
769 | free_worktrees(worktrees); | |
770 | return ret; | |
771 | } | |
772 | ||
78d986b2 NTND |
773 | static void validate_no_submodules(const struct worktree *wt) |
774 | { | |
775 | struct index_state istate = { NULL }; | |
00a6d4d1 | 776 | struct strbuf path = STRBUF_INIT; |
78d986b2 NTND |
777 | int i, found_submodules = 0; |
778 | ||
00a6d4d1 NTND |
779 | if (is_directory(worktree_git_path(wt, "modules"))) { |
780 | /* | |
781 | * There could be false positives, e.g. the "modules" | |
782 | * directory exists but is empty. But it's a rare case and | |
783 | * this simpler check is probably good enough for now. | |
784 | */ | |
785 | found_submodules = 1; | |
786 | } else if (read_index_from(&istate, worktree_git_path(wt, "index"), | |
787 | get_worktree_git_dir(wt)) > 0) { | |
78d986b2 NTND |
788 | for (i = 0; i < istate.cache_nr; i++) { |
789 | struct cache_entry *ce = istate.cache[i]; | |
00a6d4d1 | 790 | int err; |
78d986b2 | 791 | |
00a6d4d1 NTND |
792 | if (!S_ISGITLINK(ce->ce_mode)) |
793 | continue; | |
794 | ||
795 | strbuf_reset(&path); | |
796 | strbuf_addf(&path, "%s/%s", wt->path, ce->name); | |
797 | if (!is_submodule_populated_gently(path.buf, &err)) | |
798 | continue; | |
799 | ||
800 | found_submodules = 1; | |
801 | break; | |
78d986b2 NTND |
802 | } |
803 | } | |
804 | discard_index(&istate); | |
00a6d4d1 | 805 | strbuf_release(&path); |
78d986b2 NTND |
806 | |
807 | if (found_submodules) | |
cc73385c | 808 | die(_("working trees containing submodules cannot be moved or removed")); |
78d986b2 NTND |
809 | } |
810 | ||
9f792bb4 NTND |
811 | static int move_worktree(int ac, const char **av, const char *prefix) |
812 | { | |
68a6b3a1 | 813 | int force = 0; |
9f792bb4 | 814 | struct option options[] = { |
68a6b3a1 ES |
815 | OPT__FORCE(&force, |
816 | N_("force move even if worktree is dirty or locked"), | |
817 | PARSE_OPT_NOCOMPLETE), | |
9f792bb4 NTND |
818 | OPT_END() |
819 | }; | |
820 | struct worktree **worktrees, *wt; | |
821 | struct strbuf dst = STRBUF_INIT; | |
822 | struct strbuf errmsg = STRBUF_INIT; | |
68a6b3a1 | 823 | const char *reason = NULL; |
9f792bb4 NTND |
824 | char *path; |
825 | ||
826 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
827 | if (ac != 2) | |
828 | usage_with_options(worktree_usage, options); | |
829 | ||
830 | path = prefix_filename(prefix, av[1]); | |
831 | strbuf_addstr(&dst, path); | |
832 | free(path); | |
833 | ||
03f2465b | 834 | worktrees = get_worktrees(); |
9f792bb4 NTND |
835 | wt = find_worktree(worktrees, prefix, av[0]); |
836 | if (!wt) | |
837 | die(_("'%s' is not a working tree"), av[0]); | |
838 | if (is_main_worktree(wt)) | |
839 | die(_("'%s' is a main working tree"), av[0]); | |
c64a8d20 NTND |
840 | if (is_directory(dst.buf)) { |
841 | const char *sep = find_last_dir_sep(wt->path); | |
842 | ||
843 | if (!sep) | |
844 | die(_("could not figure out destination name from '%s'"), | |
845 | wt->path); | |
846 | strbuf_trim_trailing_dir_sep(&dst); | |
847 | strbuf_addstr(&dst, sep); | |
848 | } | |
810382ed | 849 | check_candidate_path(dst.buf, force, worktrees, "move"); |
9f792bb4 | 850 | |
78d986b2 NTND |
851 | validate_no_submodules(wt); |
852 | ||
68a6b3a1 | 853 | if (force < 2) |
d236f12b | 854 | reason = worktree_lock_reason(wt); |
9f792bb4 NTND |
855 | if (reason) { |
856 | if (*reason) | |
68a6b3a1 | 857 | die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"), |
9f792bb4 | 858 | reason); |
68a6b3a1 | 859 | die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first")); |
9f792bb4 | 860 | } |
ee6763af | 861 | if (validate_worktree(wt, &errmsg, 0)) |
9f792bb4 NTND |
862 | die(_("validation failed, cannot move working tree: %s"), |
863 | errmsg.buf); | |
864 | strbuf_release(&errmsg); | |
865 | ||
866 | if (rename(wt->path, dst.buf) == -1) | |
867 | die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf); | |
868 | ||
869 | update_worktree_location(wt, dst.buf); | |
870 | ||
871 | strbuf_release(&dst); | |
872 | free_worktrees(worktrees); | |
873 | return 0; | |
874 | } | |
875 | ||
cc73385c NTND |
876 | /* |
877 | * Note, "git status --porcelain" is used to determine if it's safe to | |
878 | * delete a whole worktree. "git status" does not ignore user | |
879 | * configuration, so if a normal "git status" shows "clean" for the | |
880 | * user, then it's ok to remove it. | |
881 | * | |
882 | * This assumption may be a bad one. We may want to ignore | |
883 | * (potentially bad) user settings and only delete a worktree when | |
884 | * it's absolutely safe to do so from _our_ point of view because we | |
885 | * know better. | |
886 | */ | |
887 | static void check_clean_worktree(struct worktree *wt, | |
888 | const char *original_path) | |
889 | { | |
cc73385c NTND |
890 | struct child_process cp; |
891 | char buf[1]; | |
892 | int ret; | |
893 | ||
894 | /* | |
895 | * Until we sort this out, all submodules are "dirty" and | |
896 | * will abort this function. | |
897 | */ | |
898 | validate_no_submodules(wt); | |
899 | ||
27ed6ccc | 900 | child_process_init(&cp); |
1aadb47a | 901 | strvec_pushf(&cp.env_array, "%s=%s/.git", |
f6d8942b | 902 | GIT_DIR_ENVIRONMENT, wt->path); |
1aadb47a | 903 | strvec_pushf(&cp.env_array, "%s=%s", |
f6d8942b | 904 | GIT_WORK_TREE_ENVIRONMENT, wt->path); |
22f9b7f3 | 905 | strvec_pushl(&cp.args, "status", |
f6d8942b JK |
906 | "--porcelain", "--ignore-submodules=none", |
907 | NULL); | |
cc73385c NTND |
908 | cp.git_cmd = 1; |
909 | cp.dir = wt->path; | |
910 | cp.out = -1; | |
911 | ret = start_command(&cp); | |
912 | if (ret) | |
913 | die_errno(_("failed to run 'git status' on '%s'"), | |
914 | original_path); | |
915 | ret = xread(cp.out, buf, sizeof(buf)); | |
916 | if (ret) | |
507e5470 | 917 | die(_("'%s' contains modified or untracked files, use --force to delete it"), |
cc73385c NTND |
918 | original_path); |
919 | close(cp.out); | |
920 | ret = finish_command(&cp); | |
921 | if (ret) | |
922 | die_errno(_("failed to run 'git status' on '%s', code %d"), | |
923 | original_path, ret); | |
924 | } | |
925 | ||
926 | static int delete_git_work_tree(struct worktree *wt) | |
927 | { | |
928 | struct strbuf sb = STRBUF_INIT; | |
929 | int ret = 0; | |
930 | ||
931 | strbuf_addstr(&sb, wt->path); | |
932 | if (remove_dir_recursively(&sb, 0)) { | |
933 | error_errno(_("failed to delete '%s'"), sb.buf); | |
934 | ret = -1; | |
935 | } | |
936 | strbuf_release(&sb); | |
937 | return ret; | |
938 | } | |
939 | ||
cc73385c NTND |
940 | static int remove_worktree(int ac, const char **av, const char *prefix) |
941 | { | |
942 | int force = 0; | |
943 | struct option options[] = { | |
d228eea5 | 944 | OPT__FORCE(&force, |
f4143101 | 945 | N_("force removal even if worktree is dirty or locked"), |
d228eea5 | 946 | PARSE_OPT_NOCOMPLETE), |
cc73385c NTND |
947 | OPT_END() |
948 | }; | |
949 | struct worktree **worktrees, *wt; | |
950 | struct strbuf errmsg = STRBUF_INIT; | |
f4143101 | 951 | const char *reason = NULL; |
cc73385c NTND |
952 | int ret = 0; |
953 | ||
954 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
955 | if (ac != 1) | |
956 | usage_with_options(worktree_usage, options); | |
957 | ||
03f2465b | 958 | worktrees = get_worktrees(); |
cc73385c NTND |
959 | wt = find_worktree(worktrees, prefix, av[0]); |
960 | if (!wt) | |
961 | die(_("'%s' is not a working tree"), av[0]); | |
962 | if (is_main_worktree(wt)) | |
963 | die(_("'%s' is a main working tree"), av[0]); | |
f4143101 | 964 | if (force < 2) |
d236f12b | 965 | reason = worktree_lock_reason(wt); |
cc73385c NTND |
966 | if (reason) { |
967 | if (*reason) | |
f4143101 | 968 | die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"), |
cc73385c | 969 | reason); |
f4143101 | 970 | die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first")); |
cc73385c | 971 | } |
ee6763af | 972 | if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK)) |
cc73385c NTND |
973 | die(_("validation failed, cannot remove working tree: %s"), |
974 | errmsg.buf); | |
975 | strbuf_release(&errmsg); | |
976 | ||
ee6763af NTND |
977 | if (file_exists(wt->path)) { |
978 | if (!force) | |
979 | check_clean_worktree(wt, av[0]); | |
cc73385c | 980 | |
ee6763af NTND |
981 | ret |= delete_git_work_tree(wt); |
982 | } | |
cc73385c NTND |
983 | /* |
984 | * continue on even if ret is non-zero, there's no going back | |
985 | * from here. | |
986 | */ | |
602aaed0 | 987 | ret |= delete_git_dir(wt->id); |
3a540433 | 988 | delete_worktrees_dir_if_empty(); |
cc73385c NTND |
989 | |
990 | free_worktrees(worktrees); | |
991 | return ret; | |
992 | } | |
993 | ||
bdd1f3e4 ES |
994 | static void report_repair(int iserr, const char *path, const char *msg, void *cb_data) |
995 | { | |
996 | if (!iserr) { | |
997 | printf_ln(_("repair: %s: %s"), msg, path); | |
998 | } else { | |
999 | int *exit_status = (int *)cb_data; | |
1000 | fprintf_ln(stderr, _("error: %s: %s"), msg, path); | |
1001 | *exit_status = 1; | |
1002 | } | |
1003 | } | |
1004 | ||
e8e1ff24 ES |
1005 | static int repair(int ac, const char **av, const char *prefix) |
1006 | { | |
b214ab5a ES |
1007 | const char **p; |
1008 | const char *self[] = { ".", NULL }; | |
e8e1ff24 ES |
1009 | struct option options[] = { |
1010 | OPT_END() | |
1011 | }; | |
1012 | int rc = 0; | |
1013 | ||
1014 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); | |
b214ab5a ES |
1015 | p = ac > 0 ? av : self; |
1016 | for (; *p; p++) | |
1017 | repair_worktree_at_path(*p, report_repair, &rc); | |
cf76baea | 1018 | repair_worktrees(report_repair, &rc); |
e8e1ff24 ES |
1019 | return rc; |
1020 | } | |
1021 | ||
df0b6cfb NTND |
1022 | int cmd_worktree(int ac, const char **av, const char *prefix) |
1023 | { | |
1024 | struct option options[] = { | |
1025 | OPT_END() | |
1026 | }; | |
1027 | ||
e92445a7 | 1028 | git_config(git_worktree_config, NULL); |
d49028e6 | 1029 | |
df0b6cfb NTND |
1030 | if (ac < 2) |
1031 | usage_with_options(worktree_usage, options); | |
0409e0b6 NTND |
1032 | if (!prefix) |
1033 | prefix = ""; | |
fc56361f ES |
1034 | if (!strcmp(av[1], "add")) |
1035 | return add(ac - 1, av + 1, prefix); | |
df0b6cfb NTND |
1036 | if (!strcmp(av[1], "prune")) |
1037 | return prune(ac - 1, av + 1, prefix); | |
bb9c03b8 MR |
1038 | if (!strcmp(av[1], "list")) |
1039 | return list(ac - 1, av + 1, prefix); | |
58142c09 NTND |
1040 | if (!strcmp(av[1], "lock")) |
1041 | return lock_worktree(ac - 1, av + 1, prefix); | |
6d308627 NTND |
1042 | if (!strcmp(av[1], "unlock")) |
1043 | return unlock_worktree(ac - 1, av + 1, prefix); | |
9f792bb4 NTND |
1044 | if (!strcmp(av[1], "move")) |
1045 | return move_worktree(ac - 1, av + 1, prefix); | |
cc73385c NTND |
1046 | if (!strcmp(av[1], "remove")) |
1047 | return remove_worktree(ac - 1, av + 1, prefix); | |
e8e1ff24 ES |
1048 | if (!strcmp(av[1], "repair")) |
1049 | return repair(ac - 1, av + 1, prefix); | |
df0b6cfb NTND |
1050 | usage_with_options(worktree_usage, options); |
1051 | } |