pack: move for_each_packed_object()

Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 18, 2017 at 15:20 UTC 7709f468fdeece2a99d60a581a4ced65cd2844df
6 files changed +54 -46
builtin/cat-file.c
+1
@@ -12,6 +12,7 @@
12 #include "streaming.h"
13 #include "tree-walk.h"
14 #include "sha1-array.h"
15 +#include "packfile.h"
16
17 struct batch_options {
18 int enabled;
cache.h
+1 -6
@@ -1662,17 +1662,12 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
1662 void *data);
1663
1664 /*
1665 - * Iterate over loose and packed objects in both the local
1665 + * Iterate over loose objects in both the local
1666 * repository and any alternates repositories (unless the
1667 * LOCAL_ONLY flag is set).
1668 */
1669 #define FOR_EACH_OBJECT_LOCAL_ONLY 0x1
1670 -typedef int each_packed_object_fn(const struct object_id *oid,
1671 - struct packed_git *pack,
1672 - uint32_t pos,
1673 - void *data);
1670 extern int for_each_loose_object(each_loose_object_fn, void *, unsigned flags);
1675 -extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
1671
1672 struct object_info {
1673 /* Request */
packfile.c
+40
@@ -1854,3 +1854,43 @@ int has_pack_index(const unsigned char *sha1)
1854 return 0;
1855 return 1;
1856 }
1857 +
1858 +static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
1859 +{
1860 + uint32_t i;
1861 + int r = 0;
1862 +
1863 + for (i = 0; i < p->num_objects; i++) {
1864 + struct object_id oid;
1865 +
1866 + if (!nth_packed_object_oid(&oid, p, i))
1867 + return error("unable to get sha1 of object %u in %s",
1868 + i, p->pack_name);
1869 +
1870 + r = cb(&oid, p, i, data);
1871 + if (r)
1872 + break;
1873 + }
1874 + return r;
1875 +}
1876 +
1877 +int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
1878 +{
1879 + struct packed_git *p;
1880 + int r = 0;
1881 + int pack_errors = 0;
1882 +
1883 + prepare_packed_git();
1884 + for (p = packed_git; p; p = p->next) {
1885 + if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
1886 + continue;
1887 + if (open_pack_index(p)) {
1888 + pack_errors = 1;
1889 + continue;
1890 + }
1891 + r = for_each_object_in_pack(p, cb, data);
1892 + if (r)
1893 + break;
1894 + }
1895 + return r ? r : pack_errors;
1896 +}
packfile.h
+11
@@ -124,4 +124,15 @@ extern int has_sha1_pack(const unsigned char *sha1);
124
125 extern int has_pack_index(const unsigned char *sha1);
126
127 +/*
128 + * Iterate over packed objects in both the local
129 + * repository and any alternates repositories (unless the
130 + * FOR_EACH_OBJECT_LOCAL_ONLY flag, defined in cache.h, is set).
131 + */
132 +typedef int each_packed_object_fn(const struct object_id *oid,
133 + struct packed_git *pack,
134 + uint32_t pos,
135 + void *data);
136 +extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
137 +
138 #endif
reachable.c
+1
@@ -9,6 +9,7 @@
9 #include "cache-tree.h"
10 #include "progress.h"
11 #include "list-objects.h"
12 +#include "packfile.h"
13
14 struct connectivity_progress {
15 struct progress *progress;
sha1_file.c
-40
@@ -2015,46 +2015,6 @@ int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags)
2015 return foreach_alt_odb(loose_from_alt_odb, &alt);
2016 }
2017
2018 -static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
2019 -{
2020 - uint32_t i;
2021 - int r = 0;
2022 -
2023 - for (i = 0; i < p->num_objects; i++) {
2024 - struct object_id oid;
2025 -
2026 - if (!nth_packed_object_oid(&oid, p, i))
2027 - return error("unable to get sha1 of object %u in %s",
2028 - i, p->pack_name);
2029 -
2030 - r = cb(&oid, p, i, data);
2031 - if (r)
2032 - break;
2033 - }
2034 - return r;
2035 -}
2036 -
2037 -int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
2038 -{
2039 - struct packed_git *p;
2040 - int r = 0;
2041 - int pack_errors = 0;
2042 -
2043 - prepare_packed_git();
2044 - for (p = packed_git; p; p = p->next) {
2045 - if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
2046 - continue;
2047 - if (open_pack_index(p)) {
2048 - pack_errors = 1;
2049 - continue;
2050 - }
2051 - r = for_each_object_in_pack(p, cb, data);
2052 - if (r)
2053 - break;
2054 - }
2055 - return r ? r : pack_errors;
2056 -}
2057 -
2018 static int check_stream_sha1(git_zstream *stream,
2019 const char *hdr,
2020 unsigned long size,