Git fork

hash_pos(): convert to oid_pos()

All of our callers are actually looking up an object_id, not a bare
hash. Likewise, the arrays they are looking in are actual arrays of
object_id (not just raw bytes of hashes, as we might find in a pack
.idx; those are handled by bsearch_hash()).

Using an object_id gives us more type safety, and makes the callers
slightly shorter. It also gets rid of the word "sha1" from several
access functions, though we could obviously also rename those with
s/sha1/hash/.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Jeff King and committed by
Junio C Hamano
45ee13b9 680ff910

+43 -43
+4 -4
builtin/name-rev.c
··· 390 390 } 391 391 } 392 392 393 - static const unsigned char *nth_tip_table_ent(size_t ix, void *table_) 393 + static const struct object_id *nth_tip_table_ent(size_t ix, void *table_) 394 394 { 395 395 struct tip_table_entry *table = table_; 396 - return table[ix].oid.hash; 396 + return &table[ix].oid; 397 397 } 398 398 399 399 static const char *get_exact_ref_match(const struct object *o) ··· 408 408 tip_table.sorted = 1; 409 409 } 410 410 411 - found = hash_pos(o->oid.hash, tip_table.table, tip_table.nr, 412 - nth_tip_table_ent); 411 + found = oid_pos(&o->oid, tip_table.table, tip_table.nr, 412 + nth_tip_table_ent); 413 413 if (0 <= found) 414 414 return tip_table.table[found].refname; 415 415 return NULL;
+14 -14
commit-graph.c
··· 1012 1012 return 0; 1013 1013 } 1014 1014 1015 - static const unsigned char *commit_to_sha1(size_t index, void *table) 1015 + static const struct object_id *commit_to_oid(size_t index, void *table) 1016 1016 { 1017 1017 struct commit **commits = table; 1018 - return commits[index]->object.oid.hash; 1018 + return &commits[index]->object.oid; 1019 1019 } 1020 1020 1021 1021 static int write_graph_chunk_data(struct hashfile *f, ··· 1043 1043 if (!parent) 1044 1044 edge_value = GRAPH_PARENT_NONE; 1045 1045 else { 1046 - edge_value = hash_pos(parent->item->object.oid.hash, 1047 - ctx->commits.list, 1048 - ctx->commits.nr, 1049 - commit_to_sha1); 1046 + edge_value = oid_pos(&parent->item->object.oid, 1047 + ctx->commits.list, 1048 + ctx->commits.nr, 1049 + commit_to_oid); 1050 1050 1051 1051 if (edge_value >= 0) 1052 1052 edge_value += ctx->new_num_commits_in_base; ··· 1074 1074 else if (parent->next) 1075 1075 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; 1076 1076 else { 1077 - edge_value = hash_pos(parent->item->object.oid.hash, 1078 - ctx->commits.list, 1079 - ctx->commits.nr, 1080 - commit_to_sha1); 1077 + edge_value = oid_pos(&parent->item->object.oid, 1078 + ctx->commits.list, 1079 + ctx->commits.nr, 1080 + commit_to_oid); 1081 1081 1082 1082 if (edge_value >= 0) 1083 1083 edge_value += ctx->new_num_commits_in_base; ··· 1143 1143 1144 1144 /* Since num_parents > 2, this initializer is safe. */ 1145 1145 for (parent = (*list)->parents->next; parent; parent = parent->next) { 1146 - int edge_value = hash_pos(parent->item->object.oid.hash, 1147 - ctx->commits.list, 1148 - ctx->commits.nr, 1149 - commit_to_sha1); 1146 + int edge_value = oid_pos(&parent->item->object.oid, 1147 + ctx->commits.list, 1148 + ctx->commits.nr, 1149 + commit_to_oid); 1150 1150 1151 1151 if (edge_value >= 0) 1152 1152 edge_value += ctx->new_num_commits_in_base;
+5 -5
commit.c
··· 105 105 return parse_timestamp(dateptr, NULL, 10); 106 106 } 107 107 108 - static const unsigned char *commit_graft_sha1_access(size_t index, void *table) 108 + static const struct object_id *commit_graft_oid_access(size_t index, void *table) 109 109 { 110 110 struct commit_graft **commit_graft_table = table; 111 - return commit_graft_table[index]->oid.hash; 111 + return &commit_graft_table[index]->oid; 112 112 } 113 113 114 114 int commit_graft_pos(struct repository *r, const struct object_id *oid) 115 115 { 116 - return hash_pos(oid->hash, r->parsed_objects->grafts, 117 - r->parsed_objects->grafts_nr, 118 - commit_graft_sha1_access); 116 + return oid_pos(oid, r->parsed_objects->grafts, 117 + r->parsed_objects->grafts_nr, 118 + commit_graft_oid_access); 119 119 } 120 120 121 121 int register_commit_graft(struct repository *r, struct commit_graft *graft,
+9 -9
hash-lookup.c
··· 1 1 #include "cache.h" 2 2 #include "hash-lookup.h" 3 3 4 - static uint32_t take2(const unsigned char *hash) 4 + static uint32_t take2(const struct object_id *oid, size_t ofs) 5 5 { 6 - return ((hash[0] << 8) | hash[1]); 6 + return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]); 7 7 } 8 8 9 9 /* ··· 47 47 */ 48 48 /* 49 49 * The table should contain "nr" elements. 50 - * The hash of element i (between 0 and nr - 1) should be returned 50 + * The oid of element i (between 0 and nr - 1) should be returned 51 51 * by "fn(i, table)". 52 52 */ 53 - int hash_pos(const unsigned char *hash, void *table, size_t nr, 54 - hash_access_fn fn) 53 + int oid_pos(const struct object_id *oid, void *table, size_t nr, 54 + oid_access_fn fn) 55 55 { 56 56 size_t hi = nr; 57 57 size_t lo = 0; ··· 64 64 size_t lov, hiv, miv, ofs; 65 65 66 66 for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) { 67 - lov = take2(fn(0, table) + ofs); 68 - hiv = take2(fn(nr - 1, table) + ofs); 69 - miv = take2(hash + ofs); 67 + lov = take2(fn(0, table), ofs); 68 + hiv = take2(fn(nr - 1, table), ofs); 69 + miv = take2(oid, ofs); 70 70 if (miv < lov) 71 71 return -1; 72 72 if (hiv < miv) ··· 88 88 89 89 do { 90 90 int cmp; 91 - cmp = hashcmp(fn(mi, table), hash); 91 + cmp = oidcmp(fn(mi, table), oid); 92 92 if (!cmp) 93 93 return mi; 94 94 if (cmp > 0)
+5 -5
hash-lookup.h
··· 1 1 #ifndef HASH_LOOKUP_H 2 2 #define HASH_LOOKUP_H 3 3 4 - typedef const unsigned char *hash_access_fn(size_t index, void *table); 4 + typedef const struct object_id *oid_access_fn(size_t index, void *table); 5 5 6 - int hash_pos(const unsigned char *hash, 7 - void *table, 8 - size_t nr, 9 - hash_access_fn fn); 6 + int oid_pos(const struct object_id *oid, 7 + void *table, 8 + size_t nr, 9 + oid_access_fn fn); 10 10 11 11 /* 12 12 * Searches for hash in table, using the given fanout table to determine the
+3 -3
oid-array.c
··· 22 22 array->sorted = 1; 23 23 } 24 24 25 - static const unsigned char *sha1_access(size_t index, void *table) 25 + static const struct object_id *oid_access(size_t index, void *table) 26 26 { 27 27 struct object_id *array = table; 28 - return array[index].hash; 28 + return &array[index]; 29 29 } 30 30 31 31 int oid_array_lookup(struct oid_array *array, const struct object_id *oid) 32 32 { 33 33 oid_array_sort(array); 34 - return hash_pos(oid->hash, array->oid, array->nr, sha1_access); 34 + return oid_pos(oid, array->oid, array->nr, oid_access); 35 35 } 36 36 37 37 void oid_array_clear(struct oid_array *array)
+3 -3
pack-bitmap-write.c
··· 610 610 die("Failed to write bitmap index"); 611 611 } 612 612 613 - static const unsigned char *sha1_access(size_t pos, void *table) 613 + static const struct object_id *oid_access(size_t pos, void *table) 614 614 { 615 615 struct pack_idx_entry **index = table; 616 - return index[pos]->oid.hash; 616 + return &index[pos]->oid; 617 617 } 618 618 619 619 static void write_selected_commits_v1(struct hashfile *f, ··· 626 626 struct bitmapped_commit *stored = &writer.selected[i]; 627 627 628 628 int commit_pos = 629 - hash_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access); 629 + oid_pos(&stored->commit->object.oid, index, index_nr, oid_access); 630 630 631 631 if (commit_pos < 0) 632 632 BUG("trying to write commit not in index");