Skip to content

Commit 9559de3

Browse files
committed
Merge branch 'tb/add-objects-in-unpacked-packs-simplify'
Code simplification with reduced memory usage. * tb/add-objects-in-unpacked-packs-simplify: builtin/pack-objects.c: remove duplicate hash lookup builtin/pack-objects.c: simplify add_objects_in_unpacked_packs() object-store.h: teach for_each_packed_object to ignore kept packs
2 parents 87d4aed + b017334 commit 9559de3

File tree

4 files changed

+24
-74
lines changed

4 files changed

+24
-74
lines changed

builtin/pack-objects.c

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,13 +3405,9 @@ static void read_object_list_from_stdin(void)
34053405
}
34063406
}
34073407

3408-
/* Remember to update object flag allocation in object.h */
3409-
#define OBJECT_ADDED (1u<<20)
3410-
34113408
static void show_commit(struct commit *commit, void *data)
34123409
{
34133410
add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
3414-
commit->object.flags |= OBJECT_ADDED;
34153411

34163412
if (write_bitmap_index)
34173413
index_commit_for_bitmap(commit);
@@ -3424,7 +3420,6 @@ static void show_object(struct object *obj, const char *name, void *data)
34243420
{
34253421
add_preferred_base_object(name);
34263422
add_object_entry(&obj->oid, obj->type, name, 0);
3427-
obj->flags |= OBJECT_ADDED;
34283423

34293424
if (use_delta_islands) {
34303425
const char *p;
@@ -3505,79 +3500,23 @@ static void show_edge(struct commit *commit)
35053500
add_preferred_base(&commit->object.oid);
35063501
}
35073502

3508-
struct in_pack_object {
3509-
off_t offset;
3510-
struct object *object;
3511-
};
3512-
3513-
struct in_pack {
3514-
unsigned int alloc;
3515-
unsigned int nr;
3516-
struct in_pack_object *array;
3517-
};
3518-
3519-
static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)
3520-
{
3521-
in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);
3522-
in_pack->array[in_pack->nr].object = object;
3523-
in_pack->nr++;
3524-
}
3525-
3526-
/*
3527-
* Compare the objects in the offset order, in order to emulate the
3528-
* "git rev-list --objects" output that produced the pack originally.
3529-
*/
3530-
static int ofscmp(const void *a_, const void *b_)
3503+
static int add_object_in_unpacked_pack(const struct object_id *oid,
3504+
struct packed_git *pack,
3505+
uint32_t pos,
3506+
void *_data)
35313507
{
3532-
struct in_pack_object *a = (struct in_pack_object *)a_;
3533-
struct in_pack_object *b = (struct in_pack_object *)b_;
3534-
3535-
if (a->offset < b->offset)
3536-
return -1;
3537-
else if (a->offset > b->offset)
3538-
return 1;
3539-
else
3540-
return oidcmp(&a->object->oid, &b->object->oid);
3508+
add_object_entry(oid, OBJ_NONE, "", 0);
3509+
return 0;
35413510
}
35423511

35433512
static void add_objects_in_unpacked_packs(void)
35443513
{
3545-
struct packed_git *p;
3546-
struct in_pack in_pack;
3547-
uint32_t i;
3548-
3549-
memset(&in_pack, 0, sizeof(in_pack));
3550-
3551-
for (p = get_all_packs(the_repository); p; p = p->next) {
3552-
struct object_id oid;
3553-
struct object *o;
3554-
3555-
if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
3556-
continue;
3557-
if (open_pack_index(p))
3558-
die(_("cannot open pack index"));
3559-
3560-
ALLOC_GROW(in_pack.array,
3561-
in_pack.nr + p->num_objects,
3562-
in_pack.alloc);
3563-
3564-
for (i = 0; i < p->num_objects; i++) {
3565-
nth_packed_object_id(&oid, p, i);
3566-
o = lookup_unknown_object(the_repository, &oid);
3567-
if (!(o->flags & OBJECT_ADDED))
3568-
mark_in_pack_object(o, p, &in_pack);
3569-
o->flags |= OBJECT_ADDED;
3570-
}
3571-
}
3572-
3573-
if (in_pack.nr) {
3574-
QSORT(in_pack.array, in_pack.nr, ofscmp);
3575-
for (i = 0; i < in_pack.nr; i++) {
3576-
struct object *o = in_pack.array[i].object;
3577-
add_object_entry(&o->oid, o->type, "", 0);
3578-
}
3579-
}
3580-
free(in_pack.array);
3514+
if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
3515+
FOR_EACH_OBJECT_PACK_ORDER |
3516+
FOR_EACH_OBJECT_LOCAL_ONLY |
3517+
FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
3518+
FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
3519+
die(_("cannot open pack index"));
35813520
}
35823521

35833522
static int add_loose_object(const struct object_id *oid, const char *path,

object-store.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ enum for_each_object_flags {
455455
* Visit objects within a pack in packfile order rather than .idx order
456456
*/
457457
FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
458+
459+
/* Only iterate over packs that are not marked as kept in-core. */
460+
FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
461+
462+
/* Only iterate over packs that do not have .keep files. */
463+
FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
458464
};
459465

460466
/*

object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ struct object_array {
7575
* builtin/fsck.c: 0--3
7676
* builtin/gc.c: 0
7777
* builtin/index-pack.c: 2021
78-
* builtin/pack-objects.c: 20
7978
* builtin/reflog.c: 10--12
8079
* builtin/show-branch.c: 0-------------------------------------------26
8180
* builtin/unpack-objects.c: 2021

packfile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,12 @@ int for_each_packed_object(each_packed_object_fn cb, void *data,
22052205
if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&
22062206
!p->pack_promisor)
22072207
continue;
2208+
if ((flags & FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
2209+
p->pack_keep_in_core)
2210+
continue;
2211+
if ((flags & FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
2212+
p->pack_keep)
2213+
continue;
22082214
if (open_pack_index(p)) {
22092215
pack_errors = 1;
22102216
continue;

0 commit comments

Comments
 (0)