Skip to content

Commit e6f875e

Browse files
peffgitster
authored andcommitted
for_each_loose_file_in_objdir: take an optional strbuf path
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 189a122 commit e6f875e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

cache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,10 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
12561256
*
12571257
* Any callback that is NULL will be ignored. Callbacks returning non-zero
12581258
* will end the iteration.
1259+
*
1260+
* In the "buf" variant, "path" is a strbuf which will also be used as a
1261+
* scratch buffer, but restored to its original contents before
1262+
* the function returns.
12591263
*/
12601264
typedef int each_loose_object_fn(const unsigned char *sha1,
12611265
const char *path,
@@ -1271,6 +1275,11 @@ int for_each_loose_file_in_objdir(const char *path,
12711275
each_loose_cruft_fn cruft_cb,
12721276
each_loose_subdir_fn subdir_cb,
12731277
void *data);
1278+
int for_each_loose_file_in_objdir_buf(struct strbuf *path,
1279+
each_loose_object_fn obj_cb,
1280+
each_loose_cruft_fn cruft_cb,
1281+
each_loose_subdir_fn subdir_cb,
1282+
void *data);
12741283

12751284
/*
12761285
* Iterate over loose and packed objects in both the local

sha1_file.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,31 +3358,42 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
33583358
return r;
33593359
}
33603360

3361-
int for_each_loose_file_in_objdir(const char *path,
3361+
int for_each_loose_file_in_objdir_buf(struct strbuf *path,
33623362
each_loose_object_fn obj_cb,
33633363
each_loose_cruft_fn cruft_cb,
33643364
each_loose_subdir_fn subdir_cb,
33653365
void *data)
33663366
{
3367-
struct strbuf buf = STRBUF_INIT;
3368-
size_t baselen;
3367+
size_t baselen = path->len;
33693368
int r = 0;
33703369
int i;
33713370

3372-
strbuf_addstr(&buf, path);
3373-
strbuf_addch(&buf, '/');
3374-
baselen = buf.len;
3375-
33763371
for (i = 0; i < 256; i++) {
3377-
strbuf_addf(&buf, "%02x", i);
3378-
r = for_each_file_in_obj_subdir(i, &buf, obj_cb, cruft_cb,
3372+
strbuf_addf(path, "/%02x", i);
3373+
r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
33793374
subdir_cb, data);
3380-
strbuf_setlen(&buf, baselen);
3375+
strbuf_setlen(path, baselen);
33813376
if (r)
33823377
break;
33833378
}
33843379

3380+
return r;
3381+
}
3382+
3383+
int for_each_loose_file_in_objdir(const char *path,
3384+
each_loose_object_fn obj_cb,
3385+
each_loose_cruft_fn cruft_cb,
3386+
each_loose_subdir_fn subdir_cb,
3387+
void *data)
3388+
{
3389+
struct strbuf buf = STRBUF_INIT;
3390+
int r;
3391+
3392+
strbuf_addstr(&buf, path);
3393+
r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
3394+
subdir_cb, data);
33853395
strbuf_release(&buf);
3396+
33863397
return r;
33873398
}
33883399

0 commit comments

Comments
 (0)