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 1/8] midx-write: pass down repository to static functions
Date: Wed, 27 Nov 2024 17:28:26 +0100	[thread overview]
Message-ID: <20241127-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v3-1-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 'midx-write.c' there are a lot of static functions which use global
variables `the_repository` or `the_hash_algo`. In a follow up commit,
the repository variable will be added to `write_midx_context`, which
some of the functions can use. But for functions which do not have
access to this struct, pass down the required information from
non-static functions `write_midx_file` and `write_midx_file_only`.

This requires that the function `hash_to_hex` is also replaced with
`hash_to_hex_algop` since the former internally accesses the
`the_hash_algo` global variable.

This ensures that the usage of global variables is limited to these
non-static functions, which will be cleaned up in a follow up commit.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 57 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index c57726ef9475df693890d61627ce91409c1def7c..22b5233f51ec6c6d99b8f9613818f1581dca5982 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -35,13 +35,13 @@ extern void clear_incremental_midx_files_ext(const char *object_dir,
 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 				const char *idx_name);
 
-static size_t write_midx_header(struct hashfile *f,
-				unsigned char num_chunks,
+static size_t write_midx_header(const struct git_hash_algo *hash_algo,
+				struct hashfile *f, unsigned char num_chunks,
 				uint32_t num_packs)
 {
 	hashwrite_be32(f, MIDX_SIGNATURE);
 	hashwrite_u8(f, MIDX_VERSION);
-	hashwrite_u8(f, oid_version(the_hash_algo));
+	hashwrite_u8(f, oid_version(hash_algo));
 	hashwrite_u8(f, num_chunks);
 	hashwrite_u8(f, 0); /* unused */
 	hashwrite_be32(f, num_packs);
@@ -702,7 +702,7 @@ static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
 		return 0;
 	}
 
-	if (!peel_iterated_oid(the_repository, oid, &peeled))
+	if (!peel_iterated_oid(revs->repo, oid, &peeled))
 		oid = &peeled;
 
 	object = parse_object_or_die(oid, refname);
@@ -827,7 +827,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 	return cb.commits;
 }
 
-static int write_midx_bitmap(const char *midx_name,
+static int write_midx_bitmap(struct repository *r, const char *midx_name,
 			     const unsigned char *midx_hash,
 			     struct packing_data *pdata,
 			     struct commit **commits,
@@ -840,9 +840,9 @@ static int write_midx_bitmap(const char *midx_name,
 	struct bitmap_writer writer;
 	struct pack_idx_entry **index;
 	char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
-					hash_to_hex(midx_hash));
+				    hash_to_hex_algop(midx_hash, r->hash_algo));
 
-	trace2_region_enter("midx", "write_midx_bitmap", the_repository);
+	trace2_region_enter("midx", "write_midx_bitmap", r);
 
 	if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
 		options |= BITMAP_OPT_HASH_CACHE;
@@ -859,7 +859,7 @@ static int write_midx_bitmap(const char *midx_name,
 	for (i = 0; i < pdata->nr_objects; i++)
 		index[i] = &pdata->objects[i].idx;
 
-	bitmap_writer_init(&writer, the_repository, pdata);
+	bitmap_writer_init(&writer, r, pdata);
 	bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
 	bitmap_writer_build_type_index(&writer, index);
 
@@ -892,7 +892,7 @@ static int write_midx_bitmap(const char *midx_name,
 	free(bitmap_name);
 	bitmap_writer_free(&writer);
 
-	trace2_region_leave("midx", "write_midx_bitmap", the_repository);
+	trace2_region_leave("midx", "write_midx_bitmap", r);
 
 	return ret;
 }
@@ -1049,7 +1049,7 @@ static void clear_midx_files(const char *object_dir,
 	strbuf_release(&buf);
 }
 
-static int write_midx_internal(const char *object_dir,
+static int write_midx_internal(struct repository *r, const char *object_dir,
 			       struct string_list *packs_to_include,
 			       struct string_list *packs_to_drop,
 			       const char *preferred_pack_name,
@@ -1070,7 +1070,8 @@ static int write_midx_internal(const char *object_dir,
 	const char **keep_hashes = NULL;
 	struct chunkfile *cf;
 
-	trace2_region_enter("midx", "write_midx_internal", the_repository);
+	trace2_region_enter("midx", "write_midx_internal", r);
+
 
 	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
 	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1087,8 +1088,7 @@ static int write_midx_internal(const char *object_dir,
 			  midx_name.buf);
 
 	if (!packs_to_include || ctx.incremental) {
-		struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
-								     object_dir);
+		struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
 		if (m && !midx_checksum_valid(m)) {
 			warning(_("ignoring existing multi-pack-index; checksum mismatch"));
 			m = NULL;
@@ -1351,7 +1351,7 @@ static int write_midx_internal(const char *object_dir,
 	add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
 		  write_midx_oid_fanout);
 	add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
-		  st_mult(ctx.entries_nr, the_hash_algo->rawsz),
+		  st_mult(ctx.entries_nr, r->hash_algo->rawsz),
 		  write_midx_oid_lookup);
 	add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
 		  st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
@@ -1373,7 +1373,8 @@ static int write_midx_internal(const char *object_dir,
 			  write_midx_bitmapped_packs);
 	}
 
-	write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
+	write_midx_header(r->hash_algo, f, get_num_chunks(cf),
+			  ctx.nr - dropped_packs);
 	write_chunkfile(cf, &ctx);
 
 	finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
@@ -1405,7 +1406,7 @@ static int write_midx_internal(const char *object_dir,
 		FREE_AND_NULL(ctx.entries);
 		ctx.entries_nr = 0;
 
-		if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
+		if (write_midx_bitmap(r, midx_name.buf, midx_hash, &pdata,
 				      commits, commits_nr, ctx.pack_order,
 				      flags) < 0) {
 			error(_("could not write multi-pack bitmap"));
@@ -1449,12 +1450,13 @@ static int write_midx_internal(const char *object_dir,
 		strbuf_release(&final_midx_name);
 
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 
 		for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
 			uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
 
-			keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
+			keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
+								   r->hash_algo));
 			m = m->base_midx;
 		}
 
@@ -1462,7 +1464,7 @@ static int write_midx_internal(const char *object_dir,
 			fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
 	} else {
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 	}
 
 	if (ctx.m || ctx.base_midx)
@@ -1495,7 +1497,7 @@ static int write_midx_internal(const char *object_dir,
 	}
 	strbuf_release(&midx_name);
 
-	trace2_region_leave("midx", "write_midx_internal", the_repository);
+	trace2_region_leave("midx", "write_midx_internal", r);
 
 	return result;
 }
@@ -1505,8 +1507,8 @@ int write_midx_file(const char *object_dir,
 		    const char *refs_snapshot,
 		    unsigned flags)
 {
-	return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
-				   refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int write_midx_file_only(const char *object_dir,
@@ -1515,8 +1517,9 @@ int write_midx_file_only(const char *object_dir,
 			 const char *refs_snapshot,
 			 unsigned flags)
 {
-	return write_midx_internal(object_dir, packs_to_include, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, packs_to_include,
+				   NULL, preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
@@ -1572,7 +1575,8 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
 	free(count);
 
 	if (packs_to_drop.nr)
-		result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
+		result = write_midx_internal(r, object_dir, NULL,
+					     &packs_to_drop, NULL, NULL, flags);
 
 	string_list_clear(&packs_to_drop, 0);
 
@@ -1769,7 +1773,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 		goto cleanup;
 	}
 
-	result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
+	result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL,
+				     flags);
 
 cleanup:
 	free(include_pack);

-- 
2.47.1


  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     ` Karthik Nayak [this message]
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     ` [PATCH v3 5/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
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-1-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).