| 459 | } |
| 460 | |
| 461 | static int add_worktree(const char *path, const char *refname, |
| 462 | const struct add_opts *opts) |
| 463 | { |
| 464 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; |
| 465 | struct strbuf sb = STRBUF_INIT; |
| 466 | const char *name; |
| 467 | struct strvec child_env = STRVEC_INIT; |
| 468 | unsigned int counter = 0; |
| 469 | int len, ret; |
| 470 | struct strbuf symref = STRBUF_INIT; |
| 471 | struct commit *commit = NULL; |
| 472 | int is_branch = 0; |
| 473 | struct strbuf sb_name = STRBUF_INIT; |
| 474 | struct worktree **worktrees, *wt = NULL; |
| 475 | struct ref_store *wt_refs; |
| 476 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 477 | |
| 478 | worktrees = get_worktrees(); |
| 479 | check_candidate_path(path, opts->force, worktrees, "add"); |
| 480 | free_worktrees(worktrees); |
| 481 | worktrees = NULL; |
| 482 | |
| 483 | /* is 'refname' a branch or commit? */ |
| 484 | if (!opts->detach && !check_branch_ref(&symref, refname) && |
| 485 | refs_ref_exists(get_main_ref_store(the_repository), symref.buf)) { |
| 486 | is_branch = 1; |
| 487 | if (!opts->force) |
| 488 | die_if_checked_out(symref.buf, 0); |
| 489 | } |
| 490 | commit = lookup_commit_reference_by_name(refname); |
| 491 | if (!commit && !opts->orphan) |
| 492 | die(_("invalid reference: %s"), refname); |
| 493 | |
| 494 | name = worktree_basename(path, &len); |
| 495 | strbuf_add(&sb, name, path + len - name); |
| 496 | sanitize_refname_component(sb.buf, &sb_name); |
| 497 | if (!sb_name.len) |
| 498 | BUG("How come '%s' becomes empty after sanitization?", sb.buf); |
| 499 | strbuf_reset(&sb); |
| 500 | name = sb_name.buf; |
| 501 | repo_git_path_replace(the_repository, &sb_repo, "worktrees/%s", name); |
| 502 | len = sb_repo.len; |
| 503 | if (safe_create_leading_directories_const(the_repository, sb_repo.buf)) |
| 504 | die_errno(_("could not create leading directories of '%s'"), |
| 505 | sb_repo.buf); |
| 506 | |
| 507 | while (mkdir(sb_repo.buf, 0777)) { |
| 508 | counter++; |
| 509 | if ((errno != EEXIST) || !counter /* overflow */) |
| 510 | die_errno(_("could not create directory of '%s'"), |
| 511 | sb_repo.buf); |
| 512 | strbuf_setlen(&sb_repo, len); |
| 513 | strbuf_addf(&sb_repo, "%d", counter); |
| 514 | } |
| 515 | name = strrchr(sb_repo.buf, '/') + 1; |
| 516 | |
| 517 | junk_pid = getpid(); |
| 518 | atexit(remove_junk); |
no test coverage detected