diff mbox series

[v10,09/12] builtin/merge-index.c: don't USE_THE_INDEX_VARIABLE

Message ID patch-v10-09.12-fc9a05ee034-20221215T084803Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series merge-index: prepare to rewrite merge drivers in C | expand

Commit Message

Ævar Arnfjörð Bjarmason Dec. 15, 2022, 8:52 a.m. UTC
Remove "USE_THE_INDEX_VARIABLE" and instead pass "the_index" around
between the functions in this file. In a subsequent commit we'll
libify this, and don't want to use "USE_THE_INDEX_VARIABLE" in any
more places in the top-level *.c files. Doing this first makes that
diff mbox series

Patch

diff a lot smaller.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/merge-index.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index cd160779cbf..c269d76cc8f 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -1,4 +1,3 @@ 
-#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "parse-options.h"
 #include "run-command.h"
@@ -7,7 +6,7 @@  static const char *pgm;
 static int one_shot, quiet;
 static int err;
 
-static int merge_entry(int pos, const char *path)
+static int merge_entry(struct index_state *istate, int pos, const char *path)
 {
 	int found;
 	const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
@@ -15,11 +14,11 @@  static int merge_entry(int pos, const char *path)
 	char ownbuf[4][60];
 	struct child_process cmd = CHILD_PROCESS_INIT;
 
-	if (pos >= the_index.cache_nr)
+	if (pos >= istate->cache_nr)
 		die(_("'%s' is not in the cache"), path);
 	found = 0;
 	do {
-		const struct cache_entry *ce = the_index.cache[pos];
+		const struct cache_entry *ce = istate->cache[pos];
 		int stage = ce_stage(ce);
 
 		if (strcmp(ce->name, path))
@@ -29,7 +28,7 @@  static int merge_entry(int pos, const char *path)
 		xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
 		arguments[stage] = hexbuf[stage];
 		arguments[stage + 4] = ownbuf[stage];
-	} while (++pos < the_index.cache_nr);
+	} while (++pos < istate->cache_nr);
 	if (!found)
 		die(_("'%s' is not in the cache"), path);
 
@@ -46,27 +45,27 @@  static int merge_entry(int pos, const char *path)
 	return found;
 }
 
-static void merge_one_path(const char *path)
+static void merge_one_path(struct index_state *istate, const char *path)
 {
-	int pos = index_name_pos(&the_index, path, strlen(path));
+	int pos = index_name_pos(istate, path, strlen(path));
 
 	/*
 	 * If it already exists in the cache as stage0, it's
 	 * already merged and there is nothing to do.
 	 */
 	if (pos < 0)
-		merge_entry(-pos-1, path);
+		merge_entry(istate, -pos-1, path);
 }
 
-static void merge_all(void)
+static void merge_all(struct index_state *istate)
 {
 	int i;
 
-	for (i = 0; i < the_index.cache_nr; i++) {
-		const struct cache_entry *ce = the_index.cache[i];
+	for (i = 0; i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
 		if (!ce_stage(ce))
 			continue;
-		i += merge_entry(i, ce->name)-1;
+		i += merge_entry(istate, i, ce->name)-1;
 	}
 }
 
@@ -119,13 +118,13 @@  int cmd_merge_index(int argc, const char **argv, const char *prefix)
 	repo_read_index(the_repository);
 
 	/* TODO: audit for interaction with sparse-index. */
-	ensure_full_index(&the_index);
+	ensure_full_index(the_repository->index);
 
 	if (all)
-		merge_all();
+		merge_all(the_repository->index);
 	else
 		for (size_t i = 0; i < argc; i++)
-			merge_one_path(argv[i]);
+			merge_one_path(the_repository->index, argv[i]);
 
 	if (err && !quiet)
 		die(_("merge program failed"));