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; |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 30 | int quiet; |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 31 | int checkout; |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 32 | int keep_locked; |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 33 | }; |
| 34 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 35 | static int show_only; |
| 36 | static int verbose; |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 37 | static int guess_remote; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 38 | static timestamp_t expire; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 39 | |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 40 | static int git_worktree_config(const char *var, const char *value, void *cb) |
| 41 | { |
| 42 | if (!strcmp(var, "worktree.guessremote")) { |
| 43 | guess_remote = git_config_bool(var, value); |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | return git_default_config(var, value, cb); |
| 48 | } |
| 49 | |
Eric Sunshine | 602aaed | 2018-08-28 17:20:20 -0400 | [diff] [blame] | 50 | static int delete_git_dir(const char *id) |
Eric Sunshine | e5353be | 2018-08-28 17:20:19 -0400 | [diff] [blame] | 51 | { |
| 52 | struct strbuf sb = STRBUF_INIT; |
Eric Sunshine | 602aaed | 2018-08-28 17:20:20 -0400 | [diff] [blame] | 53 | int ret; |
Eric Sunshine | e5353be | 2018-08-28 17:20:19 -0400 | [diff] [blame] | 54 | |
Eric Sunshine | 602aaed | 2018-08-28 17:20:20 -0400 | [diff] [blame] | 55 | strbuf_addstr(&sb, git_common_path("worktrees/%s", id)); |
| 56 | ret = remove_dir_recursively(&sb, 0); |
| 57 | if (ret < 0 && errno == ENOTDIR) |
| 58 | ret = unlink(sb.buf); |
| 59 | if (ret) |
Eric Sunshine | e5353be | 2018-08-28 17:20:19 -0400 | [diff] [blame] | 60 | error_errno(_("failed to delete '%s'"), sb.buf); |
Eric Sunshine | e5353be | 2018-08-28 17:20:19 -0400 | [diff] [blame] | 61 | strbuf_release(&sb); |
| 62 | return ret; |
| 63 | } |
| 64 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 65 | static int prune_worktree(const char *id, struct strbuf *reason) |
| 66 | { |
| 67 | struct stat st; |
| 68 | char *path; |
Jeff King | 228740b | 2017-09-27 02:02:21 -0400 | [diff] [blame] | 69 | int fd; |
| 70 | size_t len; |
Jeff King | 8a1a8d2 | 2017-09-27 02:02:27 -0400 | [diff] [blame] | 71 | ssize_t read_result; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 72 | |
| 73 | if (!is_directory(git_path("worktrees/%s", id))) { |
| 74 | strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id); |
| 75 | return 1; |
| 76 | } |
| 77 | if (file_exists(git_path("worktrees/%s/locked", id))) |
| 78 | return 0; |
| 79 | if (stat(git_path("worktrees/%s/gitdir", id), &st)) { |
| 80 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id); |
| 81 | return 1; |
| 82 | } |
| 83 | fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY); |
| 84 | if (fd < 0) { |
| 85 | strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"), |
| 86 | id, strerror(errno)); |
| 87 | return 1; |
| 88 | } |
Jeff King | 228740b | 2017-09-27 02:02:21 -0400 | [diff] [blame] | 89 | len = xsize_t(st.st_size); |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 90 | path = xmallocz(len); |
Jeff King | 8a1a8d2 | 2017-09-27 02:02:27 -0400 | [diff] [blame] | 91 | |
| 92 | read_result = read_in_full(fd, path, len); |
| 93 | if (read_result < 0) { |
| 94 | strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"), |
| 95 | id, strerror(errno)); |
| 96 | close(fd); |
| 97 | free(path); |
| 98 | return 1; |
| 99 | } |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 100 | close(fd); |
Jeff King | 8a1a8d2 | 2017-09-27 02:02:27 -0400 | [diff] [blame] | 101 | |
| 102 | if (read_result != len) { |
| 103 | strbuf_addf(reason, |
| 104 | _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), |
| 105 | id, (uintmax_t)len, (uintmax_t)read_result); |
| 106 | free(path); |
| 107 | return 1; |
| 108 | } |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 109 | while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) |
| 110 | len--; |
| 111 | if (!len) { |
| 112 | strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id); |
| 113 | free(path); |
| 114 | return 1; |
| 115 | } |
| 116 | path[len] = '\0'; |
| 117 | if (!file_exists(path)) { |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 118 | free(path); |
Nguyễn Thái Ngọc Duy | 327864a | 2018-03-15 17:44:12 +0100 | [diff] [blame] | 119 | if (stat(git_path("worktrees/%s/index", id), &st) || |
| 120 | st.st_mtime <= expire) { |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 121 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id); |
| 122 | return 1; |
| 123 | } else { |
| 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | free(path); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static void prune_worktrees(void) |
| 132 | { |
| 133 | struct strbuf reason = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 134 | DIR *dir = opendir(git_path("worktrees")); |
| 135 | struct dirent *d; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 136 | if (!dir) |
| 137 | return; |
| 138 | while ((d = readdir(dir)) != NULL) { |
Nguyễn Thái Ngọc Duy | afb9e30 | 2016-05-22 16:33:54 +0700 | [diff] [blame] | 139 | 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] | 140 | continue; |
| 141 | strbuf_reset(&reason); |
| 142 | if (!prune_worktree(d->d_name, &reason)) |
| 143 | continue; |
| 144 | if (show_only || verbose) |
| 145 | printf("%s\n", reason.buf); |
| 146 | if (show_only) |
| 147 | continue; |
Eric Sunshine | 602aaed | 2018-08-28 17:20:20 -0400 | [diff] [blame] | 148 | delete_git_dir(d->d_name); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 149 | } |
| 150 | closedir(dir); |
| 151 | if (!show_only) |
| 152 | rmdir(git_path("worktrees")); |
| 153 | strbuf_release(&reason); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static int prune(int ac, const char **av, const char *prefix) |
| 157 | { |
| 158 | struct option options[] = { |
| 159 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
Patrick Steinhardt | 2488dca | 2017-02-06 14:13:59 +0100 | [diff] [blame] | 160 | 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] | 161 | OPT_EXPIRY_DATE(0, "expire", &expire, |
Patrick Steinhardt | 2488dca | 2017-02-06 14:13:59 +0100 | [diff] [blame] | 162 | N_("expire working trees older than <time>")), |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 163 | OPT_END() |
| 164 | }; |
| 165 | |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 166 | expire = TIME_MAX; |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 167 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 168 | if (ac) |
| 169 | usage_with_options(worktree_usage, options); |
| 170 | prune_worktrees(); |
| 171 | return 0; |
| 172 | } |
| 173 | |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 174 | static char *junk_work_tree; |
| 175 | static char *junk_git_dir; |
| 176 | static int is_junk; |
| 177 | static pid_t junk_pid; |
| 178 | |
| 179 | static void remove_junk(void) |
| 180 | { |
| 181 | struct strbuf sb = STRBUF_INIT; |
| 182 | if (!is_junk || getpid() != junk_pid) |
| 183 | return; |
| 184 | if (junk_git_dir) { |
| 185 | strbuf_addstr(&sb, junk_git_dir); |
| 186 | remove_dir_recursively(&sb, 0); |
| 187 | strbuf_reset(&sb); |
| 188 | } |
| 189 | if (junk_work_tree) { |
| 190 | strbuf_addstr(&sb, junk_work_tree); |
| 191 | remove_dir_recursively(&sb, 0); |
| 192 | } |
| 193 | strbuf_release(&sb); |
| 194 | } |
| 195 | |
| 196 | static void remove_junk_on_signal(int signo) |
| 197 | { |
| 198 | remove_junk(); |
| 199 | sigchain_pop(signo); |
| 200 | raise(signo); |
| 201 | } |
| 202 | |
Eric Sunshine | f5682b2 | 2015-07-06 13:30:57 -0400 | [diff] [blame] | 203 | static const char *worktree_basename(const char *path, int *olen) |
| 204 | { |
| 205 | const char *name; |
| 206 | int len; |
| 207 | |
| 208 | len = strlen(path); |
| 209 | while (len && is_dir_sep(path[len - 1])) |
| 210 | len--; |
| 211 | |
| 212 | for (name = path + len - 1; name > path; name--) |
| 213 | if (is_dir_sep(*name)) { |
| 214 | name++; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | *olen = len; |
| 219 | return name; |
| 220 | } |
| 221 | |
Eric Sunshine | 45059e6 | 2018-08-28 17:20:21 -0400 | [diff] [blame] | 222 | static void validate_worktree_add(const char *path, const struct add_opts *opts) |
| 223 | { |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 224 | struct worktree **worktrees; |
| 225 | struct worktree *wt; |
| 226 | int locked; |
| 227 | |
Eric Sunshine | 45059e6 | 2018-08-28 17:20:21 -0400 | [diff] [blame] | 228 | if (file_exists(path) && !is_empty_dir(path)) |
| 229 | die(_("'%s' already exists"), path); |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 230 | |
| 231 | worktrees = get_worktrees(0); |
| 232 | /* |
| 233 | * find_worktree()'s suffix matching may undesirably find the main |
| 234 | * rather than a linked worktree (for instance, when the basenames |
| 235 | * of the main worktree and the one being created are the same). |
| 236 | * We're only interested in linked worktrees, so skip the main |
| 237 | * worktree with +1. |
| 238 | */ |
| 239 | wt = find_worktree(worktrees + 1, NULL, path); |
| 240 | if (!wt) |
| 241 | goto done; |
| 242 | |
| 243 | locked = !!is_worktree_locked(wt); |
Eric Sunshine | e19831c | 2018-08-28 17:20:23 -0400 | [diff] [blame] | 244 | if ((!locked && opts->force) || (locked && opts->force > 1)) { |
| 245 | if (delete_git_dir(wt->id)) |
| 246 | die(_("unable to re-add worktree '%s'"), path); |
| 247 | goto done; |
| 248 | } |
| 249 | |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 250 | if (locked) |
Eric Sunshine | e19831c | 2018-08-28 17:20:23 -0400 | [diff] [blame] | 251 | die(_("'%s' is a missing but locked worktree;\nuse 'add -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path); |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 252 | else |
Eric Sunshine | e19831c | 2018-08-28 17:20:23 -0400 | [diff] [blame] | 253 | die(_("'%s' is a missing but already registered worktree;\nuse 'add -f' to override, or 'prune' or 'remove' to clear"), path); |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 254 | |
| 255 | done: |
| 256 | free_worktrees(worktrees); |
Eric Sunshine | 45059e6 | 2018-08-28 17:20:21 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Eric Sunshine | 80a0548 | 2015-07-17 19:00:12 -0400 | [diff] [blame] | 259 | static int add_worktree(const char *path, const char *refname, |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 260 | const struct add_opts *opts) |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 261 | { |
| 262 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; |
| 263 | struct strbuf sb = STRBUF_INIT; |
| 264 | const char *name; |
| 265 | struct stat st; |
René Scharfe | 542aa25 | 2016-08-05 22:38:44 +0200 | [diff] [blame] | 266 | struct child_process cp = CHILD_PROCESS_INIT; |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 267 | struct argv_array child_env = ARGV_ARRAY_INIT; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 268 | int counter = 0, len, ret; |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 269 | struct strbuf symref = STRBUF_INIT; |
| 270 | struct commit *commit = NULL; |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 271 | int is_branch = 0; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 272 | |
Eric Sunshine | 45059e6 | 2018-08-28 17:20:21 -0400 | [diff] [blame] | 273 | validate_worktree_add(path, opts); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 274 | |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 275 | /* is 'refname' a branch or commit? */ |
Nguyễn Thái Ngọc Duy | 0ebf4a2 | 2016-02-15 20:35:32 +0700 | [diff] [blame] | 276 | if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 277 | ref_exists(symref.buf)) { |
| 278 | is_branch = 1; |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 279 | if (!opts->force) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 280 | die_if_checked_out(symref.buf, 0); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 281 | } |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 282 | commit = lookup_commit_reference_by_name(refname); |
| 283 | if (!commit) |
| 284 | die(_("invalid reference: %s"), refname); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 285 | |
Eric Sunshine | f5682b2 | 2015-07-06 13:30:57 -0400 | [diff] [blame] | 286 | name = worktree_basename(path, &len); |
Jeff King | 8c2ca3a | 2017-04-20 17:09:30 -0400 | [diff] [blame] | 287 | git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 288 | len = sb_repo.len; |
| 289 | if (safe_create_leading_directories_const(sb_repo.buf)) |
| 290 | die_errno(_("could not create leading directories of '%s'"), |
| 291 | sb_repo.buf); |
| 292 | while (!stat(sb_repo.buf, &st)) { |
| 293 | counter++; |
| 294 | strbuf_setlen(&sb_repo, len); |
| 295 | strbuf_addf(&sb_repo, "%d", counter); |
| 296 | } |
| 297 | name = strrchr(sb_repo.buf, '/') + 1; |
| 298 | |
| 299 | junk_pid = getpid(); |
| 300 | atexit(remove_junk); |
| 301 | sigchain_push_common(remove_junk_on_signal); |
| 302 | |
| 303 | if (mkdir(sb_repo.buf, 0777)) |
| 304 | die_errno(_("could not create directory of '%s'"), sb_repo.buf); |
| 305 | junk_git_dir = xstrdup(sb_repo.buf); |
| 306 | is_junk = 1; |
| 307 | |
| 308 | /* |
| 309 | * lock the incomplete repo so prune won't delete it, unlock |
| 310 | * after the preparation is over. |
| 311 | */ |
| 312 | 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] | 313 | if (!opts->keep_locked) |
| 314 | write_file(sb.buf, "initializing"); |
| 315 | else |
| 316 | write_file(sb.buf, "added with --lock"); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 317 | |
| 318 | strbuf_addf(&sb_git, "%s/.git", path); |
| 319 | if (safe_create_leading_directories_const(sb_git.buf)) |
| 320 | die_errno(_("could not create leading directories of '%s'"), |
| 321 | sb_git.buf); |
| 322 | junk_work_tree = xstrdup(path); |
| 323 | |
| 324 | strbuf_reset(&sb); |
| 325 | strbuf_addf(&sb, "%s/gitdir", sb_repo.buf); |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 326 | write_file(sb.buf, "%s", real_path(sb_git.buf)); |
| 327 | write_file(sb_git.buf, "gitdir: %s/worktrees/%s", |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 328 | real_path(get_git_common_dir()), name); |
| 329 | /* |
| 330 | * This is to keep resolve_ref() happy. We need a valid HEAD |
Eric Sunshine | ed197a6 | 2015-07-17 19:00:15 -0400 | [diff] [blame] | 331 | * or is_git_directory() will reject the directory. Any value which |
| 332 | * looks like an object ID will do since it will be immediately |
| 333 | * replaced by the symbolic-ref or update-ref invocation in the new |
| 334 | * worktree. |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 335 | */ |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 336 | strbuf_reset(&sb); |
| 337 | strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); |
Jeff King | dabd35f | 2016-07-08 06:35:15 -0400 | [diff] [blame] | 338 | write_file(sb.buf, "%s", sha1_to_hex(null_sha1)); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 339 | strbuf_reset(&sb); |
| 340 | strbuf_addf(&sb, "%s/commondir", sb_repo.buf); |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 341 | write_file(sb.buf, "../.."); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 342 | |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 343 | argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); |
| 344 | argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 345 | cp.git_cmd = 1; |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 346 | |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 347 | if (!is_branch) |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 348 | argv_array_pushl(&cp.args, "update-ref", "HEAD", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 349 | oid_to_hex(&commit->object.oid), NULL); |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 350 | else { |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 351 | argv_array_pushl(&cp.args, "symbolic-ref", "HEAD", |
| 352 | symref.buf, NULL); |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 353 | if (opts->quiet) |
| 354 | argv_array_push(&cp.args, "--quiet"); |
| 355 | } |
| 356 | |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 357 | cp.env = child_env.argv; |
| 358 | ret = run_command(&cp); |
| 359 | if (ret) |
| 360 | goto done; |
| 361 | |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 362 | if (opts->checkout) { |
| 363 | cp.argv = NULL; |
| 364 | argv_array_clear(&cp.args); |
| 365 | argv_array_pushl(&cp.args, "reset", "--hard", NULL); |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 366 | if (opts->quiet) |
| 367 | argv_array_push(&cp.args, "--quiet"); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 368 | cp.env = child_env.argv; |
| 369 | ret = run_command(&cp); |
| 370 | if (ret) |
| 371 | goto done; |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 372 | } |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 373 | |
| 374 | is_junk = 0; |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 375 | FREE_AND_NULL(junk_work_tree); |
| 376 | FREE_AND_NULL(junk_git_dir); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 377 | |
Eric Sunshine | 7f44e3d | 2015-07-17 19:00:14 -0400 | [diff] [blame] | 378 | done: |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 379 | if (ret || !opts->keep_locked) { |
| 380 | strbuf_reset(&sb); |
| 381 | strbuf_addf(&sb, "%s/locked", sb_repo.buf); |
| 382 | unlink_or_warn(sb.buf); |
| 383 | } |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Hook failure does not warrant worktree deletion, so run hook after |
| 387 | * is_junk is cleared, but do return appropriate code when hook fails. |
| 388 | */ |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 389 | if (!ret && opts->checkout) { |
| 390 | const char *hook = find_hook("post-checkout"); |
| 391 | if (hook) { |
| 392 | const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL }; |
| 393 | cp.git_cmd = 0; |
| 394 | cp.no_stdin = 1; |
| 395 | cp.stdout_to_stderr = 1; |
| 396 | cp.dir = path; |
| 397 | cp.env = env; |
| 398 | cp.argv = NULL; |
| 399 | argv_array_pushl(&cp.args, absolute_path(hook), |
| 400 | oid_to_hex(&null_oid), |
| 401 | oid_to_hex(&commit->object.oid), |
| 402 | "1", NULL); |
| 403 | ret = run_command(&cp); |
| 404 | } |
| 405 | } |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 406 | |
Eric Sunshine | ae2a382 | 2015-07-17 19:00:11 -0400 | [diff] [blame] | 407 | argv_array_clear(&child_env); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 408 | strbuf_release(&sb); |
Eric Sunshine | f7c9dac | 2015-07-17 19:00:13 -0400 | [diff] [blame] | 409 | strbuf_release(&symref); |
Eric Sunshine | b979d95 | 2015-07-06 13:30:55 -0400 | [diff] [blame] | 410 | strbuf_release(&sb_repo); |
| 411 | strbuf_release(&sb_git); |
| 412 | return ret; |
| 413 | } |
| 414 | |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 415 | static void print_preparing_worktree_line(int detach, |
| 416 | const char *branch, |
| 417 | const char *new_branch, |
| 418 | int force_new_branch) |
| 419 | { |
| 420 | if (force_new_branch) { |
| 421 | struct commit *commit = lookup_commit_reference_by_name(new_branch); |
| 422 | if (!commit) |
| 423 | printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); |
| 424 | else |
| 425 | printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"), |
| 426 | new_branch, |
Junio C Hamano | 10174da | 2018-05-23 14:38:18 +0900 | [diff] [blame] | 427 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 428 | } else if (new_branch) { |
| 429 | printf_ln(_("Preparing worktree (new branch '%s')"), new_branch); |
| 430 | } else { |
| 431 | struct strbuf s = STRBUF_INIT; |
| 432 | if (!detach && !strbuf_check_branch_ref(&s, branch) && |
| 433 | ref_exists(s.buf)) |
| 434 | printf_ln(_("Preparing worktree (checking out '%s')"), |
| 435 | branch); |
| 436 | else { |
| 437 | struct commit *commit = lookup_commit_reference_by_name(branch); |
| 438 | if (!commit) |
| 439 | die(_("invalid reference: %s"), branch); |
| 440 | printf_ln(_("Preparing worktree (detached HEAD %s)"), |
Junio C Hamano | 10174da | 2018-05-23 14:38:18 +0900 | [diff] [blame] | 441 | find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 442 | } |
| 443 | strbuf_release(&s); |
| 444 | } |
| 445 | } |
| 446 | |
Thomas Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 447 | static const char *dwim_branch(const char *path, const char **new_branch) |
| 448 | { |
| 449 | int n; |
| 450 | const char *s = worktree_basename(path, &n); |
Thomas Gummerer | f60a7b7 | 2018-04-24 22:56:35 +0100 | [diff] [blame] | 451 | const char *branchname = xstrndup(s, n); |
| 452 | struct strbuf ref = STRBUF_INIT; |
| 453 | |
| 454 | UNLEAK(branchname); |
| 455 | if (!strbuf_check_branch_ref(&ref, branchname) && |
| 456 | ref_exists(ref.buf)) { |
| 457 | strbuf_release(&ref); |
| 458 | return branchname; |
| 459 | } |
| 460 | |
| 461 | *new_branch = branchname; |
Thomas Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 462 | if (guess_remote) { |
| 463 | struct object_id oid; |
| 464 | const char *remote = |
Ævar Arnfjörð Bjarmason | 3c87aa9 | 2018-06-05 14:40:46 +0000 | [diff] [blame] | 465 | unique_tracking_name(*new_branch, &oid, NULL); |
Thomas Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 466 | return remote; |
| 467 | } |
| 468 | return NULL; |
| 469 | } |
| 470 | |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 471 | static int add(int ac, const char **av, const char *prefix) |
| 472 | { |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 473 | struct add_opts opts; |
| 474 | const char *new_branch_force = NULL; |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 475 | char *path; |
| 476 | const char *branch; |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 477 | const char *new_branch = NULL; |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 478 | const char *opt_track = NULL; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 479 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | 1224781 | 2018-02-09 18:01:42 +0700 | [diff] [blame] | 480 | OPT__FORCE(&opts.force, |
| 481 | 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] | 482 | PARSE_OPT_NOCOMPLETE), |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 483 | OPT_STRING('b', NULL, &new_branch, N_("branch"), |
Eric Sunshine | cbdf60f | 2015-07-06 13:30:53 -0400 | [diff] [blame] | 484 | N_("create a new branch")), |
| 485 | OPT_STRING('B', NULL, &new_branch_force, N_("branch"), |
| 486 | N_("create or reset a branch")), |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 487 | OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")), |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 488 | 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] | 489 | OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")), |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 490 | OPT__QUIET(&opts.quiet, N_("suppress progress reporting")), |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 491 | OPT_PASSTHRU(0, "track", &opt_track, NULL, |
| 492 | N_("set up tracking mode (see git-branch(1))"), |
| 493 | PARSE_OPT_NOARG | PARSE_OPT_OPTARG), |
Thomas Gummerer | 71d6682 | 2017-11-29 20:04:50 +0000 | [diff] [blame] | 494 | OPT_BOOL(0, "guess-remote", &guess_remote, |
| 495 | 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] | 496 | OPT_END() |
| 497 | }; |
| 498 | |
Eric Sunshine | 5dd6e23 | 2015-07-17 19:00:07 -0400 | [diff] [blame] | 499 | memset(&opts, 0, sizeof(opts)); |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 500 | opts.checkout = 1; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 501 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 502 | if (!!opts.detach + !!new_branch + !!new_branch_force > 1) |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 503 | die(_("-b, -B, and --detach are mutually exclusive")); |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 504 | if (ac < 1 || ac > 2) |
| 505 | usage_with_options(worktree_usage, options); |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 506 | |
Jeff King | 116fb64 | 2017-03-20 21:22:28 -0400 | [diff] [blame] | 507 | path = prefix_filename(prefix, av[0]); |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 508 | branch = ac < 2 ? "HEAD" : av[1]; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 509 | |
Jordan DE GEA | 1a450e2 | 2016-05-27 15:17:08 +0200 | [diff] [blame] | 510 | if (!strcmp(branch, "-")) |
| 511 | branch = "@{-1}"; |
| 512 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 513 | if (new_branch_force) { |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 514 | struct strbuf symref = STRBUF_INIT; |
| 515 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 516 | new_branch = new_branch_force; |
Eric Sunshine | eef005d | 2015-07-17 19:00:06 -0400 | [diff] [blame] | 517 | |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 518 | if (!opts.force && |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 519 | !strbuf_check_branch_ref(&symref, new_branch) && |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 520 | ref_exists(symref.buf)) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 521 | die_if_checked_out(symref.buf, 0); |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 522 | strbuf_release(&symref); |
| 523 | } |
| 524 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 525 | if (ac < 2 && !new_branch && !opts.detach) { |
Thomas Gummerer | 6427f87 | 2018-04-24 22:56:34 +0100 | [diff] [blame] | 526 | const char *s = dwim_branch(path, &new_branch); |
| 527 | if (s) |
| 528 | branch = s; |
Eric Sunshine | 1eb07d8 | 2015-07-06 13:30:59 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 531 | if (ac == 2 && !new_branch && !opts.detach) { |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 532 | struct object_id oid; |
| 533 | struct commit *commit; |
| 534 | const char *remote; |
| 535 | |
| 536 | commit = lookup_commit_reference_by_name(branch); |
| 537 | if (!commit) { |
Ævar Arnfjörð Bjarmason | 3c87aa9 | 2018-06-05 14:40:46 +0000 | [diff] [blame] | 538 | remote = unique_tracking_name(branch, &oid, NULL); |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 539 | if (remote) { |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 540 | new_branch = branch; |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 541 | branch = remote; |
| 542 | } |
| 543 | } |
| 544 | } |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 545 | if (!opts.quiet) |
| 546 | print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force); |
Thomas Gummerer | 2c27002 | 2018-04-24 22:56:33 +0100 | [diff] [blame] | 547 | |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 548 | if (new_branch) { |
René Scharfe | 542aa25 | 2016-08-05 22:38:44 +0200 | [diff] [blame] | 549 | struct child_process cp = CHILD_PROCESS_INIT; |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 550 | cp.git_cmd = 1; |
| 551 | argv_array_push(&cp.args, "branch"); |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 552 | if (new_branch_force) |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 553 | argv_array_push(&cp.args, "--force"); |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 554 | if (opts.quiet) |
| 555 | argv_array_push(&cp.args, "--quiet"); |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 556 | argv_array_push(&cp.args, new_branch); |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 557 | argv_array_push(&cp.args, branch); |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 558 | if (opt_track) |
| 559 | argv_array_push(&cp.args, opt_track); |
Eric Sunshine | c284243 | 2015-07-17 19:00:10 -0400 | [diff] [blame] | 560 | if (run_command(&cp)) |
| 561 | return -1; |
Thomas Gummerer | d861d34 | 2018-04-24 22:56:32 +0100 | [diff] [blame] | 562 | branch = new_branch; |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 563 | } else if (opt_track) { |
| 564 | 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] | 565 | } |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 566 | |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 567 | UNLEAK(path); |
| 568 | UNLEAK(opts); |
Eric Sunshine | 80a0548 | 2015-07-17 19:00:12 -0400 | [diff] [blame] | 569 | return add_worktree(path, branch, &opts); |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 572 | static void show_worktree_porcelain(struct worktree *wt) |
| 573 | { |
| 574 | printf("worktree %s\n", wt->path); |
| 575 | if (wt->is_bare) |
| 576 | printf("bare\n"); |
| 577 | else { |
brian m. carlson | 0f05154 | 2017-10-15 22:07:08 +0000 | [diff] [blame] | 578 | printf("HEAD %s\n", oid_to_hex(&wt->head_oid)); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 579 | if (wt->is_detached) |
| 580 | printf("detached\n"); |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 581 | else if (wt->head_ref) |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 582 | printf("branch %s\n", wt->head_ref); |
| 583 | } |
| 584 | printf("\n"); |
| 585 | } |
| 586 | |
| 587 | static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) |
| 588 | { |
| 589 | struct strbuf sb = STRBUF_INIT; |
| 590 | int cur_path_len = strlen(wt->path); |
| 591 | int path_adj = cur_path_len - utf8_strwidth(wt->path); |
| 592 | |
| 593 | strbuf_addf(&sb, "%-*s ", 1 + path_maxlen + path_adj, wt->path); |
| 594 | if (wt->is_bare) |
| 595 | strbuf_addstr(&sb, "(bare)"); |
| 596 | else { |
| 597 | strbuf_addf(&sb, "%-*s ", abbrev_len, |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 598 | 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] | 599 | if (wt->is_detached) |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 600 | strbuf_addstr(&sb, "(detached HEAD)"); |
Johannes Schindelin | 2e11f58 | 2017-05-04 15:59:13 +0200 | [diff] [blame] | 601 | else if (wt->head_ref) { |
| 602 | char *ref = shorten_unambiguous_ref(wt->head_ref, 0); |
| 603 | strbuf_addf(&sb, "[%s]", ref); |
| 604 | free(ref); |
| 605 | } else |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 606 | strbuf_addstr(&sb, "(error)"); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 607 | } |
| 608 | printf("%s\n", sb.buf); |
| 609 | |
| 610 | strbuf_release(&sb); |
| 611 | } |
| 612 | |
| 613 | static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen) |
| 614 | { |
| 615 | int i; |
| 616 | |
| 617 | for (i = 0; wt[i]; i++) { |
| 618 | int sha1_len; |
| 619 | int path_len = strlen(wt[i]->path); |
| 620 | |
| 621 | if (path_len > *maxlen) |
| 622 | *maxlen = path_len; |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 623 | sha1_len = strlen(find_unique_abbrev(&wt[i]->head_oid, *abbrev)); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 624 | if (sha1_len > *abbrev) |
| 625 | *abbrev = sha1_len; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static int list(int ac, const char **av, const char *prefix) |
| 630 | { |
| 631 | int porcelain = 0; |
| 632 | |
| 633 | struct option options[] = { |
| 634 | OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")), |
| 635 | OPT_END() |
| 636 | }; |
| 637 | |
| 638 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 639 | if (ac) |
| 640 | usage_with_options(worktree_usage, options); |
| 641 | else { |
Nguyễn Thái Ngọc Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 642 | struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 643 | int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i; |
| 644 | |
| 645 | if (!porcelain) |
| 646 | measure_widths(worktrees, &abbrev, &path_maxlen); |
| 647 | |
| 648 | for (i = 0; worktrees[i]; i++) { |
| 649 | if (porcelain) |
| 650 | show_worktree_porcelain(worktrees[i]); |
| 651 | else |
| 652 | show_worktree(worktrees[i], path_maxlen, abbrev); |
| 653 | } |
| 654 | free_worktrees(worktrees); |
| 655 | } |
| 656 | return 0; |
| 657 | } |
| 658 | |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 659 | static int lock_worktree(int ac, const char **av, const char *prefix) |
| 660 | { |
| 661 | const char *reason = "", *old_reason; |
| 662 | struct option options[] = { |
| 663 | OPT_STRING(0, "reason", &reason, N_("string"), |
| 664 | N_("reason for locking")), |
| 665 | OPT_END() |
| 666 | }; |
| 667 | struct worktree **worktrees, *wt; |
| 668 | |
| 669 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 670 | if (ac != 1) |
| 671 | usage_with_options(worktree_usage, options); |
| 672 | |
Nguyễn Thái Ngọc Duy | 4fff1ef | 2016-11-28 16:36:55 +0700 | [diff] [blame] | 673 | worktrees = get_worktrees(0); |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 674 | wt = find_worktree(worktrees, prefix, av[0]); |
| 675 | if (!wt) |
| 676 | die(_("'%s' is not a working tree"), av[0]); |
| 677 | if (is_main_worktree(wt)) |
| 678 | die(_("The main working tree cannot be locked or unlocked")); |
| 679 | |
| 680 | old_reason = is_worktree_locked(wt); |
| 681 | if (old_reason) { |
| 682 | if (*old_reason) |
| 683 | die(_("'%s' is already locked, reason: %s"), |
| 684 | av[0], old_reason); |
| 685 | die(_("'%s' is already locked"), av[0]); |
| 686 | } |
| 687 | |
| 688 | write_file(git_common_path("worktrees/%s/locked", wt->id), |
| 689 | "%s", reason); |
| 690 | free_worktrees(worktrees); |
| 691 | return 0; |
| 692 | } |
| 693 | |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 694 | static int unlock_worktree(int ac, const char **av, const char *prefix) |
| 695 | { |
| 696 | struct option options[] = { |
| 697 | OPT_END() |
| 698 | }; |
| 699 | struct worktree **worktrees, *wt; |
| 700 | int ret; |
| 701 | |
| 702 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 703 | if (ac != 1) |
| 704 | usage_with_options(worktree_usage, options); |
| 705 | |
Nguyễn Thái Ngọc Duy | 4fff1ef | 2016-11-28 16:36:55 +0700 | [diff] [blame] | 706 | worktrees = get_worktrees(0); |
Nguyễn Thái Ngọc Duy | 6d30862 | 2016-06-13 19:18:25 +0700 | [diff] [blame] | 707 | wt = find_worktree(worktrees, prefix, av[0]); |
| 708 | if (!wt) |
| 709 | die(_("'%s' is not a working tree"), av[0]); |
| 710 | if (is_main_worktree(wt)) |
| 711 | die(_("The main working tree cannot be locked or unlocked")); |
| 712 | if (!is_worktree_locked(wt)) |
| 713 | die(_("'%s' is not locked"), av[0]); |
| 714 | ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id)); |
| 715 | free_worktrees(worktrees); |
| 716 | return ret; |
| 717 | } |
| 718 | |
Nguyễn Thái Ngọc Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 719 | static void validate_no_submodules(const struct worktree *wt) |
| 720 | { |
| 721 | struct index_state istate = { NULL }; |
| 722 | int i, found_submodules = 0; |
| 723 | |
Junio C Hamano | bd0f794 | 2018-03-14 12:01:05 -0700 | [diff] [blame] | 724 | if (read_index_from(&istate, worktree_git_path(wt, "index"), |
| 725 | get_worktree_git_dir(wt)) > 0) { |
Nguyễn Thái Ngọc Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 726 | for (i = 0; i < istate.cache_nr; i++) { |
| 727 | struct cache_entry *ce = istate.cache[i]; |
| 728 | |
| 729 | if (S_ISGITLINK(ce->ce_mode)) { |
| 730 | found_submodules = 1; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | discard_index(&istate); |
| 736 | |
| 737 | if (found_submodules) |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 738 | 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] | 739 | } |
| 740 | |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 741 | static int move_worktree(int ac, const char **av, const char *prefix) |
| 742 | { |
Eric Sunshine | 68a6b3a | 2018-08-28 17:20:24 -0400 | [diff] [blame^] | 743 | int force = 0; |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 744 | struct option options[] = { |
Eric Sunshine | 68a6b3a | 2018-08-28 17:20:24 -0400 | [diff] [blame^] | 745 | OPT__FORCE(&force, |
| 746 | N_("force move even if worktree is dirty or locked"), |
| 747 | PARSE_OPT_NOCOMPLETE), |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 748 | OPT_END() |
| 749 | }; |
| 750 | struct worktree **worktrees, *wt; |
| 751 | struct strbuf dst = STRBUF_INIT; |
| 752 | struct strbuf errmsg = STRBUF_INIT; |
Eric Sunshine | 68a6b3a | 2018-08-28 17:20:24 -0400 | [diff] [blame^] | 753 | const char *reason = NULL; |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 754 | char *path; |
| 755 | |
| 756 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 757 | if (ac != 2) |
| 758 | usage_with_options(worktree_usage, options); |
| 759 | |
| 760 | path = prefix_filename(prefix, av[1]); |
| 761 | strbuf_addstr(&dst, path); |
| 762 | free(path); |
| 763 | |
| 764 | worktrees = get_worktrees(0); |
| 765 | wt = find_worktree(worktrees, prefix, av[0]); |
| 766 | if (!wt) |
| 767 | die(_("'%s' is not a working tree"), av[0]); |
| 768 | if (is_main_worktree(wt)) |
| 769 | 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] | 770 | if (is_directory(dst.buf)) { |
| 771 | const char *sep = find_last_dir_sep(wt->path); |
| 772 | |
| 773 | if (!sep) |
| 774 | die(_("could not figure out destination name from '%s'"), |
| 775 | wt->path); |
| 776 | strbuf_trim_trailing_dir_sep(&dst); |
| 777 | strbuf_addstr(&dst, sep); |
| 778 | } |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 779 | if (file_exists(dst.buf)) |
Nguyễn Thái Ngọc Duy | c64a8d2 | 2018-02-12 16:49:37 +0700 | [diff] [blame] | 780 | die(_("target '%s' already exists"), dst.buf); |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 781 | |
Nguyễn Thái Ngọc Duy | 78d986b | 2018-02-12 16:49:38 +0700 | [diff] [blame] | 782 | validate_no_submodules(wt); |
| 783 | |
Eric Sunshine | 68a6b3a | 2018-08-28 17:20:24 -0400 | [diff] [blame^] | 784 | if (force < 2) |
| 785 | reason = is_worktree_locked(wt); |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 786 | if (reason) { |
| 787 | if (*reason) |
Eric Sunshine | 68a6b3a | 2018-08-28 17:20:24 -0400 | [diff] [blame^] | 788 | die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"), |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 789 | reason); |
Eric Sunshine | 68a6b3a | 2018-08-28 17:20:24 -0400 | [diff] [blame^] | 790 | die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first")); |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 791 | } |
Nguyễn Thái Ngọc Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 792 | if (validate_worktree(wt, &errmsg, 0)) |
Nguyễn Thái Ngọc Duy | 9f792bb | 2018-02-12 16:49:36 +0700 | [diff] [blame] | 793 | die(_("validation failed, cannot move working tree: %s"), |
| 794 | errmsg.buf); |
| 795 | strbuf_release(&errmsg); |
| 796 | |
| 797 | if (rename(wt->path, dst.buf) == -1) |
| 798 | die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf); |
| 799 | |
| 800 | update_worktree_location(wt, dst.buf); |
| 801 | |
| 802 | strbuf_release(&dst); |
| 803 | free_worktrees(worktrees); |
| 804 | return 0; |
| 805 | } |
| 806 | |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 807 | /* |
| 808 | * Note, "git status --porcelain" is used to determine if it's safe to |
| 809 | * delete a whole worktree. "git status" does not ignore user |
| 810 | * configuration, so if a normal "git status" shows "clean" for the |
| 811 | * user, then it's ok to remove it. |
| 812 | * |
| 813 | * This assumption may be a bad one. We may want to ignore |
| 814 | * (potentially bad) user settings and only delete a worktree when |
| 815 | * it's absolutely safe to do so from _our_ point of view because we |
| 816 | * know better. |
| 817 | */ |
| 818 | static void check_clean_worktree(struct worktree *wt, |
| 819 | const char *original_path) |
| 820 | { |
| 821 | struct argv_array child_env = ARGV_ARRAY_INIT; |
| 822 | struct child_process cp; |
| 823 | char buf[1]; |
| 824 | int ret; |
| 825 | |
| 826 | /* |
| 827 | * Until we sort this out, all submodules are "dirty" and |
| 828 | * will abort this function. |
| 829 | */ |
| 830 | validate_no_submodules(wt); |
| 831 | |
| 832 | argv_array_pushf(&child_env, "%s=%s/.git", |
| 833 | GIT_DIR_ENVIRONMENT, wt->path); |
| 834 | argv_array_pushf(&child_env, "%s=%s", |
| 835 | GIT_WORK_TREE_ENVIRONMENT, wt->path); |
| 836 | memset(&cp, 0, sizeof(cp)); |
| 837 | argv_array_pushl(&cp.args, "status", |
| 838 | "--porcelain", "--ignore-submodules=none", |
| 839 | NULL); |
| 840 | cp.env = child_env.argv; |
| 841 | cp.git_cmd = 1; |
| 842 | cp.dir = wt->path; |
| 843 | cp.out = -1; |
| 844 | ret = start_command(&cp); |
| 845 | if (ret) |
| 846 | die_errno(_("failed to run 'git status' on '%s'"), |
| 847 | original_path); |
| 848 | ret = xread(cp.out, buf, sizeof(buf)); |
| 849 | if (ret) |
| 850 | die(_("'%s' is dirty, use --force to delete it"), |
| 851 | original_path); |
| 852 | close(cp.out); |
| 853 | ret = finish_command(&cp); |
| 854 | if (ret) |
| 855 | die_errno(_("failed to run 'git status' on '%s', code %d"), |
| 856 | original_path, ret); |
| 857 | } |
| 858 | |
| 859 | static int delete_git_work_tree(struct worktree *wt) |
| 860 | { |
| 861 | struct strbuf sb = STRBUF_INIT; |
| 862 | int ret = 0; |
| 863 | |
| 864 | strbuf_addstr(&sb, wt->path); |
| 865 | if (remove_dir_recursively(&sb, 0)) { |
| 866 | error_errno(_("failed to delete '%s'"), sb.buf); |
| 867 | ret = -1; |
| 868 | } |
| 869 | strbuf_release(&sb); |
| 870 | return ret; |
| 871 | } |
| 872 | |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 873 | static int remove_worktree(int ac, const char **av, const char *prefix) |
| 874 | { |
| 875 | int force = 0; |
| 876 | struct option options[] = { |
Stefan Beller | d228eea | 2018-04-17 11:19:39 -0700 | [diff] [blame] | 877 | OPT__FORCE(&force, |
| 878 | N_("force removing even if the worktree is dirty"), |
| 879 | PARSE_OPT_NOCOMPLETE), |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 880 | OPT_END() |
| 881 | }; |
| 882 | struct worktree **worktrees, *wt; |
| 883 | struct strbuf errmsg = STRBUF_INIT; |
| 884 | const char *reason; |
| 885 | int ret = 0; |
| 886 | |
| 887 | ac = parse_options(ac, av, prefix, options, worktree_usage, 0); |
| 888 | if (ac != 1) |
| 889 | usage_with_options(worktree_usage, options); |
| 890 | |
| 891 | worktrees = get_worktrees(0); |
| 892 | wt = find_worktree(worktrees, prefix, av[0]); |
| 893 | if (!wt) |
| 894 | die(_("'%s' is not a working tree"), av[0]); |
| 895 | if (is_main_worktree(wt)) |
| 896 | die(_("'%s' is a main working tree"), av[0]); |
| 897 | reason = is_worktree_locked(wt); |
| 898 | if (reason) { |
| 899 | if (*reason) |
| 900 | die(_("cannot remove a locked working tree, lock reason: %s"), |
| 901 | reason); |
| 902 | die(_("cannot remove a locked working tree")); |
| 903 | } |
Nguyễn Thái Ngọc Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 904 | 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] | 905 | die(_("validation failed, cannot remove working tree: %s"), |
| 906 | errmsg.buf); |
| 907 | strbuf_release(&errmsg); |
| 908 | |
Nguyễn Thái Ngọc Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 909 | if (file_exists(wt->path)) { |
| 910 | if (!force) |
| 911 | check_clean_worktree(wt, av[0]); |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 912 | |
Nguyễn Thái Ngọc Duy | ee6763a | 2018-02-12 16:49:40 +0700 | [diff] [blame] | 913 | ret |= delete_git_work_tree(wt); |
| 914 | } |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 915 | /* |
| 916 | * continue on even if ret is non-zero, there's no going back |
| 917 | * from here. |
| 918 | */ |
Eric Sunshine | 602aaed | 2018-08-28 17:20:20 -0400 | [diff] [blame] | 919 | ret |= delete_git_dir(wt->id); |
Nguyễn Thái Ngọc Duy | cc73385 | 2018-02-12 16:49:39 +0700 | [diff] [blame] | 920 | |
| 921 | free_worktrees(worktrees); |
| 922 | return ret; |
| 923 | } |
| 924 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 925 | int cmd_worktree(int ac, const char **av, const char *prefix) |
| 926 | { |
| 927 | struct option options[] = { |
| 928 | OPT_END() |
| 929 | }; |
| 930 | |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 931 | git_config(git_worktree_config, NULL); |
Junio C Hamano | d49028e | 2016-09-26 23:49:39 -0700 | [diff] [blame] | 932 | |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 933 | if (ac < 2) |
| 934 | usage_with_options(worktree_usage, options); |
Nguyễn Thái Ngọc Duy | 0409e0b | 2016-05-22 16:33:56 +0700 | [diff] [blame] | 935 | if (!prefix) |
| 936 | prefix = ""; |
Eric Sunshine | fc56361 | 2015-07-06 13:30:50 -0400 | [diff] [blame] | 937 | if (!strcmp(av[1], "add")) |
| 938 | return add(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | df0b6cf | 2015-06-29 19:51:18 +0700 | [diff] [blame] | 939 | if (!strcmp(av[1], "prune")) |
| 940 | return prune(ac - 1, av + 1, prefix); |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 941 | if (!strcmp(av[1], "list")) |
| 942 | return list(ac - 1, av + 1, prefix); |
Nguyễn Thái Ngọc Duy | 58142c0 | 2016-06-13 19:18:24 +0700 | [diff] [blame] | 943 | if (!strcmp(av[1], "lock")) |
| 944 | 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] | 945 | if (!strcmp(av[1], "unlock")) |
| 946 | 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] | 947 | if (!strcmp(av[1], "move")) |
| 948 | 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] | 949 | if (!strcmp(av[1], "remove")) |
| 950 | 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] | 951 | usage_with_options(worktree_usage, options); |
| 952 | } |