{"thread":{"id":"7339","subject":"[PATCH] index-pack: use hash_sha1_file()","startedAt":"2007-03-20T20:02:09Z","lastAt":"2007-03-20T20:02:09Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"37638","messageId":"alpine.LFD.0.83.0703201557280.18328@xanadu.home","threadId":"7339","inReplyTo":null,"subject":"[PATCH] index-pack: use hash_sha1_file()","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2007-03-20T20:02:09Z","receivedAt":"2007-03-20T20:02:09Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"Use hash_sha1_file() instead of duplicating code to compute object SHA1.\nWhile at it make it accept a const pointer.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n\nThis applies on top of my previous patch.\n\ndiff --git a/cache.h b/cache.h\nindex 1b50a74..384b260 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -283,7 +283,7 @@ char *enter_repo(char *path, int strict);\n /* Read and unpack a sha1 file into memory, write memory to a sha1 file */\n extern int sha1_object_info(const unsigned char *, unsigned long *);\n extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);\n-extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);\n+extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);\n extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);\n extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);\n \ndiff --git a/index-pack.c b/index-pack.c\nindex 4effb2d..f314937 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -348,26 +348,7 @@ static void sha1_object(const void *data, unsigned long size,\n \t\t\tenum object_type type, unsigned char *sha1,\n \t\t\tint test_for_collision)\n {\n-\tSHA_CTX ctx;\n-\tchar header[50];\n-\tint header_size;\n-\tconst char *type_str;\n-\n-\tswitch (type) {\n-\tcase OBJ_COMMIT: type_str = commit_type; break;\n-\tcase OBJ_TREE:   type_str = tree_type; break;\n-\tcase OBJ_BLOB:   type_str = blob_type; break;\n-\tcase OBJ_TAG:    type_str = tag_type; break;\n-\tdefault:\n-\t\tdie(\"bad type %d\", type);\n-\t}\n-\n-\theader_size = sprintf(header, \"%s %lu\", type_str, size) + 1;\n-\n-\tSHA1_Init(&ctx);\n-\tSHA1_Update(&ctx, header, header_size);\n-\tSHA1_Update(&ctx, data, size);\n-\tSHA1_Final(sha1, &ctx);\n+\thash_sha1_file(data, size, typename(type), sha1);\n \n \tif (test_for_collision && has_sha1_file(sha1)) {\n \t\tvoid *has_data;\ndiff --git a/sha1_file.c b/sha1_file.c\nindex c445a24..64d9813 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1808,7 +1808,7 @@ void *read_object_with_reference(const unsigned char *sha1,\n \t}\n }\n \n-static void write_sha1_file_prepare(void *buf, unsigned long len,\n+static void write_sha1_file_prepare(const void *buf, unsigned long len,\n                                     const char *type, unsigned char *sha1,\n                                     char *hdr, int *hdrlen)\n {\n@@ -1936,7 +1936,7 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon\n \tstream->avail_out -= hdrlen;\n }\n \n-int hash_sha1_file(void *buf, unsigned long len, const char *type,\n+int hash_sha1_file(const void *buf, unsigned long len, const char *type,\n                    unsigned char *sha1)\n {\n \tchar hdr[32];\n"}]}