Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/odb.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / odb.c` 841 lines ยท 20962 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "abspath.h" 3 #include "commit-graph.h" 4 #include "config.h" 5 #include "environment.h" 6 #include "gettext.h" 7 #include "hashmap.h" 8 #include "hex.h" 9 #include "lockfile.h" 10 #include "loose.h" 11 #include "midx.h" 12 #include "object-file-convert.h" 13 #include "object-file.h" 14 #include "object-name.h" 15 #include "odb.h" 16 #include "odb/source-files.h" 17 #include "odb/source-inmemory.h" 18 #include "path.h" 19 #include "promisor-remote.h" 20 #include "replace-object.h" 21 #include "run-command.h" 22 #include "setup.h" 23 #include "strbuf.h" 24 #include "strvec.h" 25 #include "submodule.h" 26 #include "tmp-objdir.h" 27 #include "trace2.h" 28 #include "write-or-die.h" 29 30 static const char * const object_storage_name_by_format[] = { 31 [OBJECT_STORAGE_FORMAT_UNKNOWN] = "unknown", 32 [OBJECT_STORAGE_FORMAT_FILES] = "files", 33 }; 34 35 enum object_storage_format object_storage_format_by_name(const char *name) 36 { 37 for (size_t i = 0; i < ARRAY_SIZE(object_storage_name_by_format); i++) 38 if (!strcmp(object_storage_name_by_format[i], name)) 39 return i; 40 return OBJECT_STORAGE_FORMAT_UNKNOWN; 41 } 42 43 const char *object_storage_format_to_name(enum object_storage_format format) 44 { 45 if (format < 0 || format >= ARRAY_SIZE(object_storage_name_by_format)) 46 format = OBJECT_STORAGE_FORMAT_UNKNOWN; 47 return object_storage_name_by_format[format]; 48 } 49 50 enum object_storage_format object_storage_format_by_uri(const char *uri, char **payload) 51 { 52 enum object_storage_format format; 53 const char *schema_end; 54 char *name; 55 56 schema_end = strstr(uri, "://"); 57 if (!schema_end) { 58 name = xstrdup(uri); 59 if (payload) 60 *payload = NULL; 61 } else { 62 name = xstrndup(uri, schema_end - uri); 63 if (payload) 64 *payload = *(schema_end + 3) ? xstrdup(schema_end + 3) : NULL; 65 } 66 67 format = object_storage_format_by_name(name); 68 69 free(name); 70 return format; 71 } 72 73 int odb_mkstemp(struct object_database *odb, 74 struct strbuf *temp_filename, const char *pattern) 75 { 76 int fd; 77 /* 78 * we let the umask do its job, don't try to be more 79 * restrictive except to remove write permission. 80 */ 81 int mode = 0444; 82 repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern); 83 fd = git_mkstemp_mode(temp_filename->buf, mode); 84 if (0 <= fd) 85 return fd; 86 87 /* slow path */ 88 /* some mkstemp implementations erase temp_filename on failure */ 89 repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern); 90 safe_create_leading_directories(odb->repo, temp_filename->buf); 91 return xmkstemp_mode(temp_filename->buf, mode); 92 } 93 94 char *compute_alternate_path(const char *path, struct strbuf *err) 95 { 96 char *ref_git = NULL; 97 const char *repo; 98 int seen_error = 0; 99 100 ref_git = real_pathdup(path, 0); 101 if (!ref_git) { 102 seen_error = 1; 103 strbuf_addf(err, _("path '%s' does not exist"), path); 104 goto out; 105 } 106 107 repo = read_gitfile(ref_git); 108 if (!repo) 109 repo = read_gitfile(mkpath("%s/.git", ref_git)); 110 if (repo) { 111 free(ref_git); 112 ref_git = xstrdup(repo); 113 } 114 115 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) { 116 char *ref_git_git = mkpathdup("%s/.git", ref_git); 117 free(ref_git); 118 ref_git = ref_git_git; 119 } else if (!is_directory(mkpath("%s/objects", ref_git))) { 120 struct strbuf sb = STRBUF_INIT; 121 seen_error = 1; 122 if (get_common_dir(&sb, ref_git)) { 123 strbuf_addf(err, 124 _("reference repository '%s' as a linked " 125 "checkout is not supported yet."), 126 path); 127 goto out; 128 } 129 130 strbuf_addf(err, _("reference repository '%s' is not a " 131 "local repository."), path); 132 goto out; 133 } 134 135 if (!access(mkpath("%s/shallow", ref_git), F_OK)) { 136 strbuf_addf(err, _("reference repository '%s' is shallow"), 137 path); 138 seen_error = 1; 139 goto out; 140 } 141 142 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) { 143 strbuf_addf(err, 144 _("reference repository '%s' is grafted"), 145 path); 146 seen_error = 1; 147 goto out; 148 } 149 150 out: 151 if (seen_error) { 152 FREE_AND_NULL(ref_git); 153 } 154 155 return ref_git; 156 } 157 158 static void fill_alternate_refs_command(struct repository *repo, 159 struct child_process *cmd, 160 const char *repo_path) 161 { 162 const char *value; 163 164 if (!repo_config_get_value(repo, "core.alternateRefsCommand", &value)) { 165 cmd->use_shell = 1; 166 167 strvec_push(&cmd->args, value); 168 strvec_push(&cmd->args, repo_path); 169 } else { 170 cmd->git_cmd = 1; 171 172 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path); 173 strvec_push(&cmd->args, "for-each-ref"); 174 strvec_push(&cmd->args, "--format=%(objectname)"); 175 176 if (!repo_config_get_value(repo, "core.alternateRefsPrefixes", &value)) { 177 strvec_push(&cmd->args, "--"); 178 strvec_split(&cmd->args, value); 179 } 180 } 181 182 strvec_pushv(&cmd->env, (const char **)local_repo_env); 183 cmd->out = -1; 184 } 185 186 static void read_alternate_refs(struct repository *repo, 187 const char *path, 188 odb_for_each_alternate_ref_fn *cb, 189 void *payload) 190 { 191 struct child_process cmd = CHILD_PROCESS_INIT; 192 struct strbuf line = STRBUF_INIT; 193 FILE *fh; 194 195 fill_alternate_refs_command(repo, &cmd, path); 196 197 if (start_command(&cmd)) 198 return; 199 200 fh = xfdopen(cmd.out, "r"); 201 while (strbuf_getline_lf(&line, fh) != EOF) { 202 struct object_id oid; 203 const char *p; 204 205 if (parse_oid_hex_algop(line.buf, &oid, &p, repo->hash_algo) || *p) { 206 warning(_("invalid line while parsing alternate refs: %s"), 207 line.buf); 208 break; 209 } 210 211 cb(&oid, payload); 212 } 213 214 fclose(fh); 215 finish_command(&cmd); 216 strbuf_release(&line); 217 } 218 219 struct alternate_refs_data { 220 struct repository *repo; 221 odb_for_each_alternate_ref_fn *fn; 222 void *payload; 223 }; 224 225 static int refs_from_alternate_cb(struct odb_files_dir *alternate, 226 void *payload) 227 { 228 struct strbuf path = STRBUF_INIT; 229 size_t base_len; 230 struct alternate_refs_data *cb = payload; 231 232 if (!strbuf_realpath(&path, alternate->abspath, 0)) 233 goto out; 234 if (!strbuf_strip_suffix(&path, "/objects")) 235 goto out; 236 base_len = path.len; 237 238 /* Is this a git repository with refs? */ 239 strbuf_addstr(&path, "/refs"); 240 if (!is_directory(path.buf)) 241 goto out; 242 strbuf_setlen(&path, base_len); 243 244 /* A shallow or grafted repository may lack the history behind its refs. */ 245 strbuf_addstr(&path, "/shallow"); 246 if (!access(path.buf, F_OK)) 247 goto out; 248 strbuf_setlen(&path, base_len); 249 250 strbuf_addstr(&path, "/info/grafts"); 251 if (!access(path.buf, F_OK)) 252 goto out; 253 strbuf_setlen(&path, base_len); 254 255 read_alternate_refs(cb->repo, path.buf, cb->fn, cb->payload); 256 257 out: 258 strbuf_release(&path); 259 return 0; 260 } 261 262 void odb_for_each_alternate_ref(struct object_database *odb, 263 odb_for_each_alternate_ref_fn cb, void *payload) 264 { 265 struct alternate_refs_data data = { 266 .fn = cb, 267 .payload = payload, 268 .repo = odb->repo, 269 }; 270 odb_for_each_alternate(odb, refs_from_alternate_cb, &data); 271 } 272 273 int odb_for_each_alternate(struct object_database *odb, 274 odb_for_each_alternate_fn cb, void *payload) 275 { 276 struct odb_source_files *files = odb_source_files_downcast(odb->source); 277 int r = 0; 278 279 for (struct odb_files_dir *dir = files->dirs->next; dir; dir = dir->next) { 280 r = cb(dir, payload); 281 if (r) 282 break; 283 } 284 return r; 285 } 286 287 int odb_has_alternates(struct object_database *odb) 288 { 289 if (odb->source->type != ODB_SOURCE_FILES) 290 return 0; 291 return !!odb_source_files_downcast(odb->source)->dirs->next; 292 } 293 294 int obj_read_use_lock = 0; 295 pthread_mutex_t obj_read_mutex; 296 297 void enable_obj_read_lock(void) 298 { 299 if (obj_read_use_lock) 300 return; 301 302 obj_read_use_lock = 1; 303 init_recursive_mutex(&obj_read_mutex); 304 } 305 306 void disable_obj_read_lock(void) 307 { 308 if (!obj_read_use_lock) 309 return; 310 311 obj_read_use_lock = 0; 312 pthread_mutex_destroy(&obj_read_mutex); 313 } 314 315 static enum odb_read_status do_oid_object_info_extended(struct object_database *odb, 316 const struct object_id *oid, 317 struct object_info *oi, unsigned flags) 318 { 319 struct strbuf corrupt_err = STRBUF_INIT; 320 const struct object_id *real = oid; 321 enum odb_read_status ret; 322 323 if (flags & OBJECT_INFO_LOOKUP_REPLACE) 324 real = lookup_replace_object(odb->repo, oid); 325 326 if (is_null_oid(real)) 327 return -1; 328 329 if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags, NULL)) 330 return 0; 331 332 ret = odb_source_read_object_info(odb->source, real, oi, flags, 333 &corrupt_err); 334 if (ret == ODB_READ_NOT_FOUND && 335 odb->repo->fetch_if_missing && 336 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT) && 337 repo_has_promisor_remote(odb->repo)) { 338 /* Try to fetch the missing object from a promisor remote. */ 339 promisor_remote_get_direct(odb->repo, real, 1); 340 ret = odb_source_read_object_info(odb->source, real, oi, flags, 341 &corrupt_err); 342 } 343 344 if (ret < 0 && (flags & OBJECT_INFO_DIE_IF_CORRUPT)) { 345 if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid)) 346 die(_("replacement %s not found for %s"), 347 oid_to_hex(real), oid_to_hex(oid)); 348 if (ret != ODB_READ_NOT_FOUND) { 349 if (corrupt_err.len) 350 die("%s", corrupt_err.buf); 351 die(_("object %s is corrupt"), oid_to_hex(real)); 352 } 353 } 354 355 strbuf_release(&corrupt_err); 356 return ret; 357 } 358 359 static int oid_object_info_convert(struct repository *r, 360 const struct object_id *input_oid, 361 struct object_info *input_oi, unsigned flags) 362 { 363 const struct git_hash_algo *input_algo = &hash_algos[input_oid->algo]; 364 int do_die = flags & OBJECT_INFO_DIE_IF_CORRUPT; 365 enum object_type type; 366 struct object_id oid, delta_base_oid; 367 struct object_info new_oi, *oi; 368 size_t size; 369 void *content; 370 int ret; 371 372 if (repo_oid_to_algop(r, input_oid, r->hash_algo, &oid)) { 373 if (do_die) 374 die(_("missing mapping of %s to %s"), 375 oid_to_hex(input_oid), r->hash_algo->name); 376 return -1; 377 } 378 379 /* Is new_oi needed? */ 380 oi = input_oi; 381 if (input_oi && (input_oi->delta_base_oid || input_oi->sizep || 382 input_oi->contentp)) { 383 new_oi = *input_oi; 384 /* Does delta_base_oid need to be converted? */ 385 if (input_oi->delta_base_oid) 386 new_oi.delta_base_oid = &delta_base_oid; 387 /* Will the attributes differ when converted? */ 388 if (input_oi->sizep || input_oi->contentp) { 389 new_oi.contentp = &content; 390 new_oi.sizep = &size; 391 new_oi.typep = &type; 392 } 393 oi = &new_oi; 394 } 395 396 ret = odb_read_object_info_extended(r->objects, &oid, oi, flags); 397 if (ret) 398 return -1; 399 if (oi == input_oi) 400 return ret; 401 402 if (new_oi.contentp) { 403 struct strbuf outbuf = STRBUF_INIT; 404 405 if (type != OBJ_BLOB) { 406 ret = convert_object_file(r, &outbuf, 407 r->hash_algo, input_algo, 408 content, size, type, !do_die); 409 free(content); 410 if (ret == -1) 411 return -1; 412 size = outbuf.len; 413 content = strbuf_detach(&outbuf, NULL); 414 } 415 if (input_oi->sizep) 416 *input_oi->sizep = size; 417 if (input_oi->contentp) 418 *input_oi->contentp = content; 419 else 420 free(content); 421 if (input_oi->typep) 422 *input_oi->typep = type; 423 } 424 if (new_oi.delta_base_oid == &delta_base_oid) { 425 if (repo_oid_to_algop(r, &delta_base_oid, input_algo, 426 input_oi->delta_base_oid)) { 427 if (do_die) 428 die(_("missing mapping of %s to %s"), 429 oid_to_hex(&delta_base_oid), 430 input_algo->name); 431 return -1; 432 } 433 } 434 if (input_oi->source_infop) 435 *input_oi->source_infop = *new_oi.source_infop; 436 return ret; 437 } 438 439 enum odb_read_status odb_read_object_info_extended(struct object_database *odb, 440 const struct object_id *oid, 441 struct object_info *oi, 442 enum object_info_flags flags) 443 { 444 enum odb_read_status ret; 445 446 if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo)) 447 return oid_object_info_convert(odb->repo, oid, oi, flags); 448 449 obj_read_lock(); 450 ret = do_oid_object_info_extended(odb, oid, oi, flags); 451 obj_read_unlock(); 452 return ret; 453 } 454 455 456 /* returns enum object_type or negative */ 457 int odb_read_object_info(struct object_database *odb, 458 const struct object_id *oid, 459 size_t *sizep) 460 { 461 enum object_type type; 462 struct object_info oi = OBJECT_INFO_INIT; 463 464 oi.typep = &type; 465 oi.sizep = sizep; 466 if (odb_read_object_info_extended(odb, oid, &oi, 467 OBJECT_INFO_LOOKUP_REPLACE) < 0) 468 return -1; 469 return type; 470 } 471 472 int odb_pretend_object(struct object_database *odb, 473 void *buf, size_t len, enum object_type type, 474 struct object_id *oid) 475 { 476 hash_object_file(odb->repo->hash_algo, buf, len, type, oid); 477 if (odb_has_object(odb, oid, 0)) 478 return 0; 479 480 return odb_source_write_object(odb->inmemory_objects, 481 buf, len, type, oid, NULL, NULL, 0); 482 } 483 484 void *odb_read_object(struct object_database *odb, 485 const struct object_id *oid, 486 enum object_type *type, 487 size_t *size) 488 { 489 struct object_info oi = OBJECT_INFO_INIT; 490 unsigned flags = OBJECT_INFO_DIE_IF_CORRUPT | OBJECT_INFO_LOOKUP_REPLACE; 491 void *data; 492 493 oi.typep = type; 494 oi.sizep = size; 495 oi.contentp = &data; 496 if (odb_read_object_info_extended(odb, oid, &oi, flags)) 497 return NULL; 498 499 return data; 500 } 501 502 void *odb_read_object_peeled(struct object_database *odb, 503 const struct object_id *oid, 504 enum object_type required_type, 505 size_t *size, 506 struct object_id *actual_oid_return) 507 { 508 enum object_type type; 509 void *buffer; 510 size_t isize; 511 struct object_id actual_oid; 512 513 oidcpy(&actual_oid, oid); 514 while (1) { 515 int ref_length = -1; 516 const char *ref_type = NULL; 517 518 buffer = odb_read_object(odb, &actual_oid, &type, &isize); 519 if (!buffer) 520 return NULL; 521 if (type == required_type) { 522 *size = isize; 523 if (actual_oid_return) 524 oidcpy(actual_oid_return, &actual_oid); 525 return buffer; 526 } 527 /* Handle references */ 528 else if (type == OBJ_COMMIT) 529 ref_type = "tree "; 530 else if (type == OBJ_TAG) 531 ref_type = "object "; 532 else { 533 free(buffer); 534 return NULL; 535 } 536 ref_length = strlen(ref_type); 537 538 if (ref_length + odb->repo->hash_algo->hexsz > isize || 539 memcmp(buffer, ref_type, ref_length) || 540 get_oid_hex_algop((char *) buffer + ref_length, &actual_oid, 541 odb->repo->hash_algo)) { 542 free(buffer); 543 return NULL; 544 } 545 free(buffer); 546 /* Now we have the ID of the referred-to object in 547 * actual_oid. Check again. */ 548 } 549 } 550 551 int odb_has_object(struct object_database *odb, const struct object_id *oid, 552 enum odb_has_object_flags flags) 553 { 554 unsigned object_info_flags = 0; 555 556 if (!startup_info->have_repository) 557 return 0; 558 if (!(flags & ODB_HAS_OBJECT_RECHECK_PACKED)) 559 object_info_flags |= OBJECT_INFO_QUICK; 560 if (!(flags & ODB_HAS_OBJECT_FETCH_PROMISOR)) 561 object_info_flags |= OBJECT_INFO_SKIP_FETCH_OBJECT; 562 563 return odb_read_object_info_extended(odb, oid, NULL, object_info_flags) >= 0; 564 } 565 566 int odb_freshen_object(struct object_database *odb, 567 const struct object_id *oid) 568 { 569 return odb_source_freshen_object(odb->source, oid, NULL); 570 } 571 572 int odb_for_each_object_ext(struct object_database *odb, 573 const struct object_info *request, 574 odb_for_each_object_cb cb, 575 void *cb_data, 576 const struct odb_for_each_object_options *opts) 577 { 578 return odb_source_for_each_object(odb->source, request, cb, cb_data, opts); 579 } 580 581 int odb_for_each_object(struct object_database *odb, 582 const struct object_info *request, 583 odb_for_each_object_cb cb, 584 void *cb_data, 585 enum odb_for_each_object_flags flags) 586 { 587 struct odb_for_each_object_options opts = { 588 .flags = flags, 589 }; 590 return odb_for_each_object_ext(odb, request, cb, cb_data, &opts); 591 } 592 593 int odb_count_objects(struct object_database *odb, 594 enum odb_count_objects_flags flags, 595 unsigned long *out) 596 { 597 unsigned long count = 0; 598 int ret; 599 600 if (odb->object_count_valid && odb->object_count_flags == flags) { 601 *out = odb->object_count; 602 return 0; 603 } 604 605 ret = odb_source_count_objects(odb->source, flags, &count); 606 if (ret < 0) 607 goto out; 608 609 odb->object_count = count; 610 odb->object_count_valid = 1; 611 odb->object_count_flags = flags; 612 613 *out = count; 614 ret = 0; 615 616 out: 617 return ret; 618 } 619 620 /* 621 * Return the slot of the most-significant bit set in "val". There are various 622 * ways to do this quickly with fls() or __builtin_clzl(), but speed is 623 * probably not a big deal here. 624 */ 625 static unsigned msb(unsigned long val) 626 { 627 unsigned r = 0; 628 while (val >>= 1) 629 r++; 630 return r; 631 } 632 633 int odb_find_abbrev_len(struct object_database *odb, 634 const struct object_id *oid, 635 int min_length, 636 unsigned *out) 637 { 638 const struct git_hash_algo *algo = 639 oid->algo ? &hash_algos[oid->algo] : odb->repo->hash_algo; 640 const unsigned hexsz = algo->hexsz; 641 unsigned len; 642 int ret; 643 644 if (min_length < 0) { 645 unsigned long count; 646 647 if (odb_count_objects(odb, ODB_COUNT_OBJECTS_APPROXIMATE, &count) < 0) 648 count = 0; 649 650 /* 651 * Add one because the MSB only tells us the highest bit set, 652 * not including the value of all the _other_ bits (so "15" 653 * is only one off of 2^4, but the MSB is the 3rd bit. 654 */ 655 len = msb(count) + 1; 656 /* 657 * We now know we have on the order of 2^len objects, which 658 * expects a collision at 2^(len/2). But we also care about hex 659 * chars, not bits, and there are 4 bits per hex. So all 660 * together we need to divide by 2 and round up. 661 */ 662 len = DIV_ROUND_UP(len, 2); 663 /* 664 * For very small repos, we stick with our regular fallback. 665 */ 666 if (len < FALLBACK_DEFAULT_ABBREV) 667 len = FALLBACK_DEFAULT_ABBREV; 668 } else { 669 len = min_length; 670 } 671 672 if (len >= hexsz || !len) { 673 *out = hexsz; 674 ret = 0; 675 goto out; 676 } 677 678 ret = odb_source_find_abbrev_len(odb->source, oid, len, &len); 679 *out = len; 680 681 out: 682 return ret; 683 } 684 685 void odb_assert_oid_type(struct object_database *odb, 686 const struct object_id *oid, enum object_type expect) 687 { 688 enum object_type type = odb_read_object_info(odb, oid, NULL); 689 if (type < 0) 690 die(_("%s is not a valid object"), oid_to_hex(oid)); 691 if (type != expect) 692 die(_("%s is not a valid '%s' object"), oid_to_hex(oid), 693 type_name(expect)); 694 } 695 696 int odb_write_object_ext(struct object_database *odb, 697 const void *buf, unsigned long len, 698 enum object_type type, 699 struct object_id *oid, 700 const struct object_id *compat_oid_in, 701 enum odb_write_object_flags flags) 702 { 703 const struct git_hash_algo *compat = odb->repo->compat_hash_algo; 704 struct object_id compat_oid, *compat_oid_p = NULL; 705 706 hash_object_file(odb->repo->hash_algo, buf, len, type, oid); 707 708 /* 709 * We can skip the write in case we already have the object available. 710 * In that case, we only freshen its mtime. 711 */ 712 if (odb_freshen_object(odb, oid)) 713 return 0; 714 715 if (compat) { 716 const struct git_hash_algo *algo = odb->repo->hash_algo; 717 718 if (compat_oid_in) { 719 oidcpy(&compat_oid, compat_oid_in); 720 } else if (type == OBJ_BLOB) { 721 hash_object_file(compat, buf, len, type, &compat_oid); 722 } else { 723 struct strbuf converted = STRBUF_INIT; 724 convert_object_file(odb->repo, &converted, algo, compat, 725 buf, len, type, 0); 726 hash_object_file(compat, converted.buf, converted.len, 727 type, &compat_oid); 728 strbuf_release(&converted); 729 } 730 731 compat_oid_p = &compat_oid; 732 } 733 734 return odb_source_write_object(odb->source, buf, len, type, 735 oid, compat_oid_p, NULL, flags); 736 } 737 738 int odb_write_object_stream(struct object_database *odb, 739 struct odb_stream *stream, 740 struct object_id *oid) 741 { 742 return odb_source_write_object_stream(odb->source, stream, oid); 743 } 744 745 int odb_optimize(struct object_database *odb, 746 const struct odb_optimize_options *opts) 747 { 748 return odb_source_optimize(odb->source, opts); 749 } 750 751 bool odb_optimize_required(struct object_database *odb, 752 const struct odb_optimize_options *opts) 753 { 754 return odb_source_optimize_required(odb->source, opts); 755 } 756 757 void odb_generate_pack_options_release(struct odb_generate_pack_options *opts) 758 { 759 oid_array_clear(&opts->wants); 760 oid_array_clear(&opts->haves); 761 oid_array_clear(&opts->shallows); 762 } 763 764 int odb_generate_pack(struct object_database *odb, 765 struct odb_pack_generator **out, 766 const struct odb_generate_pack_options *opts) 767 { 768 if (!odb->source->generate_pack) 769 return error(_("primary object source does not support generating packfiles")); 770 return odb_source_generate_pack(odb->source, out, opts); 771 } 772 773 int odb_pack_generator_finish(struct odb_pack_generator *generator) 774 { 775 return generator->finish(generator); 776 } 777 778 struct object_database *odb_new(struct repository *repo, 779 enum odb_new_flags flags) 780 { 781 struct object_database *o; 782 783 CALLOC_ARRAY(o, 1); 784 o->repo = repo; 785 pthread_mutex_init(&o->replace_mutex, NULL); 786 787 o->source = odb_source_new(o, repo->object_storage_format, 788 repo->object_storage_payload, flags); 789 o->inmemory_objects = &odb_source_inmemory_new(o)->base; 790 791 return o; 792 } 793 794 void odb_close(struct object_database *o) 795 { 796 odb_source_close(o->source); 797 close_commit_graph(o); 798 } 799 800 static void odb_free_sources(struct object_database *o) 801 { 802 odb_source_free(o->source); 803 odb_source_free(o->inmemory_objects); 804 o->inmemory_objects = NULL; 805 } 806 807 void odb_free(struct object_database *o) 808 { 809 if (!o) 810 return; 811 812 oidmap_clear(&o->replace_map, 1); 813 pthread_mutex_destroy(&o->replace_mutex); 814 815 odb_close(o); 816 odb_free_sources(o); 817 818 free(o); 819 } 820 821 void odb_prepare(struct object_database *o, enum odb_prepare_flags flags) 822 { 823 obj_read_lock(); 824 825 if (flags & ODB_PREPARE_FLUSH_CACHES) 826 o->object_count_valid = 0; 827 828 odb_source_prepare(o->source, flags); 829 830 obj_read_unlock(); 831 } 832 833 void odb_reprepare(struct object_database *o) 834 { 835 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES); 836 } 837 838 int odb_fsck(struct object_database *odb, struct odb_fsck_options *options) 839 { 840 return odb_source_fsck(odb->source, options); 841 }