Skip to content

Commit 7cf6232

Browse files
committed
Merge branch 'jk/prune-mtime'
In v2.2.0, we broke "git prune" that runs in a repository that borrows from an alternate object store. * jk/prune-mtime: sha1_file: fix iterating loose alternate objects for_each_loose_file_in_objdir: take an optional strbuf path
2 parents dcc883d + b0a4264 commit 7cf6232

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

cache.h

+9
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,10 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
12541254
*
12551255
* Any callback that is NULL will be ignored. Callbacks returning non-zero
12561256
* will end the iteration.
1257+
*
1258+
* In the "buf" variant, "path" is a strbuf which will also be used as a
1259+
* scratch buffer, but restored to its original contents before
1260+
* the function returns.
12571261
*/
12581262
typedef int each_loose_object_fn(const unsigned char *sha1,
12591263
const char *path,
@@ -1269,6 +1273,11 @@ int for_each_loose_file_in_objdir(const char *path,
12691273
each_loose_cruft_fn cruft_cb,
12701274
each_loose_subdir_fn subdir_cb,
12711275
void *data);
1276+
int for_each_loose_file_in_objdir_buf(struct strbuf *path,
1277+
each_loose_object_fn obj_cb,
1278+
each_loose_cruft_fn cruft_cb,
1279+
each_loose_subdir_fn subdir_cb,
1280+
void *data);
12721281

12731282
/*
12741283
* Iterate over loose and packed objects in both the local

sha1_file.c

+31-13
Original file line numberDiff line numberDiff line change
@@ -3359,31 +3359,42 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
33593359
return r;
33603360
}
33613361

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

3373-
strbuf_addstr(&buf, path);
3374-
strbuf_addch(&buf, '/');
3375-
baselen = buf.len;
3376-
33773372
for (i = 0; i < 256; i++) {
3378-
strbuf_addf(&buf, "%02x", i);
3379-
r = for_each_file_in_obj_subdir(i, &buf, obj_cb, cruft_cb,
3373+
strbuf_addf(path, "/%02x", i);
3374+
r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
33803375
subdir_cb, data);
3381-
strbuf_setlen(&buf, baselen);
3376+
strbuf_setlen(path, baselen);
33823377
if (r)
33833378
break;
33843379
}
33853380

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

@@ -3396,9 +3407,16 @@ static int loose_from_alt_odb(struct alternate_object_database *alt,
33963407
void *vdata)
33973408
{
33983409
struct loose_alt_odb_data *data = vdata;
3399-
return for_each_loose_file_in_objdir(alt->base,
3400-
data->cb, NULL, NULL,
3401-
data->data);
3410+
struct strbuf buf = STRBUF_INIT;
3411+
int r;
3412+
3413+
/* copy base not including trailing '/' */
3414+
strbuf_add(&buf, alt->base, alt->name - alt->base - 1);
3415+
r = for_each_loose_file_in_objdir_buf(&buf,
3416+
data->cb, NULL, NULL,
3417+
data->data);
3418+
strbuf_release(&buf);
3419+
return r;
34023420
}
34033421

34043422
int for_each_loose_object(each_loose_object_fn cb, void *data)

t/t5304-prune.sh

+8
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,12 @@ test_expect_success 'prune .git/shallow' '
253253
test_path_is_missing .git/shallow
254254
'
255255

256+
test_expect_success 'prune: handle alternate object database' '
257+
test_create_repo A &&
258+
git -C A commit --allow-empty -m "initial commit" &&
259+
git clone --shared A B &&
260+
git -C B commit --allow-empty -m "next commit" &&
261+
git -C B prune
262+
'
263+
256264
test_done

0 commit comments

Comments
 (0)