Skip to content

Commit 45ee13b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 680ff91 commit 45ee13b

File tree

7 files changed

+43
-43
lines changed

7 files changed

+43
-43
lines changed

builtin/name-rev.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ static void name_tips(void)
390390
}
391391
}
392392

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_)
394394
{
395395
struct tip_table_entry *table = table_;
396-
return table[ix].oid.hash;
396+
return &table[ix].oid;
397397
}
398398

399399
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)
408408
tip_table.sorted = 1;
409409
}
410410

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);
413413
if (0 <= found)
414414
return tip_table.table[found].refname;
415415
return NULL;

commit-graph.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,10 @@ static int write_graph_chunk_oids(struct hashfile *f,
10121012
return 0;
10131013
}
10141014

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)
10161016
{
10171017
struct commit **commits = table;
1018-
return commits[index]->object.oid.hash;
1018+
return &commits[index]->object.oid;
10191019
}
10201020

10211021
static int write_graph_chunk_data(struct hashfile *f,
@@ -1043,10 +1043,10 @@ static int write_graph_chunk_data(struct hashfile *f,
10431043
if (!parent)
10441044
edge_value = GRAPH_PARENT_NONE;
10451045
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);
10501050

10511051
if (edge_value >= 0)
10521052
edge_value += ctx->new_num_commits_in_base;
@@ -1074,10 +1074,10 @@ static int write_graph_chunk_data(struct hashfile *f,
10741074
else if (parent->next)
10751075
edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
10761076
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);
10811081

10821082
if (edge_value >= 0)
10831083
edge_value += ctx->new_num_commits_in_base;
@@ -1143,10 +1143,10 @@ static int write_graph_chunk_extra_edges(struct hashfile *f,
11431143

11441144
/* Since num_parents > 2, this initializer is safe. */
11451145
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);
11501150

11511151
if (edge_value >= 0)
11521152
edge_value += ctx->new_num_commits_in_base;

commit.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail)
105105
return parse_timestamp(dateptr, NULL, 10);
106106
}
107107

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)
109109
{
110110
struct commit_graft **commit_graft_table = table;
111-
return commit_graft_table[index]->oid.hash;
111+
return &commit_graft_table[index]->oid;
112112
}
113113

114114
int commit_graft_pos(struct repository *r, const struct object_id *oid)
115115
{
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);
119119
}
120120

121121
int register_commit_graft(struct repository *r, struct commit_graft *graft,

hash-lookup.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "cache.h"
22
#include "hash-lookup.h"
33

4-
static uint32_t take2(const unsigned char *hash)
4+
static uint32_t take2(const struct object_id *oid, size_t ofs)
55
{
6-
return ((hash[0] << 8) | hash[1]);
6+
return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);
77
}
88

99
/*
@@ -47,11 +47,11 @@ static uint32_t take2(const unsigned char *hash)
4747
*/
4848
/*
4949
* 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
5151
* by "fn(i, table)".
5252
*/
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)
5555
{
5656
size_t hi = nr;
5757
size_t lo = 0;
@@ -64,9 +64,9 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
6464
size_t lov, hiv, miv, ofs;
6565

6666
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);
7070
if (miv < lov)
7171
return -1;
7272
if (hiv < miv)
@@ -88,7 +88,7 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
8888

8989
do {
9090
int cmp;
91-
cmp = hashcmp(fn(mi, table), hash);
91+
cmp = oidcmp(fn(mi, table), oid);
9292
if (!cmp)
9393
return mi;
9494
if (cmp > 0)

hash-lookup.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef HASH_LOOKUP_H
22
#define HASH_LOOKUP_H
33

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);
55

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);
1010

1111
/*
1212
* Searches for hash in table, using the given fanout table to determine the

oid-array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ void oid_array_sort(struct oid_array *array)
2222
array->sorted = 1;
2323
}
2424

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)
2626
{
2727
struct object_id *array = table;
28-
return array[index].hash;
28+
return &array[index];
2929
}
3030

3131
int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
3232
{
3333
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);
3535
}
3636

3737
void oid_array_clear(struct oid_array *array)

pack-bitmap-write.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,10 @@ static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
610610
die("Failed to write bitmap index");
611611
}
612612

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)
614614
{
615615
struct pack_idx_entry **index = table;
616-
return index[pos]->oid.hash;
616+
return &index[pos]->oid;
617617
}
618618

619619
static void write_selected_commits_v1(struct hashfile *f,
@@ -626,7 +626,7 @@ static void write_selected_commits_v1(struct hashfile *f,
626626
struct bitmapped_commit *stored = &writer.selected[i];
627627

628628
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);
630630

631631
if (commit_pos < 0)
632632
BUG("trying to write commit not in index");

0 commit comments

Comments
 (0)