blob: 5c7d2bb1807f942139b3ec41b426320e4b0cfc2a [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"
Eric Sunshinefc563612015-07-06 13:30:50 -04007#include "argv-array.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"
Eric Sunshineb979d952015-07-06 13:30:55 -040011#include "sigchain.h"
Junio C Hamano799767c2015-07-13 14:02:18 -070012#include "refs.h"
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040013#include "utf8.h"
14#include "worktree.h"
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070015
16static const char * const worktree_usage[] = {
Junio C Hamanob780e442018-01-17 10:32:32 -080017 N_("git worktree add [<options>] <path> [<commit-ish>]"),
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040018 N_("git worktree list [<options>]"),
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +070019 N_("git worktree lock [<options>] <path>"),
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +070020 N_("git worktree move <worktree> <new-path>"),
Nguyễn Thái Ngọc Duy7b722d92016-05-22 16:33:53 +070021 N_("git worktree prune [<options>]"),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +070022 N_("git worktree remove [<options>] <worktree>"),
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +070023 N_("git worktree unlock <path>"),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070024 NULL
25};
26
Eric Sunshine5dd6e232015-07-17 19:00:07 -040027struct add_opts {
28 int force;
29 int detach;
Ray Zhangef2a0ac2016-03-29 10:11:01 +000030 int checkout;
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +070031 int keep_locked;
Eric Sunshine5dd6e232015-07-17 19:00:07 -040032};
33
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070034static int show_only;
35static int verbose;
Thomas Gummerere92445a2017-11-29 20:04:51 +000036static int guess_remote;
Johannes Schindelindddbad72017-04-26 21:29:31 +020037static timestamp_t expire;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070038
Thomas Gummerere92445a2017-11-29 20:04:51 +000039static int git_worktree_config(const char *var, const char *value, void *cb)
40{
41 if (!strcmp(var, "worktree.guessremote")) {
42 guess_remote = git_config_bool(var, value);
43 return 0;
44 }
45
46 return git_default_config(var, value, cb);
47}
48
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070049static int prune_worktree(const char *id, struct strbuf *reason)
50{
51 struct stat st;
52 char *path;
Jeff King228740b2017-09-27 02:02:21 -040053 int fd;
54 size_t len;
Jeff King8a1a8d22017-09-27 02:02:27 -040055 ssize_t read_result;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070056
57 if (!is_directory(git_path("worktrees/%s", id))) {
58 strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
59 return 1;
60 }
61 if (file_exists(git_path("worktrees/%s/locked", id)))
62 return 0;
63 if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
64 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
65 return 1;
66 }
67 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
68 if (fd < 0) {
69 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
70 id, strerror(errno));
71 return 1;
72 }
Jeff King228740b2017-09-27 02:02:21 -040073 len = xsize_t(st.st_size);
Jeff King3733e692016-02-22 17:44:28 -050074 path = xmallocz(len);
Jeff King8a1a8d22017-09-27 02:02:27 -040075
76 read_result = read_in_full(fd, path, len);
77 if (read_result < 0) {
78 strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
79 id, strerror(errno));
80 close(fd);
81 free(path);
82 return 1;
83 }
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070084 close(fd);
Jeff King8a1a8d22017-09-27 02:02:27 -040085
86 if (read_result != len) {
87 strbuf_addf(reason,
88 _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
89 id, (uintmax_t)len, (uintmax_t)read_result);
90 free(path);
91 return 1;
92 }
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +070093 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
94 len--;
95 if (!len) {
96 strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
97 free(path);
98 return 1;
99 }
100 path[len] = '\0';
101 if (!file_exists(path)) {
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700102 free(path);
Nguyễn Thái Ngọc Duy327864a2018-03-15 17:44:12 +0100103 if (stat(git_path("worktrees/%s/index", id), &st) ||
104 st.st_mtime <= expire) {
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700105 strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
106 return 1;
107 } else {
108 return 0;
109 }
110 }
111 free(path);
112 return 0;
113}
114
115static void prune_worktrees(void)
116{
117 struct strbuf reason = STRBUF_INIT;
118 struct strbuf path = STRBUF_INIT;
119 DIR *dir = opendir(git_path("worktrees"));
120 struct dirent *d;
121 int ret;
122 if (!dir)
123 return;
124 while ((d = readdir(dir)) != NULL) {
Nguyễn Thái Ngọc Duyafb9e302016-05-22 16:33:54 +0700125 if (is_dot_or_dotdot(d->d_name))
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700126 continue;
127 strbuf_reset(&reason);
128 if (!prune_worktree(d->d_name, &reason))
129 continue;
130 if (show_only || verbose)
131 printf("%s\n", reason.buf);
132 if (show_only)
133 continue;
Jeff King8c2ca3a2017-04-20 17:09:30 -0400134 git_path_buf(&path, "worktrees/%s", d->d_name);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700135 ret = remove_dir_recursively(&path, 0);
136 if (ret < 0 && errno == ENOTDIR)
137 ret = unlink(path.buf);
138 if (ret)
Nguyễn Thái Ngọc Duy8d19e932016-05-08 16:47:34 +0700139 error_errno(_("failed to remove '%s'"), path.buf);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700140 }
141 closedir(dir);
142 if (!show_only)
143 rmdir(git_path("worktrees"));
144 strbuf_release(&reason);
145 strbuf_release(&path);
146}
147
148static int prune(int ac, const char **av, const char *prefix)
149{
150 struct option options[] = {
151 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
Patrick Steinhardt2488dca2017-02-06 14:13:59 +0100152 OPT__VERBOSE(&verbose, N_("report pruned working trees")),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700153 OPT_EXPIRY_DATE(0, "expire", &expire,
Patrick Steinhardt2488dca2017-02-06 14:13:59 +0100154 N_("expire working trees older than <time>")),
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700155 OPT_END()
156 };
157
Johannes Schindelindddbad72017-04-26 21:29:31 +0200158 expire = TIME_MAX;
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700159 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
160 if (ac)
161 usage_with_options(worktree_usage, options);
162 prune_worktrees();
163 return 0;
164}
165
Eric Sunshineb979d952015-07-06 13:30:55 -0400166static char *junk_work_tree;
167static char *junk_git_dir;
168static int is_junk;
169static pid_t junk_pid;
170
171static void remove_junk(void)
172{
173 struct strbuf sb = STRBUF_INIT;
174 if (!is_junk || getpid() != junk_pid)
175 return;
176 if (junk_git_dir) {
177 strbuf_addstr(&sb, junk_git_dir);
178 remove_dir_recursively(&sb, 0);
179 strbuf_reset(&sb);
180 }
181 if (junk_work_tree) {
182 strbuf_addstr(&sb, junk_work_tree);
183 remove_dir_recursively(&sb, 0);
184 }
185 strbuf_release(&sb);
186}
187
188static void remove_junk_on_signal(int signo)
189{
190 remove_junk();
191 sigchain_pop(signo);
192 raise(signo);
193}
194
Eric Sunshinef5682b22015-07-06 13:30:57 -0400195static const char *worktree_basename(const char *path, int *olen)
196{
197 const char *name;
198 int len;
199
200 len = strlen(path);
201 while (len && is_dir_sep(path[len - 1]))
202 len--;
203
204 for (name = path + len - 1; name > path; name--)
205 if (is_dir_sep(*name)) {
206 name++;
207 break;
208 }
209
210 *olen = len;
211 return name;
212}
213
Eric Sunshine80a05482015-07-17 19:00:12 -0400214static int add_worktree(const char *path, const char *refname,
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400215 const struct add_opts *opts)
Eric Sunshineb979d952015-07-06 13:30:55 -0400216{
217 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
218 struct strbuf sb = STRBUF_INIT;
219 const char *name;
220 struct stat st;
René Scharfe542aa252016-08-05 22:38:44 +0200221 struct child_process cp = CHILD_PROCESS_INIT;
Eric Sunshineae2a3822015-07-17 19:00:11 -0400222 struct argv_array child_env = ARGV_ARRAY_INIT;
Eric Sunshineb979d952015-07-06 13:30:55 -0400223 int counter = 0, len, ret;
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400224 struct strbuf symref = STRBUF_INIT;
225 struct commit *commit = NULL;
Eric Sunshineade546b2017-12-07 16:20:17 -0500226 int is_branch = 0;
Eric Sunshineb979d952015-07-06 13:30:55 -0400227
228 if (file_exists(path) && !is_empty_dir(path))
229 die(_("'%s' already exists"), path);
230
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400231 /* is 'refname' a branch or commit? */
Nguyễn Thái Ngọc Duy0ebf4a22016-02-15 20:35:32 +0700232 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
Eric Sunshineade546b2017-12-07 16:20:17 -0500233 ref_exists(symref.buf)) {
234 is_branch = 1;
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400235 if (!opts->force)
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700236 die_if_checked_out(symref.buf, 0);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400237 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500238 commit = lookup_commit_reference_by_name(refname);
239 if (!commit)
240 die(_("invalid reference: %s"), refname);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400241
Eric Sunshinef5682b22015-07-06 13:30:57 -0400242 name = worktree_basename(path, &len);
Jeff King8c2ca3a2017-04-20 17:09:30 -0400243 git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name);
Eric Sunshineb979d952015-07-06 13:30:55 -0400244 len = sb_repo.len;
245 if (safe_create_leading_directories_const(sb_repo.buf))
246 die_errno(_("could not create leading directories of '%s'"),
247 sb_repo.buf);
248 while (!stat(sb_repo.buf, &st)) {
249 counter++;
250 strbuf_setlen(&sb_repo, len);
251 strbuf_addf(&sb_repo, "%d", counter);
252 }
253 name = strrchr(sb_repo.buf, '/') + 1;
254
255 junk_pid = getpid();
256 atexit(remove_junk);
257 sigchain_push_common(remove_junk_on_signal);
258
259 if (mkdir(sb_repo.buf, 0777))
260 die_errno(_("could not create directory of '%s'"), sb_repo.buf);
261 junk_git_dir = xstrdup(sb_repo.buf);
262 is_junk = 1;
263
264 /*
265 * lock the incomplete repo so prune won't delete it, unlock
266 * after the preparation is over.
267 */
268 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700269 if (!opts->keep_locked)
270 write_file(sb.buf, "initializing");
271 else
272 write_file(sb.buf, "added with --lock");
Eric Sunshineb979d952015-07-06 13:30:55 -0400273
274 strbuf_addf(&sb_git, "%s/.git", path);
275 if (safe_create_leading_directories_const(sb_git.buf))
276 die_errno(_("could not create leading directories of '%s'"),
277 sb_git.buf);
278 junk_work_tree = xstrdup(path);
279
280 strbuf_reset(&sb);
281 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
Junio C Hamano1f76a102015-08-24 13:20:39 -0700282 write_file(sb.buf, "%s", real_path(sb_git.buf));
283 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
Eric Sunshineb979d952015-07-06 13:30:55 -0400284 real_path(get_git_common_dir()), name);
285 /*
286 * This is to keep resolve_ref() happy. We need a valid HEAD
Eric Sunshineed197a62015-07-17 19:00:15 -0400287 * or is_git_directory() will reject the directory. Any value which
288 * looks like an object ID will do since it will be immediately
289 * replaced by the symbolic-ref or update-ref invocation in the new
290 * worktree.
Eric Sunshineb979d952015-07-06 13:30:55 -0400291 */
Eric Sunshineb979d952015-07-06 13:30:55 -0400292 strbuf_reset(&sb);
293 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
Jeff Kingdabd35f2016-07-08 06:35:15 -0400294 write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
Eric Sunshineb979d952015-07-06 13:30:55 -0400295 strbuf_reset(&sb);
296 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
Junio C Hamano1f76a102015-08-24 13:20:39 -0700297 write_file(sb.buf, "../..");
Eric Sunshineb979d952015-07-06 13:30:55 -0400298
Eric Sunshineae2a3822015-07-17 19:00:11 -0400299 argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
300 argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
Eric Sunshineb979d952015-07-06 13:30:55 -0400301 cp.git_cmd = 1;
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400302
Eric Sunshineade546b2017-12-07 16:20:17 -0500303 if (!is_branch)
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400304 argv_array_pushl(&cp.args, "update-ref", "HEAD",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000305 oid_to_hex(&commit->object.oid), NULL);
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400306 else
307 argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
308 symref.buf, NULL);
309 cp.env = child_env.argv;
310 ret = run_command(&cp);
311 if (ret)
312 goto done;
313
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000314 if (opts->checkout) {
315 cp.argv = NULL;
316 argv_array_clear(&cp.args);
317 argv_array_pushl(&cp.args, "reset", "--hard", NULL);
318 cp.env = child_env.argv;
319 ret = run_command(&cp);
320 if (ret)
321 goto done;
Eric Sunshineb979d952015-07-06 13:30:55 -0400322 }
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000323
324 is_junk = 0;
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000325 FREE_AND_NULL(junk_work_tree);
326 FREE_AND_NULL(junk_git_dir);
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000327
Eric Sunshine7f44e3d2015-07-17 19:00:14 -0400328done:
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700329 if (ret || !opts->keep_locked) {
330 strbuf_reset(&sb);
331 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
332 unlink_or_warn(sb.buf);
333 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500334
335 /*
336 * Hook failure does not warrant worktree deletion, so run hook after
337 * is_junk is cleared, but do return appropriate code when hook fails.
338 */
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500339 if (!ret && opts->checkout) {
340 const char *hook = find_hook("post-checkout");
341 if (hook) {
342 const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL };
343 cp.git_cmd = 0;
344 cp.no_stdin = 1;
345 cp.stdout_to_stderr = 1;
346 cp.dir = path;
347 cp.env = env;
348 cp.argv = NULL;
349 argv_array_pushl(&cp.args, absolute_path(hook),
350 oid_to_hex(&null_oid),
351 oid_to_hex(&commit->object.oid),
352 "1", NULL);
353 ret = run_command(&cp);
354 }
355 }
Eric Sunshineade546b2017-12-07 16:20:17 -0500356
Eric Sunshineae2a3822015-07-17 19:00:11 -0400357 argv_array_clear(&child_env);
Eric Sunshineb979d952015-07-06 13:30:55 -0400358 strbuf_release(&sb);
Eric Sunshinef7c9dac2015-07-17 19:00:13 -0400359 strbuf_release(&symref);
Eric Sunshineb979d952015-07-06 13:30:55 -0400360 strbuf_release(&sb_repo);
361 strbuf_release(&sb_git);
362 return ret;
363}
364
Thomas Gummerer2c270022018-04-24 22:56:33 +0100365static void print_preparing_worktree_line(int detach,
366 const char *branch,
367 const char *new_branch,
368 int force_new_branch)
369{
370 if (force_new_branch) {
371 struct commit *commit = lookup_commit_reference_by_name(new_branch);
372 if (!commit)
373 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
374 else
375 printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
376 new_branch,
Junio C Hamano10174da2018-05-23 14:38:18 +0900377 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer2c270022018-04-24 22:56:33 +0100378 } else if (new_branch) {
379 printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
380 } else {
381 struct strbuf s = STRBUF_INIT;
382 if (!detach && !strbuf_check_branch_ref(&s, branch) &&
383 ref_exists(s.buf))
384 printf_ln(_("Preparing worktree (checking out '%s')"),
385 branch);
386 else {
387 struct commit *commit = lookup_commit_reference_by_name(branch);
388 if (!commit)
389 die(_("invalid reference: %s"), branch);
390 printf_ln(_("Preparing worktree (detached HEAD %s)"),
Junio C Hamano10174da2018-05-23 14:38:18 +0900391 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
Thomas Gummerer2c270022018-04-24 22:56:33 +0100392 }
393 strbuf_release(&s);
394 }
395}
396
Thomas Gummerer6427f872018-04-24 22:56:34 +0100397static const char *dwim_branch(const char *path, const char **new_branch)
398{
399 int n;
400 const char *s = worktree_basename(path, &n);
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100401 const char *branchname = xstrndup(s, n);
402 struct strbuf ref = STRBUF_INIT;
403
404 UNLEAK(branchname);
405 if (!strbuf_check_branch_ref(&ref, branchname) &&
406 ref_exists(ref.buf)) {
407 strbuf_release(&ref);
408 return branchname;
409 }
410
411 *new_branch = branchname;
Thomas Gummerer6427f872018-04-24 22:56:34 +0100412 if (guess_remote) {
413 struct object_id oid;
414 const char *remote =
415 unique_tracking_name(*new_branch, &oid);
416 return remote;
417 }
418 return NULL;
419}
420
Eric Sunshinefc563612015-07-06 13:30:50 -0400421static int add(int ac, const char **av, const char *prefix)
422{
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400423 struct add_opts opts;
424 const char *new_branch_force = NULL;
Jeff Kinge4da43b2017-03-20 21:28:49 -0400425 char *path;
426 const char *branch;
Thomas Gummererd861d342018-04-24 22:56:32 +0100427 const char *new_branch = NULL;
Thomas Gummerere284e892017-11-26 19:43:53 +0000428 const char *opt_track = NULL;
Eric Sunshinefc563612015-07-06 13:30:50 -0400429 struct option options[] = {
Nguyễn Thái Ngọc Duy12247812018-02-09 18:01:42 +0700430 OPT__FORCE(&opts.force,
431 N_("checkout <branch> even if already checked out in other worktree"),
Nguyễn Thái Ngọc Duyfc3d4e02018-02-09 18:02:20 +0700432 PARSE_OPT_NOCOMPLETE),
Thomas Gummererd861d342018-04-24 22:56:32 +0100433 OPT_STRING('b', NULL, &new_branch, N_("branch"),
Eric Sunshinecbdf60f2015-07-06 13:30:53 -0400434 N_("create a new branch")),
435 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
436 N_("create or reset a branch")),
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400437 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000438 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +0700439 OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
Thomas Gummerere284e892017-11-26 19:43:53 +0000440 OPT_PASSTHRU(0, "track", &opt_track, NULL,
441 N_("set up tracking mode (see git-branch(1))"),
442 PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
Thomas Gummerer71d66822017-11-29 20:04:50 +0000443 OPT_BOOL(0, "guess-remote", &guess_remote,
444 N_("try to match the new branch name with a remote-tracking branch")),
Eric Sunshinefc563612015-07-06 13:30:50 -0400445 OPT_END()
446 };
447
Eric Sunshine5dd6e232015-07-17 19:00:07 -0400448 memset(&opts, 0, sizeof(opts));
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000449 opts.checkout = 1;
Eric Sunshinefc563612015-07-06 13:30:50 -0400450 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
Thomas Gummererd861d342018-04-24 22:56:32 +0100451 if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
Eric Sunshineab0b2c52015-07-17 19:00:08 -0400452 die(_("-b, -B, and --detach are mutually exclusive"));
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400453 if (ac < 1 || ac > 2)
454 usage_with_options(worktree_usage, options);
Eric Sunshinefc563612015-07-06 13:30:50 -0400455
Jeff King116fb642017-03-20 21:22:28 -0400456 path = prefix_filename(prefix, av[0]);
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400457 branch = ac < 2 ? "HEAD" : av[1];
Eric Sunshinefc563612015-07-06 13:30:50 -0400458
Jordan DE GEA1a450e22016-05-27 15:17:08 +0200459 if (!strcmp(branch, "-"))
460 branch = "@{-1}";
461
Thomas Gummererd861d342018-04-24 22:56:32 +0100462 if (new_branch_force) {
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700463 struct strbuf symref = STRBUF_INIT;
464
Thomas Gummererd861d342018-04-24 22:56:32 +0100465 new_branch = new_branch_force;
Eric Sunshineeef005d2015-07-17 19:00:06 -0400466
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700467 if (!opts.force &&
Thomas Gummererd861d342018-04-24 22:56:32 +0100468 !strbuf_check_branch_ref(&symref, new_branch) &&
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700469 ref_exists(symref.buf))
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700470 die_if_checked_out(symref.buf, 0);
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700471 strbuf_release(&symref);
472 }
473
Thomas Gummererd861d342018-04-24 22:56:32 +0100474 if (ac < 2 && !new_branch && !opts.detach) {
Thomas Gummerer6427f872018-04-24 22:56:34 +0100475 const char *s = dwim_branch(path, &new_branch);
476 if (s)
477 branch = s;
Eric Sunshine1eb07d82015-07-06 13:30:59 -0400478 }
479
Thomas Gummererd861d342018-04-24 22:56:32 +0100480 if (ac == 2 && !new_branch && !opts.detach) {
Thomas Gummerer4e853332017-11-26 19:43:54 +0000481 struct object_id oid;
482 struct commit *commit;
483 const char *remote;
484
485 commit = lookup_commit_reference_by_name(branch);
486 if (!commit) {
487 remote = unique_tracking_name(branch, &oid);
488 if (remote) {
Thomas Gummererd861d342018-04-24 22:56:32 +0100489 new_branch = branch;
Thomas Gummerer4e853332017-11-26 19:43:54 +0000490 branch = remote;
491 }
492 }
493 }
494
Thomas Gummerer2c270022018-04-24 22:56:33 +0100495 print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
496
Thomas Gummererd861d342018-04-24 22:56:32 +0100497 if (new_branch) {
René Scharfe542aa252016-08-05 22:38:44 +0200498 struct child_process cp = CHILD_PROCESS_INIT;
Eric Sunshinec2842432015-07-17 19:00:10 -0400499 cp.git_cmd = 1;
500 argv_array_push(&cp.args, "branch");
Thomas Gummererd861d342018-04-24 22:56:32 +0100501 if (new_branch_force)
Eric Sunshinec2842432015-07-17 19:00:10 -0400502 argv_array_push(&cp.args, "--force");
Thomas Gummererd861d342018-04-24 22:56:32 +0100503 argv_array_push(&cp.args, new_branch);
Eric Sunshinec2842432015-07-17 19:00:10 -0400504 argv_array_push(&cp.args, branch);
Thomas Gummerere284e892017-11-26 19:43:53 +0000505 if (opt_track)
506 argv_array_push(&cp.args, opt_track);
Eric Sunshinec2842432015-07-17 19:00:10 -0400507 if (run_command(&cp))
508 return -1;
Thomas Gummererd861d342018-04-24 22:56:32 +0100509 branch = new_branch;
Thomas Gummerere284e892017-11-26 19:43:53 +0000510 } else if (opt_track) {
511 die(_("--[no-]track can only be used if a new branch is created"));
Eric Sunshinec2842432015-07-17 19:00:10 -0400512 }
Eric Sunshinefc563612015-07-06 13:30:50 -0400513
Jeff King0e5bba52017-09-08 02:38:41 -0400514 UNLEAK(path);
515 UNLEAK(opts);
Eric Sunshine80a05482015-07-17 19:00:12 -0400516 return add_worktree(path, branch, &opts);
Eric Sunshinefc563612015-07-06 13:30:50 -0400517}
518
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400519static void show_worktree_porcelain(struct worktree *wt)
520{
521 printf("worktree %s\n", wt->path);
522 if (wt->is_bare)
523 printf("bare\n");
524 else {
brian m. carlson0f051542017-10-15 22:07:08 +0000525 printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400526 if (wt->is_detached)
527 printf("detached\n");
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700528 else if (wt->head_ref)
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400529 printf("branch %s\n", wt->head_ref);
530 }
531 printf("\n");
532}
533
534static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
535{
536 struct strbuf sb = STRBUF_INIT;
537 int cur_path_len = strlen(wt->path);
538 int path_adj = cur_path_len - utf8_strwidth(wt->path);
539
540 strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path);
541 if (wt->is_bare)
542 strbuf_addstr(&sb, "(bare)");
543 else {
544 strbuf_addf(&sb, "%-*s ", abbrev_len,
brian m. carlsonaab95832018-03-12 02:27:30 +0000545 find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV));
Nguyễn Thái Ngọc Duy96f09e22016-11-28 16:36:53 +0700546 if (wt->is_detached)
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400547 strbuf_addstr(&sb, "(detached HEAD)");
Johannes Schindelin2e11f582017-05-04 15:59:13 +0200548 else if (wt->head_ref) {
549 char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
550 strbuf_addf(&sb, "[%s]", ref);
551 free(ref);
552 } else
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700553 strbuf_addstr(&sb, "(error)");
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400554 }
555 printf("%s\n", sb.buf);
556
557 strbuf_release(&sb);
558}
559
560static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
561{
562 int i;
563
564 for (i = 0; wt[i]; i++) {
565 int sha1_len;
566 int path_len = strlen(wt[i]->path);
567
568 if (path_len > *maxlen)
569 *maxlen = path_len;
brian m. carlsonaab95832018-03-12 02:27:30 +0000570 sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev));
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400571 if (sha1_len > *abbrev)
572 *abbrev = sha1_len;
573 }
574}
575
576static int list(int ac, const char **av, const char *prefix)
577{
578 int porcelain = 0;
579
580 struct option options[] = {
581 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
582 OPT_END()
583 };
584
585 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
586 if (ac)
587 usage_with_options(worktree_usage, options);
588 else {
Nguyễn Thái Ngọc Duy4df1d4d2016-11-28 16:36:56 +0700589 struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400590 int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
591
592 if (!porcelain)
593 measure_widths(worktrees, &abbrev, &path_maxlen);
594
595 for (i = 0; worktrees[i]; i++) {
596 if (porcelain)
597 show_worktree_porcelain(worktrees[i]);
598 else
599 show_worktree(worktrees[i], path_maxlen, abbrev);
600 }
601 free_worktrees(worktrees);
602 }
603 return 0;
604}
605
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700606static int lock_worktree(int ac, const char **av, const char *prefix)
607{
608 const char *reason = "", *old_reason;
609 struct option options[] = {
610 OPT_STRING(0, "reason", &reason, N_("string"),
611 N_("reason for locking")),
612 OPT_END()
613 };
614 struct worktree **worktrees, *wt;
615
616 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
617 if (ac != 1)
618 usage_with_options(worktree_usage, options);
619
Nguyễn Thái Ngọc Duy4fff1ef2016-11-28 16:36:55 +0700620 worktrees = get_worktrees(0);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700621 wt = find_worktree(worktrees, prefix, av[0]);
622 if (!wt)
623 die(_("'%s' is not a working tree"), av[0]);
624 if (is_main_worktree(wt))
625 die(_("The main working tree cannot be locked or unlocked"));
626
627 old_reason = is_worktree_locked(wt);
628 if (old_reason) {
629 if (*old_reason)
630 die(_("'%s' is already locked, reason: %s"),
631 av[0], old_reason);
632 die(_("'%s' is already locked"), av[0]);
633 }
634
635 write_file(git_common_path("worktrees/%s/locked", wt->id),
636 "%s", reason);
637 free_worktrees(worktrees);
638 return 0;
639}
640
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700641static int unlock_worktree(int ac, const char **av, const char *prefix)
642{
643 struct option options[] = {
644 OPT_END()
645 };
646 struct worktree **worktrees, *wt;
647 int ret;
648
649 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
650 if (ac != 1)
651 usage_with_options(worktree_usage, options);
652
Nguyễn Thái Ngọc Duy4fff1ef2016-11-28 16:36:55 +0700653 worktrees = get_worktrees(0);
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700654 wt = find_worktree(worktrees, prefix, av[0]);
655 if (!wt)
656 die(_("'%s' is not a working tree"), av[0]);
657 if (is_main_worktree(wt))
658 die(_("The main working tree cannot be locked or unlocked"));
659 if (!is_worktree_locked(wt))
660 die(_("'%s' is not locked"), av[0]);
661 ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
662 free_worktrees(worktrees);
663 return ret;
664}
665
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700666static void validate_no_submodules(const struct worktree *wt)
667{
668 struct index_state istate = { NULL };
669 int i, found_submodules = 0;
670
Junio C Hamanobd0f7942018-03-14 12:01:05 -0700671 if (read_index_from(&istate, worktree_git_path(wt, "index"),
672 get_worktree_git_dir(wt)) > 0) {
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700673 for (i = 0; i < istate.cache_nr; i++) {
674 struct cache_entry *ce = istate.cache[i];
675
676 if (S_ISGITLINK(ce->ce_mode)) {
677 found_submodules = 1;
678 break;
679 }
680 }
681 }
682 discard_index(&istate);
683
684 if (found_submodules)
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700685 die(_("working trees containing submodules cannot be moved or removed"));
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700686}
687
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700688static int move_worktree(int ac, const char **av, const char *prefix)
689{
690 struct option options[] = {
691 OPT_END()
692 };
693 struct worktree **worktrees, *wt;
694 struct strbuf dst = STRBUF_INIT;
695 struct strbuf errmsg = STRBUF_INIT;
696 const char *reason;
697 char *path;
698
699 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
700 if (ac != 2)
701 usage_with_options(worktree_usage, options);
702
703 path = prefix_filename(prefix, av[1]);
704 strbuf_addstr(&dst, path);
705 free(path);
706
707 worktrees = get_worktrees(0);
708 wt = find_worktree(worktrees, prefix, av[0]);
709 if (!wt)
710 die(_("'%s' is not a working tree"), av[0]);
711 if (is_main_worktree(wt))
712 die(_("'%s' is a main working tree"), av[0]);
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700713 if (is_directory(dst.buf)) {
714 const char *sep = find_last_dir_sep(wt->path);
715
716 if (!sep)
717 die(_("could not figure out destination name from '%s'"),
718 wt->path);
719 strbuf_trim_trailing_dir_sep(&dst);
720 strbuf_addstr(&dst, sep);
721 }
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700722 if (file_exists(dst.buf))
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700723 die(_("target '%s' already exists"), dst.buf);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700724
Nguyễn Thái Ngọc Duy78d986b2018-02-12 16:49:38 +0700725 validate_no_submodules(wt);
726
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700727 reason = is_worktree_locked(wt);
728 if (reason) {
729 if (*reason)
730 die(_("cannot move a locked working tree, lock reason: %s"),
731 reason);
732 die(_("cannot move a locked working tree"));
733 }
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700734 if (validate_worktree(wt, &errmsg, 0))
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700735 die(_("validation failed, cannot move working tree: %s"),
736 errmsg.buf);
737 strbuf_release(&errmsg);
738
739 if (rename(wt->path, dst.buf) == -1)
740 die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
741
742 update_worktree_location(wt, dst.buf);
743
744 strbuf_release(&dst);
745 free_worktrees(worktrees);
746 return 0;
747}
748
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700749/*
750 * Note, "git status --porcelain" is used to determine if it's safe to
751 * delete a whole worktree. "git status" does not ignore user
752 * configuration, so if a normal "git status" shows "clean" for the
753 * user, then it's ok to remove it.
754 *
755 * This assumption may be a bad one. We may want to ignore
756 * (potentially bad) user settings and only delete a worktree when
757 * it's absolutely safe to do so from _our_ point of view because we
758 * know better.
759 */
760static void check_clean_worktree(struct worktree *wt,
761 const char *original_path)
762{
763 struct argv_array child_env = ARGV_ARRAY_INIT;
764 struct child_process cp;
765 char buf[1];
766 int ret;
767
768 /*
769 * Until we sort this out, all submodules are "dirty" and
770 * will abort this function.
771 */
772 validate_no_submodules(wt);
773
774 argv_array_pushf(&child_env, "%s=%s/.git",
775 GIT_DIR_ENVIRONMENT, wt->path);
776 argv_array_pushf(&child_env, "%s=%s",
777 GIT_WORK_TREE_ENVIRONMENT, wt->path);
778 memset(&cp, 0, sizeof(cp));
779 argv_array_pushl(&cp.args, "status",
780 "--porcelain", "--ignore-submodules=none",
781 NULL);
782 cp.env = child_env.argv;
783 cp.git_cmd = 1;
784 cp.dir = wt->path;
785 cp.out = -1;
786 ret = start_command(&cp);
787 if (ret)
788 die_errno(_("failed to run 'git status' on '%s'"),
789 original_path);
790 ret = xread(cp.out, buf, sizeof(buf));
791 if (ret)
792 die(_("'%s' is dirty, use --force to delete it"),
793 original_path);
794 close(cp.out);
795 ret = finish_command(&cp);
796 if (ret)
797 die_errno(_("failed to run 'git status' on '%s', code %d"),
798 original_path, ret);
799}
800
801static int delete_git_work_tree(struct worktree *wt)
802{
803 struct strbuf sb = STRBUF_INIT;
804 int ret = 0;
805
806 strbuf_addstr(&sb, wt->path);
807 if (remove_dir_recursively(&sb, 0)) {
808 error_errno(_("failed to delete '%s'"), sb.buf);
809 ret = -1;
810 }
811 strbuf_release(&sb);
812 return ret;
813}
814
815static int delete_git_dir(struct worktree *wt)
816{
817 struct strbuf sb = STRBUF_INIT;
818 int ret = 0;
819
820 strbuf_addstr(&sb, git_common_path("worktrees/%s", wt->id));
821 if (remove_dir_recursively(&sb, 0)) {
822 error_errno(_("failed to delete '%s'"), sb.buf);
823 ret = -1;
824 }
825 strbuf_release(&sb);
826 return ret;
827}
828
829static int remove_worktree(int ac, const char **av, const char *prefix)
830{
831 int force = 0;
832 struct option options[] = {
Stefan Bellerd228eea2018-04-17 11:19:39 -0700833 OPT__FORCE(&force,
834 N_("force removing even if the worktree is dirty"),
835 PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700836 OPT_END()
837 };
838 struct worktree **worktrees, *wt;
839 struct strbuf errmsg = STRBUF_INIT;
840 const char *reason;
841 int ret = 0;
842
843 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
844 if (ac != 1)
845 usage_with_options(worktree_usage, options);
846
847 worktrees = get_worktrees(0);
848 wt = find_worktree(worktrees, prefix, av[0]);
849 if (!wt)
850 die(_("'%s' is not a working tree"), av[0]);
851 if (is_main_worktree(wt))
852 die(_("'%s' is a main working tree"), av[0]);
853 reason = is_worktree_locked(wt);
854 if (reason) {
855 if (*reason)
856 die(_("cannot remove a locked working tree, lock reason: %s"),
857 reason);
858 die(_("cannot remove a locked working tree"));
859 }
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700860 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700861 die(_("validation failed, cannot remove working tree: %s"),
862 errmsg.buf);
863 strbuf_release(&errmsg);
864
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700865 if (file_exists(wt->path)) {
866 if (!force)
867 check_clean_worktree(wt, av[0]);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700868
Nguyễn Thái Ngọc Duyee6763a2018-02-12 16:49:40 +0700869 ret |= delete_git_work_tree(wt);
870 }
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700871 /*
872 * continue on even if ret is non-zero, there's no going back
873 * from here.
874 */
875 ret |= delete_git_dir(wt);
876
877 free_worktrees(worktrees);
878 return ret;
879}
880
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700881int cmd_worktree(int ac, const char **av, const char *prefix)
882{
883 struct option options[] = {
884 OPT_END()
885 };
886
Thomas Gummerere92445a2017-11-29 20:04:51 +0000887 git_config(git_worktree_config, NULL);
Junio C Hamanod49028e2016-09-26 23:49:39 -0700888
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700889 if (ac < 2)
890 usage_with_options(worktree_usage, options);
Nguyễn Thái Ngọc Duy0409e0b2016-05-22 16:33:56 +0700891 if (!prefix)
892 prefix = "";
Eric Sunshinefc563612015-07-06 13:30:50 -0400893 if (!strcmp(av[1], "add"))
894 return add(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700895 if (!strcmp(av[1], "prune"))
896 return prune(ac - 1, av + 1, prefix);
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400897 if (!strcmp(av[1], "list"))
898 return list(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duy58142c02016-06-13 19:18:24 +0700899 if (!strcmp(av[1], "lock"))
900 return lock_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duy6d308622016-06-13 19:18:25 +0700901 if (!strcmp(av[1], "unlock"))
902 return unlock_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duy9f792bb2018-02-12 16:49:36 +0700903 if (!strcmp(av[1], "move"))
904 return move_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duycc733852018-02-12 16:49:39 +0700905 if (!strcmp(av[1], "remove"))
906 return remove_worktree(ac - 1, av + 1, prefix);
Nguyễn Thái Ngọc Duydf0b6cf2015-06-29 19:51:18 +0700907 usage_with_options(worktree_usage, options);
908}