| 1 | #include "git-compat-util.h" |
| 2 | #include "abspath.h" |
| 3 | #include "commit-graph.h" |
| 4 | #include "config.h" |
| 5 | #include "dir.h" |
| 6 | #include "environment.h" |
| 7 | #include "gettext.h" |
| 8 | #include "hex.h" |
| 9 | #include "khash.h" |
| 10 | #include "lockfile.h" |
| 11 | #include "loose.h" |
| 12 | #include "midx.h" |
| 13 | #include "object-file-convert.h" |
| 14 | #include "object-file.h" |
| 15 | #include "object-name.h" |
| 16 | #include "odb.h" |
| 17 | #include "odb/source-inmemory.h" |
| 18 | #include "packfile.h" |
| 19 | #include "path.h" |
| 20 | #include "promisor-remote.h" |
| 21 | #include "quote.h" |
| 22 | #include "replace-object.h" |
| 23 | #include "run-command.h" |
| 24 | #include "setup.h" |
| 25 | #include "strbuf.h" |
| 26 | #include "strvec.h" |
| 27 | #include "submodule.h" |
| 28 | #include "tmp-objdir.h" |
| 29 | #include "trace2.h" |
| 30 | #include "write-or-die.h" |
| 31 | |
| 32 | KHASH_INIT(odb_path_map, const char * /* key: odb_path */, |
| 33 | struct odb_source *, 1, fspathhash, fspatheq) |
| 34 | |
| 35 | int odb_mkstemp(struct object_database *odb, |
| 36 | struct strbuf *temp_filename, const char *pattern) |
| 37 | { |
| 38 | int fd; |
| 39 | /* |
| 40 | * we let the umask do its job, don't try to be more |
| 41 | * restrictive except to remove write permission. |
| 42 | */ |
| 43 | int mode = 0444; |
| 44 | repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern); |
| 45 | fd = git_mkstemp_mode(temp_filename->buf, mode); |
| 46 | if (0 <= fd) |
| 47 | return fd; |
| 48 | |
| 49 | /* slow path */ |
| 50 | /* some mkstemp implementations erase temp_filename on failure */ |
| 51 | repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern); |
| 52 | safe_create_leading_directories(odb->repo, temp_filename->buf); |
| 53 | return xmkstemp_mode(temp_filename->buf, mode); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Return non-zero iff the path is usable as an alternate object database. |
| 58 | */ |
| 59 | static bool odb_is_source_usable(struct object_database *o, const char *path) |
| 60 | { |
| 61 | int r; |
| 62 | struct strbuf normalized_objdir = STRBUF_INIT; |
| 63 | bool usable = false; |
| 64 | |
| 65 | strbuf_realpath(&normalized_objdir, o->sources->path, 1); |
| 66 | |
| 67 | /* Detect cases where alternate disappeared */ |
| 68 | if (!is_directory(path)) { |
| 69 | error(_("object directory %s does not exist; " |
| 70 | "check .git/objects/info/alternates"), |
| 71 | path); |
| 72 | goto out; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Prevent the common mistake of listing the same |
| 77 | * thing twice, or object directory itself. |
| 78 | */ |
| 79 | if (!o->source_by_path) { |
| 80 | khiter_t p; |
| 81 | |
| 82 | o->source_by_path = kh_init_odb_path_map(); |
| 83 | assert(!o->sources->next); |
| 84 | p = kh_put_odb_path_map(o->source_by_path, o->sources->path, &r); |
| 85 | assert(r == 1); /* never used */ |
| 86 | kh_value(o->source_by_path, p) = o->sources; |
| 87 | } |
| 88 | |
| 89 | if (fspatheq(path, normalized_objdir.buf)) |
| 90 | goto out; |
| 91 | |
| 92 | if (kh_get_odb_path_map(o->source_by_path, path) < kh_end(o->source_by_path)) |
| 93 | goto out; |
| 94 | |
| 95 | usable = true; |
| 96 | |
| 97 | out: |
| 98 | strbuf_release(&normalized_objdir); |
| 99 | return usable; |
| 100 | } |
| 101 | |
| 102 | void parse_alternates(const char *string, |
| 103 | int sep, |
| 104 | const char *relative_base, |
| 105 | struct strvec *out) |
| 106 | { |
| 107 | struct strbuf pathbuf = STRBUF_INIT; |
| 108 | struct strbuf buf = STRBUF_INIT; |
| 109 | |
| 110 | if (!string || !*string) |
| 111 | return; |
| 112 | |
| 113 | while (*string) { |
| 114 | const char *end; |
| 115 | |
| 116 | strbuf_reset(&buf); |
| 117 | strbuf_reset(&pathbuf); |
| 118 | |
| 119 | if (*string == '#') { |
| 120 | /* comment; consume up to next separator */ |
| 121 | end = strchrnul(string, sep); |
| 122 | } else if (*string == '"' && !unquote_c_style(&buf, string, &end)) { |
| 123 | /* |
| 124 | * quoted path; unquote_c_style has copied the |
| 125 | * data for us and set "end". Broken quoting (e.g., |
| 126 | * an entry that doesn't end with a quote) falls |
| 127 | * back to the unquoted case below. |
| 128 | */ |
| 129 | } else { |
| 130 | /* normal, unquoted path */ |
| 131 | end = strchrnul(string, sep); |
| 132 | strbuf_add(&buf, string, end - string); |
| 133 | } |
| 134 | |
| 135 | if (*end) |
| 136 | end++; |
| 137 | string = end; |
| 138 | |
| 139 | if (!buf.len) |
| 140 | continue; |
| 141 | |
| 142 | if (!is_absolute_path(buf.buf) && relative_base) { |
| 143 | strbuf_realpath(&pathbuf, relative_base, 1); |
| 144 | strbuf_addch(&pathbuf, '/'); |
| 145 | } |
| 146 | strbuf_addbuf(&pathbuf, &buf); |
| 147 | |
| 148 | strbuf_reset(&buf); |
| 149 | if (!strbuf_realpath(&buf, pathbuf.buf, 0)) { |
| 150 | error(_("unable to normalize alternate object path: %s"), |
| 151 | pathbuf.buf); |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * The trailing slash after the directory name is given by |
| 157 | * this function at the end. Remove duplicates. |
| 158 | */ |
| 159 | while (buf.len && buf.buf[buf.len - 1] == '/') |
| 160 | strbuf_setlen(&buf, buf.len - 1); |
| 161 | |
| 162 | strvec_push(out, buf.buf); |
| 163 | } |
| 164 | |
| 165 | strbuf_release(&pathbuf); |
| 166 | strbuf_release(&buf); |
| 167 | } |
| 168 | |
| 169 | static struct odb_source *odb_add_alternate_recursively(struct object_database *odb, |
| 170 | const char *source, |
| 171 | int depth) |
| 172 | { |
| 173 | struct odb_source *alternate = NULL; |
| 174 | struct strvec sources = STRVEC_INIT; |
| 175 | khiter_t pos; |
| 176 | int ret; |
| 177 | |
| 178 | if (!odb_is_source_usable(odb, source)) |
| 179 | goto error; |
| 180 | |
| 181 | alternate = odb_source_new(odb, source, false); |
| 182 | |
| 183 | /* add the alternate entry */ |
| 184 | *odb->sources_tail = alternate; |
| 185 | odb->sources_tail = &(alternate->next); |
| 186 | |
| 187 | pos = kh_put_odb_path_map(odb->source_by_path, alternate->path, &ret); |
| 188 | if (!ret) |
| 189 | BUG("source must not yet exist"); |
| 190 | kh_value(odb->source_by_path, pos) = alternate; |
| 191 | |
| 192 | /* recursively add alternates */ |
| 193 | odb_source_read_alternates(alternate, &sources); |
| 194 | if (sources.nr && depth + 1 > 5) { |
| 195 | error(_("%s: ignoring alternate object stores, nesting too deep"), |
| 196 | source); |
| 197 | } else { |
| 198 | for (size_t i = 0; i < sources.nr; i++) |
| 199 | odb_add_alternate_recursively(odb, sources.v[i], depth + 1); |
| 200 | } |
| 201 | |
| 202 | error: |
| 203 | strvec_clear(&sources); |
| 204 | return alternate; |
| 205 | } |
| 206 | |
| 207 | void odb_add_to_alternates_file(struct object_database *odb, |
| 208 | const char *dir) |
| 209 | { |
| 210 | int ret = odb_source_write_alternate(odb->sources, dir); |
| 211 | if (ret < 0) |
| 212 | die(NULL); |
| 213 | if (odb->loaded_alternates) |
| 214 | odb_add_alternate_recursively(odb, dir, 0); |
| 215 | } |
| 216 | |
| 217 | struct odb_source *odb_add_to_alternates_memory(struct object_database *odb, |
| 218 | const char *dir) |
| 219 | { |
| 220 | /* |
| 221 | * Make sure alternates are initialized, or else our entry may be |
| 222 | * overwritten when they are. |
| 223 | */ |
| 224 | odb_prepare_alternates(odb); |
| 225 | return odb_add_alternate_recursively(odb, dir, 0); |
| 226 | } |
| 227 | |
| 228 | struct odb_source *odb_set_temporary_primary_source(struct object_database *odb, |
| 229 | const char *dir, int will_destroy) |
| 230 | { |
| 231 | struct odb_source *source; |
| 232 | |
| 233 | /* |
| 234 | * Make sure alternates are initialized, or else our entry may be |
| 235 | * overwritten when they are. |
| 236 | */ |
| 237 | odb_prepare_alternates(odb); |
| 238 | |
| 239 | /* |
| 240 | * Make a new primary odb and link the old primary ODB in as an |
| 241 | * alternate |
| 242 | */ |
| 243 | source = odb_source_new(odb, dir, false); |
| 244 | |
| 245 | /* |
| 246 | * Disable ref updates while a temporary odb is active, since |
| 247 | * the objects in the database may roll back. |
| 248 | */ |
| 249 | odb->repo->disable_ref_updates = true; |
| 250 | source->will_destroy = will_destroy; |
| 251 | source->next = odb->sources; |
| 252 | odb->sources = source; |
| 253 | return source->next; |
| 254 | } |
| 255 | |
| 256 | void odb_restore_primary_source(struct object_database *odb, |
| 257 | struct odb_source *restore_source, |
| 258 | const char *old_path) |
| 259 | { |
| 260 | struct odb_source *cur_source = odb->sources; |
| 261 | |
| 262 | if (strcmp(old_path, cur_source->path)) |
| 263 | BUG("expected %s as primary object store; found %s", |
| 264 | old_path, cur_source->path); |
| 265 | |
| 266 | if (cur_source->next != restore_source) |
| 267 | BUG("we expect the old primary object store to be the first alternate"); |
| 268 | |
| 269 | odb->repo->disable_ref_updates = false; |
| 270 | odb->sources = restore_source; |
| 271 | odb_source_free(cur_source); |
| 272 | } |
| 273 | |
| 274 | char *compute_alternate_path(const char *path, struct strbuf *err) |
| 275 | { |
| 276 | char *ref_git = NULL; |
| 277 | const char *repo; |
| 278 | int seen_error = 0; |
| 279 | |
| 280 | ref_git = real_pathdup(path, 0); |
| 281 | if (!ref_git) { |
| 282 | seen_error = 1; |
| 283 | strbuf_addf(err, _("path '%s' does not exist"), path); |
| 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | repo = read_gitfile(ref_git); |
| 288 | if (!repo) |
| 289 | repo = read_gitfile(mkpath("%s/.git", ref_git)); |
| 290 | if (repo) { |
| 291 | free(ref_git); |
| 292 | ref_git = xstrdup(repo); |
| 293 | } |
| 294 | |
| 295 | if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) { |
| 296 | char *ref_git_git = mkpathdup("%s/.git", ref_git); |
| 297 | free(ref_git); |
| 298 | ref_git = ref_git_git; |
| 299 | } else if (!is_directory(mkpath("%s/objects", ref_git))) { |
| 300 | struct strbuf sb = STRBUF_INIT; |
| 301 | seen_error = 1; |
| 302 | if (get_common_dir(&sb, ref_git)) { |
| 303 | strbuf_addf(err, |
| 304 | _("reference repository '%s' as a linked " |
| 305 | "checkout is not supported yet."), |
| 306 | path); |
| 307 | goto out; |
| 308 | } |
| 309 | |
| 310 | strbuf_addf(err, _("reference repository '%s' is not a " |
| 311 | "local repository."), path); |
| 312 | goto out; |
| 313 | } |
| 314 | |
| 315 | if (!access(mkpath("%s/shallow", ref_git), F_OK)) { |
| 316 | strbuf_addf(err, _("reference repository '%s' is shallow"), |
| 317 | path); |
| 318 | seen_error = 1; |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) { |
| 323 | strbuf_addf(err, |
| 324 | _("reference repository '%s' is grafted"), |
| 325 | path); |
| 326 | seen_error = 1; |
| 327 | goto out; |
| 328 | } |
| 329 | |
| 330 | out: |
| 331 | if (seen_error) { |
| 332 | FREE_AND_NULL(ref_git); |
| 333 | } |
| 334 | |
| 335 | return ref_git; |
| 336 | } |
| 337 | |
| 338 | struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir) |
| 339 | { |
| 340 | struct odb_source *source; |
| 341 | char *obj_dir_real = real_pathdup(obj_dir, 1); |
| 342 | struct strbuf odb_path_real = STRBUF_INIT; |
| 343 | |
| 344 | odb_prepare_alternates(odb); |
| 345 | for (source = odb->sources; source; source = source->next) { |
| 346 | strbuf_realpath(&odb_path_real, source->path, 1); |
| 347 | if (!strcmp(obj_dir_real, odb_path_real.buf)) |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | free(obj_dir_real); |
| 352 | strbuf_release(&odb_path_real); |
| 353 | |
| 354 | return source; |
| 355 | } |
| 356 | |
| 357 | struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir) |
| 358 | { |
| 359 | struct odb_source *source = odb_find_source(odb, obj_dir); |
| 360 | if (!source) |
| 361 | die(_("could not find object directory matching %s"), obj_dir); |
| 362 | return source; |
| 363 | } |
| 364 | |
| 365 | void odb_add_submodule_source_by_path(struct object_database *odb, |
| 366 | const char *path) |
| 367 | { |
| 368 | string_list_insert(&odb->submodule_source_paths, path); |
| 369 | } |
| 370 | |
| 371 | static void fill_alternate_refs_command(struct repository *repo, |
| 372 | struct child_process *cmd, |
| 373 | const char *repo_path) |
| 374 | { |
| 375 | const char *value; |
| 376 | |
| 377 | if (!repo_config_get_value(repo, "core.alternateRefsCommand", &value)) { |
| 378 | cmd->use_shell = 1; |
| 379 | |
| 380 | strvec_push(&cmd->args, value); |
| 381 | strvec_push(&cmd->args, repo_path); |
| 382 | } else { |
| 383 | cmd->git_cmd = 1; |
| 384 | |
| 385 | strvec_pushf(&cmd->args, "--git-dir=%s", repo_path); |
| 386 | strvec_push(&cmd->args, "for-each-ref"); |
| 387 | strvec_push(&cmd->args, "--format=%(objectname)"); |
| 388 | |
| 389 | if (!repo_config_get_value(repo, "core.alternateRefsPrefixes", &value)) { |
| 390 | strvec_push(&cmd->args, "--"); |
| 391 | strvec_split(&cmd->args, value); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | strvec_pushv(&cmd->env, (const char **)local_repo_env); |
| 396 | cmd->out = -1; |
| 397 | } |
| 398 | |
| 399 | static void read_alternate_refs(struct repository *repo, |
| 400 | const char *path, |
| 401 | odb_for_each_alternate_ref_fn *cb, |
| 402 | void *payload) |
| 403 | { |
| 404 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 405 | struct strbuf line = STRBUF_INIT; |
| 406 | FILE *fh; |
| 407 | |
| 408 | fill_alternate_refs_command(repo, &cmd, path); |
| 409 | |
| 410 | if (start_command(&cmd)) |
| 411 | return; |
| 412 | |
| 413 | fh = xfdopen(cmd.out, "r"); |
| 414 | while (strbuf_getline_lf(&line, fh) != EOF) { |
| 415 | struct object_id oid; |
| 416 | const char *p; |
| 417 | |
| 418 | if (parse_oid_hex_algop(line.buf, &oid, &p, repo->hash_algo) || *p) { |
| 419 | warning(_("invalid line while parsing alternate refs: %s"), |
| 420 | line.buf); |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | cb(&oid, payload); |
| 425 | } |
| 426 | |
| 427 | fclose(fh); |
| 428 | finish_command(&cmd); |
| 429 | strbuf_release(&line); |
| 430 | } |
| 431 | |
| 432 | struct alternate_refs_data { |
| 433 | odb_for_each_alternate_ref_fn *fn; |
| 434 | void *payload; |
| 435 | }; |
| 436 | |
| 437 | static int refs_from_alternate_cb(struct odb_source *alternate, |
| 438 | void *payload) |
| 439 | { |
| 440 | struct strbuf path = STRBUF_INIT; |
| 441 | size_t base_len; |
| 442 | struct alternate_refs_data *cb = payload; |
| 443 | |
| 444 | if (!strbuf_realpath(&path, alternate->path, 0)) |
| 445 | goto out; |
| 446 | if (!strbuf_strip_suffix(&path, "/objects")) |
| 447 | goto out; |
| 448 | base_len = path.len; |
| 449 | |
| 450 | /* Is this a git repository with refs? */ |
| 451 | strbuf_addstr(&path, "/refs"); |
| 452 | if (!is_directory(path.buf)) |
| 453 | goto out; |
| 454 | strbuf_setlen(&path, base_len); |
| 455 | |
| 456 | read_alternate_refs(alternate->odb->repo, path.buf, cb->fn, cb->payload); |
| 457 | |
| 458 | out: |
| 459 | strbuf_release(&path); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | void odb_for_each_alternate_ref(struct object_database *odb, |
| 464 | odb_for_each_alternate_ref_fn cb, void *payload) |
| 465 | { |
| 466 | struct alternate_refs_data data; |
| 467 | data.fn = cb; |
| 468 | data.payload = payload; |
| 469 | odb_for_each_alternate(odb, refs_from_alternate_cb, &data); |
| 470 | } |
| 471 | |
| 472 | int odb_for_each_alternate(struct object_database *odb, |
| 473 | odb_for_each_alternate_fn cb, void *payload) |
| 474 | { |
| 475 | struct odb_source *alternate; |
| 476 | int r = 0; |
| 477 | |
| 478 | odb_prepare_alternates(odb); |
| 479 | for (alternate = odb->sources->next; alternate; alternate = alternate->next) { |
| 480 | r = cb(alternate, payload); |
| 481 | if (r) |
| 482 | break; |
| 483 | } |
| 484 | return r; |
| 485 | } |
| 486 | |
| 487 | void odb_prepare_alternates(struct object_database *odb) |
| 488 | { |
| 489 | struct strvec sources = STRVEC_INIT; |
| 490 | |
| 491 | if (odb->loaded_alternates) |
| 492 | return; |
| 493 | |
| 494 | parse_alternates(odb->alternate_db, PATH_SEP, NULL, &sources); |
| 495 | odb_source_read_alternates(odb->sources, &sources); |
| 496 | for (size_t i = 0; i < sources.nr; i++) |
| 497 | odb_add_alternate_recursively(odb, sources.v[i], 0); |
| 498 | |
| 499 | odb->loaded_alternates = 1; |
| 500 | |
| 501 | strvec_clear(&sources); |
| 502 | } |
| 503 | |
| 504 | int odb_has_alternates(struct object_database *odb) |
| 505 | { |
| 506 | odb_prepare_alternates(odb); |
| 507 | return !!odb->sources->next; |
| 508 | } |
| 509 | |
| 510 | int obj_read_use_lock = 0; |
| 511 | pthread_mutex_t obj_read_mutex; |
| 512 | |
| 513 | void enable_obj_read_lock(void) |
| 514 | { |
| 515 | if (obj_read_use_lock) |
| 516 | return; |
| 517 | |
| 518 | obj_read_use_lock = 1; |
| 519 | init_recursive_mutex(&obj_read_mutex); |
| 520 | } |
| 521 | |
| 522 | void disable_obj_read_lock(void) |
| 523 | { |
| 524 | if (!obj_read_use_lock) |
| 525 | return; |
| 526 | |
| 527 | obj_read_use_lock = 0; |
| 528 | pthread_mutex_destroy(&obj_read_mutex); |
| 529 | } |
| 530 | |
| 531 | int fetch_if_missing = 1; |
| 532 | |
| 533 | static int register_all_submodule_sources(struct object_database *odb) |
| 534 | { |
| 535 | int ret = odb->submodule_source_paths.nr; |
| 536 | |
| 537 | for (size_t i = 0; i < odb->submodule_source_paths.nr; i++) |
| 538 | odb_add_to_alternates_memory(odb, |
| 539 | odb->submodule_source_paths.items[i].string); |
| 540 | if (ret) { |
| 541 | string_list_clear(&odb->submodule_source_paths, 0); |
| 542 | trace2_data_intmax("submodule", odb->repo, |
| 543 | "register_all_submodule_sources/registered", ret); |
| 544 | if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0)) |
| 545 | BUG("register_all_submodule_sources() called"); |
| 546 | } |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | static int do_oid_object_info_extended(struct object_database *odb, |
| 551 | const struct object_id *oid, |
| 552 | struct object_info *oi, unsigned flags) |
| 553 | { |
| 554 | const struct object_id *real = oid; |
| 555 | int already_retried = 0; |
| 556 | |
| 557 | if (flags & OBJECT_INFO_LOOKUP_REPLACE) |
| 558 | real = lookup_replace_object(odb->repo, oid); |
| 559 | |
| 560 | if (is_null_oid(real)) |
| 561 | return -1; |
| 562 | |
| 563 | if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags)) |
| 564 | return 0; |
| 565 | |
| 566 | odb_prepare_alternates(odb); |
| 567 | |
| 568 | while (1) { |
| 569 | struct odb_source *source; |
| 570 | |
| 571 | for (source = odb->sources; source; source = source->next) |
| 572 | if (!odb_source_read_object_info(source, real, oi, flags)) |
| 573 | return 0; |
| 574 | |
| 575 | /* |
| 576 | * When the object hasn't been found we try a second read and |
| 577 | * tell the sources so. This may cause them to invalidate |
| 578 | * caches or reload on-disk state. |
| 579 | */ |
| 580 | if (!(flags & OBJECT_INFO_QUICK)) { |
| 581 | for (source = odb->sources; source; source = source->next) |
| 582 | if (!odb_source_read_object_info(source, real, oi, |
| 583 | flags | OBJECT_INFO_SECOND_READ)) |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * This might be an attempt at accessing a submodule object as |
| 589 | * if it were in main object store (having called |
| 590 | * `odb_add_submodule_source_by_path()` on that submodule's |
| 591 | * ODB). If any such ODBs exist, register them and try again. |
| 592 | */ |
| 593 | if (register_all_submodule_sources(odb)) |
| 594 | /* We added some alternates; retry */ |
| 595 | continue; |
| 596 | |
| 597 | /* Check if it is a missing object */ |
| 598 | if (fetch_if_missing && repo_has_promisor_remote(odb->repo) && |
| 599 | !already_retried && |
| 600 | !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) { |
| 601 | promisor_remote_get_direct(odb->repo, real, 1); |
| 602 | already_retried = 1; |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | if (flags & OBJECT_INFO_DIE_IF_CORRUPT) { |
| 607 | const struct packed_git *p; |
| 608 | if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid)) |
| 609 | die(_("replacement %s not found for %s"), |
| 610 | oid_to_hex(real), oid_to_hex(oid)); |
| 611 | if ((p = has_packed_and_bad(odb->repo, real))) |
| 612 | die(_("packed object %s (stored in %s) is corrupt"), |
| 613 | oid_to_hex(real), p->pack_name); |
| 614 | } |
| 615 | return -1; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static int oid_object_info_convert(struct repository *r, |
| 620 | const struct object_id *input_oid, |
| 621 | struct object_info *input_oi, unsigned flags) |
| 622 | { |
| 623 | const struct git_hash_algo *input_algo = &hash_algos[input_oid->algo]; |
| 624 | int do_die = flags & OBJECT_INFO_DIE_IF_CORRUPT; |
| 625 | enum object_type type; |
| 626 | struct object_id oid, delta_base_oid; |
| 627 | struct object_info new_oi, *oi; |
| 628 | size_t size; |
| 629 | void *content; |
| 630 | int ret; |
| 631 | |
| 632 | if (repo_oid_to_algop(r, input_oid, r->hash_algo, &oid)) { |
| 633 | if (do_die) |
| 634 | die(_("missing mapping of %s to %s"), |
| 635 | oid_to_hex(input_oid), r->hash_algo->name); |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | /* Is new_oi needed? */ |
| 640 | oi = input_oi; |
| 641 | if (input_oi && (input_oi->delta_base_oid || input_oi->sizep || |
| 642 | input_oi->contentp)) { |
| 643 | new_oi = *input_oi; |
| 644 | /* Does delta_base_oid need to be converted? */ |
| 645 | if (input_oi->delta_base_oid) |
| 646 | new_oi.delta_base_oid = &delta_base_oid; |
| 647 | /* Will the attributes differ when converted? */ |
| 648 | if (input_oi->sizep || input_oi->contentp) { |
| 649 | new_oi.contentp = &content; |
| 650 | new_oi.sizep = &size; |
| 651 | new_oi.typep = &type; |
| 652 | } |
| 653 | oi = &new_oi; |
| 654 | } |
| 655 | |
| 656 | ret = odb_read_object_info_extended(r->objects, &oid, oi, flags); |
| 657 | if (ret) |
| 658 | return -1; |
| 659 | if (oi == input_oi) |
| 660 | return ret; |
| 661 | |
| 662 | if (new_oi.contentp) { |
| 663 | struct strbuf outbuf = STRBUF_INIT; |
| 664 | |
| 665 | if (type != OBJ_BLOB) { |
| 666 | ret = convert_object_file(r, &outbuf, |
| 667 | r->hash_algo, input_algo, |
| 668 | content, size, type, !do_die); |
| 669 | free(content); |
| 670 | if (ret == -1) |
| 671 | return -1; |
| 672 | size = outbuf.len; |
| 673 | content = strbuf_detach(&outbuf, NULL); |
| 674 | } |
| 675 | if (input_oi->sizep) |
| 676 | *input_oi->sizep = size; |
| 677 | if (input_oi->contentp) |
| 678 | *input_oi->contentp = content; |
| 679 | else |
| 680 | free(content); |
| 681 | if (input_oi->typep) |
| 682 | *input_oi->typep = type; |
| 683 | } |
| 684 | if (new_oi.delta_base_oid == &delta_base_oid) { |
| 685 | if (repo_oid_to_algop(r, &delta_base_oid, input_algo, |
| 686 | input_oi->delta_base_oid)) { |
| 687 | if (do_die) |
| 688 | die(_("missing mapping of %s to %s"), |
| 689 | oid_to_hex(&delta_base_oid), |
| 690 | input_algo->name); |
| 691 | return -1; |
| 692 | } |
| 693 | } |
| 694 | input_oi->whence = new_oi.whence; |
| 695 | input_oi->u = new_oi.u; |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | int odb_read_object_info_extended(struct object_database *odb, |
| 700 | const struct object_id *oid, |
| 701 | struct object_info *oi, |
| 702 | enum object_info_flags flags) |
| 703 | { |
| 704 | int ret; |
| 705 | |
| 706 | if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo)) |
| 707 | return oid_object_info_convert(odb->repo, oid, oi, flags); |
| 708 | |
| 709 | obj_read_lock(); |
| 710 | ret = do_oid_object_info_extended(odb, oid, oi, flags); |
| 711 | obj_read_unlock(); |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | |
| 716 | /* returns enum object_type or negative */ |
| 717 | int odb_read_object_info(struct object_database *odb, |
| 718 | const struct object_id *oid, |
| 719 | size_t *sizep) |
| 720 | { |
| 721 | enum object_type type; |
| 722 | struct object_info oi = OBJECT_INFO_INIT; |
| 723 | |
| 724 | oi.typep = &type; |
| 725 | oi.sizep = sizep; |
| 726 | if (odb_read_object_info_extended(odb, oid, &oi, |
| 727 | OBJECT_INFO_LOOKUP_REPLACE) < 0) |
| 728 | return -1; |
| 729 | return type; |
| 730 | } |
| 731 | |
| 732 | int odb_pretend_object(struct object_database *odb, |
| 733 | void *buf, size_t len, enum object_type type, |
| 734 | struct object_id *oid) |
| 735 | { |
| 736 | hash_object_file(odb->repo->hash_algo, buf, len, type, oid); |
| 737 | if (odb_has_object(odb, oid, 0)) |
| 738 | return 0; |
| 739 | |
| 740 | return odb_source_write_object(odb->inmemory_objects, |
| 741 | buf, len, type, oid, NULL, 0); |
| 742 | } |
| 743 | |
| 744 | void *odb_read_object(struct object_database *odb, |
| 745 | const struct object_id *oid, |
| 746 | enum object_type *type, |
| 747 | size_t *size) |
| 748 | { |
| 749 | struct object_info oi = OBJECT_INFO_INIT; |
| 750 | unsigned flags = OBJECT_INFO_DIE_IF_CORRUPT | OBJECT_INFO_LOOKUP_REPLACE; |
| 751 | void *data; |
| 752 | |
| 753 | oi.typep = type; |
| 754 | oi.sizep = size; |
| 755 | oi.contentp = &data; |
| 756 | if (odb_read_object_info_extended(odb, oid, &oi, flags)) |
| 757 | return NULL; |
| 758 | |
| 759 | return data; |
| 760 | } |
| 761 | |
| 762 | void *odb_read_object_peeled(struct object_database *odb, |
| 763 | const struct object_id *oid, |
| 764 | enum object_type required_type, |
| 765 | size_t *size, |
| 766 | struct object_id *actual_oid_return) |
| 767 | { |
| 768 | enum object_type type; |
| 769 | void *buffer; |
| 770 | size_t isize; |
| 771 | struct object_id actual_oid; |
| 772 | |
| 773 | oidcpy(&actual_oid, oid); |
| 774 | while (1) { |
| 775 | int ref_length = -1; |
| 776 | const char *ref_type = NULL; |
| 777 | |
| 778 | buffer = odb_read_object(odb, &actual_oid, &type, &isize); |
| 779 | if (!buffer) |
| 780 | return NULL; |
| 781 | if (type == required_type) { |
| 782 | *size = isize; |
| 783 | if (actual_oid_return) |
| 784 | oidcpy(actual_oid_return, &actual_oid); |
| 785 | return buffer; |
| 786 | } |
| 787 | /* Handle references */ |
| 788 | else if (type == OBJ_COMMIT) |
| 789 | ref_type = "tree "; |
| 790 | else if (type == OBJ_TAG) |
| 791 | ref_type = "object "; |
| 792 | else { |
| 793 | free(buffer); |
| 794 | return NULL; |
| 795 | } |
| 796 | ref_length = strlen(ref_type); |
| 797 | |
| 798 | if (ref_length + odb->repo->hash_algo->hexsz > isize || |
| 799 | memcmp(buffer, ref_type, ref_length) || |
| 800 | get_oid_hex_algop((char *) buffer + ref_length, &actual_oid, |
| 801 | odb->repo->hash_algo)) { |
| 802 | free(buffer); |
| 803 | return NULL; |
| 804 | } |
| 805 | free(buffer); |
| 806 | /* Now we have the ID of the referred-to object in |
| 807 | * actual_oid. Check again. */ |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | int odb_has_object(struct object_database *odb, const struct object_id *oid, |
| 812 | enum odb_has_object_flags flags) |
| 813 | { |
| 814 | unsigned object_info_flags = 0; |
| 815 | |
| 816 | if (!startup_info->have_repository) |
| 817 | return 0; |
| 818 | if (!(flags & ODB_HAS_OBJECT_RECHECK_PACKED)) |
| 819 | object_info_flags |= OBJECT_INFO_QUICK; |
| 820 | if (!(flags & ODB_HAS_OBJECT_FETCH_PROMISOR)) |
| 821 | object_info_flags |= OBJECT_INFO_SKIP_FETCH_OBJECT; |
| 822 | |
| 823 | return odb_read_object_info_extended(odb, oid, NULL, object_info_flags) >= 0; |
| 824 | } |
| 825 | |
| 826 | int odb_freshen_object(struct object_database *odb, |
| 827 | const struct object_id *oid) |
| 828 | { |
| 829 | struct odb_source *source; |
| 830 | odb_prepare_alternates(odb); |
| 831 | for (source = odb->sources; source; source = source->next) |
| 832 | if (odb_source_freshen_object(source, oid)) |
| 833 | return 1; |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | int odb_for_each_object_ext(struct object_database *odb, |
| 838 | const struct object_info *request, |
| 839 | odb_for_each_object_cb cb, |
| 840 | void *cb_data, |
| 841 | const struct odb_for_each_object_options *opts) |
| 842 | { |
| 843 | int ret; |
| 844 | |
| 845 | odb_prepare_alternates(odb); |
| 846 | for (struct odb_source *source = odb->sources; source; source = source->next) { |
| 847 | if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local) |
| 848 | continue; |
| 849 | |
| 850 | ret = odb_source_for_each_object(source, request, cb, cb_data, opts); |
| 851 | if (ret) |
| 852 | return ret; |
| 853 | } |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | int odb_for_each_object(struct object_database *odb, |
| 859 | const struct object_info *request, |
| 860 | odb_for_each_object_cb cb, |
| 861 | void *cb_data, |
| 862 | enum odb_for_each_object_flags flags) |
| 863 | { |
| 864 | struct odb_for_each_object_options opts = { |
| 865 | .flags = flags, |
| 866 | }; |
| 867 | return odb_for_each_object_ext(odb, request, cb, cb_data, &opts); |
| 868 | } |
| 869 | |
| 870 | int odb_count_objects(struct object_database *odb, |
| 871 | enum odb_count_objects_flags flags, |
| 872 | unsigned long *out) |
| 873 | { |
| 874 | struct odb_source *source; |
| 875 | unsigned long count = 0; |
| 876 | int ret; |
| 877 | |
| 878 | if (odb->object_count_valid && odb->object_count_flags == flags) { |
| 879 | *out = odb->object_count; |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | odb_prepare_alternates(odb); |
| 884 | for (source = odb->sources; source; source = source->next) { |
| 885 | unsigned long c; |
| 886 | |
| 887 | ret = odb_source_count_objects(source, flags, &c); |
| 888 | if (ret < 0) |
| 889 | goto out; |
| 890 | |
| 891 | count += c; |
| 892 | } |
| 893 | |
| 894 | odb->object_count = count; |
| 895 | odb->object_count_valid = 1; |
| 896 | odb->object_count_flags = flags; |
| 897 | |
| 898 | *out = count; |
| 899 | ret = 0; |
| 900 | |
| 901 | out: |
| 902 | return ret; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Return the slot of the most-significant bit set in "val". There are various |
| 907 | * ways to do this quickly with fls() or __builtin_clzl(), but speed is |
| 908 | * probably not a big deal here. |
| 909 | */ |
| 910 | static unsigned msb(unsigned long val) |
| 911 | { |
| 912 | unsigned r = 0; |
| 913 | while (val >>= 1) |
| 914 | r++; |
| 915 | return r; |
| 916 | } |
| 917 | |
| 918 | int odb_find_abbrev_len(struct object_database *odb, |
| 919 | const struct object_id *oid, |
| 920 | int min_length, |
| 921 | unsigned *out) |
| 922 | { |
| 923 | const struct git_hash_algo *algo = |
| 924 | oid->algo ? &hash_algos[oid->algo] : odb->repo->hash_algo; |
| 925 | const unsigned hexsz = algo->hexsz; |
| 926 | unsigned len; |
| 927 | int ret; |
| 928 | |
| 929 | if (min_length < 0) { |
| 930 | unsigned long count; |
| 931 | |
| 932 | if (odb_count_objects(odb, ODB_COUNT_OBJECTS_APPROXIMATE, &count) < 0) |
| 933 | count = 0; |
| 934 | |
| 935 | /* |
| 936 | * Add one because the MSB only tells us the highest bit set, |
| 937 | * not including the value of all the _other_ bits (so "15" |
| 938 | * is only one off of 2^4, but the MSB is the 3rd bit. |
| 939 | */ |
| 940 | len = msb(count) + 1; |
| 941 | /* |
| 942 | * We now know we have on the order of 2^len objects, which |
| 943 | * expects a collision at 2^(len/2). But we also care about hex |
| 944 | * chars, not bits, and there are 4 bits per hex. So all |
| 945 | * together we need to divide by 2 and round up. |
| 946 | */ |
| 947 | len = DIV_ROUND_UP(len, 2); |
| 948 | /* |
| 949 | * For very small repos, we stick with our regular fallback. |
| 950 | */ |
| 951 | if (len < FALLBACK_DEFAULT_ABBREV) |
| 952 | len = FALLBACK_DEFAULT_ABBREV; |
| 953 | } else { |
| 954 | len = min_length; |
| 955 | } |
| 956 | |
| 957 | if (len >= hexsz || !len) { |
| 958 | *out = hexsz; |
| 959 | ret = 0; |
| 960 | goto out; |
| 961 | } |
| 962 | |
| 963 | odb_prepare_alternates(odb); |
| 964 | for (struct odb_source *source = odb->sources; source; source = source->next) { |
| 965 | ret = odb_source_find_abbrev_len(source, oid, len, &len); |
| 966 | if (ret) |
| 967 | goto out; |
| 968 | } |
| 969 | |
| 970 | ret = 0; |
| 971 | *out = len; |
| 972 | |
| 973 | out: |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | void odb_assert_oid_type(struct object_database *odb, |
| 978 | const struct object_id *oid, enum object_type expect) |
| 979 | { |
| 980 | enum object_type type = odb_read_object_info(odb, oid, NULL); |
| 981 | if (type < 0) |
| 982 | die(_("%s is not a valid object"), oid_to_hex(oid)); |
| 983 | if (type != expect) |
| 984 | die(_("%s is not a valid '%s' object"), oid_to_hex(oid), |
| 985 | type_name(expect)); |
| 986 | } |
| 987 | |
| 988 | int odb_write_object_ext(struct object_database *odb, |
| 989 | const void *buf, unsigned long len, |
| 990 | enum object_type type, |
| 991 | struct object_id *oid, |
| 992 | struct object_id *compat_oid, |
| 993 | enum odb_write_object_flags flags) |
| 994 | { |
| 995 | return odb_source_write_object(odb->sources, buf, len, type, |
| 996 | oid, compat_oid, flags); |
| 997 | } |
| 998 | |
| 999 | int odb_write_object_stream(struct object_database *odb, |
| 1000 | struct odb_write_stream *stream, size_t len, |
| 1001 | struct object_id *oid) |
| 1002 | { |
| 1003 | return odb_source_write_object_stream(odb->sources, stream, len, oid); |
| 1004 | } |
| 1005 | |
| 1006 | struct object_database *odb_new(struct repository *repo, |
| 1007 | const char *primary_source, |
| 1008 | const char *secondary_sources) |
| 1009 | { |
| 1010 | struct object_database *o = xmalloc(sizeof(*o)); |
| 1011 | char *to_free = NULL; |
| 1012 | |
| 1013 | memset(o, 0, sizeof(*o)); |
| 1014 | o->repo = repo; |
| 1015 | pthread_mutex_init(&o->replace_mutex, NULL); |
| 1016 | string_list_init_dup(&o->submodule_source_paths); |
| 1017 | |
| 1018 | if (!primary_source) |
| 1019 | primary_source = to_free = xstrfmt("%s/objects", repo->commondir); |
| 1020 | o->sources = odb_source_new(o, primary_source, true); |
| 1021 | o->sources_tail = &o->sources->next; |
| 1022 | o->alternate_db = xstrdup_or_null(secondary_sources); |
| 1023 | o->inmemory_objects = &odb_source_inmemory_new(o)->base; |
| 1024 | |
| 1025 | free(to_free); |
| 1026 | |
| 1027 | return o; |
| 1028 | } |
| 1029 | |
| 1030 | void odb_close(struct object_database *o) |
| 1031 | { |
| 1032 | struct odb_source *source; |
| 1033 | for (source = o->sources; source; source = source->next) |
| 1034 | odb_source_close(source); |
| 1035 | close_commit_graph(o); |
| 1036 | } |
| 1037 | |
| 1038 | static void odb_free_sources(struct object_database *o) |
| 1039 | { |
| 1040 | while (o->sources) { |
| 1041 | struct odb_source *next; |
| 1042 | |
| 1043 | next = o->sources->next; |
| 1044 | odb_source_free(o->sources); |
| 1045 | o->sources = next; |
| 1046 | } |
| 1047 | |
| 1048 | odb_source_free(o->inmemory_objects); |
| 1049 | o->inmemory_objects = NULL; |
| 1050 | |
| 1051 | kh_destroy_odb_path_map(o->source_by_path); |
| 1052 | o->source_by_path = NULL; |
| 1053 | } |
| 1054 | |
| 1055 | void odb_free(struct object_database *o) |
| 1056 | { |
| 1057 | if (!o) |
| 1058 | return; |
| 1059 | |
| 1060 | free(o->alternate_db); |
| 1061 | |
| 1062 | oidmap_clear(&o->replace_map, 1); |
| 1063 | pthread_mutex_destroy(&o->replace_mutex); |
| 1064 | |
| 1065 | odb_close(o); |
| 1066 | odb_free_sources(o); |
| 1067 | |
| 1068 | string_list_clear(&o->submodule_source_paths, 0); |
| 1069 | |
| 1070 | free(o); |
| 1071 | } |
| 1072 | |
| 1073 | void odb_reprepare(struct object_database *o) |
| 1074 | { |
| 1075 | struct odb_source *source; |
| 1076 | |
| 1077 | obj_read_lock(); |
| 1078 | |
| 1079 | /* |
| 1080 | * Reprepare alt odbs, in case the alternates file was modified |
| 1081 | * during the course of this process. This only _adds_ odbs to |
| 1082 | * the linked list, so existing odbs will continue to exist for |
| 1083 | * the lifetime of the process. |
| 1084 | */ |
| 1085 | o->loaded_alternates = 0; |
| 1086 | odb_prepare_alternates(o); |
| 1087 | |
| 1088 | for (source = o->sources; source; source = source->next) |
| 1089 | odb_source_reprepare(source); |
| 1090 | |
| 1091 | o->object_count_valid = 0; |
| 1092 | |
| 1093 | obj_read_unlock(); |
| 1094 | } |