git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Karthik Nayak <karthik.188@gmail.com>
To: git@vger.kernel.org
Cc: Karthik Nayak <karthik.188@gmail.com>,
	me@ttaylorr.com,  shejialuo@gmail.com, gitster@pobox.com,
	 Christian Couder <chriscool@tuxfamily.org>
Subject: [PATCH v3 5/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo`
Date: Wed, 27 Nov 2024 17:28:30 +0100	[thread overview]
Message-ID: <20241127-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v3-5-c5a99f85009b@gmail.com> (raw)
In-Reply-To: <20241127-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v3-0-c5a99f85009b@gmail.com>

In the `midx.c` file, there are multiple usages of `the_repository` and
`the_hash_algo` within static functions of the file. Some of the usages
can be simply swapped out with the available `repository` struct. While
some of them can be swapped out by passing the repository to the
required functions.

This leaves out only some other usages of `the_repository` and
`the_hash_algo` in the file in non-static functions, which we'll tackle
in upcoming commits.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/midx.c b/midx.c
index 079c45a1aafb658a7b887ac216cc6ecf5f0fb6ff..6f0fb8285af14843da132ef1b0c3a8bdd06732c9 100644
--- a/midx.c
+++ b/midx.c
@@ -25,7 +25,7 @@ int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 
 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
 {
-	return m->data + m->data_len - the_hash_algo->rawsz;
+	return m->data + m->data_len - m->repo->hash_algo->rawsz;
 }
 
 void get_midx_filename(struct strbuf *out, const char *object_dir)
@@ -94,7 +94,8 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 
 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
 
-static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
+							  const char *object_dir,
 							  const char *midx_name,
 							  int local)
 {
@@ -131,7 +132,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 	m->data = midx_map;
 	m->data_len = midx_size;
 	m->local = local;
-	m->repo = the_repository;
+	m->repo = r;
 
 	m->signature = get_be32(m->data);
 	if (m->signature != MIDX_SIGNATURE)
@@ -144,12 +145,12 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 		      m->version);
 
 	hash_version = m->data[MIDX_BYTE_HASH_VERSION];
-	if (hash_version != oid_version(the_hash_algo)) {
+	if (hash_version != oid_version(r->hash_algo)) {
 		error(_("multi-pack-index hash version %u does not match version %u"),
-		      hash_version, oid_version(the_hash_algo));
+		      hash_version, oid_version(r->hash_algo));
 		goto cleanup_fail;
 	}
-	m->hash_len = the_hash_algo->rawsz;
+	m->hash_len = r->hash_algo->rawsz;
 
 	m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
 
@@ -206,8 +207,8 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 			      m->pack_names[i]);
 	}
 
-	trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
-	trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
+	trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
+	trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
 
 	free_chunkfile(cf);
 	return m;
@@ -240,8 +241,9 @@ void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
 }
 
-static int open_multi_pack_index_chain(const char *chain_file,
-				       int *fd, struct stat *st)
+static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
+				       const char *chain_file, int *fd,
+				       struct stat *st)
 {
 	*fd = git_open(chain_file);
 	if (*fd < 0)
@@ -250,7 +252,7 @@ static int open_multi_pack_index_chain(const char *chain_file,
 		close(*fd);
 		return 0;
 	}
-	if (st->st_size < the_hash_algo->hexsz) {
+	if (st->st_size < hash_algo->hexsz) {
 		close(*fd);
 		if (!st->st_size) {
 			/* treat empty files the same as missing */
@@ -292,7 +294,8 @@ static int add_midx_to_chain(struct multi_pack_index *midx,
 	return 1;
 }
 
-static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
+static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
+						      const char *object_dir,
 						      int local,
 						      int fd, struct stat *st,
 						      int *incomplete_chain)
@@ -303,7 +306,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	uint32_t i, count;
 	FILE *fp = xfdopen(fd, "r");
 
-	count = st->st_size / (the_hash_algo->hexsz + 1);
+	count = st->st_size / (r->hash_algo->hexsz + 1);
 
 	for (i = 0; i < count; i++) {
 		struct multi_pack_index *m;
@@ -312,7 +315,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		if (strbuf_getline_lf(&buf, fp) == EOF)
 			break;
 
-		if (get_oid_hex(buf.buf, &layer)) {
+		if (get_oid_hex_algop(buf.buf, &layer, r->hash_algo)) {
 			warning(_("invalid multi-pack-index chain: line '%s' "
 				  "not a hash"),
 				buf.buf);
@@ -325,7 +328,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		strbuf_reset(&buf);
 		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
 					    MIDX_EXT_MIDX);
-		m = load_multi_pack_index_one(object_dir, buf.buf, local);
+		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
 			if (add_midx_to_chain(m, midx_chain)) {
@@ -348,7 +351,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	return midx_chain;
 }
 
-static struct multi_pack_index *load_multi_pack_index_chain(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r,
+							    const char *object_dir,
 							    int local)
 {
 	struct strbuf chain_file = STRBUF_INIT;
@@ -357,10 +361,10 @@ static struct multi_pack_index *load_multi_pack_index_chain(const char *object_d
 	struct multi_pack_index *m = NULL;
 
 	get_midx_chain_filename(&chain_file, object_dir);
-	if (open_multi_pack_index_chain(chain_file.buf, &fd, &st)) {
+	if (open_multi_pack_index_chain(r->hash_algo, chain_file.buf, &fd, &st)) {
 		int incomplete;
 		/* ownership of fd is taken over by load function */
-		m = load_midx_chain_fd_st(object_dir, local, fd, &st,
+		m = load_midx_chain_fd_st(r, object_dir, local, fd, &st,
 					  &incomplete);
 	}
 
@@ -376,9 +380,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(object_dir, midx_name.buf, local);
+	m = load_multi_pack_index_one(the_repository, object_dir,
+				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(object_dir, local);
+		m = load_multi_pack_index_chain(the_repository, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -520,7 +525,7 @@ int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
 		     uint32_t *result)
 {
 	int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
-			       m->chunk_oid_lookup, the_hash_algo->rawsz,
+			       m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
 			       result);
 	if (result)
 		*result += m->num_objects_in_base;
@@ -551,7 +556,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
 	n = midx_for_object(&m, n);
 
 	oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
-		the_repository->hash_algo);
+		m->repo->hash_algo);
 	return oid;
 }
 

-- 
2.47.1


  parent reply	other threads:[~2024-11-27 16:28 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
2024-11-15 13:42 ` [PATCH 1/8] builtin: pass repository to sub commands Karthik Nayak
2024-11-16 12:49   ` shejialuo
2024-11-18  8:05     ` Patrick Steinhardt
2024-11-18 15:17     ` karthik nayak
2024-11-15 13:42 ` [PATCH 2/8] midx-write: add repository field to `write_midx_context` Karthik Nayak
2024-11-15 23:05   ` brian m. carlson
2024-11-18 15:17     ` karthik nayak
2024-11-18  8:05   ` Patrick Steinhardt
2024-11-18 15:37     ` karthik nayak
2024-11-15 13:42 ` [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
2024-11-18  8:05   ` Patrick Steinhardt
2024-11-15 13:42 ` [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
2024-11-18  8:06   ` Patrick Steinhardt
2024-11-18 16:16     ` karthik nayak
2024-11-15 13:42 ` [PATCH 5/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
2024-11-15 13:42 ` [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
2024-11-16 13:16   ` shejialuo
2024-11-18 16:25     ` karthik nayak
2024-11-19 12:12       ` shejialuo
2024-11-15 13:42 ` [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
2024-11-16 13:20   ` shejialuo
2024-11-15 13:42 ` [PATCH 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
2024-11-15 14:13 ` [PATCH 0/8] Change midx.c and midx-write.c to not use global variables karthik nayak
2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 01/10] builtin: pass repository to sub commands Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 02/10] midx-write: pass down repository to static functions Karthik Nayak
2024-11-20 18:15     ` Christian Couder
2024-11-20 19:41       ` Taylor Blau
2024-11-21 14:57       ` karthik nayak
2024-11-20 19:43     ` Taylor Blau
2024-11-21 15:06       ` karthik nayak
2024-11-19 15:36   ` [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
2024-11-20 12:58     ` shejialuo
2024-11-20 14:26       ` Richard Kerry
2024-11-20 19:46         ` Taylor Blau
2024-11-21 15:30           ` karthik nayak
2024-11-20 19:44     ` Taylor Blau
2024-11-19 15:36   ` [PATCH v2 04/10] write-midx: add repository field to `write_midx_context` Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
2024-11-20 20:11     ` Taylor Blau
2024-11-19 15:36   ` [PATCH v2 06/10] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 07/10] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
2024-11-20 18:15     ` Christian Couder
2024-11-21 15:35       ` karthik nayak
2024-11-19 15:36   ` [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
2024-11-20 18:15     ` Christian Couder
2024-11-20 22:23       ` Taylor Blau
2024-11-21 15:41         ` karthik nayak
2024-11-19 15:36   ` [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
2024-11-20 13:13     ` shejialuo
2024-11-21 15:41       ` karthik nayak
2024-11-20 18:20   ` [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables Christian Couder
2024-11-20 22:24     ` Taylor Blau
2024-11-21  0:09       ` Junio C Hamano
2024-11-21  2:19   ` Junio C Hamano
2024-11-22 10:25     ` karthik nayak
2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 1/8] midx-write: pass down repository to static functions Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 2/8] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 3/8] write-midx: add repository field to `write_midx_context` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 4/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
2024-11-27 16:28     ` Karthik Nayak [this message]
2024-11-27 16:28     ` [PATCH v3 6/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 7/8] midx: pass down `hash_algo` to functions using global variables Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
2024-11-28  1:27     ` [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables Junio C Hamano
2024-12-03  9:43       ` Patrick Steinhardt
2024-12-03 23:06         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241127-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v3-5-c5a99f85009b@gmail.com \
    --to=karthik.188@gmail.com \
    --cc=chriscool@tuxfamily.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    --cc=shejialuo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).