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