Git fork

Merge branch 'ps/commit-graph-wo-globals'

Remove dependency on the_repository and other globals from the
commit-graph code, and other changes unrelated to de-globaling.

* ps/commit-graph-wo-globals:
commit-graph: stop passing in redundant repository
commit-graph: stop using `the_repository`
commit-graph: stop using `the_hash_algo`
commit-graph: refactor `parse_commit_graph()` to take a repository
commit-graph: store the hash algorithm instead of its length
commit-graph: stop using `the_hash_algo` via macros

+157 -168
+5 -4
builtin/commit-graph.c
··· 109 109 opened = OPENED_GRAPH; 110 110 else if (errno != ENOENT) 111 111 die_errno(_("Could not open commit-graph '%s'"), graph_name); 112 - else if (open_commit_graph_chain(chain_name, &fd, &st)) 112 + else if (open_commit_graph_chain(chain_name, &fd, &st, 113 + the_repository->hash_algo)) 113 114 opened = OPENED_CHAIN; 114 115 else if (errno != ENOENT) 115 116 die_errno(_("could not open commit-graph chain '%s'"), chain_name); ··· 121 122 if (opened == OPENED_NONE) 122 123 return 0; 123 124 else if (opened == OPENED_GRAPH) 124 - graph = load_commit_graph_one_fd_st(the_repository, fd, &st, source); 125 + graph = load_commit_graph_one_fd_st(source, fd, &st); 125 126 else 126 - graph = load_commit_graph_chain_fd_st(the_repository, fd, &st, 127 + graph = load_commit_graph_chain_fd_st(the_repository->objects, fd, &st, 127 128 &incomplete_chain); 128 129 129 130 if (!graph) 130 131 return 1; 131 132 132 - ret = verify_commit_graph(the_repository, graph, flags); 133 + ret = verify_commit_graph(graph, flags); 133 134 free_commit_graph(graph); 134 135 135 136 if (incomplete_chain) {
+1 -1
builtin/commit.c
··· 1947 1947 "new index file. Check that disk is not full and quota is\n" 1948 1948 "not exceeded, and then \"git restore --staged :/\" to recover.")); 1949 1949 1950 - git_test_write_commit_graph_or_die(); 1950 + git_test_write_commit_graph_or_die(the_repository->objects->sources); 1951 1951 1952 1952 repo_rerere(the_repository, 0); 1953 1953 run_auto_maintenance(quiet);
+1 -1
builtin/merge.c
··· 1863 1863 if (squash) { 1864 1864 finish(head_commit, remoteheads, NULL, NULL); 1865 1865 1866 - git_test_write_commit_graph_or_die(); 1866 + git_test_write_commit_graph_or_die(the_repository->objects->sources); 1867 1867 } else 1868 1868 write_merge_state(remoteheads); 1869 1869
+137 -146
commit-graph.c
··· 1 - #define USE_THE_REPOSITORY_VARIABLE 2 1 #define DISABLE_SIGN_COMPARE_WARNINGS 3 2 4 3 #include "git-compat-util.h" ··· 29 28 #include "tree.h" 30 29 #include "chunk-format.h" 31 30 32 - void git_test_write_commit_graph_or_die(void) 31 + void git_test_write_commit_graph_or_die(struct odb_source *source) 33 32 { 34 33 int flags = 0; 35 34 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0)) ··· 38 37 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0)) 39 38 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS; 40 39 41 - if (write_commit_graph_reachable(the_repository->objects->sources, 42 - flags, NULL)) 40 + if (write_commit_graph_reachable(source, flags, NULL)) 43 41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH"); 44 42 } 45 43 ··· 54 52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */ 55 53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */ 56 54 57 - #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16) 58 - 59 55 #define GRAPH_VERSION_1 0x1 60 56 #define GRAPH_VERSION GRAPH_VERSION_1 61 57 ··· 67 63 68 64 #define GRAPH_HEADER_SIZE 8 69 65 #define GRAPH_FANOUT_SIZE (4 * 256) 70 - #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \ 71 - + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) 72 66 73 67 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31) 74 68 ··· 81 75 define_commit_slab(commit_pos, int); 82 76 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos); 83 77 78 + static size_t graph_data_width(const struct git_hash_algo *algop) 79 + { 80 + return algop->rawsz + 16; 81 + } 82 + 83 + static size_t graph_min_size(const struct git_hash_algo *algop) 84 + { 85 + return GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE + GRAPH_FANOUT_SIZE + algop->rawsz; 86 + } 87 + 84 88 static void set_commit_pos(struct repository *r, const struct object_id *oid) 85 89 { 86 90 static int32_t max_pos; ··· 249 253 return 1; 250 254 } 251 255 252 - struct commit_graph *load_commit_graph_one_fd_st(struct repository *r, 253 - int fd, struct stat *st, 254 - struct odb_source *source) 256 + struct commit_graph *load_commit_graph_one_fd_st(struct odb_source *source, 257 + int fd, struct stat *st) 255 258 { 256 259 void *graph_map; 257 260 size_t graph_size; ··· 259 262 260 263 graph_size = xsize_t(st->st_size); 261 264 262 - if (graph_size < GRAPH_MIN_SIZE) { 265 + if (graph_size < graph_min_size(source->odb->repo->hash_algo)) { 263 266 close(fd); 264 267 error(_("commit-graph file is too small")); 265 268 return NULL; 266 269 } 267 270 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); 268 271 close(fd); 269 - prepare_repo_settings(r); 270 - ret = parse_commit_graph(&r->settings, graph_map, graph_size); 271 272 273 + ret = parse_commit_graph(source->odb->repo, graph_map, graph_size); 272 274 if (ret) 273 275 ret->odb_source = source; 274 276 else ··· 306 308 { 307 309 struct commit_graph *g = data; 308 310 g->chunk_oid_lookup = chunk_start; 309 - if (chunk_size / g->hash_len != g->num_commits) 311 + if (chunk_size / g->hash_algo->rawsz != g->num_commits) 310 312 return error(_("commit-graph OID lookup chunk is the wrong size")); 311 313 return 0; 312 314 } ··· 315 317 size_t chunk_size, void *data) 316 318 { 317 319 struct commit_graph *g = data; 318 - if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits) 320 + if (chunk_size / graph_data_width(g->hash_algo) != g->num_commits) 319 321 return error(_("commit-graph commit data chunk is wrong size")); 320 322 g->chunk_commit_data = chunk_start; 321 323 return 0; ··· 368 370 return 0; 369 371 } 370 372 371 - struct commit_graph *parse_commit_graph(struct repo_settings *s, 373 + struct commit_graph *parse_commit_graph(struct repository *r, 372 374 void *graph_map, size_t graph_size) 373 375 { 374 376 const unsigned char *data; ··· 380 382 if (!graph_map) 381 383 return NULL; 382 384 383 - if (graph_size < GRAPH_MIN_SIZE) 385 + if (graph_size < graph_min_size(r->hash_algo)) 384 386 return NULL; 385 387 386 388 data = (const unsigned char *)graph_map; ··· 400 402 } 401 403 402 404 hash_version = *(unsigned char*)(data + 5); 403 - if (hash_version != oid_version(the_hash_algo)) { 405 + if (hash_version != oid_version(r->hash_algo)) { 404 406 error(_("commit-graph hash version %X does not match version %X"), 405 - hash_version, oid_version(the_hash_algo)); 407 + hash_version, oid_version(r->hash_algo)); 406 408 return NULL; 407 409 } 408 410 409 411 graph = alloc_commit_graph(); 410 412 411 - graph->hash_len = the_hash_algo->rawsz; 413 + graph->hash_algo = r->hash_algo; 412 414 graph->num_chunks = *(unsigned char*)(data + 6); 413 415 graph->data = graph_map; 414 416 graph->data_len = graph_size; 415 417 416 418 if (graph_size < GRAPH_HEADER_SIZE + 417 419 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE + 418 - GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) { 420 + GRAPH_FANOUT_SIZE + r->hash_algo->rawsz) { 419 421 error(_("commit-graph file is too small to hold %u chunks"), 420 422 graph->num_chunks); 421 423 free(graph); ··· 446 448 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs, 447 449 &graph->chunk_base_graphs_size); 448 450 449 - if (s->commit_graph_generation_version >= 2) { 451 + prepare_repo_settings(r); 452 + 453 + if (r->settings.commit_graph_generation_version >= 2) { 450 454 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA, 451 455 graph_read_generation_data, graph); 452 456 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW, ··· 457 461 graph->read_generation_data = 1; 458 462 } 459 463 460 - if (s->commit_graph_changed_paths_version) { 464 + if (r->settings.commit_graph_changed_paths_version) { 461 465 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES, 462 466 graph_read_bloom_index, graph); 463 467 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA, ··· 473 477 FREE_AND_NULL(graph->bloom_filter_settings); 474 478 } 475 479 476 - oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len, 477 - the_repository->hash_algo); 480 + oidread(&graph->oid, graph->data + graph->data_len - graph->hash_algo->rawsz, 481 + r->hash_algo); 478 482 479 483 free_chunkfile(cf); 480 484 return graph; ··· 486 490 return NULL; 487 491 } 488 492 489 - static struct commit_graph *load_commit_graph_one(struct repository *r, 490 - const char *graph_file, 491 - struct odb_source *source) 493 + static struct commit_graph *load_commit_graph_one(struct odb_source *source, 494 + const char *graph_file) 492 495 { 493 - 494 496 struct stat st; 495 497 int fd; 496 498 struct commit_graph *g; ··· 499 501 if (!open_ok) 500 502 return NULL; 501 503 502 - g = load_commit_graph_one_fd_st(r, fd, &st, source); 503 - 504 + g = load_commit_graph_one_fd_st(source, fd, &st); 504 505 if (g) 505 506 g->filename = xstrdup(graph_file); 506 507 507 508 return g; 508 509 } 509 510 510 - static struct commit_graph *load_commit_graph_v1(struct repository *r, 511 - struct odb_source *source) 511 + static struct commit_graph *load_commit_graph_v1(struct odb_source *source) 512 512 { 513 513 char *graph_name = get_commit_graph_filename(source); 514 - struct commit_graph *g = load_commit_graph_one(r, graph_name, source); 514 + struct commit_graph *g = load_commit_graph_one(source, graph_name); 515 515 free(graph_name); 516 516 517 517 return g; ··· 579 579 return 0; 580 580 } 581 581 582 - if (g->chunk_base_graphs_size / g->hash_len < n) { 582 + if (g->chunk_base_graphs_size / g->hash_algo->rawsz < n) { 583 583 warning(_("commit-graph base graphs chunk is too small")); 584 584 return 0; 585 585 } ··· 589 589 590 590 if (!cur_g || 591 591 !oideq(&oids[n], &cur_g->oid) || 592 - !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n), 593 - the_repository->hash_algo)) { 592 + !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n), 593 + g->hash_algo)) { 594 594 warning(_("commit-graph chain does not match")); 595 595 return 0; 596 596 } ··· 614 614 } 615 615 616 616 int open_commit_graph_chain(const char *chain_file, 617 - int *fd, struct stat *st) 617 + int *fd, struct stat *st, 618 + const struct git_hash_algo *hash_algo) 618 619 { 619 620 *fd = git_open(chain_file); 620 621 if (*fd < 0) ··· 623 624 close(*fd); 624 625 return 0; 625 626 } 626 - if (st->st_size < the_hash_algo->hexsz) { 627 + if (st->st_size < hash_algo->hexsz) { 627 628 close(*fd); 628 629 if (!st->st_size) { 629 630 /* treat empty files the same as missing */ ··· 637 638 return 1; 638 639 } 639 640 640 - struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r, 641 + struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, 641 642 int fd, struct stat *st, 642 643 int *incomplete_chain) 643 644 { ··· 647 648 int i = 0, valid = 1, count; 648 649 FILE *fp = xfdopen(fd, "r"); 649 650 650 - count = st->st_size / (the_hash_algo->hexsz + 1); 651 + count = st->st_size / (odb->repo->hash_algo->hexsz + 1); 651 652 CALLOC_ARRAY(oids, count); 652 653 653 - odb_prepare_alternates(r->objects); 654 + odb_prepare_alternates(odb); 654 655 655 656 for (i = 0; i < count; i++) { 656 657 struct odb_source *source; ··· 658 659 if (strbuf_getline_lf(&line, fp) == EOF) 659 660 break; 660 661 661 - if (get_oid_hex(line.buf, &oids[i])) { 662 + if (get_oid_hex_algop(line.buf, &oids[i], odb->repo->hash_algo)) { 662 663 warning(_("invalid commit-graph chain: line '%s' not a hash"), 663 664 line.buf); 664 665 valid = 0; ··· 666 667 } 667 668 668 669 valid = 0; 669 - for (source = r->objects->sources; source; source = source->next) { 670 + for (source = odb->sources; source; source = source->next) { 670 671 char *graph_name = get_split_graph_filename(source, line.buf); 671 - struct commit_graph *g = load_commit_graph_one(r, graph_name, source); 672 + struct commit_graph *g = load_commit_graph_one(source, graph_name); 672 673 673 674 free(graph_name); 674 675 ··· 701 702 return graph_chain; 702 703 } 703 704 704 - static struct commit_graph *load_commit_graph_chain(struct repository *r, 705 - struct odb_source *source) 705 + static struct commit_graph *load_commit_graph_chain(struct odb_source *source) 706 706 { 707 707 char *chain_file = get_commit_graph_chain_filename(source); 708 708 struct stat st; 709 709 int fd; 710 710 struct commit_graph *g = NULL; 711 711 712 - if (open_commit_graph_chain(chain_file, &fd, &st)) { 712 + if (open_commit_graph_chain(chain_file, &fd, &st, source->odb->repo->hash_algo)) { 713 713 int incomplete; 714 714 /* ownership of fd is taken over by load function */ 715 - g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete); 715 + g = load_commit_graph_chain_fd_st(source->odb, fd, &st, &incomplete); 716 716 } 717 717 718 718 free(chain_file); 719 719 return g; 720 720 } 721 721 722 - struct commit_graph *read_commit_graph_one(struct repository *r, 723 - struct odb_source *source) 722 + struct commit_graph *read_commit_graph_one(struct odb_source *source) 724 723 { 725 - struct commit_graph *g = load_commit_graph_v1(r, source); 724 + struct commit_graph *g = load_commit_graph_v1(source); 726 725 727 726 if (!g) 728 - g = load_commit_graph_chain(r, source); 727 + g = load_commit_graph_chain(source); 729 728 730 729 return g; 731 730 } 732 731 733 - static void prepare_commit_graph_one(struct repository *r, 734 - struct odb_source *source) 735 - { 736 - 737 - if (r->objects->commit_graph) 738 - return; 739 - 740 - r->objects->commit_graph = read_commit_graph_one(r, source); 741 - } 742 - 743 732 /* 744 733 * Return 1 if commit_graph is non-NULL, and 0 otherwise. 745 734 * 746 735 * On the first invocation, this function attempts to load the commit 747 - * graph if the_repository is configured to have one. 736 + * graph if the repository is configured to have one. 748 737 */ 749 738 static int prepare_commit_graph(struct repository *r) 750 739 { ··· 780 769 return 0; 781 770 782 771 odb_prepare_alternates(r->objects); 783 - for (source = r->objects->sources; 784 - !r->objects->commit_graph && source; 785 - source = source->next) 786 - prepare_commit_graph_one(r, source); 772 + for (source = r->objects->sources; source; source = source->next) { 773 + r->objects->commit_graph = read_commit_graph_one(source); 774 + if (r->objects->commit_graph) 775 + break; 776 + } 777 + 787 778 return !!r->objects->commit_graph; 788 779 } 789 780 ··· 800 791 return 0; 801 792 802 793 first_generation = get_be32(g->chunk_commit_data + 803 - g->hash_len + 8) >> 2; 794 + g->hash_algo->rawsz + 8) >> 2; 804 795 805 796 return !!first_generation; 806 797 } ··· 844 835 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos) 845 836 { 846 837 return bsearch_hash(oid->hash, g->chunk_oid_fanout, 847 - g->chunk_oid_lookup, g->hash_len, pos); 838 + g->chunk_oid_lookup, g->hash_algo->rawsz, pos); 848 839 } 849 840 850 841 static void load_oid_from_graph(struct commit_graph *g, ··· 864 855 865 856 lex_index = pos - g->num_commits_in_base; 866 857 867 - oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index), 868 - the_repository->hash_algo); 858 + oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, lex_index), 859 + g->hash_algo); 869 860 } 870 861 871 - static struct commit_list **insert_parent_or_die(struct repository *r, 872 - struct commit_graph *g, 862 + static struct commit_list **insert_parent_or_die(struct commit_graph *g, 873 863 uint32_t pos, 874 864 struct commit_list **pptr) 875 865 { ··· 880 870 die("invalid parent position %"PRIu32, pos); 881 871 882 872 load_oid_from_graph(g, pos, &oid); 883 - c = lookup_commit(r, &oid); 873 + c = lookup_commit(g->odb_source->odb->repo, &oid); 884 874 if (!c) 885 875 die(_("could not find commit %s"), oid_to_hex(&oid)); 886 876 commit_graph_data_at(c)->graph_pos = pos; ··· 901 891 die(_("invalid commit position. commit-graph is likely corrupt")); 902 892 903 893 lex_index = pos - g->num_commits_in_base; 904 - commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index); 894 + commit_data = g->chunk_commit_data + st_mult(graph_data_width(g->hash_algo), lex_index); 905 895 906 896 graph_data = commit_graph_data_at(item); 907 897 graph_data->graph_pos = pos; 908 898 909 - date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; 910 - date_low = get_be32(commit_data + g->hash_len + 12); 899 + date_high = get_be32(commit_data + g->hash_algo->rawsz + 8) & 0x3; 900 + date_low = get_be32(commit_data + g->hash_algo->rawsz + 12); 911 901 item->date = (timestamp_t)((date_high << 32) | date_low); 912 902 913 903 if (g->read_generation_data) { ··· 925 915 } else 926 916 graph_data->generation = item->date + offset; 927 917 } else 928 - graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2; 918 + graph_data->generation = get_be32(commit_data + g->hash_algo->rawsz + 8) >> 2; 929 919 930 920 if (g->topo_levels) 931 - *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2; 921 + *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_algo->rawsz + 8) >> 2; 932 922 } 933 923 934 924 static inline void set_commit_tree(struct commit *c, struct tree *t) ··· 936 926 c->maybe_tree = t; 937 927 } 938 928 939 - static int fill_commit_in_graph(struct repository *r, 940 - struct commit *item, 929 + static int fill_commit_in_graph(struct commit *item, 941 930 struct commit_graph *g, uint32_t pos) 942 931 { 943 932 uint32_t edge_value; ··· 952 941 fill_commit_graph_info(item, g, pos); 953 942 954 943 lex_index = pos - g->num_commits_in_base; 955 - commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index); 944 + commit_data = g->chunk_commit_data + st_mult(g->hash_algo->rawsz + 16, lex_index); 956 945 957 946 item->object.parsed = 1; 958 947 ··· 960 949 961 950 pptr = &item->parents; 962 951 963 - edge_value = get_be32(commit_data + g->hash_len); 952 + edge_value = get_be32(commit_data + g->hash_algo->rawsz); 964 953 if (edge_value == GRAPH_PARENT_NONE) 965 954 return 1; 966 - pptr = insert_parent_or_die(r, g, edge_value, pptr); 955 + pptr = insert_parent_or_die(g, edge_value, pptr); 967 956 968 - edge_value = get_be32(commit_data + g->hash_len + 4); 957 + edge_value = get_be32(commit_data + g->hash_algo->rawsz + 4); 969 958 if (edge_value == GRAPH_PARENT_NONE) 970 959 return 1; 971 960 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { 972 - pptr = insert_parent_or_die(r, g, edge_value, pptr); 961 + pptr = insert_parent_or_die(g, edge_value, pptr); 973 962 return 1; 974 963 } 975 964 ··· 984 973 } 985 974 edge_value = get_be32(g->chunk_extra_edges + 986 975 sizeof(uint32_t) * parent_data_pos); 987 - pptr = insert_parent_or_die(r, g, 976 + pptr = insert_parent_or_die(g, 988 977 edge_value & GRAPH_EDGE_LAST_MASK, 989 978 pptr); 990 979 parent_data_pos++; ··· 1050 1039 if (commit->object.parsed) 1051 1040 return commit; 1052 1041 1053 - if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos)) 1042 + if (!fill_commit_in_graph(commit, repo->objects->commit_graph, pos)) 1054 1043 return NULL; 1055 1044 1056 1045 return commit; 1057 1046 } 1058 1047 1059 - static int parse_commit_in_graph_one(struct repository *r, 1060 - struct commit_graph *g, 1048 + static int parse_commit_in_graph_one(struct commit_graph *g, 1061 1049 struct commit *item) 1062 1050 { 1063 1051 uint32_t pos; ··· 1066 1054 return 1; 1067 1055 1068 1056 if (find_commit_pos_in_graph(item, g, &pos)) 1069 - return fill_commit_in_graph(r, item, g, pos); 1057 + return fill_commit_in_graph(item, g, pos); 1070 1058 1071 1059 return 0; 1072 1060 } ··· 1083 1071 1084 1072 if (!prepare_commit_graph(r)) 1085 1073 return 0; 1086 - return parse_commit_in_graph_one(r, r->objects->commit_graph, item); 1074 + return parse_commit_in_graph_one(r->objects->commit_graph, item); 1087 1075 } 1088 1076 1089 1077 void load_commit_graph_info(struct repository *r, struct commit *item) ··· 1093 1081 fill_commit_graph_info(item, r->objects->commit_graph, pos); 1094 1082 } 1095 1083 1096 - static struct tree *load_tree_for_commit(struct repository *r, 1097 - struct commit_graph *g, 1084 + static struct tree *load_tree_for_commit(struct commit_graph *g, 1098 1085 struct commit *c) 1099 1086 { 1100 1087 struct object_id oid; ··· 1105 1092 g = g->base_graph; 1106 1093 1107 1094 commit_data = g->chunk_commit_data + 1108 - st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base); 1095 + st_mult(graph_data_width(g->hash_algo), 1096 + graph_pos - g->num_commits_in_base); 1109 1097 1110 - oidread(&oid, commit_data, the_repository->hash_algo); 1111 - set_commit_tree(c, lookup_tree(r, &oid)); 1098 + oidread(&oid, commit_data, g->hash_algo); 1099 + set_commit_tree(c, lookup_tree(g->odb_source->odb->repo, &oid)); 1112 1100 1113 1101 return c->maybe_tree; 1114 1102 } 1115 1103 1116 - static struct tree *get_commit_tree_in_graph_one(struct repository *r, 1117 - struct commit_graph *g, 1104 + static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g, 1118 1105 const struct commit *c) 1119 1106 { 1120 1107 if (c->maybe_tree) ··· 1122 1109 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH) 1123 1110 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); 1124 1111 1125 - return load_tree_for_commit(r, g, (struct commit *)c); 1112 + return load_tree_for_commit(g, (struct commit *)c); 1126 1113 } 1127 1114 1128 1115 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) 1129 1116 { 1130 - return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); 1117 + return get_commit_tree_in_graph_one(r->objects->commit_graph, c); 1131 1118 } 1132 1119 1133 1120 struct packed_commit_list { ··· 1213 1200 int count; 1214 1201 for (count = 0; count < ctx->commits.nr; count++, list++) { 1215 1202 display_progress(ctx->progress, ++ctx->progress_cnt); 1216 - hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz); 1203 + hashwrite(f, (*list)->object.oid.hash, f->algop->rawsz); 1217 1204 } 1218 1205 1219 1206 return 0; ··· 1244 1231 die(_("unable to parse commit %s"), 1245 1232 oid_to_hex(&(*list)->object.oid)); 1246 1233 tree = get_commit_tree_oid(*list); 1247 - hashwrite(f, tree->hash, the_hash_algo->rawsz); 1234 + hashwrite(f, tree->hash, ctx->r->hash_algo->rawsz); 1248 1235 1249 1236 parent = (*list)->parents; 1250 1237 ··· 1534 1521 1535 1522 if (ctx->report_progress) 1536 1523 ctx->progress = start_delayed_progress( 1537 - the_repository, 1524 + ctx->r, 1538 1525 _("Loading known commits in commit graph"), 1539 1526 ctx->oids.nr); 1540 1527 for (i = 0; i < ctx->oids.nr; i++) { ··· 1552 1539 */ 1553 1540 if (ctx->report_progress) 1554 1541 ctx->progress = start_delayed_progress( 1555 - the_repository, 1542 + ctx->r, 1556 1543 _("Expanding reachable commits in commit graph"), 1557 1544 0); 1558 1545 for (i = 0; i < ctx->oids.nr; i++) { ··· 1573 1560 1574 1561 if (ctx->report_progress) 1575 1562 ctx->progress = start_delayed_progress( 1576 - the_repository, 1563 + ctx->r, 1577 1564 _("Clearing commit marks in commit graph"), 1578 1565 ctx->oids.nr); 1579 1566 for (i = 0; i < ctx->oids.nr; i++) { ··· 1691 1678 if (ctx->report_progress) 1692 1679 info.progress = ctx->progress 1693 1680 = start_delayed_progress( 1694 - the_repository, 1681 + ctx->r, 1695 1682 _("Computing commit graph topological levels"), 1696 1683 ctx->commits.nr); 1697 1684 ··· 1726 1713 if (ctx->report_progress) 1727 1714 info.progress = ctx->progress 1728 1715 = start_delayed_progress( 1729 - the_repository, 1716 + ctx->r, 1730 1717 _("Computing commit graph generation numbers"), 1731 1718 ctx->commits.nr); 1732 1719 ··· 1803 1790 1804 1791 if (ctx->report_progress) 1805 1792 progress = start_delayed_progress( 1806 - the_repository, 1793 + ctx->r, 1807 1794 _("Computing commit changed paths Bloom filters"), 1808 1795 ctx->commits.nr); 1809 1796 ··· 1849 1836 } 1850 1837 1851 1838 struct refs_cb_data { 1839 + struct repository *repo; 1852 1840 struct oidset *commits; 1853 1841 struct progress *progress; 1854 1842 }; ··· 1861 1849 struct object_id peeled; 1862 1850 struct refs_cb_data *data = (struct refs_cb_data *)cb_data; 1863 1851 1864 - if (!peel_iterated_oid(the_repository, oid, &peeled)) 1852 + if (!peel_iterated_oid(data->repo, oid, &peeled)) 1865 1853 oid = &peeled; 1866 - if (odb_read_object_info(the_repository->objects, oid, NULL) == OBJ_COMMIT) 1854 + if (odb_read_object_info(data->repo->objects, oid, NULL) == OBJ_COMMIT) 1867 1855 oidset_insert(data->commits, oid); 1868 1856 1869 1857 display_progress(data->progress, oidset_size(data->commits)); ··· 1880 1868 int result; 1881 1869 1882 1870 memset(&data, 0, sizeof(data)); 1871 + data.repo = source->odb->repo; 1883 1872 data.commits = &commits; 1873 + 1884 1874 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) 1885 1875 data.progress = start_delayed_progress( 1886 - the_repository, 1876 + source->odb->repo, 1887 1877 _("Collecting referenced commits"), 0); 1888 1878 1889 - refs_for_each_ref(get_main_ref_store(the_repository), add_ref_to_set, 1879 + refs_for_each_ref(get_main_ref_store(source->odb->repo), add_ref_to_set, 1890 1880 &data); 1891 1881 1892 1882 stop_progress(&data.progress); ··· 1915 1905 "Finding commits for commit graph in %"PRIuMAX" packs", 1916 1906 pack_indexes->nr), 1917 1907 (uintmax_t)pack_indexes->nr); 1918 - ctx->progress = start_delayed_progress(the_repository, 1908 + ctx->progress = start_delayed_progress(ctx->r, 1919 1909 progress_title.buf, 0); 1920 1910 ctx->progress_done = 0; 1921 1911 } ··· 1969 1959 { 1970 1960 if (ctx->report_progress) 1971 1961 ctx->progress = start_delayed_progress( 1972 - the_repository, 1962 + ctx->r, 1973 1963 _("Finding commits for commit graph among packed objects"), 1974 1964 ctx->approx_nr_objects); 1975 1965 for_each_packed_object(ctx->r, add_packed_commits, ctx, ··· 1988 1978 ctx->num_extra_edges = 0; 1989 1979 if (ctx->report_progress) 1990 1980 ctx->progress = start_delayed_progress( 1991 - the_repository, 1981 + ctx->r, 1992 1982 _("Finding extra edges in commit graph"), 1993 1983 ctx->oids.nr); 1994 1984 oid_array_sort(&ctx->oids); ··· 2027 2017 return 0; 2028 2018 2029 2019 num = write_graph_chunk_base_1(f, g->base_graph); 2030 - hashwrite(f, g->oid.hash, the_hash_algo->rawsz); 2020 + hashwrite(f, g->oid.hash, g->hash_algo->rawsz); 2031 2021 return num + 1; 2032 2022 } 2033 2023 ··· 2051 2041 struct hashfile *f; 2052 2042 struct tempfile *graph_layer; /* when ctx->split is non-zero */ 2053 2043 struct lock_file lk = LOCK_INIT; 2054 - const unsigned hashsz = the_hash_algo->rawsz; 2044 + const unsigned hashsz = ctx->r->hash_algo->rawsz; 2055 2045 struct strbuf progress_title = STRBUF_INIT; 2056 2046 struct chunkfile *cf; 2057 2047 unsigned char file_hash[GIT_MAX_RAWSZ]; ··· 2067 2057 ctx->graph_name = get_commit_graph_filename(ctx->odb_source); 2068 2058 } 2069 2059 2070 - if (safe_create_leading_directories(the_repository, ctx->graph_name)) { 2060 + if (safe_create_leading_directories(ctx->r, ctx->graph_name)) { 2071 2061 error(_("unable to create leading directories of %s"), 2072 2062 ctx->graph_name); 2073 2063 return -1; ··· 2086 2076 return -1; 2087 2077 } 2088 2078 2089 - if (adjust_shared_perm(the_repository, get_tempfile_path(graph_layer))) { 2079 + if (adjust_shared_perm(ctx->r, get_tempfile_path(graph_layer))) { 2090 2080 error(_("unable to adjust shared permissions for '%s'"), 2091 2081 get_tempfile_path(graph_layer)); 2092 2082 return -1; 2093 2083 } 2094 2084 2095 - f = hashfd(the_repository->hash_algo, 2085 + f = hashfd(ctx->r->hash_algo, 2096 2086 get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer)); 2097 2087 } else { 2098 2088 hold_lock_file_for_update_mode(&lk, ctx->graph_name, 2099 2089 LOCK_DIE_ON_ERROR, 0444); 2100 - f = hashfd(the_repository->hash_algo, 2090 + f = hashfd(ctx->r->hash_algo, 2101 2091 get_lock_file_fd(&lk), get_lock_file_path(&lk)); 2102 2092 } 2103 2093 ··· 2139 2129 hashwrite_be32(f, GRAPH_SIGNATURE); 2140 2130 2141 2131 hashwrite_u8(f, GRAPH_VERSION); 2142 - hashwrite_u8(f, oid_version(the_hash_algo)); 2132 + hashwrite_u8(f, oid_version(ctx->r->hash_algo)); 2143 2133 hashwrite_u8(f, get_num_chunks(cf)); 2144 2134 hashwrite_u8(f, ctx->num_commit_graphs_after - 1); 2145 2135 ··· 2150 2140 get_num_chunks(cf)), 2151 2141 get_num_chunks(cf)); 2152 2142 ctx->progress = start_delayed_progress( 2153 - the_repository, 2143 + ctx->r, 2154 2144 progress_title.buf, 2155 2145 st_mult(get_num_chunks(cf), ctx->commits.nr)); 2156 2146 } ··· 2208 2198 } 2209 2199 2210 2200 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]); 2211 - ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash)); 2201 + ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = 2202 + xstrdup(hash_to_hex_algop(file_hash, ctx->r->hash_algo)); 2212 2203 final_graph_name = get_split_graph_filename(ctx->odb_source, 2213 2204 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]); 2214 2205 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]); ··· 2363 2354 2364 2355 if (ctx->report_progress) 2365 2356 ctx->progress = start_delayed_progress( 2366 - the_repository, 2357 + ctx->r, 2367 2358 _("Scanning merged commits"), 2368 2359 ctx->commits.nr); 2369 2360 ··· 2408 2399 current_graph_number--; 2409 2400 2410 2401 if (ctx->report_progress) 2411 - ctx->progress = start_delayed_progress(the_repository, 2402 + ctx->progress = start_delayed_progress(ctx->r, 2412 2403 _("Merging commit-graph"), 0); 2413 2404 2414 2405 merge_commit_graph(ctx, g); ··· 2511 2502 enum commit_graph_write_flags flags, 2512 2503 const struct commit_graph_opts *opts) 2513 2504 { 2514 - struct repository *r = the_repository; 2505 + struct repository *r = source->odb->repo; 2515 2506 struct write_commit_graph_context ctx = { 2516 2507 .r = r, 2517 2508 .odb_source = source, ··· 2611 2602 replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE; 2612 2603 } 2613 2604 2614 - ctx.approx_nr_objects = repo_approximate_object_count(the_repository); 2605 + ctx.approx_nr_objects = repo_approximate_object_count(r); 2615 2606 2616 2607 if (ctx.append && ctx.r->objects->commit_graph) { 2617 2608 struct commit_graph *g = ctx.r->objects->commit_graph; 2618 2609 for (i = 0; i < g->num_commits; i++) { 2619 2610 struct object_id oid; 2620 - oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i), 2621 - the_repository->hash_algo); 2611 + oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i), 2612 + r->hash_algo); 2622 2613 oid_array_append(&ctx.oids, &oid); 2623 2614 } 2624 2615 } ··· 2726 2717 2727 2718 static int commit_graph_checksum_valid(struct commit_graph *g) 2728 2719 { 2729 - return hashfile_checksum_valid(the_repository->hash_algo, 2720 + return hashfile_checksum_valid(g->hash_algo, 2730 2721 g->data, g->data_len); 2731 2722 } 2732 2723 2733 - static int verify_one_commit_graph(struct repository *r, 2734 - struct commit_graph *g, 2724 + static int verify_one_commit_graph(struct commit_graph *g, 2735 2725 struct progress *progress, 2736 2726 uint64_t *seen) 2737 2727 { 2728 + struct repository *r = g->odb_source->odb->repo; 2738 2729 uint32_t i, cur_fanout_pos = 0; 2739 2730 struct object_id prev_oid, cur_oid; 2740 2731 struct commit *seen_gen_zero = NULL; ··· 2748 2739 for (i = 0; i < g->num_commits; i++) { 2749 2740 struct commit *graph_commit; 2750 2741 2751 - oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i), 2752 - the_repository->hash_algo); 2742 + oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i), 2743 + g->hash_algo); 2753 2744 2754 2745 if (i && oidcmp(&prev_oid, &cur_oid) >= 0) 2755 2746 graph_report(_("commit-graph has incorrect OID order: %s then %s"), ··· 2768 2759 } 2769 2760 2770 2761 graph_commit = lookup_commit(r, &cur_oid); 2771 - if (!parse_commit_in_graph_one(r, g, graph_commit)) 2762 + if (!parse_commit_in_graph_one(g, graph_commit)) 2772 2763 graph_report(_("failed to parse commit %s from commit-graph"), 2773 2764 oid_to_hex(&cur_oid)); 2774 2765 } ··· 2793 2784 timestamp_t generation; 2794 2785 2795 2786 display_progress(progress, ++(*seen)); 2796 - oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i), 2797 - the_repository->hash_algo); 2787 + oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i), 2788 + g->hash_algo); 2798 2789 2799 2790 graph_commit = lookup_commit(r, &cur_oid); 2800 2791 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r)); ··· 2804 2795 continue; 2805 2796 } 2806 2797 2807 - if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, 2798 + if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid, 2808 2799 get_commit_tree_oid(odb_commit))) 2809 2800 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), 2810 2801 oid_to_hex(&cur_oid), ··· 2822 2813 } 2823 2814 2824 2815 /* parse parent in case it is in a base graph */ 2825 - parse_commit_in_graph_one(r, g, graph_parents->item); 2816 + parse_commit_in_graph_one(g, graph_parents->item); 2826 2817 2827 2818 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) 2828 2819 graph_report(_("commit-graph parent for %s is %s != %s"), ··· 2882 2873 return verify_commit_graph_error; 2883 2874 } 2884 2875 2885 - int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags) 2876 + int verify_commit_graph(struct commit_graph *g, int flags) 2886 2877 { 2887 2878 struct progress *progress = NULL; 2888 2879 int local_error = 0; ··· 2898 2889 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW)) 2899 2890 total += g->num_commits_in_base; 2900 2891 2901 - progress = start_progress(the_repository, 2892 + progress = start_progress(g->odb_source->odb->repo, 2902 2893 _("Verifying commits in commit graph"), 2903 2894 total); 2904 2895 } 2905 2896 2906 2897 for (; g; g = g->base_graph) { 2907 - local_error |= verify_one_commit_graph(r, g, progress, &seen); 2898 + local_error |= verify_one_commit_graph(g, progress, &seen); 2908 2899 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW) 2909 2900 break; 2910 2901 }
+10 -11
commit-graph.h
··· 21 21 * call this method oustide of a builtin, and only if you know what 22 22 * you are doing! 23 23 */ 24 - void git_test_write_commit_graph_or_die(void); 24 + void git_test_write_commit_graph_or_die(struct odb_source *source); 25 25 26 26 struct commit; 27 27 struct bloom_filter_settings; ··· 32 32 char *get_commit_graph_filename(struct odb_source *source); 33 33 char *get_commit_graph_chain_filename(struct odb_source *source); 34 34 int open_commit_graph(const char *graph_file, int *fd, struct stat *st); 35 - int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st); 35 + int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st, 36 + const struct git_hash_algo *hash_algo); 36 37 37 38 /* 38 39 * Given a commit struct, try to fill the commit struct info, including: ··· 84 85 const unsigned char *data; 85 86 size_t data_len; 86 87 87 - unsigned char hash_len; 88 + const struct git_hash_algo *hash_algo; 88 89 unsigned char num_chunks; 89 90 uint32_t num_commits; 90 91 struct object_id oid; ··· 113 114 struct bloom_filter_settings *bloom_filter_settings; 114 115 }; 115 116 116 - struct commit_graph *load_commit_graph_one_fd_st(struct repository *r, 117 - int fd, struct stat *st, 118 - struct odb_source *source); 119 - struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r, 117 + struct commit_graph *load_commit_graph_one_fd_st(struct odb_source *source, 118 + int fd, struct stat *st); 119 + struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, 120 120 int fd, struct stat *st, 121 121 int *incomplete_chain); 122 - struct commit_graph *read_commit_graph_one(struct repository *r, 123 - struct odb_source *source); 122 + struct commit_graph *read_commit_graph_one(struct odb_source *source); 124 123 125 124 struct repo_settings; 126 125 ··· 128 127 * Callers should initialize the repo_settings with prepare_repo_settings() 129 128 * prior to calling parse_commit_graph(). 130 129 */ 131 - struct commit_graph *parse_commit_graph(struct repo_settings *s, 130 + struct commit_graph *parse_commit_graph(struct repository *r, 132 131 void *graph_map, size_t graph_size); 133 132 134 133 /* ··· 184 183 185 184 #define COMMIT_GRAPH_VERIFY_SHALLOW (1 << 0) 186 185 187 - int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags); 186 + int verify_commit_graph(struct commit_graph *g, int flags); 188 187 189 188 void close_commit_graph(struct object_database *); 190 189 void free_commit_graph(struct commit_graph *);
+2 -4
oss-fuzz/fuzz-commit-graph.c
··· 4 4 #include "commit-graph.h" 5 5 #include "repository.h" 6 6 7 - struct commit_graph *parse_commit_graph(struct repo_settings *s, 8 - void *graph_map, size_t graph_size); 9 - 10 7 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); 11 8 12 9 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) ··· 22 19 * possible. 23 20 */ 24 21 repo_set_hash_algo(the_repository, GIT_HASH_SHA1); 22 + the_repository->settings.initialized = 1; 25 23 the_repository->settings.commit_graph_generation_version = 2; 26 24 the_repository->settings.commit_graph_changed_paths_version = 1; 27 - g = parse_commit_graph(&the_repository->settings, (void *)data, size); 25 + g = parse_commit_graph(the_repository, (void *)data, size); 28 26 repo_clear(the_repository); 29 27 free_commit_graph(g); 30 28
+1 -1
t/helper/test-read-graph.c
··· 81 81 82 82 prepare_repo_settings(the_repository); 83 83 84 - graph = read_commit_graph_one(the_repository, source); 84 + graph = read_commit_graph_one(source); 85 85 if (!graph) { 86 86 ret = 1; 87 87 goto done;