diff mbox series

[03/20] packfile: pass down repository to `close_one_pack`

Message ID 5160e79cbb67c0e7596bd0a48ecdedbf3e5236c9.1729504641.git.karthik.188@gmail.com (mailing list archive)
State Superseded
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 `close_one_pack` 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/pack-objects.c |  6 +++---
 midx.c                 |  2 +-
 pack-bitmap.c          |  4 ++--
 pack-check.c           |  2 +-
 packfile.c             | 24 ++++++++++++------------
 packfile.h             |  2 +-
 6 files changed, 20 insertions(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 4dd6ada184..ec321da8dc 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1482,7 +1482,7 @@  static int want_found_object(const struct object_id *oid, int exclude,
 	if (incremental)
 		return 0;
 
-	if (!is_pack_valid(p))
+	if (!is_pack_valid(the_repository, p))
 		return -1;
 
 	/*
@@ -1560,7 +1560,7 @@  static int want_object_in_pack_one(struct packed_git *p,
 
 	if (offset) {
 		if (!*found_pack) {
-			if (!is_pack_valid(p))
+			if (!is_pack_valid(the_repository, p))
 				return -1;
 			*found_offset = offset;
 			*found_pack = p;
@@ -3513,7 +3513,7 @@  static void read_packs_list_from_stdin(void)
 		struct packed_git *p = item->util;
 		if (!p)
 			die(_("could not find pack '%s'"), item->string);
-		if (!is_pack_valid(p))
+		if (!is_pack_valid(the_repository, p))
 			die(_("packfile %s cannot be accessed"), p->pack_name);
 	}
 
diff --git a/midx.c b/midx.c
index 67e0d64004..4a05f74606 100644
--- a/midx.c
+++ b/midx.c
@@ -597,7 +597,7 @@  int fill_midx_entry(struct repository *r,
 	* answer, as it may have been deleted since the MIDX was
 	* loaded!
 	*/
-	if (!is_pack_valid(p))
+	if (!is_pack_valid(r, p))
 		return 0;
 
 	if (oidset_size(&p->bad_objects) &&
diff --git a/pack-bitmap.c b/pack-bitmap.c
index 32b222a7af..067d1741d2 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -451,7 +451,7 @@  static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
 	}
 
 	preferred = bitmap_git->midx->packs[preferred_pack];
-	if (!is_pack_valid(preferred)) {
+	if (!is_pack_valid(the_repository, preferred)) {
 		warning(_("preferred pack (%s) is invalid"),
 			preferred->pack_name);
 		goto cleanup;
@@ -498,7 +498,7 @@  static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git
 		return -1;
 	}
 
-	if (!is_pack_valid(packfile)) {
+	if (!is_pack_valid(the_repository, packfile)) {
 		close(fd);
 		return -1;
 	}
diff --git a/pack-check.c b/pack-check.c
index e4636e9897..bb649edbc1 100644
--- a/pack-check.c
+++ b/pack-check.c
@@ -64,7 +64,7 @@  static int verify_packfile(struct repository *r,
 	int err = 0;
 	struct idx_entry *entries;
 
-	if (!is_pack_valid(p))
+	if (!is_pack_valid(the_repository, p))
 		return error("packfile %s cannot be accessed", p->pack_name);
 
 	r->hash_algo->init_fn(&ctx);
diff --git a/packfile.c b/packfile.c
index b0a3bfcd72..4588004223 100644
--- a/packfile.c
+++ b/packfile.c
@@ -462,13 +462,13 @@  static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struc
 	*accept_windows_inuse = has_windows_inuse;
 }
 
-static int close_one_pack(void)
+static int close_one_pack(struct repository *repo)
 {
 	struct packed_git *p, *lru_p = NULL;
 	struct pack_window *mru_w = NULL;
 	int accept_windows_inuse = 1;
 
-	for (p = the_repository->objects->packed_git; p; p = p->next) {
+	for (p = repo->objects->packed_git; p; p = p->next) {
 		if (p->pack_fd == -1)
 			continue;
 		find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
@@ -535,7 +535,7 @@  const char *pack_basename(struct packed_git *p)
  * Do not call this directly as this leaks p->pack_fd on error return;
  * call open_packed_git() instead.
  */
-static int open_packed_git_1(struct packed_git *p)
+static int open_packed_git_1(struct repository *repo, struct packed_git *p)
 {
 	struct stat st;
 	struct pack_header hdr;
@@ -557,7 +557,7 @@  static int open_packed_git_1(struct packed_git *p)
 			pack_max_fds = 1;
 	}
 
-	while (pack_max_fds <= pack_open_fds && close_one_pack())
+	while (pack_max_fds <= pack_open_fds && close_one_pack(repo))
 		; /* nothing */
 
 	p->pack_fd = git_open(p->pack_name);
@@ -599,14 +599,14 @@  static int open_packed_git_1(struct packed_git *p)
 	if (read_result != hashsz)
 		return error("packfile %s signature is unavailable", p->pack_name);
 	idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2;
-	if (!hasheq(hash, idx_hash, the_repository->hash_algo))
+	if (!hasheq(hash, idx_hash, repo->hash_algo))
 		return error("packfile %s does not match index", p->pack_name);
 	return 0;
 }
 
-static int open_packed_git(struct packed_git *p)
+static int open_packed_git(struct repository *repo, struct packed_git *p)
 {
-	if (!open_packed_git_1(p))
+	if (!open_packed_git_1(repo, p))
 		return 0;
 	close_pack_fd(p);
 	return -1;
@@ -636,7 +636,7 @@  unsigned char *use_pack(struct repository *repo, struct packed_git *p,
 	 * hash, and the in_window function above wouldn't match
 	 * don't allow an offset too close to the end of the file.
 	 */
-	if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
+	if (!p->pack_size && p->pack_fd == -1 && open_packed_git(repo, p))
 		die("packfile %s cannot be accessed", p->pack_name);
 	if (offset > (p->pack_size - the_hash_algo->rawsz))
 		die("offset beyond end of packfile (truncated pack?)");
@@ -654,7 +654,7 @@  unsigned char *use_pack(struct repository *repo, struct packed_git *p,
 			size_t window_align = packed_git_window_size / 2;
 			off_t len;
 
-			if (p->pack_fd == -1 && open_packed_git(p))
+			if (p->pack_fd == -1 && open_packed_git(repo, p))
 				die("packfile %s cannot be accessed", p->pack_name);
 
 			CALLOC_ARRAY(win, 1);
@@ -1994,7 +1994,7 @@  off_t find_pack_entry_one(const unsigned char *sha1,
 	return 0;
 }
 
-int is_pack_valid(struct packed_git *p)
+int is_pack_valid(struct repository *repo, struct packed_git *p)
 {
 	/* An already open pack is known to be valid. */
 	if (p->pack_fd != -1)
@@ -2012,7 +2012,7 @@  int is_pack_valid(struct packed_git *p)
 	}
 
 	/* Force the pack to open to prove its valid. */
-	return !open_packed_git(p);
+	return !open_packed_git(repo, p);
 }
 
 struct packed_git *find_sha1_pack(const unsigned char *sha1,
@@ -2049,7 +2049,7 @@  static int fill_pack_entry(const struct object_id *oid,
 	 * answer, as it may have been deleted since the index was
 	 * loaded!
 	 */
-	if (!is_pack_valid(p))
+	if (!is_pack_valid(the_repository, p))
 		return 0;
 	e->offset = offset;
 	e->p = p;
diff --git a/packfile.h b/packfile.h
index 90a1f2e1cf..b74d649c23 100644
--- a/packfile.h
+++ b/packfile.h
@@ -163,7 +163,7 @@  off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
  */
 off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
 
-int is_pack_valid(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 *);
 unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
 unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);