]> git.ipfire.org Git - thirdparty/git.git/commitdiff
packfile: convert find_sha1_pack() to use object_id
authorJeff King <peff@peff.net>
Fri, 25 Oct 2024 07:05:03 +0000 (03:05 -0400)
committerTaylor Blau <me@ttaylorr.com>
Fri, 25 Oct 2024 21:35:46 +0000 (17:35 -0400)
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>
builtin/fast-import.c
http-push.c
http-walker.c
packfile.c
packfile.h

index 1e7ab67f6e5f131c2e75c5acfd2eccd6d45d4d54..76d5c20f141f42da988dddae0e549144d1379031 100644 (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! */
index aad89f2eab3b52765711f1f8a1852c79f877dcac..4d24e6b8d4e092591d5687be2bdb3840fa74893c 100644 (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;
index 36dd1f33c0fe8156bd2eec5023eef1bb00a5838e..43cde0ebe51c3fa311e9b42fb0608763e777b2f5 100644 (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);
index 0d4fd2737acd2c6ebf86b456d78699bd01f3c603..c51eab15a57dc9a83441079de460ebaf8ec5a3a2 100644 (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;
index ad393be9f1aad9a2e582856711508fd298a758fa..3baffa940cf2ab65b7c68e8c72a297bdf76a7ad7 100644 (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);