Skip to content

Commit

Permalink
for_each_loose_file_in_objdir: take an optional strbuf path
Browse files Browse the repository at this point in the history
We feed a root "objdir" path to this iterator function,
which then copies the result into a strbuf, so that it can
repeatedly append the object sub-directories to it. Let's
make it easy for callers to just pass us a strbuf in the
first place.

We leave the original interface as a convenience for callers
who want to just pass a const string like the result of
get_object_directory().

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Feb 9, 2015
1 parent 189a122 commit e6f875e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
9 changes: 9 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
*
* Any callback that is NULL will be ignored. Callbacks returning non-zero
* will end the iteration.
*
* In the "buf" variant, "path" is a strbuf which will also be used as a
* scratch buffer, but restored to its original contents before
* the function returns.
*/
typedef int each_loose_object_fn(const unsigned char *sha1,
const char *path,
Expand All @@ -1271,6 +1275,11 @@ int for_each_loose_file_in_objdir(const char *path,
each_loose_cruft_fn cruft_cb,
each_loose_subdir_fn subdir_cb,
void *data);
int for_each_loose_file_in_objdir_buf(struct strbuf *path,
each_loose_object_fn obj_cb,
each_loose_cruft_fn cruft_cb,
each_loose_subdir_fn subdir_cb,
void *data);

/*
* Iterate over loose and packed objects in both the local
Expand Down
31 changes: 21 additions & 10 deletions sha1_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -3358,31 +3358,42 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
return r;
}

int for_each_loose_file_in_objdir(const char *path,
int for_each_loose_file_in_objdir_buf(struct strbuf *path,
each_loose_object_fn obj_cb,
each_loose_cruft_fn cruft_cb,
each_loose_subdir_fn subdir_cb,
void *data)
{
struct strbuf buf = STRBUF_INIT;
size_t baselen;
size_t baselen = path->len;
int r = 0;
int i;

strbuf_addstr(&buf, path);
strbuf_addch(&buf, '/');
baselen = buf.len;

for (i = 0; i < 256; i++) {
strbuf_addf(&buf, "%02x", i);
r = for_each_file_in_obj_subdir(i, &buf, obj_cb, cruft_cb,
strbuf_addf(path, "/%02x", i);
r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
subdir_cb, data);
strbuf_setlen(&buf, baselen);
strbuf_setlen(path, baselen);
if (r)
break;
}

return r;
}

int for_each_loose_file_in_objdir(const char *path,
each_loose_object_fn obj_cb,
each_loose_cruft_fn cruft_cb,
each_loose_subdir_fn subdir_cb,
void *data)
{
struct strbuf buf = STRBUF_INIT;
int r;

strbuf_addstr(&buf, path);
r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
subdir_cb, data);
strbuf_release(&buf);

return r;
}

Expand Down

0 comments on commit e6f875e

Please sign in to comment.