treewide: convert users of `repo_has_object_file()` to `has_object()`

As the comment of `repo_has_object_file()` and its `_with_flags()` variant tells us, these functions are considered to be deprecated in favor of `has_object()`. There are a couple of slight benefits in favor of the replacement: - The new function has a short-and-sweet name. - More explicit defaults: `has_object()` doesn't fetch missing objects via promisor remotes, and neither does it reload packfiles if an object wasn't found by default. This ensures that it becomes immediately obvious when a simple object existence check may result in expensive actions. Most importantly though, it is confusing that we have two sets of functions that ultimately do the same thing, but with different defaults. Start sunsetting `repo_has_object_file()` and its `_with_flags()` sibling by replacing all callsites with `has_object()`: - `repo_has_object_file(...)` is equivalent to `has_object(..., HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)`. - `repo_has_object_file_with_flags(..., OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT)` is equivalent to `has_object(..., 0)`. - `repo_has_object_file_with_flags(..., OBJECT_INFO_SKIP_FETCH_OBJECT)` is equivalent to `has_object(..., HAS_OBJECT_RECHECK_PACKED)`. - `repo_has_object_file_with_flags(..., OBJECT_INFO_QUICK)` is equivalent to `has_object(..., HAS_OBJECT_FETCH_PROMISOR)`. The replacements should be functionally equivalent. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 29, 2025 at 09:52 UTC 062b914c841329a003f74e1340ea5178391274a6
23 files changed +65 -50
builtin/cat-file.c
+2 -1
@@ -169,7 +169,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
169 goto cleanup;
170
171 case 'e':
172 - ret = !repo_has_object_file(the_repository, &oid);
172 + ret = !has_object(the_repository, &oid,
173 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR);
174 goto cleanup;
175
176 case 'w':
builtin/clone.c
+1 -3
@@ -504,9 +504,7 @@ static void write_followtags(const struct ref *refs, const char *msg)
504 continue;
505 if (ends_with(ref->name, "^{}"))
506 continue;
507 - if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid,
508 - OBJECT_INFO_QUICK |
509 - OBJECT_INFO_SKIP_FETCH_OBJECT))
507 + if (!has_object(the_repository, &ref->old_oid, 0))
508 continue;
509 refs_update_ref(get_main_ref_store(the_repository), msg,
510 ref->name, &ref->old_oid, NULL, 0,
builtin/fetch.c
+7 -8
@@ -337,7 +337,6 @@ static void find_non_local_tags(const struct ref *refs,
337 struct string_list_item *remote_ref_item;
338 const struct ref *ref;
339 struct refname_hash_entry *item = NULL;
340 - const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT;
340
341 refname_hash_init(&existing_refs);
342 refname_hash_init(&remote_refs);
@@ -367,9 +366,9 @@ static void find_non_local_tags(const struct ref *refs,
366 */
367 if (ends_with(ref->name, "^{}")) {
368 if (item &&
370 - !repo_has_object_file_with_flags(the_repository, &ref->old_oid, quick_flags) &&
369 + !has_object(the_repository, &ref->old_oid, 0) &&
370 !oidset_contains(&fetch_oids, &ref->old_oid) &&
372 - !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) &&
371 + !has_object(the_repository, &item->oid, 0) &&
372 !oidset_contains(&fetch_oids, &item->oid))
373 clear_item(item);
374 item = NULL;
@@ -383,7 +382,7 @@ static void find_non_local_tags(const struct ref *refs,
382 * fetch.
383 */
384 if (item &&
386 - !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) &&
385 + !has_object(the_repository, &item->oid, 0) &&
386 !oidset_contains(&fetch_oids, &item->oid))
387 clear_item(item);
388
@@ -404,7 +403,7 @@ static void find_non_local_tags(const struct ref *refs,
403 * checked to see if it needs fetching.
404 */
405 if (item &&
407 - !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) &&
406 + !has_object(the_repository, &item->oid, 0) &&
407 !oidset_contains(&fetch_oids, &item->oid))
408 clear_item(item);
409
@@ -911,7 +910,8 @@ static int update_local_ref(struct ref *ref,
910 struct commit *current = NULL, *updated;
911 int fast_forward = 0;
912
914 - if (!repo_has_object_file(the_repository, &ref->new_oid))
913 + if (!has_object(the_repository, &ref->new_oid,
914 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
915 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
916
917 if (oideq(&ref->old_oid, &ref->new_oid)) {
@@ -1330,8 +1330,7 @@ static int check_exist_and_connected(struct ref *ref_map)
1330 * we need all direct targets to exist.
1331 */
1332 for (r = rm; r; r = r->next) {
1333 - if (!repo_has_object_file_with_flags(the_repository, &r->old_oid,
1334 - OBJECT_INFO_SKIP_FETCH_OBJECT))
1333 + if (!has_object(the_repository, &r->old_oid, HAS_OBJECT_RECHECK_PACKED))
1334 return -1;
1335 }
1336
builtin/index-pack.c
+2 -3
@@ -892,9 +892,8 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
892
893 if (startup_info->have_repository) {
894 read_lock();
895 - collision_test_needed =
896 - repo_has_object_file_with_flags(the_repository, oid,
897 - OBJECT_INFO_QUICK);
895 + collision_test_needed = has_object(the_repository, oid,
896 + HAS_OBJECT_FETCH_PROMISOR);
897 read_unlock();
898 }
899
builtin/receive-pack.c
+3 -1
@@ -1506,7 +1506,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1506 }
1507 }
1508
1509 - if (!is_null_oid(new_oid) && !repo_has_object_file(the_repository, new_oid)) {
1509 + if (!is_null_oid(new_oid) &&
1510 + !has_object(the_repository, new_oid,
1511 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
1512 error("unpack should have generated %s, "
1513 "but I can't find it!", oid_to_hex(new_oid));
1514 ret = "bad pack";
builtin/remote.c
+2 -1
@@ -454,7 +454,8 @@ static int get_push_ref_states(const struct ref *remote_refs,
454 info->status = PUSH_STATUS_UPTODATE;
455 else if (is_null_oid(&ref->old_oid))
456 info->status = PUSH_STATUS_CREATE;
457 - else if (repo_has_object_file(the_repository, &ref->old_oid) &&
457 + else if (has_object(the_repository, &ref->old_oid,
458 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR) &&
459 ref_newer(&ref->new_oid, &ref->old_oid))
460 info->status = PUSH_STATUS_FASTFORWARD;
461 else
builtin/show-ref.c
+2 -1
@@ -35,7 +35,8 @@ static void show_one(const struct show_one_options *opts,
35 const char *hex;
36 struct object_id peeled;
37
38 - if (!repo_has_object_file(the_repository, oid))
38 + if (!has_object(the_repository, oid,
39 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
40 die("git show-ref: bad ref %s (%s)", refname,
41 oid_to_hex(oid));
42
builtin/unpack-objects.c
+2 -1
@@ -449,7 +449,8 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
449 delta_data = get_data(delta_size);
450 if (!delta_data)
451 return;
452 - if (repo_has_object_file(the_repository, &base_oid))
452 + if (has_object(the_repository, &base_oid,
453 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
454 ; /* Ok we have this one */
455 else if (resolve_against_held(nr, &base_oid,
456 delta_data, delta_size))
bulk-checkin.c
+2 -1
@@ -130,7 +130,8 @@ static void flush_batch_fsync(void)
130 static int already_written(struct bulk_checkin_packfile *state, struct object_id *oid)
131 {
132 /* The object may already exist in the repository */
133 - if (repo_has_object_file(the_repository, oid))
133 + if (has_object(the_repository, oid,
134 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
135 return 1;
136
137 /* Might want to keep the list sorted */
cache-tree.c
+9 -4
@@ -238,7 +238,9 @@ int cache_tree_fully_valid(struct cache_tree *it)
238 int i;
239 if (!it)
240 return 0;
241 - if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid))
241 + if (it->entry_count < 0 ||
242 + has_object(the_repository, &it->oid,
243 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
244 return 0;
245 for (i = 0; i < it->subtree_nr; i++) {
246 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
@@ -289,7 +291,9 @@ static int update_one(struct cache_tree *it,
291 }
292 }
293
292 - if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid))
294 + if (0 <= it->entry_count &&
295 + has_object(the_repository, &it->oid,
296 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
297 return it->entry_count;
298
299 /*
@@ -395,7 +399,8 @@ static int update_one(struct cache_tree *it,
399 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
400 !must_check_existence(ce);
401 if (is_null_oid(oid) ||
398 - (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) {
402 + (!ce_missing_ok && !has_object(the_repository, oid,
403 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))) {
404 strbuf_release(&buffer);
405 if (expected_missing)
406 return -1;
@@ -443,7 +448,7 @@ static int update_one(struct cache_tree *it,
448 struct object_id oid;
449 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
450 OBJ_TREE, &oid);
446 - if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
451 + if (has_object(the_repository, &oid, HAS_OBJECT_RECHECK_PACKED))
452 oidcpy(&it->oid, &oid);
453 else
454 to_invalidate = 1;
fetch-pack.c
+3 -4
@@ -769,9 +769,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
769 if (!commit) {
770 struct object *o;
771
772 - if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid,
773 - OBJECT_INFO_QUICK |
774 - OBJECT_INFO_SKIP_FETCH_OBJECT))
772 + if (!has_object(the_repository, &ref->old_oid, 0))
773 continue;
774 o = parse_object(the_repository, &ref->old_oid);
775 if (!o || o->type != OBJ_COMMIT)
@@ -1985,7 +1983,8 @@ static void update_shallow(struct fetch_pack_args *args,
1983 struct oid_array extra = OID_ARRAY_INIT;
1984 struct object_id *oid = si->shallow->oid;
1985 for (i = 0; i < si->shallow->nr; i++)
1988 - if (repo_has_object_file(the_repository, &oid[i]))
1986 + if (has_object(the_repository, &oid[i],
1987 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
1988 oid_array_append(&extra, &oid[i]);
1989 if (extra.nr) {
1990 setup_alternate_shallow(&shallow_lock,
http-push.c
+7 -4
@@ -1446,7 +1446,9 @@ static void one_remote_ref(const char *refname)
1446 * Fetch a copy of the object if it doesn't exist locally - it
1447 * may be required for updating server info later.
1448 */
1449 - if (repo->can_update_info_refs && !repo_has_object_file(the_repository, &ref->old_oid)) {
1449 + if (repo->can_update_info_refs &&
1450 + !has_object(the_repository, &ref->old_oid,
1451 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
1452 obj = lookup_unknown_object(the_repository, &ref->old_oid);
1453 fprintf(stderr, " fetch %s for %s\n",
1454 oid_to_hex(&ref->old_oid), refname);
@@ -1651,14 +1653,14 @@ static int delete_remote_branch(const char *pattern, int force)
1653 return error("Remote HEAD symrefs too deep");
1654 if (is_null_oid(&head_oid))
1655 return error("Unable to resolve remote HEAD");
1654 - if (!repo_has_object_file(the_repository, &head_oid))
1656 + if (!has_object(the_repository, &head_oid, HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
1657 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid));
1658
1659 /* Remote branch must resolve to a known object */
1660 if (is_null_oid(&remote_ref->old_oid))
1661 return error("Unable to resolve remote branch %s",
1662 remote_ref->name);
1661 - if (!repo_has_object_file(the_repository, &remote_ref->old_oid))
1663 + if (!has_object(the_repository, &remote_ref->old_oid, HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
1664 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, oid_to_hex(&remote_ref->old_oid));
1665
1666 /* Remote branch must be an ancestor of remote HEAD */
@@ -1879,7 +1881,8 @@ int cmd_main(int argc, const char **argv)
1881 if (!force_all &&
1882 !is_null_oid(&ref->old_oid) &&
1883 !ref->force) {
1882 - if (!repo_has_object_file(the_repository, &ref->old_oid) ||
1884 + if (!has_object(the_repository, &ref->old_oid,
1885 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR) ||
1886 !ref_newer(&ref->peer_ref->new_oid,
1887 &ref->old_oid)) {
1888 /*
http-walker.c
+4 -2
@@ -138,7 +138,8 @@ static int fill_active_slot(void *data UNUSED)
138 list_for_each_safe(pos, tmp, head) {
139 obj_req = list_entry(pos, struct object_request, node);
140 if (obj_req->state == WAITING) {
141 - if (repo_has_object_file(the_repository, &obj_req->oid))
141 + if (has_object(the_repository, &obj_req->oid,
142 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
143 obj_req->state = COMPLETE;
144 else {
145 start_object_request(obj_req);
@@ -496,7 +497,8 @@ static int fetch_object(struct walker *walker, const struct object_id *oid)
497 if (!obj_req)
498 return error("Couldn't find request for %s in the queue", hex);
499
499 - if (repo_has_object_file(the_repository, &obj_req->oid)) {
500 + if (has_object(the_repository, &obj_req->oid,
501 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
502 if (obj_req->req)
503 abort_http_object_request(&obj_req->req);
504 abort_object_request(obj_req);
list-objects.c
+2 -1
@@ -74,7 +74,8 @@ static void process_blob(struct traversal_context *ctx,
74 * of missing objects.
75 */
76 if (ctx->revs->exclude_promisor_objects &&
77 - !repo_has_object_file(the_repository, &obj->oid) &&
77 + !has_object(the_repository, &obj->oid,
78 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR) &&
79 is_promisor_object(ctx->revs->repo, &obj->oid))
80 return;
81
notes.c
+2 -1
@@ -794,7 +794,8 @@ static int prune_notes_helper(const struct object_id *object_oid,
794 struct note_delete_list **l = (struct note_delete_list **) cb_data;
795 struct note_delete_list *n;
796
797 - if (repo_has_object_file(the_repository, object_oid))
797 + if (has_object(the_repository, object_oid,
798 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
799 return 0; /* nothing to do for this note */
800
801 /* failed to find object => prune this note */
object-store.c
+1 -1
@@ -847,7 +847,7 @@ int pretend_object_file(struct repository *repo,
847 char *co_buf;
848
849 hash_object_file(repo->hash_algo, buf, len, type, oid);
850 - if (repo_has_object_file_with_flags(repo, oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
850 + if (has_object(repo, oid, 0) ||
851 find_cached_object(repo->objects, oid))
852 return 0;
853
reflog.c
+2 -1
@@ -152,7 +152,8 @@ static int tree_is_complete(const struct object_id *oid)
152 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
153 complete = 1;
154 while (tree_entry(&desc, &entry)) {
155 - if (!repo_has_object_file(the_repository, &entry.oid) ||
155 + if (!has_object(the_repository, &entry.oid,
156 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR) ||
157 (S_ISDIR(entry.mode) && !tree_is_complete(&entry.oid))) {
158 tree->object.flags |= INCOMPLETE;
159 complete = 0;
refs.c
+1 -1
@@ -376,7 +376,7 @@ int ref_resolves_to_object(const char *refname,
376 {
377 if (flags & REF_ISBROKEN)
378 return 0;
379 - if (!repo_has_object_file(repo, oid)) {
379 + if (!has_object(repo, oid, HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
380 error(_("%s does not point to a valid object!"), refname);
381 return 0;
382 }
remote.c
+1 -1
@@ -1702,7 +1702,7 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1702 if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) {
1703 if (starts_with(ref->name, "refs/tags/"))
1704 reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
1705 - else if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
1705 + else if (!has_object(the_repository, &ref->old_oid, HAS_OBJECT_RECHECK_PACKED))
1706 reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
1707 else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) ||
1708 !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1))
send-pack.c
+1 -4
@@ -45,10 +45,7 @@ int option_parse_push_signed(const struct option *opt,
45 static void feed_object(struct repository *r,
46 const struct object_id *oid, FILE *fh, int negative)
47 {
48 - if (negative &&
49 - !repo_has_object_file_with_flags(r, oid,
50 - OBJECT_INFO_SKIP_FETCH_OBJECT |
51 - OBJECT_INFO_QUICK))
48 + if (negative && !has_object(r, oid, 0))
49 return;
50
51 if (negative)
shallow.c
+6 -3
@@ -310,7 +310,8 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
310 if (graft->nr_parent != -1)
311 return 0;
312 if (data->flags & QUICK) {
313 - if (!repo_has_object_file(the_repository, &graft->oid))
313 + if (!has_object(the_repository, &graft->oid,
314 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
315 return 0;
316 } else if (data->flags & SEEN_ONLY) {
317 struct commit *c = lookup_commit(the_repository, &graft->oid);
@@ -476,7 +477,8 @@ void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
477 ALLOC_ARRAY(info->ours, sa->nr);
478 ALLOC_ARRAY(info->theirs, sa->nr);
479 for (size_t i = 0; i < sa->nr; i++) {
479 - if (repo_has_object_file(the_repository, sa->oid + i)) {
480 + if (has_object(the_repository, sa->oid + i,
481 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
482 struct commit_graft *graft;
483 graft = lookup_commit_graft(the_repository,
484 &sa->oid[i]);
@@ -513,7 +515,8 @@ void remove_nonexistent_theirs_shallow(struct shallow_info *info)
515 for (i = dst = 0; i < info->nr_theirs; i++) {
516 if (i != dst)
517 info->theirs[dst] = info->theirs[i];
516 - if (repo_has_object_file(the_repository, oid + info->theirs[i]))
518 + if (has_object(the_repository, oid + info->theirs[i],
519 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
520 dst++;
521 }
522 info->nr_theirs = dst;
upload-pack.c
+1 -2
@@ -509,8 +509,7 @@ static int got_oid(struct upload_pack_data *data,
509 {
510 if (get_oid_hex(hex, oid))
511 die("git upload-pack: expected SHA1 object, got '%s'", hex);
512 - if (!repo_has_object_file_with_flags(the_repository, oid,
513 - OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
512 + if (!has_object(the_repository, oid, 0))
513 return -1;
514 return do_got_oid(data, oid);
515 }
walker.c
+2 -1
@@ -150,7 +150,8 @@ static int process(struct walker *walker, struct object *obj)
150 return 0;
151 obj->flags |= SEEN;
152
153 - if (repo_has_object_file(the_repository, &obj->oid)) {
153 + if (has_object(the_repository, &obj->oid,
154 + HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) {
155 /* We already have it, so we should scan it now. */
156 obj->flags |= TO_SCAN;
157 }