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>
Jeff King committed
Jan 28, 2021 at 01:19 UTC
45ee13b942b26925b33d827bda2856e1ed0af0b7
7 files changed
+43
-43
builtin/name-rev.c
+4
-4
@@ -390,10 +390,10 @@ static void name_tips(void)
390
}
391
}
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
{
395
struct tip_table_entry *table = table_;
396
- return table[ix].oid.hash;
396
+ return &table[ix].oid;
397
}
398
399
static const char *get_exact_ref_match(const struct object *o)
@@ -408,8 +408,8 @@ static const char *get_exact_ref_match(const struct object *o)
408
tip_table.sorted = 1;
409
}
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
if (0 <= found)
414
return tip_table.table[found].refname;
415
return NULL;
commit-graph.c
+14
-14
@@ -1012,10 +1012,10 @@ static int write_graph_chunk_oids(struct hashfile *f,
1012
return 0;
1013
}
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
{
1017
struct commit **commits = table;
1018
- return commits[index]->object.oid.hash;
1018
+ return &commits[index]->object.oid;
1019
}
1020
1021
static int write_graph_chunk_data(struct hashfile *f,
@@ -1043,10 +1043,10 @@ static int write_graph_chunk_data(struct hashfile *f,
1043
if (!parent)
1044
edge_value = GRAPH_PARENT_NONE;
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
1051
if (edge_value >= 0)
1052
edge_value += ctx->new_num_commits_in_base;
@@ -1074,10 +1074,10 @@ static int write_graph_chunk_data(struct hashfile *f,
1074
else if (parent->next)
1075
edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
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
1082
if (edge_value >= 0)
1083
edge_value += ctx->new_num_commits_in_base;
@@ -1143,10 +1143,10 @@ static int write_graph_chunk_extra_edges(struct hashfile *f,
1143
1144
/* Since num_parents > 2, this initializer is safe. */
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
1151
if (edge_value >= 0)
1152
edge_value += ctx->new_num_commits_in_base;
commit.c
+5
-5
@@ -105,17 +105,17 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail)
105
return parse_timestamp(dateptr, NULL, 10);
106
}
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
{
110
struct commit_graft **commit_graft_table = table;
111
- return commit_graft_table[index]->oid.hash;
111
+ return &commit_graft_table[index]->oid;
112
}
113
114
int commit_graft_pos(struct repository *r, const struct object_id *oid)
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
}
120
121
int register_commit_graft(struct repository *r, struct commit_graft *graft,
hash-lookup.c
+9
-9
@@ -1,9 +1,9 @@
1
#include "cache.h"
2
#include "hash-lookup.h"
3
4
-static uint32_t take2(const unsigned char *hash)
4
+static uint32_t take2(const struct object_id *oid, size_t ofs)
5
{
6
- return ((hash[0] << 8) | hash[1]);
6
+ return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);
7
}
8
9
/*
@@ -47,11 +47,11 @@ static uint32_t take2(const unsigned char *hash)
47
*/
48
/*
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
* by "fn(i, table)".
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
{
56
size_t hi = nr;
57
size_t lo = 0;
@@ -64,9 +64,9 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
64
size_t lov, hiv, miv, ofs;
65
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
if (miv < lov)
71
return -1;
72
if (hiv < miv)
@@ -88,7 +88,7 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
88
89
do {
90
int cmp;
91
- cmp = hashcmp(fn(mi, table), hash);
91
+ cmp = oidcmp(fn(mi, table), oid);
92
if (!cmp)
93
return mi;
94
if (cmp > 0)
hash-lookup.h
+5
-5
@@ -1,12 +1,12 @@
1
#ifndef HASH_LOOKUP_H
2
#define HASH_LOOKUP_H
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
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
11
/*
12
* Searches for hash in table, using the given fanout table to determine the
oid-array.c
+3
-3
@@ -22,16 +22,16 @@ void oid_array_sort(struct oid_array *array)
22
array->sorted = 1;
23
}
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
{
27
struct object_id *array = table;
28
- return array[index].hash;
28
+ return &array[index];
29
}
30
31
int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
32
{
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
}
36
37
void oid_array_clear(struct oid_array *array)
pack-bitmap-write.c
+3
-3
@@ -610,10 +610,10 @@ static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
610
die("Failed to write bitmap index");
611
}
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
{
615
struct pack_idx_entry **index = table;
616
- return index[pos]->oid.hash;
616
+ return &index[pos]->oid;
617
}
618
619
static void write_selected_commits_v1(struct hashfile *f,
@@ -626,7 +626,7 @@ static void write_selected_commits_v1(struct hashfile *f,
626
struct bitmapped_commit *stored = &writer.selected[i];
627
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
631
if (commit_pos < 0)
632
BUG("trying to write commit not in index");