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