Skip to content

Commit 62a24c8

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3bdc4e commit 62a24c8

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

sha1_file.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
18841884
DIR *dir;
18851885
struct dirent *de;
18861886
int r = 0;
1887+
struct object_id oid;
18871888

18881889
if (subdir_nr > 0xff)
18891890
BUG("invalid loose object subdirectory: %x", subdir_nr);
@@ -1901,27 +1902,24 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
19011902
return r;
19021903
}
19031904

1905+
oid.hash[0] = subdir_nr;
1906+
19041907
while ((de = readdir(dir))) {
19051908
if (is_dot_or_dotdot(de->d_name))
19061909
continue;
19071910

19081911
strbuf_setlen(path, baselen);
19091912
strbuf_addf(path, "/%s", de->d_name);
19101913

1911-
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2) {
1912-
char hex[GIT_MAX_HEXSZ+1];
1913-
struct object_id oid;
1914-
1915-
xsnprintf(hex, sizeof(hex), "%02x%s",
1916-
subdir_nr, de->d_name);
1917-
if (!get_oid_hex(hex, &oid)) {
1918-
if (obj_cb) {
1919-
r = obj_cb(&oid, path->buf, data);
1920-
if (r)
1921-
break;
1922-
}
1923-
continue;
1914+
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
1915+
!hex_to_bytes(oid.hash + 1, de->d_name,
1916+
GIT_SHA1_RAWSZ - 1)) {
1917+
if (obj_cb) {
1918+
r = obj_cb(&oid, path->buf, data);
1919+
if (r)
1920+
break;
19241921
}
1922+
continue;
19251923
}
19261924

19271925
if (cruft_cb) {

0 commit comments

Comments
 (0)