Skip to content

Commit 1d8d9cb

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc04167 commit 1d8d9cb

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

object-store.h

+23-2
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,33 @@ int read_loose_object(const char *path,
239239
unsigned long *size,
240240
void **contents);
241241

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+
*/
242265
#ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
243266
#define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
244267
#define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
245268
#endif
246-
247-
/* Same as the above, except for struct object_id. */
248269
int repo_has_object_file(struct repository *r, const struct object_id *oid);
249270
int repo_has_object_file_with_flags(struct repository *r,
250271
const struct object_id *oid, int flags);

sha1-file.c

+12
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,18 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
19891989
return ret;
19901990
}
19911991

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+
19922004
int repo_has_object_file_with_flags(struct repository *r,
19932005
const struct object_id *oid, int flags)
19942006
{

0 commit comments

Comments
 (0)