| 1 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "abspath.h" |
| 5 | #include "copy.h" |
| 6 | #include "environment.h" |
| 7 | #include "exec-cmd.h" |
| 8 | #include "gettext.h" |
| 9 | #include "hex.h" |
| 10 | #include "object-file.h" |
| 11 | #include "object-name.h" |
| 12 | #include "refs.h" |
| 13 | #include "repository.h" |
| 14 | #include "config.h" |
| 15 | #include "dir.h" |
| 16 | #include "setup.h" |
| 17 | #include "shallow.h" |
| 18 | #include "string-list.h" |
| 19 | #include "strvec.h" |
| 20 | #include "chdir-notify.h" |
| 21 | #include "path.h" |
| 22 | #include "quote.h" |
| 23 | #include "trace.h" |
| 24 | #include "trace2.h" |
| 25 | #include "worktree.h" |
| 26 | |
| 27 | enum allowed_bare_repo { |
| 28 | ALLOWED_BARE_REPO_EXPLICIT = 0, |
| 29 | ALLOWED_BARE_REPO_ALL, |
| 30 | }; |
| 31 | |
| 32 | static struct startup_info the_startup_info; |
| 33 | struct startup_info *startup_info = &the_startup_info; |
| 34 | const char *tmp_original_cwd; |
| 35 | |
| 36 | /* |
| 37 | * The input parameter must contain an absolute path, and it must already be |
| 38 | * normalized. |
| 39 | * |
| 40 | * Find the part of an absolute path that lies inside the work tree by |
| 41 | * dereferencing symlinks outside the work tree, for example: |
| 42 | * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file |
| 43 | * /dir/file (work tree is /) -> dir/file |
| 44 | * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2 |
| 45 | * /dir/repolink/file (repolink points to /dir/repo) -> file |
| 46 | * /dir/repo (exactly equal to work tree) -> (empty string) |
| 47 | */ |
| 48 | static int abspath_part_inside_repo(struct repository *repo, char *path) |
| 49 | { |
| 50 | size_t len; |
| 51 | size_t wtlen; |
| 52 | char *path0; |
| 53 | int off; |
| 54 | const char *work_tree = precompose_string_if_needed(repo_get_work_tree(repo)); |
| 55 | struct strbuf realpath = STRBUF_INIT; |
| 56 | |
| 57 | if (!work_tree) |
| 58 | return -1; |
| 59 | wtlen = strlen(work_tree); |
| 60 | len = strlen(path); |
| 61 | off = offset_1st_component(path); |
| 62 | |
| 63 | /* check if work tree is already the prefix */ |
| 64 | if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) { |
| 65 | if (path[wtlen] == '/') { |
| 66 | memmove(path, path + wtlen + 1, len - wtlen); |
| 67 | return 0; |
| 68 | } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') { |
| 69 | /* work tree is the root, or the whole path */ |
| 70 | memmove(path, path + wtlen, len - wtlen + 1); |
| 71 | return 0; |
| 72 | } |
| 73 | /* work tree might match beginning of a symlink to work tree */ |
| 74 | off = wtlen; |
| 75 | } |
| 76 | path0 = path; |
| 77 | path += off; |
| 78 | |
| 79 | /* check each '/'-terminated level */ |
| 80 | while (*path) { |
| 81 | path++; |
| 82 | if (*path == '/') { |
| 83 | *path = '\0'; |
| 84 | strbuf_realpath(&realpath, path0, 1); |
| 85 | if (fspathcmp(realpath.buf, work_tree) == 0) { |
| 86 | memmove(path0, path + 1, len - (path - path0)); |
| 87 | strbuf_release(&realpath); |
| 88 | return 0; |
| 89 | } |
| 90 | *path = '/'; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* check whole path */ |
| 95 | strbuf_realpath(&realpath, path0, 1); |
| 96 | if (fspathcmp(realpath.buf, work_tree) == 0) { |
| 97 | *path0 = '\0'; |
| 98 | strbuf_release(&realpath); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | strbuf_release(&realpath); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Normalize "path", prepending the "prefix" for relative paths. If |
| 108 | * remaining_prefix is not NULL, return the actual prefix still |
| 109 | * remains in the path. For example, prefix = sub1/sub2/ and path is |
| 110 | * |
| 111 | * foo -> sub1/sub2/foo (full prefix) |
| 112 | * ../foo -> sub1/foo (remaining prefix is sub1/) |
| 113 | * ../../bar -> bar (no remaining prefix) |
| 114 | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) |
| 115 | * `pwd`/../bar -> sub1/bar (no remaining prefix) |
| 116 | */ |
| 117 | char *prefix_path_gently(struct repository *repo, |
| 118 | const char *prefix, int len, |
| 119 | int *remaining_prefix, const char *path) |
| 120 | { |
| 121 | const char *orig = path; |
| 122 | char *sanitized; |
| 123 | if (is_absolute_path(orig)) { |
| 124 | sanitized = xmallocz(strlen(path)); |
| 125 | if (remaining_prefix) |
| 126 | *remaining_prefix = 0; |
| 127 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
| 128 | free(sanitized); |
| 129 | return NULL; |
| 130 | } |
| 131 | if (abspath_part_inside_repo(repo, sanitized)) { |
| 132 | free(sanitized); |
| 133 | return NULL; |
| 134 | } |
| 135 | } else { |
| 136 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
| 137 | if (remaining_prefix) |
| 138 | *remaining_prefix = len; |
| 139 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
| 140 | free(sanitized); |
| 141 | return NULL; |
| 142 | } |
| 143 | } |
| 144 | return sanitized; |
| 145 | } |
| 146 | |
| 147 | char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path) |
| 148 | { |
| 149 | char *r = prefix_path_gently(repo, prefix, len, NULL, path); |
| 150 | if (!r) { |
| 151 | const char *hint_path = repo_get_work_tree(repo); |
| 152 | if (!hint_path) |
| 153 | hint_path = repo_get_git_dir(repo); |
| 154 | die(_("'%s' is outside repository at '%s'"), path, |
| 155 | absolute_path(hint_path)); |
| 156 | } |
| 157 | return r; |
| 158 | } |
| 159 | |
| 160 | int path_inside_repo(struct repository *repo, const char *prefix, const char *path) |
| 161 | { |
| 162 | int len = prefix ? strlen(prefix) : 0; |
| 163 | char *r = prefix_path_gently(repo, prefix, len, NULL, path); |
| 164 | if (r) { |
| 165 | free(r); |
| 166 | return 1; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int check_filename(const char *prefix, const char *arg) |
| 172 | { |
| 173 | char *to_free = NULL; |
| 174 | struct stat st; |
| 175 | |
| 176 | if (skip_prefix(arg, ":/", &arg)) { |
| 177 | if (!*arg) /* ":/" is root dir, always exists */ |
| 178 | return 1; |
| 179 | prefix = NULL; |
| 180 | } else if (skip_prefix(arg, ":!", &arg) || |
| 181 | skip_prefix(arg, ":^", &arg)) { |
| 182 | if (!*arg) /* excluding everything is silly, but allowed */ |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | if (prefix) |
| 187 | arg = to_free = prefix_filename(prefix, arg); |
| 188 | |
| 189 | if (!lstat(arg, &st)) { |
| 190 | free(to_free); |
| 191 | return 1; /* file exists */ |
| 192 | } |
| 193 | if (is_missing_file_error(errno)) { |
| 194 | free(to_free); |
| 195 | return 0; /* file does not exist */ |
| 196 | } |
| 197 | die_errno(_("failed to stat '%s'"), arg); |
| 198 | } |
| 199 | |
| 200 | static void NORETURN die_verify_filename(struct repository *r, |
| 201 | const char *prefix, |
| 202 | const char *arg, |
| 203 | int diagnose_misspelt_rev) |
| 204 | { |
| 205 | if (!diagnose_misspelt_rev) |
| 206 | die(_("%s: no such path in the working tree.\n" |
| 207 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally."), |
| 208 | arg); |
| 209 | /* |
| 210 | * Saying "'(icase)foo' does not exist in the index" when the |
| 211 | * user gave us ":(icase)foo" is just stupid. A magic pathspec |
| 212 | * begins with a colon and is followed by a non-alnum; do not |
| 213 | * let maybe_die_on_misspelt_object_name() even trigger. |
| 214 | */ |
| 215 | if (!(arg[0] == ':' && !isalnum(arg[1]))) |
| 216 | maybe_die_on_misspelt_object_name(r, arg, prefix); |
| 217 | |
| 218 | /* ... or fall back the most general message. */ |
| 219 | die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
| 220 | "Use '--' to separate paths from revisions, like this:\n" |
| 221 | "'git <command> [<revision>...] -- [<file>...]'"), arg); |
| 222 | |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Check for arguments that don't resolve as actual files, |
| 227 | * but which look sufficiently like pathspecs that we'll consider |
| 228 | * them such for the purposes of rev/pathspec DWIM parsing. |
| 229 | */ |
| 230 | static int looks_like_pathspec(const char *arg) |
| 231 | { |
| 232 | const char *p; |
| 233 | int escaped = 0; |
| 234 | |
| 235 | /* |
| 236 | * Wildcard characters imply the user is looking to match pathspecs |
| 237 | * that aren't in the filesystem. Note that this doesn't include |
| 238 | * backslash even though it's a glob special; by itself it doesn't |
| 239 | * cause any increase in the match. Likewise ignore backslash-escaped |
| 240 | * wildcard characters. |
| 241 | */ |
| 242 | for (p = arg; *p; p++) { |
| 243 | if (escaped) { |
| 244 | escaped = 0; |
| 245 | } else if (is_glob_special(*p)) { |
| 246 | if (*p == '\\') |
| 247 | escaped = 1; |
| 248 | else |
| 249 | return 1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* long-form pathspec magic */ |
| 254 | if (starts_with(arg, ":(")) |
| 255 | return 1; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Verify a filename that we got as an argument for a pathspec |
| 262 | * entry. Note that a filename that begins with "-" never verifies |
| 263 | * as true, because even if such a filename were to exist, we want |
| 264 | * it to be preceded by the "--" marker (or we want the user to |
| 265 | * use a format like "./-filename") |
| 266 | * |
| 267 | * The "diagnose_misspelt_rev" is used to provide a user-friendly |
| 268 | * diagnosis when dying upon finding that "name" is not a pathname. |
| 269 | * If set to 1, the diagnosis will try to diagnose "name" as an |
| 270 | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis |
| 271 | * will only complain about an inexisting file. |
| 272 | * |
| 273 | * This function is typically called to check that a "file or rev" |
| 274 | * argument is unambiguous. In this case, the caller will want |
| 275 | * diagnose_misspelt_rev == 1 when verifying the first non-rev |
| 276 | * argument (which could have been a revision), and |
| 277 | * diagnose_misspelt_rev == 0 for the next ones (because we already |
| 278 | * saw a filename, there's not ambiguity anymore). |
| 279 | */ |
| 280 | void verify_filename(struct repository *repo, |
| 281 | const char *prefix, |
| 282 | const char *arg, |
| 283 | int diagnose_misspelt_rev) |
| 284 | { |
| 285 | if (*arg == '-') |
| 286 | die(_("option '%s' must come before non-option arguments"), arg); |
| 287 | if (looks_like_pathspec(arg) || check_filename(prefix, arg)) |
| 288 | return; |
| 289 | die_verify_filename(repo, prefix, arg, diagnose_misspelt_rev); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Opposite of the above: the command line did not have -- marker |
| 294 | * and we parsed the arg as a refname. It should not be interpretable |
| 295 | * as a filename. |
| 296 | */ |
| 297 | void verify_non_filename(struct repository *repo, const char *prefix, const char *arg) |
| 298 | { |
| 299 | if (!is_inside_work_tree(repo) || is_inside_git_dir(repo)) |
| 300 | return; |
| 301 | if (*arg == '-') |
| 302 | return; /* flag */ |
| 303 | if (!check_filename(prefix, arg)) |
| 304 | return; |
| 305 | die(_("ambiguous argument '%s': both revision and filename\n" |
| 306 | "Use '--' to separate paths from revisions, like this:\n" |
| 307 | "'git <command> [<revision>...] -- [<file>...]'"), arg); |
| 308 | } |
| 309 | |
| 310 | int get_common_dir(struct strbuf *sb, const char *gitdir) |
| 311 | { |
| 312 | const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT); |
| 313 | if (git_env_common_dir) { |
| 314 | strbuf_addstr(sb, git_env_common_dir); |
| 315 | return 1; |
| 316 | } else { |
| 317 | return get_common_dir_noenv(sb, gitdir); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) |
| 322 | { |
| 323 | struct strbuf data = STRBUF_INIT; |
| 324 | struct strbuf path = STRBUF_INIT; |
| 325 | int ret = 0; |
| 326 | |
| 327 | strbuf_addf(&path, "%s/commondir", gitdir); |
| 328 | if (file_exists(path.buf)) { |
| 329 | if (strbuf_read_file(&data, path.buf, 0) <= 0) |
| 330 | die_errno(_("failed to read %s"), path.buf); |
| 331 | while (data.len && (data.buf[data.len - 1] == '\n' || |
| 332 | data.buf[data.len - 1] == '\r')) |
| 333 | data.len--; |
| 334 | data.buf[data.len] = '\0'; |
| 335 | strbuf_reset(&path); |
| 336 | if (!is_absolute_path(data.buf)) |
| 337 | strbuf_addf(&path, "%s/", gitdir); |
| 338 | strbuf_addbuf(&path, &data); |
| 339 | strbuf_add_real_path(sb, path.buf); |
| 340 | ret = 1; |
| 341 | } else { |
| 342 | strbuf_addstr(sb, gitdir); |
| 343 | } |
| 344 | |
| 345 | strbuf_release(&data); |
| 346 | strbuf_release(&path); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static int validate_headref(const char *path) |
| 351 | { |
| 352 | struct stat st; |
| 353 | char buffer[256]; |
| 354 | const char *refname; |
| 355 | struct object_id oid; |
| 356 | int fd; |
| 357 | ssize_t len; |
| 358 | |
| 359 | if (lstat(path, &st) < 0) |
| 360 | return -1; |
| 361 | |
| 362 | /* Make sure it is a "refs/.." symlink */ |
| 363 | if (S_ISLNK(st.st_mode)) { |
| 364 | len = readlink(path, buffer, sizeof(buffer)-1); |
| 365 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
| 366 | return 0; |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Anything else, just open it and try to see if it is a symbolic ref. |
| 372 | */ |
| 373 | fd = open(path, O_RDONLY); |
| 374 | if (fd < 0) |
| 375 | return -1; |
| 376 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
| 377 | close(fd); |
| 378 | |
| 379 | if (len < 0) |
| 380 | return -1; |
| 381 | buffer[len] = '\0'; |
| 382 | |
| 383 | /* |
| 384 | * Is it a symbolic ref? |
| 385 | */ |
| 386 | if (skip_prefix(buffer, "ref:", &refname)) { |
| 387 | while (isspace(*refname)) |
| 388 | refname++; |
| 389 | if (starts_with(refname, "refs/")) |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Is this a detached HEAD? |
| 395 | */ |
| 396 | if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN) |
| 397 | return 0; |
| 398 | |
| 399 | return -1; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Test if it looks like we're at a git directory. |
| 404 | * We want to see: |
| 405 | * |
| 406 | * - either an objects/ directory _or_ the proper |
| 407 | * GIT_OBJECT_DIRECTORY environment variable |
| 408 | * - a refs/ directory |
| 409 | * - either a HEAD symlink or a HEAD file that is formatted as |
| 410 | * a proper "ref:", or a regular file HEAD that has a properly |
| 411 | * formatted sha1 object name. |
| 412 | */ |
| 413 | int is_git_directory(const char *suspect) |
| 414 | { |
| 415 | struct strbuf path = STRBUF_INIT; |
| 416 | int ret = 0; |
| 417 | size_t len; |
| 418 | |
| 419 | /* Check worktree-related signatures */ |
| 420 | strbuf_addstr(&path, suspect); |
| 421 | strbuf_complete(&path, '/'); |
| 422 | strbuf_addstr(&path, "HEAD"); |
| 423 | if (validate_headref(path.buf)) |
| 424 | goto done; |
| 425 | |
| 426 | strbuf_reset(&path); |
| 427 | get_common_dir(&path, suspect); |
| 428 | len = path.len; |
| 429 | |
| 430 | /* Check non-worktree-related signatures */ |
| 431 | if (getenv(DB_ENVIRONMENT)) { |
| 432 | if (access(getenv(DB_ENVIRONMENT), X_OK)) |
| 433 | goto done; |
| 434 | } |
| 435 | else { |
| 436 | strbuf_setlen(&path, len); |
| 437 | strbuf_addstr(&path, "/objects"); |
| 438 | if (access(path.buf, X_OK)) |
| 439 | goto done; |
| 440 | } |
| 441 | |
| 442 | strbuf_setlen(&path, len); |
| 443 | strbuf_addstr(&path, "/refs"); |
| 444 | if (access(path.buf, X_OK)) |
| 445 | goto done; |
| 446 | |
| 447 | ret = 1; |
| 448 | done: |
| 449 | strbuf_release(&path); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | int is_nonbare_repository_dir(struct strbuf *path) |
| 454 | { |
| 455 | int ret = 0; |
| 456 | int gitfile_error; |
| 457 | size_t orig_path_len = path->len; |
| 458 | assert(orig_path_len != 0); |
| 459 | strbuf_complete(path, '/'); |
| 460 | strbuf_addstr(path, ".git"); |
| 461 | if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) |
| 462 | ret = 1; |
| 463 | if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || |
| 464 | gitfile_error == READ_GITFILE_ERR_READ_FAILED) |
| 465 | ret = 1; |
| 466 | strbuf_setlen(path, orig_path_len); |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | int is_inside_git_dir(struct repository *repo) |
| 471 | { |
| 472 | struct strbuf buf = STRBUF_INIT; |
| 473 | int ret = is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1)); |
| 474 | strbuf_release(&buf); |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | int is_inside_work_tree(struct repository *repo) |
| 479 | { |
| 480 | struct strbuf buf = STRBUF_INIT; |
| 481 | const char *worktree; |
| 482 | int ret; |
| 483 | |
| 484 | worktree = repo_get_work_tree(repo); |
| 485 | if (!worktree) |
| 486 | return 0; |
| 487 | |
| 488 | ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1)); |
| 489 | |
| 490 | strbuf_release(&buf); |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | void setup_work_tree(struct repository *repo) |
| 495 | { |
| 496 | const char *work_tree; |
| 497 | |
| 498 | if (repo->worktree_config_is_bogus) |
| 499 | die(_("unable to set up work tree using invalid config")); |
| 500 | |
| 501 | work_tree = repo_get_work_tree(repo); |
| 502 | if (!work_tree || chdir_notify(work_tree)) |
| 503 | die(_("this operation must be run in a work tree")); |
| 504 | |
| 505 | /* |
| 506 | * Make sure subsequent git processes find correct worktree |
| 507 | * if $GIT_WORK_TREE is set relative |
| 508 | */ |
| 509 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) |
| 510 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); |
| 511 | } |
| 512 | |
| 513 | static void setup_original_cwd(struct repository *repo) |
| 514 | { |
| 515 | struct strbuf tmp = STRBUF_INIT; |
| 516 | const char *worktree = NULL; |
| 517 | int offset = -1; |
| 518 | |
| 519 | if (!tmp_original_cwd) |
| 520 | return; |
| 521 | |
| 522 | /* |
| 523 | * startup_info->original_cwd points to the current working |
| 524 | * directory we inherited from our parent process, which is a |
| 525 | * directory we want to avoid removing. |
| 526 | * |
| 527 | * For convenience, we would like to have the path relative to the |
| 528 | * worktree instead of an absolute path. |
| 529 | * |
| 530 | * Yes, startup_info->original_cwd is usually the same as 'prefix', |
| 531 | * but differs in two ways: |
| 532 | * - prefix has a trailing '/' |
| 533 | * - if the user passes '-C' to git, that modifies the prefix but |
| 534 | * not startup_info->original_cwd. |
| 535 | */ |
| 536 | |
| 537 | /* Normalize the directory */ |
| 538 | if (!strbuf_realpath(&tmp, tmp_original_cwd, 0)) { |
| 539 | trace2_data_string("setup", repo, |
| 540 | "realpath-path", tmp_original_cwd); |
| 541 | trace2_data_string("setup", repo, |
| 542 | "realpath-failure", strerror(errno)); |
| 543 | free((char*)tmp_original_cwd); |
| 544 | tmp_original_cwd = NULL; |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | free((char*)tmp_original_cwd); |
| 549 | tmp_original_cwd = NULL; |
| 550 | startup_info->original_cwd = strbuf_detach(&tmp, NULL); |
| 551 | |
| 552 | /* |
| 553 | * Get our worktree; we only protect the current working directory |
| 554 | * if it's in the worktree. |
| 555 | */ |
| 556 | worktree = repo_get_work_tree(repo); |
| 557 | if (!worktree) |
| 558 | goto no_prevention_needed; |
| 559 | |
| 560 | offset = dir_inside_of(startup_info->original_cwd, worktree); |
| 561 | if (offset >= 0) { |
| 562 | /* |
| 563 | * If startup_info->original_cwd == worktree, that is already |
| 564 | * protected and we don't need original_cwd as a secondary |
| 565 | * protection measure. |
| 566 | */ |
| 567 | if (!*(startup_info->original_cwd + offset)) |
| 568 | goto no_prevention_needed; |
| 569 | |
| 570 | /* |
| 571 | * original_cwd was inside worktree; precompose it just as |
| 572 | * we do prefix so that built up paths will match |
| 573 | */ |
| 574 | startup_info->original_cwd = \ |
| 575 | precompose_string_if_needed(startup_info->original_cwd |
| 576 | + offset); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | no_prevention_needed: |
| 581 | free((char*)startup_info->original_cwd); |
| 582 | startup_info->original_cwd = NULL; |
| 583 | } |
| 584 | |
| 585 | static int read_worktree_config(const char *var, const char *value, |
| 586 | const struct config_context *ctx UNUSED, |
| 587 | void *vdata) |
| 588 | { |
| 589 | struct repository_format *data = vdata; |
| 590 | |
| 591 | if (strcmp(var, "core.bare") == 0) { |
| 592 | data->is_bare = git_config_bool(var, value); |
| 593 | } else if (strcmp(var, "core.worktree") == 0) { |
| 594 | if (!value) |
| 595 | return config_error_nonbool(var); |
| 596 | free(data->work_tree); |
| 597 | data->work_tree = xstrdup(value); |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | enum extension_result { |
| 603 | EXTENSION_ERROR = -1, /* compatible with error(), etc */ |
| 604 | EXTENSION_UNKNOWN = 0, |
| 605 | EXTENSION_OK = 1 |
| 606 | }; |
| 607 | |
| 608 | /* |
| 609 | * Do not add new extensions to this function. It handles extensions which are |
| 610 | * respected even in v0-format repositories for historical compatibility. |
| 611 | */ |
| 612 | static enum extension_result handle_extension_v0(const char *var, |
| 613 | const char *value, |
| 614 | const char *ext, |
| 615 | struct repository_format *data) |
| 616 | { |
| 617 | if (!strcmp(ext, "noop")) { |
| 618 | return EXTENSION_OK; |
| 619 | } else if (!strcmp(ext, "preciousobjects")) { |
| 620 | data->precious_objects = git_config_bool(var, value); |
| 621 | return EXTENSION_OK; |
| 622 | } else if (!strcmp(ext, "partialclone")) { |
| 623 | if (!value) |
| 624 | return config_error_nonbool(var); |
| 625 | data->partial_clone = xstrdup(value); |
| 626 | return EXTENSION_OK; |
| 627 | } else if (!strcmp(ext, "worktreeconfig")) { |
| 628 | data->worktree_config = git_config_bool(var, value); |
| 629 | return EXTENSION_OK; |
| 630 | } |
| 631 | |
| 632 | return EXTENSION_UNKNOWN; |
| 633 | } |
| 634 | |
| 635 | static void parse_reference_uri(const char *value, char **format, |
| 636 | char **payload) |
| 637 | { |
| 638 | const char *schema_end; |
| 639 | |
| 640 | schema_end = strstr(value, "://"); |
| 641 | if (!schema_end) { |
| 642 | *format = xstrdup(value); |
| 643 | *payload = NULL; |
| 644 | } else { |
| 645 | *format = xstrndup(value, schema_end - value); |
| 646 | *payload = xstrdup_or_null(schema_end + 3); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Record any new extensions in this function. |
| 652 | */ |
| 653 | static enum extension_result handle_extension(const char *var, |
| 654 | const char *value, |
| 655 | const char *ext, |
| 656 | struct repository_format *data) |
| 657 | { |
| 658 | if (!strcmp(ext, "noop-v1")) { |
| 659 | return EXTENSION_OK; |
| 660 | } else if (!strcmp(ext, "objectformat")) { |
| 661 | int format; |
| 662 | |
| 663 | if (!value) |
| 664 | return config_error_nonbool(var); |
| 665 | format = hash_algo_by_name(value); |
| 666 | if (format == GIT_HASH_UNKNOWN) |
| 667 | return error(_("invalid value for '%s': '%s'"), |
| 668 | "extensions.objectformat", value); |
| 669 | data->hash_algo = format; |
| 670 | return EXTENSION_OK; |
| 671 | } else if (!strcmp(ext, "compatobjectformat")) { |
| 672 | struct string_list_item *item; |
| 673 | int format; |
| 674 | |
| 675 | if (!value) |
| 676 | return config_error_nonbool(var); |
| 677 | format = hash_algo_by_name(value); |
| 678 | if (format == GIT_HASH_UNKNOWN) |
| 679 | return error(_("invalid value for '%s': '%s'"), |
| 680 | "extensions.compatobjectformat", value); |
| 681 | /* For now only support compatObjectFormat being specified once. */ |
| 682 | for_each_string_list_item(item, &data->v1_only_extensions) { |
| 683 | if (!strcmp(item->string, "compatobjectformat")) |
| 684 | return error(_("'%s' already specified as '%s'"), |
| 685 | "extensions.compatobjectformat", |
| 686 | hash_algos[data->compat_hash_algo].name); |
| 687 | } |
| 688 | data->compat_hash_algo = format; |
| 689 | return EXTENSION_OK; |
| 690 | } else if (!strcmp(ext, "refstorage")) { |
| 691 | unsigned int format; |
| 692 | char *format_str; |
| 693 | |
| 694 | if (!value) |
| 695 | return config_error_nonbool(var); |
| 696 | |
| 697 | parse_reference_uri(value, &format_str, |
| 698 | &data->ref_storage_payload); |
| 699 | |
| 700 | format = ref_storage_format_by_name(format_str); |
| 701 | free(format_str); |
| 702 | |
| 703 | if (format == REF_STORAGE_FORMAT_UNKNOWN) |
| 704 | return error(_("invalid value for '%s': '%s'"), |
| 705 | "extensions.refstorage", value); |
| 706 | data->ref_storage_format = format; |
| 707 | return EXTENSION_OK; |
| 708 | } else if (!strcmp(ext, "relativeworktrees")) { |
| 709 | data->relative_worktrees = git_config_bool(var, value); |
| 710 | return EXTENSION_OK; |
| 711 | } else if (!strcmp(ext, "submodulepathconfig")) { |
| 712 | data->submodule_path_cfg = git_config_bool(var, value); |
| 713 | return EXTENSION_OK; |
| 714 | } |
| 715 | return EXTENSION_UNKNOWN; |
| 716 | } |
| 717 | |
| 718 | static int check_repo_format(const char *var, const char *value, |
| 719 | const struct config_context *ctx, void *vdata) |
| 720 | { |
| 721 | struct repository_format *data = vdata; |
| 722 | const char *ext; |
| 723 | |
| 724 | if (strcmp(var, "core.repositoryformatversion") == 0) |
| 725 | data->version = git_config_int(var, value, ctx->kvi); |
| 726 | else if (skip_prefix(var, "extensions.", &ext)) { |
| 727 | switch (handle_extension_v0(var, value, ext, data)) { |
| 728 | case EXTENSION_ERROR: |
| 729 | return -1; |
| 730 | case EXTENSION_OK: |
| 731 | return 0; |
| 732 | case EXTENSION_UNKNOWN: |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | switch (handle_extension(var, value, ext, data)) { |
| 737 | case EXTENSION_ERROR: |
| 738 | return -1; |
| 739 | case EXTENSION_OK: |
| 740 | string_list_append(&data->v1_only_extensions, ext); |
| 741 | return 0; |
| 742 | case EXTENSION_UNKNOWN: |
| 743 | string_list_append(&data->unknown_extensions, ext); |
| 744 | return 0; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | return read_worktree_config(var, value, ctx, vdata); |
| 749 | } |
| 750 | |
| 751 | static int read_and_verify_repository_format(struct repository_format *format, |
| 752 | const char *gitdir, |
| 753 | int *nongit_ok) |
| 754 | { |
| 755 | struct strbuf sb = STRBUF_INIT; |
| 756 | struct strbuf err = STRBUF_INIT; |
| 757 | int has_common; |
| 758 | |
| 759 | has_common = get_common_dir(&sb, gitdir); |
| 760 | strbuf_addstr(&sb, "/config"); |
| 761 | read_repository_format(format, sb.buf); |
| 762 | strbuf_release(&sb); |
| 763 | |
| 764 | /* |
| 765 | * For historical use of check_and_apply_repository_format() in git-init, |
| 766 | * we treat a missing config as a silent "ok", even when nongit_ok |
| 767 | * is unset. |
| 768 | */ |
| 769 | if (format->version < 0) |
| 770 | return 0; |
| 771 | |
| 772 | if (verify_repository_format(format, &err) < 0) { |
| 773 | if (nongit_ok) { |
| 774 | warning("%s", err.buf); |
| 775 | strbuf_release(&err); |
| 776 | *nongit_ok = -1; |
| 777 | return -1; |
| 778 | } |
| 779 | die("%s", err.buf); |
| 780 | } |
| 781 | |
| 782 | string_list_clear(&format->unknown_extensions, 0); |
| 783 | string_list_clear(&format->v1_only_extensions, 0); |
| 784 | |
| 785 | if (format->worktree_config) { |
| 786 | /* |
| 787 | * pick up core.bare and core.worktree from per-worktree |
| 788 | * config if present |
| 789 | */ |
| 790 | strbuf_addf(&sb, "%s/config.worktree", gitdir); |
| 791 | git_config_from_file(read_worktree_config, sb.buf, format); |
| 792 | strbuf_release(&sb); |
| 793 | has_common = 0; |
| 794 | } |
| 795 | |
| 796 | if (startup_info->force_bare_repository) { |
| 797 | format->is_bare = 1; |
| 798 | FREE_AND_NULL(format->work_tree); |
| 799 | } else if (has_common) { |
| 800 | /* |
| 801 | * When sharing a common dir with another repository (e.g. a |
| 802 | * linked worktree), do not let this repository's config |
| 803 | * dictate bareness; it is inherited from the main worktree. |
| 804 | */ |
| 805 | format->is_bare = -1; |
| 806 | |
| 807 | /* |
| 808 | * Furthermore, "core.worktree" is supposed to be ignored when |
| 809 | * we have a commondir configured, unless it comes from the |
| 810 | * per-worktree configuration. |
| 811 | */ |
| 812 | FREE_AND_NULL(format->work_tree); |
| 813 | } |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | int upgrade_repository_format(struct repository *repo, int target_version) |
| 819 | { |
| 820 | struct strbuf sb = STRBUF_INIT; |
| 821 | struct strbuf err = STRBUF_INIT; |
| 822 | struct strbuf repo_version = STRBUF_INIT; |
| 823 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
| 824 | int ret; |
| 825 | |
| 826 | repo_common_path_append(repo, &sb, "config"); |
| 827 | read_repository_format(&repo_fmt, sb.buf); |
| 828 | strbuf_release(&sb); |
| 829 | |
| 830 | if (repo_fmt.version >= target_version) { |
| 831 | ret = 0; |
| 832 | goto out; |
| 833 | } |
| 834 | |
| 835 | if (verify_repository_format(&repo_fmt, &err) < 0) { |
| 836 | ret = error("cannot upgrade repository format from %d to %d: %s", |
| 837 | repo_fmt.version, target_version, err.buf); |
| 838 | goto out; |
| 839 | } |
| 840 | if (!repo_fmt.version && repo_fmt.unknown_extensions.nr) { |
| 841 | ret = error("cannot upgrade repository format: " |
| 842 | "unknown extension %s", |
| 843 | repo_fmt.unknown_extensions.items[0].string); |
| 844 | goto out; |
| 845 | } |
| 846 | |
| 847 | strbuf_addf(&repo_version, "%d", target_version); |
| 848 | repo_config_set(repo, "core.repositoryformatversion", repo_version.buf); |
| 849 | |
| 850 | ret = 1; |
| 851 | |
| 852 | out: |
| 853 | clear_repository_format(&repo_fmt); |
| 854 | strbuf_release(&repo_version); |
| 855 | strbuf_release(&err); |
| 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | static void init_repository_format(struct repository_format *format) |
| 860 | { |
| 861 | const struct repository_format fresh = REPOSITORY_FORMAT_INIT; |
| 862 | |
| 863 | memcpy(format, &fresh, sizeof(fresh)); |
| 864 | } |
| 865 | |
| 866 | int read_repository_format(struct repository_format *format, const char *path) |
| 867 | { |
| 868 | clear_repository_format(format); |
| 869 | format->hash_algo = GIT_HASH_SHA1_LEGACY; |
| 870 | git_config_from_file(check_repo_format, path, format); |
| 871 | if (format->version == -1) { |
| 872 | clear_repository_format(format); |
| 873 | format->hash_algo = GIT_HASH_SHA1_LEGACY; |
| 874 | } |
| 875 | return format->version; |
| 876 | } |
| 877 | |
| 878 | void clear_repository_format(struct repository_format *format) |
| 879 | { |
| 880 | string_list_clear(&format->unknown_extensions, 0); |
| 881 | string_list_clear(&format->v1_only_extensions, 0); |
| 882 | free(format->work_tree); |
| 883 | free(format->partial_clone); |
| 884 | free(format->ref_storage_payload); |
| 885 | init_repository_format(format); |
| 886 | } |
| 887 | |
| 888 | int verify_repository_format(const struct repository_format *format, |
| 889 | struct strbuf *err) |
| 890 | { |
| 891 | if (GIT_REPO_VERSION_READ < format->version) { |
| 892 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
| 893 | GIT_REPO_VERSION_READ, format->version); |
| 894 | return -1; |
| 895 | } |
| 896 | |
| 897 | if (format->version >= 1 && format->unknown_extensions.nr) { |
| 898 | int i; |
| 899 | |
| 900 | strbuf_addstr(err, Q_("unknown repository extension found:", |
| 901 | "unknown repository extensions found:", |
| 902 | format->unknown_extensions.nr)); |
| 903 | |
| 904 | for (i = 0; i < format->unknown_extensions.nr; i++) |
| 905 | strbuf_addf(err, "\n\t%s", |
| 906 | format->unknown_extensions.items[i].string); |
| 907 | return -1; |
| 908 | } |
| 909 | |
| 910 | if (format->version == 0 && format->v1_only_extensions.nr) { |
| 911 | int i; |
| 912 | |
| 913 | strbuf_addstr(err, |
| 914 | Q_("repo version is 0, but v1-only extension found:", |
| 915 | "repo version is 0, but v1-only extensions found:", |
| 916 | format->v1_only_extensions.nr)); |
| 917 | |
| 918 | for (i = 0; i < format->v1_only_extensions.nr; i++) |
| 919 | strbuf_addf(err, "\n\t%s", |
| 920 | format->v1_only_extensions.items[i].string); |
| 921 | return -1; |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | void read_gitfile_error_die(int error_code, const char *path) |
| 928 | { |
| 929 | switch (error_code) { |
| 930 | case READ_GITFILE_ERR_NOT_A_FILE: |
| 931 | case READ_GITFILE_ERR_STAT_FAILED: |
| 932 | case READ_GITFILE_ERR_MISSING: |
| 933 | case READ_GITFILE_ERR_IS_A_DIR: |
| 934 | /* non-fatal; follow return path */ |
| 935 | break; |
| 936 | case READ_GITFILE_ERR_OPEN_FAILED: |
| 937 | die_errno(_("error opening '%s'"), path); |
| 938 | case READ_GITFILE_ERR_TOO_LARGE: |
| 939 | die(_("too large to be a .git file: '%s'"), path); |
| 940 | case READ_GITFILE_ERR_READ_FAILED: |
| 941 | die(_("error reading %s"), path); |
| 942 | case READ_GITFILE_ERR_INVALID_FORMAT: |
| 943 | die(_("invalid gitfile format: %s"), path); |
| 944 | case READ_GITFILE_ERR_NO_PATH: |
| 945 | die(_("no path in gitfile: %s"), path); |
| 946 | case READ_GITFILE_ERR_NOT_A_REPO: |
| 947 | die(_("gitfile does not point to a valid repository: %s"), |
| 948 | path); |
| 949 | default: |
| 950 | BUG("unknown error code"); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * Try to read the location of the git directory from the .git file, |
| 956 | * return path to git directory if found. The return value comes from |
| 957 | * a shared buffer. |
| 958 | * |
| 959 | * On failure, if return_error_code is not NULL, return_error_code |
| 960 | * will be set to an error code and NULL will be returned. If |
| 961 | * return_error_code is NULL the function will die instead (for most |
| 962 | * cases). |
| 963 | */ |
| 964 | const char *read_gitfile_gently(const char *path, int *return_error_code) |
| 965 | { |
| 966 | const int max_file_size = 1 << 20; /* 1MB */ |
| 967 | int error_code = 0; |
| 968 | char *buf = NULL; |
| 969 | char *dir = NULL; |
| 970 | const char *slash; |
| 971 | struct stat st; |
| 972 | int fd; |
| 973 | ssize_t len; |
| 974 | static struct strbuf realpath = STRBUF_INIT; |
| 975 | |
| 976 | if (stat(path, &st)) { |
| 977 | if (errno == ENOENT || errno == ENOTDIR) |
| 978 | error_code = READ_GITFILE_ERR_MISSING; |
| 979 | else |
| 980 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
| 981 | goto cleanup_return; |
| 982 | } |
| 983 | if (S_ISDIR(st.st_mode)) { |
| 984 | error_code = READ_GITFILE_ERR_IS_A_DIR; |
| 985 | goto cleanup_return; |
| 986 | } |
| 987 | if (!S_ISREG(st.st_mode)) { |
| 988 | error_code = READ_GITFILE_ERR_NOT_A_FILE; |
| 989 | goto cleanup_return; |
| 990 | } |
| 991 | if (st.st_size > max_file_size) { |
| 992 | error_code = READ_GITFILE_ERR_TOO_LARGE; |
| 993 | goto cleanup_return; |
| 994 | } |
| 995 | fd = open(path, O_RDONLY); |
| 996 | if (fd < 0) { |
| 997 | error_code = READ_GITFILE_ERR_OPEN_FAILED; |
| 998 | goto cleanup_return; |
| 999 | } |
| 1000 | buf = xmallocz(st.st_size); |
| 1001 | len = read_in_full(fd, buf, st.st_size); |
| 1002 | close(fd); |
| 1003 | if (len != st.st_size) { |
| 1004 | error_code = READ_GITFILE_ERR_READ_FAILED; |
| 1005 | goto cleanup_return; |
| 1006 | } |
| 1007 | if (!starts_with(buf, "gitdir: ")) { |
| 1008 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; |
| 1009 | goto cleanup_return; |
| 1010 | } |
| 1011 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
| 1012 | len--; |
| 1013 | if (len < 9) { |
| 1014 | error_code = READ_GITFILE_ERR_NO_PATH; |
| 1015 | goto cleanup_return; |
| 1016 | } |
| 1017 | buf[len] = '\0'; |
| 1018 | dir = buf + 8; |
| 1019 | |
| 1020 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { |
| 1021 | size_t pathlen = slash+1 - path; |
| 1022 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
| 1023 | (int)(len - 8), buf + 8); |
| 1024 | free(buf); |
| 1025 | buf = dir; |
| 1026 | } |
| 1027 | if (!is_git_directory(dir)) { |
| 1028 | error_code = READ_GITFILE_ERR_NOT_A_REPO; |
| 1029 | goto cleanup_return; |
| 1030 | } |
| 1031 | |
| 1032 | strbuf_realpath(&realpath, dir, 1); |
| 1033 | path = realpath.buf; |
| 1034 | |
| 1035 | cleanup_return: |
| 1036 | if (return_error_code) |
| 1037 | *return_error_code = error_code; |
| 1038 | else if (error_code) |
| 1039 | read_gitfile_error_die(error_code, path); |
| 1040 | |
| 1041 | free(buf); |
| 1042 | return error_code ? NULL : path; |
| 1043 | } |
| 1044 | |
| 1045 | static void apply_gitdir_and_environment(struct repository *repo, const char *path) |
| 1046 | { |
| 1047 | struct strvec to_free = STRVEC_INIT; |
| 1048 | struct set_gitdir_args args = { |
| 1049 | .commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT), |
| 1050 | .graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT), |
| 1051 | .index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT), |
| 1052 | .disable_ref_updates = !!getenv(GIT_QUARANTINE_ENVIRONMENT), |
| 1053 | }; |
| 1054 | |
| 1055 | repo_set_gitdir(repo, path, &args); |
| 1056 | |
| 1057 | strvec_clear(&to_free); |
| 1058 | } |
| 1059 | |
| 1060 | static void update_relative_gitdir(const char *name UNUSED, |
| 1061 | const char *old_cwd, |
| 1062 | const char *new_cwd, |
| 1063 | void *data) |
| 1064 | { |
| 1065 | struct repository *repo = data; |
| 1066 | char *path = reparent_relative_path(old_cwd, new_cwd, |
| 1067 | repo_get_git_dir(repo)); |
| 1068 | trace_printf_key(&trace_setup_key, |
| 1069 | "setup: move $GIT_DIR to '%s'", |
| 1070 | path); |
| 1071 | apply_gitdir_and_environment(repo, path); |
| 1072 | xsetenv(GIT_DIR_ENVIRONMENT, path, 1); |
| 1073 | free(path); |
| 1074 | } |
| 1075 | |
| 1076 | static void apply_and_export_relative_gitdir(struct repository *repo, const char *path, int make_realpath) |
| 1077 | { |
| 1078 | struct strbuf realpath = STRBUF_INIT; |
| 1079 | |
| 1080 | if (make_realpath) { |
| 1081 | strbuf_realpath(&realpath, path, 1); |
| 1082 | path = realpath.buf; |
| 1083 | } |
| 1084 | |
| 1085 | apply_gitdir_and_environment(repo, path); |
| 1086 | xsetenv(GIT_DIR_ENVIRONMENT, path, 1); |
| 1087 | |
| 1088 | if (!is_absolute_path(path)) |
| 1089 | chdir_notify_register(NULL, update_relative_gitdir, repo); |
| 1090 | |
| 1091 | strbuf_release(&realpath); |
| 1092 | } |
| 1093 | |
| 1094 | struct repo_discovery { |
| 1095 | struct repository_format format; |
| 1096 | char *gitdir; |
| 1097 | char *worktree; |
| 1098 | char *prefix; |
| 1099 | }; |
| 1100 | |
| 1101 | #define REPO_DISCOVERY_INIT { \ |
| 1102 | .format = REPOSITORY_FORMAT_INIT, \ |
| 1103 | } |
| 1104 | |
| 1105 | static void repo_discovery_release(struct repo_discovery *r) |
| 1106 | { |
| 1107 | clear_repository_format(&r->format); |
| 1108 | free(r->gitdir); |
| 1109 | free(r->worktree); |
| 1110 | free(r->prefix); |
| 1111 | } |
| 1112 | |
| 1113 | static void repo_discovery_set_gitdir(struct repo_discovery *r, |
| 1114 | const char *gitdir, |
| 1115 | int make_realpath) |
| 1116 | { |
| 1117 | free(r->gitdir); |
| 1118 | if (make_realpath) { |
| 1119 | struct strbuf realpath = STRBUF_INIT; |
| 1120 | strbuf_realpath(&realpath, gitdir, 1); |
| 1121 | r->gitdir = strbuf_detach(&realpath, NULL); |
| 1122 | } else { |
| 1123 | r->gitdir = xstrdup(gitdir); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | static void repo_discovery_set_worktree(struct repo_discovery *r, |
| 1128 | const char *worktree) |
| 1129 | { |
| 1130 | free(r->worktree); |
| 1131 | r->worktree = real_pathdup(worktree, 1); |
| 1132 | } |
| 1133 | |
| 1134 | static void repo_discover_explicit_gitdir(struct repo_discovery *discovery, |
| 1135 | const char *gitdirenv, |
| 1136 | struct strbuf *cwd, |
| 1137 | int *nongit_ok) |
| 1138 | { |
| 1139 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
| 1140 | char *gitfile; |
| 1141 | int offset; |
| 1142 | |
| 1143 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
| 1144 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
| 1145 | |
| 1146 | gitfile = (char*)read_gitfile(gitdirenv); |
| 1147 | if (gitfile) { |
| 1148 | gitfile = xstrdup(gitfile); |
| 1149 | gitdirenv = gitfile; |
| 1150 | } |
| 1151 | |
| 1152 | if (!is_git_directory(gitdirenv)) { |
| 1153 | if (nongit_ok) { |
| 1154 | *nongit_ok = 1; |
| 1155 | goto out; |
| 1156 | } |
| 1157 | die(_("not a git repository: '%s'"), gitdirenv); |
| 1158 | } |
| 1159 | |
| 1160 | if (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok)) |
| 1161 | goto out; |
| 1162 | |
| 1163 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ |
| 1164 | if (work_tree_env) { |
| 1165 | /* |
| 1166 | * The environment variable overrides "core.worktree". This |
| 1167 | * also has the consequence that we don't want to flag cases as |
| 1168 | * bogus where we have both "core.worktree" and "core.bare", so |
| 1169 | * we have to explicitly unset the configuration. |
| 1170 | */ |
| 1171 | FREE_AND_NULL(discovery->format.work_tree); |
| 1172 | repo_discovery_set_worktree(discovery, work_tree_env); |
| 1173 | } else if (discovery->format.is_bare > 0) { |
| 1174 | /* #18, #26 */ |
| 1175 | repo_discovery_set_gitdir(discovery, gitdirenv, 0); |
| 1176 | goto out; |
| 1177 | } else if (discovery->format.work_tree) { /* #6, #14 */ |
| 1178 | if (is_absolute_path(discovery->format.work_tree)) { |
| 1179 | repo_discovery_set_worktree(discovery, discovery->format.work_tree); |
| 1180 | } else { |
| 1181 | char *core_worktree; |
| 1182 | if (chdir(gitdirenv)) |
| 1183 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
| 1184 | if (chdir(discovery->format.work_tree)) |
| 1185 | die_errno(_("cannot chdir to '%s'"), discovery->format.work_tree); |
| 1186 | core_worktree = xgetcwd(); |
| 1187 | if (chdir(cwd->buf)) |
| 1188 | die_errno(_("cannot come back to cwd")); |
| 1189 | repo_discovery_set_worktree(discovery, core_worktree); |
| 1190 | free(core_worktree); |
| 1191 | } |
| 1192 | } else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
| 1193 | /* #16d */ |
| 1194 | repo_discovery_set_gitdir(discovery, gitdirenv, 0); |
| 1195 | goto out; |
| 1196 | } else { /* #2, #10 */ |
| 1197 | repo_discovery_set_worktree(discovery, "."); |
| 1198 | } |
| 1199 | |
| 1200 | /* both the worktree and cwd are already normalized */ |
| 1201 | if (!strcmp(cwd->buf, discovery->worktree)) { /* cwd == worktree */ |
| 1202 | repo_discovery_set_gitdir(discovery, gitdirenv, 0); |
| 1203 | goto out; |
| 1204 | } |
| 1205 | |
| 1206 | offset = dir_inside_of(cwd->buf, discovery->worktree); |
| 1207 | if (offset >= 0) { /* cwd inside discovery->worktree? */ |
| 1208 | repo_discovery_set_gitdir(discovery, gitdirenv, 1); |
| 1209 | if (chdir(discovery->worktree)) |
| 1210 | die_errno(_("cannot chdir to '%s'"), discovery->worktree); |
| 1211 | discovery->prefix = xstrfmt("%s/", cwd->buf + offset); |
| 1212 | goto out; |
| 1213 | } |
| 1214 | |
| 1215 | /* cwd outside worktree */ |
| 1216 | repo_discovery_set_gitdir(discovery, gitdirenv, 0); |
| 1217 | |
| 1218 | out: |
| 1219 | free(gitfile); |
| 1220 | } |
| 1221 | |
| 1222 | static void repo_discover_implicit_gitdir(struct repo_discovery *discovery, |
| 1223 | const char *gitdir, |
| 1224 | struct strbuf *cwd, int offset, |
| 1225 | int *nongit_ok) |
| 1226 | { |
| 1227 | if (read_and_verify_repository_format(&discovery->format, gitdir, nongit_ok)) |
| 1228 | return; |
| 1229 | |
| 1230 | /* --work-tree is set without --git-dir; use discovered one */ |
| 1231 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || discovery->format.work_tree) { |
| 1232 | char *to_free = NULL; |
| 1233 | |
| 1234 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
| 1235 | gitdir = to_free = real_pathdup(gitdir, 1); |
| 1236 | if (chdir(cwd->buf)) |
| 1237 | die_errno(_("cannot come back to cwd")); |
| 1238 | repo_discover_explicit_gitdir(discovery, gitdir, cwd, |
| 1239 | nongit_ok); |
| 1240 | free(to_free); |
| 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
| 1245 | if (discovery->format.is_bare > 0) { |
| 1246 | repo_discovery_set_gitdir(discovery, gitdir, (offset != cwd->len)); |
| 1247 | if (chdir(cwd->buf)) |
| 1248 | die_errno(_("cannot come back to cwd")); |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | /* #0, #1, #5, #8, #9, #12, #13 */ |
| 1253 | repo_discovery_set_worktree(discovery, "."); |
| 1254 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) |
| 1255 | repo_discovery_set_gitdir(discovery, gitdir, 0); |
| 1256 | if (offset >= cwd->len) |
| 1257 | return; |
| 1258 | |
| 1259 | /* Make "offset" point past the '/' (already the case for root dirs) */ |
| 1260 | if (offset != offset_1st_component(cwd->buf)) |
| 1261 | offset++; |
| 1262 | discovery->prefix = xstrfmt("%s/", cwd->buf + offset); |
| 1263 | } |
| 1264 | |
| 1265 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
| 1266 | static void repo_discover_bare_gitdir(struct repo_discovery *discovery, |
| 1267 | struct strbuf *cwd, int offset, |
| 1268 | int *nongit_ok) |
| 1269 | { |
| 1270 | int root_len; |
| 1271 | |
| 1272 | if (read_and_verify_repository_format(&discovery->format, ".", nongit_ok)) |
| 1273 | return; |
| 1274 | |
| 1275 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
| 1276 | |
| 1277 | /* --work-tree is set without --git-dir; use discovered one */ |
| 1278 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || discovery->format.work_tree) { |
| 1279 | char *gitdir = offset == cwd->len ? xstrdup(".") : xmemdupz(cwd->buf, offset); |
| 1280 | if (chdir(cwd->buf)) |
| 1281 | die_errno(_("cannot come back to cwd")); |
| 1282 | repo_discover_explicit_gitdir(discovery, gitdir, cwd, nongit_ok); |
| 1283 | free(gitdir); |
| 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | if (offset != cwd->len) { |
| 1288 | if (chdir(cwd->buf)) |
| 1289 | die_errno(_("cannot come back to cwd")); |
| 1290 | root_len = offset_1st_component(cwd->buf); |
| 1291 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); |
| 1292 | repo_discovery_set_gitdir(discovery, cwd->buf, 0); |
| 1293 | } else { |
| 1294 | repo_discovery_set_gitdir(discovery, ".", 0); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
| 1299 | { |
| 1300 | struct stat buf; |
| 1301 | if (stat(path, &buf)) { |
| 1302 | die_errno(_("failed to stat '%*s%s%s'"), |
| 1303 | prefix_len, |
| 1304 | prefix ? prefix : "", |
| 1305 | prefix ? "/" : "", path); |
| 1306 | } |
| 1307 | return buf.st_dev; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * A "string_list_each_func_t" function that canonicalizes an entry |
| 1312 | * from GIT_CEILING_DIRECTORIES using real_pathdup(), or |
| 1313 | * discards it if unusable. The presence of an empty entry in |
| 1314 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all |
| 1315 | * subsequent entries. |
| 1316 | */ |
| 1317 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
| 1318 | void *cb_data) |
| 1319 | { |
| 1320 | int *empty_entry_found = cb_data; |
| 1321 | char *ceil = item->string; |
| 1322 | |
| 1323 | if (!*ceil) { |
| 1324 | *empty_entry_found = 1; |
| 1325 | return 0; |
| 1326 | } else if (!is_absolute_path(ceil)) { |
| 1327 | return 0; |
| 1328 | } else if (*empty_entry_found) { |
| 1329 | /* Keep entry but do not canonicalize it */ |
| 1330 | return 1; |
| 1331 | } else { |
| 1332 | char *real_path = real_pathdup(ceil, 0); |
| 1333 | if (!real_path) { |
| 1334 | return 0; |
| 1335 | } |
| 1336 | free(item->string); |
| 1337 | item->string = real_path; |
| 1338 | return 1; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | struct safe_directory_data { |
| 1343 | char *path; |
| 1344 | int is_safe; |
| 1345 | }; |
| 1346 | |
| 1347 | static int safe_directory_cb(const char *key, const char *value, |
| 1348 | const struct config_context *ctx UNUSED, void *d) |
| 1349 | { |
| 1350 | struct safe_directory_data *data = d; |
| 1351 | |
| 1352 | if (strcmp(key, "safe.directory")) |
| 1353 | return 0; |
| 1354 | |
| 1355 | if (!value || !*value) { |
| 1356 | data->is_safe = 0; |
| 1357 | } else if (!strcmp(value, "*")) { |
| 1358 | data->is_safe = 1; |
| 1359 | } else { |
| 1360 | char *allowed = NULL; |
| 1361 | |
| 1362 | if (!git_config_pathname(&allowed, key, value) && allowed) { |
| 1363 | char *normalized = NULL; |
| 1364 | |
| 1365 | /* |
| 1366 | * Setting safe.directory to a non-absolute path |
| 1367 | * makes little sense---it won't be relative to |
| 1368 | * the configuration file the item is defined in. |
| 1369 | * Except for ".", which means "if we are at the top |
| 1370 | * level of a repository, then it is OK", which is |
| 1371 | * slightly tighter than "*" that allows discovery. |
| 1372 | */ |
| 1373 | if (!is_absolute_path(allowed) && strcmp(allowed, ".")) { |
| 1374 | warning(_("safe.directory '%s' not absolute"), |
| 1375 | allowed); |
| 1376 | goto next; |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * A .gitconfig in $HOME may be shared across |
| 1381 | * different machines and safe.directory entries |
| 1382 | * may or may not exist as paths on all of these |
| 1383 | * machines. In other words, it is not a warning |
| 1384 | * worthy event when there is no such path on this |
| 1385 | * machine---the entry may be useful elsewhere. |
| 1386 | */ |
| 1387 | normalized = real_pathdup(allowed, 0); |
| 1388 | if (!normalized) |
| 1389 | goto next; |
| 1390 | |
| 1391 | if (ends_with(normalized, "/*")) { |
| 1392 | size_t len = strlen(normalized); |
| 1393 | if (!fspathncmp(normalized, data->path, len - 1)) |
| 1394 | data->is_safe = 1; |
| 1395 | } else if (!fspathcmp(data->path, normalized)) { |
| 1396 | data->is_safe = 1; |
| 1397 | } |
| 1398 | next: |
| 1399 | free(normalized); |
| 1400 | free(allowed); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * Check if a repository is safe, by verifying the ownership of the |
| 1409 | * worktree (if any), the git directory, and the gitfile (if any). |
| 1410 | * |
| 1411 | * Exemptions for known-safe repositories can be added via `safe.directory` |
| 1412 | * config settings; for non-bare repositories, their worktree needs to be |
| 1413 | * added, for bare ones their git directory. |
| 1414 | */ |
| 1415 | static int ensure_valid_ownership(const char *gitfile, |
| 1416 | const char *worktree, const char *gitdir, |
| 1417 | struct strbuf *report) |
| 1418 | { |
| 1419 | struct safe_directory_data data = { 0 }; |
| 1420 | |
| 1421 | if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) && |
| 1422 | (!gitfile || is_path_owned_by_current_user(gitfile, report)) && |
| 1423 | (!worktree || is_path_owned_by_current_user(worktree, report)) && |
| 1424 | (!gitdir || is_path_owned_by_current_user(gitdir, report))) |
| 1425 | return 1; |
| 1426 | |
| 1427 | /* |
| 1428 | * normalize the data.path for comparison with normalized paths |
| 1429 | * that come from the configuration file. The path is unsafe |
| 1430 | * if it cannot be normalized. |
| 1431 | */ |
| 1432 | data.path = real_pathdup(worktree ? worktree : gitdir, 0); |
| 1433 | if (!data.path) |
| 1434 | return 0; |
| 1435 | |
| 1436 | /* |
| 1437 | * data.path is the "path" that identifies the repository and it is |
| 1438 | * constant regardless of what failed above. data.is_safe should be |
| 1439 | * initialized to false, and might be changed by the callback. |
| 1440 | */ |
| 1441 | git_protected_config(safe_directory_cb, &data); |
| 1442 | |
| 1443 | free(data.path); |
| 1444 | return data.is_safe; |
| 1445 | } |
| 1446 | |
| 1447 | void die_upon_dubious_ownership(const char *gitfile, const char *worktree, |
| 1448 | const char *gitdir) |
| 1449 | { |
| 1450 | struct strbuf report = STRBUF_INIT, quoted = STRBUF_INIT; |
| 1451 | const char *path; |
| 1452 | |
| 1453 | if (ensure_valid_ownership(gitfile, worktree, gitdir, &report)) |
| 1454 | return; |
| 1455 | |
| 1456 | strbuf_complete(&report, '\n'); |
| 1457 | path = gitfile ? gitfile : gitdir; |
| 1458 | sq_quote_buf_pretty("ed, path); |
| 1459 | |
| 1460 | die(_("detected dubious ownership in repository at '%s'\n" |
| 1461 | "%s" |
| 1462 | "To add an exception for this directory, call:\n" |
| 1463 | "\n" |
| 1464 | "\tgit config --global --add safe.directory %s"), |
| 1465 | path, report.buf, quoted.buf); |
| 1466 | } |
| 1467 | |
| 1468 | static int allowed_bare_repo_cb(const char *key, const char *value, |
| 1469 | const struct config_context *ctx UNUSED, |
| 1470 | void *d) |
| 1471 | { |
| 1472 | enum allowed_bare_repo *allowed_bare_repo = d; |
| 1473 | |
| 1474 | if (strcasecmp(key, "safe.bareRepository")) |
| 1475 | return 0; |
| 1476 | |
| 1477 | if (!strcmp(value, "explicit")) { |
| 1478 | *allowed_bare_repo = ALLOWED_BARE_REPO_EXPLICIT; |
| 1479 | return 0; |
| 1480 | } |
| 1481 | if (!strcmp(value, "all")) { |
| 1482 | *allowed_bare_repo = ALLOWED_BARE_REPO_ALL; |
| 1483 | return 0; |
| 1484 | } |
| 1485 | return -1; |
| 1486 | } |
| 1487 | |
| 1488 | static enum allowed_bare_repo get_allowed_bare_repo(void) |
| 1489 | { |
| 1490 | #ifdef WITH_BREAKING_CHANGES |
| 1491 | enum allowed_bare_repo result = ALLOWED_BARE_REPO_EXPLICIT; |
| 1492 | #else |
| 1493 | enum allowed_bare_repo result = ALLOWED_BARE_REPO_ALL; |
| 1494 | #endif |
| 1495 | git_protected_config(allowed_bare_repo_cb, &result); |
| 1496 | return result; |
| 1497 | } |
| 1498 | |
| 1499 | static const char *allowed_bare_repo_to_string( |
| 1500 | enum allowed_bare_repo allowed_bare_repo) |
| 1501 | { |
| 1502 | switch (allowed_bare_repo) { |
| 1503 | case ALLOWED_BARE_REPO_EXPLICIT: |
| 1504 | return "explicit"; |
| 1505 | case ALLOWED_BARE_REPO_ALL: |
| 1506 | return "all"; |
| 1507 | default: |
| 1508 | BUG("invalid allowed_bare_repo %d", |
| 1509 | allowed_bare_repo); |
| 1510 | } |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
| 1514 | static int is_implicit_bare_repo(const char *path) |
| 1515 | { |
| 1516 | /* |
| 1517 | * what we found is a ".git" directory at the root of |
| 1518 | * the working tree. |
| 1519 | */ |
| 1520 | if (ends_with_path_components(path, ".git")) |
| 1521 | return 1; |
| 1522 | |
| 1523 | /* |
| 1524 | * we are inside $GIT_DIR of a secondary worktree of a |
| 1525 | * non-bare repository. |
| 1526 | */ |
| 1527 | if (strstr(path, "/.git/worktrees/")) |
| 1528 | return 1; |
| 1529 | |
| 1530 | /* |
| 1531 | * we are inside $GIT_DIR of a worktree of a non-embedded |
| 1532 | * submodule, whose superproject is not a bare repository. |
| 1533 | */ |
| 1534 | if (strstr(path, "/.git/modules/")) |
| 1535 | return 1; |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * We cannot decide in this function whether we are in the work tree or |
| 1542 | * not, since the config can only be read _after_ this function was called. |
| 1543 | * |
| 1544 | * Also, we avoid changing any global state (such as the current working |
| 1545 | * directory) to allow early callers. |
| 1546 | * |
| 1547 | * The directory where the search should start needs to be passed in via the |
| 1548 | * `dir` parameter; upon return, the `dir` buffer will contain the path of |
| 1549 | * the directory where the search ended, and `gitdir` will contain the path of |
| 1550 | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it |
| 1551 | * is relative to `dir` (i.e. *not* necessarily the cwd). |
| 1552 | */ |
| 1553 | static enum discovery_result repo_discovery_find_dir(struct strbuf *dir, |
| 1554 | struct strbuf *gitdir, |
| 1555 | struct strbuf *report, |
| 1556 | int die_on_error) |
| 1557 | { |
| 1558 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
| 1559 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
| 1560 | const char *gitdirenv; |
| 1561 | int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); |
| 1562 | dev_t current_device = 0; |
| 1563 | int one_filesystem = 1; |
| 1564 | |
| 1565 | /* |
| 1566 | * If GIT_DIR is set explicitly, we're not going |
| 1567 | * to do any discovery, but we still do repository |
| 1568 | * validation. |
| 1569 | */ |
| 1570 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
| 1571 | if (gitdirenv) { |
| 1572 | strbuf_addstr(gitdir, gitdirenv); |
| 1573 | return GIT_DIR_EXPLICIT; |
| 1574 | } |
| 1575 | |
| 1576 | if (env_ceiling_dirs) { |
| 1577 | int empty_entry_found = 0; |
| 1578 | static const char path_sep[] = { PATH_SEP, '\0' }; |
| 1579 | |
| 1580 | string_list_split(&ceiling_dirs, env_ceiling_dirs, path_sep, -1); |
| 1581 | filter_string_list(&ceiling_dirs, 0, |
| 1582 | canonicalize_ceiling_entry, &empty_entry_found); |
| 1583 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
| 1584 | string_list_clear(&ceiling_dirs, 0); |
| 1585 | } |
| 1586 | |
| 1587 | if (ceil_offset < 0) |
| 1588 | ceil_offset = min_offset - 2; |
| 1589 | |
| 1590 | if (min_offset && min_offset == dir->len && |
| 1591 | !is_dir_sep(dir->buf[min_offset - 1])) { |
| 1592 | strbuf_addch(dir, '/'); |
| 1593 | min_offset++; |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * Test in the following order (relative to the dir): |
| 1598 | * - .git (file containing "gitdir: <path>") |
| 1599 | * - .git/ |
| 1600 | * - ./ (bare) |
| 1601 | * - ../.git |
| 1602 | * - ../.git/ |
| 1603 | * - ../ (bare) |
| 1604 | * - ../../.git |
| 1605 | * etc. |
| 1606 | */ |
| 1607 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
| 1608 | if (one_filesystem) |
| 1609 | current_device = get_device_or_die(dir->buf, NULL, 0); |
| 1610 | for (;;) { |
| 1611 | int offset = dir->len, error_code = 0; |
| 1612 | char *gitdir_path = NULL; |
| 1613 | char *gitfile = NULL; |
| 1614 | |
| 1615 | if (offset > min_offset) |
| 1616 | strbuf_addch(dir, '/'); |
| 1617 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); |
| 1618 | gitdirenv = read_gitfile_gently(dir->buf, &error_code); |
| 1619 | if (!gitdirenv) { |
| 1620 | switch (error_code) { |
| 1621 | case READ_GITFILE_ERR_MISSING: |
| 1622 | /* no .git in this directory, move on */ |
| 1623 | break; |
| 1624 | case READ_GITFILE_ERR_IS_A_DIR: |
| 1625 | if (is_git_directory(dir->buf)) { |
| 1626 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 1627 | gitdir_path = xstrdup(dir->buf); |
| 1628 | } |
| 1629 | break; |
| 1630 | case READ_GITFILE_ERR_STAT_FAILED: |
| 1631 | if (die_on_error) |
| 1632 | die(_("error reading '%s'"), dir->buf); |
| 1633 | else |
| 1634 | return GIT_DIR_INVALID_GITFILE; |
| 1635 | case READ_GITFILE_ERR_NOT_A_FILE: |
| 1636 | if (die_on_error) |
| 1637 | die(_("not a regular file: '%s'"), dir->buf); |
| 1638 | else |
| 1639 | return GIT_DIR_INVALID_GITFILE; |
| 1640 | default: |
| 1641 | if (die_on_error) |
| 1642 | read_gitfile_error_die(error_code, dir->buf); |
| 1643 | else |
| 1644 | return GIT_DIR_INVALID_GITFILE; |
| 1645 | } |
| 1646 | } else { |
| 1647 | gitfile = xstrdup(dir->buf); |
| 1648 | } |
| 1649 | /* |
| 1650 | * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT |
| 1651 | * to check that directory for a repository. |
| 1652 | * Now trim that tentative addition away, because we want to |
| 1653 | * focus on the real directory we are in. |
| 1654 | */ |
| 1655 | strbuf_setlen(dir, offset); |
| 1656 | if (gitdirenv) { |
| 1657 | enum discovery_result ret; |
| 1658 | const char *gitdir_candidate = |
| 1659 | gitdir_path ? gitdir_path : gitdirenv; |
| 1660 | |
| 1661 | if (ensure_valid_ownership(gitfile, dir->buf, |
| 1662 | gitdir_candidate, report)) { |
| 1663 | strbuf_addstr(gitdir, gitdirenv); |
| 1664 | ret = GIT_DIR_DISCOVERED; |
| 1665 | } else |
| 1666 | ret = GIT_DIR_INVALID_OWNERSHIP; |
| 1667 | |
| 1668 | /* |
| 1669 | * Earlier, during discovery, we might have allocated |
| 1670 | * string copies for gitdir_path or gitfile so make |
| 1671 | * sure we don't leak by freeing them now, before |
| 1672 | * leaving the loop and function. |
| 1673 | * |
| 1674 | * Note: gitdirenv will be non-NULL whenever these are |
| 1675 | * allocated, therefore we need not take care of releasing |
| 1676 | * them outside of this conditional block. |
| 1677 | */ |
| 1678 | free(gitdir_path); |
| 1679 | free(gitfile); |
| 1680 | |
| 1681 | return ret; |
| 1682 | } |
| 1683 | |
| 1684 | if (is_git_directory(dir->buf)) { |
| 1685 | trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf); |
| 1686 | if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT && |
| 1687 | !is_implicit_bare_repo(dir->buf)) |
| 1688 | return GIT_DIR_DISALLOWED_BARE; |
| 1689 | if (!ensure_valid_ownership(NULL, NULL, dir->buf, report)) |
| 1690 | return GIT_DIR_INVALID_OWNERSHIP; |
| 1691 | strbuf_addstr(gitdir, "."); |
| 1692 | return GIT_DIR_BARE; |
| 1693 | } |
| 1694 | |
| 1695 | if (offset <= min_offset) |
| 1696 | return GIT_DIR_HIT_CEILING; |
| 1697 | |
| 1698 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
| 1699 | ; /* continue */ |
| 1700 | if (offset <= ceil_offset) |
| 1701 | return GIT_DIR_HIT_CEILING; |
| 1702 | |
| 1703 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); |
| 1704 | if (one_filesystem && |
| 1705 | current_device != get_device_or_die(dir->buf, NULL, offset)) |
| 1706 | return GIT_DIR_HIT_MOUNT_POINT; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | enum discovery_result discover_git_directory_reason(struct strbuf *commondir, |
| 1711 | struct strbuf *gitdir) |
| 1712 | { |
| 1713 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; |
| 1714 | size_t gitdir_offset = gitdir->len, cwd_len; |
| 1715 | size_t commondir_offset = commondir->len; |
| 1716 | struct repository_format candidate = REPOSITORY_FORMAT_INIT; |
| 1717 | enum discovery_result result; |
| 1718 | |
| 1719 | if (strbuf_getcwd(&dir)) |
| 1720 | return GIT_DIR_CWD_FAILURE; |
| 1721 | |
| 1722 | cwd_len = dir.len; |
| 1723 | result = repo_discovery_find_dir(&dir, gitdir, NULL, 0); |
| 1724 | if (result <= 0) { |
| 1725 | strbuf_release(&dir); |
| 1726 | return result; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * The returned gitdir is relative to dir, and if dir does not reflect |
| 1731 | * the current working directory, we simply make the gitdir absolute. |
| 1732 | */ |
| 1733 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { |
| 1734 | /* Avoid a trailing "/." */ |
| 1735 | if (!strcmp(".", gitdir->buf + gitdir_offset)) |
| 1736 | strbuf_setlen(gitdir, gitdir_offset); |
| 1737 | else |
| 1738 | strbuf_addch(&dir, '/'); |
| 1739 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); |
| 1740 | } |
| 1741 | |
| 1742 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
| 1743 | |
| 1744 | strbuf_reset(&dir); |
| 1745 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
| 1746 | read_repository_format(&candidate, dir.buf); |
| 1747 | strbuf_release(&dir); |
| 1748 | |
| 1749 | if (verify_repository_format(&candidate, &err) < 0) { |
| 1750 | warning("ignoring git dir '%s': %s", |
| 1751 | gitdir->buf + gitdir_offset, err.buf); |
| 1752 | strbuf_release(&err); |
| 1753 | strbuf_setlen(commondir, commondir_offset); |
| 1754 | strbuf_setlen(gitdir, gitdir_offset); |
| 1755 | clear_repository_format(&candidate); |
| 1756 | return GIT_DIR_INVALID_FORMAT; |
| 1757 | } |
| 1758 | |
| 1759 | clear_repository_format(&candidate); |
| 1760 | return result; |
| 1761 | } |
| 1762 | |
| 1763 | int apply_repository_format(struct repository *repo, |
| 1764 | const struct repository_format *format, |
| 1765 | enum apply_repository_format_flags flags, |
| 1766 | struct strbuf *err) |
| 1767 | { |
| 1768 | char *object_directory = NULL, *alternate_object_directories = NULL; |
| 1769 | |
| 1770 | if (verify_repository_format(format, err) < 0) |
| 1771 | return -1; |
| 1772 | |
| 1773 | if (format->is_bare > 0 && format->work_tree) { |
| 1774 | /* #22.2, #30 */ |
| 1775 | warning("core.bare and core.worktree do not make sense"); |
| 1776 | repo->worktree_config_is_bogus = true; |
| 1777 | } |
| 1778 | |
| 1779 | if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) { |
| 1780 | const char *shallow_file; |
| 1781 | |
| 1782 | object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT)); |
| 1783 | alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT)); |
| 1784 | shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT); |
| 1785 | if (shallow_file) |
| 1786 | set_alternate_shallow_file(repo, shallow_file); |
| 1787 | } |
| 1788 | |
| 1789 | repo->bare_cfg = format->is_bare; |
| 1790 | repo_set_hash_algo(repo, format->hash_algo); |
| 1791 | repo->objects = odb_new(repo, object_directory, |
| 1792 | alternate_object_directories); |
| 1793 | repo_set_compat_hash_algo(repo, format->compat_hash_algo); |
| 1794 | repo_set_ref_storage_format(repo, |
| 1795 | format->ref_storage_format, |
| 1796 | format->ref_storage_payload); |
| 1797 | repo->repository_format_worktree_config = |
| 1798 | format->worktree_config; |
| 1799 | repo->repository_format_submodule_path_cfg = |
| 1800 | format->submodule_path_cfg; |
| 1801 | repo->repository_format_relative_worktrees = |
| 1802 | format->relative_worktrees; |
| 1803 | repo->repository_format_partial_clone = |
| 1804 | xstrdup_or_null(format->partial_clone); |
| 1805 | repo->repository_format_precious_objects = |
| 1806 | format->precious_objects; |
| 1807 | |
| 1808 | free(alternate_object_directories); |
| 1809 | free(object_directory); |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | const char *enter_repo(struct repository *repo, const char *path, unsigned flags) |
| 1814 | { |
| 1815 | static struct strbuf validated_path = STRBUF_INIT; |
| 1816 | static struct strbuf used_path = STRBUF_INIT; |
| 1817 | |
| 1818 | if (!path) |
| 1819 | return NULL; |
| 1820 | |
| 1821 | if (!(flags & ENTER_REPO_STRICT)) { |
| 1822 | static const char *suffix[] = { |
| 1823 | "/.git", "", ".git/.git", ".git", NULL, |
| 1824 | }; |
| 1825 | const char *gitfile; |
| 1826 | int len = strlen(path); |
| 1827 | int i; |
| 1828 | while ((1 < len) && (path[len-1] == '/')) |
| 1829 | len--; |
| 1830 | |
| 1831 | /* |
| 1832 | * We can handle arbitrary-sized buffers, but this remains as a |
| 1833 | * sanity check on untrusted input. |
| 1834 | */ |
| 1835 | if (PATH_MAX <= len) |
| 1836 | return NULL; |
| 1837 | |
| 1838 | strbuf_reset(&used_path); |
| 1839 | strbuf_reset(&validated_path); |
| 1840 | strbuf_add(&used_path, path, len); |
| 1841 | strbuf_add(&validated_path, path, len); |
| 1842 | |
| 1843 | if (used_path.buf[0] == '~') { |
| 1844 | char *newpath = interpolate_path(used_path.buf, 0); |
| 1845 | if (!newpath) |
| 1846 | return NULL; |
| 1847 | strbuf_attach(&used_path, newpath, strlen(newpath), |
| 1848 | strlen(newpath)); |
| 1849 | } |
| 1850 | for (i = 0; suffix[i]; i++) { |
| 1851 | struct stat st; |
| 1852 | size_t baselen = used_path.len; |
| 1853 | strbuf_addstr(&used_path, suffix[i]); |
| 1854 | if (!stat(used_path.buf, &st) && |
| 1855 | (S_ISREG(st.st_mode) || |
| 1856 | (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) { |
| 1857 | strbuf_addstr(&validated_path, suffix[i]); |
| 1858 | break; |
| 1859 | } |
| 1860 | strbuf_setlen(&used_path, baselen); |
| 1861 | } |
| 1862 | if (!suffix[i]) |
| 1863 | return NULL; |
| 1864 | gitfile = read_gitfile(used_path.buf); |
| 1865 | if (!(flags & ENTER_REPO_ANY_OWNER_OK)) |
| 1866 | die_upon_dubious_ownership(gitfile, NULL, used_path.buf); |
| 1867 | if (gitfile) { |
| 1868 | strbuf_reset(&used_path); |
| 1869 | strbuf_addstr(&used_path, gitfile); |
| 1870 | } |
| 1871 | if (chdir(used_path.buf)) |
| 1872 | return NULL; |
| 1873 | path = validated_path.buf; |
| 1874 | } |
| 1875 | else { |
| 1876 | const char *gitfile = read_gitfile(path); |
| 1877 | if (!(flags & ENTER_REPO_ANY_OWNER_OK)) |
| 1878 | die_upon_dubious_ownership(gitfile, NULL, path); |
| 1879 | if (gitfile) |
| 1880 | path = gitfile; |
| 1881 | if (chdir(path)) |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | |
| 1885 | if (is_git_directory(".")) { |
| 1886 | struct repository_format fmt = REPOSITORY_FORMAT_INIT; |
| 1887 | struct strbuf err = STRBUF_INIT; |
| 1888 | |
| 1889 | apply_and_export_relative_gitdir(repo, ".", 0); |
| 1890 | read_and_verify_repository_format(&fmt, ".", NULL); |
| 1891 | if (apply_repository_format(repo, &fmt, APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0) |
| 1892 | die("%s", err.buf); |
| 1893 | startup_info->have_repository = 1; |
| 1894 | |
| 1895 | clear_repository_format(&fmt); |
| 1896 | strbuf_release(&err); |
| 1897 | return path; |
| 1898 | } |
| 1899 | |
| 1900 | return NULL; |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * Note. This works only before you used a work tree. This was added |
| 1905 | * primarily to support git-clone to work in a new repository it just |
| 1906 | * created, and is not meant to flip between different work trees. |
| 1907 | */ |
| 1908 | static void set_git_work_tree(struct repository *repo, const char *new_work_tree) |
| 1909 | { |
| 1910 | if (repo->worktree_initialized) { |
| 1911 | struct strbuf realpath = STRBUF_INIT; |
| 1912 | |
| 1913 | strbuf_realpath(&realpath, new_work_tree, 1); |
| 1914 | new_work_tree = realpath.buf; |
| 1915 | if (strcmp(new_work_tree, repo->worktree)) |
| 1916 | die("internal error: work tree has already been set\n" |
| 1917 | "Current worktree: %s\nNew worktree: %s", |
| 1918 | repo->worktree, new_work_tree); |
| 1919 | strbuf_release(&realpath); |
| 1920 | return; |
| 1921 | } |
| 1922 | repo->worktree_initialized = true; |
| 1923 | repo_set_worktree(repo, new_work_tree); |
| 1924 | } |
| 1925 | |
| 1926 | static void repo_discover(struct repo_discovery *discovery, int *nongit_ok) |
| 1927 | { |
| 1928 | struct strbuf cwd = STRBUF_INIT; |
| 1929 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT; |
| 1930 | |
| 1931 | /* |
| 1932 | * Let's assume that we are in a git repository. |
| 1933 | * If it turns out later that we are somewhere else, the value will be |
| 1934 | * updated accordingly. |
| 1935 | */ |
| 1936 | if (nongit_ok) |
| 1937 | *nongit_ok = 0; |
| 1938 | |
| 1939 | if (strbuf_getcwd(&cwd)) |
| 1940 | die_errno(_("Unable to read current working directory")); |
| 1941 | strbuf_addbuf(&dir, &cwd); |
| 1942 | |
| 1943 | switch (repo_discovery_find_dir(&dir, &gitdir, &report, 1)) { |
| 1944 | case GIT_DIR_EXPLICIT: |
| 1945 | repo_discover_explicit_gitdir(discovery, gitdir.buf, &cwd, |
| 1946 | nongit_ok); |
| 1947 | break; |
| 1948 | case GIT_DIR_DISCOVERED: |
| 1949 | if (dir.len < cwd.len && chdir(dir.buf)) |
| 1950 | die(_("cannot change to '%s'"), dir.buf); |
| 1951 | repo_discover_implicit_gitdir(discovery, gitdir.buf, &cwd, dir.len, |
| 1952 | nongit_ok); |
| 1953 | break; |
| 1954 | case GIT_DIR_BARE: |
| 1955 | if (dir.len < cwd.len && chdir(dir.buf)) |
| 1956 | die(_("cannot change to '%s'"), dir.buf); |
| 1957 | repo_discover_bare_gitdir(discovery, &cwd, dir.len, nongit_ok); |
| 1958 | break; |
| 1959 | case GIT_DIR_HIT_CEILING: |
| 1960 | if (!nongit_ok) |
| 1961 | die(_("not a git repository (or any of the parent directories): %s"), |
| 1962 | DEFAULT_GIT_DIR_ENVIRONMENT); |
| 1963 | *nongit_ok = 1; |
| 1964 | break; |
| 1965 | case GIT_DIR_HIT_MOUNT_POINT: |
| 1966 | if (!nongit_ok) |
| 1967 | die(_("not a git repository (or any parent up to mount point %s)\n" |
| 1968 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), |
| 1969 | dir.buf); |
| 1970 | *nongit_ok = 1; |
| 1971 | break; |
| 1972 | case GIT_DIR_INVALID_OWNERSHIP: |
| 1973 | if (!nongit_ok) { |
| 1974 | struct strbuf quoted = STRBUF_INIT; |
| 1975 | |
| 1976 | strbuf_complete(&report, '\n'); |
| 1977 | sq_quote_buf_pretty("ed, dir.buf); |
| 1978 | die(_("detected dubious ownership in repository at '%s'\n" |
| 1979 | "%s" |
| 1980 | "To add an exception for this directory, call:\n" |
| 1981 | "\n" |
| 1982 | "\tgit config --global --add safe.directory %s"), |
| 1983 | dir.buf, report.buf, quoted.buf); |
| 1984 | } |
| 1985 | *nongit_ok = 1; |
| 1986 | break; |
| 1987 | case GIT_DIR_DISALLOWED_BARE: |
| 1988 | if (!nongit_ok) { |
| 1989 | die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"), |
| 1990 | dir.buf, |
| 1991 | allowed_bare_repo_to_string(get_allowed_bare_repo())); |
| 1992 | } |
| 1993 | *nongit_ok = 1; |
| 1994 | break; |
| 1995 | case GIT_DIR_CWD_FAILURE: |
| 1996 | case GIT_DIR_INVALID_FORMAT: |
| 1997 | /* |
| 1998 | * As a safeguard against repo_discovery_find_dir returning |
| 1999 | * these values, fallthrough to BUG. Otherwise it is possible to |
| 2000 | * set startup_info->have_repository to 1 when we did nothing to |
| 2001 | * find a repository. |
| 2002 | */ |
| 2003 | default: |
| 2004 | BUG("unhandled repo_discovery_find_dir() result"); |
| 2005 | } |
| 2006 | |
| 2007 | strbuf_release(&dir); |
| 2008 | strbuf_release(&cwd); |
| 2009 | strbuf_release(&gitdir); |
| 2010 | strbuf_release(&report); |
| 2011 | } |
| 2012 | |
| 2013 | const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok) |
| 2014 | { |
| 2015 | struct repo_discovery discovery = REPO_DISCOVERY_INIT; |
| 2016 | |
| 2017 | /* |
| 2018 | * We may have read an incomplete configuration before |
| 2019 | * setting-up the git directory. If so, clear the cache so |
| 2020 | * that the next queries to the configuration reload complete |
| 2021 | * configuration (including the per-repo config file that we |
| 2022 | * ignored previously). |
| 2023 | */ |
| 2024 | repo_config_clear(repo); |
| 2025 | |
| 2026 | repo_discover(&discovery, nongit_ok); |
| 2027 | |
| 2028 | /* |
| 2029 | * At this point, nongit_ok is stable. If it is non-NULL and points |
| 2030 | * to a non-zero value, then this means that we haven't found a |
| 2031 | * repository and that the caller expects startup_info to reflect |
| 2032 | * this. |
| 2033 | * |
| 2034 | * Regardless of the state of nongit_ok, the_repository->prefix and |
| 2035 | * the GIT_PREFIX environment variable must always match. For details |
| 2036 | * see Documentation/config/alias.adoc. |
| 2037 | */ |
| 2038 | if (nongit_ok && *nongit_ok) |
| 2039 | startup_info->have_repository = 0; |
| 2040 | else |
| 2041 | startup_info->have_repository = 1; |
| 2042 | |
| 2043 | /* |
| 2044 | * Not all paths through the setup code will have recorded a gitdir |
| 2045 | * above, so in order to guarantee that the environment is in a |
| 2046 | * consistent state after setup, explicitly set up the gitdir and |
| 2047 | * environment if we have a repository. |
| 2048 | * |
| 2049 | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some |
| 2050 | * code paths so we also need to explicitly setup the environment if |
| 2051 | * the user has set GIT_DIR. It may be beneficial to disallow bogus |
| 2052 | * GIT_DIR values at some point in the future. |
| 2053 | */ |
| 2054 | if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */ |
| 2055 | startup_info->have_repository || |
| 2056 | /* GIT_DIR_EXPLICIT */ |
| 2057 | getenv(GIT_DIR_ENVIRONMENT)) { |
| 2058 | if (discovery.worktree) |
| 2059 | set_git_work_tree(repo, discovery.worktree); |
| 2060 | |
| 2061 | if (discovery.gitdir) { |
| 2062 | apply_and_export_relative_gitdir(repo, discovery.gitdir, 0); |
| 2063 | } else { |
| 2064 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
| 2065 | if (!gitdir) |
| 2066 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 2067 | apply_gitdir_and_environment(repo, gitdir); |
| 2068 | } |
| 2069 | |
| 2070 | if (startup_info->have_repository) { |
| 2071 | struct strbuf err = STRBUF_INIT; |
| 2072 | const char *ref_backend_uri; |
| 2073 | |
| 2074 | /* |
| 2075 | * The env variable should override the repository config |
| 2076 | * for 'extensions.refStorage'. |
| 2077 | */ |
| 2078 | ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT); |
| 2079 | if (ref_backend_uri) { |
| 2080 | char *format; |
| 2081 | |
| 2082 | free(discovery.format.ref_storage_payload); |
| 2083 | |
| 2084 | parse_reference_uri(ref_backend_uri, &format, &discovery.format.ref_storage_payload); |
| 2085 | discovery.format.ref_storage_format = ref_storage_format_by_name(format); |
| 2086 | if (discovery.format.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN) |
| 2087 | die(_("unknown ref storage format: '%s'"), format); |
| 2088 | |
| 2089 | free(format); |
| 2090 | } |
| 2091 | |
| 2092 | if (apply_repository_format(repo, &discovery.format, |
| 2093 | APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0) |
| 2094 | die("%s", err.buf); |
| 2095 | |
| 2096 | clear_repository_format(&discovery.format); |
| 2097 | strbuf_release(&err); |
| 2098 | } |
| 2099 | } |
| 2100 | /* |
| 2101 | * Since precompose_string_if_needed() needs to look at |
| 2102 | * the core.precomposeunicode configuration, this |
| 2103 | * has to happen after the above block that finds |
| 2104 | * out where the repository is, i.e. a preparation |
| 2105 | * for calling repo_config_get_bool(). |
| 2106 | */ |
| 2107 | if (discovery.prefix) { |
| 2108 | const char *prefix = precompose_string_if_needed(discovery.prefix); |
| 2109 | repo->prefix = xstrdup(prefix); |
| 2110 | setenv(GIT_PREFIX_ENVIRONMENT, repo->prefix, 1); |
| 2111 | } else { |
| 2112 | FREE_AND_NULL(repo->prefix); |
| 2113 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); |
| 2114 | } |
| 2115 | |
| 2116 | setup_original_cwd(repo); |
| 2117 | |
| 2118 | repo_discovery_release(&discovery); |
| 2119 | return repo->prefix; |
| 2120 | } |
| 2121 | |
| 2122 | int git_config_perm(const char *var, const char *value) |
| 2123 | { |
| 2124 | int i; |
| 2125 | char *endptr; |
| 2126 | |
| 2127 | if (!value) |
| 2128 | return PERM_GROUP; |
| 2129 | |
| 2130 | if (!strcmp(value, "umask")) |
| 2131 | return PERM_UMASK; |
| 2132 | if (!strcmp(value, "group")) |
| 2133 | return PERM_GROUP; |
| 2134 | if (!strcmp(value, "all") || |
| 2135 | !strcmp(value, "world") || |
| 2136 | !strcmp(value, "everybody")) |
| 2137 | return PERM_EVERYBODY; |
| 2138 | |
| 2139 | /* Parse octal numbers */ |
| 2140 | i = strtol(value, &endptr, 8); |
| 2141 | |
| 2142 | /* If not an octal number, maybe true/false? */ |
| 2143 | if (*endptr != 0) |
| 2144 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; |
| 2145 | |
| 2146 | /* |
| 2147 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is |
| 2148 | * a chmod value to restrict to. |
| 2149 | */ |
| 2150 | switch (i) { |
| 2151 | case PERM_UMASK: /* 0 */ |
| 2152 | return PERM_UMASK; |
| 2153 | case OLD_PERM_GROUP: /* 1 */ |
| 2154 | return PERM_GROUP; |
| 2155 | case OLD_PERM_EVERYBODY: /* 2 */ |
| 2156 | return PERM_EVERYBODY; |
| 2157 | } |
| 2158 | |
| 2159 | /* A filemode value was given: 0xxx */ |
| 2160 | |
| 2161 | if ((i & 0600) != 0600) |
| 2162 | die(_("problem with core.sharedRepository filemode value " |
| 2163 | "(0%.3o).\nThe owner of files must always have " |
| 2164 | "read and write permissions."), i); |
| 2165 | |
| 2166 | /* |
| 2167 | * Mask filemode value. Others can not get write permission. |
| 2168 | * x flags for directories are handled separately. |
| 2169 | */ |
| 2170 | return -(i & 0666); |
| 2171 | } |
| 2172 | |
| 2173 | /* |
| 2174 | * Returns the "prefix", a path to the current working directory |
| 2175 | * relative to the work tree root, or NULL, if the current working |
| 2176 | * directory is not a strict subdirectory of the work tree root. The |
| 2177 | * prefix always ends with a '/' character. |
| 2178 | */ |
| 2179 | const char *setup_git_directory(struct repository *repo) |
| 2180 | { |
| 2181 | return setup_git_directory_gently(repo, NULL); |
| 2182 | } |
| 2183 | |
| 2184 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
| 2185 | { |
| 2186 | if (is_git_directory(suspect)) |
| 2187 | return suspect; |
| 2188 | return read_gitfile_gently(suspect, return_error_code); |
| 2189 | } |
| 2190 | |
| 2191 | /* if any standard file descriptor is missing open it to /dev/null */ |
| 2192 | void sanitize_stdfds(void) |
| 2193 | { |
| 2194 | int fd = xopen("/dev/null", O_RDWR); |
| 2195 | while (fd < 2) |
| 2196 | fd = xdup(fd); |
| 2197 | if (fd > 2) |
| 2198 | close(fd); |
| 2199 | } |
| 2200 | |
| 2201 | int daemonize(void) |
| 2202 | { |
| 2203 | #ifdef NO_POSIX_GOODIES |
| 2204 | errno = ENOSYS; |
| 2205 | return -1; |
| 2206 | #else |
| 2207 | pid_t parent_pid = getpid(); |
| 2208 | pid_t child_pid = fork(); |
| 2209 | |
| 2210 | switch (child_pid) { |
| 2211 | case 0: |
| 2212 | /* |
| 2213 | * We're in the child process, so we take ownership of |
| 2214 | * all tempfiles. |
| 2215 | */ |
| 2216 | reassign_tempfile_ownership(parent_pid, getpid()); |
| 2217 | break; |
| 2218 | case -1: |
| 2219 | die_errno(_("fork failed")); |
| 2220 | default: |
| 2221 | /* |
| 2222 | * We're in the parent process, so we drop ownership of |
| 2223 | * all tempfiles to prevent us from removing them upon |
| 2224 | * exit. |
| 2225 | */ |
| 2226 | reassign_tempfile_ownership(parent_pid, child_pid); |
| 2227 | exit(0); |
| 2228 | } |
| 2229 | if (setsid() == -1) |
| 2230 | die_errno(_("setsid failed")); |
| 2231 | close(0); |
| 2232 | close(1); |
| 2233 | close(2); |
| 2234 | sanitize_stdfds(); |
| 2235 | return 0; |
| 2236 | #endif |
| 2237 | } |
| 2238 | |
| 2239 | struct template_dir_cb_data { |
| 2240 | char *path; |
| 2241 | int initialized; |
| 2242 | }; |
| 2243 | |
| 2244 | static int template_dir_cb(const char *key, const char *value, |
| 2245 | const struct config_context *ctx UNUSED, void *d) |
| 2246 | { |
| 2247 | struct template_dir_cb_data *data = d; |
| 2248 | |
| 2249 | if (strcmp(key, "init.templatedir")) |
| 2250 | return 0; |
| 2251 | |
| 2252 | if (!value) { |
| 2253 | data->path = NULL; |
| 2254 | } else { |
| 2255 | char *path = NULL; |
| 2256 | |
| 2257 | FREE_AND_NULL(data->path); |
| 2258 | if (!git_config_pathname(&path, key, value)) |
| 2259 | data->path = path ? path : xstrdup(value); |
| 2260 | } |
| 2261 | |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | const char *get_template_dir(const char *option_template) |
| 2266 | { |
| 2267 | const char *template_dir = option_template; |
| 2268 | |
| 2269 | if (!template_dir) |
| 2270 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
| 2271 | if (!template_dir) { |
| 2272 | static struct template_dir_cb_data data; |
| 2273 | |
| 2274 | if (!data.initialized) { |
| 2275 | git_protected_config(template_dir_cb, &data); |
| 2276 | data.initialized = 1; |
| 2277 | } |
| 2278 | template_dir = data.path; |
| 2279 | } |
| 2280 | if (!template_dir) { |
| 2281 | static char *dir; |
| 2282 | |
| 2283 | if (!dir) |
| 2284 | dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); |
| 2285 | template_dir = dir; |
| 2286 | } |
| 2287 | return template_dir; |
| 2288 | } |
| 2289 | |
| 2290 | #ifdef NO_TRUSTABLE_FILEMODE |
| 2291 | #define TEST_FILEMODE 0 |
| 2292 | #else |
| 2293 | #define TEST_FILEMODE 1 |
| 2294 | #endif |
| 2295 | |
| 2296 | #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH" |
| 2297 | |
| 2298 | static void copy_templates_1(struct repository *repo, |
| 2299 | struct strbuf *path, |
| 2300 | struct strbuf *template_path, |
| 2301 | DIR *dir) |
| 2302 | { |
| 2303 | size_t path_baselen = path->len; |
| 2304 | size_t template_baselen = template_path->len; |
| 2305 | struct dirent *de; |
| 2306 | |
| 2307 | /* Note: if ".git/hooks" file exists in the repository being |
| 2308 | * re-initialized, /etc/core-git/templates/hooks/update would |
| 2309 | * cause "git init" to fail here. I think this is sane but |
| 2310 | * it means that the set of templates we ship by default, along |
| 2311 | * with the way the namespace under .git/ is organized, should |
| 2312 | * be really carefully chosen. |
| 2313 | */ |
| 2314 | safe_create_dir(repo, path->buf, 1); |
| 2315 | while ((de = readdir(dir)) != NULL) { |
| 2316 | struct stat st_git, st_template; |
| 2317 | int exists = 0; |
| 2318 | |
| 2319 | strbuf_setlen(path, path_baselen); |
| 2320 | strbuf_setlen(template_path, template_baselen); |
| 2321 | |
| 2322 | if (de->d_name[0] == '.') |
| 2323 | continue; |
| 2324 | strbuf_addstr(path, de->d_name); |
| 2325 | strbuf_addstr(template_path, de->d_name); |
| 2326 | if (lstat(path->buf, &st_git)) { |
| 2327 | if (errno != ENOENT) |
| 2328 | die_errno(_("cannot stat '%s'"), path->buf); |
| 2329 | } |
| 2330 | else |
| 2331 | exists = 1; |
| 2332 | |
| 2333 | if (lstat(template_path->buf, &st_template)) |
| 2334 | die_errno(_("cannot stat template '%s'"), template_path->buf); |
| 2335 | |
| 2336 | if (S_ISDIR(st_template.st_mode)) { |
| 2337 | DIR *subdir = opendir(template_path->buf); |
| 2338 | if (!subdir) |
| 2339 | die_errno(_("cannot opendir '%s'"), template_path->buf); |
| 2340 | strbuf_addch(path, '/'); |
| 2341 | strbuf_addch(template_path, '/'); |
| 2342 | copy_templates_1(repo, path, template_path, subdir); |
| 2343 | closedir(subdir); |
| 2344 | } |
| 2345 | else if (exists) |
| 2346 | continue; |
| 2347 | else if (S_ISLNK(st_template.st_mode)) { |
| 2348 | struct strbuf lnk = STRBUF_INIT; |
| 2349 | if (strbuf_readlink(&lnk, template_path->buf, |
| 2350 | st_template.st_size) < 0) |
| 2351 | die_errno(_("cannot readlink '%s'"), template_path->buf); |
| 2352 | if (symlink(lnk.buf, path->buf)) |
| 2353 | die_errno(_("cannot symlink '%s' '%s'"), |
| 2354 | lnk.buf, path->buf); |
| 2355 | strbuf_release(&lnk); |
| 2356 | } |
| 2357 | else if (S_ISREG(st_template.st_mode)) { |
| 2358 | if (copy_file(repo, path->buf, template_path->buf, st_template.st_mode)) |
| 2359 | die_errno(_("cannot copy '%s' to '%s'"), |
| 2360 | template_path->buf, path->buf); |
| 2361 | } |
| 2362 | else |
| 2363 | error(_("ignoring template %s"), template_path->buf); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | static void copy_templates(struct repository *repo, const char *option_template) |
| 2368 | { |
| 2369 | const char *template_dir = get_template_dir(option_template); |
| 2370 | struct strbuf path = STRBUF_INIT; |
| 2371 | struct strbuf template_path = STRBUF_INIT; |
| 2372 | size_t template_len; |
| 2373 | struct repository_format template_format = REPOSITORY_FORMAT_INIT; |
| 2374 | struct strbuf err = STRBUF_INIT; |
| 2375 | DIR *dir; |
| 2376 | char *to_free = NULL; |
| 2377 | |
| 2378 | if (!template_dir || !*template_dir) |
| 2379 | return; |
| 2380 | |
| 2381 | strbuf_addstr(&template_path, template_dir); |
| 2382 | strbuf_complete(&template_path, '/'); |
| 2383 | template_len = template_path.len; |
| 2384 | |
| 2385 | dir = opendir(template_path.buf); |
| 2386 | if (!dir) { |
| 2387 | warning(_("templates not found in %s"), template_dir); |
| 2388 | goto free_return; |
| 2389 | } |
| 2390 | |
| 2391 | /* Make sure that template is from the correct vintage */ |
| 2392 | strbuf_addstr(&template_path, "config"); |
| 2393 | read_repository_format(&template_format, template_path.buf); |
| 2394 | strbuf_setlen(&template_path, template_len); |
| 2395 | |
| 2396 | /* |
| 2397 | * No mention of version at all is OK, but anything else should be |
| 2398 | * verified. |
| 2399 | */ |
| 2400 | if (template_format.version >= 0 && |
| 2401 | verify_repository_format(&template_format, &err) < 0) { |
| 2402 | warning(_("not copying templates from '%s': %s"), |
| 2403 | template_dir, err.buf); |
| 2404 | strbuf_release(&err); |
| 2405 | goto close_free_return; |
| 2406 | } |
| 2407 | |
| 2408 | strbuf_addstr(&path, repo_get_common_dir(repo)); |
| 2409 | strbuf_complete(&path, '/'); |
| 2410 | copy_templates_1(repo, &path, &template_path, dir); |
| 2411 | close_free_return: |
| 2412 | closedir(dir); |
| 2413 | free_return: |
| 2414 | free(to_free); |
| 2415 | strbuf_release(&path); |
| 2416 | strbuf_release(&template_path); |
| 2417 | clear_repository_format(&template_format); |
| 2418 | } |
| 2419 | |
| 2420 | /* |
| 2421 | * If the git_dir is not directly inside the working tree, then git will not |
| 2422 | * find it by default, and we need to set the worktree explicitly. |
| 2423 | */ |
| 2424 | static int needs_work_tree_config(const char *git_dir, const char *work_tree) |
| 2425 | { |
| 2426 | if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git")) |
| 2427 | return 0; |
| 2428 | if (skip_prefix(git_dir, work_tree, &git_dir) && |
| 2429 | !strcmp(git_dir, "/.git")) |
| 2430 | return 0; |
| 2431 | return 1; |
| 2432 | } |
| 2433 | |
| 2434 | void initialize_repository_version(struct repository *repo, |
| 2435 | int hash_algo, |
| 2436 | enum ref_storage_format ref_storage_format, |
| 2437 | int reinit) |
| 2438 | { |
| 2439 | struct strbuf repo_version = STRBUF_INIT; |
| 2440 | int target_version = GIT_REPO_VERSION; |
| 2441 | int default_submodule_path_config = 0; |
| 2442 | |
| 2443 | /* |
| 2444 | * Note that we initialize the repository version to 1 when the ref |
| 2445 | * storage format is unknown. This is on purpose so that we can add the |
| 2446 | * correct object format to the config during git-clone(1). The format |
| 2447 | * version will get adjusted by git-clone(1) once it has learned about |
| 2448 | * the remote repository's format. |
| 2449 | */ |
| 2450 | if (hash_algo != GIT_HASH_SHA1_LEGACY || |
| 2451 | ref_storage_format != REF_STORAGE_FORMAT_FILES || |
| 2452 | repo->ref_storage_payload) |
| 2453 | target_version = GIT_REPO_VERSION_READ; |
| 2454 | |
| 2455 | if (hash_algo != GIT_HASH_SHA1_LEGACY && hash_algo != GIT_HASH_UNKNOWN) |
| 2456 | repo_config_set(repo, "extensions.objectformat", |
| 2457 | hash_algos[hash_algo].name); |
| 2458 | else if (reinit) |
| 2459 | repo_config_set_gently(repo, "extensions.objectformat", NULL); |
| 2460 | |
| 2461 | if (repo->ref_storage_payload) { |
| 2462 | struct strbuf ref_uri = STRBUF_INIT; |
| 2463 | |
| 2464 | strbuf_addf(&ref_uri, "%s://%s", |
| 2465 | ref_storage_format_to_name(ref_storage_format), |
| 2466 | repo->ref_storage_payload); |
| 2467 | repo_config_set(repo, "extensions.refstorage", ref_uri.buf); |
| 2468 | strbuf_release(&ref_uri); |
| 2469 | } else if (ref_storage_format != REF_STORAGE_FORMAT_FILES) { |
| 2470 | repo_config_set(repo, "extensions.refstorage", |
| 2471 | ref_storage_format_to_name(ref_storage_format)); |
| 2472 | } else if (reinit) { |
| 2473 | repo_config_set_gently(repo, "extensions.refstorage", NULL); |
| 2474 | } |
| 2475 | |
| 2476 | if (reinit) { |
| 2477 | struct strbuf config = STRBUF_INIT; |
| 2478 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
| 2479 | |
| 2480 | repo_common_path_append(repo, &config, "config"); |
| 2481 | read_repository_format(&repo_fmt, config.buf); |
| 2482 | |
| 2483 | if (repo_fmt.v1_only_extensions.nr) |
| 2484 | target_version = GIT_REPO_VERSION_READ; |
| 2485 | |
| 2486 | strbuf_release(&config); |
| 2487 | clear_repository_format(&repo_fmt); |
| 2488 | } |
| 2489 | |
| 2490 | repo_config_get_bool(repo, "init.defaultSubmodulePathConfig", |
| 2491 | &default_submodule_path_config); |
| 2492 | if (default_submodule_path_config) { |
| 2493 | /* extensions.submodulepathconfig requires at least version 1 */ |
| 2494 | if (target_version == 0) |
| 2495 | target_version = 1; |
| 2496 | repo_config_set(repo, "extensions.submodulepathconfig", "true"); |
| 2497 | } |
| 2498 | |
| 2499 | strbuf_addf(&repo_version, "%d", target_version); |
| 2500 | repo_config_set(repo, "core.repositoryformatversion", repo_version.buf); |
| 2501 | |
| 2502 | strbuf_release(&repo_version); |
| 2503 | } |
| 2504 | |
| 2505 | static int is_reinit(struct repository *repo) |
| 2506 | { |
| 2507 | struct strbuf buf = STRBUF_INIT; |
| 2508 | char junk[2]; |
| 2509 | int ret; |
| 2510 | |
| 2511 | repo_git_path_replace(repo, &buf, "HEAD"); |
| 2512 | ret = !access(buf.buf, R_OK) || readlink(buf.buf, junk, sizeof(junk) - 1) != -1; |
| 2513 | strbuf_release(&buf); |
| 2514 | return ret; |
| 2515 | } |
| 2516 | |
| 2517 | void create_reference_database(struct repository *repo, |
| 2518 | const char *initial_branch, int quiet) |
| 2519 | { |
| 2520 | struct strbuf err = STRBUF_INIT; |
| 2521 | char *to_free = NULL; |
| 2522 | int reinit = is_reinit(repo); |
| 2523 | |
| 2524 | if (ref_store_create_on_disk(get_main_ref_store(repo), 0, &err)) |
| 2525 | die("failed to set up refs db: %s", err.buf); |
| 2526 | |
| 2527 | /* |
| 2528 | * Point the HEAD symref to the initial branch with if HEAD does |
| 2529 | * not yet exist. |
| 2530 | */ |
| 2531 | if (!reinit) { |
| 2532 | char *ref; |
| 2533 | |
| 2534 | if (!initial_branch) |
| 2535 | initial_branch = to_free = |
| 2536 | repo_default_branch_name(repo, quiet); |
| 2537 | |
| 2538 | ref = xstrfmt("refs/heads/%s", initial_branch); |
| 2539 | if (check_refname_format(ref, 0) < 0) |
| 2540 | die(_("invalid initial branch name: '%s'"), |
| 2541 | initial_branch); |
| 2542 | |
| 2543 | if (refs_update_symref(get_main_ref_store(repo), "HEAD", ref, NULL) < 0) |
| 2544 | exit(1); |
| 2545 | free(ref); |
| 2546 | } |
| 2547 | |
| 2548 | if (reinit && initial_branch) |
| 2549 | warning(_("re-init: ignored --initial-branch=%s"), |
| 2550 | initial_branch); |
| 2551 | |
| 2552 | strbuf_release(&err); |
| 2553 | free(to_free); |
| 2554 | } |
| 2555 | |
| 2556 | static int create_default_files(struct repository *repo, |
| 2557 | const char *template_path, |
| 2558 | const char *original_git_dir, |
| 2559 | const struct repository_format *fmt, |
| 2560 | int init_shared_repository) |
| 2561 | { |
| 2562 | struct stat st1; |
| 2563 | struct strbuf path = STRBUF_INIT; |
| 2564 | int reinit; |
| 2565 | int filemode; |
| 2566 | const char *work_tree = repo_get_work_tree(repo); |
| 2567 | |
| 2568 | /* |
| 2569 | * First copy the templates -- we might have the default |
| 2570 | * config file there, in which case we would want to read |
| 2571 | * from it after installing. |
| 2572 | * |
| 2573 | * Before reading that config, we also need to clear out any cached |
| 2574 | * values (since we've just potentially changed what's available on |
| 2575 | * disk). |
| 2576 | */ |
| 2577 | copy_templates(repo, template_path); |
| 2578 | repo_config_clear(repo); |
| 2579 | repo_settings_reset_shared_repository(repo); |
| 2580 | repo_config(repo, git_default_config, NULL); |
| 2581 | |
| 2582 | reinit = is_reinit(repo); |
| 2583 | |
| 2584 | /* |
| 2585 | * We must make sure command-line options continue to override any |
| 2586 | * values we might have just re-read from the config. |
| 2587 | */ |
| 2588 | if (init_shared_repository != -1) |
| 2589 | repo_settings_set_shared_repository(repo, |
| 2590 | init_shared_repository); |
| 2591 | |
| 2592 | repo->bare_cfg = !work_tree; |
| 2593 | |
| 2594 | /* |
| 2595 | * We would have created the above under user's umask -- under |
| 2596 | * shared-repository settings, we would need to fix them up. |
| 2597 | */ |
| 2598 | if (repo_settings_get_shared_repository(repo)) { |
| 2599 | adjust_shared_perm(repo, repo_get_git_dir(repo)); |
| 2600 | } |
| 2601 | |
| 2602 | initialize_repository_version(repo, fmt->hash_algo, fmt->ref_storage_format, reinit); |
| 2603 | |
| 2604 | /* Check filemode trustability */ |
| 2605 | repo_git_path_replace(repo, &path, "config"); |
| 2606 | filemode = TEST_FILEMODE; |
| 2607 | if (TEST_FILEMODE && !lstat(path.buf, &st1)) { |
| 2608 | struct stat st2; |
| 2609 | filemode = (!chmod(path.buf, st1.st_mode ^ S_IXUSR) && |
| 2610 | !lstat(path.buf, &st2) && |
| 2611 | st1.st_mode != st2.st_mode && |
| 2612 | !chmod(path.buf, st1.st_mode)); |
| 2613 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) |
| 2614 | filemode = 0; |
| 2615 | } |
| 2616 | repo_config_set(repo, "core.filemode", filemode ? "true" : "false"); |
| 2617 | |
| 2618 | if (is_bare_repository(repo)) |
| 2619 | repo_config_set(repo, "core.bare", "true"); |
| 2620 | else { |
| 2621 | const char *value; |
| 2622 | |
| 2623 | repo_config_set(repo, "core.bare", "false"); |
| 2624 | |
| 2625 | /* allow template config file to override the default */ |
| 2626 | if (repo_config_get_string_tmp(repo, "core.logallrefupdates", &value)) |
| 2627 | repo_config_set(repo, "core.logallrefupdates", "true"); |
| 2628 | |
| 2629 | if (needs_work_tree_config(original_git_dir, work_tree)) |
| 2630 | repo_config_set(repo, "core.worktree", work_tree); |
| 2631 | } |
| 2632 | |
| 2633 | if (!reinit) { |
| 2634 | /* Check if symlink is supported in the work tree */ |
| 2635 | repo_git_path_replace(repo, &path, "tXXXXXX"); |
| 2636 | if (!close(xmkstemp(path.buf)) && |
| 2637 | !unlink(path.buf) && |
| 2638 | !symlink("testing", path.buf) && |
| 2639 | !lstat(path.buf, &st1) && |
| 2640 | S_ISLNK(st1.st_mode)) |
| 2641 | unlink(path.buf); /* good */ |
| 2642 | else |
| 2643 | repo_config_set(repo, "core.symlinks", "false"); |
| 2644 | |
| 2645 | /* Check if the filesystem is case-insensitive */ |
| 2646 | repo_git_path_replace(repo, &path, "CoNfIg"); |
| 2647 | if (!access(path.buf, F_OK)) |
| 2648 | repo_config_set(repo, "core.ignorecase", "true"); |
| 2649 | probe_utf8_pathname_composition(); |
| 2650 | } |
| 2651 | |
| 2652 | strbuf_release(&path); |
| 2653 | return reinit; |
| 2654 | } |
| 2655 | |
| 2656 | static void create_object_directory(struct repository *repo) |
| 2657 | { |
| 2658 | struct strbuf path = STRBUF_INIT; |
| 2659 | size_t baselen; |
| 2660 | |
| 2661 | strbuf_addstr(&path, repo_get_object_directory(repo)); |
| 2662 | baselen = path.len; |
| 2663 | |
| 2664 | safe_create_dir(repo, path.buf, 1); |
| 2665 | |
| 2666 | strbuf_setlen(&path, baselen); |
| 2667 | strbuf_addstr(&path, "/pack"); |
| 2668 | safe_create_dir(repo, path.buf, 1); |
| 2669 | |
| 2670 | strbuf_setlen(&path, baselen); |
| 2671 | strbuf_addstr(&path, "/info"); |
| 2672 | safe_create_dir(repo, path.buf, 1); |
| 2673 | |
| 2674 | strbuf_release(&path); |
| 2675 | } |
| 2676 | |
| 2677 | static void separate_git_dir(struct repository *repo, |
| 2678 | const char *git_dir, const char *git_link) |
| 2679 | { |
| 2680 | struct stat st; |
| 2681 | |
| 2682 | if (!stat(git_link, &st)) { |
| 2683 | const char *src; |
| 2684 | |
| 2685 | if (S_ISREG(st.st_mode)) |
| 2686 | src = read_gitfile(git_link); |
| 2687 | else if (S_ISDIR(st.st_mode)) |
| 2688 | src = git_link; |
| 2689 | else |
| 2690 | die(_("unable to handle file type %d"), (int)st.st_mode); |
| 2691 | |
| 2692 | if (rename(src, git_dir)) |
| 2693 | die_errno(_("unable to move %s to %s"), src, git_dir); |
| 2694 | repair_worktrees_after_gitdir_move(repo, src); |
| 2695 | } |
| 2696 | |
| 2697 | write_file(git_link, "gitdir: %s", git_dir); |
| 2698 | } |
| 2699 | |
| 2700 | struct default_format_config { |
| 2701 | int hash; |
| 2702 | enum ref_storage_format ref_format; |
| 2703 | }; |
| 2704 | |
| 2705 | static int read_default_format_config(const char *key, const char *value, |
| 2706 | const struct config_context *ctx UNUSED, |
| 2707 | void *payload) |
| 2708 | { |
| 2709 | struct default_format_config *cfg = payload; |
| 2710 | char *str = NULL; |
| 2711 | int ret; |
| 2712 | |
| 2713 | if (!strcmp(key, "init.defaultobjectformat")) { |
| 2714 | ret = git_config_string(&str, key, value); |
| 2715 | if (ret) |
| 2716 | goto out; |
| 2717 | cfg->hash = hash_algo_by_name(str); |
| 2718 | if (cfg->hash == GIT_HASH_UNKNOWN) |
| 2719 | warning(_("unknown hash algorithm '%s'"), str); |
| 2720 | goto out; |
| 2721 | } |
| 2722 | |
| 2723 | if (!strcmp(key, "init.defaultrefformat")) { |
| 2724 | ret = git_config_string(&str, key, value); |
| 2725 | if (ret) |
| 2726 | goto out; |
| 2727 | cfg->ref_format = ref_storage_format_by_name(str); |
| 2728 | if (cfg->ref_format == REF_STORAGE_FORMAT_UNKNOWN) |
| 2729 | warning(_("unknown ref storage format '%s'"), str); |
| 2730 | goto out; |
| 2731 | } |
| 2732 | |
| 2733 | /* |
| 2734 | * Enable the reftable format when "features.experimental" is enabled. |
| 2735 | * "init.defaultRefFormat" takes precedence over this setting. |
| 2736 | */ |
| 2737 | if (!strcmp(key, "feature.experimental") && |
| 2738 | cfg->ref_format == REF_STORAGE_FORMAT_UNKNOWN && |
| 2739 | git_config_bool(key, value)) { |
| 2740 | cfg->ref_format = REF_STORAGE_FORMAT_REFTABLE; |
| 2741 | ret = 0; |
| 2742 | goto out; |
| 2743 | } |
| 2744 | |
| 2745 | ret = 0; |
| 2746 | out: |
| 2747 | free(str); |
| 2748 | return ret; |
| 2749 | } |
| 2750 | |
| 2751 | static void repository_format_configure(struct repository_format *repo_fmt, |
| 2752 | int hash, enum ref_storage_format ref_format) |
| 2753 | { |
| 2754 | struct default_format_config cfg = { |
| 2755 | .hash = GIT_HASH_UNKNOWN, |
| 2756 | .ref_format = REF_STORAGE_FORMAT_UNKNOWN, |
| 2757 | }; |
| 2758 | struct config_options opts = { |
| 2759 | .respect_includes = 1, |
| 2760 | .ignore_repo = 1, |
| 2761 | .ignore_worktree = 1, |
| 2762 | }; |
| 2763 | const char *ref_backend_uri; |
| 2764 | const char *env; |
| 2765 | |
| 2766 | config_with_options(read_default_format_config, &cfg, NULL, NULL, &opts); |
| 2767 | |
| 2768 | /* |
| 2769 | * If we already have an initialized repo, don't allow the user to |
| 2770 | * specify a different algorithm, as that could cause corruption. |
| 2771 | * Otherwise, if the user has specified one on the command line, use it. |
| 2772 | */ |
| 2773 | env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT); |
| 2774 | if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo) |
| 2775 | die(_("attempt to reinitialize repository with different hash")); |
| 2776 | else if (hash != GIT_HASH_UNKNOWN) |
| 2777 | repo_fmt->hash_algo = hash; |
| 2778 | else if (env) { |
| 2779 | int env_algo = hash_algo_by_name(env); |
| 2780 | if (env_algo == GIT_HASH_UNKNOWN) |
| 2781 | die(_("unknown hash algorithm '%s'"), env); |
| 2782 | if (repo_fmt->version < 0 || |
| 2783 | repo_fmt->hash_algo == GIT_HASH_UNKNOWN) |
| 2784 | repo_fmt->hash_algo = env_algo; |
| 2785 | } else if (cfg.hash != GIT_HASH_UNKNOWN) { |
| 2786 | repo_fmt->hash_algo = cfg.hash; |
| 2787 | } |
| 2788 | |
| 2789 | env = getenv("GIT_DEFAULT_REF_FORMAT"); |
| 2790 | if (repo_fmt->version >= 0 && |
| 2791 | ref_format != REF_STORAGE_FORMAT_UNKNOWN && |
| 2792 | ref_format != repo_fmt->ref_storage_format) { |
| 2793 | die(_("attempt to reinitialize repository with different reference storage format")); |
| 2794 | } else if (ref_format != REF_STORAGE_FORMAT_UNKNOWN) { |
| 2795 | repo_fmt->ref_storage_format = ref_format; |
| 2796 | } else if (env) { |
| 2797 | ref_format = ref_storage_format_by_name(env); |
| 2798 | if (ref_format == REF_STORAGE_FORMAT_UNKNOWN) |
| 2799 | die(_("unknown ref storage format '%s'"), env); |
| 2800 | if (repo_fmt->version < 0 || |
| 2801 | repo_fmt->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN) |
| 2802 | repo_fmt->ref_storage_format = ref_format; |
| 2803 | } else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) { |
| 2804 | repo_fmt->ref_storage_format = cfg.ref_format; |
| 2805 | } else { |
| 2806 | repo_fmt->ref_storage_format = REF_STORAGE_FORMAT_DEFAULT; |
| 2807 | } |
| 2808 | |
| 2809 | |
| 2810 | ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT); |
| 2811 | if (ref_backend_uri) { |
| 2812 | char *backend, *payload; |
| 2813 | enum ref_storage_format format; |
| 2814 | |
| 2815 | parse_reference_uri(ref_backend_uri, &backend, &payload); |
| 2816 | format = ref_storage_format_by_name(backend); |
| 2817 | if (format == REF_STORAGE_FORMAT_UNKNOWN) |
| 2818 | die(_("unknown ref storage format: '%s'"), backend); |
| 2819 | |
| 2820 | repo_fmt->ref_storage_format = format; |
| 2821 | repo_fmt->ref_storage_payload = payload; |
| 2822 | |
| 2823 | free(backend); |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | int init_db(struct repository *repo, |
| 2828 | const char *git_dir, |
| 2829 | const char *real_git_dir, |
| 2830 | const char *worktree, |
| 2831 | const char *template_dir, int hash, |
| 2832 | enum ref_storage_format ref_storage_format, |
| 2833 | const char *initial_branch, |
| 2834 | int init_shared_repository, unsigned int flags) |
| 2835 | { |
| 2836 | int reinit; |
| 2837 | int exist_ok = flags & INIT_DB_EXIST_OK; |
| 2838 | char *original_git_dir = real_pathdup(git_dir, 1); |
| 2839 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
| 2840 | struct strbuf err = STRBUF_INIT; |
| 2841 | |
| 2842 | if (real_git_dir) { |
| 2843 | struct stat st; |
| 2844 | |
| 2845 | if (!exist_ok && !stat(git_dir, &st)) |
| 2846 | die(_("%s already exists"), git_dir); |
| 2847 | |
| 2848 | if (!exist_ok && !stat(real_git_dir, &st)) |
| 2849 | die(_("%s already exists"), real_git_dir); |
| 2850 | |
| 2851 | apply_and_export_relative_gitdir(repo, real_git_dir, 1); |
| 2852 | git_dir = repo_get_git_dir(repo); |
| 2853 | separate_git_dir(repo, git_dir, original_git_dir); |
| 2854 | } else { |
| 2855 | apply_and_export_relative_gitdir(repo, git_dir, 1); |
| 2856 | git_dir = repo_get_git_dir(repo); |
| 2857 | } |
| 2858 | |
| 2859 | if (worktree) |
| 2860 | set_git_work_tree(repo, worktree); |
| 2861 | |
| 2862 | /* |
| 2863 | * Check to see if the repository version is right. |
| 2864 | * Note that a newly created repository does not have |
| 2865 | * config file, so this will not fail. What we are catching |
| 2866 | * is an attempt to reinitialize new repository with an old tool. |
| 2867 | */ |
| 2868 | read_and_verify_repository_format(&repo_fmt, repo_get_git_dir(repo), NULL); |
| 2869 | repository_format_configure(&repo_fmt, hash, ref_storage_format); |
| 2870 | if (apply_repository_format(repo, &repo_fmt, APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0) |
| 2871 | die("%s", err.buf); |
| 2872 | startup_info->have_repository = 1; |
| 2873 | |
| 2874 | /* |
| 2875 | * Ensure `core.hidedotfiles` is processed. This must happen after we |
| 2876 | * have set up the repository format such that we can evaluate |
| 2877 | * includeIf conditions correctly in the case of re-initialization. |
| 2878 | */ |
| 2879 | repo_config(repo, git_default_core_config, NULL); |
| 2880 | |
| 2881 | safe_create_dir(repo, git_dir, 0); |
| 2882 | |
| 2883 | reinit = create_default_files(repo, template_dir, original_git_dir, |
| 2884 | &repo_fmt, init_shared_repository); |
| 2885 | |
| 2886 | if (!(flags & INIT_DB_SKIP_REFDB)) |
| 2887 | create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET); |
| 2888 | create_object_directory(repo); |
| 2889 | |
| 2890 | if (repo_settings_get_shared_repository(repo)) { |
| 2891 | char buf[10]; |
| 2892 | /* We do not spell "group" and such, so that |
| 2893 | * the configuration can be read by older version |
| 2894 | * of git. Note, we use octal numbers for new share modes, |
| 2895 | * and compatibility values for PERM_GROUP and |
| 2896 | * PERM_EVERYBODY. |
| 2897 | */ |
| 2898 | if (repo_settings_get_shared_repository(repo) < 0) |
| 2899 | /* force to the mode value */ |
| 2900 | xsnprintf(buf, sizeof(buf), "0%o", -repo_settings_get_shared_repository(repo)); |
| 2901 | else if (repo_settings_get_shared_repository(repo) == PERM_GROUP) |
| 2902 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); |
| 2903 | else if (repo_settings_get_shared_repository(repo) == PERM_EVERYBODY) |
| 2904 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); |
| 2905 | else |
| 2906 | BUG("invalid value for shared_repository"); |
| 2907 | repo_config_set(repo, "core.sharedrepository", buf); |
| 2908 | repo_config_set(repo, "receive.denyNonFastforwards", "true"); |
| 2909 | } |
| 2910 | |
| 2911 | if (!(flags & INIT_DB_QUIET)) { |
| 2912 | int len = strlen(git_dir); |
| 2913 | |
| 2914 | if (reinit) |
| 2915 | printf(repo_settings_get_shared_repository(repo) |
| 2916 | ? _("Reinitialized existing shared Git repository in %s%s\n") |
| 2917 | : _("Reinitialized existing Git repository in %s%s\n"), |
| 2918 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
| 2919 | else |
| 2920 | printf(repo_settings_get_shared_repository(repo) |
| 2921 | ? _("Initialized empty shared Git repository in %s%s\n") |
| 2922 | : _("Initialized empty Git repository in %s%s\n"), |
| 2923 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
| 2924 | } |
| 2925 | |
| 2926 | clear_repository_format(&repo_fmt); |
| 2927 | strbuf_release(&err); |
| 2928 | free(original_git_dir); |
| 2929 | return 0; |
| 2930 | } |