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>

René Scharfe committed Oct 31, 2017 at 14:50 UTC 62a24c8923274b0ef941b2e7bc6efdb2fd52e6bf
1 file changed +11 -13
sha1_file.c
+11 -13
@@ -1884,6 +1884,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1884 DIR *dir;
1885 struct dirent *de;
1886 int r = 0;
1887 + struct object_id oid;
1888
1889 if (subdir_nr > 0xff)
1890 BUG("invalid loose object subdirectory: %x", subdir_nr);
@@ -1901,6 +1902,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1902 return r;
1903 }
1904
1905 + oid.hash[0] = subdir_nr;
1906 +
1907 while ((de = readdir(dir))) {
1908 if (is_dot_or_dotdot(de->d_name))
1909 continue;
@@ -1908,20 +1911,15 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1911 strbuf_setlen(path, baselen);
1912 strbuf_addf(path, "/%s", de->d_name);
1913
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;
1921 }
1922 + continue;
1923 }
1924
1925 if (cruft_cb) {