diff mbox series

[10/20] packfile: pass down repository to `find_pack_entry_one`

Message ID 7d580c72ce97a822bacb082fa05b1e7e41bfc011.1729504641.git.karthik.188@gmail.com (mailing list archive)
State New
Headers show
Series packfile: avoid using the 'the_repository' global variable | expand

Commit Message

karthik nayak Oct. 21, 2024, 9:57 a.m. UTC
The function `find_pack_entry_one` currently relies on the global
variable `the_repository`. To eliminate global variable usage in
`packfile.c`, we should progressively shift the dependency on
the_repository to higher layers. Let's remove its usage from this
function and any related ones.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/fast-import.c     |  4 ++--
 builtin/pack-objects.c    |  4 ++--
 connected.c               |  5 +++--
 http-push.c               |  5 +++--
 http-walker.c             |  2 +-
 midx.c                    |  2 +-
 pack-bitmap.c             |  6 ++++--
 packfile.c                | 15 ++++++++-------
 packfile.h                |  6 ++++--
 t/helper/test-find-pack.c |  2 +-
 10 files changed, 29 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 51d1cc0deb..a6743db85c 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -966,7 +966,7 @@  static int store_object(
 	if (e->idx.offset) {
 		duplicate_count_by_type[type]++;
 		return 1;
-	} else if (find_sha1_pack(oid.hash,
+	} else if (find_sha1_pack(the_repository, oid.hash,
 				  get_all_packs(the_repository))) {
 		e->type = type;
 		e->pack_id = MAX_PACK_ID;
@@ -1167,7 +1167,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,
+	} else if (find_sha1_pack(the_repository, oid.hash,
 				  get_all_packs(the_repository))) {
 		e->type = OBJ_BLOB;
 		e->pack_id = MAX_PACK_ID;
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index adf55d892f..d41259a423 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1558,7 +1558,7 @@  static int want_object_in_pack_one(struct packed_git *p,
 	if (p == *found_pack)
 		offset = *found_offset;
 	else
-		offset = find_pack_entry_one(oid->hash, p);
+		offset = find_pack_entry_one(the_repository, oid->hash, p);
 
 	if (offset) {
 		if (!*found_pack) {
@@ -3986,7 +3986,7 @@  static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
 	while (p) {
 		if ((!p->pack_local || p->pack_keep ||
 				p->pack_keep_in_core) &&
-			find_pack_entry_one(oid->hash, p)) {
+			find_pack_entry_one(the_repository, oid->hash, p)) {
 			last_found = p;
 			return 1;
 		}
diff --git a/connected.c b/connected.c
index 235890efd0..00b7de34c6 100644
--- a/connected.c
+++ b/connected.c
@@ -78,7 +78,7 @@  int check_connected(oid_iterate_fn fn, void *cb_data,
 			for (p = get_all_packs(the_repository); p; p = p->next) {
 				if (!p->pack_promisor)
 					continue;
-				if (find_pack_entry_one(oid->hash, p))
+				if (find_pack_entry_one(the_repository, oid->hash, p))
 					goto promisor_pack_found;
 			}
 			/*
@@ -144,7 +144,8 @@  int check_connected(oid_iterate_fn fn, void *cb_data,
 		 * are sure the ref is good and not sending it to
 		 * rev-list for verification.
 		 */
-		if (new_pack && find_pack_entry_one(oid->hash, new_pack))
+		if (new_pack && find_pack_entry_one(the_repository, oid->hash,
+						    new_pack))
 			continue;
 
 		if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
diff --git a/http-push.c b/http-push.c
index aad89f2eab..cb6cf1696e 100644
--- a/http-push.c
+++ b/http-push.c
@@ -309,7 +309,8 @@  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_sha1_pack(the_repository, request->obj->oid.hash,
+				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 +682,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_sha1_pack(the_repository, obj->oid.hash, repo->packs);
 	if (target) {
 		obj->flags |= REMOTE;
 		return 0;
diff --git a/http-walker.c b/http-walker.c
index fb2d86d5e7..0a11ed6ecf 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -431,7 +431,7 @@  static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
 
 	if (fetch_indices(walker, repo))
 		return -1;
-	target = find_sha1_pack(sha1, repo->packs);
+	target = find_sha1_pack(the_repository, sha1, repo->packs);
 	if (!target)
 		return -1;
 	close_pack_index(target);
diff --git a/midx.c b/midx.c
index 94609456a2..c76df95d6d 100644
--- a/midx.c
+++ b/midx.c
@@ -973,7 +973,7 @@  int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
 		}
 
 		m_offset = e.offset;
-		p_offset = find_pack_entry_one(oid.hash, e.p);
+		p_offset = find_pack_entry_one(r, oid.hash, e.p);
 
 		if (m_offset != p_offset)
 			midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
diff --git a/pack-bitmap.c b/pack-bitmap.c
index 96716c785b..b699875555 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -935,7 +935,8 @@  static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
 					   const struct object_id *oid)
 {
 	uint32_t pos;
-	off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
+	off_t offset = find_pack_entry_one(the_repository, oid->hash,
+					   bitmap_git->pack);
 	if (!offset)
 		return -1;
 
@@ -1609,7 +1610,8 @@  static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
 			if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
 				return 1;
 		} else {
-			if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
+			if (find_pack_entry_one(the_repository, object->oid.hash,
+						bitmap_git->pack) > 0)
 				return 1;
 		}
 	}
diff --git a/packfile.c b/packfile.c
index 92c919d628..bf70fd60a8 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1240,7 +1240,7 @@  off_t get_delta_base(struct repository *repo, struct packed_git *p,
 		*curpos += used;
 	} else if (type == OBJ_REF_DELTA) {
 		/* The base entry _must_ be in the same pack */
-		base_offset = find_pack_entry_one(base_info, p);
+		base_offset = find_pack_entry_one(repo, base_info, p);
 		*curpos += repo->hash_algo->rawsz;
 	} else
 		die("I am totally screwed");
@@ -1975,8 +1975,8 @@  off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
 	}
 }
 
-off_t find_pack_entry_one(const unsigned char *sha1,
-				  struct packed_git *p)
+off_t find_pack_entry_one(struct repository *repo, const unsigned char *sha1,
+			  struct packed_git *p)
 {
 	const unsigned char *index = p->index_data;
 	struct object_id oid;
@@ -1987,7 +1987,7 @@  off_t find_pack_entry_one(const unsigned char *sha1,
 			return 0;
 	}
 
-	hashcpy(oid.hash, sha1, the_repository->hash_algo);
+	hashcpy(oid.hash, sha1, repo->hash_algo);
 	if (bsearch_pack(&oid, p, &result))
 		return nth_packed_object_offset(p, result);
 	return 0;
@@ -2014,13 +2014,14 @@  int is_pack_valid(struct repository *repo, struct packed_git *p)
 	return !open_packed_git(repo, p);
 }
 
-struct packed_git *find_sha1_pack(const unsigned char *sha1,
+struct packed_git *find_sha1_pack(struct repository *repo,
+				  const unsigned char *sha1,
 				  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(repo, sha1, p))
 			return p;
 	}
 	return NULL;
@@ -2037,7 +2038,7 @@  static int fill_pack_entry(const struct object_id *oid,
 	    oidset_contains(&p->bad_objects, oid))
 		return 0;
 
-	offset = find_pack_entry_one(oid->hash, p);
+	offset = find_pack_entry_one(the_repository, oid->hash, p);
 	if (!offset)
 		return 0;
 
diff --git a/packfile.h b/packfile.h
index f744af6e9b..983d6df385 100644
--- a/packfile.h
+++ b/packfile.h
@@ -87,7 +87,8 @@  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 *find_sha1_pack(struct repository *repo,
+				  const unsigned char *sha1,
 				  struct packed_git *packs);
 
 void pack_report(void);
@@ -163,7 +164,8 @@  off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
  * If the object named sha1 is present in the specified packfile,
  * return its offset within the packfile; otherwise, return 0.
  */
-off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
+off_t find_pack_entry_one(struct repository *repo, const unsigned char *sha1,
+			  struct packed_git *);
 
 int is_pack_valid(struct repository *repo, struct packed_git *);
 void *unpack_entry(struct repository *r, struct packed_git *, off_t, enum object_type *, unsigned long *);
diff --git a/t/helper/test-find-pack.c b/t/helper/test-find-pack.c
index 14b2b0c12c..c5cdea98f6 100644
--- a/t/helper/test-find-pack.c
+++ b/t/helper/test-find-pack.c
@@ -40,7 +40,7 @@  int cmd__find_pack(int argc, const char **argv)
 		die("cannot parse %s as an object name", argv[0]);
 
 	for (p = get_all_packs(the_repository); p; p = p->next)
-		if (find_pack_entry_one(oid.hash, p)) {
+		if (find_pack_entry_one(the_repository, oid.hash, p)) {
 			printf("%s\n", p->pack_name);
 			actual_count++;
 		}