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