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