Add hash_sha1_file()

Most callers of write_sha1_file_prepare() are only interested in the
resulting hash but don't care about the returned file name or the header.
This patch adds a simple wrapper named hash_sha1_file() which does just
that, and converts potential callers.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Rene Scharfe 2006-10-14 12:45:36 +02:00 committed by Junio C Hamano
parent ce91fc6eb9
commit abdc3fc842
5 changed files with 22 additions and 34 deletions

View File

@ -1783,8 +1783,6 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
{ {
const char *name = patch->old_name ? patch->old_name : patch->new_name; const char *name = patch->old_name ? patch->old_name : patch->new_name;
unsigned char sha1[20]; unsigned char sha1[20];
unsigned char hdr[50];
int hdrlen;
/* For safety, we require patch index line to contain /* For safety, we require patch index line to contain
* full 40-byte textual SHA1 for old and new, at least for now. * full 40-byte textual SHA1 for old and new, at least for now.
@ -1800,8 +1798,7 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
/* See if the old one matches what the patch /* See if the old one matches what the patch
* applies to. * applies to.
*/ */
write_sha1_file_prepare(desc->buffer, desc->size, hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
blob_type, sha1, hdr, &hdrlen);
if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix)) if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
return error("the patch applies to '%s' (%s), " return error("the patch applies to '%s' (%s), "
"which does not match the " "which does not match the "
@ -1846,8 +1843,7 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
name); name);
/* verify that the result matches */ /* verify that the result matches */
write_sha1_file_prepare(desc->buffer, desc->size, blob_type, hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
sha1, hdr, &hdrlen);
if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix)) if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1)); return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1));
} }

View File

@ -344,12 +344,8 @@ static int update_one(struct cache_tree *it,
#endif #endif
} }
if (dryrun) { if (dryrun)
unsigned char hdr[200]; hash_sha1_file(buffer, offset, tree_type, it->sha1);
int hdrlen;
write_sha1_file_prepare(buffer, offset, tree_type, it->sha1,
hdr, &hdrlen);
}
else else
write_sha1_file(buffer, offset, tree_type, it->sha1); write_sha1_file(buffer, offset, tree_type, it->sha1);
free(buffer); free(buffer);

View File

@ -245,6 +245,7 @@ char *enter_repo(char *path, int strict);
extern int sha1_object_info(const unsigned char *, char *, unsigned long *); extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size); extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size); extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1); extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
extern char *write_sha1_file_prepare(void *buf, extern char *write_sha1_file_prepare(void *buf,
unsigned long len, unsigned long len,

View File

@ -1235,13 +1235,10 @@ int merge(struct commit *h1,
if (merged_common_ancestors == NULL) { if (merged_common_ancestors == NULL) {
/* if there is no common ancestor, make an empty tree */ /* if there is no common ancestor, make an empty tree */
struct tree *tree = xcalloc(1, sizeof(struct tree)); struct tree *tree = xcalloc(1, sizeof(struct tree));
unsigned char hdr[40];
int hdrlen;
tree->object.parsed = 1; tree->object.parsed = 1;
tree->object.type = OBJ_TREE; tree->object.type = OBJ_TREE;
write_sha1_file_prepare(NULL, 0, tree_type, tree->object.sha1, hash_sha1_file(NULL, 0, tree_type, tree->object.sha1);
hdr, &hdrlen);
merged_common_ancestors = make_virtual_commit(tree, "ancestor"); merged_common_ancestors = make_virtual_commit(tree, "ancestor");
} }

View File

@ -1501,6 +1501,15 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon
stream->avail_out -= hdr; stream->avail_out -= hdr;
} }
int hash_sha1_file(void *buf, unsigned long len, const char *type,
unsigned char *sha1)
{
unsigned char hdr[50];
int hdrlen;
write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
return 0;
}
int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1) int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
{ {
int size; int size;
@ -1784,8 +1793,6 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
unsigned long size = 4096; unsigned long size = 4096;
char *buf = xmalloc(size); char *buf = xmalloc(size);
int ret; int ret;
unsigned char hdr[50];
int hdrlen;
if (read_pipe(fd, &buf, &size)) { if (read_pipe(fd, &buf, &size)) {
free(buf); free(buf);
@ -1796,10 +1803,8 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
type = blob_type; type = blob_type;
if (write_object) if (write_object)
ret = write_sha1_file(buf, size, type, sha1); ret = write_sha1_file(buf, size, type, sha1);
else { else
write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen); ret = hash_sha1_file(buf, size, type, sha1);
ret = 0;
}
free(buf); free(buf);
return ret; return ret;
} }
@ -1809,8 +1814,6 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
unsigned long size = st->st_size; unsigned long size = st->st_size;
void *buf; void *buf;
int ret; int ret;
unsigned char hdr[50];
int hdrlen;
buf = ""; buf = "";
if (size) if (size)
@ -1823,10 +1826,8 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
type = blob_type; type = blob_type;
if (write_object) if (write_object)
ret = write_sha1_file(buf, size, type, sha1); ret = write_sha1_file(buf, size, type, sha1);
else { else
write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen); ret = hash_sha1_file(buf, size, type, sha1);
ret = 0;
}
if (size) if (size)
munmap(buf, size); munmap(buf, size);
return ret; return ret;
@ -1855,12 +1856,9 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
return error("readlink(\"%s\"): %s", path, return error("readlink(\"%s\"): %s", path,
errstr); errstr);
} }
if (!write_object) { if (!write_object)
unsigned char hdr[50]; hash_sha1_file(target, st->st_size, blob_type, sha1);
int hdrlen; else if (write_sha1_file(target, st->st_size, blob_type, sha1))
write_sha1_file_prepare(target, st->st_size, blob_type,
sha1, hdr, &hdrlen);
} else if (write_sha1_file(target, st->st_size, blob_type, sha1))
return error("%s: failed to insert into database", return error("%s: failed to insert into database",
path); path);
free(target); free(target);