blob: 9a5204857695bd608e12b5010114abc2ea664f66 [file] [log] [blame]
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001#include "cache.h"
Thomas Gummerer4e853332017-11-26 19:43:54 +00002#include "checkout.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07004#include "builtin.h"
5#include "dir.h"
6#include "parse-options.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04007#include "strvec.h"
Eric Sunshinef7c9dac2015-07-17 19:00:13 -04008#include "branch.h"
9#include "refs.h"
Eric Sunshinefc563612015-07-06 13:30:50 -040010#include "run-command.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 21:03:26 +020011#include "hook.h"
Eric Sunshineb979d952015-07-06 13:30:55 -040012#include "sigchain.h"
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +070013#include "submodule.h"
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040014#include "utf8.h"
15#include "worktree.h"
Rafael Silva862c7232021-01-27 09:03:08 +010016#include "quote.h"
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070017
18static const char * const worktree_usage[] = {
Junio C Hamanob780e442018-01-17 10:32:32 -080019 N_("git worktree add [<options>] <path> [<commit-ish>]"),
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040020 N_("git worktree list [<options>]"),
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +070021 N_("git worktree lock [<options>] <path>"),
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +070022 N_("git worktree move <worktree> <new-path>"),
Nguyễn Thái Ngọc Duy7b722d92016-05-22 16:33:53 +070023 N_("git worktree prune [<options>]"),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +070024 N_("git worktree remove [<options>] <worktree>"),
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +070025 N_("git worktree unlock <path>"),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070026 NULL
27};
28
Eric Sunshine5dd6e232015-07-17 19:00:07 -040029struct add_opts {
30 int force;
31 int detach;
Elia Pinto371979c2018-08-15 20:56:30 +000032 int quiet;
Ray Zhangef2a0ac2016-03-29 10:11:01 +000033 int checkout;
Stephen Manz0db49612021-07-15 02:32:30 +000034 const char *keep_locked;
Eric Sunshine5dd6e232015-07-17 19:00:07 -040035};
36
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070037static int show_only;
38static int verbose;
Thomas Gummerere92445a2017-11-29 20:04:51 +000039static int guess_remote;
Johannes Schindelindddbad72017-04-26 21:29:31 +020040static timestamp_t expire;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070041
Thomas Gummerere92445a2017-11-29 20:04:51 +000042static int git_worktree_config(const char *var, const char *value, void *cb)
43{
44 if (!strcmp(var, "worktree.guessremote")) {
45 guess_remote = git_config_bool(var, value);
46 return 0;
47 }
48
49 return git_default_config(var, value, cb);
50}
51
Eric Sunshine602aaed2018-08-28 17:20:20 -040052static int delete_git_dir(const char *id)
Eric Sunshinee5353be2018-08-28 17:20:19 -040053{
54 struct strbuf sb = STRBUF_INIT;
Eric Sunshine602aaed2018-08-28 17:20:20 -040055 int ret;
Eric Sunshinee5353be2018-08-28 17:20:19 -040056
Eric Sunshine602aaed2018-08-28 17:20:20 -040057 strbuf_addstr(&sb, git_common_path("worktrees/%s", id));
58 ret = remove_dir_recursively(&sb, 0);
59 if (ret < 0 && errno == ENOTDIR)
60 ret = unlink(sb.buf);
61 if (ret)
Eric Sunshinee5353be2018-08-28 17:20:19 -040062 error_errno(_("failed to delete '%s'"), sb.buf);
Eric Sunshinee5353be2018-08-28 17:20:19 -040063 strbuf_release(&sb);
64 return ret;
65}
66
Eric Sunshine3a540432018-08-28 17:20:26 -040067static void delete_worktrees_dir_if_empty(void)
68{
69 rmdir(git_path("worktrees")); /* ignore failed removal */
70}
71
Eric Sunshinedd9609a2020-06-10 02:30:45 -040072static void prune_worktree(const char *id, const char *reason)
73{
74 if (show_only || verbose)
Eric Sunshineda8fb6b2021-12-02 22:44:19 -050075 fprintf_ln(stderr, _("Removing %s/%s: %s"), "worktrees", id, reason);
Eric Sunshinedd9609a2020-06-10 02:30:45 -040076 if (!show_only)
77 delete_git_dir(id);
78}
79
Eric Sunshine4a3ce472020-06-10 02:30:46 -040080static int prune_cmp(const void *a, const void *b)
81{
82 const struct string_list_item *x = a;
83 const struct string_list_item *y = b;
84 int c;
85
86 if ((c = fspathcmp(x->string, y->string)))
87 return c;
Eric Sunshine916133e2020-06-10 02:30:47 -040088 /*
89 * paths same; prune_dupes() removes all but the first worktree entry
90 * having the same path, so sort main worktree ('util' is NULL) above
91 * linked worktrees ('util' not NULL) since main worktree can't be
92 * removed
93 */
94 if (!x->util)
95 return -1;
96 if (!y->util)
97 return 1;
Eric Sunshine4a3ce472020-06-10 02:30:46 -040098 /* paths same; sort by .git/worktrees/<id> */
99 return strcmp(x->util, y->util);
100}
101
102static void prune_dups(struct string_list *l)
103{
104 int i;
105
106 QSORT(l->items, l->nr, prune_cmp);
107 for (i = 1; i < l->nr; i++) {
108 if (!fspathcmp(l->items[i].string, l->items[i - 1].string))
109 prune_worktree(l->items[i].util, "duplicate entry");
110 }
111}
112
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700113static void prune_worktrees(void)
114{
115 struct strbuf reason = STRBUF_INIT;
Eric Sunshine916133e2020-06-10 02:30:47 -0400116 struct strbuf main_path = STRBUF_INIT;
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400117 struct string_list kept = STRING_LIST_INIT_NODUP;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700118 DIR *dir = opendir(git_path("worktrees"));
119 struct dirent *d;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700120 if (!dir)
121 return;
Elijah Newrenb548f0f2021-05-12 17:28:22 +0000122 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400123 char *path;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700124 strbuf_reset(&reason);
Rafael Silvaa29a8b72021-01-19 22:27:33 +0100125 if (should_prune_worktree(d->d_name, &reason, &path, expire))
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400126 prune_worktree(d->d_name, reason.buf);
127 else if (path)
128 string_list_append(&kept, path)->util = xstrdup(d->d_name);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700129 }
130 closedir(dir);
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400131
Eric Sunshine916133e2020-06-10 02:30:47 -0400132 strbuf_add_absolute_path(&main_path, get_git_common_dir());
133 /* massage main worktree absolute path to match 'gitdir' content */
134 strbuf_strip_suffix(&main_path, "/.");
135 string_list_append(&kept, strbuf_detach(&main_path, NULL));
Eric Sunshine4a3ce472020-06-10 02:30:46 -0400136 prune_dups(&kept);
137 string_list_clear(&kept, 1);
138
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700139 if (!show_only)
Eric Sunshine3a540432018-08-28 17:20:26 -0400140 delete_worktrees_dir_if_empty();
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700141 strbuf_release(&reason);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700142}
143
144static int prune(int ac, const char **av, const char *prefix)
145{
146 struct option options[] = {
147 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
Patrick Steinhardt2488dca2017-02-06 14:13:59 +0100148 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700149 OPT_EXPIRY_DATE(0, "expire", &expire,
Patrick Steinhardt2488dca2017-02-06 14:13:59 +0100150 N_("expire working trees older than <time>")),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700151 OPT_END()
152 };
153
Johannes Schindelindddbad72017-04-26 21:29:31 +0200154 expire = TIME_MAX;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700155 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
156 if (ac)
157 usage_with_options(worktree_usage, options);
158 prune_worktrees();
159 return 0;
160}
161
Eric Sunshineb979d952015-07-06 13:30:55 -0400162static char *junk_work_tree;
163static char *junk_git_dir;
164static int is_junk;
165static pid_t junk_pid;
166
167static void remove_junk(void)
168{
169 struct strbuf sb = STRBUF_INIT;
170 if (!is_junk || getpid() != junk_pid)
171 return;
172 if (junk_git_dir) {
173 strbuf_addstr(&sb, junk_git_dir);
174 remove_dir_recursively(&sb, 0);
175 strbuf_reset(&sb);
176 }
177 if (junk_work_tree) {
178 strbuf_addstr(&sb, junk_work_tree);
179 remove_dir_recursively(&sb, 0);
180 }
181 strbuf_release(&sb);
182}
183
184static void remove_junk_on_signal(int signo)
185{
186 remove_junk();
187 sigchain_pop(signo);
188 raise(signo);
189}
190
Eric Sunshinef5682b22015-07-06 13:30:57 -0400191static const char *worktree_basename(const char *path, int *olen)
192{
193 const char *name;
194 int len;
195
196 len = strlen(path);
197 while (len && is_dir_sep(path[len - 1]))
198 len--;
199
200 for (name = path + len - 1; name > path; name--)
201 if (is_dir_sep(*name)) {
202 name++;
203 break;
204 }
205
206 *olen = len;
207 return name;
208}
209
Eric Sunshined179af62020-06-10 02:30:48 -0400210/* check that path is viable location for worktree */
211static void check_candidate_path(const char *path,
212 int force,
213 struct worktree **worktrees,
214 const char *cmd)
Eric Sunshine45059e62018-08-28 17:20:21 -0400215{
Eric Sunshinecb56f552018-08-28 17:20:22 -0400216 struct worktree *wt;
217 int locked;
218
Eric Sunshine45059e62018-08-28 17:20:21 -0400219 if (file_exists(path) && !is_empty_dir(path))
220 die(_("'%s' already exists"), path);
Eric Sunshinecb56f552018-08-28 17:20:22 -0400221
Eric Sunshinebb69b3b2020-02-24 04:08:48 -0500222 wt = find_worktree_by_path(worktrees, path);
Eric Sunshinecb56f552018-08-28 17:20:22 -0400223 if (!wt)
Eric Sunshined179af62020-06-10 02:30:48 -0400224 return;
Eric Sunshinecb56f552018-08-28 17:20:22 -0400225
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700226 locked = !!worktree_lock_reason(wt);
Eric Sunshined179af62020-06-10 02:30:48 -0400227 if ((!locked && force) || (locked && force > 1)) {
Eric Sunshinee19831c2018-08-28 17:20:23 -0400228 if (delete_git_dir(wt->id))
Eric Sunshined179af62020-06-10 02:30:48 -0400229 die(_("unusable worktree destination '%s'"), path);
230 return;
Eric Sunshinee19831c2018-08-28 17:20:23 -0400231 }
232
Eric Sunshinecb56f552018-08-28 17:20:22 -0400233 if (locked)
Matheus Tavaresb86339b2020-11-20 12:09:39 -0300234 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 -0400235 else
Matheus Tavaresb86339b2020-11-20 12:09:39 -0300236 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 -0400237}
238
Eric Sunshine80a05482015-07-17 19:00:12 -0400239static int add_worktree(const char *path, const char *refname,
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400240 const struct add_opts *opts)
Eric Sunshineb979d952015-07-06 13:30:55 -0400241{
242 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000243 struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT;
Eric Sunshineb979d952015-07-06 13:30:55 -0400244 const char *name;
René Scharfe542aa252016-08-05 22:38:44 +0200245 struct child_process cp = CHILD_PROCESS_INIT;
Jeff King22f9b7f2020-07-28 16:24:27 -0400246 struct strvec child_env = STRVEC_INIT;
Michal Suchanek7af01f22019-02-20 17:16:48 +0100247 unsigned int counter = 0;
248 int len, ret;
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400249 struct strbuf symref = STRBUF_INIT;
250 struct commit *commit = NULL;
Eric Sunshineade546b2017-12-07 16:20:17 -0500251 int is_branch = 0;
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700252 struct strbuf sb_name = STRBUF_INIT;
Eric Sunshined179af62020-06-10 02:30:48 -0400253 struct worktree **worktrees;
Eric Sunshineb979d952015-07-06 13:30:55 -0400254
Eric Sunshine03f24652020-06-19 19:35:44 -0400255 worktrees = get_worktrees();
Eric Sunshined179af62020-06-10 02:30:48 -0400256 check_candidate_path(path, opts->force, worktrees, "add");
257 free_worktrees(worktrees);
258 worktrees = NULL;
Eric Sunshineb979d952015-07-06 13:30:55 -0400259
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400260 /* is 'refname' a branch or commit? */
Nguyễn Thái Ngọc Duy0ebf4a22016-02-15 20:35:32 +0700261 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
Eric Sunshineade546b2017-12-07 16:20:17 -0500262 ref_exists(symref.buf)) {
263 is_branch = 1;
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400264 if (!opts->force)
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700265 die_if_checked_out(symref.buf, 0);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400266 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500267 commit = lookup_commit_reference_by_name(refname);
268 if (!commit)
269 die(_("invalid reference: %s"), refname);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400270
Eric Sunshinef5682b22015-07-06 13:30:57 -0400271 name = worktree_basename(path, &len);
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700272 strbuf_add(&sb, name, path + len - name);
273 sanitize_refname_component(sb.buf, &sb_name);
274 if (!sb_name.len)
275 BUG("How come '%s' becomes empty after sanitization?", sb.buf);
276 strbuf_reset(&sb);
277 name = sb_name.buf;
278 git_path_buf(&sb_repo, "worktrees/%s", name);
Eric Sunshineb979d952015-07-06 13:30:55 -0400279 len = sb_repo.len;
280 if (safe_create_leading_directories_const(sb_repo.buf))
281 die_errno(_("could not create leading directories of '%s'"),
282 sb_repo.buf);
Michal Suchanek7af01f22019-02-20 17:16:48 +0100283
284 while (mkdir(sb_repo.buf, 0777)) {
Eric Sunshineb979d952015-07-06 13:30:55 -0400285 counter++;
Michal Suchanek7af01f22019-02-20 17:16:48 +0100286 if ((errno != EEXIST) || !counter /* overflow */)
287 die_errno(_("could not create directory of '%s'"),
288 sb_repo.buf);
Eric Sunshineb979d952015-07-06 13:30:55 -0400289 strbuf_setlen(&sb_repo, len);
290 strbuf_addf(&sb_repo, "%d", counter);
291 }
292 name = strrchr(sb_repo.buf, '/') + 1;
293
294 junk_pid = getpid();
295 atexit(remove_junk);
296 sigchain_push_common(remove_junk_on_signal);
297
Eric Sunshineb979d952015-07-06 13:30:55 -0400298 junk_git_dir = xstrdup(sb_repo.buf);
299 is_junk = 1;
300
301 /*
302 * lock the incomplete repo so prune won't delete it, unlock
303 * after the preparation is over.
304 */
305 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
Stephen Manz0db49612021-07-15 02:32:30 +0000306 if (opts->keep_locked)
307 write_file(sb.buf, "%s", opts->keep_locked);
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700308 else
Stephen Manz0db49612021-07-15 02:32:30 +0000309 write_file(sb.buf, _("initializing"));
Eric Sunshineb979d952015-07-06 13:30:55 -0400310
311 strbuf_addf(&sb_git, "%s/.git", path);
312 if (safe_create_leading_directories_const(sb_git.buf))
313 die_errno(_("could not create leading directories of '%s'"),
314 sb_git.buf);
315 junk_work_tree = xstrdup(path);
316
317 strbuf_reset(&sb);
318 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000319 strbuf_realpath(&realpath, sb_git.buf, 1);
320 write_file(sb.buf, "%s", realpath.buf);
321 strbuf_realpath(&realpath, get_git_common_dir(), 1);
Junio C Hamano1f76a102015-08-24 13:20:39 -0700322 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000323 realpath.buf, name);
Eric Sunshineb979d952015-07-06 13:30:55 -0400324 /*
325 * This is to keep resolve_ref() happy. We need a valid HEAD
Eric Sunshineed197a62015-07-17 19:00:15 -0400326 * or is_git_directory() will reject the directory. Any value which
327 * looks like an object ID will do since it will be immediately
328 * replaced by the symbolic-ref or update-ref invocation in the new
329 * worktree.
Eric Sunshineb979d952015-07-06 13:30:55 -0400330 */
Eric Sunshineb979d952015-07-06 13:30:55 -0400331 strbuf_reset(&sb);
332 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
brian m. carlson14228442021-04-26 01:02:56 +0000333 write_file(sb.buf, "%s", oid_to_hex(null_oid()));
Eric Sunshineb979d952015-07-06 13:30:55 -0400334 strbuf_reset(&sb);
335 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
Junio C Hamano1f76a102015-08-24 13:20:39 -0700336 write_file(sb.buf, "../..");
Eric Sunshineb979d952015-07-06 13:30:55 -0400337
Jeff King22f9b7f2020-07-28 16:24:27 -0400338 strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
339 strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
Eric Sunshineb979d952015-07-06 13:30:55 -0400340 cp.git_cmd = 1;
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400341
Eric Sunshineade546b2017-12-07 16:20:17 -0500342 if (!is_branch)
Jeff King22f9b7f2020-07-28 16:24:27 -0400343 strvec_pushl(&cp.args, "update-ref", "HEAD",
Jeff Kingf6d89422020-07-28 16:26:31 -0400344 oid_to_hex(&commit->object.oid), NULL);
Elia Pinto371979c2018-08-15 20:56:30 +0000345 else {
Jeff King22f9b7f2020-07-28 16:24:27 -0400346 strvec_pushl(&cp.args, "symbolic-ref", "HEAD",
Jeff Kingf6d89422020-07-28 16:26:31 -0400347 symref.buf, NULL);
Elia Pinto371979c2018-08-15 20:56:30 +0000348 if (opts->quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400349 strvec_push(&cp.args, "--quiet");
Elia Pinto371979c2018-08-15 20:56:30 +0000350 }
351
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100352 strvec_pushv(&cp.env_array, child_env.v);
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400353 ret = run_command(&cp);
354 if (ret)
355 goto done;
356
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000357 if (opts->checkout) {
Eric Sunshine33c997a2021-11-25 23:52:16 +0100358 struct child_process cp = CHILD_PROCESS_INIT;
359 cp.git_cmd = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -0400360 strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
Elia Pinto371979c2018-08-15 20:56:30 +0000361 if (opts->quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400362 strvec_push(&cp.args, "--quiet");
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100363 strvec_pushv(&cp.env_array, child_env.v);
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000364 ret = run_command(&cp);
365 if (ret)
366 goto done;
Eric Sunshineb979d952015-07-06 13:30:55 -0400367 }
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000368
369 is_junk = 0;
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000370 FREE_AND_NULL(junk_work_tree);
371 FREE_AND_NULL(junk_git_dir);
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000372
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400373done:
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700374 if (ret || !opts->keep_locked) {
375 strbuf_reset(&sb);
376 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
377 unlink_or_warn(sb.buf);
378 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500379
380 /*
381 * Hook failure does not warrant worktree deletion, so run hook after
382 * is_junk is cleared, but do return appropriate code when hook fails.
383 */
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500384 if (!ret && opts->checkout) {
385 const char *hook = find_hook("post-checkout");
386 if (hook) {
387 const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL };
Eric Sunshine33c997a2021-11-25 23:52:16 +0100388 struct child_process cp = CHILD_PROCESS_INIT;
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500389 cp.no_stdin = 1;
390 cp.stdout_to_stderr = 1;
391 cp.dir = path;
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100392 strvec_pushv(&cp.env_array, env);
Jeff Hostetler62062862019-02-22 14:25:06 -0800393 cp.trace2_hook_name = "post-checkout";
Jeff King22f9b7f2020-07-28 16:24:27 -0400394 strvec_pushl(&cp.args, absolute_path(hook),
brian m. carlson14228442021-04-26 01:02:56 +0000395 oid_to_hex(null_oid()),
Jeff Kingf6d89422020-07-28 16:26:31 -0400396 oid_to_hex(&commit->object.oid),
397 "1", NULL);
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500398 ret = run_command(&cp);
399 }
400 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500401
Jeff King22f9b7f2020-07-28 16:24:27 -0400402 strvec_clear(&child_env);
Eric Sunshineb979d952015-07-06 13:30:55 -0400403 strbuf_release(&sb);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400404 strbuf_release(&symref);
Eric Sunshineb979d952015-07-06 13:30:55 -0400405 strbuf_release(&sb_repo);
406 strbuf_release(&sb_git);
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700407 strbuf_release(&sb_name);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000408 strbuf_release(&realpath);
Eric Sunshineb979d952015-07-06 13:30:55 -0400409 return ret;
410}
411
Thomas Gummerer2c270022018-04-24 22:56:33 +0100412static void print_preparing_worktree_line(int detach,
413 const char *branch,
414 const char *new_branch,
415 int force_new_branch)
416{
417 if (force_new_branch) {
418 struct commit *commit = lookup_commit_reference_by_name(new_branch);
419 if (!commit)
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500420 fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
Thomas Gummerer2c270022018-04-24 22:56:33 +0100421 else
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500422 fprintf_ln(stderr, _("Preparing worktree (resetting branch '%s'; was at %s)"),
Thomas Gummerer2c270022018-04-24 22:56:33 +0100423 new_branch,
Junio C Hamano10174da2018-05-23 14:38:18 +0900424 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer2c270022018-04-24 22:56:33 +0100425 } else if (new_branch) {
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500426 fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
Thomas Gummerer2c270022018-04-24 22:56:33 +0100427 } else {
428 struct strbuf s = STRBUF_INIT;
429 if (!detach && !strbuf_check_branch_ref(&s, branch) &&
430 ref_exists(s.buf))
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500431 fprintf_ln(stderr, _("Preparing worktree (checking out '%s')"),
Thomas Gummerer2c270022018-04-24 22:56:33 +0100432 branch);
433 else {
434 struct commit *commit = lookup_commit_reference_by_name(branch);
435 if (!commit)
436 die(_("invalid reference: %s"), branch);
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500437 fprintf_ln(stderr, _("Preparing worktree (detached HEAD %s)"),
Junio C Hamano10174da2018-05-23 14:38:18 +0900438 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer2c270022018-04-24 22:56:33 +0100439 }
440 strbuf_release(&s);
441 }
442}
443
Thomas Gummerer6427f872018-04-24 22:56:34 +0100444static const char *dwim_branch(const char *path, const char **new_branch)
445{
446 int n;
Andrzej Huntaa1b6392021-03-14 18:47:37 +0000447 int branch_exists;
Thomas Gummerer6427f872018-04-24 22:56:34 +0100448 const char *s = worktree_basename(path, &n);
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100449 const char *branchname = xstrndup(s, n);
450 struct strbuf ref = STRBUF_INIT;
451
452 UNLEAK(branchname);
Andrzej Huntaa1b6392021-03-14 18:47:37 +0000453
454 branch_exists = !strbuf_check_branch_ref(&ref, branchname) &&
455 ref_exists(ref.buf);
456 strbuf_release(&ref);
457 if (branch_exists)
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100458 return branchname;
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100459
460 *new_branch = branchname;
Thomas Gummerer6427f872018-04-24 22:56:34 +0100461 if (guess_remote) {
462 struct object_id oid;
463 const char *remote =
Ævar Arnfjörð Bjarmason3c87aa92018-06-05 14:40:46 +0000464 unique_tracking_name(*new_branch, &oid, NULL);
Thomas Gummerer6427f872018-04-24 22:56:34 +0100465 return remote;
466 }
467 return NULL;
468}
469
Eric Sunshinefc563612015-07-06 13:30:50 -0400470static int add(int ac, const char **av, const char *prefix)
471{
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400472 struct add_opts opts;
473 const char *new_branch_force = NULL;
Jeff Kinge4da43b2017-03-20 21:28:49 -0400474 char *path;
475 const char *branch;
Thomas Gummererd861d342018-04-24 22:56:32 +0100476 const char *new_branch = NULL;
Thomas Gummerere284e892017-11-26 19:43:53 +0000477 const char *opt_track = NULL;
Stephen Manz0db49612021-07-15 02:32:30 +0000478 const char *lock_reason = NULL;
479 int keep_locked = 0;
Eric Sunshinefc563612015-07-06 13:30:50 -0400480 struct option options[] = {
Nguyễn Thái Ngọc Duy12247812018-02-09 18:01:42 +0700481 OPT__FORCE(&opts.force,
482 N_("checkout <branch> even if already checked out in other worktree"),
Nguyễn Thái Ngọc Duyfc3d4e02018-02-09 18:02:20 +0700483 PARSE_OPT_NOCOMPLETE),
Thomas Gummererd861d342018-04-24 22:56:32 +0100484 OPT_STRING('b', NULL, &new_branch, N_("branch"),
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400485 N_("create a new branch")),
486 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
487 N_("create or reset a branch")),
Eric Sunshinec670aa42020-09-06 20:02:21 -0400488 OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")),
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000489 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
Stephen Manz0db49612021-07-15 02:32:30 +0000490 OPT_BOOL(0, "lock", &keep_locked, N_("keep the new working tree locked")),
491 OPT_STRING(0, "reason", &lock_reason, N_("string"),
492 N_("reason for locking")),
Elia Pinto371979c2018-08-15 20:56:30 +0000493 OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
Thomas Gummerere284e892017-11-26 19:43:53 +0000494 OPT_PASSTHRU(0, "track", &opt_track, NULL,
495 N_("set up tracking mode (see git-branch(1))"),
496 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
Thomas Gummerer71d66822017-11-29 20:04:50 +0000497 OPT_BOOL(0, "guess-remote", &guess_remote,
498 N_("try to match the new branch name with a remote-tracking branch")),
Eric Sunshinefc563612015-07-06 13:30:50 -0400499 OPT_END()
500 };
501
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400502 memset(&opts, 0, sizeof(opts));
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000503 opts.checkout = 1;
Eric Sunshinefc563612015-07-06 13:30:50 -0400504 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
Thomas Gummererd861d342018-04-24 22:56:32 +0100505 if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
Eric Sunshineab0b2c52015-07-17 19:00:08 -0400506 die(_("-b, -B, and --detach are mutually exclusive"));
Stephen Manz0db49612021-07-15 02:32:30 +0000507 if (lock_reason && !keep_locked)
508 die(_("--reason requires --lock"));
509 if (lock_reason)
510 opts.keep_locked = lock_reason;
511 else if (keep_locked)
512 opts.keep_locked = _("added with --lock");
513
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400514 if (ac < 1 || ac > 2)
515 usage_with_options(worktree_usage, options);
Eric Sunshinefc563612015-07-06 13:30:50 -0400516
Jeff King116fb642017-03-20 21:22:28 -0400517 path = prefix_filename(prefix, av[0]);
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400518 branch = ac < 2 ? "HEAD" : av[1];
Eric Sunshinefc563612015-07-06 13:30:50 -0400519
Jordan DE GEA1a450e22016-05-27 15:17:08 +0200520 if (!strcmp(branch, "-"))
521 branch = "@{-1}";
522
Thomas Gummererd861d342018-04-24 22:56:32 +0100523 if (new_branch_force) {
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700524 struct strbuf symref = STRBUF_INIT;
525
Thomas Gummererd861d342018-04-24 22:56:32 +0100526 new_branch = new_branch_force;
Eric Sunshineeef005d2015-07-17 19:00:06 -0400527
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700528 if (!opts.force &&
Thomas Gummererd861d342018-04-24 22:56:32 +0100529 !strbuf_check_branch_ref(&symref, new_branch) &&
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700530 ref_exists(symref.buf))
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700531 die_if_checked_out(symref.buf, 0);
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700532 strbuf_release(&symref);
533 }
534
Thomas Gummererd861d342018-04-24 22:56:32 +0100535 if (ac < 2 && !new_branch && !opts.detach) {
Thomas Gummerer6427f872018-04-24 22:56:34 +0100536 const char *s = dwim_branch(path, &new_branch);
537 if (s)
538 branch = s;
Eric Sunshine1eb07d82015-07-06 13:30:59 -0400539 }
540
Thomas Gummererd861d342018-04-24 22:56:32 +0100541 if (ac == 2 && !new_branch && !opts.detach) {
Thomas Gummerer4e853332017-11-26 19:43:54 +0000542 struct object_id oid;
543 struct commit *commit;
544 const char *remote;
545
546 commit = lookup_commit_reference_by_name(branch);
547 if (!commit) {
Ævar Arnfjörð Bjarmason3c87aa92018-06-05 14:40:46 +0000548 remote = unique_tracking_name(branch, &oid, NULL);
Thomas Gummerer4e853332017-11-26 19:43:54 +0000549 if (remote) {
Thomas Gummererd861d342018-04-24 22:56:32 +0100550 new_branch = branch;
Thomas Gummerer4e853332017-11-26 19:43:54 +0000551 branch = remote;
552 }
553 }
554 }
Elia Pinto371979c2018-08-15 20:56:30 +0000555 if (!opts.quiet)
556 print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
Thomas Gummerer2c270022018-04-24 22:56:33 +0100557
Thomas Gummererd861d342018-04-24 22:56:32 +0100558 if (new_branch) {
René Scharfe542aa252016-08-05 22:38:44 +0200559 struct child_process cp = CHILD_PROCESS_INIT;
Eric Sunshinec2842432015-07-17 19:00:10 -0400560 cp.git_cmd = 1;
Jeff King22f9b7f2020-07-28 16:24:27 -0400561 strvec_push(&cp.args, "branch");
Thomas Gummererd861d342018-04-24 22:56:32 +0100562 if (new_branch_force)
Jeff King22f9b7f2020-07-28 16:24:27 -0400563 strvec_push(&cp.args, "--force");
Elia Pinto371979c2018-08-15 20:56:30 +0000564 if (opts.quiet)
Jeff King22f9b7f2020-07-28 16:24:27 -0400565 strvec_push(&cp.args, "--quiet");
566 strvec_push(&cp.args, new_branch);
567 strvec_push(&cp.args, branch);
Thomas Gummerere284e892017-11-26 19:43:53 +0000568 if (opt_track)
Jeff King22f9b7f2020-07-28 16:24:27 -0400569 strvec_push(&cp.args, opt_track);
Eric Sunshinec2842432015-07-17 19:00:10 -0400570 if (run_command(&cp))
571 return -1;
Thomas Gummererd861d342018-04-24 22:56:32 +0100572 branch = new_branch;
Thomas Gummerere284e892017-11-26 19:43:53 +0000573 } else if (opt_track) {
574 die(_("--[no-]track can only be used if a new branch is created"));
Eric Sunshinec2842432015-07-17 19:00:10 -0400575 }
Eric Sunshinefc563612015-07-06 13:30:50 -0400576
Jeff King0e5bba52017-09-08 02:38:41 -0400577 UNLEAK(path);
578 UNLEAK(opts);
Eric Sunshine80a05482015-07-17 19:00:12 -0400579 return add_worktree(path, branch, &opts);
Eric Sunshinefc563612015-07-06 13:30:50 -0400580}
581
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400582static void show_worktree_porcelain(struct worktree *wt)
583{
Rafael Silva862c7232021-01-27 09:03:08 +0100584 const char *reason;
585
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400586 printf("worktree %s\n", wt->path);
587 if (wt->is_bare)
588 printf("bare\n");
589 else {
brian m. carlson0f051542017-10-15 22:07:08 +0000590 printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400591 if (wt->is_detached)
592 printf("detached\n");
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700593 else if (wt->head_ref)
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400594 printf("branch %s\n", wt->head_ref);
595 }
Rafael Silva862c7232021-01-27 09:03:08 +0100596
597 reason = worktree_lock_reason(wt);
598 if (reason && *reason) {
599 struct strbuf sb = STRBUF_INIT;
600 quote_c_style(reason, &sb, NULL, 0);
601 printf("locked %s\n", sb.buf);
602 strbuf_release(&sb);
603 } else if (reason)
604 printf("locked\n");
605
Rafael Silva9b19a582021-01-27 09:03:09 +0100606 reason = worktree_prune_reason(wt, expire);
607 if (reason)
608 printf("prunable %s\n", reason);
609
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400610 printf("\n");
611}
612
613static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
614{
615 struct strbuf sb = STRBUF_INIT;
616 int cur_path_len = strlen(wt->path);
617 int path_adj = cur_path_len - utf8_strwidth(wt->path);
Rafael Silva076b4442021-01-27 09:03:10 +0100618 const char *reason;
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400619
620 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
621 if (wt->is_bare)
622 strbuf_addstr(&sb, "(bare)");
623 else {
624 strbuf_addf(&sb, "%-*s ", abbrev_len,
brian m. carlsonaab95832018-03-12 02:27:30 +0000625 find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV));
Nguyễn Thái Ngọc Duy96f09e22016-11-28 16:36:53 +0700626 if (wt->is_detached)
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400627 strbuf_addstr(&sb, "(detached HEAD)");
Johannes Schindelin2e11f582017-05-04 15:59:13 +0200628 else if (wt->head_ref) {
629 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
630 strbuf_addf(&sb, "[%s]", ref);
631 free(ref);
632 } else
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700633 strbuf_addstr(&sb, "(error)");
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400634 }
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400635
Rafael Silva076b4442021-01-27 09:03:10 +0100636 reason = worktree_lock_reason(wt);
637 if (verbose && reason && *reason)
638 strbuf_addf(&sb, "\n\tlocked: %s", reason);
639 else if (reason)
Rafael Silvac57b3362020-10-11 10:11:52 +0000640 strbuf_addstr(&sb, " locked");
641
Rafael Silva076b4442021-01-27 09:03:10 +0100642 reason = worktree_prune_reason(wt, expire);
643 if (verbose && reason)
644 strbuf_addf(&sb, "\n\tprunable: %s", reason);
645 else if (reason)
Rafael Silva9b19a582021-01-27 09:03:09 +0100646 strbuf_addstr(&sb, " prunable");
647
Rafael Silvac57b3362020-10-11 10:11:52 +0000648 printf("%s\n", sb.buf);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400649 strbuf_release(&sb);
650}
651
652static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
653{
654 int i;
655
656 for (i = 0; wt[i]; i++) {
657 int sha1_len;
658 int path_len = strlen(wt[i]->path);
659
660 if (path_len > *maxlen)
661 *maxlen = path_len;
brian m. carlsonaab95832018-03-12 02:27:30 +0000662 sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev));
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400663 if (sha1_len > *abbrev)
664 *abbrev = sha1_len;
665 }
666}
667
Eric Sunshined9c54c22020-06-19 19:35:43 -0400668static int pathcmp(const void *a_, const void *b_)
669{
670 const struct worktree *const *a = a_;
671 const struct worktree *const *b = b_;
672 return fspathcmp((*a)->path, (*b)->path);
673}
674
675static void pathsort(struct worktree **wt)
676{
677 int n = 0;
678 struct worktree **p = wt;
679
680 while (*p++)
681 n++;
682 QSORT(wt, n, pathcmp);
683}
684
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400685static int list(int ac, const char **av, const char *prefix)
686{
687 int porcelain = 0;
688
689 struct option options[] = {
690 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
Rafael Silva076b4442021-01-27 09:03:10 +0100691 OPT__VERBOSE(&verbose, N_("show extended annotations and reasons, if available")),
Rafael Silva9b19a582021-01-27 09:03:09 +0100692 OPT_EXPIRY_DATE(0, "expire", &expire,
693 N_("add 'prunable' annotation to worktrees older than <time>")),
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400694 OPT_END()
695 };
696
Rafael Silva9b19a582021-01-27 09:03:09 +0100697 expire = TIME_MAX;
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400698 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
699 if (ac)
700 usage_with_options(worktree_usage, options);
Rafael Silva076b4442021-01-27 09:03:10 +0100701 else if (verbose && porcelain)
Jean-Noël Avila43ea6352022-01-05 20:02:14 +0000702 die(_("options '%s' and '%s' cannot be used together"), "--verbose", "--porcelain");
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400703 else {
Eric Sunshine03f24652020-06-19 19:35:44 -0400704 struct worktree **worktrees = get_worktrees();
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400705 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
706
Eric Sunshined9c54c22020-06-19 19:35:43 -0400707 /* sort worktrees by path but keep main worktree at top */
708 pathsort(worktrees + 1);
709
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400710 if (!porcelain)
711 measure_widths(worktrees, &abbrev, &path_maxlen);
712
713 for (i = 0; worktrees[i]; i++) {
714 if (porcelain)
715 show_worktree_porcelain(worktrees[i]);
716 else
717 show_worktree(worktrees[i], path_maxlen, abbrev);
718 }
719 free_worktrees(worktrees);
720 }
721 return 0;
722}
723
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700724static int lock_worktree(int ac, const char **av, const char *prefix)
725{
726 const char *reason = "", *old_reason;
727 struct option options[] = {
728 OPT_STRING(0, "reason", &reason, N_("string"),
729 N_("reason for locking")),
730 OPT_END()
731 };
732 struct worktree **worktrees, *wt;
733
734 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
735 if (ac != 1)
736 usage_with_options(worktree_usage, options);
737
Eric Sunshine03f24652020-06-19 19:35:44 -0400738 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700739 wt = find_worktree(worktrees, prefix, av[0]);
740 if (!wt)
741 die(_("'%s' is not a working tree"), av[0]);
742 if (is_main_worktree(wt))
743 die(_("The main working tree cannot be locked or unlocked"));
744
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700745 old_reason = worktree_lock_reason(wt);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700746 if (old_reason) {
747 if (*old_reason)
748 die(_("'%s' is already locked, reason: %s"),
749 av[0], old_reason);
750 die(_("'%s' is already locked"), av[0]);
751 }
752
753 write_file(git_common_path("worktrees/%s/locked", wt->id),
754 "%s", reason);
755 free_worktrees(worktrees);
756 return 0;
757}
758
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700759static int unlock_worktree(int ac, const char **av, const char *prefix)
760{
761 struct option options[] = {
762 OPT_END()
763 };
764 struct worktree **worktrees, *wt;
765 int ret;
766
767 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
768 if (ac != 1)
769 usage_with_options(worktree_usage, options);
770
Eric Sunshine03f24652020-06-19 19:35:44 -0400771 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700772 wt = find_worktree(worktrees, prefix, av[0]);
773 if (!wt)
774 die(_("'%s' is not a working tree"), av[0]);
775 if (is_main_worktree(wt))
776 die(_("The main working tree cannot be locked or unlocked"));
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700777 if (!worktree_lock_reason(wt))
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700778 die(_("'%s' is not locked"), av[0]);
779 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
780 free_worktrees(worktrees);
781 return ret;
782}
783
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700784static void validate_no_submodules(const struct worktree *wt)
785{
786 struct index_state istate = { NULL };
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +0700787 struct strbuf path = STRBUF_INIT;
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700788 int i, found_submodules = 0;
789
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +0700790 if (is_directory(worktree_git_path(wt, "modules"))) {
791 /*
792 * There could be false positives, e.g. the "modules"
793 * directory exists but is empty. But it's a rare case and
794 * this simpler check is probably good enough for now.
795 */
796 found_submodules = 1;
797 } else if (read_index_from(&istate, worktree_git_path(wt, "index"),
798 get_worktree_git_dir(wt)) > 0) {
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700799 for (i = 0; i < istate.cache_nr; i++) {
800 struct cache_entry *ce = istate.cache[i];
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +0700801 int err;
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700802
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +0700803 if (!S_ISGITLINK(ce->ce_mode))
804 continue;
805
806 strbuf_reset(&path);
807 strbuf_addf(&path, "%s/%s", wt->path, ce->name);
808 if (!is_submodule_populated_gently(path.buf, &err))
809 continue;
810
811 found_submodules = 1;
812 break;
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700813 }
814 }
815 discard_index(&istate);
Nguyễn Thái Ngọc Duy00a6d4d2019-01-05 12:08:40 +0700816 strbuf_release(&path);
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700817
818 if (found_submodules)
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700819 die(_("working trees containing submodules cannot be moved or removed"));
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700820}
821
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700822static int move_worktree(int ac, const char **av, const char *prefix)
823{
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400824 int force = 0;
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700825 struct option options[] = {
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400826 OPT__FORCE(&force,
827 N_("force move even if worktree is dirty or locked"),
828 PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700829 OPT_END()
830 };
831 struct worktree **worktrees, *wt;
832 struct strbuf dst = STRBUF_INIT;
833 struct strbuf errmsg = STRBUF_INIT;
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400834 const char *reason = NULL;
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700835 char *path;
836
837 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
838 if (ac != 2)
839 usage_with_options(worktree_usage, options);
840
841 path = prefix_filename(prefix, av[1]);
842 strbuf_addstr(&dst, path);
843 free(path);
844
Eric Sunshine03f24652020-06-19 19:35:44 -0400845 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700846 wt = find_worktree(worktrees, prefix, av[0]);
847 if (!wt)
848 die(_("'%s' is not a working tree"), av[0]);
849 if (is_main_worktree(wt))
850 die(_("'%s' is a main working tree"), av[0]);
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700851 if (is_directory(dst.buf)) {
852 const char *sep = find_last_dir_sep(wt->path);
853
854 if (!sep)
855 die(_("could not figure out destination name from '%s'"),
856 wt->path);
857 strbuf_trim_trailing_dir_sep(&dst);
858 strbuf_addstr(&dst, sep);
859 }
Eric Sunshine810382e2020-06-10 02:30:49 -0400860 check_candidate_path(dst.buf, force, worktrees, "move");
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700861
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700862 validate_no_submodules(wt);
863
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400864 if (force < 2)
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700865 reason = worktree_lock_reason(wt);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700866 if (reason) {
867 if (*reason)
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400868 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 +0700869 reason);
Eric Sunshine68a6b3a2018-08-28 17:20:24 -0400870 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 +0700871 }
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700872 if (validate_worktree(wt, &errmsg, 0))
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700873 die(_("validation failed, cannot move working tree: %s"),
874 errmsg.buf);
875 strbuf_release(&errmsg);
876
877 if (rename(wt->path, dst.buf) == -1)
878 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
879
880 update_worktree_location(wt, dst.buf);
881
882 strbuf_release(&dst);
883 free_worktrees(worktrees);
884 return 0;
885}
886
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700887/*
888 * Note, "git status --porcelain" is used to determine if it's safe to
889 * delete a whole worktree. "git status" does not ignore user
890 * configuration, so if a normal "git status" shows "clean" for the
891 * user, then it's ok to remove it.
892 *
893 * This assumption may be a bad one. We may want to ignore
894 * (potentially bad) user settings and only delete a worktree when
895 * it's absolutely safe to do so from _our_ point of view because we
896 * know better.
897 */
898static void check_clean_worktree(struct worktree *wt,
899 const char *original_path)
900{
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700901 struct child_process cp;
902 char buf[1];
903 int ret;
904
905 /*
906 * Until we sort this out, all submodules are "dirty" and
907 * will abort this function.
908 */
909 validate_no_submodules(wt);
910
Jeff King27ed6cc2020-08-27 01:25:04 -0400911 child_process_init(&cp);
Junio C Hamano1aadb472020-09-09 13:53:07 -0700912 strvec_pushf(&cp.env_array, "%s=%s/.git",
Jeff Kingf6d89422020-07-28 16:26:31 -0400913 GIT_DIR_ENVIRONMENT, wt->path);
Junio C Hamano1aadb472020-09-09 13:53:07 -0700914 strvec_pushf(&cp.env_array, "%s=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400915 GIT_WORK_TREE_ENVIRONMENT, wt->path);
Jeff King22f9b7f2020-07-28 16:24:27 -0400916 strvec_pushl(&cp.args, "status",
Jeff Kingf6d89422020-07-28 16:26:31 -0400917 "--porcelain", "--ignore-submodules=none",
918 NULL);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700919 cp.git_cmd = 1;
920 cp.dir = wt->path;
921 cp.out = -1;
922 ret = start_command(&cp);
923 if (ret)
924 die_errno(_("failed to run 'git status' on '%s'"),
925 original_path);
926 ret = xread(cp.out, buf, sizeof(buf));
927 if (ret)
SZEDER Gábor507e5472019-08-13 20:02:44 +0200928 die(_("'%s' contains modified or untracked files, use --force to delete it"),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700929 original_path);
930 close(cp.out);
931 ret = finish_command(&cp);
932 if (ret)
933 die_errno(_("failed to run 'git status' on '%s', code %d"),
934 original_path, ret);
935}
936
937static int delete_git_work_tree(struct worktree *wt)
938{
939 struct strbuf sb = STRBUF_INIT;
940 int ret = 0;
941
942 strbuf_addstr(&sb, wt->path);
943 if (remove_dir_recursively(&sb, 0)) {
944 error_errno(_("failed to delete '%s'"), sb.buf);
945 ret = -1;
946 }
947 strbuf_release(&sb);
948 return ret;
949}
950
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700951static int remove_worktree(int ac, const char **av, const char *prefix)
952{
953 int force = 0;
954 struct option options[] = {
Stefan Bellerd228eea2018-04-17 11:19:39 -0700955 OPT__FORCE(&force,
Eric Sunshinef4143102018-08-28 17:20:25 -0400956 N_("force removal even if worktree is dirty or locked"),
Stefan Bellerd228eea2018-04-17 11:19:39 -0700957 PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700958 OPT_END()
959 };
960 struct worktree **worktrees, *wt;
961 struct strbuf errmsg = STRBUF_INIT;
Eric Sunshinef4143102018-08-28 17:20:25 -0400962 const char *reason = NULL;
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700963 int ret = 0;
964
965 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
966 if (ac != 1)
967 usage_with_options(worktree_usage, options);
968
Eric Sunshine03f24652020-06-19 19:35:44 -0400969 worktrees = get_worktrees();
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700970 wt = find_worktree(worktrees, prefix, av[0]);
971 if (!wt)
972 die(_("'%s' is not a working tree"), av[0]);
973 if (is_main_worktree(wt))
974 die(_("'%s' is a main working tree"), av[0]);
Eric Sunshinef4143102018-08-28 17:20:25 -0400975 if (force < 2)
Nickolai Belakovskid236f122018-10-29 23:24:09 -0700976 reason = worktree_lock_reason(wt);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700977 if (reason) {
978 if (*reason)
Eric Sunshinef4143102018-08-28 17:20:25 -0400979 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 +0700980 reason);
Eric Sunshinef4143102018-08-28 17:20:25 -0400981 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 +0700982 }
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700983 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700984 die(_("validation failed, cannot remove working tree: %s"),
985 errmsg.buf);
986 strbuf_release(&errmsg);
987
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700988 if (file_exists(wt->path)) {
989 if (!force)
990 check_clean_worktree(wt, av[0]);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700991
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700992 ret |= delete_git_work_tree(wt);
993 }
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700994 /*
995 * continue on even if ret is non-zero, there's no going back
996 * from here.
997 */
Eric Sunshine602aaed2018-08-28 17:20:20 -0400998 ret |= delete_git_dir(wt->id);
Eric Sunshine3a540432018-08-28 17:20:26 -0400999 delete_worktrees_dir_if_empty();
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001000
1001 free_worktrees(worktrees);
1002 return ret;
1003}
1004
Eric Sunshinebdd1f3e2020-08-31 02:57:57 -04001005static void report_repair(int iserr, const char *path, const char *msg, void *cb_data)
1006{
1007 if (!iserr) {
Eric Sunshineda8fb6b2021-12-02 22:44:19 -05001008 fprintf_ln(stderr, _("repair: %s: %s"), msg, path);
Eric Sunshinebdd1f3e2020-08-31 02:57:57 -04001009 } else {
1010 int *exit_status = (int *)cb_data;
1011 fprintf_ln(stderr, _("error: %s: %s"), msg, path);
1012 *exit_status = 1;
1013 }
1014}
1015
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001016static int repair(int ac, const char **av, const char *prefix)
1017{
Eric Sunshineb214ab52020-08-31 02:57:58 -04001018 const char **p;
1019 const char *self[] = { ".", NULL };
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001020 struct option options[] = {
1021 OPT_END()
1022 };
1023 int rc = 0;
1024
1025 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
Eric Sunshineb214ab52020-08-31 02:57:58 -04001026 p = ac > 0 ? av : self;
1027 for (; *p; p++)
1028 repair_worktree_at_path(*p, report_repair, &rc);
Eric Sunshinecf76bae2020-12-21 03:16:01 -05001029 repair_worktrees(report_repair, &rc);
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001030 return rc;
1031}
1032
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001033int cmd_worktree(int ac, const char **av, const char *prefix)
1034{
1035 struct option options[] = {
1036 OPT_END()
1037 };
1038
Thomas Gummerere92445a2017-11-29 20:04:51 +00001039 git_config(git_worktree_config, NULL);
Junio C Hamanod49028e2016-09-26 23:49:39 -07001040
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001041 if (ac < 2)
1042 usage_with_options(worktree_usage, options);
Nguyễn Thái Ngọc Duy0409e0b2016-05-22 16:33:56 +07001043 if (!prefix)
1044 prefix = "";
Eric Sunshinefc563612015-07-06 13:30:50 -04001045 if (!strcmp(av[1], "add"))
1046 return add(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001047 if (!strcmp(av[1], "prune"))
1048 return prune(ac - 1, av + 1, prefix);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -04001049 if (!strcmp(av[1], "list"))
1050 return list(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +07001051 if (!strcmp(av[1], "lock"))
1052 return lock_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +07001053 if (!strcmp(av[1], "unlock"))
1054 return unlock_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +07001055 if (!strcmp(av[1], "move"))
1056 return move_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +07001057 if (!strcmp(av[1], "remove"))
1058 return remove_worktree(ac - 1, av + 1, prefix);
Eric Sunshinee8e1ff22020-08-27 04:21:25 -04001059 if (!strcmp(av[1], "repair"))
1060 return repair(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +07001061 usage_with_options(worktree_usage, options);
1062}