@matheusbsbs / matheus-bagliano / commits / 1d8d9cb620
Fork 0

sha1-file: introduce no-lazy-fetch has_object()

There have been a few bugs wherein Git fetches missing objects whenever the existence of an object is checked, even though it does not need to perform such a fetch. To resolve these bugs, we could look at all the places that has_object_file() (or a similar function) is used. As a first step, introduce a new function has_object() that checks for the existence of an object, with a default behavior of not fetching if the object is missing and the repository is a partial clone. As we verify each has_object_file() (or similar) usage, we can replace it with has_object(), and we will know that we are done when we can delete has_object_file() (and the other similar functions). Also, the new function has_object() has more appropriate defaults: besides not fetching, it also does not recheck packed storage. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 5, 2020 at 16:06 UTC 1d8d9cb62099e1524ce1269ea88faad871c2197f
2 files changed +35 -2
object-store.h
+23 -2
@@ -239,12 +239,33 @@ int read_loose_object(const char *path,
239 unsigned long *size,
240 void **contents);
241
242 +/* Retry packed storage after checking packed and loose storage */
243 +#define HAS_OBJECT_RECHECK_PACKED 1
244 +
245 +/*
246 + * Returns 1 if the object exists. This function will not lazily fetch objects
247 + * in a partial clone.
248 + */
249 +int has_object(struct repository *r, const struct object_id *oid,
250 + unsigned flags);
251 +
252 +/*
253 + * These macros and functions are deprecated. If checking existence for an
254 + * object that is likely to be missing and/or whose absence is relatively
255 + * inconsequential (or is consequential but the caller is prepared to handle
256 + * it), use has_object(), which has better defaults (no lazy fetch in a partial
257 + * clone and no rechecking of packed storage). In the unlikely event that a
258 + * caller needs to assert existence of an object that it fully expects to
259 + * exist, and wants to trigger a lazy fetch in a partial clone, use
260 + * oid_object_info_extended() with a NULL struct object_info.
261 + *
262 + * These functions can be removed once all callers have migrated to
263 + * has_object() and/or oid_object_info_extended().
264 + */
265 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
266 #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
267 #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
268 #endif
246 -
247 -/* Same as the above, except for struct object_id. */
269 int repo_has_object_file(struct repository *r, const struct object_id *oid);
270 int repo_has_object_file_with_flags(struct repository *r,
271 const struct object_id *oid, int flags);
sha1-file.c
+12
@@ -1989,6 +1989,18 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
1989 return ret;
1990 }
1991
1992 +int has_object(struct repository *r, const struct object_id *oid,
1993 + unsigned flags)
1994 +{
1995 + int quick = !(flags & HAS_OBJECT_RECHECK_PACKED);
1996 + unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT |
1997 + (quick ? OBJECT_INFO_QUICK : 0);
1998 +
1999 + if (!startup_info->have_repository)
2000 + return 0;
2001 + return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0;
2002 +}
2003 +
2004 int repo_has_object_file_with_flags(struct repository *r,
2005 const struct object_id *oid, int flags)
2006 {