Skip to content

Commit

Permalink
Merge branch 'tb/add-objects-in-unpacked-packs-simplify'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitster committed Sep 10, 2021
2 parents 87d4aed + b017334 commit 9559de3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 74 deletions.
85 changes: 12 additions & 73 deletions builtin/pack-objects.c
Expand Up @@ -3405,13 +3405,9 @@ static void read_object_list_from_stdin(void)
}
}

/* Remember to update object flag allocation in object.h */
#define OBJECT_ADDED (1u<<20)

static void show_commit(struct commit *commit, void *data)
{
add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
commit->object.flags |= OBJECT_ADDED;

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

if (use_delta_islands) {
const char *p;
Expand Down Expand Up @@ -3505,79 +3500,23 @@ static void show_edge(struct commit *commit)
add_preferred_base(&commit->object.oid);
}

struct in_pack_object {
off_t offset;
struct object *object;
};

struct in_pack {
unsigned int alloc;
unsigned int nr;
struct in_pack_object *array;
};

static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)
{
in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);
in_pack->array[in_pack->nr].object = object;
in_pack->nr++;
}

/*
* Compare the objects in the offset order, in order to emulate the
* "git rev-list --objects" output that produced the pack originally.
*/
static int ofscmp(const void *a_, const void *b_)
static int add_object_in_unpacked_pack(const struct object_id *oid,
struct packed_git *pack,
uint32_t pos,
void *_data)
{
struct in_pack_object *a = (struct in_pack_object *)a_;
struct in_pack_object *b = (struct in_pack_object *)b_;

if (a->offset < b->offset)
return -1;
else if (a->offset > b->offset)
return 1;
else
return oidcmp(&a->object->oid, &b->object->oid);
add_object_entry(oid, OBJ_NONE, "", 0);
return 0;
}

static void add_objects_in_unpacked_packs(void)
{
struct packed_git *p;
struct in_pack in_pack;
uint32_t i;

memset(&in_pack, 0, sizeof(in_pack));

for (p = get_all_packs(the_repository); p; p = p->next) {
struct object_id oid;
struct object *o;

if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
continue;
if (open_pack_index(p))
die(_("cannot open pack index"));

ALLOC_GROW(in_pack.array,
in_pack.nr + p->num_objects,
in_pack.alloc);

for (i = 0; i < p->num_objects; i++) {
nth_packed_object_id(&oid, p, i);
o = lookup_unknown_object(the_repository, &oid);
if (!(o->flags & OBJECT_ADDED))
mark_in_pack_object(o, p, &in_pack);
o->flags |= OBJECT_ADDED;
}
}

if (in_pack.nr) {
QSORT(in_pack.array, in_pack.nr, ofscmp);
for (i = 0; i < in_pack.nr; i++) {
struct object *o = in_pack.array[i].object;
add_object_entry(&o->oid, o->type, "", 0);
}
}
free(in_pack.array);
if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
FOR_EACH_OBJECT_PACK_ORDER |
FOR_EACH_OBJECT_LOCAL_ONLY |
FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
die(_("cannot open pack index"));
}

static int add_loose_object(const struct object_id *oid, const char *path,
Expand Down
6 changes: 6 additions & 0 deletions object-store.h
Expand Up @@ -455,6 +455,12 @@ enum for_each_object_flags {
* Visit objects within a pack in packfile order rather than .idx order
*/
FOR_EACH_OBJECT_PACK_ORDER = (1<<2),

/* Only iterate over packs that are not marked as kept in-core. */
FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),

/* Only iterate over packs that do not have .keep files. */
FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
};

/*
Expand Down
1 change: 0 additions & 1 deletion object.h
Expand Up @@ -75,7 +75,6 @@ struct object_array {
* builtin/fsck.c: 0--3
* builtin/gc.c: 0
* builtin/index-pack.c: 2021
* builtin/pack-objects.c: 20
* builtin/reflog.c: 10--12
* builtin/show-branch.c: 0-------------------------------------------26
* builtin/unpack-objects.c: 2021
Expand Down
6 changes: 6 additions & 0 deletions packfile.c
Expand Up @@ -2205,6 +2205,12 @@ int for_each_packed_object(each_packed_object_fn cb, void *data,
if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&
!p->pack_promisor)
continue;
if ((flags & FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
p->pack_keep_in_core)
continue;
if ((flags & FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
p->pack_keep)
continue;
if (open_pack_index(p)) {
pack_errors = 1;
continue;
Expand Down

0 comments on commit 9559de3

Please sign in to comment.