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