Skip to content

Commit d1a8a89

Browse files
committed
Merge branch 'jt/has_object'
A new helper function has_object() has been introduced to make it easier to mark object existence checks that do and don't want to trigger lazy fetches, and a few such checks are converted using it. * jt/has_object: fsck: do not lazy fetch known non-promisor object pack-objects: no fetch when allow-{any,promisor} apply: do not lazy fetch when applying binary sha1-file: introduce no-lazy-fetch has_object()
2 parents 092b677 + 9eb86f4 commit d1a8a89

File tree

7 files changed

+62
-10
lines changed

7 files changed

+62
-10
lines changed

Documentation/git-pack-objects.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,18 @@ So does `git bundle` (see linkgit:git-bundle[1]) when it creates a bundle.
270270
This option specifies how missing objects are handled.
271271
+
272272
The form '--missing=error' requests that pack-objects stop with an error if
273-
a missing object is encountered. This is the default action.
273+
a missing object is encountered. If the repository is a partial clone, an
274+
attempt to fetch missing objects will be made before declaring them missing.
275+
This is the default action.
274276
+
275277
The form '--missing=allow-any' will allow object traversal to continue
276-
if a missing object is encountered. Missing objects will silently be
277-
omitted from the results.
278+
if a missing object is encountered. No fetch of a missing object will occur.
279+
Missing objects will silently be omitted from the results.
278280
+
279281
The form '--missing=allow-promisor' is like 'allow-any', but will only
280282
allow object traversal to continue for EXPECTED promisor missing objects.
281-
Unexpected missing object will raise an error.
283+
No fetch of a missing object will occur. An unexpected missing object will
284+
raise an error.
282285

283286
--exclude-promisor-objects::
284287
Omit objects that are known to be in the promisor remote. (This

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3178,7 +3178,7 @@ static int apply_binary(struct apply_state *state,
31783178
return 0; /* deletion patch */
31793179
}
31803180

3181-
if (has_object_file(&oid)) {
3181+
if (has_object(the_repository, &oid, 0)) {
31823182
/* We already have the postimage */
31833183
enum object_type type;
31843184
unsigned long size;

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
168168
return 0;
169169

170170
if (!(obj->flags & HAS_OBJ)) {
171-
if (parent && !has_object_file(&obj->oid)) {
171+
if (parent && !has_object(the_repository, &obj->oid, 1)) {
172172
printf_ln(_("broken link from %7s %s\n"
173173
" to %7s %s"),
174174
printable_type(&parent->oid, parent->type),

builtin/pack-objects.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,7 +3048,7 @@ static void show_object__ma_allow_any(struct object *obj, const char *name, void
30483048
* Quietly ignore ALL missing objects. This avoids problems with
30493049
* staging them now and getting an odd error later.
30503050
*/
3051-
if (!has_object_file(&obj->oid))
3051+
if (!has_object(the_repository, &obj->oid, 0))
30523052
return;
30533053

30543054
show_object(obj, name, data);
@@ -3062,7 +3062,7 @@ static void show_object__ma_allow_promisor(struct object *obj, const char *name,
30623062
* Quietly ignore EXPECTED missing objects. This avoids problems with
30633063
* staging them now and getting an odd error later.
30643064
*/
3065-
if (!has_object_file(&obj->oid) && is_promisor_object(&obj->oid))
3065+
if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
30663066
return;
30673067

30683068
show_object(obj, name, data);

object-store.h

Lines changed: 23 additions & 2 deletions
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

Lines changed: 12 additions & 0 deletions
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
{

t/t4150-am.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,4 +1133,20 @@ test_expect_success 'am and .gitattibutes' '
11331133
)
11341134
'
11351135

1136+
test_expect_success 'apply binary blob in partial clone' '
1137+
printf "\\000" >binary &&
1138+
git add binary &&
1139+
git commit -m "binary blob" &&
1140+
git format-patch --stdout -m HEAD^ >patch &&
1141+
1142+
test_create_repo server &&
1143+
test_config -C server uploadpack.allowfilter 1 &&
1144+
test_config -C server uploadpack.allowanysha1inwant 1 &&
1145+
git clone --filter=blob:none "file://$(pwd)/server" client &&
1146+
test_when_finished "rm -rf client" &&
1147+
1148+
# Exercise to make sure that it works
1149+
git -C client am ../patch
1150+
'
1151+
11361152
test_done

0 commit comments

Comments
 (0)