Skip to content

Commit 3fa6f2a

Browse files
peffgitster
authored andcommitted
cache.h: move hash/oid functions to hash.h
We define git_hash_algo and object_id in hash.h, but most of the utility functions are declared in the main cache.h. Let's move them to hash.h along with their struct definitions. This cleans up cache.h a bit, but also avoids circular dependencies when other headers need to know about these functions (e.g., if oid-array.h were to have an inline that used oideq(), it couldn't include cache.h because it is itself included by cache.h). No including C files should be affected, because hash.h is always included in cache.h already. We do have to mention repository.h at the top of hash.h, though, since we depend on the_repository in some of our inline functions. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ea922f commit 3fa6f2a

File tree

2 files changed

+95
-94
lines changed

2 files changed

+95
-94
lines changed

cache.h

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,100 +1123,6 @@ const char *repo_find_unique_abbrev(struct repository *r, const struct object_id
11231123
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
11241124
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)
11251125

1126-
extern const struct object_id null_oid;
1127-
1128-
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
1129-
{
1130-
/*
1131-
* Teach the compiler that there are only two possibilities of hash size
1132-
* here, so that it can optimize for this case as much as possible.
1133-
*/
1134-
if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
1135-
return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
1136-
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
1137-
}
1138-
1139-
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
1140-
{
1141-
return hashcmp(oid1->hash, oid2->hash);
1142-
}
1143-
1144-
static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
1145-
{
1146-
/*
1147-
* We write this here instead of deferring to hashcmp so that the
1148-
* compiler can properly inline it and avoid calling memcmp.
1149-
*/
1150-
if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
1151-
return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
1152-
return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
1153-
}
1154-
1155-
static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
1156-
{
1157-
return hasheq(oid1->hash, oid2->hash);
1158-
}
1159-
1160-
static inline int is_null_oid(const struct object_id *oid)
1161-
{
1162-
return oideq(oid, &null_oid);
1163-
}
1164-
1165-
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
1166-
{
1167-
memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
1168-
}
1169-
1170-
static inline void oidcpy(struct object_id *dst, const struct object_id *src)
1171-
{
1172-
memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
1173-
}
1174-
1175-
static inline struct object_id *oiddup(const struct object_id *src)
1176-
{
1177-
struct object_id *dst = xmalloc(sizeof(struct object_id));
1178-
oidcpy(dst, src);
1179-
return dst;
1180-
}
1181-
1182-
static inline void hashclr(unsigned char *hash)
1183-
{
1184-
memset(hash, 0, the_hash_algo->rawsz);
1185-
}
1186-
1187-
static inline void oidclr(struct object_id *oid)
1188-
{
1189-
memset(oid->hash, 0, GIT_MAX_RAWSZ);
1190-
}
1191-
1192-
static inline void oidread(struct object_id *oid, const unsigned char *hash)
1193-
{
1194-
memcpy(oid->hash, hash, the_hash_algo->rawsz);
1195-
}
1196-
1197-
static inline int is_empty_blob_sha1(const unsigned char *sha1)
1198-
{
1199-
return hasheq(sha1, the_hash_algo->empty_blob->hash);
1200-
}
1201-
1202-
static inline int is_empty_blob_oid(const struct object_id *oid)
1203-
{
1204-
return oideq(oid, the_hash_algo->empty_blob);
1205-
}
1206-
1207-
static inline int is_empty_tree_sha1(const unsigned char *sha1)
1208-
{
1209-
return hasheq(sha1, the_hash_algo->empty_tree->hash);
1210-
}
1211-
1212-
static inline int is_empty_tree_oid(const struct object_id *oid)
1213-
{
1214-
return oideq(oid, the_hash_algo->empty_tree);
1215-
}
1216-
1217-
const char *empty_tree_oid_hex(void);
1218-
const char *empty_blob_oid_hex(void);
1219-
12201126
/* set default permissions by passing mode arguments to open(2) */
12211127
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
12221128
int git_mkstemp_mode(char *pattern, int mode);

hash.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define HASH_H
33

44
#include "git-compat-util.h"
5+
#include "repository.h"
56

67
#if defined(SHA1_PPC)
78
#include "ppc/sha1.h"
@@ -184,4 +185,98 @@ struct object_id {
184185

185186
#define the_hash_algo the_repository->hash_algo
186187

188+
extern const struct object_id null_oid;
189+
190+
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
191+
{
192+
/*
193+
* Teach the compiler that there are only two possibilities of hash size
194+
* here, so that it can optimize for this case as much as possible.
195+
*/
196+
if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
197+
return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
198+
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
199+
}
200+
201+
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
202+
{
203+
return hashcmp(oid1->hash, oid2->hash);
204+
}
205+
206+
static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
207+
{
208+
/*
209+
* We write this here instead of deferring to hashcmp so that the
210+
* compiler can properly inline it and avoid calling memcmp.
211+
*/
212+
if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
213+
return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
214+
return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
215+
}
216+
217+
static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
218+
{
219+
return hasheq(oid1->hash, oid2->hash);
220+
}
221+
222+
static inline int is_null_oid(const struct object_id *oid)
223+
{
224+
return oideq(oid, &null_oid);
225+
}
226+
227+
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
228+
{
229+
memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
230+
}
231+
232+
static inline void oidcpy(struct object_id *dst, const struct object_id *src)
233+
{
234+
memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
235+
}
236+
237+
static inline struct object_id *oiddup(const struct object_id *src)
238+
{
239+
struct object_id *dst = xmalloc(sizeof(struct object_id));
240+
oidcpy(dst, src);
241+
return dst;
242+
}
243+
244+
static inline void hashclr(unsigned char *hash)
245+
{
246+
memset(hash, 0, the_hash_algo->rawsz);
247+
}
248+
249+
static inline void oidclr(struct object_id *oid)
250+
{
251+
memset(oid->hash, 0, GIT_MAX_RAWSZ);
252+
}
253+
254+
static inline void oidread(struct object_id *oid, const unsigned char *hash)
255+
{
256+
memcpy(oid->hash, hash, the_hash_algo->rawsz);
257+
}
258+
259+
static inline int is_empty_blob_sha1(const unsigned char *sha1)
260+
{
261+
return hasheq(sha1, the_hash_algo->empty_blob->hash);
262+
}
263+
264+
static inline int is_empty_blob_oid(const struct object_id *oid)
265+
{
266+
return oideq(oid, the_hash_algo->empty_blob);
267+
}
268+
269+
static inline int is_empty_tree_sha1(const unsigned char *sha1)
270+
{
271+
return hasheq(sha1, the_hash_algo->empty_tree->hash);
272+
}
273+
274+
static inline int is_empty_tree_oid(const struct object_id *oid)
275+
{
276+
return oideq(oid, the_hash_algo->empty_tree);
277+
}
278+
279+
const char *empty_tree_oid_hex(void);
280+
const char *empty_blob_oid_hex(void);
281+
187282
#endif

0 commit comments

Comments
 (0)