@matheusbsbs / matheus-bagliano / commits / ce9fbf16e0
Fork 0

index-pack: use hash_sha1_file()

Use hash_sha1_file() instead of duplicating code to compute object SHA1. While at it make it accept a const pointer. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Mar 20, 2007 at 16:02 UTC ce9fbf16e0000ecca92870a90e4b30afcb3917b7
3 files changed +4 -23
cache.h
+1 -1
@@ -283,7 +283,7 @@ char *enter_repo(char *path, int strict);
283 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
284 extern int sha1_object_info(const unsigned char *, unsigned long *);
285 extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
286 -extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
286 +extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
287 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
288 extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
289
index-pack.c
+1 -20
@@ -348,26 +348,7 @@ static void sha1_object(const void *data, unsigned long size,
348 enum object_type type, unsigned char *sha1,
349 int test_for_collision)
350 {
351 - SHA_CTX ctx;
352 - char header[50];
353 - int header_size;
354 - const char *type_str;
355 -
356 - switch (type) {
357 - case OBJ_COMMIT: type_str = commit_type; break;
358 - case OBJ_TREE: type_str = tree_type; break;
359 - case OBJ_BLOB: type_str = blob_type; break;
360 - case OBJ_TAG: type_str = tag_type; break;
361 - default:
362 - die("bad type %d", type);
363 - }
364 -
365 - header_size = sprintf(header, "%s %lu", type_str, size) + 1;
366 -
367 - SHA1_Init(&ctx);
368 - SHA1_Update(&ctx, header, header_size);
369 - SHA1_Update(&ctx, data, size);
370 - SHA1_Final(sha1, &ctx);
351 + hash_sha1_file(data, size, typename(type), sha1);
352
353 if (test_for_collision && has_sha1_file(sha1)) {
354 void *has_data;
sha1_file.c
+2 -2
@@ -1808,7 +1808,7 @@ void *read_object_with_reference(const unsigned char *sha1,
1808 }
1809 }
1810
1811 -static void write_sha1_file_prepare(void *buf, unsigned long len,
1811 +static void write_sha1_file_prepare(const void *buf, unsigned long len,
1812 const char *type, unsigned char *sha1,
1813 char *hdr, int *hdrlen)
1814 {
@@ -1936,7 +1936,7 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon
1936 stream->avail_out -= hdrlen;
1937 }
1938
1939 -int hash_sha1_file(void *buf, unsigned long len, const char *type,
1939 +int hash_sha1_file(const void *buf, unsigned long len, const char *type,
1940 unsigned char *sha1)
1941 {
1942 char hdr[32];