packfile: use `repository` from `packed_git` directly

In the previous commit, we introduced the `repository` structure inside `packed_git`. This provides an alternative route instead of using the global `the_repository` variable. Let's modify `packfile.c` now to use this field wherever possible instead of relying on the global state. There are still a few instances of `the_repository` usage in the file, where there is no struct `packed_git` locally available, which will be fixed in the following commits. Helped-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Dec 3, 2024 at 15:43 UTC 9c5ce06d74251601ca0dcb3ebe2284639f96f9a2
1 file changed +27 -23
packfile.c
+27 -23
@@ -79,7 +79,7 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
79 size_t idx_size;
80 int fd = git_open(path), ret;
81 struct stat st;
82 - const unsigned int hashsz = the_hash_algo->rawsz;
82 + const unsigned int hashsz = p->repo->hash_algo->rawsz;
83
84 if (fd < 0)
85 return -1;
@@ -243,7 +243,7 @@ struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
243
244 memcpy(p->pack_name, path, alloc); /* includes NUL */
245 free(path);
246 - hashcpy(p->hash, sha1, the_repository->hash_algo);
246 + hashcpy(p->hash, sha1, p->repo->hash_algo);
247 if (check_packed_git_idx(idx_path, p)) {
248 free(p);
249 return NULL;
@@ -278,7 +278,7 @@ static int unuse_one_window(struct packed_git *current)
278
279 if (current)
280 scan_windows(current, &lru_p, &lru_w, &lru_l);
281 - for (p = the_repository->objects->packed_git; p; p = p->next)
281 + for (p = current->repo->objects->packed_git; p; p = p->next)
282 scan_windows(p, &lru_p, &lru_w, &lru_l);
283 if (lru_p) {
284 munmap(lru_w->base, lru_w->len);
@@ -540,7 +540,7 @@ static int open_packed_git_1(struct packed_git *p)
540 unsigned char hash[GIT_MAX_RAWSZ];
541 unsigned char *idx_hash;
542 ssize_t read_result;
543 - const unsigned hashsz = the_hash_algo->rawsz;
543 + const unsigned hashsz = p->repo->hash_algo->rawsz;
544
545 if (open_pack_index(p))
546 return error("packfile %s index unavailable", p->pack_name);
@@ -597,7 +597,7 @@ static int open_packed_git_1(struct packed_git *p)
597 if (read_result != hashsz)
598 return error("packfile %s signature is unavailable", p->pack_name);
599 idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2;
600 - if (!hasheq(hash, idx_hash, the_repository->hash_algo))
600 + if (!hasheq(hash, idx_hash, p->repo->hash_algo))
601 return error("packfile %s does not match index", p->pack_name);
602 return 0;
603 }
@@ -637,7 +637,7 @@ unsigned char *use_pack(struct packed_git *p,
637 */
638 if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
639 die("packfile %s cannot be accessed", p->pack_name);
640 - if (offset > (p->pack_size - the_hash_algo->rawsz))
640 + if (offset > (p->pack_size - p->repo->hash_algo->rawsz))
641 die("offset beyond end of packfile (truncated pack?)");
642 if (offset < 0)
643 die(_("offset before end of packfile (broken .idx?)"));
@@ -711,6 +711,7 @@ struct packed_git *add_packed_git(struct repository *r, const char *path,
711 struct stat st;
712 size_t alloc;
713 struct packed_git *p;
714 + struct object_id oid;
715
716 /*
717 * Make sure a corresponding .pack file exists and that
@@ -751,9 +752,13 @@ struct packed_git *add_packed_git(struct repository *r, const char *path,
752 p->pack_size = st.st_size;
753 p->pack_local = local;
754 p->mtime = st.st_mtime;
754 - if (path_len < the_hash_algo->hexsz ||
755 - get_hash_hex(path + path_len - the_hash_algo->hexsz, p->hash))
756 - hashclr(p->hash, the_repository->hash_algo);
755 + if (path_len < r->hash_algo->hexsz ||
756 + get_oid_hex_algop(path + path_len - r->hash_algo->hexsz, &oid,
757 + r->hash_algo))
758 + hashclr(p->hash, r->hash_algo);
759 + else
760 + hashcpy(p->hash, oid.hash, r->hash_algo);
761 +
762 return p;
763 }
764
@@ -1243,9 +1248,9 @@ off_t get_delta_base(struct packed_git *p,
1248 } else if (type == OBJ_REF_DELTA) {
1249 /* The base entry _must_ be in the same pack */
1250 struct object_id oid;
1246 - oidread(&oid, base_info, the_repository->hash_algo);
1251 + oidread(&oid, base_info, p->repo->hash_algo);
1252 base_offset = find_pack_entry_one(&oid, p);
1248 - *curpos += the_hash_algo->rawsz;
1253 + *curpos += p->repo->hash_algo->rawsz;
1254 } else
1255 die("I am totally screwed");
1256 return base_offset;
@@ -1266,7 +1271,7 @@ static int get_delta_base_oid(struct packed_git *p,
1271 {
1272 if (type == OBJ_REF_DELTA) {
1273 unsigned char *base = use_pack(p, w_curs, curpos, NULL);
1269 - oidread(oid, base, the_repository->hash_algo);
1274 + oidread(oid, base, p->repo->hash_algo);
1275 return 0;
1276 } else if (type == OBJ_OFS_DELTA) {
1277 uint32_t base_pos;
@@ -1608,7 +1613,7 @@ int packed_object_info(struct repository *r, struct packed_git *p,
1613 goto out;
1614 }
1615 } else
1611 - oidclr(oi->delta_base_oid, the_repository->hash_algo);
1616 + oidclr(oi->delta_base_oid, p->repo->hash_algo);
1617 }
1618
1619 oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
@@ -1897,7 +1902,7 @@ int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32
1902 {
1903 const unsigned char *index_fanout = p->index_data;
1904 const unsigned char *index_lookup;
1900 - const unsigned int hashsz = the_hash_algo->rawsz;
1905 + const unsigned int hashsz = p->repo->hash_algo->rawsz;
1906 int index_lookup_width;
1907
1908 if (!index_fanout)
@@ -1922,7 +1927,7 @@ int nth_packed_object_id(struct object_id *oid,
1927 uint32_t n)
1928 {
1929 const unsigned char *index = p->index_data;
1925 - const unsigned int hashsz = the_hash_algo->rawsz;
1930 + const unsigned int hashsz = p->repo->hash_algo->rawsz;
1931 if (!index) {
1932 if (open_pack_index(p))
1933 return -1;
@@ -1933,11 +1938,10 @@ int nth_packed_object_id(struct object_id *oid,
1938 index += 4 * 256;
1939 if (p->index_version == 1) {
1940 oidread(oid, index + st_add(st_mult(hashsz + 4, n), 4),
1936 - the_repository->hash_algo);
1941 + p->repo->hash_algo);
1942 } else {
1943 index += 8;
1939 - oidread(oid, index + st_mult(hashsz, n),
1940 - the_repository->hash_algo);
1944 + oidread(oid, index + st_mult(hashsz, n), p->repo->hash_algo);
1945 }
1946 return 0;
1947 }
@@ -1959,7 +1963,7 @@ void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1963 off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1964 {
1965 const unsigned char *index = p->index_data;
1962 - const unsigned int hashsz = the_hash_algo->rawsz;
1966 + const unsigned int hashsz = p->repo->hash_algo->rawsz;
1967 index += 4 * 256;
1968 if (p->index_version == 1) {
1969 return ntohl(*((uint32_t *)(index + st_mult(hashsz + 4, n))));
@@ -2159,7 +2163,7 @@ int for_each_object_in_pack(struct packed_git *p,
2163 int r = 0;
2164
2165 if (flags & FOR_EACH_OBJECT_PACK_ORDER) {
2162 - if (load_pack_revindex(the_repository, p))
2166 + if (load_pack_revindex(p->repo, p))
2167 return -1;
2168 }
2169
@@ -2227,7 +2231,7 @@ int for_each_packed_object(each_packed_object_fn cb, void *data,
2231 }
2232
2233 static int add_promisor_object(const struct object_id *oid,
2230 - struct packed_git *pack UNUSED,
2234 + struct packed_git *pack,
2235 uint32_t pos UNUSED,
2236 void *set_)
2237 {
@@ -2235,12 +2239,12 @@ static int add_promisor_object(const struct object_id *oid,
2239 struct object *obj;
2240 int we_parsed_object;
2241
2238 - obj = lookup_object(the_repository, oid);
2242 + obj = lookup_object(pack->repo, oid);
2243 if (obj && obj->parsed) {
2244 we_parsed_object = 0;
2245 } else {
2246 we_parsed_object = 1;
2243 - obj = parse_object(the_repository, oid);
2247 + obj = parse_object(pack->repo, oid);
2248 }
2249
2250 if (!obj)