hash-ll: merge with "hash.h"

The "hash-ll.h" header was introduced via d1cbe1e6d8 (hash-ll.h: split out of hash.h to remove dependency on repository.h, 2023-04-22) to make explicit the split between hash-related functions that rely on the global `the_repository`, and those that don't. This split is no longer necessary now that we we have removed the reliance on `the_repository`. Merge "hash-ll.h" back into "hash.h". This causes some code units to not include "repository.h" anymore, which requires us to add some forward declarations. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 14, 2024 at 08:50 UTC 8a676bdc5c3a9377322834326605b3804c7c54bf
39 files changed +400 -398
apply.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef APPLY_H
2 #define APPLY_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "lockfile.h"
6 #include "string-list.h"
7 #include "strmap.h"
bloom.c
+1
@@ -6,6 +6,7 @@
6 #include "commit-graph.h"
7 #include "commit.h"
8 #include "commit-slab.h"
9 +#include "repository.h"
10
11 define_commit_slab(bloom_filter_slab, struct bloom_filter);
12
checkout.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef CHECKOUT_H
2 #define CHECKOUT_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 /*
7 * Check if the branch name uniquely matches a branch name on a remote
chunk-format.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef CHUNK_FORMAT_H
2 #define CHUNK_FORMAT_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 struct hashfile;
7 struct chunkfile;
commit-graph.h
+2
@@ -122,6 +122,8 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
122 struct commit_graph *read_commit_graph_one(struct repository *r,
123 struct object_directory *odb);
124
125 +struct repo_settings;
126 +
127 /*
128 * Callers should initialize the repo_settings with prepare_repo_settings()
129 * prior to calling parse_commit_graph().
compat/sha1-chunked.c
+1 -1
@@ -1,5 +1,5 @@
1 #include "git-compat-util.h"
2 -#include "hash-ll.h"
2 +#include "hash.h"
3
4 int git_SHA1_Update_Chunked(platform_SHA_CTX *c, const void *data, size_t len)
5 {
convert.h
+1 -1
@@ -4,7 +4,7 @@
4 #ifndef CONVERT_H
5 #define CONVERT_H
6
7 -#include "hash-ll.h"
7 +#include "hash.h"
8 #include "string-list.h"
9
10 struct index_state;
csum-file.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef CSUM_FILE_H
2 #define CSUM_FILE_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "write-or-die.h"
6
7 struct progress;
diff.h
+1 -1
@@ -4,7 +4,7 @@
4 #ifndef DIFF_H
5 #define DIFF_H
6
7 -#include "hash-ll.h"
7 +#include "hash.h"
8 #include "pathspec.h"
9 #include "strbuf.h"
10
diffcore.h
+1 -1
@@ -4,7 +4,7 @@
4 #ifndef DIFFCORE_H
5 #define DIFFCORE_H
6
7 -#include "hash-ll.h"
7 +#include "hash.h"
8
9 struct diff_options;
10 struct mem_pool;
dir.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef DIR_H
2 #define DIR_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "hashmap.h"
6 #include "pathspec.h"
7 #include "statinfo.h"
hash-ll.h deleted
-364
@@ -1,364 +0,0 @@
1 -#ifndef HASH_LL_H
2 -#define HASH_LL_H
3 -
4 -#if defined(SHA1_APPLE)
5 -#include <CommonCrypto/CommonDigest.h>
6 -#elif defined(SHA1_OPENSSL)
7 -# include <openssl/sha.h>
8 -# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
9 -# define SHA1_NEEDS_CLONE_HELPER
10 -# include "sha1/openssl.h"
11 -# endif
12 -#elif defined(SHA1_DC)
13 -#include "sha1dc_git.h"
14 -#else /* SHA1_BLK */
15 -#include "block-sha1/sha1.h"
16 -#endif
17 -
18 -#if defined(SHA256_NETTLE)
19 -#include "sha256/nettle.h"
20 -#elif defined(SHA256_GCRYPT)
21 -#define SHA256_NEEDS_CLONE_HELPER
22 -#include "sha256/gcrypt.h"
23 -#elif defined(SHA256_OPENSSL)
24 -# include <openssl/sha.h>
25 -# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
26 -# define SHA256_NEEDS_CLONE_HELPER
27 -# include "sha256/openssl.h"
28 -# endif
29 -#else
30 -#include "sha256/block/sha256.h"
31 -#endif
32 -
33 -#ifndef platform_SHA_CTX
34 -/*
35 - * platform's underlying implementation of SHA-1; could be OpenSSL,
36 - * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
37 - * SHA-1 header may have already defined platform_SHA_CTX for our
38 - * own implementations like block-sha1, so we list
39 - * the default for OpenSSL compatible SHA-1 implementations here.
40 - */
41 -#define platform_SHA_CTX SHA_CTX
42 -#define platform_SHA1_Init SHA1_Init
43 -#define platform_SHA1_Update SHA1_Update
44 -#define platform_SHA1_Final SHA1_Final
45 -#endif
46 -
47 -#define git_SHA_CTX platform_SHA_CTX
48 -#define git_SHA1_Init platform_SHA1_Init
49 -#define git_SHA1_Update platform_SHA1_Update
50 -#define git_SHA1_Final platform_SHA1_Final
51 -
52 -#ifdef platform_SHA1_Clone
53 -#define git_SHA1_Clone platform_SHA1_Clone
54 -#endif
55 -
56 -#ifndef platform_SHA256_CTX
57 -#define platform_SHA256_CTX SHA256_CTX
58 -#define platform_SHA256_Init SHA256_Init
59 -#define platform_SHA256_Update SHA256_Update
60 -#define platform_SHA256_Final SHA256_Final
61 -#endif
62 -
63 -#define git_SHA256_CTX platform_SHA256_CTX
64 -#define git_SHA256_Init platform_SHA256_Init
65 -#define git_SHA256_Update platform_SHA256_Update
66 -#define git_SHA256_Final platform_SHA256_Final
67 -
68 -#ifdef platform_SHA256_Clone
69 -#define git_SHA256_Clone platform_SHA256_Clone
70 -#endif
71 -
72 -#ifdef SHA1_MAX_BLOCK_SIZE
73 -#include "compat/sha1-chunked.h"
74 -#undef git_SHA1_Update
75 -#define git_SHA1_Update git_SHA1_Update_Chunked
76 -#endif
77 -
78 -#ifndef SHA1_NEEDS_CLONE_HELPER
79 -static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
80 -{
81 - memcpy(dst, src, sizeof(*dst));
82 -}
83 -#endif
84 -
85 -#ifndef SHA256_NEEDS_CLONE_HELPER
86 -static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
87 -{
88 - memcpy(dst, src, sizeof(*dst));
89 -}
90 -#endif
91 -
92 -/*
93 - * Note that these constants are suitable for indexing the hash_algos array and
94 - * comparing against each other, but are otherwise arbitrary, so they should not
95 - * be exposed to the user or serialized to disk. To know whether a
96 - * git_hash_algo struct points to some usable hash function, test the format_id
97 - * field for being non-zero. Use the name field for user-visible situations and
98 - * the format_id field for fixed-length fields on disk.
99 - */
100 -/* An unknown hash function. */
101 -#define GIT_HASH_UNKNOWN 0
102 -/* SHA-1 */
103 -#define GIT_HASH_SHA1 1
104 -/* SHA-256 */
105 -#define GIT_HASH_SHA256 2
106 -/* Number of algorithms supported (including unknown). */
107 -#define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
108 -
109 -/* "sha1", big-endian */
110 -#define GIT_SHA1_FORMAT_ID 0x73686131
111 -
112 -/* The length in bytes and in hex digits of an object name (SHA-1 value). */
113 -#define GIT_SHA1_RAWSZ 20
114 -#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
115 -/* The block size of SHA-1. */
116 -#define GIT_SHA1_BLKSZ 64
117 -
118 -/* "s256", big-endian */
119 -#define GIT_SHA256_FORMAT_ID 0x73323536
120 -
121 -/* The length in bytes and in hex digits of an object name (SHA-256 value). */
122 -#define GIT_SHA256_RAWSZ 32
123 -#define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
124 -/* The block size of SHA-256. */
125 -#define GIT_SHA256_BLKSZ 64
126 -
127 -/* The length in byte and in hex digits of the largest possible hash value. */
128 -#define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
129 -#define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
130 -/* The largest possible block size for any supported hash. */
131 -#define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
132 -
133 -struct object_id {
134 - unsigned char hash[GIT_MAX_RAWSZ];
135 - int algo; /* XXX requires 4-byte alignment */
136 -};
137 -
138 -#define GET_OID_QUIETLY 01
139 -#define GET_OID_COMMIT 02
140 -#define GET_OID_COMMITTISH 04
141 -#define GET_OID_TREE 010
142 -#define GET_OID_TREEISH 020
143 -#define GET_OID_BLOB 040
144 -#define GET_OID_FOLLOW_SYMLINKS 0100
145 -#define GET_OID_RECORD_PATH 0200
146 -#define GET_OID_ONLY_TO_DIE 04000
147 -#define GET_OID_REQUIRE_PATH 010000
148 -#define GET_OID_HASH_ANY 020000
149 -
150 -#define GET_OID_DISAMBIGUATORS \
151 - (GET_OID_COMMIT | GET_OID_COMMITTISH | \
152 - GET_OID_TREE | GET_OID_TREEISH | \
153 - GET_OID_BLOB)
154 -
155 -enum get_oid_result {
156 - FOUND = 0,
157 - MISSING_OBJECT = -1, /* The requested object is missing */
158 - SHORT_NAME_AMBIGUOUS = -2,
159 - /* The following only apply when symlinks are followed */
160 - DANGLING_SYMLINK = -4, /*
161 - * The initial symlink is there, but
162 - * (transitively) points to a missing
163 - * in-tree file
164 - */
165 - SYMLINK_LOOP = -5,
166 - NOT_DIR = -6, /*
167 - * Somewhere along the symlink chain, a path is
168 - * requested which contains a file as a
169 - * non-final element.
170 - */
171 -};
172 -
173 -/* A suitably aligned type for stack allocations of hash contexts. */
174 -union git_hash_ctx {
175 - git_SHA_CTX sha1;
176 - git_SHA256_CTX sha256;
177 -};
178 -typedef union git_hash_ctx git_hash_ctx;
179 -
180 -typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
181 -typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
182 -typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
183 -typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
184 -typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
185 -
186 -struct git_hash_algo {
187 - /*
188 - * The name of the algorithm, as appears in the config file and in
189 - * messages.
190 - */
191 - const char *name;
192 -
193 - /* A four-byte version identifier, used in pack indices. */
194 - uint32_t format_id;
195 -
196 - /* The length of the hash in binary. */
197 - size_t rawsz;
198 -
199 - /* The length of the hash in hex characters. */
200 - size_t hexsz;
201 -
202 - /* The block size of the hash. */
203 - size_t blksz;
204 -
205 - /* The hash initialization function. */
206 - git_hash_init_fn init_fn;
207 -
208 - /* The hash context cloning function. */
209 - git_hash_clone_fn clone_fn;
210 -
211 - /* The hash update function. */
212 - git_hash_update_fn update_fn;
213 -
214 - /* The hash finalization function. */
215 - git_hash_final_fn final_fn;
216 -
217 - /* The hash finalization function for object IDs. */
218 - git_hash_final_oid_fn final_oid_fn;
219 -
220 - /* The OID of the empty tree. */
221 - const struct object_id *empty_tree;
222 -
223 - /* The OID of the empty blob. */
224 - const struct object_id *empty_blob;
225 -
226 - /* The all-zeros OID. */
227 - const struct object_id *null_oid;
228 -};
229 -extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
230 -
231 -/*
232 - * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
233 - * the name doesn't match a known algorithm.
234 - */
235 -int hash_algo_by_name(const char *name);
236 -/* Identical, except based on the format ID. */
237 -int hash_algo_by_id(uint32_t format_id);
238 -/* Identical, except based on the length. */
239 -int hash_algo_by_length(int len);
240 -/* Identical, except for a pointer to struct git_hash_algo. */
241 -static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
242 -{
243 - return p - hash_algos;
244 -}
245 -
246 -const struct object_id *null_oid(void);
247 -
248 -static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
249 -{
250 - /*
251 - * Teach the compiler that there are only two possibilities of hash size
252 - * here, so that it can optimize for this case as much as possible.
253 - */
254 - if (algop->rawsz == GIT_MAX_RAWSZ)
255 - return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
256 - return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
257 -}
258 -
259 -static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
260 -{
261 - /*
262 - * We write this here instead of deferring to hashcmp so that the
263 - * compiler can properly inline it and avoid calling memcmp.
264 - */
265 - if (algop->rawsz == GIT_MAX_RAWSZ)
266 - return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
267 - return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
268 -}
269 -
270 -static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src,
271 - const struct git_hash_algo *algop)
272 -{
273 - memcpy(sha_dst, sha_src, algop->rawsz);
274 -}
275 -
276 -static inline void hashclr(unsigned char *hash, const struct git_hash_algo *algop)
277 -{
278 - memset(hash, 0, algop->rawsz);
279 -}
280 -
281 -static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
282 -{
283 - return memcmp(oid1->hash, oid2->hash, GIT_MAX_RAWSZ);
284 -}
285 -
286 -static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
287 -{
288 - return !memcmp(oid1->hash, oid2->hash, GIT_MAX_RAWSZ);
289 -}
290 -
291 -static inline void oidcpy(struct object_id *dst, const struct object_id *src)
292 -{
293 - memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
294 - dst->algo = src->algo;
295 -}
296 -
297 -static inline void oidread(struct object_id *oid, const unsigned char *hash,
298 - const struct git_hash_algo *algop)
299 -{
300 - memcpy(oid->hash, hash, algop->rawsz);
301 - if (algop->rawsz < GIT_MAX_RAWSZ)
302 - memset(oid->hash + algop->rawsz, 0, GIT_MAX_RAWSZ - algop->rawsz);
303 - oid->algo = hash_algo_by_ptr(algop);
304 -}
305 -
306 -static inline void oidclr(struct object_id *oid,
307 - const struct git_hash_algo *algop)
308 -{
309 - memset(oid->hash, 0, GIT_MAX_RAWSZ);
310 - oid->algo = hash_algo_by_ptr(algop);
311 -}
312 -
313 -static inline struct object_id *oiddup(const struct object_id *src)
314 -{
315 - struct object_id *dst = xmalloc(sizeof(struct object_id));
316 - oidcpy(dst, src);
317 - return dst;
318 -}
319 -
320 -static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
321 -{
322 - oid->algo = hash_algo_by_ptr(algop);
323 -}
324 -
325 -/*
326 - * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
327 - * for use in hash tables. Cryptographic hashes are supposed to have
328 - * uniform distribution, so in contrast to `memhash()`, this just copies
329 - * the first `sizeof(int)` bytes without shuffling any bits. Note that
330 - * the results will be different on big-endian and little-endian
331 - * platforms, so they should not be stored or transferred over the net.
332 - */
333 -static inline unsigned int oidhash(const struct object_id *oid)
334 -{
335 - /*
336 - * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
337 - * platforms that don't support unaligned reads.
338 - */
339 - unsigned int hash;
340 - memcpy(&hash, oid->hash, sizeof(hash));
341 - return hash;
342 -}
343 -
344 -static inline int is_null_oid(const struct object_id *oid)
345 -{
346 - static const unsigned char null_hash[GIT_MAX_RAWSZ];
347 - return !memcmp(oid->hash, null_hash, GIT_MAX_RAWSZ);
348 -}
349 -
350 -const char *empty_tree_oid_hex(const struct git_hash_algo *algop);
351 -
352 -static inline int is_empty_blob_oid(const struct object_id *oid,
353 - const struct git_hash_algo *algop)
354 -{
355 - return oideq(oid, algop->empty_blob);
356 -}
357 -
358 -static inline int is_empty_tree_oid(const struct object_id *oid,
359 - const struct git_hash_algo *algop)
360 -{
361 - return oideq(oid, algop->empty_tree);
362 -}
363 -
364 -#endif
hash.h
+360 -2
@@ -1,11 +1,369 @@
1 #ifndef HASH_H
2 #define HASH_H
3
4 -#include "hash-ll.h"
5 -#include "repository.h"
4 +#if defined(SHA1_APPLE)
5 +#include <CommonCrypto/CommonDigest.h>
6 +#elif defined(SHA1_OPENSSL)
7 +# include <openssl/sha.h>
8 +# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
9 +# define SHA1_NEEDS_CLONE_HELPER
10 +# include "sha1/openssl.h"
11 +# endif
12 +#elif defined(SHA1_DC)
13 +#include "sha1dc_git.h"
14 +#else /* SHA1_BLK */
15 +#include "block-sha1/sha1.h"
16 +#endif
17 +
18 +#if defined(SHA256_NETTLE)
19 +#include "sha256/nettle.h"
20 +#elif defined(SHA256_GCRYPT)
21 +#define SHA256_NEEDS_CLONE_HELPER
22 +#include "sha256/gcrypt.h"
23 +#elif defined(SHA256_OPENSSL)
24 +# include <openssl/sha.h>
25 +# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
26 +# define SHA256_NEEDS_CLONE_HELPER
27 +# include "sha256/openssl.h"
28 +# endif
29 +#else
30 +#include "sha256/block/sha256.h"
31 +#endif
32 +
33 +#ifndef platform_SHA_CTX
34 +/*
35 + * platform's underlying implementation of SHA-1; could be OpenSSL,
36 + * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
37 + * SHA-1 header may have already defined platform_SHA_CTX for our
38 + * own implementations like block-sha1, so we list
39 + * the default for OpenSSL compatible SHA-1 implementations here.
40 + */
41 +#define platform_SHA_CTX SHA_CTX
42 +#define platform_SHA1_Init SHA1_Init
43 +#define platform_SHA1_Update SHA1_Update
44 +#define platform_SHA1_Final SHA1_Final
45 +#endif
46 +
47 +#define git_SHA_CTX platform_SHA_CTX
48 +#define git_SHA1_Init platform_SHA1_Init
49 +#define git_SHA1_Update platform_SHA1_Update
50 +#define git_SHA1_Final platform_SHA1_Final
51 +
52 +#ifdef platform_SHA1_Clone
53 +#define git_SHA1_Clone platform_SHA1_Clone
54 +#endif
55 +
56 +#ifndef platform_SHA256_CTX
57 +#define platform_SHA256_CTX SHA256_CTX
58 +#define platform_SHA256_Init SHA256_Init
59 +#define platform_SHA256_Update SHA256_Update
60 +#define platform_SHA256_Final SHA256_Final
61 +#endif
62 +
63 +#define git_SHA256_CTX platform_SHA256_CTX
64 +#define git_SHA256_Init platform_SHA256_Init
65 +#define git_SHA256_Update platform_SHA256_Update
66 +#define git_SHA256_Final platform_SHA256_Final
67 +
68 +#ifdef platform_SHA256_Clone
69 +#define git_SHA256_Clone platform_SHA256_Clone
70 +#endif
71 +
72 +#ifdef SHA1_MAX_BLOCK_SIZE
73 +#include "compat/sha1-chunked.h"
74 +#undef git_SHA1_Update
75 +#define git_SHA1_Update git_SHA1_Update_Chunked
76 +#endif
77 +
78 +#ifndef SHA1_NEEDS_CLONE_HELPER
79 +static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
80 +{
81 + memcpy(dst, src, sizeof(*dst));
82 +}
83 +#endif
84 +
85 +#ifndef SHA256_NEEDS_CLONE_HELPER
86 +static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
87 +{
88 + memcpy(dst, src, sizeof(*dst));
89 +}
90 +#endif
91 +
92 +/*
93 + * Note that these constants are suitable for indexing the hash_algos array and
94 + * comparing against each other, but are otherwise arbitrary, so they should not
95 + * be exposed to the user or serialized to disk. To know whether a
96 + * git_hash_algo struct points to some usable hash function, test the format_id
97 + * field for being non-zero. Use the name field for user-visible situations and
98 + * the format_id field for fixed-length fields on disk.
99 + */
100 +/* An unknown hash function. */
101 +#define GIT_HASH_UNKNOWN 0
102 +/* SHA-1 */
103 +#define GIT_HASH_SHA1 1
104 +/* SHA-256 */
105 +#define GIT_HASH_SHA256 2
106 +/* Number of algorithms supported (including unknown). */
107 +#define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
108 +
109 +/* "sha1", big-endian */
110 +#define GIT_SHA1_FORMAT_ID 0x73686131
111 +
112 +/* The length in bytes and in hex digits of an object name (SHA-1 value). */
113 +#define GIT_SHA1_RAWSZ 20
114 +#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
115 +/* The block size of SHA-1. */
116 +#define GIT_SHA1_BLKSZ 64
117 +
118 +/* "s256", big-endian */
119 +#define GIT_SHA256_FORMAT_ID 0x73323536
120 +
121 +/* The length in bytes and in hex digits of an object name (SHA-256 value). */
122 +#define GIT_SHA256_RAWSZ 32
123 +#define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
124 +/* The block size of SHA-256. */
125 +#define GIT_SHA256_BLKSZ 64
126 +
127 +/* The length in byte and in hex digits of the largest possible hash value. */
128 +#define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
129 +#define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
130 +/* The largest possible block size for any supported hash. */
131 +#define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
132 +
133 +struct object_id {
134 + unsigned char hash[GIT_MAX_RAWSZ];
135 + int algo; /* XXX requires 4-byte alignment */
136 +};
137 +
138 +#define GET_OID_QUIETLY 01
139 +#define GET_OID_COMMIT 02
140 +#define GET_OID_COMMITTISH 04
141 +#define GET_OID_TREE 010
142 +#define GET_OID_TREEISH 020
143 +#define GET_OID_BLOB 040
144 +#define GET_OID_FOLLOW_SYMLINKS 0100
145 +#define GET_OID_RECORD_PATH 0200
146 +#define GET_OID_ONLY_TO_DIE 04000
147 +#define GET_OID_REQUIRE_PATH 010000
148 +#define GET_OID_HASH_ANY 020000
149 +
150 +#define GET_OID_DISAMBIGUATORS \
151 + (GET_OID_COMMIT | GET_OID_COMMITTISH | \
152 + GET_OID_TREE | GET_OID_TREEISH | \
153 + GET_OID_BLOB)
154 +
155 +enum get_oid_result {
156 + FOUND = 0,
157 + MISSING_OBJECT = -1, /* The requested object is missing */
158 + SHORT_NAME_AMBIGUOUS = -2,
159 + /* The following only apply when symlinks are followed */
160 + DANGLING_SYMLINK = -4, /*
161 + * The initial symlink is there, but
162 + * (transitively) points to a missing
163 + * in-tree file
164 + */
165 + SYMLINK_LOOP = -5,
166 + NOT_DIR = -6, /*
167 + * Somewhere along the symlink chain, a path is
168 + * requested which contains a file as a
169 + * non-final element.
170 + */
171 +};
172
173 #ifdef USE_THE_REPOSITORY_VARIABLE
174 +# include "repository.h"
175 # define the_hash_algo the_repository->hash_algo
176 #endif
177
178 +/* A suitably aligned type for stack allocations of hash contexts. */
179 +union git_hash_ctx {
180 + git_SHA_CTX sha1;
181 + git_SHA256_CTX sha256;
182 +};
183 +typedef union git_hash_ctx git_hash_ctx;
184 +
185 +typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
186 +typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
187 +typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
188 +typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
189 +typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
190 +
191 +struct git_hash_algo {
192 + /*
193 + * The name of the algorithm, as appears in the config file and in
194 + * messages.
195 + */
196 + const char *name;
197 +
198 + /* A four-byte version identifier, used in pack indices. */
199 + uint32_t format_id;
200 +
201 + /* The length of the hash in binary. */
202 + size_t rawsz;
203 +
204 + /* The length of the hash in hex characters. */
205 + size_t hexsz;
206 +
207 + /* The block size of the hash. */
208 + size_t blksz;
209 +
210 + /* The hash initialization function. */
211 + git_hash_init_fn init_fn;
212 +
213 + /* The hash context cloning function. */
214 + git_hash_clone_fn clone_fn;
215 +
216 + /* The hash update function. */
217 + git_hash_update_fn update_fn;
218 +
219 + /* The hash finalization function. */
220 + git_hash_final_fn final_fn;
221 +
222 + /* The hash finalization function for object IDs. */
223 + git_hash_final_oid_fn final_oid_fn;
224 +
225 + /* The OID of the empty tree. */
226 + const struct object_id *empty_tree;
227 +
228 + /* The OID of the empty blob. */
229 + const struct object_id *empty_blob;
230 +
231 + /* The all-zeros OID. */
232 + const struct object_id *null_oid;
233 +};
234 +extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
235 +
236 +/*
237 + * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
238 + * the name doesn't match a known algorithm.
239 + */
240 +int hash_algo_by_name(const char *name);
241 +/* Identical, except based on the format ID. */
242 +int hash_algo_by_id(uint32_t format_id);
243 +/* Identical, except based on the length. */
244 +int hash_algo_by_length(int len);
245 +/* Identical, except for a pointer to struct git_hash_algo. */
246 +static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
247 +{
248 + return p - hash_algos;
249 +}
250 +
251 +const struct object_id *null_oid(void);
252 +
253 +static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
254 +{
255 + /*
256 + * Teach the compiler that there are only two possibilities of hash size
257 + * here, so that it can optimize for this case as much as possible.
258 + */
259 + if (algop->rawsz == GIT_MAX_RAWSZ)
260 + return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
261 + return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
262 +}
263 +
264 +static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
265 +{
266 + /*
267 + * We write this here instead of deferring to hashcmp so that the
268 + * compiler can properly inline it and avoid calling memcmp.
269 + */
270 + if (algop->rawsz == GIT_MAX_RAWSZ)
271 + return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
272 + return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
273 +}
274 +
275 +static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src,
276 + const struct git_hash_algo *algop)
277 +{
278 + memcpy(sha_dst, sha_src, algop->rawsz);
279 +}
280 +
281 +static inline void hashclr(unsigned char *hash, const struct git_hash_algo *algop)
282 +{
283 + memset(hash, 0, algop->rawsz);
284 +}
285 +
286 +static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
287 +{
288 + return memcmp(oid1->hash, oid2->hash, GIT_MAX_RAWSZ);
289 +}
290 +
291 +static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
292 +{
293 + return !memcmp(oid1->hash, oid2->hash, GIT_MAX_RAWSZ);
294 +}
295 +
296 +static inline void oidcpy(struct object_id *dst, const struct object_id *src)
297 +{
298 + memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
299 + dst->algo = src->algo;
300 +}
301 +
302 +static inline void oidread(struct object_id *oid, const unsigned char *hash,
303 + const struct git_hash_algo *algop)
304 +{
305 + memcpy(oid->hash, hash, algop->rawsz);
306 + if (algop->rawsz < GIT_MAX_RAWSZ)
307 + memset(oid->hash + algop->rawsz, 0, GIT_MAX_RAWSZ - algop->rawsz);
308 + oid->algo = hash_algo_by_ptr(algop);
309 +}
310 +
311 +static inline void oidclr(struct object_id *oid,
312 + const struct git_hash_algo *algop)
313 +{
314 + memset(oid->hash, 0, GIT_MAX_RAWSZ);
315 + oid->algo = hash_algo_by_ptr(algop);
316 +}
317 +
318 +static inline struct object_id *oiddup(const struct object_id *src)
319 +{
320 + struct object_id *dst = xmalloc(sizeof(struct object_id));
321 + oidcpy(dst, src);
322 + return dst;
323 +}
324 +
325 +static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
326 +{
327 + oid->algo = hash_algo_by_ptr(algop);
328 +}
329 +
330 +/*
331 + * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
332 + * for use in hash tables. Cryptographic hashes are supposed to have
333 + * uniform distribution, so in contrast to `memhash()`, this just copies
334 + * the first `sizeof(int)` bytes without shuffling any bits. Note that
335 + * the results will be different on big-endian and little-endian
336 + * platforms, so they should not be stored or transferred over the net.
337 + */
338 +static inline unsigned int oidhash(const struct object_id *oid)
339 +{
340 + /*
341 + * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
342 + * platforms that don't support unaligned reads.
343 + */
344 + unsigned int hash;
345 + memcpy(&hash, oid->hash, sizeof(hash));
346 + return hash;
347 +}
348 +
349 +static inline int is_null_oid(const struct object_id *oid)
350 +{
351 + static const unsigned char null_hash[GIT_MAX_RAWSZ];
352 + return !memcmp(oid->hash, null_hash, GIT_MAX_RAWSZ);
353 +}
354 +
355 +const char *empty_tree_oid_hex(const struct git_hash_algo *algop);
356 +
357 +static inline int is_empty_blob_oid(const struct object_id *oid,
358 + const struct git_hash_algo *algop)
359 +{
360 + return oideq(oid, algop->empty_blob);
361 +}
362 +
363 +static inline int is_empty_tree_oid(const struct object_id *oid,
364 + const struct git_hash_algo *algop)
365 +{
366 + return oideq(oid, algop->empty_tree);
367 +}
368 +
369 #endif
hex.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef HEX_H
2 #define HEX_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "hex-ll.h"
6
7 /*
loose.h
+2
@@ -3,6 +3,8 @@
3
4 #include "khash.h"
5
6 +struct repository;
7 +
8 struct loose_object_map {
9 kh_oid_map_t *to_compat;
10 kh_oid_map_t *to_storage;
merge-ort.h
+1 -1
@@ -2,7 +2,7 @@
2 #define MERGE_ORT_H
3
4 #include "merge-recursive.h"
5 -#include "hash-ll.h"
5 +#include "hash.h"
6
7 struct commit;
8 struct tree;
object-file-convert.c
+1 -1
@@ -5,7 +5,7 @@
5 #include "strbuf.h"
6 #include "hex.h"
7 #include "repository.h"
8 -#include "hash-ll.h"
8 +#include "hash.h"
9 #include "hash.h"
10 #include "object.h"
11 #include "loose.h"
object.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef OBJECT_H
2 #define OBJECT_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 struct buffer_slab;
7 struct repository;
oidmap.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef OIDMAP_H
2 #define OIDMAP_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "hashmap.h"
6
7 /*
oidtree.h
+1 -1
@@ -2,7 +2,7 @@
2 #define OIDTREE_H
3
4 #include "cbtree.h"
5 -#include "hash-ll.h"
5 +#include "hash.h"
6 #include "mem-pool.h"
7
8 struct oidtree {
packfile.h
+2
@@ -101,6 +101,8 @@ int close_pack_fd(struct packed_git *p);
101
102 uint32_t get_pack_fanout(struct packed_git *p, uint32_t value);
103
104 +struct raw_object_store;
105 +
106 unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
107 void close_pack_windows(struct packed_git *);
108 void close_pack(struct packed_git *);
protocol-caps.c
+1 -1
@@ -3,7 +3,7 @@
3 #include "gettext.h"
4 #include "hex.h"
5 #include "pkt-line.h"
6 -#include "hash-ll.h"
6 +#include "hash.h"
7 #include "hex.h"
8 #include "object.h"
9 #include "object-store-ll.h"
read-cache-ll.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef READ_CACHE_LL_H
2 #define READ_CACHE_LL_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "hashmap.h"
6 #include "statinfo.h"
7
refs/ref-cache.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef REFS_REF_CACHE_H
2 #define REFS_REF_CACHE_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 struct ref_dir;
7 struct ref_store;
reftable/dump.c
+1 -1
@@ -7,7 +7,7 @@ https://developers.google.com/open-source/licenses/bsd
7 */
8
9 #include "git-compat-util.h"
10 -#include "hash-ll.h"
10 +#include "hash.h"
11
12 #include "reftable-blocksource.h"
13 #include "reftable-error.h"
reftable/reftable-record.h
+1 -1
@@ -9,7 +9,7 @@ https://developers.google.com/open-source/licenses/bsd
9 #ifndef REFTABLE_RECORD_H
10 #define REFTABLE_RECORD_H
11
12 -#include "hash-ll.h"
12 +#include "hash.h"
13 #include <stdint.h>
14
15 /*
reftable/system.h
+1 -1
@@ -15,7 +15,7 @@ https://developers.google.com/open-source/licenses/bsd
15 #include "lockfile.h"
16 #include "strbuf.h"
17 #include "tempfile.h"
18 -#include "hash-ll.h" /* hash ID, sizes.*/
18 +#include "hash.h" /* hash ID, sizes.*/
19 #include "dir.h" /* remove_dir_recursively, for tests.*/
20
21 int hash_size(uint32_t id);
remote.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef REMOTE_H
2 #define REMOTE_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "hashmap.h"
6 #include "refspec.h"
7
reset.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef RESET_H
2 #define RESET_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "repository.h"
6
7 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
resolve-undo.h
+1 -1
@@ -6,7 +6,7 @@ struct index_state;
6 struct pathspec;
7 struct string_list;
8
9 -#include "hash-ll.h"
9 +#include "hash.h"
10
11 struct resolve_undo_info {
12 unsigned int mode[3];
serve.c
+1 -1
@@ -3,7 +3,7 @@
3 #include "git-compat-util.h"
4 #include "repository.h"
5 #include "config.h"
6 -#include "hash-ll.h"
6 +#include "hash.h"
7 #include "pkt-line.h"
8 #include "version.h"
9 #include "ls-refs.h"
split-index.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef SPLIT_INDEX_H
2 #define SPLIT_INDEX_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 struct index_state;
7 struct strbuf;
t/helper/test-hash-speed.c
+1 -1
@@ -1,5 +1,5 @@
1 #include "test-tool.h"
2 -#include "hash-ll.h"
2 +#include "hash.h"
3
4 #define NUM_SECONDS 3
5
t/helper/test-sha1.c
+1 -1
@@ -1,5 +1,5 @@
1 #include "test-tool.h"
2 -#include "hash-ll.h"
2 +#include "hash.h"
3
4 int cmd__sha1(int ac, const char **av)
5 {
t/helper/test-sha256.c
+1 -1
@@ -1,5 +1,5 @@
1 #include "test-tool.h"
2 -#include "hash-ll.h"
2 +#include "hash.h"
3
4 int cmd__sha256(int ac, const char **av)
5 {
t/unit-tests/lib-oid.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef LIB_OID_H
2 #define LIB_OID_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 /*
7 * Convert arbitrary hex string to object_id.
tree-diff.c
+1
@@ -8,6 +8,7 @@
8 #include "tree.h"
9 #include "tree-walk.h"
10 #include "environment.h"
11 +#include "repository.h"
12
13 /*
14 * Some mode bits are also used internally for computations.
tree-walk.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef TREE_WALK_H
2 #define TREE_WALK_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5
6 struct index_state;
7 struct repository;
xdiff-interface.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef XDIFF_INTERFACE_H
2 #define XDIFF_INTERFACE_H
3
4 -#include "hash-ll.h"
4 +#include "hash.h"
5 #include "xdiff/xdiff.h"
6
7 /*