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