Skip to content

Commit d5ec702

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 c9b94a7 commit d5ec702

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
@@ -642,25 +642,18 @@ static int batch_object_cb(const struct object_id *oid, void *vdata)
642642
return 0;
643643
}
644644

645-
static int collect_loose_object(const struct object_id *oid,
646-
const char *path UNUSED,
647-
void *data)
648-
{
649-
oid_array_append(data, oid);
650-
return 0;
651-
}
652-
653-
static int collect_packed_object(const struct object_id *oid,
654-
struct packed_git *pack UNUSED,
655-
uint32_t pos UNUSED,
656-
void *data)
645+
static int collect_object(const struct object_id *oid,
646+
struct packed_git *pack UNUSED,
647+
off_t offset UNUSED,
648+
void *data)
657649
{
658650
oid_array_append(data, oid);
659651
return 0;
660652
}
661653

662654
static int batch_unordered_object(const struct object_id *oid,
663-
struct packed_git *pack, off_t offset,
655+
struct packed_git *pack,
656+
off_t offset,
664657
void *vdata)
665658
{
666659
struct object_cb_data *data = vdata;
@@ -674,23 +667,6 @@ static int batch_unordered_object(const struct object_id *oid,
674667
return 0;
675668
}
676669

677-
static int batch_unordered_loose(const struct object_id *oid,
678-
const char *path UNUSED,
679-
void *data)
680-
{
681-
return batch_unordered_object(oid, NULL, 0, data);
682-
}
683-
684-
static int batch_unordered_packed(const struct object_id *oid,
685-
struct packed_git *pack,
686-
uint32_t pos,
687-
void *data)
688-
{
689-
return batch_unordered_object(oid, pack,
690-
nth_packed_object_offset(pack, pos),
691-
data);
692-
}
693-
694670
typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
695671
struct strbuf *, struct expand_data *);
696672

@@ -823,6 +799,45 @@ static void batch_objects_command(struct batch_options *opt,
823799

824800
#define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
825801

802+
typedef int (*for_each_object_fn)(const struct object_id *oid, struct packed_git *pack,
803+
off_t offset, void *data);
804+
805+
struct for_each_object_payload {
806+
for_each_object_fn callback;
807+
void *payload;
808+
};
809+
810+
static int batch_one_object_loose(const struct object_id *oid,
811+
const char *path UNUSED,
812+
void *_payload)
813+
{
814+
struct for_each_object_payload *payload = _payload;
815+
return payload->callback(oid, NULL, 0, payload->payload);
816+
}
817+
818+
static int batch_one_object_packed(const struct object_id *oid,
819+
struct packed_git *pack,
820+
uint32_t pos,
821+
void *_payload)
822+
{
823+
struct for_each_object_payload *payload = _payload;
824+
return payload->callback(oid, pack, nth_packed_object_offset(pack, pos),
825+
payload->payload);
826+
}
827+
828+
static void batch_each_object(for_each_object_fn callback,
829+
unsigned flags,
830+
void *_payload)
831+
{
832+
struct for_each_object_payload payload = {
833+
.callback = callback,
834+
.payload = _payload,
835+
};
836+
for_each_loose_object(batch_one_object_loose, &payload, 0);
837+
for_each_packed_object(the_repository, batch_one_object_packed,
838+
&payload, flags);
839+
}
840+
826841
static int batch_objects(struct batch_options *opt)
827842
{
828843
struct strbuf input = STRBUF_INIT;
@@ -877,18 +892,14 @@ static int batch_objects(struct batch_options *opt)
877892

878893
cb.seen = &seen;
879894

880-
for_each_loose_object(batch_unordered_loose, &cb, 0);
881-
for_each_packed_object(the_repository, batch_unordered_packed,
882-
&cb, FOR_EACH_OBJECT_PACK_ORDER);
895+
batch_each_object(batch_unordered_object,
896+
FOR_EACH_OBJECT_PACK_ORDER, &cb);
883897

884898
oidset_clear(&seen);
885899
} else {
886900
struct oid_array sa = OID_ARRAY_INIT;
887901

888-
for_each_loose_object(collect_loose_object, &sa, 0);
889-
for_each_packed_object(the_repository, collect_packed_object,
890-
&sa, 0);
891-
902+
batch_each_object(collect_object, 0, &sa);
892903
oid_array_for_each_unique(&sa, batch_object_cb, &cb);
893904

894905
oid_array_clear(&sa);

0 commit comments

Comments
 (0)