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[] = { |
Sidhant Sharma | ae9f274 | 2015-10-19 10:14:53 +0530 | [diff] [blame] | 17 | N_("git worktree add [<options>] <path> [<branch>]"), |
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 | 7b722d9 | 2016-05-22 16:33:53 +0700 | [diff] [blame] | 20 | N_("git worktree prune [<options>]"), |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 21 | N_("git worktree unlock <path>"), |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 22 | NULL |
| 23 | }; |
| 24 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 25 | struct add_opts { |
| 26 | int force; |
| 27 | int detach; |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 28 | int checkout; |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 29 | int keep_locked; |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 30 | const char *new_branch; |
| 31 | int force_new_branch; |
| 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)) { |
| 102 | struct stat st_link; |
| 103 | free(path); |
| 104 | /* |
| 105 | * the repo is moved manually and has not been |
| 106 | * accessed since? |
| 107 | */ |
| 108 | if (!stat(git_path("worktrees/%s/link", id), &st_link) && |
| 109 | st_link.st_nlink > 1) |
| 110 | return 0; |
| 111 | if (st.st_mtime <= expire) { |
| 112 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id); |
| 113 | return 1; |
| 114 | } else { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | free(path); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static void prune_worktrees(void) |
| 123 | { |
| 124 | struct strbuf reason = STRBUF_INIT; |
| 125 | struct strbuf path = STRBUF_INIT; |
| 126 | DIR *dir = opendir(git_path("worktrees")); |
| 127 | struct dirent *d; |
| 128 | int ret; |
| 129 | if (!dir) |
| 130 | return; |
| 131 | while ((d = readdir(dir)) != NULL) { |
Nguyễn Thái Ngọc Duy | afb9e30 | 2016-05-22 16:33:54 +0700 | [diff] [blame] | 132 | 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] | 133 | continue; |
| 134 | strbuf_reset(&reason); |
| 135 | if (!prune_worktree(d->d_name, &reason)) |
| 136 | continue; |
| 137 | if (show_only || verbose) |
| 138 | printf("%s\n", reason.buf); |
| 139 | if (show_only) |
| 140 | continue; |
Jeff King | 8c2ca3a | 2017-04-20 17:09:30 -0400 | [diff] [blame] | 141 | 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] | 142 | ret = remove_dir_recursively(&path, 0); |
| 143 | if (ret < 0 && errno == ENOTDIR) |
| 144 | ret = unlink(path.buf); |
| 145 | if (ret) |
Nguyễn Thái Ngọc Duy | 8d19e93 | 2016-05-08 16:47:34 +0700 | [diff] [blame] | 146 | 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] | 147 | } |
| 148 | closedir(dir); |
| 149 | if (!show_only) |
| 150 | rmdir(git_path("worktrees")); |
| 151 | strbuf_release(&reason); |
| 152 | strbuf_release(&path); |
| 153 | } |
| 154 | |
| 155 | static int prune(int ac, const char **av, const char *prefix) |
| 156 | { |
| 157 | struct option options[] = { |
| 158 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
Patrick Steinhardt | 2488dca | 2017-02-06 14:13:59 +0100 | [diff] [blame] | 159 | 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] | 160 | OPT_EXPIRY_DATE(0, "expire", &expire, |
Patrick Steinhardt | 2488dca | 2017-02-06 14:13:59 +0100 | [diff] [blame] | 161 | N_("expire working trees older than <time>")), |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 162 | OPT_END() |
| 163 | }; |
| 164 | |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 165 | expire = TIME_MAX; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 166 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 167 | if (ac) |
| 168 | usage_with_options(worktree_usage, options); |
| 169 | prune_worktrees(); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 173 | static char *junk_work_tree; |
| 174 | static char *junk_git_dir; |
| 175 | static int is_junk; |
| 176 | static pid_t junk_pid; |
| 177 | |
| 178 | static void remove_junk(void) |
| 179 | { |
| 180 | struct strbuf sb = STRBUF_INIT; |
| 181 | if (!is_junk || getpid() != junk_pid) |
| 182 | return; |
| 183 | if (junk_git_dir) { |
| 184 | strbuf_addstr(&sb, junk_git_dir); |
| 185 | remove_dir_recursively(&sb, 0); |
| 186 | strbuf_reset(&sb); |
| 187 | } |
| 188 | if (junk_work_tree) { |
| 189 | strbuf_addstr(&sb, junk_work_tree); |
| 190 | remove_dir_recursively(&sb, 0); |
| 191 | } |
| 192 | strbuf_release(&sb); |
| 193 | } |
| 194 | |
| 195 | static void remove_junk_on_signal(int signo) |
| 196 | { |
| 197 | remove_junk(); |
| 198 | sigchain_pop(signo); |
| 199 | raise(signo); |
| 200 | } |
| 201 | |
Eric Sunshine | f5682b2 | 2015-07-06 13:30:57 -0400 | [diff] [blame] | 202 | static const char *worktree_basename(const char *path, int *olen) |
| 203 | { |
| 204 | const char *name; |
| 205 | int len; |
| 206 | |
| 207 | len = strlen(path); |
| 208 | while (len && is_dir_sep(path[len - 1])) |
| 209 | len--; |
| 210 | |
| 211 | for (name = path + len - 1; name > path; name--) |
| 212 | if (is_dir_sep(*name)) { |
| 213 | name++; |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | *olen = len; |
| 218 | return name; |
| 219 | } |
| 220 | |
Eric Sunshine | 80a0548 | 2015-07-17 19:00:12 -0400 | [diff] [blame] | 221 | static int add_worktree(const char *path, const char *refname, |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 222 | const struct add_opts *opts) |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 223 | { |
| 224 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; |
| 225 | struct strbuf sb = STRBUF_INIT; |
| 226 | const char *name; |
| 227 | struct stat st; |
René Scharfe | 542aa25 | 2016-08-05 22:38:44 +0200 | [diff] [blame] | 228 | struct child_process cp = CHILD_PROCESS_INIT; |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 229 | struct argv_array child_env = ARGV_ARRAY_INIT; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 230 | int counter = 0, len, ret; |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 231 | struct strbuf symref = STRBUF_INIT; |
| 232 | struct commit *commit = NULL; |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 233 | int is_branch = 0; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 234 | |
| 235 | if (file_exists(path) && !is_empty_dir(path)) |
| 236 | die(_("'%s' already exists"), path); |
| 237 | |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 238 | /* is 'refname' a branch or commit? */ |
Nguyễn Thái Ngọc Duy | 0ebf4a2 | 2016-02-15 20:35:32 +0700 | [diff] [blame] | 239 | if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 240 | ref_exists(symref.buf)) { |
| 241 | is_branch = 1; |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 242 | if (!opts->force) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 243 | die_if_checked_out(symref.buf, 0); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 244 | } |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 245 | commit = lookup_commit_reference_by_name(refname); |
| 246 | if (!commit) |
| 247 | die(_("invalid reference: %s"), refname); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 248 | |
Eric Sunshine | f5682b2 | 2015-07-06 13:30:57 -0400 | [diff] [blame] | 249 | name = worktree_basename(path, &len); |
Jeff King | 8c2ca3a | 2017-04-20 17:09:30 -0400 | [diff] [blame] | 250 | git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 251 | len = sb_repo.len; |
| 252 | if (safe_create_leading_directories_const(sb_repo.buf)) |
| 253 | die_errno(_("could not create leading directories of '%s'"), |
| 254 | sb_repo.buf); |
| 255 | while (!stat(sb_repo.buf, &st)) { |
| 256 | counter++; |
| 257 | strbuf_setlen(&sb_repo, len); |
| 258 | strbuf_addf(&sb_repo, "%d", counter); |
| 259 | } |
| 260 | name = strrchr(sb_repo.buf, '/') + 1; |
| 261 | |
| 262 | junk_pid = getpid(); |
| 263 | atexit(remove_junk); |
| 264 | sigchain_push_common(remove_junk_on_signal); |
| 265 | |
| 266 | if (mkdir(sb_repo.buf, 0777)) |
| 267 | die_errno(_("could not create directory of '%s'"), sb_repo.buf); |
| 268 | junk_git_dir = xstrdup(sb_repo.buf); |
| 269 | is_junk = 1; |
| 270 | |
| 271 | /* |
| 272 | * lock the incomplete repo so prune won't delete it, unlock |
| 273 | * after the preparation is over. |
| 274 | */ |
| 275 | 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] | 276 | if (!opts->keep_locked) |
| 277 | write_file(sb.buf, "initializing"); |
| 278 | else |
| 279 | write_file(sb.buf, "added with --lock"); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 280 | |
| 281 | strbuf_addf(&sb_git, "%s/.git", path); |
| 282 | if (safe_create_leading_directories_const(sb_git.buf)) |
| 283 | die_errno(_("could not create leading directories of '%s'"), |
| 284 | sb_git.buf); |
| 285 | junk_work_tree = xstrdup(path); |
| 286 | |
| 287 | strbuf_reset(&sb); |
| 288 | strbuf_addf(&sb, "%s/gitdir", sb_repo.buf); |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 289 | write_file(sb.buf, "%s", real_path(sb_git.buf)); |
| 290 | write_file(sb_git.buf, "gitdir: %s/worktrees/%s", |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 291 | real_path(get_git_common_dir()), name); |
| 292 | /* |
| 293 | * This is to keep resolve_ref() happy. We need a valid HEAD |
Eric Sunshine | ed197a6 | 2015-07-17 19:00:15 -0400 | [diff] [blame] | 294 | * or is_git_directory() will reject the directory. Any value which |
| 295 | * looks like an object ID will do since it will be immediately |
| 296 | * replaced by the symbolic-ref or update-ref invocation in the new |
| 297 | * worktree. |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 298 | */ |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 299 | strbuf_reset(&sb); |
| 300 | strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); |
Jeff King | dabd35f | 2016-07-08 06:35:15 -0400 | [diff] [blame] | 301 | write_file(sb.buf, "%s", sha1_to_hex(null_sha1)); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 302 | strbuf_reset(&sb); |
| 303 | strbuf_addf(&sb, "%s/commondir", sb_repo.buf); |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 304 | write_file(sb.buf, "../.."); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 305 | |
Eric Sunshine | cd2f471 | 2015-07-17 19:00:05 -0400 | [diff] [blame] | 306 | fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 307 | |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 308 | argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); |
| 309 | argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 310 | cp.git_cmd = 1; |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 311 | |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 312 | if (!is_branch) |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 313 | argv_array_pushl(&cp.args, "update-ref", "HEAD", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 314 | oid_to_hex(&commit->object.oid), NULL); |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 315 | else |
| 316 | argv_array_pushl(&cp.args, "symbolic-ref", "HEAD", |
| 317 | symref.buf, NULL); |
| 318 | cp.env = child_env.argv; |
| 319 | ret = run_command(&cp); |
| 320 | if (ret) |
| 321 | goto done; |
| 322 | |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 323 | if (opts->checkout) { |
| 324 | cp.argv = NULL; |
| 325 | argv_array_clear(&cp.args); |
| 326 | argv_array_pushl(&cp.args, "reset", "--hard", NULL); |
| 327 | cp.env = child_env.argv; |
| 328 | ret = run_command(&cp); |
| 329 | if (ret) |
| 330 | goto done; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 331 | } |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 332 | |
| 333 | is_junk = 0; |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 334 | FREE_AND_NULL(junk_work_tree); |
| 335 | FREE_AND_NULL(junk_git_dir); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 336 | |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 337 | done: |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 338 | if (ret || !opts->keep_locked) { |
| 339 | strbuf_reset(&sb); |
| 340 | strbuf_addf(&sb, "%s/locked", sb_repo.buf); |
| 341 | unlink_or_warn(sb.buf); |
| 342 | } |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * Hook failure does not warrant worktree deletion, so run hook after |
| 346 | * is_junk is cleared, but do return appropriate code when hook fails. |
| 347 | */ |
| 348 | if (!ret && opts->checkout) |
| 349 | ret = run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid), |
| 350 | oid_to_hex(&commit->object.oid), "1", NULL); |
| 351 | |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 352 | argv_array_clear(&child_env); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 353 | strbuf_release(&sb); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 354 | strbuf_release(&symref); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 355 | strbuf_release(&sb_repo); |
| 356 | strbuf_release(&sb_git); |
| 357 | return ret; |
| 358 | } |
| 359 | |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 360 | static int add(int ac, const char **av, const char *prefix) |
| 361 | { |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 362 | struct add_opts opts; |
| 363 | const char *new_branch_force = NULL; |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 364 | char *path; |
| 365 | const char *branch; |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 366 | const char *opt_track = NULL; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 367 | struct option options[] = { |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 368 | OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")), |
| 369 | OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), |
Eric Sunshine | cbdf60f | 2015-07-06 13:30:53 -0400 | [diff] [blame] | 370 | N_("create a new branch")), |
| 371 | OPT_STRING('B', NULL, &new_branch_force, N_("branch"), |
| 372 | N_("create or reset a branch")), |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 373 | OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")), |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 374 | 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] | 375 | 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] | 376 | OPT_PASSTHRU(0, "track", &opt_track, NULL, |
| 377 | N_("set up tracking mode (see git-branch(1))"), |
| 378 | PARSE_OPT_NOARG | PARSE_OPT_OPTARG), |
Thomas Gummerer | 71d6682 | 2017-11-29 20:04:50 +0000 | [diff] [blame] | 379 | OPT_BOOL(0, "guess-remote", &guess_remote, |
| 380 | 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] | 381 | OPT_END() |
| 382 | }; |
| 383 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 384 | memset(&opts, 0, sizeof(opts)); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 385 | opts.checkout = 1; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 386 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 387 | if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1) |
| 388 | die(_("-b, -B, and --detach are mutually exclusive")); |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 389 | if (ac < 1 || ac > 2) |
| 390 | usage_with_options(worktree_usage, options); |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 391 | |
Jeff King | 116fb64 | 2017-03-20 21:22:28 -0400 | [diff] [blame] | 392 | path = prefix_filename(prefix, av[0]); |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 393 | branch = ac < 2 ? "HEAD" : av[1]; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 394 | |
Jordan DE GEA | 1a450e2 | 2016-05-27 15:17:08 +0200 | [diff] [blame] | 395 | if (!strcmp(branch, "-")) |
| 396 | branch = "@{-1}"; |
| 397 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 398 | opts.force_new_branch = !!new_branch_force; |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 399 | if (opts.force_new_branch) { |
| 400 | struct strbuf symref = STRBUF_INIT; |
| 401 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 402 | opts.new_branch = new_branch_force; |
Eric Sunshine | eef005d | 2015-07-17 19:00:06 -0400 | [diff] [blame] | 403 | |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 404 | if (!opts.force && |
| 405 | !strbuf_check_branch_ref(&symref, opts.new_branch) && |
| 406 | ref_exists(symref.buf)) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 407 | die_if_checked_out(symref.buf, 0); |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 408 | strbuf_release(&symref); |
| 409 | } |
| 410 | |
Eric Sunshine | 5c94257 | 2015-07-17 19:00:09 -0400 | [diff] [blame] | 411 | if (ac < 2 && !opts.new_branch && !opts.detach) { |
Eric Sunshine | 1eb07d8 | 2015-07-06 13:30:59 -0400 | [diff] [blame] | 412 | int n; |
| 413 | const char *s = worktree_basename(path, &n); |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 414 | opts.new_branch = xstrndup(s, n); |
Thomas Gummerer | 71d6682 | 2017-11-29 20:04:50 +0000 | [diff] [blame] | 415 | if (guess_remote) { |
| 416 | struct object_id oid; |
| 417 | const char *remote = |
| 418 | unique_tracking_name(opts.new_branch, &oid); |
| 419 | if (remote) |
| 420 | branch = remote; |
| 421 | } |
Eric Sunshine | 1eb07d8 | 2015-07-06 13:30:59 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 424 | if (ac == 2 && !opts.new_branch && !opts.detach) { |
| 425 | struct object_id oid; |
| 426 | struct commit *commit; |
| 427 | const char *remote; |
| 428 | |
| 429 | commit = lookup_commit_reference_by_name(branch); |
| 430 | if (!commit) { |
| 431 | remote = unique_tracking_name(branch, &oid); |
| 432 | if (remote) { |
| 433 | opts.new_branch = branch; |
| 434 | branch = remote; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 439 | if (opts.new_branch) { |
René Scharfe | 542aa25 | 2016-08-05 22:38:44 +0200 | [diff] [blame] | 440 | struct child_process cp = CHILD_PROCESS_INIT; |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 441 | cp.git_cmd = 1; |
| 442 | argv_array_push(&cp.args, "branch"); |
| 443 | if (opts.force_new_branch) |
| 444 | argv_array_push(&cp.args, "--force"); |
| 445 | argv_array_push(&cp.args, opts.new_branch); |
| 446 | argv_array_push(&cp.args, branch); |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 447 | if (opt_track) |
| 448 | argv_array_push(&cp.args, opt_track); |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 449 | if (run_command(&cp)) |
| 450 | return -1; |
| 451 | branch = opts.new_branch; |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 452 | } else if (opt_track) { |
| 453 | 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] | 454 | } |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 455 | |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 456 | UNLEAK(path); |
| 457 | UNLEAK(opts); |
Eric Sunshine | 80a0548 | 2015-07-17 19:00:12 -0400 | [diff] [blame] | 458 | return add_worktree(path, branch, &opts); |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 461 | static void show_worktree_porcelain(struct worktree *wt) |
| 462 | { |
| 463 | printf("worktree %s\n", wt->path); |
| 464 | if (wt->is_bare) |
| 465 | printf("bare\n"); |
| 466 | else { |
brian m. carlson | 0f05154 | 2017-10-15 22:07:08 +0000 | [diff] [blame] | 467 | printf("HEAD %s\n", oid_to_hex(&wt->head_oid)); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 468 | if (wt->is_detached) |
| 469 | printf("detached\n"); |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 470 | else if (wt->head_ref) |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 471 | printf("branch %s\n", wt->head_ref); |
| 472 | } |
| 473 | printf("\n"); |
| 474 | } |
| 475 | |
| 476 | static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) |
| 477 | { |
| 478 | struct strbuf sb = STRBUF_INIT; |
| 479 | int cur_path_len = strlen(wt->path); |
| 480 | int path_adj = cur_path_len - utf8_strwidth(wt->path); |
| 481 | |
| 482 | strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path); |
| 483 | if (wt->is_bare) |
| 484 | strbuf_addstr(&sb, "(bare)"); |
| 485 | else { |
| 486 | strbuf_addf(&sb, "%-*s ", abbrev_len, |
brian m. carlson | 0f05154 | 2017-10-15 22:07:08 +0000 | [diff] [blame] | 487 | find_unique_abbrev(wt->head_oid.hash, DEFAULT_ABBREV)); |
Nguyễn Thái Ngọc Duy | 96f09e2 | 2016-11-28 16:36:53 +0700 | [diff] [blame] | 488 | if (wt->is_detached) |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 489 | strbuf_addstr(&sb, "(detached HEAD)"); |
Johannes Schindelin | 2e11f58 | 2017-05-04 15:59:13 +0200 | [diff] [blame] | 490 | else if (wt->head_ref) { |
| 491 | char *ref = shorten_unambiguous_ref(wt->head_ref, 0); |
| 492 | strbuf_addf(&sb, "[%s]", ref); |
| 493 | free(ref); |
| 494 | } else |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 495 | strbuf_addstr(&sb, "(error)"); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 496 | } |
| 497 | printf("%s\n", sb.buf); |
| 498 | |
| 499 | strbuf_release(&sb); |
| 500 | } |
| 501 | |
| 502 | static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | for (i = 0; wt[i]; i++) { |
| 507 | int sha1_len; |
| 508 | int path_len = strlen(wt[i]->path); |
| 509 | |
| 510 | if (path_len > *maxlen) |
| 511 | *maxlen = path_len; |
brian m. carlson | 0f05154 | 2017-10-15 22:07:08 +0000 | [diff] [blame] | 512 | sha1_len = strlen(find_unique_abbrev(wt[i]->head_oid.hash, *abbrev)); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 513 | if (sha1_len > *abbrev) |
| 514 | *abbrev = sha1_len; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static int list(int ac, const char **av, const char *prefix) |
| 519 | { |
| 520 | int porcelain = 0; |
| 521 | |
| 522 | struct option options[] = { |
| 523 | OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")), |
| 524 | OPT_END() |
| 525 | }; |
| 526 | |
| 527 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 528 | if (ac) |
| 529 | usage_with_options(worktree_usage, options); |
| 530 | else { |
Nguyễn Thái Ngọc Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 531 | struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 532 | int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i; |
| 533 | |
| 534 | if (!porcelain) |
| 535 | measure_widths(worktrees, &abbrev, &path_maxlen); |
| 536 | |
| 537 | for (i = 0; worktrees[i]; i++) { |
| 538 | if (porcelain) |
| 539 | show_worktree_porcelain(worktrees[i]); |
| 540 | else |
| 541 | show_worktree(worktrees[i], path_maxlen, abbrev); |
| 542 | } |
| 543 | free_worktrees(worktrees); |
| 544 | } |
| 545 | return 0; |
| 546 | } |
| 547 | |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 548 | static int lock_worktree(int ac, const char **av, const char *prefix) |
| 549 | { |
| 550 | const char *reason = "", *old_reason; |
| 551 | struct option options[] = { |
| 552 | OPT_STRING(0, "reason", &reason, N_("string"), |
| 553 | N_("reason for locking")), |
| 554 | OPT_END() |
| 555 | }; |
| 556 | struct worktree **worktrees, *wt; |
| 557 | |
| 558 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 559 | if (ac != 1) |
| 560 | usage_with_options(worktree_usage, options); |
| 561 | |
Nguyễn Thái Ngọc Duy | 4fff1ef | 2016-11-28 16:36:55 +0700 | [diff] [blame] | 562 | worktrees = get_worktrees(0); |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 563 | wt = find_worktree(worktrees, prefix, av[0]); |
| 564 | if (!wt) |
| 565 | die(_("'%s' is not a working tree"), av[0]); |
| 566 | if (is_main_worktree(wt)) |
| 567 | die(_("The main working tree cannot be locked or unlocked")); |
| 568 | |
| 569 | old_reason = is_worktree_locked(wt); |
| 570 | if (old_reason) { |
| 571 | if (*old_reason) |
| 572 | die(_("'%s' is already locked, reason: %s"), |
| 573 | av[0], old_reason); |
| 574 | die(_("'%s' is already locked"), av[0]); |
| 575 | } |
| 576 | |
| 577 | write_file(git_common_path("worktrees/%s/locked", wt->id), |
| 578 | "%s", reason); |
| 579 | free_worktrees(worktrees); |
| 580 | return 0; |
| 581 | } |
| 582 | |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 583 | static int unlock_worktree(int ac, const char **av, const char *prefix) |
| 584 | { |
| 585 | struct option options[] = { |
| 586 | OPT_END() |
| 587 | }; |
| 588 | struct worktree **worktrees, *wt; |
| 589 | int ret; |
| 590 | |
| 591 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 592 | if (ac != 1) |
| 593 | usage_with_options(worktree_usage, options); |
| 594 | |
Nguyễn Thái Ngọc Duy | 4fff1ef | 2016-11-28 16:36:55 +0700 | [diff] [blame] | 595 | worktrees = get_worktrees(0); |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 596 | wt = find_worktree(worktrees, prefix, av[0]); |
| 597 | if (!wt) |
| 598 | die(_("'%s' is not a working tree"), av[0]); |
| 599 | if (is_main_worktree(wt)) |
| 600 | die(_("The main working tree cannot be locked or unlocked")); |
| 601 | if (!is_worktree_locked(wt)) |
| 602 | die(_("'%s' is not locked"), av[0]); |
| 603 | ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id)); |
| 604 | free_worktrees(worktrees); |
| 605 | return ret; |
| 606 | } |
| 607 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 608 | int cmd_worktree(int ac, const char **av, const char *prefix) |
| 609 | { |
| 610 | struct option options[] = { |
| 611 | OPT_END() |
| 612 | }; |
| 613 | |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 614 | git_config(git_worktree_config, NULL); |
Junio C Hamano | d49028e | 2016-09-26 23:49:39 -0700 | [diff] [blame] | 615 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 616 | if (ac < 2) |
| 617 | usage_with_options(worktree_usage, options); |
Nguyễn Thái Ngọc Duy | 0409e0b | 2016-05-22 16:33:56 +0700 | [diff] [blame] | 618 | if (!prefix) |
| 619 | prefix = ""; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 620 | if (!strcmp(av[1], "add")) |
| 621 | return add(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 622 | if (!strcmp(av[1], "prune")) |
| 623 | return prune(ac - 1, av + 1, prefix); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 624 | if (!strcmp(av[1], "list")) |
| 625 | return list(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 626 | if (!strcmp(av[1], "lock")) |
| 627 | 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] | 628 | if (!strcmp(av[1], "unlock")) |
| 629 | return unlock_worktree(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 630 | usage_with_options(worktree_usage, options); |
| 631 | } |