sha1_file: let for_each_file_in_obj_subdir() handle subdir names

The function for_each_file_in_obj_subdir() takes a object subdirectory number and expects the name of the same subdirectory to be included in the path strbuf. Avoid this redundancy by letting the function append the hexadecimal subdirectory name itself. This makes it a bit easier and safer to use the function -- it becomes impossible to specify different subdirectories in subdir_nr and path. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 24, 2017 at 14:12 UTC 0375f472d484041f9b1e5550b57d69286b3322e7
2 files changed +14 -9
sha1_file.c
+14 -8
@@ -3742,15 +3742,22 @@ int for_each_file_in_obj_subdir(int subdir_nr,
3742 each_loose_subdir_fn subdir_cb,
3743 void *data)
3744 {
3745 - size_t baselen = path->len;
3746 - DIR *dir = opendir(path->buf);
3745 + size_t origlen, baselen;
3746 + DIR *dir;
3747 struct dirent *de;
3748 int r = 0;
3749
3750 + origlen = path->len;
3751 + strbuf_complete(path, '/');
3752 + strbuf_addf(path, "%02x", subdir_nr);
3753 + baselen = path->len;
3754 +
3755 + dir = opendir(path->buf);
3756 if (!dir) {
3751 - if (errno == ENOENT)
3752 - return 0;
3753 - return error_errno("unable to open %s", path->buf);
3757 + if (errno != ENOENT)
3758 + r = error_errno("unable to open %s", path->buf);
3759 + strbuf_setlen(path, origlen);
3760 + return r;
3761 }
3762
3763 while ((de = readdir(dir))) {
@@ -3788,6 +3795,8 @@ int for_each_file_in_obj_subdir(int subdir_nr,
3795 if (!r && subdir_cb)
3796 r = subdir_cb(subdir_nr, path->buf, data);
3797
3798 + strbuf_setlen(path, origlen);
3799 +
3800 return r;
3801 }
3802
@@ -3797,15 +3806,12 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
3806 each_loose_subdir_fn subdir_cb,
3807 void *data)
3808 {
3800 - size_t baselen = path->len;
3809 int r = 0;
3810 int i;
3811
3812 for (i = 0; i < 256; i++) {
3805 - strbuf_addf(path, "/%02x", i);
3813 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
3814 subdir_cb, data);
3808 - strbuf_setlen(path, baselen);
3815 if (r)
3816 break;
3817 }
sha1_name.c
-1
@@ -109,7 +109,6 @@ static void find_short_object_filename(struct disambiguate_state *ds)
109
110 if (!alt->loose_objects_subdir_seen[subdir_nr]) {
111 struct strbuf *buf = alt_scratch_buf(alt);
112 - strbuf_addf(buf, "%02x/", subdir_nr);
112 for_each_file_in_obj_subdir(subdir_nr, buf,
113 append_loose_object,
114 NULL, NULL,