for_each_*_object: move declarations to object-store.h

The for_each_loose_object() and for_each_packed_object() functions are meant to be part of a unified interface: they use the same set of for_each_object_flags, and it's not inconceivable that we might one day add a single for_each_object() wrapper around them. Let's put them together in a single file, so we can avoid awkwardness like saying "the flags for this function are over in cache.h". Moving the loose functions to packfile.h is silly. Moving the packed functions to cache.h works, but makes the "cache.h is a kitchen sink" problem worse. The best place is the recently-created object-store.h, since these are quite obviously related to object storage. The for_each_*_in_objdir() functions do not use the same flags, but they are logically part of the same interface as for_each_loose_object(), and share callback signatures. So we'll move those, as well, as they also make sense in object-store.h. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 14, 2018 at 14:21 UTC 0889aae1cd18c1804ba01c1a4229e516dfb9fe9b
4 files changed +91 -95
builtin/prune-packed.c
+1
@@ -3,6 +3,7 @@
3 #include "progress.h"
4 #include "parse-options.h"
5 #include "packfile.h"
6 +#include "object-store.h"
7
8 static const char * const prune_packed_usage[] = {
9 N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
cache.h
-75
@@ -1575,81 +1575,6 @@ extern int odb_mkstemp(struct strbuf *temp_filename, const char *pattern);
1575 */
1576 extern int odb_pack_keep(const char *name);
1577
1578 -/*
1579 - * Iterate over the files in the loose-object parts of the object
1580 - * directory "path", triggering the following callbacks:
1581 - *
1582 - * - loose_object is called for each loose object we find.
1583 - *
1584 - * - loose_cruft is called for any files that do not appear to be
1585 - * loose objects. Note that we only look in the loose object
1586 - * directories "objects/[0-9a-f]{2}/", so we will not report
1587 - * "objects/foobar" as cruft.
1588 - *
1589 - * - loose_subdir is called for each top-level hashed subdirectory
1590 - * of the object directory (e.g., "$OBJDIR/f0"). It is called
1591 - * after the objects in the directory are processed.
1592 - *
1593 - * Any callback that is NULL will be ignored. Callbacks returning non-zero
1594 - * will end the iteration.
1595 - *
1596 - * In the "buf" variant, "path" is a strbuf which will also be used as a
1597 - * scratch buffer, but restored to its original contents before
1598 - * the function returns.
1599 - */
1600 -typedef int each_loose_object_fn(const struct object_id *oid,
1601 - const char *path,
1602 - void *data);
1603 -typedef int each_loose_cruft_fn(const char *basename,
1604 - const char *path,
1605 - void *data);
1606 -typedef int each_loose_subdir_fn(unsigned int nr,
1607 - const char *path,
1608 - void *data);
1609 -int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1610 - struct strbuf *path,
1611 - each_loose_object_fn obj_cb,
1612 - each_loose_cruft_fn cruft_cb,
1613 - each_loose_subdir_fn subdir_cb,
1614 - void *data);
1615 -int for_each_loose_file_in_objdir(const char *path,
1616 - each_loose_object_fn obj_cb,
1617 - each_loose_cruft_fn cruft_cb,
1618 - each_loose_subdir_fn subdir_cb,
1619 - void *data);
1620 -int for_each_loose_file_in_objdir_buf(struct strbuf *path,
1621 - each_loose_object_fn obj_cb,
1622 - each_loose_cruft_fn cruft_cb,
1623 - each_loose_subdir_fn subdir_cb,
1624 - void *data);
1625 -
1626 -/*
1627 - * Flags for for_each_*_object(), including for_each_loose below and
1628 - * for_each_packed in packfile.h.
1629 - */
1630 -enum for_each_object_flags {
1631 - /* Iterate only over local objects, not alternates. */
1632 - FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
1633 -
1634 - /* Only iterate over packs obtained from the promisor remote. */
1635 - FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
1636 -
1637 - /*
1638 - * Visit objects within a pack in packfile order rather than .idx order
1639 - */
1640 - FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
1641 -};
1642 -
1643 -/*
1644 - * Iterate over all accessible loose objects without respect to
1645 - * reachability. By default, this includes both local and alternate objects.
1646 - * The order in which objects are visited is unspecified.
1647 - *
1648 - * Any flags specific to packs are ignored.
1649 - */
1650 -int for_each_loose_object(each_loose_object_fn, void *,
1651 - enum for_each_object_flags flags);
1652 -
1578 /*
1579 * Set this to 0 to prevent sha1_object_info_extended() from fetching missing
1580 * blobs. This has a difference only if extensions.partialClone is set.
object-store.h
+90
@@ -262,4 +262,94 @@ int oid_object_info_extended(struct repository *r,
262 const struct object_id *,
263 struct object_info *, unsigned flags);
264
265 +/*
266 + * Iterate over the files in the loose-object parts of the object
267 + * directory "path", triggering the following callbacks:
268 + *
269 + * - loose_object is called for each loose object we find.
270 + *
271 + * - loose_cruft is called for any files that do not appear to be
272 + * loose objects. Note that we only look in the loose object
273 + * directories "objects/[0-9a-f]{2}/", so we will not report
274 + * "objects/foobar" as cruft.
275 + *
276 + * - loose_subdir is called for each top-level hashed subdirectory
277 + * of the object directory (e.g., "$OBJDIR/f0"). It is called
278 + * after the objects in the directory are processed.
279 + *
280 + * Any callback that is NULL will be ignored. Callbacks returning non-zero
281 + * will end the iteration.
282 + *
283 + * In the "buf" variant, "path" is a strbuf which will also be used as a
284 + * scratch buffer, but restored to its original contents before
285 + * the function returns.
286 + */
287 +typedef int each_loose_object_fn(const struct object_id *oid,
288 + const char *path,
289 + void *data);
290 +typedef int each_loose_cruft_fn(const char *basename,
291 + const char *path,
292 + void *data);
293 +typedef int each_loose_subdir_fn(unsigned int nr,
294 + const char *path,
295 + void *data);
296 +int for_each_file_in_obj_subdir(unsigned int subdir_nr,
297 + struct strbuf *path,
298 + each_loose_object_fn obj_cb,
299 + each_loose_cruft_fn cruft_cb,
300 + each_loose_subdir_fn subdir_cb,
301 + void *data);
302 +int for_each_loose_file_in_objdir(const char *path,
303 + each_loose_object_fn obj_cb,
304 + each_loose_cruft_fn cruft_cb,
305 + each_loose_subdir_fn subdir_cb,
306 + void *data);
307 +int for_each_loose_file_in_objdir_buf(struct strbuf *path,
308 + each_loose_object_fn obj_cb,
309 + each_loose_cruft_fn cruft_cb,
310 + each_loose_subdir_fn subdir_cb,
311 + void *data);
312 +
313 +/* Flags for for_each_*_object() below. */
314 +enum for_each_object_flags {
315 + /* Iterate only over local objects, not alternates. */
316 + FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
317 +
318 + /* Only iterate over packs obtained from the promisor remote. */
319 + FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
320 +
321 + /*
322 + * Visit objects within a pack in packfile order rather than .idx order
323 + */
324 + FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
325 +};
326 +
327 +/*
328 + * Iterate over all accessible loose objects without respect to
329 + * reachability. By default, this includes both local and alternate objects.
330 + * The order in which objects are visited is unspecified.
331 + *
332 + * Any flags specific to packs are ignored.
333 + */
334 +int for_each_loose_object(each_loose_object_fn, void *,
335 + enum for_each_object_flags flags);
336 +
337 +/*
338 + * Iterate over all accessible packed objects without respect to reachability.
339 + * By default, this includes both local and alternate packs.
340 + *
341 + * Note that some objects may appear twice if they are found in multiple packs.
342 + * Each pack is visited in an unspecified order. By default, objects within a
343 + * pack are visited in pack-idx order (i.e., sorted by oid).
344 + */
345 +typedef int each_packed_object_fn(const struct object_id *oid,
346 + struct packed_git *pack,
347 + uint32_t pos,
348 + void *data);
349 +int for_each_object_in_pack(struct packed_git *p,
350 + each_packed_object_fn, void *data,
351 + enum for_each_object_flags flags);
352 +int for_each_packed_object(each_packed_object_fn, void *,
353 + enum for_each_object_flags flags);
354 +
355 #endif /* OBJECT_STORE_H */
packfile.h
-20
@@ -148,26 +148,6 @@ extern int has_object_pack(const struct object_id *oid);
148
149 extern int has_pack_index(const unsigned char *sha1);
150
151 -/*
152 - * Iterate over all accessible packed objects without respect to reachability.
153 - * By default, this includes both local and alternate packs.
154 - *
155 - * Note that some objects may appear twice if they are found in multiple packs.
156 - * Each pack is visited in an unspecified order. By default, objects within a
157 - * pack are visited in pack-idx order (i.e., sorted by oid).
158 - *
159 - * The list of flags can be found in cache.h.
160 - */
161 -typedef int each_packed_object_fn(const struct object_id *oid,
162 - struct packed_git *pack,
163 - uint32_t pos,
164 - void *data);
165 -int for_each_object_in_pack(struct packed_git *p,
166 - each_packed_object_fn, void *data,
167 - enum for_each_object_flags flags);
168 -int for_each_packed_object(each_packed_object_fn, void *,
169 - enum for_each_object_flags flags);
170 -
151 /*
152 * Return 1 if an object in a promisor packfile is or refers to the given
153 * object, 0 otherwise.