sha1_file: use hex_to_bytes()
The path of a loose object contains its hash value encoded into two substrings of 2 and 38 hexadecimal digits separated by a slash. The first part is handed to for_each_file_in_obj_subdir() in decoded form as subdir_nr. The current code builds a full hexadecimal representation of the hash in a temporary buffer, then uses get_oid_hex() to decode it. Avoid the intermediate step by taking subdir_nr as-is and using hex_to_bytes() directly on the second substring. That's shorter and easier. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
c3bdc4e779
commit
62a24c8923
24
sha1_file.c
24
sha1_file.c
@ -1884,6 +1884,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
int r = 0;
|
int r = 0;
|
||||||
|
struct object_id oid;
|
||||||
|
|
||||||
if (subdir_nr > 0xff)
|
if (subdir_nr > 0xff)
|
||||||
BUG("invalid loose object subdirectory: %x", subdir_nr);
|
BUG("invalid loose object subdirectory: %x", subdir_nr);
|
||||||
@ -1901,6 +1902,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oid.hash[0] = subdir_nr;
|
||||||
|
|
||||||
while ((de = readdir(dir))) {
|
while ((de = readdir(dir))) {
|
||||||
if (is_dot_or_dotdot(de->d_name))
|
if (is_dot_or_dotdot(de->d_name))
|
||||||
continue;
|
continue;
|
||||||
@ -1908,20 +1911,15 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
strbuf_setlen(path, baselen);
|
strbuf_setlen(path, baselen);
|
||||||
strbuf_addf(path, "/%s", de->d_name);
|
strbuf_addf(path, "/%s", de->d_name);
|
||||||
|
|
||||||
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2) {
|
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
|
||||||
char hex[GIT_MAX_HEXSZ+1];
|
!hex_to_bytes(oid.hash + 1, de->d_name,
|
||||||
struct object_id oid;
|
GIT_SHA1_RAWSZ - 1)) {
|
||||||
|
if (obj_cb) {
|
||||||
xsnprintf(hex, sizeof(hex), "%02x%s",
|
r = obj_cb(&oid, path->buf, data);
|
||||||
subdir_nr, de->d_name);
|
if (r)
|
||||||
if (!get_oid_hex(hex, &oid)) {
|
break;
|
||||||
if (obj_cb) {
|
|
||||||
r = obj_cb(&oid, path->buf, data);
|
|
||||||
if (r)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cruft_cb) {
|
if (cruft_cb) {
|
||||||
|
Reference in New Issue
Block a user