Skip to content

Commit abdc3fc

Browse files
Rene ScharfeJunio C Hamano
authored andcommitted
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>
1 parent ce91fc6 commit abdc3fc

File tree

5 files changed

+22
-34
lines changed

5 files changed

+22
-34
lines changed

builtin-apply.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,8 +1783,6 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
17831783
{
17841784
const char *name = patch->old_name ? patch->old_name : patch->new_name;
17851785
unsigned char sha1[20];
1786-
unsigned char hdr[50];
1787-
int hdrlen;
17881786

17891787
/* For safety, we require patch index line to contain
17901788
* 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)
18001798
/* See if the old one matches what the patch
18011799
* applies to.
18021800
*/
1803-
write_sha1_file_prepare(desc->buffer, desc->size,
1804-
blob_type, sha1, hdr, &hdrlen);
1801+
hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
18051802
if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
18061803
return error("the patch applies to '%s' (%s), "
18071804
"which does not match the "
@@ -1846,8 +1843,7 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
18461843
name);
18471844

18481845
/* verify that the result matches */
1849-
write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
1850-
sha1, hdr, &hdrlen);
1846+
hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
18511847
if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
18521848
return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1));
18531849
}

cache-tree.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,8 @@ static int update_one(struct cache_tree *it,
344344
#endif
345345
}
346346

347-
if (dryrun) {
348-
unsigned char hdr[200];
349-
int hdrlen;
350-
write_sha1_file_prepare(buffer, offset, tree_type, it->sha1,
351-
hdr, &hdrlen);
352-
}
347+
if (dryrun)
348+
hash_sha1_file(buffer, offset, tree_type, it->sha1);
353349
else
354350
write_sha1_file(buffer, offset, tree_type, it->sha1);
355351
free(buffer);

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ char *enter_repo(char *path, int strict);
245245
extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
246246
extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
247247
extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
248+
extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
248249
extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
249250
extern char *write_sha1_file_prepare(void *buf,
250251
unsigned long len,

merge-recursive.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,10 @@ int merge(struct commit *h1,
12351235
if (merged_common_ancestors == NULL) {
12361236
/* if there is no common ancestor, make an empty tree */
12371237
struct tree *tree = xcalloc(1, sizeof(struct tree));
1238-
unsigned char hdr[40];
1239-
int hdrlen;
12401238

12411239
tree->object.parsed = 1;
12421240
tree->object.type = OBJ_TREE;
1243-
write_sha1_file_prepare(NULL, 0, tree_type, tree->object.sha1,
1244-
hdr, &hdrlen);
1241+
hash_sha1_file(NULL, 0, tree_type, tree->object.sha1);
12451242
merged_common_ancestors = make_virtual_commit(tree, "ancestor");
12461243
}
12471244

sha1_file.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,15 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon
15011501
stream->avail_out -= hdr;
15021502
}
15031503

1504+
int hash_sha1_file(void *buf, unsigned long len, const char *type,
1505+
unsigned char *sha1)
1506+
{
1507+
unsigned char hdr[50];
1508+
int hdrlen;
1509+
write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1510+
return 0;
1511+
}
1512+
15041513
int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
15051514
{
15061515
int size;
@@ -1784,8 +1793,6 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
17841793
unsigned long size = 4096;
17851794
char *buf = xmalloc(size);
17861795
int ret;
1787-
unsigned char hdr[50];
1788-
int hdrlen;
17891796

17901797
if (read_pipe(fd, &buf, &size)) {
17911798
free(buf);
@@ -1796,10 +1803,8 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
17961803
type = blob_type;
17971804
if (write_object)
17981805
ret = write_sha1_file(buf, size, type, sha1);
1799-
else {
1800-
write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1801-
ret = 0;
1802-
}
1806+
else
1807+
ret = hash_sha1_file(buf, size, type, sha1);
18031808
free(buf);
18041809
return ret;
18051810
}
@@ -1809,8 +1814,6 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
18091814
unsigned long size = st->st_size;
18101815
void *buf;
18111816
int ret;
1812-
unsigned char hdr[50];
1813-
int hdrlen;
18141817

18151818
buf = "";
18161819
if (size)
@@ -1823,10 +1826,8 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
18231826
type = blob_type;
18241827
if (write_object)
18251828
ret = write_sha1_file(buf, size, type, sha1);
1826-
else {
1827-
write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1828-
ret = 0;
1829-
}
1829+
else
1830+
ret = hash_sha1_file(buf, size, type, sha1);
18301831
if (size)
18311832
munmap(buf, size);
18321833
return ret;
@@ -1855,12 +1856,9 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
18551856
return error("readlink(\"%s\"): %s", path,
18561857
errstr);
18571858
}
1858-
if (!write_object) {
1859-
unsigned char hdr[50];
1860-
int hdrlen;
1861-
write_sha1_file_prepare(target, st->st_size, blob_type,
1862-
sha1, hdr, &hdrlen);
1863-
} else if (write_sha1_file(target, st->st_size, blob_type, sha1))
1859+
if (!write_object)
1860+
hash_sha1_file(target, st->st_size, blob_type, sha1);
1861+
else if (write_sha1_file(target, st->st_size, blob_type, sha1))
18641862
return error("%s: failed to insert into database",
18651863
path);
18661864
free(target);

0 commit comments

Comments
 (0)