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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Apr 2, 2025 at 13:13 UTC
d5ec7027bcbf755b95fba84ad1de50ca6d3c3daa
1 file changed
+48
-37
builtin/cat-file.c
+48
-37
@@ -642,25 +642,18 @@ static int batch_object_cb(const struct object_id *oid, void *vdata)
642
return 0;
643
}
644
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)
649
{
650
oid_array_append(data, oid);
651
return 0;
652
}
653
654
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,
657
void *vdata)
658
{
659
struct object_cb_data *data = vdata;
@@ -674,23 +667,6 @@ static int batch_unordered_object(const struct object_id *oid,
667
return 0;
668
}
669
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
-
670
typedef void (*parse_cmd_fn_t)(struct batch_options *, const char *,
671
struct strbuf *, struct expand_data *);
672
@@ -823,6 +799,45 @@ static void batch_objects_command(struct batch_options *opt,
799
800
#define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
801
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
+
841
static int batch_objects(struct batch_options *opt)
842
{
843
struct strbuf input = STRBUF_INIT;
@@ -877,18 +892,14 @@ static int batch_objects(struct batch_options *opt)
892
893
cb.seen = &seen;
894
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);
897
898
oidset_clear(&seen);
899
} else {
900
struct oid_array sa = OID_ARRAY_INIT;
901
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);
903
oid_array_for_each_unique(&sa, batch_object_cb, &cb);
904
905
oid_array_clear(&sa);