packfile: convert find_sha1_pack() to use object_id

The find_sha1_pack() function has a few problems:

  - it's badly named, since it works with any object hash

  - it takes the hash as a bare pointer rather than an object_id struct

We can fix both of these easily, as all callers actually have a real
object_id anyway.

I also found the existence of this function somewhat confusing, as it is
about looking in an arbitrary set of linked packed_git structs. It's
good for things like dumb-http which are looking in downloaded remote
packs, and not our local packs. But despite the name, it is not a good
way to find the pack which contains a local object (it skips the use of
the midx, the pack mru list, and so on).

So let's also add an explanatory comment above the declaration that may
point people in the right direction.

I suspect the calls in fast-import.c, which use the packed_git list from
the repository struct, could actually just be using find_pack_entry().
But since we'd need to keep it anyway for dumb-http, I didn't dig
further there. If we eventually drop dumb-http support, then it might be
worth examining them to see if we can get rid of the function entirely.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
This commit is contained in:
Jeff King 2024-10-25 03:05:03 -04:00 committed by Taylor Blau
parent 0af861e0c8
commit 4d99559147
5 changed files with 15 additions and 12 deletions

View File

@ -966,8 +966,7 @@ static int store_object(
if (e->idx.offset) {
duplicate_count_by_type[type]++;
return 1;
} else if (find_sha1_pack(oid.hash,
get_all_packs(the_repository))) {
} else if (find_oid_pack(&oid, get_all_packs(the_repository))) {
e->type = type;
e->pack_id = MAX_PACK_ID;
e->idx.offset = 1; /* just not zero! */
@ -1167,8 +1166,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
duplicate_count_by_type[OBJ_BLOB]++;
truncate_pack(&checkpoint);
} else if (find_sha1_pack(oid.hash,
get_all_packs(the_repository))) {
} else if (find_oid_pack(&oid, get_all_packs(the_repository))) {
e->type = OBJ_BLOB;
e->pack_id = MAX_PACK_ID;
e->idx.offset = 1; /* just not zero! */

View File

@ -309,7 +309,7 @@ static void start_fetch_packed(struct transfer_request *request)
struct transfer_request *check_request = request_queue_head;
struct http_pack_request *preq;
target = find_sha1_pack(request->obj->oid.hash, repo->packs);
target = find_oid_pack(&request->obj->oid, repo->packs);
if (!target) {
fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
repo->can_update_info_refs = 0;
@ -681,7 +681,7 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)
get_remote_object_list(obj->oid.hash[0]);
if (obj->flags & (REMOTE | PUSHING))
return 0;
target = find_sha1_pack(obj->oid.hash, repo->packs);
target = find_oid_pack(&obj->oid, repo->packs);
if (target) {
obj->flags |= REMOTE;
return 0;

View File

@ -432,7 +432,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo,
if (fetch_indices(walker, repo))
return -1;
target = find_sha1_pack(oid->hash, repo->packs);
target = find_oid_pack(oid, repo->packs);
if (!target)
return -1;
close_pack_index(target);

View File

@ -2010,13 +2010,13 @@ int is_pack_valid(struct packed_git *p)
return !open_packed_git(p);
}
struct packed_git *find_sha1_pack(const unsigned char *sha1,
struct packed_git *packs)
struct packed_git *find_oid_pack(const struct object_id *oid,
struct packed_git *packs)
{
struct packed_git *p;
for (p = packs; p; p = p->next) {
if (find_pack_entry_one(sha1, p))
if (find_pack_entry_one(oid->hash, p))
return p;
}
return NULL;

View File

@ -79,8 +79,13 @@ struct packed_git *get_all_packs(struct repository *r);
*/
unsigned long repo_approximate_object_count(struct repository *r);
struct packed_git *find_sha1_pack(const unsigned char *sha1,
struct packed_git *packs);
/*
* Find the pack within the "packs" list whose index contains the object "oid".
* For general object lookups, you probably don't want this; use
* find_pack_entry() instead.
*/
struct packed_git *find_oid_pack(const struct object_id *oid,
struct packed_git *packs);
void pack_report(void);