]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hash_pos(): convert to oid_pos()
authorJeff King <peff@peff.net>
Thu, 28 Jan 2021 06:19:42 +0000 (01:19 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Jan 2021 20:02:39 +0000 (12:02 -0800)
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>
builtin/name-rev.c
commit-graph.c
commit.c
hash-lookup.c
hash-lookup.h
oid-array.c
pack-bitmap-write.c

index 3fe71a8c01012fd7ce7e97a65e4cb9cbd697c86d..27138fdce4718ad0395b512d763b00c5c2df72d5 100644 (file)
@@ -390,10 +390,10 @@ static void name_tips(void)
        }
 }
 
-static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
+static const struct object_id *nth_tip_table_ent(size_t ix, void *table_)
 {
        struct tip_table_entry *table = table_;
-       return table[ix].oid.hash;
+       return &table[ix].oid;
 }
 
 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)
                tip_table.sorted = 1;
        }
 
-       found = hash_pos(o->oid.hash, tip_table.table, tip_table.nr,
-                        nth_tip_table_ent);
+       found = oid_pos(&o->oid, tip_table.table, tip_table.nr,
+                       nth_tip_table_ent);
        if (0 <= found)
                return tip_table.table[found].refname;
        return NULL;
index f3486ec18f1b884fe65c74ac7ad75eb77fdcbb85..248f1efb73ea942d45054837088e2def79daf02b 100644 (file)
@@ -1012,10 +1012,10 @@ static int write_graph_chunk_oids(struct hashfile *f,
        return 0;
 }
 
-static const unsigned char *commit_to_sha1(size_t index, void *table)
+static const struct object_id *commit_to_oid(size_t index, void *table)
 {
        struct commit **commits = table;
-       return commits[index]->object.oid.hash;
+       return &commits[index]->object.oid;
 }
 
 static int write_graph_chunk_data(struct hashfile *f,
@@ -1043,10 +1043,10 @@ static int write_graph_chunk_data(struct hashfile *f,
                if (!parent)
                        edge_value = GRAPH_PARENT_NONE;
                else {
-                       edge_value = hash_pos(parent->item->object.oid.hash,
-                                             ctx->commits.list,
-                                             ctx->commits.nr,
-                                             commit_to_sha1);
+                       edge_value = oid_pos(&parent->item->object.oid,
+                                            ctx->commits.list,
+                                            ctx->commits.nr,
+                                            commit_to_oid);
 
                        if (edge_value >= 0)
                                edge_value += ctx->new_num_commits_in_base;
@@ -1074,10 +1074,10 @@ static int write_graph_chunk_data(struct hashfile *f,
                else if (parent->next)
                        edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
                else {
-                       edge_value = hash_pos(parent->item->object.oid.hash,
-                                             ctx->commits.list,
-                                             ctx->commits.nr,
-                                             commit_to_sha1);
+                       edge_value = oid_pos(&parent->item->object.oid,
+                                            ctx->commits.list,
+                                            ctx->commits.nr,
+                                            commit_to_oid);
 
                        if (edge_value >= 0)
                                edge_value += ctx->new_num_commits_in_base;
@@ -1143,10 +1143,10 @@ static int write_graph_chunk_extra_edges(struct hashfile *f,
 
                /* Since num_parents > 2, this initializer is safe. */
                for (parent = (*list)->parents->next; parent; parent = parent->next) {
-                       int edge_value = hash_pos(parent->item->object.oid.hash,
-                                                 ctx->commits.list,
-                                                 ctx->commits.nr,
-                                                 commit_to_sha1);
+                       int edge_value = oid_pos(&parent->item->object.oid,
+                                                ctx->commits.list,
+                                                ctx->commits.nr,
+                                                commit_to_oid);
 
                        if (edge_value >= 0)
                                edge_value += ctx->new_num_commits_in_base;
index fa26729ba51666bbf09cf5a73b84e9315132bd9e..39eab5b36bce9916ff94a935d2fe2cfa1baa1df9 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -105,17 +105,17 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail)
        return parse_timestamp(dateptr, NULL, 10);
 }
 
-static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
+static const struct object_id *commit_graft_oid_access(size_t index, void *table)
 {
        struct commit_graft **commit_graft_table = table;
-       return commit_graft_table[index]->oid.hash;
+       return &commit_graft_table[index]->oid;
 }
 
 int commit_graft_pos(struct repository *r, const struct object_id *oid)
 {
-       return hash_pos(oid->hash, r->parsed_objects->grafts,
-                       r->parsed_objects->grafts_nr,
-                       commit_graft_sha1_access);
+       return oid_pos(oid, r->parsed_objects->grafts,
+                      r->parsed_objects->grafts_nr,
+                      commit_graft_oid_access);
 }
 
 int register_commit_graft(struct repository *r, struct commit_graft *graft,
index 1191856a32e6f5dba93b444cae269fac50cbf15f..d15bb345749470b0aac9e6c3d4b1f36b78d6bdee 100644 (file)
@@ -1,9 +1,9 @@
 #include "cache.h"
 #include "hash-lookup.h"
 
-static uint32_t take2(const unsigned char *hash)
+static uint32_t take2(const struct object_id *oid, size_t ofs)
 {
-       return ((hash[0] << 8) | hash[1]);
+       return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);
 }
 
 /*
@@ -47,11 +47,11 @@ static uint32_t take2(const unsigned char *hash)
  */
 /*
  * The table should contain "nr" elements.
- * The hash of element i (between 0 and nr - 1) should be returned
+ * The oid of element i (between 0 and nr - 1) should be returned
  * by "fn(i, table)".
  */
-int hash_pos(const unsigned char *hash, void *table, size_t nr,
-            hash_access_fn fn)
+int oid_pos(const struct object_id *oid, void *table, size_t nr,
+           oid_access_fn fn)
 {
        size_t hi = nr;
        size_t lo = 0;
@@ -64,9 +64,9 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
                size_t lov, hiv, miv, ofs;
 
                for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) {
-                       lov = take2(fn(0, table) + ofs);
-                       hiv = take2(fn(nr - 1, table) + ofs);
-                       miv = take2(hash + ofs);
+                       lov = take2(fn(0, table), ofs);
+                       hiv = take2(fn(nr - 1, table), ofs);
+                       miv = take2(oid, ofs);
                        if (miv < lov)
                                return -1;
                        if (hiv < miv)
@@ -88,7 +88,7 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
 
        do {
                int cmp;
-               cmp = hashcmp(fn(mi, table), hash);
+               cmp = oidcmp(fn(mi, table), oid);
                if (!cmp)
                        return mi;
                if (cmp > 0)
index 5d476dec72c7b20357a6cb448bf1147472a1c152..7b3ecad1f0331bc9fbc1562a67c95729029cdaca 100644 (file)
@@ -1,12 +1,12 @@
 #ifndef HASH_LOOKUP_H
 #define HASH_LOOKUP_H
 
-typedef const unsigned char *hash_access_fn(size_t index, void *table);
+typedef const struct object_id *oid_access_fn(size_t index, void *table);
 
-int hash_pos(const unsigned char *hash,
-            void *table,
-            size_t nr,
-            hash_access_fn fn);
+int oid_pos(const struct object_id *oid,
+           void *table,
+           size_t nr,
+           oid_access_fn fn);
 
 /*
  * Searches for hash in table, using the given fanout table to determine the
index 889b311f22777deefb87d552c791b0538b6bcf0a..a19235afbf8408734b38eddea7ef5bf3fe29e673 100644 (file)
@@ -22,16 +22,16 @@ void oid_array_sort(struct oid_array *array)
        array->sorted = 1;
 }
 
-static const unsigned char *sha1_access(size_t index, void *table)
+static const struct object_id *oid_access(size_t index, void *table)
 {
        struct object_id *array = table;
-       return array[index].hash;
+       return &array[index];
 }
 
 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
 {
        oid_array_sort(array);
-       return hash_pos(oid->hash, array->oid, array->nr, sha1_access);
+       return oid_pos(oid, array->oid, array->nr, oid_access);
 }
 
 void oid_array_clear(struct oid_array *array)
index 92460a61262822e2a8412fc3e1f1acd4007b824a..f21259dfc82fc556e698524e24193d2b584d5149 100644 (file)
@@ -610,10 +610,10 @@ static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
                die("Failed to write bitmap index");
 }
 
-static const unsigned char *sha1_access(size_t pos, void *table)
+static const struct object_id *oid_access(size_t pos, void *table)
 {
        struct pack_idx_entry **index = table;
-       return index[pos]->oid.hash;
+       return &index[pos]->oid;
 }
 
 static void write_selected_commits_v1(struct hashfile *f,
@@ -626,7 +626,7 @@ static void write_selected_commits_v1(struct hashfile *f,
                struct bitmapped_commit *stored = &writer.selected[i];
 
                int commit_pos =
-                       hash_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
+                       oid_pos(&stored->commit->object.oid, index, index_nr, oid_access);
 
                if (commit_pos < 0)
                        BUG("trying to write commit not in index");