Skip to content

Commit fb7dc2c

Browse files
pks-tgitster
authored andcommitted
builtin/cat-file: deduplicate logic to iterate over all objects
Pull out a common function that allows us to iterate over all objects in a repository. Right now the logic is trivial and would only require two function calls, making this refactoring a bit pointless. But in the next commit we will iterate on this logic to make use of bitmaps, so this is about to become a bit more complex. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a8b3d38 commit fb7dc2c

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

builtin/cat-file.c

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -622,25 +622,18 @@ static int batch_object_cb(const struct object_id *oid, void *vdata)
622622
return 0;
623623
}
624624

625-
static int collect_loose_object(const struct object_id *oid,
626-
const char *path UNUSED,
627-
void *data)
628-
{
629-
oid_array_append(data, oid);
630-
return 0;
631-
}
632-
633-
static int collect_packed_object(const struct object_id *oid,
634-
struct packed_git *pack UNUSED,
635-
uint32_t pos UNUSED,
636-
void *data)
625+
static int collect_object(const struct object_id *oid,
626+
struct packed_git *pack UNUSED,
627+
off_t offset UNUSED,
628+
void *data)
637629
{
638630
oid_array_append(data, oid);
639631
return 0;
640632
}
641633

642634
static int batch_unordered_object(const struct object_id *oid,
643-
struct packed_git *pack, off_t offset,
635+
struct packed_git *pack,
636+
off_t offset,
644637
void *vdata)
645638
{
646639
struct object_cb_data *data = vdata;
@@ -654,23 +647,6 @@ static int batch_unordered_object(const struct object_id *oid,
654647
return 0;
655648
}
656649

657-
static int batch_unordered_loose(const struct object_id *oid,
658-
const char *path UNUSED,
659-
void *data)
660-
{
661-
return batch_unordered_object(oid, NULL, 0, data);
662-
}
663-
664-
static int batch_unordered_packed(const struct object_id *oid,
665-
struct packed_git *pack,
666-
uint32_t pos,
667-
void *data)
668-
{
669-
return batch_unordered_object(oid, pack,
670-
nth_packed_object_offset(pack, pos),
671-
data);
672-
}
673-
674650
typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
675651
struct strbuf *, struct expand_data *);
676652

@@ -803,6 +779,45 @@ static void batch_objects_command(struct batch_options *opt,
803779

804780
#define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
805781

782+
typedef int (*for_each_object_fn)(const struct object_id *oid, struct packed_git *pack,
783+
off_t offset, void *data);
784+
785+
struct for_each_object_payload {
786+
for_each_object_fn callback;
787+
void *payload;
788+
};
789+
790+
static int batch_one_object_loose(const struct object_id *oid,
791+
const char *path UNUSED,
792+
void *_payload)
793+
{
794+
struct for_each_object_payload *payload = _payload;
795+
return payload->callback(oid, NULL, 0, payload->payload);
796+
}
797+
798+
static int batch_one_object_packed(const struct object_id *oid,
799+
struct packed_git *pack,
800+
uint32_t pos,
801+
void *_payload)
802+
{
803+
struct for_each_object_payload *payload = _payload;
804+
return payload->callback(oid, pack, nth_packed_object_offset(pack, pos),
805+
payload->payload);
806+
}
807+
808+
static void batch_each_object(for_each_object_fn callback,
809+
unsigned flags,
810+
void *_payload)
811+
{
812+
struct for_each_object_payload payload = {
813+
.callback = callback,
814+
.payload = _payload,
815+
};
816+
for_each_loose_object(batch_one_object_loose, &payload, 0);
817+
for_each_packed_object(the_repository, batch_one_object_packed,
818+
&payload, flags);
819+
}
820+
806821
static int batch_objects(struct batch_options *opt)
807822
{
808823
struct strbuf input = STRBUF_INIT;
@@ -857,18 +872,14 @@ static int batch_objects(struct batch_options *opt)
857872

858873
cb.seen = &seen;
859874

860-
for_each_loose_object(batch_unordered_loose, &cb, 0);
861-
for_each_packed_object(the_repository, batch_unordered_packed,
862-
&cb, FOR_EACH_OBJECT_PACK_ORDER);
875+
batch_each_object(batch_unordered_object,
876+
FOR_EACH_OBJECT_PACK_ORDER, &cb);
863877

864878
oidset_clear(&seen);
865879
} else {
866880
struct oid_array sa = OID_ARRAY_INIT;
867881

868-
for_each_loose_object(collect_loose_object, &sa, 0);
869-
for_each_packed_object(the_repository, collect_packed_object,
870-
&sa, 0);
871-
882+
batch_each_object(collect_object, 0, &sa);
872883
oid_array_for_each_unique(&sa, batch_object_cb, &cb);
873884

874885
oid_array_clear(&sa);

0 commit comments

Comments
 (0)