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