diff mbox series

[18/20] packfile: pass down repository to `nth_packed_object_offset`

Message ID 973e8a560d48343e07a02bab88b31bfb5d3e903a.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:58 a.m. UTC
The function `nth_packed_object_offset` 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/cat-file.c     | 4 ++--
 builtin/pack-objects.c | 6 +++---
 commit-graph.c         | 2 +-
 midx-write.c           | 2 +-
 pack-check.c           | 2 +-
 pack-revindex.c        | 3 ++-
 packfile.c             | 8 +++++---
 packfile.h             | 4 +++-
 reachable.c            | 3 ++-
 9 files changed, 20 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index e5d774b097..8aab7481b6 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -632,14 +632,14 @@  static int batch_unordered_loose(const struct object_id *oid,
 	return batch_unordered_object(oid, NULL, 0, data);
 }
 
-static int batch_unordered_packed(struct repository *repo UNUSED,
+static int batch_unordered_packed(struct repository *repo,
 				  const struct object_id *oid,
 				  struct packed_git *pack,
 				  uint32_t pos,
 				  void *data)
 {
 	return batch_unordered_object(oid, pack,
-				      nth_packed_object_offset(pack, pos),
+				      nth_packed_object_offset(repo, pack, pos),
 				      data);
 }
 
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ffbd48c60c..8ab51eab8d 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3361,7 +3361,7 @@  static int git_pack_config(const char *k, const char *v,
 static int stdin_packs_found_nr;
 static int stdin_packs_hints_nr;
 
-static int add_object_entry_from_pack(struct repository *repo UNUSED,
+static int add_object_entry_from_pack(struct repository *repo,
 				      const struct object_id *oid,
 				      struct packed_git *p,
 				      uint32_t pos,
@@ -3375,7 +3375,7 @@  static int add_object_entry_from_pack(struct repository *repo UNUSED,
 	if (have_duplicate_entry(oid, 0))
 		return 0;
 
-	ofs = nth_packed_object_offset(p, pos);
+	ofs = nth_packed_object_offset(repo, p, pos);
 	if (!want_object_in_pack(oid, 0, &p, &ofs))
 		return 0;
 
@@ -3919,7 +3919,7 @@  static int add_object_in_unpacked_pack(struct repository *repo,
 		} else {
 			mtime = pack->mtime;
 		}
-		offset = nth_packed_object_offset(pack, pos);
+		offset = nth_packed_object_offset(repo, pack, pos);
 
 		add_cruft_object_entry(oid, OBJ_NONE, pack, offset,
 				       NULL, mtime);
diff --git a/commit-graph.c b/commit-graph.c
index 35e4e9d99d..24aae68195 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1495,7 +1495,7 @@  static int add_packed_commits(struct repository *repo UNUSED,
 {
 	struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
 	enum object_type type;
-	off_t offset = nth_packed_object_offset(pack, pos);
+	off_t offset = nth_packed_object_offset(ctx->r, pack, pos);
 	struct object_info oi = OBJECT_INFO_INIT;
 
 	if (ctx->progress)
diff --git a/midx-write.c b/midx-write.c
index 2b7c50d25c..69aada253f 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -233,7 +233,7 @@  static void fill_pack_entry(uint32_t pack_int_id,
 	entry->pack_int_id = pack_int_id;
 	entry->pack_mtime = p->mtime;
 
-	entry->offset = nth_packed_object_offset(p, cur_object);
+	entry->offset = nth_packed_object_offset(the_repository, p, cur_object);
 	entry->preferred = !!preferred;
 }
 
diff --git a/pack-check.c b/pack-check.c
index d93cf3f224..01562267a4 100644
--- a/pack-check.c
+++ b/pack-check.c
@@ -98,7 +98,7 @@  static int verify_packfile(struct repository *r,
 	entries[nr_objects].offset = pack_sig_ofs;
 	/* first sort entries by pack offset, since unpacking them is more efficient that way */
 	for (i = 0; i < nr_objects; i++) {
-		entries[i].offset = nth_packed_object_offset(p, i);
+		entries[i].offset = nth_packed_object_offset(r, p, i);
 		entries[i].nr = i;
 	}
 	QSORT(entries, nr_objects, compare_entries);
diff --git a/pack-revindex.c b/pack-revindex.c
index c14ef971da..03d8c39c94 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -466,7 +466,8 @@  off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos)
 	else if (pos == p->num_objects)
 		return p->pack_size - the_hash_algo->rawsz;
 	else
-		return nth_packed_object_offset(p, pack_pos_to_index(p, pos));
+		return nth_packed_object_offset(the_repository, p,
+						pack_pos_to_index(p, pos));
 }
 
 uint32_t pack_pos_to_midx(struct multi_pack_index *m, uint32_t pos)
diff --git a/packfile.c b/packfile.c
index e06931154f..1415df38e9 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1964,10 +1964,12 @@  void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
 		    p->pack_name);
 }
 
-off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
+off_t nth_packed_object_offset(struct repository *repo,
+			       const struct packed_git *p,
+			       uint32_t n)
 {
 	const unsigned char *index = p->index_data;
-	const unsigned int hashsz = the_hash_algo->rawsz;
+	const unsigned int hashsz = repo->hash_algo->rawsz;
 	index += 4 * 256;
 	if (p->index_version == 1) {
 		return ntohl(*((uint32_t *)(index + st_mult(hashsz + 4, n))));
@@ -1998,7 +2000,7 @@  off_t find_pack_entry_one(struct repository *repo, const unsigned char *sha1,
 
 	hashcpy(oid.hash, sha1, repo->hash_algo);
 	if (bsearch_pack(repo, &oid, p, &result))
-		return nth_packed_object_offset(p, result);
+		return nth_packed_object_offset(repo, p, result);
 	return 0;
 }
 
diff --git a/packfile.h b/packfile.h
index d145959480..9184560f0e 100644
--- a/packfile.h
+++ b/packfile.h
@@ -160,7 +160,9 @@  int nth_packed_object_id(struct repository *repo, struct object_id *,
  * Return the offset of the nth object within the specified packfile.
  * The index must already be opened.
  */
-off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
+off_t nth_packed_object_offset(struct repository *repo,
+			       const struct packed_git *,
+			       uint32_t n);
 
 /*
  * If the object named sha1 is present in the specified packfile,
diff --git a/reachable.c b/reachable.c
index 833013e7a3..e347e2d6ca 100644
--- a/reachable.c
+++ b/reachable.c
@@ -295,7 +295,8 @@  static int add_recent_packed(struct repository *repo,
 			die(_("could not load cruft pack .mtimes"));
 		mtime = nth_packed_mtime(p, pos);
 	}
-	add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
+	add_recent_object(oid, p, nth_packed_object_offset(repo, p, pos), mtime,
+			  data);
 	return 0;
 }