blob: 15bdb380c7a58d5e7c71156a3db699f2535aff00 [file] [log] [blame]
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001#include "cache.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00002#include "abspath.h"
Jacob Abel35f03832023-05-17 21:48:52 +00003#include "advice.h"
Thomas Gummerer4e853332017-11-26 19:43:54 +00004#include "checkout.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07006#include "builtin.h"
7#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00008#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00009#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000010#include "hex.h"
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070011#include "parse-options.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040012#include "strvec.h"
Eric Sunshinef7c9dac2015-07-17 19:00:13 -040013#include "branch.h"
14#include "refs.h"
Eric Sunshinefc563612015-07-06 13:30:50 -040015#include "run-command.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 21:03:26 +020016#include "hook.h"
Eric Sunshineb979d952015-07-06 13:30:55 -040017#include "sigchain.h"
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +070018#include "submodule.h"
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040019#include "utf8.h"
20#include "worktree.h"
Elijah Newrend5ebb502023-03-21 06:26:01 +000021#include "wrapper.h"
Rafael Silva862c7232021-01-27 09:03:08 +010022#include "quote.h"
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070023
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020024#define BUILTIN_WORKTREE_ADD_USAGE \
Ævar Arnfjörð Bjarmason97f03a52022-10-13 17:39:26 +020025 N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]\n" \
Jacob Abel7ab89182023-05-17 21:48:47 +000026 " [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]")
27
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020028#define BUILTIN_WORKTREE_LIST_USAGE \
Ævar Arnfjörð Bjarmason97f03a52022-10-13 17:39:26 +020029 N_("git worktree list [-v | --porcelain [-z]]")
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020030#define BUILTIN_WORKTREE_LOCK_USAGE \
Ævar Arnfjörð Bjarmason97f03a52022-10-13 17:39:26 +020031 N_("git worktree lock [--reason <string>] <worktree>")
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020032#define BUILTIN_WORKTREE_MOVE_USAGE \
33 N_("git worktree move <worktree> <new-path>")
34#define BUILTIN_WORKTREE_PRUNE_USAGE \
Ævar Arnfjörð Bjarmason97f03a52022-10-13 17:39:26 +020035 N_("git worktree prune [-n] [-v] [--expire <expire>]")
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020036#define BUILTIN_WORKTREE_REMOVE_USAGE \
Ævar Arnfjörð Bjarmason97f03a52022-10-13 17:39:26 +020037 N_("git worktree remove [-f] <worktree>")
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020038#define BUILTIN_WORKTREE_REPAIR_USAGE \
39 N_("git worktree repair [<path>...]")
40#define BUILTIN_WORKTREE_UNLOCK_USAGE \
41 N_("git worktree unlock <worktree>")
42
Jacob Abel35f03832023-05-17 21:48:52 +000043#define WORKTREE_ADD_ORPHAN_WITH_DASH_B_HINT_TEXT \
44 _("If you meant to create a worktree containing a new orphan branch\n" \
45 "(branch with no commits) for this repository, you can do so\n" \
46 "using the --orphan flag:\n" \
47 "\n" \
48 " git worktree add --orphan -b %s %s\n")
49
50#define WORKTREE_ADD_ORPHAN_NO_DASH_B_HINT_TEXT \
51 _("If you meant to create a worktree containing a new orphan branch\n" \
52 "(branch with no commits) for this repository, you can do so\n" \
53 "using the --orphan flag:\n" \
54 "\n" \
55 " git worktree add --orphan %s\n")
56
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +020057static const char * const git_worktree_usage[] = {
58 BUILTIN_WORKTREE_ADD_USAGE,
59 BUILTIN_WORKTREE_LIST_USAGE,
60 BUILTIN_WORKTREE_LOCK_USAGE,
61 BUILTIN_WORKTREE_MOVE_USAGE,
62 BUILTIN_WORKTREE_PRUNE_USAGE,
63 BUILTIN_WORKTREE_REMOVE_USAGE,
64 BUILTIN_WORKTREE_REPAIR_USAGE,
65 BUILTIN_WORKTREE_UNLOCK_USAGE,
66 NULL
67};
68
69static const char * const git_worktree_add_usage[] = {
70 BUILTIN_WORKTREE_ADD_USAGE,
71 NULL,
72};
73
74static const char * const git_worktree_list_usage[] = {
75 BUILTIN_WORKTREE_LIST_USAGE,
76 NULL
77};
78
79static const char * const git_worktree_lock_usage[] = {
80 BUILTIN_WORKTREE_LOCK_USAGE,
81 NULL
82};
83
84static const char * const git_worktree_move_usage[] = {
85 BUILTIN_WORKTREE_MOVE_USAGE,
86 NULL
87};
88
89static const char * const git_worktree_prune_usage[] = {
90 BUILTIN_WORKTREE_PRUNE_USAGE,
91 NULL
92};
93
94static const char * const git_worktree_remove_usage[] = {
95 BUILTIN_WORKTREE_REMOVE_USAGE,
96 NULL
97};
98
99static const char * const git_worktree_repair_usage[] = {
100 BUILTIN_WORKTREE_REPAIR_USAGE,
101 NULL
102};
103
104static const char * const git_worktree_unlock_usage[] = {
105 BUILTIN_WORKTREE_UNLOCK_USAGE,
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700106 NULL
107};
108
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400109struct add_opts {
110 int force;
111 int detach;
Elia Pinto371979c2018-08-15 20:56:30 +0000112 int quiet;
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000113 int checkout;
Jacob Abel7ab89182023-05-17 21:48:47 +0000114 int orphan;
Stephen Manz0db49612021-07-15 02:32:30 +0000115 const char *keep_locked;
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400116};
117
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700118static int show_only;
119static int verbose;
Thomas Gummerere92445a2017-11-29 20:04:51 +0000120static int guess_remote;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200121static timestamp_t expire;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700122
Thomas Gummerere92445a2017-11-29 20:04:51 +0000123static int git_worktree_config(const char *var, const char *value, void *cb)
124{
125 if (!strcmp(var, "worktree.guessremote")) {
126 guess_remote = git_config_bool(var, value);
127 return 0;
128 }
129
130 return git_default_config(var, value, cb);
131}
132
Eric Sunshine602aaed2018-08-28 17:20:20 -0400133static int delete_git_dir(const char *id)
Eric Sunshinee5353be2018-08-28 17:20:19 -0400134{
135 struct strbuf sb = STRBUF_INIT;
Eric Sunshine602aaed2018-08-28 17:20:20 -0400136 int ret;
Eric Sunshinee5353be2018-08-28 17:20:19 -0400137
Eric Sunshine602aaed2018-08-28 17:20:20 -0400138 strbuf_addstr(&sb, git_common_path("worktrees/%s", id));
139 ret = remove_dir_recursively(&sb, 0);
140 if (ret < 0 && errno == ENOTDIR)
141 ret = unlink(sb.buf);
142 if (ret)
Eric Sunshinee5353be2018-08-28 17:20:19 -0400143 error_errno(_("failed to delete '%s'"), sb.buf);
Eric Sunshinee5353be2018-08-28 17:20:19 -0400144 strbuf_release(&sb);
145 return ret;
146}
147
Eric Sunshine3a540432018-08-28 17:20:26 -0400148static void delete_worktrees_dir_if_empty(void)
149{
150 rmdir(git_path("worktrees")); /* ignore failed removal */
151}
152
Eric Sunshinedd9609a2020-06-10 02:30:45 -0400153static void prune_worktree(const char *id, const char *reason)
154{
155 if (show_only || verbose)
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500156 fprintf_ln(stderr, _("Removing %s/%s: %s"), "worktrees", id, reason);
Eric Sunshinedd9609a2020-06-10 02:30:45 -0400157 if (!show_only)
158 delete_git_dir(id);
159}
160
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400161static int prune_cmp(const void *a, const void *b)
162{
163 const struct string_list_item *x = a;
164 const struct string_list_item *y = b;
165 int c;
166
167 if ((c = fspathcmp(x->string, y->string)))
168 return c;
Eric Sunshine916133e2020-06-10 02:30:47 -0400169 /*
170 * paths same; prune_dupes() removes all but the first worktree entry
171 * having the same path, so sort main worktree ('util' is NULL) above
172 * linked worktrees ('util' not NULL) since main worktree can't be
173 * removed
174 */
175 if (!x->util)
176 return -1;
177 if (!y->util)
178 return 1;
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400179 /* paths same; sort by .git/worktrees/<id> */
180 return strcmp(x->util, y->util);
181}
182
183static void prune_dups(struct string_list *l)
184{
185 int i;
186
187 QSORT(l->items, l->nr, prune_cmp);
188 for (i = 1; i < l->nr; i++) {
189 if (!fspathcmp(l->items[i].string, l->items[i - 1].string))
190 prune_worktree(l->items[i].util, "duplicate entry");
191 }
192}
193
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700194static void prune_worktrees(void)
195{
196 struct strbuf reason = STRBUF_INIT;
Eric Sunshine916133e2020-06-10 02:30:47 -0400197 struct strbuf main_path = STRBUF_INIT;
Ævar Arnfjörð Bjarmason9f24f3c2023-02-07 00:07:43 +0100198 struct string_list kept = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700199 DIR *dir = opendir(git_path("worktrees"));
200 struct dirent *d;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700201 if (!dir)
202 return;
Elijah Newrenb548f0f2021-05-12 17:28:22 +0000203 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400204 char *path;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700205 strbuf_reset(&reason);
Rafael Silvaa29a8b72021-01-19 22:27:33 +0100206 if (should_prune_worktree(d->d_name, &reason, &path, expire))
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400207 prune_worktree(d->d_name, reason.buf);
208 else if (path)
Ævar Arnfjörð Bjarmason9f24f3c2023-02-07 00:07:43 +0100209 string_list_append_nodup(&kept, path)->util = xstrdup(d->d_name);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700210 }
211 closedir(dir);
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400212
Eric Sunshine916133e2020-06-10 02:30:47 -0400213 strbuf_add_absolute_path(&main_path, get_git_common_dir());
214 /* massage main worktree absolute path to match 'gitdir' content */
215 strbuf_strip_suffix(&main_path, "/.");
Ævar Arnfjörð Bjarmason9f24f3c2023-02-07 00:07:43 +0100216 string_list_append_nodup(&kept, strbuf_detach(&main_path, NULL));
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400217 prune_dups(&kept);
218 string_list_clear(&kept, 1);
219
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700220 if (!show_only)
Eric Sunshine3a540432018-08-28 17:20:26 -0400221 delete_worktrees_dir_if_empty();
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700222 strbuf_release(&reason);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700223}
224
225static int prune(int ac, const char **av, const char *prefix)
226{
227 struct option options[] = {
228 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
Patrick Steinhardt2488dca2017-02-06 14:13:59 +0100229 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700230 OPT_EXPIRY_DATE(0, "expire", &expire,
Patrick Steinhardt2488dca2017-02-06 14:13:59 +0100231 N_("expire working trees older than <time>")),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700232 OPT_END()
233 };
234
Johannes Schindelindddbad72017-04-26 21:29:31 +0200235 expire = TIME_MAX;
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200236 ac = parse_options(ac, av, prefix, options, git_worktree_prune_usage,
237 0);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700238 if (ac)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200239 usage_with_options(git_worktree_prune_usage, options);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700240 prune_worktrees();
241 return 0;
242}
243
Eric Sunshineb979d952015-07-06 13:30:55 -0400244static char *junk_work_tree;
245static char *junk_git_dir;
246static int is_junk;
247static pid_t junk_pid;
248
249static void remove_junk(void)
250{
251 struct strbuf sb = STRBUF_INIT;
252 if (!is_junk || getpid() != junk_pid)
253 return;
254 if (junk_git_dir) {
255 strbuf_addstr(&sb, junk_git_dir);
256 remove_dir_recursively(&sb, 0);
257 strbuf_reset(&sb);
258 }
259 if (junk_work_tree) {
260 strbuf_addstr(&sb, junk_work_tree);
261 remove_dir_recursively(&sb, 0);
262 }
263 strbuf_release(&sb);
264}
265
266static void remove_junk_on_signal(int signo)
267{
268 remove_junk();
269 sigchain_pop(signo);
270 raise(signo);
271}
272
Eric Sunshinef5682b22015-07-06 13:30:57 -0400273static const char *worktree_basename(const char *path, int *olen)
274{
275 const char *name;
276 int len;
277
278 len = strlen(path);
279 while (len && is_dir_sep(path[len - 1]))
280 len--;
281
282 for (name = path + len - 1; name > path; name--)
283 if (is_dir_sep(*name)) {
284 name++;
285 break;
286 }
287
288 *olen = len;
289 return name;
290}
291
Eric Sunshined179af62020-06-10 02:30:48 -0400292/* check that path is viable location for worktree */
293static void check_candidate_path(const char *path,
294 int force,
295 struct worktree **worktrees,
296 const char *cmd)
Eric Sunshine45059e62018-08-28 17:20:21 -0400297{
Eric Sunshinecb56f552018-08-28 17:20:22 -0400298 struct worktree *wt;
299 int locked;
300
Eric Sunshine45059e62018-08-28 17:20:21 -0400301 if (file_exists(path) && !is_empty_dir(path))
302 die(_("'%s' already exists"), path);
Eric Sunshinecb56f552018-08-28 17:20:22 -0400303
Eric Sunshinebb69b3b2020-02-24 04:08:48 -0500304 wt = find_worktree_by_path(worktrees, path);
Eric Sunshinecb56f552018-08-28 17:20:22 -0400305 if (!wt)
Eric Sunshined179af62020-06-10 02:30:48 -0400306 return;
Eric Sunshinecb56f552018-08-28 17:20:22 -0400307
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700308 locked = !!worktree_lock_reason(wt);
Eric Sunshined179af62020-06-10 02:30:48 -0400309 if ((!locked && force) || (locked && force > 1)) {
Eric Sunshinee19831c2018-08-28 17:20:23 -0400310 if (delete_git_dir(wt->id))
Eric Sunshined179af62020-06-10 02:30:48 -0400311 die(_("unusable worktree destination '%s'"), path);
312 return;
Eric Sunshinee19831c2018-08-28 17:20:23 -0400313 }
314
Eric Sunshinecb56f552018-08-28 17:20:22 -0400315 if (locked)
Matheus Tavaresb86339b2020-11-20 12:09:39 -0300316 die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path, cmd);
Eric Sunshinecb56f552018-08-28 17:20:22 -0400317 else
Matheus Tavaresb86339b2020-11-20 12:09:39 -0300318 die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path, cmd);
Eric Sunshine45059e62018-08-28 17:20:21 -0400319}
320
Derrick Stoleeace5ac52022-02-23 14:29:11 +0000321static void copy_sparse_checkout(const char *worktree_git_dir)
322{
323 char *from_file = git_pathdup("info/sparse-checkout");
324 char *to_file = xstrfmt("%s/info/sparse-checkout", worktree_git_dir);
325
326 if (file_exists(from_file)) {
327 if (safe_create_leading_directories(to_file) ||
328 copy_file(to_file, from_file, 0666))
329 error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
330 from_file, to_file);
331 }
332
333 free(from_file);
334 free(to_file);
335}
336
Derrick Stolee86397052022-02-23 14:29:10 +0000337static void copy_filtered_worktree_config(const char *worktree_git_dir)
338{
339 char *from_file = git_pathdup("config.worktree");
340 char *to_file = xstrfmt("%s/config.worktree", worktree_git_dir);
341
342 if (file_exists(from_file)) {
343 struct config_set cs = { { 0 } };
Derrick Stolee86397052022-02-23 14:29:10 +0000344 int bare;
345
346 if (safe_create_leading_directories(to_file) ||
347 copy_file(to_file, from_file, 0666)) {
348 error(_("failed to copy worktree config from '%s' to '%s'"),
349 from_file, to_file);
350 goto worktree_copy_cleanup;
351 }
352
353 git_configset_init(&cs);
354 git_configset_add_file(&cs, from_file);
355
356 if (!git_configset_get_bool(&cs, "core.bare", &bare) &&
357 bare &&
358 git_config_set_multivar_in_file_gently(
359 to_file, "core.bare", NULL, "true", 0))
360 error(_("failed to unset '%s' in '%s'"),
361 "core.bare", to_file);
Ævar Arnfjörð Bjarmasonb83efce2023-03-28 16:04:22 +0200362 if (!git_configset_get(&cs, "core.worktree") &&
Derrick Stolee86397052022-02-23 14:29:10 +0000363 git_config_set_in_file_gently(to_file,
364 "core.worktree", NULL))
365 error(_("failed to unset '%s' in '%s'"),
366 "core.worktree", to_file);
367
368 git_configset_clear(&cs);
369 }
370
371worktree_copy_cleanup:
372 free(from_file);
373 free(to_file);
374}
375
Derrick Stolee23f832e2022-02-23 14:29:12 +0000376static int checkout_worktree(const struct add_opts *opts,
377 struct strvec *child_env)
378{
379 struct child_process cp = CHILD_PROCESS_INIT;
380 cp.git_cmd = 1;
381 strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
382 if (opts->quiet)
383 strvec_push(&cp.args, "--quiet");
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200384 strvec_pushv(&cp.env, child_env->v);
Derrick Stolee23f832e2022-02-23 14:29:12 +0000385 return run_command(&cp);
386}
387
Jacob Abel7ab89182023-05-17 21:48:47 +0000388static int make_worktree_orphan(const char * ref, const struct add_opts *opts,
389 struct strvec *child_env)
390{
391 struct strbuf symref = STRBUF_INIT;
392 struct child_process cp = CHILD_PROCESS_INIT;
393
394 validate_new_branchname(ref, &symref, 0);
395 strvec_pushl(&cp.args, "symbolic-ref", "HEAD", symref.buf, NULL);
396 if (opts->quiet)
397 strvec_push(&cp.args, "--quiet");
398 strvec_pushv(&cp.env, child_env->v);
399 strbuf_release(&symref);
400 cp.git_cmd = 1;
401 return run_command(&cp);
402}
403
Eric Sunshine80a05482015-07-17 19:00:12 -0400404static int add_worktree(const char *path, const char *refname,
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400405 const struct add_opts *opts)
Eric Sunshineb979d952015-07-06 13:30:55 -0400406{
407 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000408 struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT;
Eric Sunshineb979d952015-07-06 13:30:55 -0400409 const char *name;
René Scharfe542aa252016-08-05 22:38:44 +0200410 struct child_process cp = CHILD_PROCESS_INIT;
Jeff King22f9b7f2020-07-28 16:24:27 -0400411 struct strvec child_env = STRVEC_INIT;
Michal Suchanek7af01f22019-02-20 17:16:48 +0100412 unsigned int counter = 0;
413 int len, ret;
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400414 struct strbuf symref = STRBUF_INIT;
415 struct commit *commit = NULL;
Eric Sunshineade546b2017-12-07 16:20:17 -0500416 int is_branch = 0;
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700417 struct strbuf sb_name = STRBUF_INIT;
Eric Sunshined179af62020-06-10 02:30:48 -0400418 struct worktree **worktrees;
Eric Sunshineb979d952015-07-06 13:30:55 -0400419
Eric Sunshine03f24652020-06-19 19:35:44 -0400420 worktrees = get_worktrees();
Eric Sunshined179af62020-06-10 02:30:48 -0400421 check_candidate_path(path, opts->force, worktrees, "add");
422 free_worktrees(worktrees);
423 worktrees = NULL;
Eric Sunshineb979d952015-07-06 13:30:55 -0400424
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400425 /* is 'refname' a branch or commit? */
Nguyễn Thái Ngọc Duy0ebf4a22016-02-15 20:35:32 +0700426 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
Eric Sunshineade546b2017-12-07 16:20:17 -0500427 ref_exists(symref.buf)) {
428 is_branch = 1;
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400429 if (!opts->force)
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700430 die_if_checked_out(symref.buf, 0);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400431 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500432 commit = lookup_commit_reference_by_name(refname);
Jacob Abel7ab89182023-05-17 21:48:47 +0000433 if (!commit && !opts->orphan)
Eric Sunshineade546b2017-12-07 16:20:17 -0500434 die(_("invalid reference: %s"), refname);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400435
Eric Sunshinef5682b22015-07-06 13:30:57 -0400436 name = worktree_basename(path, &len);
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700437 strbuf_add(&sb, name, path + len - name);
438 sanitize_refname_component(sb.buf, &sb_name);
439 if (!sb_name.len)
440 BUG("How come '%s' becomes empty after sanitization?", sb.buf);
441 strbuf_reset(&sb);
442 name = sb_name.buf;
443 git_path_buf(&sb_repo, "worktrees/%s", name);
Eric Sunshineb979d952015-07-06 13:30:55 -0400444 len = sb_repo.len;
445 if (safe_create_leading_directories_const(sb_repo.buf))
446 die_errno(_("could not create leading directories of '%s'"),
447 sb_repo.buf);
Michal Suchanek7af01f22019-02-20 17:16:48 +0100448
449 while (mkdir(sb_repo.buf, 0777)) {
Eric Sunshineb979d952015-07-06 13:30:55 -0400450 counter++;
Michal Suchanek7af01f22019-02-20 17:16:48 +0100451 if ((errno != EEXIST) || !counter /* overflow */)
452 die_errno(_("could not create directory of '%s'"),
453 sb_repo.buf);
Eric Sunshineb979d952015-07-06 13:30:55 -0400454 strbuf_setlen(&sb_repo, len);
455 strbuf_addf(&sb_repo, "%d", counter);
456 }
457 name = strrchr(sb_repo.buf, '/') + 1;
458
459 junk_pid = getpid();
460 atexit(remove_junk);
461 sigchain_push_common(remove_junk_on_signal);
462
Eric Sunshineb979d952015-07-06 13:30:55 -0400463 junk_git_dir = xstrdup(sb_repo.buf);
464 is_junk = 1;
465
466 /*
467 * lock the incomplete repo so prune won't delete it, unlock
468 * after the preparation is over.
469 */
470 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
Stephen Manz0db49612021-07-15 02:32:30 +0000471 if (opts->keep_locked)
472 write_file(sb.buf, "%s", opts->keep_locked);
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700473 else
Stephen Manz0db49612021-07-15 02:32:30 +0000474 write_file(sb.buf, _("initializing"));
Eric Sunshineb979d952015-07-06 13:30:55 -0400475
476 strbuf_addf(&sb_git, "%s/.git", path);
477 if (safe_create_leading_directories_const(sb_git.buf))
478 die_errno(_("could not create leading directories of '%s'"),
479 sb_git.buf);
480 junk_work_tree = xstrdup(path);
481
482 strbuf_reset(&sb);
483 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000484 strbuf_realpath(&realpath, sb_git.buf, 1);
485 write_file(sb.buf, "%s", realpath.buf);
486 strbuf_realpath(&realpath, get_git_common_dir(), 1);
Junio C Hamano1f76a102015-08-24 13:20:39 -0700487 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000488 realpath.buf, name);
Eric Sunshineb979d952015-07-06 13:30:55 -0400489 /*
490 * This is to keep resolve_ref() happy. We need a valid HEAD
Eric Sunshineed197a62015-07-17 19:00:15 -0400491 * or is_git_directory() will reject the directory. Any value which
492 * looks like an object ID will do since it will be immediately
493 * replaced by the symbolic-ref or update-ref invocation in the new
494 * worktree.
Eric Sunshineb979d952015-07-06 13:30:55 -0400495 */
Eric Sunshineb979d952015-07-06 13:30:55 -0400496 strbuf_reset(&sb);
497 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
brian m. carlson14228442021-04-26 01:02:56 +0000498 write_file(sb.buf, "%s", oid_to_hex(null_oid()));
Eric Sunshineb979d952015-07-06 13:30:55 -0400499 strbuf_reset(&sb);
500 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
Junio C Hamano1f76a102015-08-24 13:20:39 -0700501 write_file(sb.buf, "../..");
Eric Sunshineb979d952015-07-06 13:30:55 -0400502
Derrick Stolee53255912022-02-07 21:33:02 +0000503 /*
504 * If the current worktree has sparse-checkout enabled, then copy
505 * the sparse-checkout patterns from the current worktree.
506 */
Derrick Stoleeace5ac52022-02-23 14:29:11 +0000507 if (core_apply_sparse_checkout)
508 copy_sparse_checkout(sb_repo.buf);
Derrick Stolee53255912022-02-07 21:33:02 +0000509
510 /*
511 * If we are using worktree config, then copy all current config
512 * values from the current worktree into the new one, that way the
513 * new worktree behaves the same as this one.
514 */
Derrick Stolee86397052022-02-23 14:29:10 +0000515 if (repository_format_worktree_config)
516 copy_filtered_worktree_config(sb_repo.buf);
Derrick Stolee53255912022-02-07 21:33:02 +0000517
Jeff King22f9b7f2020-07-28 16:24:27 -0400518 strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
519 strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
Eric Sunshineb979d952015-07-06 13:30:55 -0400520 cp.git_cmd = 1;
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400521
Jacob Abel7ab89182023-05-17 21:48:47 +0000522 if (!is_branch && commit) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400523 strvec_pushl(&cp.args, "update-ref", "HEAD",
Jeff Kingf6d89422020-07-28 16:26:31 -0400524 oid_to_hex(&commit->object.oid), NULL);
Jacob Abel7ab89182023-05-17 21:48:47 +0000525 } else {
Jeff King22f9b7f2020-07-28 16:24:27 -0400526 strvec_pushl(&cp.args, "symbolic-ref", "HEAD",
Jeff Kingf6d89422020-07-28 16:26:31 -0400527 symref.buf, NULL);
Elia Pinto371979c2018-08-15 20:56:30 +0000528 if (opts->quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400529 strvec_push(&cp.args, "--quiet");
Elia Pinto371979c2018-08-15 20:56:30 +0000530 }
531
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200532 strvec_pushv(&cp.env, child_env.v);
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400533 ret = run_command(&cp);
534 if (ret)
535 goto done;
536
Jacob Abel7ab89182023-05-17 21:48:47 +0000537 if (opts->orphan &&
538 (ret = make_worktree_orphan(refname, opts, &child_env)))
539 goto done;
540
Derrick Stolee23f832e2022-02-23 14:29:12 +0000541 if (opts->checkout &&
542 (ret = checkout_worktree(opts, &child_env)))
543 goto done;
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000544
545 is_junk = 0;
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000546 FREE_AND_NULL(junk_work_tree);
547 FREE_AND_NULL(junk_git_dir);
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000548
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400549done:
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700550 if (ret || !opts->keep_locked) {
551 strbuf_reset(&sb);
552 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
553 unlink_or_warn(sb.buf);
554 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500555
556 /*
557 * Hook failure does not warrant worktree deletion, so run hook after
558 * is_junk is cleared, but do return appropriate code when hook fails.
559 */
Jacob Abel7ab89182023-05-17 21:48:47 +0000560 if (!ret && opts->checkout && !opts->orphan) {
Emily Shaffer1a3017d2021-12-22 04:59:36 +0100561 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
562
563 strvec_pushl(&opt.env, "GIT_DIR", "GIT_WORK_TREE", NULL);
564 strvec_pushl(&opt.args,
565 oid_to_hex(null_oid()),
566 oid_to_hex(&commit->object.oid),
567 "1",
568 NULL);
569 opt.dir = path;
570
571 ret = run_hooks_opt("post-checkout", &opt);
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500572 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500573
Jeff King22f9b7f2020-07-28 16:24:27 -0400574 strvec_clear(&child_env);
Eric Sunshineb979d952015-07-06 13:30:55 -0400575 strbuf_release(&sb);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400576 strbuf_release(&symref);
Eric Sunshineb979d952015-07-06 13:30:55 -0400577 strbuf_release(&sb_repo);
578 strbuf_release(&sb_git);
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700579 strbuf_release(&sb_name);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000580 strbuf_release(&realpath);
Eric Sunshineb979d952015-07-06 13:30:55 -0400581 return ret;
582}
583
Thomas Gummerer2c270022018-04-24 22:56:33 +0100584static void print_preparing_worktree_line(int detach,
585 const char *branch,
586 const char *new_branch,
587 int force_new_branch)
588{
589 if (force_new_branch) {
590 struct commit *commit = lookup_commit_reference_by_name(new_branch);
591 if (!commit)
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500592 fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
Thomas Gummerer2c270022018-04-24 22:56:33 +0100593 else
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500594 fprintf_ln(stderr, _("Preparing worktree (resetting branch '%s'; was at %s)"),
Thomas Gummerer2c270022018-04-24 22:56:33 +0100595 new_branch,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200596 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer2c270022018-04-24 22:56:33 +0100597 } else if (new_branch) {
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500598 fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
Thomas Gummerer2c270022018-04-24 22:56:33 +0100599 } else {
600 struct strbuf s = STRBUF_INIT;
601 if (!detach && !strbuf_check_branch_ref(&s, branch) &&
602 ref_exists(s.buf))
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500603 fprintf_ln(stderr, _("Preparing worktree (checking out '%s')"),
Thomas Gummerer2c270022018-04-24 22:56:33 +0100604 branch);
605 else {
606 struct commit *commit = lookup_commit_reference_by_name(branch);
607 if (!commit)
Jacob Abel7ab89182023-05-17 21:48:47 +0000608 BUG(_("unreachable: invalid reference: %s"), branch);
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500609 fprintf_ln(stderr, _("Preparing worktree (detached HEAD %s)"),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200610 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer2c270022018-04-24 22:56:33 +0100611 }
612 strbuf_release(&s);
613 }
614}
615
Thomas Gummerer6427f872018-04-24 22:56:34 +0100616static const char *dwim_branch(const char *path, const char **new_branch)
617{
618 int n;
Andrzej Huntaa1b6392021-03-14 18:47:37 +0000619 int branch_exists;
Thomas Gummerer6427f872018-04-24 22:56:34 +0100620 const char *s = worktree_basename(path, &n);
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100621 const char *branchname = xstrndup(s, n);
622 struct strbuf ref = STRBUF_INIT;
623
624 UNLEAK(branchname);
Andrzej Huntaa1b6392021-03-14 18:47:37 +0000625
626 branch_exists = !strbuf_check_branch_ref(&ref, branchname) &&
627 ref_exists(ref.buf);
628 strbuf_release(&ref);
629 if (branch_exists)
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100630 return branchname;
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100631
632 *new_branch = branchname;
Thomas Gummerer6427f872018-04-24 22:56:34 +0100633 if (guess_remote) {
634 struct object_id oid;
635 const char *remote =
Ævar Arnfjörð Bjarmason3c87aa92018-06-05 14:40:46 +0000636 unique_tracking_name(*new_branch, &oid, NULL);
Thomas Gummerer6427f872018-04-24 22:56:34 +0100637 return remote;
638 }
639 return NULL;
640}
641
Eric Sunshinefc563612015-07-06 13:30:50 -0400642static int add(int ac, const char **av, const char *prefix)
643{
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400644 struct add_opts opts;
645 const char *new_branch_force = NULL;
Jeff Kinge4da43b2017-03-20 21:28:49 -0400646 char *path;
647 const char *branch;
Thomas Gummererd861d342018-04-24 22:56:32 +0100648 const char *new_branch = NULL;
Thomas Gummerere284e892017-11-26 19:43:53 +0000649 const char *opt_track = NULL;
Stephen Manz0db49612021-07-15 02:32:30 +0000650 const char *lock_reason = NULL;
651 int keep_locked = 0;
Jacob Abel35f03832023-05-17 21:48:52 +0000652 int used_new_branch_options;
Eric Sunshinefc563612015-07-06 13:30:50 -0400653 struct option options[] = {
Nguyễn Thái Ngọc Duy12247812018-02-09 18:01:42 +0700654 OPT__FORCE(&opts.force,
655 N_("checkout <branch> even if already checked out in other worktree"),
Nguyễn Thái Ngọc Duyfc3d4e02018-02-09 18:02:20 +0700656 PARSE_OPT_NOCOMPLETE),
Thomas Gummererd861d342018-04-24 22:56:32 +0100657 OPT_STRING('b', NULL, &new_branch, N_("branch"),
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400658 N_("create a new branch")),
659 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
660 N_("create or reset a branch")),
Jacob Abel7ab89182023-05-17 21:48:47 +0000661 OPT_BOOL(0, "orphan", &opts.orphan, N_("create unborn/orphaned branch")),
Eric Sunshinec670aa42020-09-06 20:02:21 -0400662 OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")),
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000663 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
Stephen Manz0db49612021-07-15 02:32:30 +0000664 OPT_BOOL(0, "lock", &keep_locked, N_("keep the new working tree locked")),
665 OPT_STRING(0, "reason", &lock_reason, N_("string"),
666 N_("reason for locking")),
Elia Pinto371979c2018-08-15 20:56:30 +0000667 OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
Thomas Gummerere284e892017-11-26 19:43:53 +0000668 OPT_PASSTHRU(0, "track", &opt_track, NULL,
669 N_("set up tracking mode (see git-branch(1))"),
670 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
Thomas Gummerer71d66822017-11-29 20:04:50 +0000671 OPT_BOOL(0, "guess-remote", &guess_remote,
672 N_("try to match the new branch name with a remote-tracking branch")),
Eric Sunshinefc563612015-07-06 13:30:50 -0400673 OPT_END()
674 };
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100675 int ret;
Eric Sunshinefc563612015-07-06 13:30:50 -0400676
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400677 memset(&opts, 0, sizeof(opts));
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000678 opts.checkout = 1;
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200679 ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);
Thomas Gummererd861d342018-04-24 22:56:32 +0100680 if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
Jean-Noël Avilac4881822022-01-05 20:02:15 +0000681 die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
Jacob Abel7ab89182023-05-17 21:48:47 +0000682 if (opts.detach && opts.orphan)
683 die(_("options '%s', and '%s' cannot be used together"),
684 "--orphan", "--detach");
685 if (opts.orphan && opt_track)
686 die(_("'%s' and '%s' cannot be used together"), "--orphan", "--track");
687 if (opts.orphan && !opts.checkout)
688 die(_("'%s' and '%s' cannot be used together"), "--orphan",
689 "--no-checkout");
690 if (opts.orphan && ac == 2)
691 die(_("'%s' and '%s' cannot be used together"), "--orphan",
692 _("<commit-ish>"));
Stephen Manz0db49612021-07-15 02:32:30 +0000693 if (lock_reason && !keep_locked)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000694 die(_("the option '%s' requires '%s'"), "--reason", "--lock");
Stephen Manz0db49612021-07-15 02:32:30 +0000695 if (lock_reason)
696 opts.keep_locked = lock_reason;
697 else if (keep_locked)
698 opts.keep_locked = _("added with --lock");
699
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400700 if (ac < 1 || ac > 2)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200701 usage_with_options(git_worktree_add_usage, options);
Eric Sunshinefc563612015-07-06 13:30:50 -0400702
Jeff King116fb642017-03-20 21:22:28 -0400703 path = prefix_filename(prefix, av[0]);
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400704 branch = ac < 2 ? "HEAD" : av[1];
Jacob Abel35f03832023-05-17 21:48:52 +0000705 used_new_branch_options = new_branch || new_branch_force;
Eric Sunshinefc563612015-07-06 13:30:50 -0400706
Jordan DE GEA1a450e22016-05-27 15:17:08 +0200707 if (!strcmp(branch, "-"))
708 branch = "@{-1}";
709
Thomas Gummererd861d342018-04-24 22:56:32 +0100710 if (new_branch_force) {
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700711 struct strbuf symref = STRBUF_INIT;
712
Thomas Gummererd861d342018-04-24 22:56:32 +0100713 new_branch = new_branch_force;
Eric Sunshineeef005d2015-07-17 19:00:06 -0400714
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700715 if (!opts.force &&
Thomas Gummererd861d342018-04-24 22:56:32 +0100716 !strbuf_check_branch_ref(&symref, new_branch) &&
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700717 ref_exists(symref.buf))
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700718 die_if_checked_out(symref.buf, 0);
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700719 strbuf_release(&symref);
720 }
721
Jacob Abel7ab89182023-05-17 21:48:47 +0000722 if (opts.orphan && !new_branch) {
723 int n;
724 const char *s = worktree_basename(path, &n);
725 new_branch = xstrndup(s, n);
726 } else if (new_branch || opts.detach || opts.orphan) {
727 // No-op
728 } else if (ac < 2) {
Thomas Gummerer6427f872018-04-24 22:56:34 +0100729 const char *s = dwim_branch(path, &new_branch);
730 if (s)
731 branch = s;
Jacob Abel7ab89182023-05-17 21:48:47 +0000732 } else if (ac == 2) {
Thomas Gummerer4e853332017-11-26 19:43:54 +0000733 struct object_id oid;
734 struct commit *commit;
735 const char *remote;
736
737 commit = lookup_commit_reference_by_name(branch);
738 if (!commit) {
Ævar Arnfjörð Bjarmason3c87aa92018-06-05 14:40:46 +0000739 remote = unique_tracking_name(branch, &oid, NULL);
Thomas Gummerer4e853332017-11-26 19:43:54 +0000740 if (remote) {
Thomas Gummererd861d342018-04-24 22:56:32 +0100741 new_branch = branch;
Thomas Gummerer4e853332017-11-26 19:43:54 +0000742 branch = remote;
743 }
744 }
745 }
Jacob Abel7ab89182023-05-17 21:48:47 +0000746
747 if (!opts.orphan && !lookup_commit_reference_by_name(branch)) {
Jacob Abel35f03832023-05-17 21:48:52 +0000748 int attempt_hint = !opts.quiet && (ac < 2);
749 if (attempt_hint && used_new_branch_options) {
750 advise_if_enabled(ADVICE_WORKTREE_ADD_ORPHAN,
751 WORKTREE_ADD_ORPHAN_WITH_DASH_B_HINT_TEXT,
752 new_branch, path);
753 } else if (attempt_hint) {
754 advise_if_enabled(ADVICE_WORKTREE_ADD_ORPHAN,
755 WORKTREE_ADD_ORPHAN_NO_DASH_B_HINT_TEXT, path);
756 }
Jacob Abel7ab89182023-05-17 21:48:47 +0000757 die(_("invalid reference: %s"), branch);
758 }
759
Elia Pinto371979c2018-08-15 20:56:30 +0000760 if (!opts.quiet)
761 print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
Thomas Gummerer2c270022018-04-24 22:56:33 +0100762
Jacob Abel7ab89182023-05-17 21:48:47 +0000763 if (opts.orphan) {
764 branch = new_branch;
765 } else if (new_branch) {
René Scharfe542aa252016-08-05 22:38:44 +0200766 struct child_process cp = CHILD_PROCESS_INIT;
Eric Sunshinec2842432015-07-17 19:00:10 -0400767 cp.git_cmd = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -0400768 strvec_push(&cp.args, "branch");
Thomas Gummererd861d342018-04-24 22:56:32 +0100769 if (new_branch_force)
Jeff King22f9b7f2020-07-28 16:24:27 -0400770 strvec_push(&cp.args, "--force");
Elia Pinto371979c2018-08-15 20:56:30 +0000771 if (opts.quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400772 strvec_push(&cp.args, "--quiet");
773 strvec_push(&cp.args, new_branch);
774 strvec_push(&cp.args, branch);
Thomas Gummerere284e892017-11-26 19:43:53 +0000775 if (opt_track)
Jeff King22f9b7f2020-07-28 16:24:27 -0400776 strvec_push(&cp.args, opt_track);
Eric Sunshinec2842432015-07-17 19:00:10 -0400777 if (run_command(&cp))
778 return -1;
Thomas Gummererd861d342018-04-24 22:56:32 +0100779 branch = new_branch;
Thomas Gummerere284e892017-11-26 19:43:53 +0000780 } else if (opt_track) {
781 die(_("--[no-]track can only be used if a new branch is created"));
Eric Sunshinec2842432015-07-17 19:00:10 -0400782 }
Eric Sunshinefc563612015-07-06 13:30:50 -0400783
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100784 ret = add_worktree(path, branch, &opts);
785 free(path);
786 return ret;
Eric Sunshinefc563612015-07-06 13:30:50 -0400787}
788
Phillip Woodd97eb302022-03-31 16:21:28 +0000789static void show_worktree_porcelain(struct worktree *wt, int line_terminator)
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400790{
Rafael Silva862c7232021-01-27 09:03:08 +0100791 const char *reason;
792
Phillip Woodd97eb302022-03-31 16:21:28 +0000793 printf("worktree %s%c", wt->path, line_terminator);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400794 if (wt->is_bare)
Phillip Woodd97eb302022-03-31 16:21:28 +0000795 printf("bare%c", line_terminator);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400796 else {
Phillip Woodd97eb302022-03-31 16:21:28 +0000797 printf("HEAD %s%c", oid_to_hex(&wt->head_oid), line_terminator);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400798 if (wt->is_detached)
Phillip Woodd97eb302022-03-31 16:21:28 +0000799 printf("detached%c", line_terminator);
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700800 else if (wt->head_ref)
Phillip Woodd97eb302022-03-31 16:21:28 +0000801 printf("branch %s%c", wt->head_ref, line_terminator);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400802 }
Rafael Silva862c7232021-01-27 09:03:08 +0100803
804 reason = worktree_lock_reason(wt);
Phillip Woodd97eb302022-03-31 16:21:28 +0000805 if (reason) {
806 fputs("locked", stdout);
807 if (*reason) {
808 fputc(' ', stdout);
809 write_name_quoted(reason, stdout, line_terminator);
810 } else {
811 fputc(line_terminator, stdout);
812 }
813 }
Rafael Silva862c7232021-01-27 09:03:08 +0100814
Rafael Silva9b19a582021-01-27 09:03:09 +0100815 reason = worktree_prune_reason(wt, expire);
816 if (reason)
Phillip Woodd97eb302022-03-31 16:21:28 +0000817 printf("prunable %s%c", reason, line_terminator);
Rafael Silva9b19a582021-01-27 09:03:09 +0100818
Phillip Woodd97eb302022-03-31 16:21:28 +0000819 fputc(line_terminator, stdout);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400820}
821
822static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
823{
824 struct strbuf sb = STRBUF_INIT;
825 int cur_path_len = strlen(wt->path);
826 int path_adj = cur_path_len - utf8_strwidth(wt->path);
Rafael Silva076b4442021-01-27 09:03:10 +0100827 const char *reason;
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400828
829 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
830 if (wt->is_bare)
831 strbuf_addstr(&sb, "(bare)");
832 else {
833 strbuf_addf(&sb, "%-*s ", abbrev_len,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200834 repo_find_unique_abbrev(the_repository, &wt->head_oid, DEFAULT_ABBREV));
Nguyễn Thái Ngọc Duy96f09e22016-11-28 16:36:53 +0700835 if (wt->is_detached)
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400836 strbuf_addstr(&sb, "(detached HEAD)");
Johannes Schindelin2e11f582017-05-04 15:59:13 +0200837 else if (wt->head_ref) {
838 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
839 strbuf_addf(&sb, "[%s]", ref);
840 free(ref);
841 } else
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700842 strbuf_addstr(&sb, "(error)");
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400843 }
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400844
Rafael Silva076b4442021-01-27 09:03:10 +0100845 reason = worktree_lock_reason(wt);
846 if (verbose && reason && *reason)
847 strbuf_addf(&sb, "\n\tlocked: %s", reason);
848 else if (reason)
Rafael Silvac57b3362020-10-11 10:11:52 +0000849 strbuf_addstr(&sb, " locked");
850
Rafael Silva076b4442021-01-27 09:03:10 +0100851 reason = worktree_prune_reason(wt, expire);
852 if (verbose && reason)
853 strbuf_addf(&sb, "\n\tprunable: %s", reason);
854 else if (reason)
Rafael Silva9b19a582021-01-27 09:03:09 +0100855 strbuf_addstr(&sb, " prunable");
856
Rafael Silvac57b3362020-10-11 10:11:52 +0000857 printf("%s\n", sb.buf);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400858 strbuf_release(&sb);
859}
860
861static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
862{
863 int i;
864
865 for (i = 0; wt[i]; i++) {
866 int sha1_len;
867 int path_len = strlen(wt[i]->path);
868
869 if (path_len > *maxlen)
870 *maxlen = path_len;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200871 sha1_len = strlen(repo_find_unique_abbrev(the_repository, &wt[i]->head_oid, *abbrev));
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400872 if (sha1_len > *abbrev)
873 *abbrev = sha1_len;
874 }
875}
876
Eric Sunshined9c54c22020-06-19 19:35:43 -0400877static int pathcmp(const void *a_, const void *b_)
878{
879 const struct worktree *const *a = a_;
880 const struct worktree *const *b = b_;
881 return fspathcmp((*a)->path, (*b)->path);
882}
883
884static void pathsort(struct worktree **wt)
885{
886 int n = 0;
887 struct worktree **p = wt;
888
889 while (*p++)
890 n++;
891 QSORT(wt, n, pathcmp);
892}
893
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400894static int list(int ac, const char **av, const char *prefix)
895{
896 int porcelain = 0;
Phillip Woodd97eb302022-03-31 16:21:28 +0000897 int line_terminator = '\n';
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400898
899 struct option options[] = {
900 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
Rafael Silva076b4442021-01-27 09:03:10 +0100901 OPT__VERBOSE(&verbose, N_("show extended annotations and reasons, if available")),
Rafael Silva9b19a582021-01-27 09:03:09 +0100902 OPT_EXPIRY_DATE(0, "expire", &expire,
903 N_("add 'prunable' annotation to worktrees older than <time>")),
Phillip Woodd97eb302022-03-31 16:21:28 +0000904 OPT_SET_INT('z', NULL, &line_terminator,
905 N_("terminate records with a NUL character"), '\0'),
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400906 OPT_END()
907 };
908
Rafael Silva9b19a582021-01-27 09:03:09 +0100909 expire = TIME_MAX;
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200910 ac = parse_options(ac, av, prefix, options, git_worktree_list_usage, 0);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400911 if (ac)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200912 usage_with_options(git_worktree_list_usage, options);
Rafael Silva076b4442021-01-27 09:03:10 +0100913 else if (verbose && porcelain)
Jean-Noël Avila43ea6352022-01-05 20:02:14 +0000914 die(_("options '%s' and '%s' cannot be used together"), "--verbose", "--porcelain");
Phillip Woodd97eb302022-03-31 16:21:28 +0000915 else if (!line_terminator && !porcelain)
916 die(_("the option '%s' requires '%s'"), "-z", "--porcelain");
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400917 else {
Eric Sunshine03f24652020-06-19 19:35:44 -0400918 struct worktree **worktrees = get_worktrees();
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400919 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
920
Eric Sunshined9c54c22020-06-19 19:35:43 -0400921 /* sort worktrees by path but keep main worktree at top */
922 pathsort(worktrees + 1);
923
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400924 if (!porcelain)
925 measure_widths(worktrees, &abbrev, &path_maxlen);
926
927 for (i = 0; worktrees[i]; i++) {
928 if (porcelain)
Phillip Woodd97eb302022-03-31 16:21:28 +0000929 show_worktree_porcelain(worktrees[i],
930 line_terminator);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400931 else
932 show_worktree(worktrees[i], path_maxlen, abbrev);
933 }
934 free_worktrees(worktrees);
935 }
936 return 0;
937}
938
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700939static int lock_worktree(int ac, const char **av, const char *prefix)
940{
941 const char *reason = "", *old_reason;
942 struct option options[] = {
943 OPT_STRING(0, "reason", &reason, N_("string"),
944 N_("reason for locking")),
945 OPT_END()
946 };
947 struct worktree **worktrees, *wt;
948
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200949 ac = parse_options(ac, av, prefix, options, git_worktree_lock_usage, 0);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700950 if (ac != 1)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200951 usage_with_options(git_worktree_lock_usage, options);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700952
Eric Sunshine03f24652020-06-19 19:35:44 -0400953 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700954 wt = find_worktree(worktrees, prefix, av[0]);
955 if (!wt)
956 die(_("'%s' is not a working tree"), av[0]);
957 if (is_main_worktree(wt))
958 die(_("The main working tree cannot be locked or unlocked"));
959
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700960 old_reason = worktree_lock_reason(wt);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700961 if (old_reason) {
962 if (*old_reason)
963 die(_("'%s' is already locked, reason: %s"),
964 av[0], old_reason);
965 die(_("'%s' is already locked"), av[0]);
966 }
967
968 write_file(git_common_path("worktrees/%s/locked", wt->id),
969 "%s", reason);
970 free_worktrees(worktrees);
971 return 0;
972}
973
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700974static int unlock_worktree(int ac, const char **av, const char *prefix)
975{
976 struct option options[] = {
977 OPT_END()
978 };
979 struct worktree **worktrees, *wt;
980 int ret;
981
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200982 ac = parse_options(ac, av, prefix, options, git_worktree_unlock_usage, 0);
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700983 if (ac != 1)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +0200984 usage_with_options(git_worktree_unlock_usage, options);
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700985
Eric Sunshine03f24652020-06-19 19:35:44 -0400986 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700987 wt = find_worktree(worktrees, prefix, av[0]);
988 if (!wt)
989 die(_("'%s' is not a working tree"), av[0]);
990 if (is_main_worktree(wt))
991 die(_("The main working tree cannot be locked or unlocked"));
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700992 if (!worktree_lock_reason(wt))
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700993 die(_("'%s' is not locked"), av[0]);
994 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
995 free_worktrees(worktrees);
996 return ret;
997}
998
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700999static void validate_no_submodules(const struct worktree *wt)
1000{
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +01001001 struct index_state istate = INDEX_STATE_INIT(the_repository);
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +07001002 struct strbuf path = STRBUF_INIT;
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001003 int i, found_submodules = 0;
1004
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +07001005 if (is_directory(worktree_git_path(wt, "modules"))) {
1006 /*
1007 * There could be false positives, e.g. the "modules"
1008 * directory exists but is empty. But it's a rare case and
1009 * this simpler check is probably good enough for now.
1010 */
1011 found_submodules = 1;
1012 } else if (read_index_from(&istate, worktree_git_path(wt, "index"),
1013 get_worktree_git_dir(wt)) > 0) {
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001014 for (i = 0; i < istate.cache_nr; i++) {
1015 struct cache_entry *ce = istate.cache[i];
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +07001016 int err;
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001017
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +07001018 if (!S_ISGITLINK(ce->ce_mode))
1019 continue;
1020
1021 strbuf_reset(&path);
1022 strbuf_addf(&path, "%s/%s", wt->path, ce->name);
1023 if (!is_submodule_populated_gently(path.buf, &err))
1024 continue;
1025
1026 found_submodules = 1;
1027 break;
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001028 }
1029 }
1030 discard_index(&istate);
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +07001031 strbuf_release(&path);
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001032
1033 if (found_submodules)
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001034 die(_("working trees containing submodules cannot be moved or removed"));
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001035}
1036
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001037static int move_worktree(int ac, const char **av, const char *prefix)
1038{
Eric Sunshine68a6b3a2018-08-28 17:20:24 -04001039 int force = 0;
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001040 struct option options[] = {
Eric Sunshine68a6b3a2018-08-28 17:20:24 -04001041 OPT__FORCE(&force,
1042 N_("force move even if worktree is dirty or locked"),
1043 PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001044 OPT_END()
1045 };
1046 struct worktree **worktrees, *wt;
1047 struct strbuf dst = STRBUF_INIT;
1048 struct strbuf errmsg = STRBUF_INIT;
Eric Sunshine68a6b3a2018-08-28 17:20:24 -04001049 const char *reason = NULL;
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001050 char *path;
1051
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +02001052 ac = parse_options(ac, av, prefix, options, git_worktree_move_usage,
1053 0);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001054 if (ac != 2)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +02001055 usage_with_options(git_worktree_move_usage, options);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001056
1057 path = prefix_filename(prefix, av[1]);
1058 strbuf_addstr(&dst, path);
1059 free(path);
1060
Eric Sunshine03f24652020-06-19 19:35:44 -04001061 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001062 wt = find_worktree(worktrees, prefix, av[0]);
1063 if (!wt)
1064 die(_("'%s' is not a working tree"), av[0]);
1065 if (is_main_worktree(wt))
1066 die(_("'%s' is a main working tree"), av[0]);
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +07001067 if (is_directory(dst.buf)) {
1068 const char *sep = find_last_dir_sep(wt->path);
1069
1070 if (!sep)
1071 die(_("could not figure out destination name from '%s'"),
1072 wt->path);
1073 strbuf_trim_trailing_dir_sep(&dst);
1074 strbuf_addstr(&dst, sep);
1075 }
Eric Sunshine810382e2020-06-10 02:30:49 -04001076 check_candidate_path(dst.buf, force, worktrees, "move");
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001077
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +07001078 validate_no_submodules(wt);
1079
Eric Sunshine68a6b3a2018-08-28 17:20:24 -04001080 if (force < 2)
Nickolai Belakovskid236f122018-10-29 23:24:09 -07001081 reason = worktree_lock_reason(wt);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001082 if (reason) {
1083 if (*reason)
Eric Sunshine68a6b3a2018-08-28 17:20:24 -04001084 die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001085 reason);
Eric Sunshine68a6b3a2018-08-28 17:20:24 -04001086 die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001087 }
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +07001088 if (validate_worktree(wt, &errmsg, 0))
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001089 die(_("validation failed, cannot move working tree: %s"),
1090 errmsg.buf);
1091 strbuf_release(&errmsg);
1092
1093 if (rename(wt->path, dst.buf) == -1)
1094 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
1095
1096 update_worktree_location(wt, dst.buf);
1097
1098 strbuf_release(&dst);
1099 free_worktrees(worktrees);
1100 return 0;
1101}
1102
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001103/*
1104 * Note, "git status --porcelain" is used to determine if it's safe to
1105 * delete a whole worktree. "git status" does not ignore user
1106 * configuration, so if a normal "git status" shows "clean" for the
1107 * user, then it's ok to remove it.
1108 *
1109 * This assumption may be a bad one. We may want to ignore
1110 * (potentially bad) user settings and only delete a worktree when
1111 * it's absolutely safe to do so from _our_ point of view because we
1112 * know better.
1113 */
1114static void check_clean_worktree(struct worktree *wt,
1115 const char *original_path)
1116{
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001117 struct child_process cp;
1118 char buf[1];
1119 int ret;
1120
1121 /*
1122 * Until we sort this out, all submodules are "dirty" and
1123 * will abort this function.
1124 */
1125 validate_no_submodules(wt);
1126
Jeff King27ed6cc2020-08-27 01:25:04 -04001127 child_process_init(&cp);
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001128 strvec_pushf(&cp.env, "%s=%s/.git",
Jeff Kingf6d89422020-07-28 16:26:31 -04001129 GIT_DIR_ENVIRONMENT, wt->path);
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001130 strvec_pushf(&cp.env, "%s=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001131 GIT_WORK_TREE_ENVIRONMENT, wt->path);
Jeff King22f9b7f2020-07-28 16:24:27 -04001132 strvec_pushl(&cp.args, "status",
Jeff Kingf6d89422020-07-28 16:26:31 -04001133 "--porcelain", "--ignore-submodules=none",
1134 NULL);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001135 cp.git_cmd = 1;
1136 cp.dir = wt->path;
1137 cp.out = -1;
1138 ret = start_command(&cp);
1139 if (ret)
1140 die_errno(_("failed to run 'git status' on '%s'"),
1141 original_path);
1142 ret = xread(cp.out, buf, sizeof(buf));
1143 if (ret)
SZEDER Gábor507e5472019-08-13 20:02:44 +02001144 die(_("'%s' contains modified or untracked files, use --force to delete it"),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001145 original_path);
1146 close(cp.out);
1147 ret = finish_command(&cp);
1148 if (ret)
1149 die_errno(_("failed to run 'git status' on '%s', code %d"),
1150 original_path, ret);
1151}
1152
1153static int delete_git_work_tree(struct worktree *wt)
1154{
1155 struct strbuf sb = STRBUF_INIT;
1156 int ret = 0;
1157
1158 strbuf_addstr(&sb, wt->path);
1159 if (remove_dir_recursively(&sb, 0)) {
1160 error_errno(_("failed to delete '%s'"), sb.buf);
1161 ret = -1;
1162 }
1163 strbuf_release(&sb);
1164 return ret;
1165}
1166
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001167static int remove_worktree(int ac, const char **av, const char *prefix)
1168{
1169 int force = 0;
1170 struct option options[] = {
Stefan Bellerd228eea2018-04-17 11:19:39 -07001171 OPT__FORCE(&force,
Eric Sunshinef4143102018-08-28 17:20:25 -04001172 N_("force removal even if worktree is dirty or locked"),
Stefan Bellerd228eea2018-04-17 11:19:39 -07001173 PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001174 OPT_END()
1175 };
1176 struct worktree **worktrees, *wt;
1177 struct strbuf errmsg = STRBUF_INIT;
Eric Sunshinef4143102018-08-28 17:20:25 -04001178 const char *reason = NULL;
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001179 int ret = 0;
1180
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +02001181 ac = parse_options(ac, av, prefix, options, git_worktree_remove_usage, 0);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001182 if (ac != 1)
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +02001183 usage_with_options(git_worktree_remove_usage, options);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001184
Eric Sunshine03f24652020-06-19 19:35:44 -04001185 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001186 wt = find_worktree(worktrees, prefix, av[0]);
1187 if (!wt)
1188 die(_("'%s' is not a working tree"), av[0]);
1189 if (is_main_worktree(wt))
1190 die(_("'%s' is a main working tree"), av[0]);
Eric Sunshinef4143102018-08-28 17:20:25 -04001191 if (force < 2)
Nickolai Belakovskid236f122018-10-29 23:24:09 -07001192 reason = worktree_lock_reason(wt);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001193 if (reason) {
1194 if (*reason)
Eric Sunshinef4143102018-08-28 17:20:25 -04001195 die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001196 reason);
Eric Sunshinef4143102018-08-28 17:20:25 -04001197 die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001198 }
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +07001199 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001200 die(_("validation failed, cannot remove working tree: %s"),
1201 errmsg.buf);
1202 strbuf_release(&errmsg);
1203
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +07001204 if (file_exists(wt->path)) {
1205 if (!force)
1206 check_clean_worktree(wt, av[0]);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001207
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +07001208 ret |= delete_git_work_tree(wt);
1209 }
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001210 /*
1211 * continue on even if ret is non-zero, there's no going back
1212 * from here.
1213 */
Eric Sunshine602aaed2018-08-28 17:20:20 -04001214 ret |= delete_git_dir(wt->id);
Eric Sunshine3a540432018-08-28 17:20:26 -04001215 delete_worktrees_dir_if_empty();
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001216
1217 free_worktrees(worktrees);
1218 return ret;
1219}
1220
Eric Sunshinebdd1f3e2020-08-31 02:57:57 -04001221static void report_repair(int iserr, const char *path, const char *msg, void *cb_data)
1222{
1223 if (!iserr) {
Eric Sunshineda8fb6b2021-12-02 22:44:19 -05001224 fprintf_ln(stderr, _("repair: %s: %s"), msg, path);
Eric Sunshinebdd1f3e2020-08-31 02:57:57 -04001225 } else {
1226 int *exit_status = (int *)cb_data;
1227 fprintf_ln(stderr, _("error: %s: %s"), msg, path);
1228 *exit_status = 1;
1229 }
1230}
1231
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001232static int repair(int ac, const char **av, const char *prefix)
1233{
Eric Sunshineb214ab52020-08-31 02:57:58 -04001234 const char **p;
1235 const char *self[] = { ".", NULL };
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001236 struct option options[] = {
1237 OPT_END()
1238 };
1239 int rc = 0;
1240
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +02001241 ac = parse_options(ac, av, prefix, options, git_worktree_repair_usage, 0);
Eric Sunshineb214ab52020-08-31 02:57:58 -04001242 p = ac > 0 ? av : self;
1243 for (; *p; p++)
1244 repair_worktree_at_path(*p, report_repair, &rc);
Eric Sunshinecf76bae2020-12-21 03:16:01 -05001245 repair_worktrees(report_repair, &rc);
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001246 return rc;
1247}
1248
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001249int cmd_worktree(int ac, const char **av, const char *prefix)
1250{
SZEDER Gábor398c4ff2022-08-19 18:04:11 +02001251 parse_opt_subcommand_fn *fn = NULL;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001252 struct option options[] = {
SZEDER Gábor398c4ff2022-08-19 18:04:11 +02001253 OPT_SUBCOMMAND("add", &fn, add),
1254 OPT_SUBCOMMAND("prune", &fn, prune),
1255 OPT_SUBCOMMAND("list", &fn, list),
1256 OPT_SUBCOMMAND("lock", &fn, lock_worktree),
1257 OPT_SUBCOMMAND("unlock", &fn, unlock_worktree),
1258 OPT_SUBCOMMAND("move", &fn, move_worktree),
1259 OPT_SUBCOMMAND("remove", &fn, remove_worktree),
1260 OPT_SUBCOMMAND("repair", &fn, repair),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001261 OPT_END()
1262 };
1263
Thomas Gummerere92445a2017-11-29 20:04:51 +00001264 git_config(git_worktree_config, NULL);
Junio C Hamanod49028e2016-09-26 23:49:39 -07001265
Nguyễn Thái Ngọc Duy0409e0b2016-05-22 16:33:56 +07001266 if (!prefix)
1267 prefix = "";
SZEDER Gábor398c4ff2022-08-19 18:04:11 +02001268
Ævar Arnfjörð Bjarmason0afd5562022-10-13 17:39:25 +02001269 ac = parse_options(ac, av, prefix, options, git_worktree_usage, 0);
SZEDER Gábor398c4ff2022-08-19 18:04:11 +02001270 return fn(ac, av, prefix);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001271}