Skip to content

Commit d40abc8

Browse files
peffgitster
authored andcommitted
hashmap: convert sha1hash() to oidhash()
There are no callers left of sha1hash() that do not simply pass the "hash" member of a "struct object_id". Let's get rid of the outdated sha1-specific function and provide one that operates on the whole struct (even though the technique, taking the first few bytes of the hash, will remain the same). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0566d7 commit d40abc8

8 files changed

+13
-11
lines changed

builtin/describe.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static int commit_name_neq(const void *unused_cmp_data,
7676

7777
static inline struct commit_name *find_commit_name(const struct object_id *peeled)
7878
{
79-
return hashmap_get_from_hash(&names, sha1hash(peeled->hash), peeled);
79+
return hashmap_get_from_hash(&names, oidhash(peeled), peeled);
8080
}
8181

8282
static int replace_name(struct commit_name *e,
@@ -123,7 +123,7 @@ static void add_to_known_names(const char *path,
123123
if (!e) {
124124
e = xmalloc(sizeof(struct commit_name));
125125
oidcpy(&e->peeled, peeled);
126-
hashmap_entry_init(e, sha1hash(peeled->hash));
126+
hashmap_entry_init(e, oidhash(peeled));
127127
hashmap_add(&names, e);
128128
e->path = NULL;
129129
}

decorate.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
static unsigned int hash_obj(const struct object *obj, unsigned int n)
1010
{
11-
return sha1hash(obj->oid.hash) % n;
11+
return oidhash(&obj->oid) % n;
1212
}
1313

1414
static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)

diffcore-rename.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static unsigned int hash_filespec(struct repository *r,
266266
hash_object_file(filespec->data, filespec->size, "blob",
267267
&filespec->oid);
268268
}
269-
return sha1hash(filespec->oid.hash);
269+
return oidhash(&filespec->oid);
270270
}
271271

272272
static int find_identical_files(struct hashmap *srcs,

hashmap.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef HASHMAP_H
22
#define HASHMAP_H
33

4+
#include "hash.h"
5+
46
/*
57
* Generic implementation of hash-based key-value mappings.
68
*
@@ -118,14 +120,14 @@ unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len);
118120
* the results will be different on big-endian and little-endian
119121
* platforms, so they should not be stored or transferred over the net.
120122
*/
121-
static inline unsigned int sha1hash(const unsigned char *sha1)
123+
static inline unsigned int oidhash(const struct object_id *oid)
122124
{
123125
/*
124-
* Equivalent to 'return *(unsigned int *)sha1;', but safe on
126+
* Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
125127
* platforms that don't support unaligned reads.
126128
*/
127129
unsigned int hash;
128-
memcpy(&hash, sha1, sizeof(hash));
130+
memcpy(&hash, oid->hash, sizeof(hash));
129131
return hash;
130132
}
131133

khash.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ static const double __ac_HASH_UPPER = 0.77;
326326

327327
static inline unsigned int oidhash_by_value(struct object_id oid)
328328
{
329-
return sha1hash(oid.hash);
329+
return oidhash(&oid);
330330
}
331331

332332
static inline int oideq_by_value(struct object_id a, struct object_id b)

object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
6161
*/
6262
static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
6363
{
64-
return sha1hash(oid->hash) & (n - 1);
64+
return oidhash(oid) & (n - 1);
6565
}
6666

6767
/*

pack-objects.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static uint32_t locate_object_entry_hash(struct packing_data *pdata,
1111
{
1212
uint32_t i, mask = (pdata->index_size - 1);
1313

14-
i = sha1hash(oid->hash) & mask;
14+
i = oidhash(oid) & mask;
1515

1616
while (pdata->index[i] > 0) {
1717
uint32_t pos = pdata->index[i] - 1;

patch-ids.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int init_patch_id_entry(struct patch_id *patch,
8383
if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0))
8484
return -1;
8585

86-
hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash));
86+
hashmap_entry_init(patch, oidhash(&header_only_patch_id));
8787
return 0;
8888
}
8989

0 commit comments

Comments
 (0)