Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 1 | #include "cache.h" |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 2 | #include "checkout.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 4 | #include "builtin.h" |
| 5 | #include "dir.h" |
| 6 | #include "parse-options.h" |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 7 | #include "argv-array.h" |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 8 | #include "branch.h" |
| 9 | #include "refs.h" |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 10 | #include "run-command.h" |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 11 | #include "sigchain.h" |
Junio C Hamano | 799767c | 2015-07-13 14:02:18 -0700 | [diff] [blame] | 12 | #include "refs.h" |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 13 | #include "utf8.h" |
| 14 | #include "worktree.h" |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 15 | |
| 16 | static const char * const worktree_usage[] = { |
Junio C Hamano | b780e44 | 2018-01-17 10:32:32 -0800 | [diff] [blame] | 17 | N_("git worktree add [<options>] <path> [<commit-ish>]"), |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 18 | N_("git worktree list [<options>]"), |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 19 | N_("git worktree lock [<options>] <path>"), |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 20 | N_("git worktree move <worktree> <new-path>"), |
Nguyễn Thái Ngọc Duy | 7b722d9 | 2016-05-22 16:33:53 +0700 | [diff] [blame] | 21 | N_("git worktree prune [<options>]"), |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 22 | N_("git worktree remove [<options>] <worktree>"), |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 23 | N_("git worktree unlock <path>"), |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 24 | NULL |
| 25 | }; |
| 26 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 27 | struct add_opts { |
| 28 | int force; |
| 29 | int detach; |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 30 | int checkout; |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 31 | int keep_locked; |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 32 | }; |
| 33 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 34 | static int show_only; |
| 35 | static int verbose; |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 36 | static int guess_remote; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 37 | static timestamp_t expire; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 38 | |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 39 | static 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 Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 49 | static int prune_worktree(const char *id, struct strbuf *reason) |
| 50 | { |
| 51 | struct stat st; |
| 52 | char *path; |
Jeff King | 228740b | 2017-09-27 02:02:21 -0400 | [diff] [blame] | 53 | int fd; |
| 54 | size_t len; |
Jeff King | 8a1a8d2 | 2017-09-27 02:02:27 -0400 | [diff] [blame] | 55 | ssize_t read_result; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 56 | |
| 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 King | 228740b | 2017-09-27 02:02:21 -0400 | [diff] [blame] | 73 | len = xsize_t(st.st_size); |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 74 | path = xmallocz(len); |
Jeff King | 8a1a8d2 | 2017-09-27 02:02:27 -0400 | [diff] [blame] | 75 | |
| 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 Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 84 | close(fd); |
Jeff King | 8a1a8d2 | 2017-09-27 02:02:27 -0400 | [diff] [blame] | 85 | |
| 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 Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 93 | 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 Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 102 | free(path); |
Nguyễn Thái Ngọc Duy | 327864a | 2018-03-15 17:44:12 +0100 | [diff] [blame] | 103 | if (stat(git_path("worktrees/%s/index", id), &st) || |
| 104 | st.st_mtime <= expire) { |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 105 | 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 | |
| 115 | static 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 Duy | afb9e30 | 2016-05-22 16:33:54 +0700 | [diff] [blame] | 125 | if (is_dot_or_dotdot(d->d_name)) |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 126 | 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 King | 8c2ca3a | 2017-04-20 17:09:30 -0400 | [diff] [blame] | 134 | git_path_buf(&path, "worktrees/%s", d->d_name); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 135 | 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 Duy | 8d19e93 | 2016-05-08 16:47:34 +0700 | [diff] [blame] | 139 | error_errno(_("failed to remove '%s'"), path.buf); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 140 | } |
| 141 | closedir(dir); |
| 142 | if (!show_only) |
| 143 | rmdir(git_path("worktrees")); |
| 144 | strbuf_release(&reason); |
| 145 | strbuf_release(&path); |
| 146 | } |
| 147 | |
| 148 | static 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 Steinhardt | 2488dca | 2017-02-06 14:13:59 +0100 | [diff] [blame] | 152 | OPT__VERBOSE(&verbose, N_("report pruned working trees")), |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 153 | OPT_EXPIRY_DATE(0, "expire", &expire, |
Patrick Steinhardt | 2488dca | 2017-02-06 14:13:59 +0100 | [diff] [blame] | 154 | N_("expire working trees older than <time>")), |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 155 | OPT_END() |
| 156 | }; |
| 157 | |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 158 | expire = TIME_MAX; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 159 | 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 Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 166 | static char *junk_work_tree; |
| 167 | static char *junk_git_dir; |
| 168 | static int is_junk; |
| 169 | static pid_t junk_pid; |
| 170 | |
| 171 | static 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 | |
| 188 | static void remove_junk_on_signal(int signo) |
| 189 | { |
| 190 | remove_junk(); |
| 191 | sigchain_pop(signo); |
| 192 | raise(signo); |
| 193 | } |
| 194 | |
Eric Sunshine | f5682b2 | 2015-07-06 13:30:57 -0400 | [diff] [blame] | 195 | static 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 Sunshine | 80a0548 | 2015-07-17 19:00:12 -0400 | [diff] [blame] | 214 | static int add_worktree(const char *path, const char *refname, |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 215 | const struct add_opts *opts) |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 216 | { |
| 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é Scharfe | 542aa25 | 2016-08-05 22:38:44 +0200 | [diff] [blame] | 221 | struct child_process cp = CHILD_PROCESS_INIT; |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 222 | struct argv_array child_env = ARGV_ARRAY_INIT; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 223 | int counter = 0, len, ret; |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 224 | struct strbuf symref = STRBUF_INIT; |
| 225 | struct commit *commit = NULL; |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 226 | int is_branch = 0; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 227 | |
| 228 | if (file_exists(path) && !is_empty_dir(path)) |
| 229 | die(_("'%s' already exists"), path); |
| 230 | |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 231 | /* is 'refname' a branch or commit? */ |
Nguyễn Thái Ngọc Duy | 0ebf4a2 | 2016-02-15 20:35:32 +0700 | [diff] [blame] | 232 | if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 233 | ref_exists(symref.buf)) { |
| 234 | is_branch = 1; |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 235 | if (!opts->force) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 236 | die_if_checked_out(symref.buf, 0); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 237 | } |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 238 | commit = lookup_commit_reference_by_name(refname); |
| 239 | if (!commit) |
| 240 | die(_("invalid reference: %s"), refname); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 241 | |
Eric Sunshine | f5682b2 | 2015-07-06 13:30:57 -0400 | [diff] [blame] | 242 | name = worktree_basename(path, &len); |
Jeff King | 8c2ca3a | 2017-04-20 17:09:30 -0400 | [diff] [blame] | 243 | git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 244 | 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 Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 269 | if (!opts->keep_locked) |
| 270 | write_file(sb.buf, "initializing"); |
| 271 | else |
| 272 | write_file(sb.buf, "added with --lock"); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 273 | |
| 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 Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 282 | write_file(sb.buf, "%s", real_path(sb_git.buf)); |
| 283 | write_file(sb_git.buf, "gitdir: %s/worktrees/%s", |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 284 | real_path(get_git_common_dir()), name); |
| 285 | /* |
| 286 | * This is to keep resolve_ref() happy. We need a valid HEAD |
Eric Sunshine | ed197a6 | 2015-07-17 19:00:15 -0400 | [diff] [blame] | 287 | * 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 Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 291 | */ |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 292 | strbuf_reset(&sb); |
| 293 | strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); |
Jeff King | dabd35f | 2016-07-08 06:35:15 -0400 | [diff] [blame] | 294 | write_file(sb.buf, "%s", sha1_to_hex(null_sha1)); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 295 | strbuf_reset(&sb); |
| 296 | strbuf_addf(&sb, "%s/commondir", sb_repo.buf); |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 297 | write_file(sb.buf, "../.."); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 298 | |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 299 | 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 Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 301 | cp.git_cmd = 1; |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 302 | |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 303 | if (!is_branch) |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 304 | argv_array_pushl(&cp.args, "update-ref", "HEAD", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 305 | oid_to_hex(&commit->object.oid), NULL); |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 306 | 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 Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 314 | 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 Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 322 | } |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 323 | |
| 324 | is_junk = 0; |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 325 | FREE_AND_NULL(junk_work_tree); |
| 326 | FREE_AND_NULL(junk_git_dir); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 327 | |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 328 | done: |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 329 | 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 Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 334 | |
| 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 Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 339 | 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 Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 356 | |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 357 | argv_array_clear(&child_env); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 358 | strbuf_release(&sb); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 359 | strbuf_release(&symref); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 360 | strbuf_release(&sb_repo); |
| 361 | strbuf_release(&sb_git); |
| 362 | return ret; |
| 363 | } |
| 364 | |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 365 | static 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 Hamano | 10174da | 2018-05-23 14:38:18 +0900 | [diff] [blame^] | 377 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 378 | } 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 Hamano | 10174da | 2018-05-23 14:38:18 +0900 | [diff] [blame^] | 391 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 392 | } |
| 393 | strbuf_release(&s); |
| 394 | } |
| 395 | } |
| 396 | |
Thomas Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 397 | static const char *dwim_branch(const char *path, const char **new_branch) |
| 398 | { |
| 399 | int n; |
| 400 | const char *s = worktree_basename(path, &n); |
Thomas Gummerer | f60a7b7 | 2018-04-24 22:56:35 +0100 | [diff] [blame] | 401 | 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 Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 412 | 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 Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 421 | static int add(int ac, const char **av, const char *prefix) |
| 422 | { |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 423 | struct add_opts opts; |
| 424 | const char *new_branch_force = NULL; |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 425 | char *path; |
| 426 | const char *branch; |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 427 | const char *new_branch = NULL; |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 428 | const char *opt_track = NULL; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 429 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | 1224781 | 2018-02-09 18:01:42 +0700 | [diff] [blame] | 430 | OPT__FORCE(&opts.force, |
| 431 | N_("checkout <branch> even if already checked out in other worktree"), |
Nguyễn Thái Ngọc Duy | fc3d4e0 | 2018-02-09 18:02:20 +0700 | [diff] [blame] | 432 | PARSE_OPT_NOCOMPLETE), |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 433 | OPT_STRING('b', NULL, &new_branch, N_("branch"), |
Eric Sunshine | cbdf60f | 2015-07-06 13:30:53 -0400 | [diff] [blame] | 434 | N_("create a new branch")), |
| 435 | OPT_STRING('B', NULL, &new_branch_force, N_("branch"), |
| 436 | N_("create or reset a branch")), |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 437 | OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")), |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 438 | OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")), |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 439 | OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")), |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 440 | 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 Gummerer | 71d6682 | 2017-11-29 20:04:50 +0000 | [diff] [blame] | 443 | OPT_BOOL(0, "guess-remote", &guess_remote, |
| 444 | N_("try to match the new branch name with a remote-tracking branch")), |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 445 | OPT_END() |
| 446 | }; |
| 447 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 448 | memset(&opts, 0, sizeof(opts)); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 449 | opts.checkout = 1; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 450 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 451 | if (!!opts.detach + !!new_branch + !!new_branch_force > 1) |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 452 | die(_("-b, -B, and --detach are mutually exclusive")); |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 453 | if (ac < 1 || ac > 2) |
| 454 | usage_with_options(worktree_usage, options); |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 455 | |
Jeff King | 116fb64 | 2017-03-20 21:22:28 -0400 | [diff] [blame] | 456 | path = prefix_filename(prefix, av[0]); |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 457 | branch = ac < 2 ? "HEAD" : av[1]; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 458 | |
Jordan DE GEA | 1a450e2 | 2016-05-27 15:17:08 +0200 | [diff] [blame] | 459 | if (!strcmp(branch, "-")) |
| 460 | branch = "@{-1}"; |
| 461 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 462 | if (new_branch_force) { |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 463 | struct strbuf symref = STRBUF_INIT; |
| 464 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 465 | new_branch = new_branch_force; |
Eric Sunshine | eef005d | 2015-07-17 19:00:06 -0400 | [diff] [blame] | 466 | |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 467 | if (!opts.force && |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 468 | !strbuf_check_branch_ref(&symref, new_branch) && |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 469 | ref_exists(symref.buf)) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 470 | die_if_checked_out(symref.buf, 0); |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 471 | strbuf_release(&symref); |
| 472 | } |
| 473 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 474 | if (ac < 2 && !new_branch && !opts.detach) { |
Thomas Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 475 | const char *s = dwim_branch(path, &new_branch); |
| 476 | if (s) |
| 477 | branch = s; |
Eric Sunshine | 1eb07d8 | 2015-07-06 13:30:59 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 480 | if (ac == 2 && !new_branch && !opts.detach) { |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 481 | 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 Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 489 | new_branch = branch; |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 490 | branch = remote; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 495 | print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force); |
| 496 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 497 | if (new_branch) { |
René Scharfe | 542aa25 | 2016-08-05 22:38:44 +0200 | [diff] [blame] | 498 | struct child_process cp = CHILD_PROCESS_INIT; |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 499 | cp.git_cmd = 1; |
| 500 | argv_array_push(&cp.args, "branch"); |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 501 | if (new_branch_force) |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 502 | argv_array_push(&cp.args, "--force"); |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 503 | argv_array_push(&cp.args, new_branch); |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 504 | argv_array_push(&cp.args, branch); |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 505 | if (opt_track) |
| 506 | argv_array_push(&cp.args, opt_track); |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 507 | if (run_command(&cp)) |
| 508 | return -1; |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 509 | branch = new_branch; |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 510 | } else if (opt_track) { |
| 511 | die(_("--[no-]track can only be used if a new branch is created")); |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 512 | } |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 513 | |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 514 | UNLEAK(path); |
| 515 | UNLEAK(opts); |
Eric Sunshine | 80a0548 | 2015-07-17 19:00:12 -0400 | [diff] [blame] | 516 | return add_worktree(path, branch, &opts); |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 519 | static 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. carlson | 0f05154 | 2017-10-15 22:07:08 +0000 | [diff] [blame] | 525 | printf("HEAD %s\n", oid_to_hex(&wt->head_oid)); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 526 | if (wt->is_detached) |
| 527 | printf("detached\n"); |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 528 | else if (wt->head_ref) |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 529 | printf("branch %s\n", wt->head_ref); |
| 530 | } |
| 531 | printf("\n"); |
| 532 | } |
| 533 | |
| 534 | static 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. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 545 | find_unique_abbrev(&wt->head_oid, DEFAULT_ABBREV)); |
Nguyễn Thái Ngọc Duy | 96f09e2 | 2016-11-28 16:36:53 +0700 | [diff] [blame] | 546 | if (wt->is_detached) |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 547 | strbuf_addstr(&sb, "(detached HEAD)"); |
Johannes Schindelin | 2e11f58 | 2017-05-04 15:59:13 +0200 | [diff] [blame] | 548 | 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 Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 553 | strbuf_addstr(&sb, "(error)"); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 554 | } |
| 555 | printf("%s\n", sb.buf); |
| 556 | |
| 557 | strbuf_release(&sb); |
| 558 | } |
| 559 | |
| 560 | static 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. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 570 | sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev)); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 571 | if (sha1_len > *abbrev) |
| 572 | *abbrev = sha1_len; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | static 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 Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 589 | struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 590 | 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 Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 606 | static 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 Duy | 4fff1ef | 2016-11-28 16:36:55 +0700 | [diff] [blame] | 620 | worktrees = get_worktrees(0); |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 621 | 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 Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 641 | static 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 Duy | 4fff1ef | 2016-11-28 16:36:55 +0700 | [diff] [blame] | 653 | worktrees = get_worktrees(0); |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 654 | 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 Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 666 | static void validate_no_submodules(const struct worktree *wt) |
| 667 | { |
| 668 | struct index_state istate = { NULL }; |
| 669 | int i, found_submodules = 0; |
| 670 | |
Junio C Hamano | bd0f794 | 2018-03-14 12:01:05 -0700 | [diff] [blame] | 671 | if (read_index_from(&istate, worktree_git_path(wt, "index"), |
| 672 | get_worktree_git_dir(wt)) > 0) { |
Nguyễn Thái Ngọc Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 673 | 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 Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 685 | die(_("working trees containing submodules cannot be moved or removed")); |
Nguyễn Thái Ngọc Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 686 | } |
| 687 | |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 688 | static 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 Duy | c64a8d2 | 2018-02-12 16:49:37 +0700 | [diff] [blame] | 713 | 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 Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 722 | if (file_exists(dst.buf)) |
Nguyễn Thái Ngọc Duy | c64a8d2 | 2018-02-12 16:49:37 +0700 | [diff] [blame] | 723 | die(_("target '%s' already exists"), dst.buf); |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 724 | |
Nguyễn Thái Ngọc Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 725 | validate_no_submodules(wt); |
| 726 | |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 727 | 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 Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 734 | if (validate_worktree(wt, &errmsg, 0)) |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 735 | 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 Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 749 | /* |
| 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 | */ |
| 760 | static 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 | |
| 801 | static 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 | |
| 815 | static 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 | |
| 829 | static int remove_worktree(int ac, const char **av, const char *prefix) |
| 830 | { |
| 831 | int force = 0; |
| 832 | struct option options[] = { |
Stefan Beller | d228eea | 2018-04-17 11:19:39 -0700 | [diff] [blame] | 833 | OPT__FORCE(&force, |
| 834 | N_("force removing even if the worktree is dirty"), |
| 835 | PARSE_OPT_NOCOMPLETE), |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 836 | 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 Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 860 | if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK)) |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 861 | die(_("validation failed, cannot remove working tree: %s"), |
| 862 | errmsg.buf); |
| 863 | strbuf_release(&errmsg); |
| 864 | |
Nguyễn Thái Ngọc Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 865 | if (file_exists(wt->path)) { |
| 866 | if (!force) |
| 867 | check_clean_worktree(wt, av[0]); |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 868 | |
Nguyễn Thái Ngọc Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 869 | ret |= delete_git_work_tree(wt); |
| 870 | } |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 871 | /* |
| 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 Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 881 | int cmd_worktree(int ac, const char **av, const char *prefix) |
| 882 | { |
| 883 | struct option options[] = { |
| 884 | OPT_END() |
| 885 | }; |
| 886 | |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 887 | git_config(git_worktree_config, NULL); |
Junio C Hamano | d49028e | 2016-09-26 23:49:39 -0700 | [diff] [blame] | 888 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 889 | if (ac < 2) |
| 890 | usage_with_options(worktree_usage, options); |
Nguyễn Thái Ngọc Duy | 0409e0b | 2016-05-22 16:33:56 +0700 | [diff] [blame] | 891 | if (!prefix) |
| 892 | prefix = ""; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 893 | if (!strcmp(av[1], "add")) |
| 894 | return add(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 895 | if (!strcmp(av[1], "prune")) |
| 896 | return prune(ac - 1, av + 1, prefix); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 897 | if (!strcmp(av[1], "list")) |
| 898 | return list(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 899 | if (!strcmp(av[1], "lock")) |
| 900 | return lock_worktree(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 901 | if (!strcmp(av[1], "unlock")) |
| 902 | return unlock_worktree(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 903 | if (!strcmp(av[1], "move")) |
| 904 | return move_worktree(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 905 | if (!strcmp(av[1], "remove")) |
| 906 | return remove_worktree(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 907 | usage_with_options(worktree_usage, options); |
| 908 | } |