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