git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS
@ 2022-11-18 11:30 Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 01/12] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
                   ` (13 more replies)
  0 siblings, 14 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:30 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

As 3/12 here notes we've been undergoing a sloooow migration away from
functions that provide "the_index" for you since 2007, with the last
major move away from some of them being in 2019.

This series has a large diffstat, but should be disproportionately
easy to review as I wrote almost none of the code: coccinelle did :)

I.e. after some trivial cleanup in 1-2/12 the C code changes in 3/12
are mostly generated from a new index-compatibility.cocci migration.

The commits that deviate from that are:

 * 04/12: Make a function that always returns "0" return "void"
   instead.

 * 09-10/12: Add a "USE_THE_INDEX_VARIABLE", for those leftover users
   that only need "the_index", but not the compatility macros.

 * 12/12: Manual follow-up to the cocci-applied 11/12.

The only exceptions to this are the changes to cache.h itself, and
moving things around in contrib/coccinelle/* as we can fully migrate
some things, so they move from a "pending" rule to a regular
coccinelle rule.

At the start of this the patch to migrate all of this would have been
~2k lines, after it's down to below ~1k.

I've carefully crafted this so that it doesn't have conflicts with
anything that's in-flight, or is likely to develop such conflicts (but
I could always get unlucky). If there are any the resolution should be
trivially validated by running the cocci rule on "theirs" and keeping
that version.

Branch & passing CI at:
https://github.com/avar/git/tree/avar/narrow-down-the-index-compatibilty-macros

Ævar Arnfjörð Bjarmason (12):
  cache.h: remove unused "the_index" compat macros
  builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS"
  cocci & cache.h: remove rarely used "the_index" compat macros
  read-cache API & users: make discard_index() return void
  cocci: add a index-compatibility.pending.cocci
  cocci & cache.h: apply a selection of "pending" index-compatibility
  cocci & cache.h: apply variable section of "pending"
    index-compatibility
  cocci: apply "pending" index-compatibility to "t/helper/*.c"
  {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE"
  cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
  cocci: apply "pending" index-compatibility to some "builtin/*.c"
  builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS"

 add-interactive.c                             |  8 +-
 add-patch.c                                   |  3 +-
 builtin/add.c                                 | 21 ++---
 builtin/am.c                                  |  4 +-
 builtin/cat-file.c                            |  2 +-
 builtin/check-attr.c                          |  4 +-
 builtin/check-ignore.c                        |  4 +-
 builtin/checkout-index.c                      | 19 ++--
 builtin/checkout.c                            | 55 ++++++------
 builtin/clean.c                               |  6 +-
 builtin/clone.c                               |  4 +-
 builtin/commit.c                              | 20 ++---
 builtin/describe.c                            |  7 +-
 builtin/diff-files.c                          |  3 +-
 builtin/diff-index.c                          |  5 +-
 builtin/diff-tree.c                           |  4 +-
 builtin/difftool.c                            |  2 +-
 builtin/fsck.c                                | 16 ++--
 builtin/grep.c                                |  1 -
 builtin/log.c                                 |  1 -
 builtin/merge-index.c                         | 16 ++--
 builtin/merge-ours.c                          |  3 +-
 builtin/merge-tree.c                          |  2 +-
 builtin/merge.c                               | 13 +--
 builtin/mv.c                                  | 25 +++---
 builtin/pull.c                                |  6 +-
 builtin/read-tree.c                           |  6 +-
 builtin/rebase.c                              |  6 +-
 builtin/reset.c                               | 20 +++--
 builtin/rev-parse.c                           |  4 +-
 builtin/rm.c                                  | 18 ++--
 builtin/stash.c                               |  8 +-
 builtin/submodule--helper.c                   | 14 +--
 builtin/update-index.c                        | 72 +++++++--------
 cache.h                                       | 30 ++-----
 contrib/coccinelle/index-compatibility.cocci  | 87 +++++++++++++++++++
 .../index-compatibility.pending.cocci         | 78 +++++++++++++++++
 read-cache.c                                  |  4 +-
 repository.c                                  |  2 +-
 sequencer.c                                   | 15 ++--
 t/helper/test-dump-cache-tree.c               |  5 +-
 t/helper/test-dump-split-index.c              |  1 +
 t/helper/test-dump-untracked-cache.c          |  4 +-
 t/helper/test-fast-rebase.c                   |  4 +-
 t/helper/test-lazy-init-name-hash.c           | 21 ++---
 t/helper/test-read-cache.c                    |  5 +-
 t/helper/test-scrap-cache-tree.c              |  9 +-
 t/helper/test-write-cache.c                   |  6 +-
 48 files changed, 417 insertions(+), 256 deletions(-)
 create mode 100644 contrib/coccinelle/index-compatibility.cocci
 create mode 100644 contrib/coccinelle/index-compatibility.pending.cocci

-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH 01/12] cache.h: remove unused "the_index" compat macros
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:30 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 02/12] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:30 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

The "active_alloc" macro added in 228e94f9357 (Move index-related
variables into a structure., 2007-04-01) has not been used since
4aab5b46f44 (Make read-cache.c "the_index" free., 2007-04-01). Let's
remove it.

The rest of these are likewise unused, so let's not keep them
around. E.g. 12cd0bf9b02 (dir: stop using the index compatibility
macros, 2017-05-05) is the last use of "cache_dir_exists".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 cache.h | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/cache.h b/cache.h
index 26ed03bd6de..29bb0ad2b41 100644
--- a/cache.h
+++ b/cache.h
@@ -438,7 +438,6 @@ extern struct index_state the_index;
 
 #define active_cache (the_index.cache)
 #define active_nr (the_index.cache_nr)
-#define active_alloc (the_index.cache_alloc)
 #define active_cache_changed (the_index.cache_changed)
 #define active_cache_tree (the_index.cache_tree)
 
@@ -452,7 +451,6 @@ extern struct index_state the_index;
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
 #define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
-#define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
 #define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
 #define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
@@ -461,13 +459,10 @@ extern struct index_state the_index;
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
-#define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
 #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
 #define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
-#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
-#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 02/12] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS"
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 01/12] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:30 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 03/12] cocci & cache.h: remove rarely used "the_index" compat macros Ævar Arnfjörð Bjarmason
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:30 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Adding "USE_THE_INDEX_COMPATIBILITY_MACROS" to these two appears to
have been unnecessary from the start, as going back and compiling
f8adbec9fea (cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch,
2019-01-24) without that addition works.

Let's not have these ask for the compatibility macros from cache.h
that they don't need.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/grep.c | 1 -
 builtin/log.c  | 1 -
 2 files changed, 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 5fa927d4e22..f7821c5fbba 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -3,7 +3,6 @@
  *
  * Copyright (c) 2006 Junio C Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "repository.h"
 #include "config.h"
diff --git a/builtin/log.c b/builtin/log.c
index 5eafcf26b49..89447a50838 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -4,7 +4,6 @@
  * (C) Copyright 2006 Linus Torvalds
  *		 2006 Junio Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "refs.h"
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 03/12] cocci & cache.h: remove rarely used "the_index" compat macros
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 01/12] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 02/12] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:30 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 04/12] read-cache API & users: make discard_index() return void Ævar Arnfjörð Bjarmason
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:30 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Since 4aab5b46f44 (Make read-cache.c "the_index" free., 2007-04-01)
we've been undergoing a slow migration away from these macros, but
haven't made much progress since f8adbec9fea (cache.h: flip
NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24).

Let's move forward a bit by changing the users of those macros that
are rare enough that we can convert them in one go, and then remove
the compatibility shim.

The only manual change to the C code here is to "cache.h", the rest is
all the result of applying the new "index-compatibility.cocci".

Even though it's a one-off, let's keep the coccinelle rules for
now. We'll extend them in subsequent commits, and this will help
anything that's in-flight or out-of-tree to migrate.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c                                |  5 ++-
 builtin/am.c                                 |  4 +-
 builtin/checkout.c                           |  4 +-
 builtin/clean.c                              |  2 +-
 builtin/commit.c                             |  2 +-
 builtin/merge.c                              |  2 +-
 builtin/mv.c                                 |  4 +-
 builtin/pull.c                               |  4 +-
 builtin/read-tree.c                          |  4 +-
 builtin/reset.c                              |  4 +-
 builtin/update-index.c                       |  4 +-
 cache.h                                      | 10 -----
 contrib/coccinelle/index-compatibility.cocci | 45 ++++++++++++++++++++
 13 files changed, 65 insertions(+), 29 deletions(-)
 create mode 100644 contrib/coccinelle/index-compatibility.cocci

diff --git a/builtin/add.c b/builtin/add.c
index 626c71ec6aa..75be1d021ab 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -55,7 +55,7 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
 			continue;
 
 		if (!show_only)
-			err = chmod_cache_entry(ce, flip);
+			err = chmod_index_entry(&the_index, ce, flip);
 		else
 			err = S_ISREG(ce->ce_mode) ? 0 : -1;
 
@@ -172,7 +172,8 @@ static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
 			continue; /* do not touch non blobs */
 		if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
 			continue;
-		retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
+		retval |= add_file_to_index(&the_index, ce->name,
+					    flags | ADD_CACHE_RENORMALIZE);
 	}
 
 	return retval;
diff --git a/builtin/am.c b/builtin/am.c
index 20aea0d2487..f362a1c29fd 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1930,7 +1930,7 @@ static void am_resolve(struct am_state *state, int allow_empty)
 		}
 	}
 
-	if (unmerged_cache()) {
+	if (unmerged_index(&the_index)) {
 		printf_ln(_("You still have unmerged paths in your index.\n"
 			"You should 'git add' each file with resolved conflicts to mark them as such.\n"
 			"You might run `git rm` on a file to accept \"deleted by them\" for it."));
@@ -2045,7 +2045,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
 	if (!remote_tree)
 		return error(_("Could not parse object '%s'."), oid_to_hex(remote));
 
-	read_cache_unmerged();
+	repo_read_index_unmerged(the_repository);
 
 	if (fast_forward_to(head_tree, head_tree, 1))
 		return -1;
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2a132392fbe..aa610b274e3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -722,7 +722,7 @@ static void init_topts(struct unpack_trees_options *topts, int merge,
 
 	setup_unpack_trees_porcelain(topts, "checkout");
 
-	topts->initial_checkout = is_cache_unborn();
+	topts->initial_checkout = is_index_unborn(&the_index);
 	topts->update = 1;
 	topts->merge = 1;
 	topts->quiet = merge && old_commit;
@@ -763,7 +763,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 
 		refresh_cache(REFRESH_QUIET);
 
-		if (unmerged_cache()) {
+		if (unmerged_index(&the_index)) {
 			error(_("you need to resolve your current index first"));
 			return 1;
 		}
diff --git a/builtin/clean.c b/builtin/clean.c
index 40ff2c578de..7084e685f6f 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -1031,7 +1031,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		struct stat st;
 		const char *rel;
 
-		if (!cache_name_is_other(ent->name, ent->len))
+		if (!index_name_is_other(&the_index, ent->name, ent->len))
 			continue;
 
 		if (lstat(ent->name, &st))
diff --git a/builtin/commit.c b/builtin/commit.c
index e22bdf23f5f..985503d43a6 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -302,7 +302,7 @@ static void add_remove_files(struct string_list *list)
 			continue;
 
 		if (!lstat(p->string, &st)) {
-			if (add_to_cache(p->string, &st, 0))
+			if (add_to_index(&the_index, p->string, &st, 0))
 				die(_("updating files failed"));
 		} else
 			remove_file_from_cache(p->string);
diff --git a/builtin/merge.c b/builtin/merge.c
index b3f75f55c8a..da11dfae19e 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1375,7 +1375,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		goto done;
 	}
 
-	if (read_cache_unmerged())
+	if (repo_read_index_unmerged(the_repository))
 		die_resolve_conflict("merge");
 
 	if (file_exists(git_path_merge_head(the_repository))) {
diff --git a/builtin/mv.c b/builtin/mv.c
index 3413ad1c9b1..df6157fd6b5 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -343,7 +343,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			argc += last - first;
 			goto act_on_entry;
 		}
-		if (!(ce = cache_file_exists(src, length, 0))) {
+		if (!(ce = index_file_exists(&the_index, src, length, 0))) {
 			bad = _("not under version control");
 			goto act_on_entry;
 		}
@@ -472,7 +472,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
 			sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
-		rename_cache_entry_at(pos, dst);
+		rename_index_entry_at(&the_index, pos, dst);
 
 		if (ignore_sparse &&
 		    core_apply_sparse_checkout &&
diff --git a/builtin/pull.c b/builtin/pull.c
index b21edd767aa..4a2a6db2dce 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -1030,7 +1030,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 	if (opt_rebase < 0)
 		opt_rebase = config_get_rebase(&rebase_unspecified);
 
-	if (read_cache_unmerged())
+	if (repo_read_index_unmerged(the_repository))
 		die_resolve_conflict("pull");
 
 	if (file_exists(git_path_merge_head(the_repository)))
@@ -1043,7 +1043,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 		if (opt_autostash == -1)
 			opt_autostash = config_autostash;
 
-		if (is_null_oid(&orig_head) && !is_cache_unborn())
+		if (is_null_oid(&orig_head) && !is_index_unborn(&the_index))
 			die(_("Updating an unborn branch with changes added to the index."));
 
 		if (!opt_autostash)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index f4cbe460b97..8a697a782fc 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -188,7 +188,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 	 */
 
 	if (opts.reset || opts.merge || opts.prefix) {
-		if (read_cache_unmerged() && (opts.prefix || opts.merge))
+		if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
 			die(_("You need to resolve your current index first"));
 		stage = opts.merge = 1;
 	}
@@ -232,7 +232,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 			break;
 		case 2:
 			opts.fn = twoway_merge;
-			opts.initial_checkout = is_cache_unborn();
+			opts.initial_checkout = is_index_unborn(&the_index);
 			break;
 		case 3:
 		default:
diff --git a/builtin/reset.c b/builtin/reset.c
index fdce6f8c856..ee874ee6d5b 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -82,7 +82,7 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t
 		BUG("invalid reset_type passed to reset_index");
 	}
 
-	read_cache_unmerged();
+	repo_read_index_unmerged(the_repository);
 
 	if (reset_type == KEEP) {
 		struct object_id head_oid;
@@ -218,7 +218,7 @@ static void set_reflog_message(struct strbuf *sb, const char *action,
 
 static void die_if_unmerged_cache(int reset_type)
 {
-	if (is_merge() || unmerged_cache())
+	if (is_merge() || unmerged_index(&the_index))
 		die(_("Cannot do a %s reset in the middle of a merge."),
 		    _(reset_type_names[reset_type]));
 
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 7b0c924d7d4..e1b2bb78e72 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -445,7 +445,7 @@ static void chmod_path(char flip, const char *path)
 	if (pos < 0)
 		goto fail;
 	ce = active_cache[pos];
-	if (chmod_cache_entry(ce, flip) < 0)
+	if (chmod_index_entry(&the_index, ce, flip) < 0)
 		goto fail;
 
 	report("chmod %cx '%s'", flip, path);
@@ -641,7 +641,7 @@ static int unresolve_one(const char *path)
 	pos = cache_name_pos(path, namelen);
 	if (0 <= pos) {
 		/* already merged */
-		pos = unmerge_cache_entry_at(pos);
+		pos = unmerge_index_entry_at(&the_index, pos);
 		if (pos < active_nr) {
 			const struct cache_entry *ce = active_cache[pos];
 			if (ce_stage(ce) &&
diff --git a/cache.h b/cache.h
index 29bb0ad2b41..53dc4341717 100644
--- a/cache.h
+++ b/cache.h
@@ -444,25 +444,15 @@ extern struct index_state the_index;
 #define read_cache() repo_read_index(the_repository)
 #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
 #define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
-#define is_cache_unborn() is_index_unborn(&the_index)
-#define read_cache_unmerged() repo_read_index_unmerged(the_repository)
 #define discard_cache() discard_index(&the_index)
-#define unmerged_cache() unmerged_index(&the_index)
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
-#define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
-#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
-#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
-#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
-#define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
-#define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
-#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
new file mode 100644
index 00000000000..04207c409e1
--- /dev/null
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -0,0 +1,45 @@
+// "the_repository" simple cases
+@@
+@@
+(
+- read_cache_unmerged
++ repo_read_index_unmerged
+)
+  (
++ the_repository,
+  ...)
+
+// "the_index" simple cases
+@@
+@@
+(
+- is_cache_unborn
++ is_index_unborn
+|
+- unmerged_cache
++ unmerged_index
+|
+- rename_cache_entry_at
++ rename_index_entry_at
+|
+- chmod_cache_entry
++ chmod_index_entry
+|
+- cache_file_exists
++ index_file_exists
+|
+- cache_name_is_other
++ index_name_is_other
+|
+- unmerge_cache_entry_at
++ unmerge_index_entry_at
+|
+- add_to_cache
++ add_to_index
+|
+- add_file_to_cache
++ add_file_to_index
+)
+  (
++ &the_index,
+  ...)
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 04/12] read-cache API & users: make discard_index() return void
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (2 preceding siblings ...)
  2022-11-18 11:30 ` [PATCH 03/12] cocci & cache.h: remove rarely used "the_index" compat macros Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:30 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:30 ` [PATCH 05/12] cocci: add a index-compatibility.pending.cocci Ævar Arnfjörð Bjarmason
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:30 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

The discard_index() function has not returned non-zero since
7a51ed66f65 (Make on-disk index representation separate from in-core
one, 2008-01-14), but we've had various code in-tree still acting as
though that might be the case.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 add-interactive.c |  8 ++++----
 add-patch.c       |  3 ++-
 builtin/merge.c   |  3 ++-
 cache.h           |  2 +-
 read-cache.c      |  4 +---
 sequencer.c       | 15 +++++++++------
 6 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/add-interactive.c b/add-interactive.c
index ecc5ae1b249..ae1839c04a7 100644
--- a/add-interactive.c
+++ b/add-interactive.c
@@ -530,8 +530,8 @@ static int get_modified_files(struct repository *r,
 	struct collection_status s = { 0 };
 	int i;
 
-	if (discard_index(r->index) < 0 ||
-	    repo_read_index_preload(r, ps, 0) < 0)
+	discard_index(r->index);
+	if (repo_read_index_preload(r, ps, 0) < 0)
 		return error(_("could not read index"));
 
 	prefix_item_list_clear(files);
@@ -1156,8 +1156,8 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
 		    _("staged"), _("unstaged"), _("path"));
 	opts.list_opts.header = header.buf;
 
-	if (discard_index(r->index) < 0 ||
-	    repo_read_index(r) < 0 ||
+	discard_index(r->index);
+	if (repo_read_index(r) < 0 ||
 	    repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
 					 NULL, NULL, NULL) < 0)
 		warning(_("could not refresh index"));
diff --git a/add-patch.c b/add-patch.c
index 33ecd8398a1..a86a92e1646 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1750,7 +1750,8 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
 		s.mode = &patch_mode_add;
 	s.revision = revision;
 
-	if (discard_index(r->index) < 0 || repo_read_index(r) < 0 ||
+	discard_index(r->index);
+	if (repo_read_index(r) < 0 ||
 	    (!s.mode->index_only &&
 	     repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
 					  NULL, NULL, NULL) < 0) ||
diff --git a/builtin/merge.c b/builtin/merge.c
index da11dfae19e..89fdac24e40 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -390,7 +390,8 @@ static void restore_state(const struct object_id *head,
 	run_command(&cmd);
 
 refresh_cache:
-	if (discard_cache() < 0 || read_cache() < 0)
+	discard_cache();
+	if (read_cache() < 0)
 		die(_("could not read index"));
 }
 
diff --git a/cache.h b/cache.h
index 53dc4341717..489e9e1925b 100644
--- a/cache.h
+++ b/cache.h
@@ -774,7 +774,7 @@ void ensure_full_index(struct index_state *istate);
  */
 int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
 
-int discard_index(struct index_state *);
+void discard_index(struct index_state *);
 void move_index_extensions(struct index_state *dst, struct index_state *src);
 int unmerged_index(const struct index_state *);
 
diff --git a/read-cache.c b/read-cache.c
index 32024029274..46f5e497b14 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2531,7 +2531,7 @@ int is_index_unborn(struct index_state *istate)
 	return (!istate->cache_nr && !istate->timestamp.sec);
 }
 
-int discard_index(struct index_state *istate)
+void discard_index(struct index_state *istate)
 {
 	/*
 	 * Cache entries in istate->cache[] should have been allocated
@@ -2562,8 +2562,6 @@ int discard_index(struct index_state *istate)
 		mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
 		FREE_AND_NULL(istate->ce_mem_pool);
 	}
-
-	return 0;
 }
 
 /*
diff --git a/sequencer.c b/sequencer.c
index f0f1af4d478..58ad2a77565 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -3564,7 +3564,8 @@ static int do_exec(struct repository *r, const char *command_line)
 	status = run_command(&cmd);
 
 	/* force re-reading of the cache */
-	if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
+	discard_index(r->index);
+	if (repo_read_index(r) < 0)
 		return error(_("could not read index"));
 
 	dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
@@ -4028,9 +4029,11 @@ static int do_merge(struct repository *r,
 		ret = run_command(&cmd);
 
 		/* force re-reading of the cache */
-		if (!ret && (discard_index(r->index) < 0 ||
-			     repo_read_index(r) < 0))
-			ret = error(_("could not read index"));
+		if (!ret) {
+			discard_index(r->index);
+			if (repo_read_index(r) < 0)
+				ret = error(_("could not read index"));
+		}
 		goto leave_merge;
 	}
 
@@ -4400,8 +4403,8 @@ void create_autostash(struct repository *r, const char *path)
 		printf(_("Created autostash: %s\n"), buf.buf);
 		if (reset_head(r, &ropts) < 0)
 			die(_("could not reset --hard"));
-		if (discard_index(r->index) < 0 ||
-			repo_read_index(r) < 0)
+		discard_index(r->index);
+		if (repo_read_index(r) < 0)
 			die(_("could not read index"));
 	}
 	strbuf_release(&buf);
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 05/12] cocci: add a index-compatibility.pending.cocci
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (3 preceding siblings ...)
  2022-11-18 11:30 ` [PATCH 04/12] read-cache API & users: make discard_index() return void Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:30 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:31 ` [PATCH 06/12] cocci & cache.h: apply a selection of "pending" index-compatibility Ævar Arnfjörð Bjarmason
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:30 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Add a coccinelle rule which covers the rest of the macros guarded by
"USE_THE_INDEX_COMPATIBILITY_MACROS" cache.h. If the result of this
were applied it can be reduced down to just:

	#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
	extern struct index_state the_index;
	#endif

But that patch is just under 2000 lines, so let's first add this as a
"pending", and then incrementally pick changes from it in subsequent
commits. In doing that we'll migrate rules from this
"index-compatibility.pending.cocci" to the "index-compatibility.cocci"
created in a preceding commit.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 .../index-compatibility.pending.cocci         | 110 ++++++++++++++++++
 1 file changed, 110 insertions(+)
 create mode 100644 contrib/coccinelle/index-compatibility.pending.cocci

diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
new file mode 100644
index 00000000000..6e96de0f638
--- /dev/null
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -0,0 +1,110 @@
+// the_index.* variables
+@@
+@@
+(
+- active_cache
++ the_index.cache
+|
+- active_nr
++ the_index.cache_nr
+|
+- active_cache_changed
++ the_index.cache_changed
+|
+- active_cache_tree
++ the_index.cache_tree
+)
+
+// "the_repository" simple cases
+@@
+@@
+(
+- read_cache
++ repo_read_index
+|
+- hold_locked_index
++ repo_hold_locked_index
+)
+  (
++ the_repository,
+  ...)
+
+// "the_index" simple cases
+@@
+@@
+(
+- discard_cache
++ discard_index
+|
+- cache_name_pos
++ index_name_pos
+|
+- add_cache_entry
++ add_index_entry
+|
+- remove_file_from_cache
++ remove_file_from_index
+|
+- ce_match_stat
++ ie_match_stat
+|
+- ce_modified
++ ie_modified
+|
+- resolve_undo_clear
++ resolve_undo_clear_index
+)
+  (
++ &the_index,
+  ...)
+
+// "the_repository" special-cases
+@@
+@@
+(
+- read_cache_preload
++ repo_read_index_preload
+)
+  (
++ the_repository,
+  ...
++ , 0
+  )
+
+@@
+@@
+(
+- refresh_and_write_cache
++ repo_refresh_and_write_index
+)
+  (
++ the_repository,
+  ...
++ , NULL, NULL, NULL
+  )
+
+
+// "the_index" special-cases
+@@
+@@
+(
+- read_cache_from
++ read_index_from
+)
+  (
++ &the_index,
+  ...
++ , get_git_dir()
+  )
+
+@@
+@@
+(
+- refresh_cache
++ refresh_index
+)
+  (
++ &the_index,
+  ...
++ , NULL, NULL, NULL
+  )
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 06/12] cocci & cache.h: apply a selection of "pending" index-compatibility
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (4 preceding siblings ...)
  2022-11-18 11:30 ` [PATCH 05/12] cocci: add a index-compatibility.pending.cocci Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:31 ` [PATCH 07/12] cocci & cache.h: apply variable section " Ævar Arnfjörð Bjarmason
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Apply a selection of rules in "index-compatibility.pending.cocci"
tree-wide, and in doing so migrate them to
"index-compatibility.cocci".

As in preceding commits the only manual changes here are the macro
removals in "cache.h", and the update to the '*.cocci" rules. The rest
of the C code changes are the result of applying those updated rules.

Move rules for some rarely used cache compatibility macros from
"index-compatibility.pending.cocci" to "index-compatibility.cocci" and
apply them.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/checkout.c                            |  5 +++--
 builtin/commit.c                              |  2 +-
 builtin/merge.c                               |  2 +-
 builtin/mv.c                                  |  4 +++-
 builtin/read-tree.c                           |  2 +-
 builtin/reset.c                               |  5 +++--
 builtin/rm.c                                  |  4 ++--
 builtin/update-index.c                        | 22 +++++++++----------
 cache.h                                       |  5 -----
 contrib/coccinelle/index-compatibility.cocci  | 15 +++++++++++++
 .../index-compatibility.pending.cocci         | 15 -------------
 11 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index aa610b274e3..afbd71764a3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -160,7 +160,8 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
 		}
 	}
 
-	add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
+	add_index_entry(&the_index, ce,
+			ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
 	return 0;
 }
 
@@ -744,7 +745,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 	if (read_cache_preload(NULL) < 0)
 		return error(_("index file corrupt"));
 
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 	if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
 		if (new_branch_info->commit)
 			BUG("'switch --orphan' should never accept a commit as starting point");
diff --git a/builtin/commit.c b/builtin/commit.c
index 985503d43a6..10a827b1f4a 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -305,7 +305,7 @@ static void add_remove_files(struct string_list *list)
 			if (add_to_index(&the_index, p->string, &st, 0))
 				die(_("updating files failed"));
 		} else
-			remove_file_from_cache(p->string);
+			remove_file_from_index(&the_index, p->string);
 	}
 }
 
diff --git a/builtin/merge.c b/builtin/merge.c
index 89fdac24e40..2046992d75a 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1397,7 +1397,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		else
 			die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
 	}
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 
 	if (option_edit < 0)
 		option_edit = default_edit_option();
diff --git a/builtin/mv.c b/builtin/mv.c
index df6157fd6b5..6f94c3b9238 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -471,7 +471,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		pos = cache_name_pos(src, strlen(src));
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
-			sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
+			sparse_and_dirty = ie_modified(&the_index,
+						       active_cache[pos], &st,
+						       0);
 		rename_index_entry_at(&the_index, pos, dst);
 
 		if (ignore_sparse &&
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 8a697a782fc..5a05cbe02c7 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -192,7 +192,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 			die(_("You need to resolve your current index first"));
 		stage = opts.merge = 1;
 	}
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
diff --git a/builtin/reset.c b/builtin/reset.c
index ee874ee6d5b..d38fb7d13fa 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -143,7 +143,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 		struct cache_entry *ce;
 
 		if (!is_in_reset_tree && !intent_to_add) {
-			remove_file_from_cache(one->path);
+			remove_file_from_index(&the_index, one->path);
 			continue;
 		}
 
@@ -170,7 +170,8 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 			ce->ce_flags |= CE_INTENT_TO_ADD;
 			set_object_name_for_intent_to_add_entry(ce);
 		}
-		add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
+		add_index_entry(&the_index, ce,
+				ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
 	}
 }
 
diff --git a/builtin/rm.c b/builtin/rm.c
index f0d025a4e23..c717cdc97f5 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -168,7 +168,7 @@ static int check_local_mod(struct object_id *head, int index_only)
 		 * Is the index different from the file in the work tree?
 		 * If it's a submodule, is its work tree modified?
 		 */
-		if (ce_match_stat(ce, &st, 0) ||
+		if (ie_match_stat(&the_index, ce, &st, 0) ||
 		    (S_ISGITLINK(ce->ce_mode) &&
 		     bad_to_remove_submodule(ce->name,
 				SUBMODULE_REMOVAL_DIE_ON_ERROR |
@@ -386,7 +386,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		if (!quiet)
 			printf("rm '%s'\n", path);
 
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(&the_index, path))
 			die(_("git rm: unable to remove %s"), path);
 	}
 
diff --git a/builtin/update-index.c b/builtin/update-index.c
index e1b2bb78e72..533ba66df05 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -256,7 +256,7 @@ static int remove_one_path(const char *path)
 {
 	if (!allow_remove)
 		return error("%s: does not exist and --remove not passed", path);
-	if (remove_file_from_cache(path))
+	if (remove_file_from_index(&the_index, path))
 		return error("%s: cannot remove from the index", path);
 	return 0;
 }
@@ -281,7 +281,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	struct cache_entry *ce;
 
 	/* Was the old index entry already up-to-date? */
-	if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
+	if (old && !ce_stage(old) && !ie_match_stat(&the_index, old, st, 0))
 		return 0;
 
 	ce = make_empty_cache_entry(&the_index, len);
@@ -298,7 +298,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	}
 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
-	if (add_cache_entry(ce, option)) {
+	if (add_index_entry(&the_index, ce, option)) {
 		discard_cache_entry(ce);
 		return error("%s: cannot add to the index - missing --add option?", path);
 	}
@@ -390,7 +390,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 		 * On the other hand, removing it from index should work
 		 */
 		if (!ignore_skip_worktree_entries && allow_remove &&
-		    remove_file_from_cache(path))
+		    remove_file_from_index(&the_index, path))
 			return error("%s: cannot remove from the index", path);
 		return 0;
 	}
@@ -429,7 +429,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 		ce->ce_flags |= CE_VALID;
 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
-	if (add_cache_entry(ce, option))
+	if (add_index_entry(&the_index, ce, option))
 		return error("%s: cannot add to the index - missing --add option?",
 			     path);
 	report("add '%s'", path);
@@ -488,7 +488,7 @@ static void update_one(const char *path)
 	}
 
 	if (force_remove) {
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(&the_index, path))
 			die("git update-index: unable to remove %s", path);
 		report("remove '%s'", path);
 		return;
@@ -571,7 +571,7 @@ static void read_index_info(int nul_term_line)
 
 		if (!mode) {
 			/* mode == 0 means there is no such path -- remove */
-			if (remove_file_from_cache(path_name))
+			if (remove_file_from_index(&the_index, path_name))
 				die("git update-index: unable to remove %s",
 				    ptr);
 		}
@@ -686,13 +686,13 @@ static int unresolve_one(const char *path)
 		goto free_return;
 	}
 
-	remove_file_from_cache(path);
-	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
+	remove_file_from_index(&the_index, path);
+	if (add_index_entry(&the_index, ce_2, ADD_CACHE_OK_TO_ADD)) {
 		error("%s: cannot add our version to the index.", path);
 		ret = -1;
 		goto free_return;
 	}
-	if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
+	if (!add_index_entry(&the_index, ce_3, ADD_CACHE_OK_TO_ADD))
 		return 0;
 	error("%s: cannot add their version to the index.", path);
 	ret = -1;
@@ -850,7 +850,7 @@ static int resolve_undo_clear_callback(const struct option *opt,
 {
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 	return 0;
 }
 
diff --git a/cache.h b/cache.h
index 489e9e1925b..79b8d479710 100644
--- a/cache.h
+++ b/cache.h
@@ -446,13 +446,8 @@ extern struct index_state the_index;
 #define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
 #define discard_cache() discard_index(&the_index)
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
-#define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
-#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
-#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
-#define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
-#define resolve_undo_clear() resolve_undo_clear_index(&the_index)
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
index 04207c409e1..89c18986160 100644
--- a/contrib/coccinelle/index-compatibility.cocci
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -39,6 +39,21 @@
 |
 - add_file_to_cache
 + add_file_to_index
+|
+- add_cache_entry
++ add_index_entry
+|
+- remove_file_from_cache
++ remove_file_from_index
+|
+- ce_match_stat
++ ie_match_stat
+|
+- ce_modified
++ ie_modified
+|
+- resolve_undo_clear
++ resolve_undo_clear_index
 )
   (
 + &the_index,
diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
index 6e96de0f638..cf1ea57d700 100644
--- a/contrib/coccinelle/index-compatibility.pending.cocci
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -38,21 +38,6 @@
 |
 - cache_name_pos
 + index_name_pos
-|
-- add_cache_entry
-+ add_index_entry
-|
-- remove_file_from_cache
-+ remove_file_from_index
-|
-- ce_match_stat
-+ ie_match_stat
-|
-- ce_modified
-+ ie_modified
-|
-- resolve_undo_clear
-+ resolve_undo_clear_index
 )
   (
 + &the_index,
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 07/12] cocci & cache.h: apply variable section of "pending" index-compatibility
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (5 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 06/12] cocci & cache.h: apply a selection of "pending" index-compatibility Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:31 ` [PATCH 08/12] cocci: apply "pending" index-compatibility to "t/helper/*.c" Ævar Arnfjörð Bjarmason
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Mostly apply the part of "index-compatibility.pending.cocci" that
renames the global variables like "active_nr", which are a shorthand
to referencing (in that case) a struct member as "the_index.cache_nr".

In doing so move more of "index-compatibility.pending.cocci" to
"index-compatibility.cocci".

In the case of "active_cache_tree" we'd have a textual conflict with
"vd/skip-cache-tree-update" in "next"[1], and in the case of
"active_nr" with "ab/various-leak-fixes" in "seen". Let's exclude
those specific cases while moving the rule over from "pending".

1. dc5d40f5bc4 (read-tree: use 'skip_cache_tree_update' option,
   2022-11-10)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c                                 |  8 ++--
 builtin/checkout-index.c                      | 10 ++--
 builtin/checkout.c                            | 46 +++++++++----------
 builtin/commit.c                              | 16 +++----
 builtin/fsck.c                                | 12 ++---
 builtin/merge-index.c                         | 10 ++--
 builtin/merge.c                               |  6 +--
 builtin/mv.c                                  | 19 ++++----
 builtin/reset.c                               |  2 +-
 builtin/rm.c                                  | 14 +++---
 builtin/stash.c                               |  8 ++--
 builtin/submodule--helper.c                   | 14 +++---
 builtin/update-index.c                        | 46 +++++++++----------
 cache.h                                       |  2 -
 contrib/coccinelle/index-compatibility.cocci  | 27 +++++++++++
 .../index-compatibility.pending.cocci         | 17 -------
 t/helper/test-dump-cache-tree.c               |  2 +-
 t/helper/test-scrap-cache-tree.c              |  4 +-
 18 files changed, 136 insertions(+), 127 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 75be1d021ab..23f9f0e08bb 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -42,8 +42,8 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
 {
 	int i, ret = 0;
 
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		struct cache_entry *ce = the_index.cache[i];
 		int err;
 
 		if (!include_sparse &&
@@ -159,8 +159,8 @@ static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
 {
 	int i, retval = 0;
 
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		struct cache_entry *ce = the_index.cache[i];
 
 		if (!include_sparse &&
 		    (ce_skip_worktree(ce) ||
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 97e06e8c52c..c2aee450c40 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -75,8 +75,8 @@ static int checkout_file(const char *name, const char *prefix)
 	if (pos < 0)
 		pos = -pos - 1;
 
-	while (pos < active_nr) {
-		struct cache_entry *ce = active_cache[pos];
+	while (pos < the_index.cache_nr) {
+		struct cache_entry *ce = the_index.cache[pos];
 		if (ce_namelen(ce) != namelen ||
 		    memcmp(ce->name, name, namelen))
 			break;
@@ -136,8 +136,8 @@ static int checkout_all(const char *prefix, int prefix_length)
 	int i, errs = 0;
 	struct cache_entry *last_ce = NULL;
 
-	for (i = 0; i < active_nr ; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr ; i++) {
+		struct cache_entry *ce = the_index.cache[i];
 
 		if (S_ISSPARSEDIR(ce->ce_mode)) {
 			if (!ce_skip_worktree(ce))
@@ -151,7 +151,7 @@ static int checkout_all(const char *prefix, int prefix_length)
 			 */
 			if (ignore_skip_worktree) {
 				ensure_full_index(&the_index);
-				ce = active_cache[i];
+				ce = the_index.cache[i];
 			}
 		}
 
diff --git a/builtin/checkout.c b/builtin/checkout.c
index afbd71764a3..3fd303f5604 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -150,7 +150,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
 	 */
 	pos = cache_name_pos(ce->name, ce->ce_namelen);
 	if (pos >= 0) {
-		struct cache_entry *old = active_cache[pos];
+		struct cache_entry *old = the_index.cache[pos];
 		if (ce->ce_mode == old->ce_mode &&
 		    !ce_intent_to_add(old) &&
 		    oideq(&ce->oid, &old->oid)) {
@@ -179,8 +179,8 @@ static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
 
 static int skip_same_name(const struct cache_entry *ce, int pos)
 {
-	while (++pos < active_nr &&
-	       !strcmp(active_cache[pos]->name, ce->name))
+	while (++pos < the_index.cache_nr &&
+	       !strcmp(the_index.cache[pos]->name, ce->name))
 		; /* skip */
 	return pos;
 }
@@ -188,9 +188,9 @@ static int skip_same_name(const struct cache_entry *ce, int pos)
 static int check_stage(int stage, const struct cache_entry *ce, int pos,
 		       int overlay_mode)
 {
-	while (pos < active_nr &&
-	       !strcmp(active_cache[pos]->name, ce->name)) {
-		if (ce_stage(active_cache[pos]) == stage)
+	while (pos < the_index.cache_nr &&
+	       !strcmp(the_index.cache[pos]->name, ce->name)) {
+		if (ce_stage(the_index.cache[pos]) == stage)
 			return 0;
 		pos++;
 	}
@@ -207,8 +207,8 @@ static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
 	unsigned seen = 0;
 	const char *name = ce->name;
 
-	while (pos < active_nr) {
-		ce = active_cache[pos];
+	while (pos < the_index.cache_nr) {
+		ce = the_index.cache[pos];
 		if (strcmp(name, ce->name))
 			break;
 		seen |= (1 << ce_stage(ce));
@@ -224,10 +224,10 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
 			  const struct checkout *state, int *nr_checkouts,
 			  int overlay_mode)
 {
-	while (pos < active_nr &&
-	       !strcmp(active_cache[pos]->name, ce->name)) {
-		if (ce_stage(active_cache[pos]) == stage)
-			return checkout_entry(active_cache[pos], state,
+	while (pos < the_index.cache_nr &&
+	       !strcmp(the_index.cache[pos]->name, ce->name)) {
+		if (ce_stage(the_index.cache[pos]) == stage)
+			return checkout_entry(the_index.cache[pos], state,
 					      NULL, nr_checkouts);
 		pos++;
 	}
@@ -244,7 +244,7 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
 static int checkout_merged(int pos, const struct checkout *state,
 			   int *nr_checkouts, struct mem_pool *ce_mem_pool)
 {
-	struct cache_entry *ce = active_cache[pos];
+	struct cache_entry *ce = the_index.cache[pos];
 	const char *path = ce->name;
 	mmfile_t ancestor, ours, theirs;
 	enum ll_merge_result merge_status;
@@ -257,7 +257,7 @@ static int checkout_merged(int pos, const struct checkout *state,
 	int renormalize = 0;
 
 	memset(threeway, 0, sizeof(threeway));
-	while (pos < active_nr) {
+	while (pos < the_index.cache_nr) {
 		int stage;
 		stage = ce_stage(ce);
 		if (!stage || strcmp(path, ce->name))
@@ -266,7 +266,7 @@ static int checkout_merged(int pos, const struct checkout *state,
 		if (stage == 2)
 			mode = create_ce_mode(ce->ce_mode);
 		pos++;
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 	}
 	if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2]))
 		return error(_("path '%s' does not have necessary versions"), path);
@@ -392,8 +392,8 @@ static int checkout_worktree(const struct checkout_opts *opts,
 	if (pc_workers > 1)
 		init_parallel_checkout();
 
-	for (pos = 0; pos < active_nr; pos++) {
-		struct cache_entry *ce = active_cache[pos];
+	for (pos = 0; pos < the_index.cache_nr; pos++) {
+		struct cache_entry *ce = the_index.cache[pos];
 		if (ce->ce_flags & CE_MATCHED) {
 			if (!ce_stage(ce)) {
 				errs |= checkout_entry(ce, &state,
@@ -541,13 +541,13 @@ static int checkout_paths(const struct checkout_opts *opts,
 	 * Make sure all pathspecs participated in locating the paths
 	 * to be checked out.
 	 */
-	for (pos = 0; pos < active_nr; pos++)
+	for (pos = 0; pos < the_index.cache_nr; pos++)
 		if (opts->overlay_mode)
-			mark_ce_for_checkout_overlay(active_cache[pos],
+			mark_ce_for_checkout_overlay(the_index.cache[pos],
 						     ps_matched,
 						     opts);
 		else
-			mark_ce_for_checkout_no_overlay(active_cache[pos],
+			mark_ce_for_checkout_no_overlay(the_index.cache[pos],
 							ps_matched,
 							opts);
 
@@ -562,8 +562,8 @@ static int checkout_paths(const struct checkout_opts *opts,
 		unmerge_marked_index(&the_index);
 
 	/* Any unmerged paths? */
-	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+	for (pos = 0; pos < the_index.cache_nr; pos++) {
+		const struct cache_entry *ce = the_index.cache[pos];
 		if (ce->ce_flags & CE_MATCHED) {
 			if (!ce_stage(ce))
 				continue;
@@ -868,7 +868,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 		}
 	}
 
-	if (!cache_tree_fully_valid(active_cache_tree))
+	if (!cache_tree_fully_valid(the_index.cache_tree))
 		cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
 
 	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
diff --git a/builtin/commit.c b/builtin/commit.c
index 10a827b1f4a..abbed750276 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -272,8 +272,8 @@ static int list_paths(struct string_list *list, const char *with_tree,
 
 	/* TODO: audit for interaction with sparse-index. */
 	ensure_full_index(&the_index);
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 		struct string_list_item *item;
 
 		if (ce->ce_flags & CE_UPDATE)
@@ -461,8 +461,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	if (!only && !pathspec.nr) {
 		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
 		refresh_cache_or_die(refresh_flags);
-		if (active_cache_changed
-		    || !cache_tree_fully_valid(active_cache_tree))
+		if (the_index.cache_changed
+		    || !cache_tree_fully_valid(the_index.cache_tree))
 			update_main_cache_tree(WRITE_TREE_SILENT);
 		if (write_locked_index(&the_index, &index_lock,
 				       COMMIT_LOCK | SKIP_IF_UNCHANGED))
@@ -998,10 +998,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 
 			/* TODO: audit for interaction with sparse-index. */
 			ensure_full_index(&the_index);
-			for (i = 0; i < active_nr; i++)
-				if (ce_intent_to_add(active_cache[i]))
+			for (i = 0; i < the_index.cache_nr; i++)
+				if (ce_intent_to_add(the_index.cache[i]))
 					ita_nr++;
-			committable = active_nr - ita_nr > 0;
+			committable = the_index.cache_nr - ita_nr > 0;
 		} else {
 			/*
 			 * Unless the user did explicitly request a submodule
@@ -1823,7 +1823,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		append_merge_tag_headers(parents, &tail);
 	}
 
-	if (commit_tree_extended(sb.buf, sb.len, &active_cache_tree->oid,
+	if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
 				 parents, &oid, author_ident.buf, NULL,
 				 sign_commit, extra)) {
 		rollback_index_files();
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 7436e1a68ef..cd47675e371 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -961,26 +961,26 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 		read_cache();
 		/* TODO: audit for interaction with sparse-index. */
 		ensure_full_index(&the_index);
-		for (i = 0; i < active_nr; i++) {
+		for (i = 0; i < the_index.cache_nr; i++) {
 			unsigned int mode;
 			struct blob *blob;
 			struct object *obj;
 
-			mode = active_cache[i]->ce_mode;
+			mode = the_index.cache[i]->ce_mode;
 			if (S_ISGITLINK(mode))
 				continue;
 			blob = lookup_blob(the_repository,
-					   &active_cache[i]->oid);
+					   &the_index.cache[i]->oid);
 			if (!blob)
 				continue;
 			obj = &blob->object;
 			obj->flags |= USED;
 			fsck_put_object_name(&fsck_walk_options, &obj->oid,
-					     ":%s", active_cache[i]->name);
+					     ":%s", the_index.cache[i]->name);
 			mark_object_reachable(obj);
 		}
-		if (active_cache_tree)
-			fsck_cache_tree(active_cache_tree);
+		if (the_index.cache_tree)
+			fsck_cache_tree(the_index.cache_tree);
 		fsck_resolve_undo(&the_index);
 	}
 
diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index 012f52bd007..a334c513c9d 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -14,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 >= active_nr)
+	if (pos >= the_index.cache_nr)
 		die("git merge-index: %s not in the cache", path);
 	found = 0;
 	do {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = the_index.cache[pos];
 		int stage = ce_stage(ce);
 
 		if (strcmp(ce->name, path))
@@ -28,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 < active_nr);
+	} while (++pos < the_index.cache_nr);
 	if (!found)
 		die("git merge-index: %s not in the cache", path);
 
@@ -62,8 +62,8 @@ static void merge_all(void)
 	int i;
 	/* TODO: audit for interaction with sparse-index. */
 	ensure_full_index(&the_index);
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 		if (!ce_stage(ce))
 			continue;
 		i += merge_entry(i, ce->name)-1;
diff --git a/builtin/merge.c b/builtin/merge.c
index 2046992d75a..27218ee93c9 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -694,7 +694,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
 	if (!trees[nr_trees++])
 		return -1;
 	opts.fn = threeway_merge;
-	cache_tree_free(&active_cache_tree);
+	cache_tree_free(&the_index.cache_tree);
 	for (i = 0; i < nr_trees; i++) {
 		parse_tree(trees[i]);
 		init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
@@ -784,8 +784,8 @@ static int count_unmerged_entries(void)
 {
 	int i, ret = 0;
 
-	for (i = 0; i < active_nr; i++)
-		if (ce_stage(active_cache[i]))
+	for (i = 0; i < the_index.cache_nr; i++)
+		if (ce_stage(the_index.cache[i]))
 			ret++;
 
 	return ret;
diff --git a/builtin/mv.c b/builtin/mv.c
index 6f94c3b9238..883cf3ff12b 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -87,7 +87,7 @@ static void prepare_move_submodule(const char *src, int first,
 				   const char **submodule_gitfile)
 {
 	struct strbuf submodule_dotgit = STRBUF_INIT;
-	if (!S_ISGITLINK(active_cache[first]->ce_mode))
+	if (!S_ISGITLINK(the_index.cache[first]->ce_mode))
 		die(_("Directory %s is in index and no submodule?"), src);
 	if (!is_staging_gitmodules_ok(&the_index))
 		die(_("Please stage your changes to .gitmodules or stash them to proceed"));
@@ -111,8 +111,8 @@ static int index_range_of_same_dir(const char *src, int length,
 		die(_("%.*s is in index"), len_w_slash, src_w_slash);
 
 	first = -1 - first;
-	for (last = first; last < active_nr; last++) {
-		const char *path = active_cache[last]->name;
+	for (last = first; last < the_index.cache_nr; last++) {
+		const char *path = the_index.cache[last]->name;
 		if (strncmp(path, src_w_slash, len_w_slash))
 			break;
 	}
@@ -143,7 +143,7 @@ static int empty_dir_has_sparse_contents(const char *name)
 		pos = -pos - 1;
 		if (pos >= the_index.cache_nr)
 			goto free_return;
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 		if (strncmp(with_slash, ce->name, length))
 			goto free_return;
 		if (ce_skip_worktree(ce))
@@ -268,7 +268,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 					bad = _("bad source");
 				goto act_on_entry;
 			}
-			ce = active_cache[pos];
+			ce = the_index.cache[pos];
 			if (!ce_skip_worktree(ce)) {
 				bad = _("bad source");
 				goto act_on_entry;
@@ -331,7 +331,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			dst_len = strlen(dst);
 
 			for (j = 0; j < last - first; j++) {
-				const struct cache_entry *ce = active_cache[first + j];
+				const struct cache_entry *ce = the_index.cache[first + j];
 				const char *path = ce->name;
 				source[argc + j] = path;
 				destination[argc + j] =
@@ -472,7 +472,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
 			sparse_and_dirty = ie_modified(&the_index,
-						       active_cache[pos], &st,
+						       the_index.cache[pos],
+						       &st,
 						       0);
 		rename_index_entry_at(&the_index, pos, dst);
 
@@ -489,7 +490,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			    path_in_sparse_checkout(dst, &the_index)) {
 				/* from out-of-cone to in-cone */
 				int dst_pos = cache_name_pos(dst, strlen(dst));
-				struct cache_entry *dst_ce = active_cache[dst_pos];
+				struct cache_entry *dst_ce = the_index.cache[dst_pos];
 
 				dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
 
@@ -500,7 +501,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				   !path_in_sparse_checkout(dst, &the_index)) {
 				/* from in-cone to out-of-cone */
 				int dst_pos = cache_name_pos(dst, strlen(dst));
-				struct cache_entry *dst_ce = active_cache[dst_pos];
+				struct cache_entry *dst_ce = the_index.cache[dst_pos];
 
 				/*
 				 * if src is clean, it will suffice to remove it
diff --git a/builtin/reset.c b/builtin/reset.c
index d38fb7d13fa..25ff76c17a3 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -159,7 +159,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 		 * to properly construct the reset sparse directory.
 		 */
 		pos = cache_name_pos(one->path, strlen(one->path));
-		if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
+		if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) ||
 		    (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
 			ce->ce_flags |= CE_SKIP_WORKTREE;
 
diff --git a/builtin/rm.c b/builtin/rm.c
index c717cdc97f5..2b23eb25438 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -35,8 +35,8 @@ static int get_ours_cache_pos(const char *path, int pos)
 {
 	int i = -pos - 1;
 
-	while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {
-		if (ce_stage(active_cache[i]) == 2)
+	while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) {
+		if (ce_stage(the_index.cache[i]) == 2)
 			return i;
 		i++;
 	}
@@ -78,7 +78,7 @@ static void submodules_absorb_gitdir_if_needed(void)
 			if (pos < 0)
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 
 		if (!S_ISGITLINK(ce->ce_mode) ||
 		    !file_exists(ce->name) ||
@@ -127,11 +127,11 @@ static int check_local_mod(struct object_id *head, int index_only)
 			if (pos < 0)
 				continue;
 
-			if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
+			if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) ||
 			    is_empty_dir(name))
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 
 		if (lstat(ce->name, &st) < 0) {
 			if (!is_missing_file_error(errno))
@@ -303,8 +303,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	if (pathspec_needs_expanded_index(&the_index, &pathspec))
 		ensure_full_index(&the_index);
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 
 		if (!include_sparse &&
 		    (ce_skip_worktree(ce) ||
diff --git a/builtin/stash.c b/builtin/stash.c
index bb5485b4095..c251419af7c 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -454,10 +454,10 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
 		 * path, but left it out of the working tree, then clear the
 		 * SKIP_WORKTREE bit and write it to the working tree.
 		 */
-		if (pos >= 0 && ce_skip_worktree(active_cache[pos])) {
+		if (pos >= 0 && ce_skip_worktree(the_index.cache[pos])) {
 			struct stat st;
 
-			ce = active_cache[pos];
+			ce = the_index.cache[pos];
 			if (!lstat(ce->name, &st)) {
 				/* Conflicting path present; relocate it */
 				struct strbuf new_path = STRBUF_INIT;
@@ -1520,8 +1520,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 
 		/* TODO: audit for interaction with sparse-index. */
 		ensure_full_index(&the_index);
-		for (i = 0; i < active_nr; i++)
-			ce_path_match(&the_index, active_cache[i], ps,
+		for (i = 0; i < the_index.cache_nr; i++)
+			ce_path_match(&the_index, the_index.cache[i], ps,
 				      ps_matched);
 
 		if (report_path_error(ps_matched, ps)) {
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index a7683d35299..808865a5e31 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -199,8 +199,8 @@ static int module_list_compute(const char **argv,
 	if (read_cache() < 0)
 		die(_("index file corrupt"));
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 
 		if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
 				    0, ps_matched, 1) ||
@@ -209,8 +209,8 @@ static int module_list_compute(const char **argv,
 
 		ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
 		list->entries[list->nr++] = ce;
-		while (i + 1 < active_nr &&
-		       !strcmp(ce->name, active_cache[i + 1]->name))
+		while (i + 1 < the_index.cache_nr &&
+		       !strcmp(ce->name, the_index.cache[i + 1]->name))
 			/*
 			 * Skip entries with the same name in different stages
 			 * to make sure an entry is returned only once.
@@ -3255,15 +3255,15 @@ static void die_on_index_match(const char *path, int force)
 		 * need to check ps_matched[0] to know if a cache
 		 * entry matched.
 		 */
-		for (i = 0; i < active_nr; i++) {
-			ce_path_match(&the_index, active_cache[i], &ps,
+		for (i = 0; i < the_index.cache_nr; i++) {
+			ce_path_match(&the_index, the_index.cache[i], &ps,
 				      ps_matched);
 
 			if (ps_matched[0]) {
 				if (!force)
 					die(_("'%s' already exists in the index"),
 					    path);
-				if (!S_ISGITLINK(active_cache[i]->ce_mode))
+				if (!S_ISGITLINK(the_index.cache[i]->ce_mode))
 					die(_("'%s' already exists in the index "
 					      "and is not a submodule"), path);
 				break;
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 533ba66df05..fec3da05abb 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -239,14 +239,14 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 	int namelen = strlen(path);
 	int pos = cache_name_pos(path, namelen);
 	if (0 <= pos) {
-		mark_fsmonitor_invalid(&the_index, active_cache[pos]);
+		mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
 		if (mark)
-			active_cache[pos]->ce_flags |= flag;
+			the_index.cache[pos]->ce_flags |= flag;
 		else
-			active_cache[pos]->ce_flags &= ~flag;
-		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
+			the_index.cache[pos]->ce_flags &= ~flag;
+		the_index.cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(&the_index, path);
-		active_cache_changed |= CE_ENTRY_CHANGED;
+		the_index.cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
 	return -1;
@@ -335,7 +335,7 @@ static int process_directory(const char *path, int len, struct stat *st)
 
 	/* Exact match: file or existing gitlink */
 	if (pos >= 0) {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = the_index.cache[pos];
 		if (S_ISGITLINK(ce->ce_mode)) {
 
 			/* Do nothing to the index if there is no HEAD! */
@@ -350,8 +350,8 @@ static int process_directory(const char *path, int len, struct stat *st)
 
 	/* Inexact match: is there perhaps a subdirectory match? */
 	pos = -pos-1;
-	while (pos < active_nr) {
-		const struct cache_entry *ce = active_cache[pos++];
+	while (pos < the_index.cache_nr) {
+		const struct cache_entry *ce = the_index.cache[pos++];
 
 		if (strncmp(ce->name, path, len))
 			break;
@@ -382,7 +382,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 		return error("'%s' is beyond a symbolic link", path);
 
 	pos = cache_name_pos(path, len);
-	ce = pos < 0 ? NULL : active_cache[pos];
+	ce = pos < 0 ? NULL : the_index.cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
 		 * working directory version is assumed "good"
@@ -444,7 +444,7 @@ static void chmod_path(char flip, const char *path)
 	pos = cache_name_pos(path, strlen(path));
 	if (pos < 0)
 		goto fail;
-	ce = active_cache[pos];
+	ce = the_index.cache[pos];
 	if (chmod_index_entry(&the_index, ce, flip) < 0)
 		goto fail;
 
@@ -642,8 +642,8 @@ static int unresolve_one(const char *path)
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_index_entry_at(&the_index, pos);
-		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+		if (pos < the_index.cache_nr) {
+			const struct cache_entry *ce = the_index.cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen))
@@ -656,8 +656,8 @@ static int unresolve_one(const char *path)
 		 * want to do anything in the former case.
 		 */
 		pos = -pos-1;
-		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+		if (pos < the_index.cache_nr) {
+			const struct cache_entry *ce = the_index.cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
 				fprintf(stderr,
@@ -752,8 +752,8 @@ static int do_reupdate(const char **paths,
 		 */
 		has_head = 0;
  redo:
-	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+	for (pos = 0; pos < the_index.cache_nr; pos++) {
+		const struct cache_entry *ce = the_index.cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
 		char *path;
@@ -782,12 +782,12 @@ static int do_reupdate(const char **paths,
 		 * path anymore, in which case, under 'allow_remove',
 		 * or worse yet 'allow_replace', active_nr may decrease.
 		 */
-		save_nr = active_nr;
+		save_nr = the_index.cache_nr;
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
 		discard_cache_entry(old);
-		if (save_nr != active_nr)
+		if (save_nr != the_index.cache_nr)
 			goto redo;
 	}
 	clear_pathspec(&pathspec);
@@ -813,7 +813,7 @@ static int refresh(struct refresh_params *o, unsigned int flag)
 		 * refresh_cache() as these are no actual errors.
 		 * cmd_status() does the same.
 		 */
-		active_cache_changed |= SOMETHING_CHANGED;
+		the_index.cache_changed |= SOMETHING_CHANGED;
 	}
 	return 0;
 }
@@ -951,7 +951,7 @@ static enum parse_opt_result unresolve_callback(
 	*has_errors = do_unresolve(ctx->argc, ctx->argv,
 				prefix, prefix ? strlen(prefix) : 0);
 	if (*has_errors)
-		active_cache_changed = 0;
+		the_index.cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -972,7 +972,7 @@ static enum parse_opt_result reupdate_callback(
 	setup_work_tree();
 	*has_errors = do_reupdate(ctx->argv + 1, prefix);
 	if (*has_errors)
-		active_cache_changed = 0;
+		the_index.cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -1178,7 +1178,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
 		if (the_index.version != preferred_index_format)
-			active_cache_changed |= SOMETHING_CHANGED;
+			the_index.cache_changed |= SOMETHING_CHANGED;
 		the_index.version = preferred_index_format;
 	}
 
@@ -1290,7 +1290,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		report(_("fsmonitor disabled"));
 	}
 
-	if (active_cache_changed || force_write) {
+	if (the_index.cache_changed || force_write) {
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
diff --git a/cache.h b/cache.h
index 79b8d479710..8b54b627459 100644
--- a/cache.h
+++ b/cache.h
@@ -436,9 +436,7 @@ void prefetch_cache_entries(const struct index_state *istate,
 #ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
 extern struct index_state the_index;
 
-#define active_cache (the_index.cache)
 #define active_nr (the_index.cache_nr)
-#define active_cache_changed (the_index.cache_changed)
 #define active_cache_tree (the_index.cache_tree)
 
 #define read_cache() repo_read_index(the_repository)
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
index 89c18986160..2e9d8296076 100644
--- a/contrib/coccinelle/index-compatibility.cocci
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -1,3 +1,30 @@
+// the_index.* variables
+@@
+@@
+(
+- active_cache
++ the_index.cache
+|
+- active_cache_changed
++ the_index.cache_changed
+)
+
+@@
+identifier f != prepare_to_commit;
+@@
+  f(...) {<...
+- active_nr
++ the_index.cache_nr
+  ...>}
+
+@@
+identifier f != cmd_read_tree;
+@@
+  f(...) {<...
+- active_cache_tree
++ the_index.cache_tree
+  ...>}
+
 // "the_repository" simple cases
 @@
 @@
diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
index cf1ea57d700..8207aee62c3 100644
--- a/contrib/coccinelle/index-compatibility.pending.cocci
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -1,20 +1,3 @@
-// the_index.* variables
-@@
-@@
-(
-- active_cache
-+ the_index.cache
-|
-- active_nr
-+ the_index.cache_nr
-|
-- active_cache_changed
-+ the_index.cache_changed
-|
-- active_cache_tree
-+ the_index.cache_tree
-)
-
 // "the_repository" simple cases
 @@
 @@
diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
index 0d6d7f1ecbf..8d88111b357 100644
--- a/t/helper/test-dump-cache-tree.c
+++ b/t/helper/test-dump-cache-tree.c
@@ -67,7 +67,7 @@ int cmd__dump_cache_tree(int ac, const char **av)
 	istate = the_index;
 	istate.cache_tree = another;
 	cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
-	ret = dump_cache_tree(active_cache_tree, another, "");
+	ret = dump_cache_tree(the_index.cache_tree, another, "");
 	cache_tree_free(&another);
 
 	return ret;
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
index 026c802479d..20e2e31f9e2 100644
--- a/t/helper/test-scrap-cache-tree.c
+++ b/t/helper/test-scrap-cache-tree.c
@@ -12,8 +12,8 @@ int cmd__scrap_cache_tree(int ac, const char **av)
 	hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
 	if (read_cache() < 0)
 		die("unable to read index file");
-	cache_tree_free(&active_cache_tree);
-	active_cache_tree = NULL;
+	cache_tree_free(&the_index.cache_tree);
+	the_index.cache_tree = NULL;
 	if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
 		die("unable to write index file");
 	return 0;
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 08/12] cocci: apply "pending" index-compatibility to "t/helper/*.c"
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (6 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 07/12] cocci & cache.h: apply variable section " Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:31 ` [PATCH 09/12] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Apply the "index-compatibility.pending.cocci" rule to the "t/helper/*"
directory, a subsequent commit will extend cache.h to further narrow
down the use of "USE_THE_INDEX_COMPATIBILITY_MACROS" in this area.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/helper/test-dump-cache-tree.c      |  2 +-
 t/helper/test-dump-untracked-cache.c |  2 +-
 t/helper/test-fast-rebase.c          |  2 +-
 t/helper/test-lazy-init-name-hash.c  | 20 ++++++++++----------
 t/helper/test-read-cache.c           |  4 ++--
 t/helper/test-scrap-cache-tree.c     |  4 ++--
 t/helper/test-write-cache.c          |  5 +++--
 7 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
index 8d88111b357..4bf8bc3df6e 100644
--- a/t/helper/test-dump-cache-tree.c
+++ b/t/helper/test-dump-cache-tree.c
@@ -62,7 +62,7 @@ int cmd__dump_cache_tree(int ac, const char **av)
 	int ret;
 
 	setup_git_directory();
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die("unable to read index file");
 	istate = the_index;
 	istate.cache_tree = another;
diff --git a/t/helper/test-dump-untracked-cache.c b/t/helper/test-dump-untracked-cache.c
index 99010614f6d..ffe34736e53 100644
--- a/t/helper/test-dump-untracked-cache.c
+++ b/t/helper/test-dump-untracked-cache.c
@@ -51,7 +51,7 @@ int cmd__dump_untracked_cache(int ac, const char **av)
 	xsetenv("GIT_CONFIG_VALUE_0", "keep", 1);
 
 	setup_git_directory();
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die("unable to read index file");
 	uc = the_index.untracked;
 	if (!uc) {
diff --git a/t/helper/test-fast-rebase.c b/t/helper/test-fast-rebase.c
index 45665ec19a5..9def2ba6889 100644
--- a/t/helper/test-fast-rebase.c
+++ b/t/helper/test-fast-rebase.c
@@ -123,7 +123,7 @@ int cmd__fast_rebase(int argc, const char **argv)
 		die(_("Cannot read HEAD"));
 	assert(oideq(&onto->object.oid, &head));
 
-	hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
 	if (repo_read_index(the_repository) < 0)
 		BUG("Could not read index");
 
diff --git a/t/helper/test-lazy-init-name-hash.c b/t/helper/test-lazy-init-name-hash.c
index cd1b4c9736e..7e8723a1900 100644
--- a/t/helper/test-lazy-init-name-hash.c
+++ b/t/helper/test-lazy-init-name-hash.c
@@ -32,7 +32,7 @@ static void dump_run(void)
 	struct dir_entry *dir;
 	struct cache_entry *ce;
 
-	read_cache();
+	repo_read_index(the_repository);
 	if (single) {
 		test_lazy_init_name_hash(&the_index, 0);
 	} else {
@@ -49,7 +49,7 @@ static void dump_run(void)
 				ent /* member name */)
 		printf("name %08x %s\n", ce->ent.hash, ce->name);
 
-	discard_cache();
+	discard_index(&the_index);
 }
 
 /*
@@ -66,7 +66,7 @@ static uint64_t time_runs(int try_threaded)
 
 	for (i = 0; i < count; i++) {
 		t0 = getnanotime();
-		read_cache();
+		repo_read_index(the_repository);
 		t1 = getnanotime();
 		nr_threads_used = test_lazy_init_name_hash(&the_index, try_threaded);
 		t2 = getnanotime();
@@ -89,7 +89,7 @@ static uint64_t time_runs(int try_threaded)
 				   the_index.cache_nr);
 		fflush(stdout);
 
-		discard_cache();
+		discard_index(&the_index);
 	}
 
 	avg = sum / count;
@@ -113,9 +113,9 @@ static void analyze_run(void)
 	int i;
 	int nr;
 
-	read_cache();
+	repo_read_index(the_repository);
 	cache_nr_limit = the_index.cache_nr;
-	discard_cache();
+	discard_index(&the_index);
 
 	nr = analyze;
 	while (1) {
@@ -128,23 +128,23 @@ static void analyze_run(void)
 			nr = cache_nr_limit;
 
 		for (i = 0; i < count; i++) {
-			read_cache();
+			repo_read_index(the_repository);
 			the_index.cache_nr = nr; /* cheap truncate of index */
 			t1s = getnanotime();
 			test_lazy_init_name_hash(&the_index, 0);
 			t2s = getnanotime();
 			sum_single += (t2s - t1s);
 			the_index.cache_nr = cache_nr_limit;
-			discard_cache();
+			discard_index(&the_index);
 
-			read_cache();
+			repo_read_index(the_repository);
 			the_index.cache_nr = nr; /* cheap truncate of index */
 			t1m = getnanotime();
 			nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
 			t2m = getnanotime();
 			sum_multi += (t2m - t1m);
 			the_index.cache_nr = cache_nr_limit;
-			discard_cache();
+			discard_index(&the_index);
 
 			if (!nr_threads_used)
 				printf("    [size %8d] [single %f]   non-threaded code path used\n",
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
index b736ef16421..a089be9104c 100644
--- a/t/helper/test-read-cache.c
+++ b/t/helper/test-read-cache.c
@@ -20,7 +20,7 @@ int cmd__read_cache(int argc, const char **argv)
 	git_config(git_default_config, NULL);
 
 	for (i = 0; i < cnt; i++) {
-		read_cache();
+		repo_read_index(the_repository);
 		if (name) {
 			int pos;
 
@@ -33,7 +33,7 @@ int cmd__read_cache(int argc, const char **argv)
 			       ce_uptodate(the_index.cache[pos]) ? "" : " not");
 			write_file(name, "%d\n", i);
 		}
-		discard_cache();
+		discard_index(&the_index);
 	}
 	return 0;
 }
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
index 20e2e31f9e2..9206dee1fd2 100644
--- a/t/helper/test-scrap-cache-tree.c
+++ b/t/helper/test-scrap-cache-tree.c
@@ -9,8 +9,8 @@ int cmd__scrap_cache_tree(int ac, const char **av)
 	struct lock_file index_lock = LOCK_INIT;
 
 	setup_git_directory();
-	hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
-	if (read_cache() < 0)
+	repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
+	if (repo_read_index(the_repository) < 0)
 		die("unable to read index file");
 	cache_tree_free(&the_index.cache_tree);
 	the_index.cache_tree = NULL;
diff --git a/t/helper/test-write-cache.c b/t/helper/test-write-cache.c
index 8837717d36a..66287d6d47d 100644
--- a/t/helper/test-write-cache.c
+++ b/t/helper/test-write-cache.c
@@ -9,9 +9,10 @@ int cmd__write_cache(int argc, const char **argv)
 	if (argc == 2)
 		cnt = strtol(argv[1], NULL, 0);
 	setup_git_directory();
-	read_cache();
+	repo_read_index(the_repository);
 	for (i = 0; i < cnt; i++) {
-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &index_lock,
+				       LOCK_DIE_ON_ERROR);
 		if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
 			die("unable to write index file");
 	}
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 09/12] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE"
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (7 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 08/12] cocci: apply "pending" index-compatibility to "t/helper/*.c" Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 18:41   ` Taylor Blau
  2022-11-18 11:31 ` [PATCH 10/12] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
                   ` (4 subsequent siblings)
  13 siblings, 1 reply; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Split up the "USE_THE_INDEX_COMPATIBILITY_MACROS" into that setting
and a more narrow "USE_THE_INDEX_VARIABLE". In the case of these
built-ins we only need "the_index" variable, but not the compatibility
wrapper for functions we're not using.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/cat-file.c   | 2 +-
 builtin/difftool.c   | 2 +-
 builtin/merge-tree.c | 2 +-
 builtin/pull.c       | 2 +-
 cache.h              | 6 +++++-
 repository.c         | 2 +-
 6 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index fa7bd891692..b3be58b1fb0 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/builtin/difftool.c b/builtin/difftool.c
index d7f08c8a7fa..d9b76226f6a 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -11,7 +11,7 @@
  *
  * Copyright (C) 2016 Johannes Schindelin
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..e3767087bbf 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "tree-walk.h"
 #include "xdiff-interface.h"
diff --git a/builtin/pull.c b/builtin/pull.c
index 4a2a6db2dce..1ab4de0005d 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -5,7 +5,7 @@
  *
  * Fetch one or more remote refs and merge it/them into the current HEAD.
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/cache.h b/cache.h
index 8b54b627459..72c3d4e2ef5 100644
--- a/cache.h
+++ b/cache.h
@@ -433,9 +433,11 @@ typedef int (*must_prefetch_predicate)(const struct cache_entry *);
 void prefetch_cache_entries(const struct index_state *istate,
 			    must_prefetch_predicate must_prefetch);
 
-#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
+#if defined(USE_THE_INDEX_COMPATIBILITY_MACROS) || defined(USE_THE_INDEX_VARIABLE)
 extern struct index_state the_index;
 
+#ifndef USE_THE_INDEX_VARIABLE
+#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
 #define active_nr (the_index.cache_nr)
 #define active_cache_tree (the_index.cache_tree)
 
@@ -448,6 +450,8 @@ extern struct index_state the_index;
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
+#endif
+#endif
 
 #define TYPE_BITS 3
 
diff --git a/repository.c b/repository.c
index 5d166b692c8..3427085fd6d 100644
--- a/repository.c
+++ b/repository.c
@@ -2,7 +2,7 @@
  * not really _using_ the compat macros, just make sure the_index
  * declaration matches the definition in this file.
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "repository.h"
 #include "object-store.h"
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 10/12] cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (8 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 09/12] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 18:41   ` Taylor Blau
  2022-11-18 11:31 ` [PATCH 11/12] cocci: apply "pending" index-compatibility to some "builtin/*.c" Ævar Arnfjörð Bjarmason
                   ` (3 subsequent siblings)
  13 siblings, 1 reply; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

In a preceding commit we fully applied the
"index-compatibility.pending.cocci" rule to "t/helper/*". Let's now
stop defining "USE_THE_INDEX_COMPATIBILITY_MACROS" in test-tool.h
itself, and instead instead move the define to the individual test
helpers that need it. This mirrors how we do the same thing in the
"builtin/" directory.

We could start using "USE_THE_INDEX_COMPATIBILITY_MACROS", but let's
instead introduce a narrow version of it named
"USE_THE_INDEX_VARIABLE". As the name suggests this doesn't give us
any of the compatibility macros, but only "the_index" variable.

Eventually we'll move other outstanding users of
"USE_THE_INDEX_COMPATIBILITY_MACROS" over to using this more narrow
and descriptive define.

For context: The USE_THE_INDEX_COMPATIBILITY_MACROS macro was added to
test-tool.h in f8adbec9fea (cache.h: flip
NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24).

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/helper/test-dump-cache-tree.c      | 1 +
 t/helper/test-dump-split-index.c     | 1 +
 t/helper/test-dump-untracked-cache.c | 2 +-
 t/helper/test-fast-rebase.c          | 2 +-
 t/helper/test-lazy-init-name-hash.c  | 1 +
 t/helper/test-read-cache.c           | 1 +
 t/helper/test-scrap-cache-tree.c     | 1 +
 t/helper/test-write-cache.c          | 1 +
 8 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
index 4bf8bc3df6e..454f17b1a0c 100644
--- a/t/helper/test-dump-cache-tree.c
+++ b/t/helper/test-dump-cache-tree.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "tree.h"
diff --git a/t/helper/test-dump-split-index.c b/t/helper/test-dump-split-index.c
index a209880eb37..0ea97b84072 100644
--- a/t/helper/test-dump-split-index.c
+++ b/t/helper/test-dump-split-index.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "split-index.h"
diff --git a/t/helper/test-dump-untracked-cache.c b/t/helper/test-dump-untracked-cache.c
index ffe34736e53..6d53683f13b 100644
--- a/t/helper/test-dump-untracked-cache.c
+++ b/t/helper/test-dump-untracked-cache.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "dir.h"
diff --git a/t/helper/test-fast-rebase.c b/t/helper/test-fast-rebase.c
index 9def2ba6889..efc82dd80c5 100644
--- a/t/helper/test-fast-rebase.c
+++ b/t/helper/test-fast-rebase.c
@@ -10,7 +10,7 @@
  * refactoring is the better route).
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 
 #include "cache-tree.h"
diff --git a/t/helper/test-lazy-init-name-hash.c b/t/helper/test-lazy-init-name-hash.c
index 7e8723a1900..ab86c14c8ba 100644
--- a/t/helper/test-lazy-init-name-hash.c
+++ b/t/helper/test-lazy-init-name-hash.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "parse-options.h"
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
index a089be9104c..23e9e27109f 100644
--- a/t/helper/test-read-cache.c
+++ b/t/helper/test-read-cache.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "config.h"
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
index 9206dee1fd2..a26107ed70a 100644
--- a/t/helper/test-scrap-cache-tree.c
+++ b/t/helper/test-scrap-cache-tree.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "lockfile.h"
diff --git a/t/helper/test-write-cache.c b/t/helper/test-write-cache.c
index 66287d6d47d..7d45cd61e82 100644
--- a/t/helper/test-write-cache.c
+++ b/t/helper/test-write-cache.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "lockfile.h"
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 11/12] cocci: apply "pending" index-compatibility to some "builtin/*.c"
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (9 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 10/12] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 11:31 ` [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Apply "index-compatibility.pending.cocci" rule to those "builtin/*",
and commit the ones that have 3 uses or less of the relevant
macros.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c            | 6 +++---
 builtin/check-attr.c     | 2 +-
 builtin/check-ignore.c   | 2 +-
 builtin/checkout-index.c | 7 ++++---
 builtin/clean.c          | 2 +-
 builtin/clone.c          | 2 +-
 builtin/describe.c       | 5 +++--
 builtin/diff-files.c     | 2 +-
 builtin/diff-index.c     | 4 ++--
 builtin/diff-tree.c      | 2 +-
 builtin/fsck.c           | 2 +-
 builtin/merge-index.c    | 4 ++--
 builtin/merge-ours.c     | 2 +-
 builtin/rebase.c         | 4 ++--
 builtin/reset.c          | 7 ++++---
 builtin/rev-parse.c      | 2 +-
 16 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 23f9f0e08bb..566c27e5311 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -312,7 +312,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
 
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("Could not read the index"));
 
 	repo_init_revisions(the_repository, &rev, prefix);
@@ -544,7 +544,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	/*
 	 * Check the "pathspec '%s' did not match any files" block
@@ -587,7 +587,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		 (!(addremove || take_worktree_changes)
 		  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 
-	if (read_cache_preload(&pathspec) < 0)
+	if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
 		die(_("index file corrupt"));
 
 	die_in_unpopulated_submodule(&the_index, prefix);
diff --git a/builtin/check-attr.c b/builtin/check-attr.c
index dd833977864..2b9271f816c 100644
--- a/builtin/check-attr.c
+++ b/builtin/check-attr.c
@@ -115,7 +115,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, check_attr_options,
 			     check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
 
-	if (read_cache() < 0) {
+	if (repo_read_index(the_repository) < 0) {
 		die("invalid cache");
 	}
 
diff --git a/builtin/check-ignore.c b/builtin/check-ignore.c
index 21912569650..6ebd0169fc8 100644
--- a/builtin/check-ignore.c
+++ b/builtin/check-ignore.c
@@ -179,7 +179,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
 		die(_("--non-matching is only valid with --verbose"));
 
 	/* read_cache() is only necessary so we can watch out for submodules. */
-	if (!no_index && read_cache() < 0)
+	if (!no_index && repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	setup_standard_excludes(&dir);
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index c2aee450c40..9087005b4fb 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -65,7 +65,7 @@ static void write_tempfile_record(const char *name, const char *prefix)
 static int checkout_file(const char *name, const char *prefix)
 {
 	int namelen = strlen(name);
-	int pos = cache_name_pos(name, namelen);
+	int pos = index_name_pos(&the_index, name, namelen);
 	int has_same_name = 0;
 	int is_file = 0;
 	int is_skipped = 1;
@@ -249,7 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	if (read_cache() < 0) {
+	if (repo_read_index(the_repository) < 0) {
 		die("invalid cache");
 	}
 
@@ -270,7 +270,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	if (index_opt && !state.base_dir_len && !to_tempfile) {
 		state.refresh_cache = 1;
 		state.istate = &the_index;
-		hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &lock_file,
+				       LOCK_DIE_ON_ERROR);
 	}
 
 	get_parallel_checkout_configs(&pc_workers, &pc_threshold);
diff --git a/builtin/clean.c b/builtin/clean.c
index 7084e685f6f..7bdeb01d30e 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -1012,7 +1012,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
diff --git a/builtin/clone.c b/builtin/clone.c
index 0e4348686b6..8ab0453a292 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -703,7 +703,7 @@ static int checkout(int submodule_progress, int filter_submodules)
 	/* We need to be in the new work tree for the checkout */
 	setup_work_tree();
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	memset(&opts, 0, sizeof opts);
 	opts.update = 1;
diff --git a/builtin/describe.c b/builtin/describe.c
index 23e3f05fb10..4f4a58eef2b 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -653,10 +653,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			int fd, result;
 
 			setup_work_tree();
-			read_cache();
+			repo_read_index(the_repository);
 			refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
 				      NULL, NULL, NULL);
-			fd = hold_locked_index(&index_lock, 0);
+			fd = repo_hold_locked_index(the_repository,
+						    &index_lock, 0);
 			if (0 <= fd)
 				repo_update_index_if_able(the_repository, &index_lock);
 
diff --git a/builtin/diff-files.c b/builtin/diff-files.c
index 096ea2fedbc..1f3deb13c28 100644
--- a/builtin/diff-files.c
+++ b/builtin/diff-files.c
@@ -76,7 +76,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
 	    (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
 		diff_merges_set_dense_combined_if_unset(&rev);
 
-	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
+	if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
 		perror("read_cache_preload");
 		result = -1;
 		goto cleanup;
diff --git a/builtin/diff-index.c b/builtin/diff-index.c
index aea139b9d8f..a178e774a5a 100644
--- a/builtin/diff-index.c
+++ b/builtin/diff-index.c
@@ -62,11 +62,11 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 		usage(diff_cache_usage);
 	if (!(option & DIFF_INDEX_CACHED)) {
 		setup_work_tree();
-		if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
+		if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
 			perror("read_cache_preload");
 			return -1;
 		}
-	} else if (read_cache() < 0) {
+	} else if (repo_read_index(the_repository) < 0) {
 		perror("read_cache");
 		return -1;
 	}
diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c
index 85e8c81e594..a724087261d 100644
--- a/builtin/diff-tree.c
+++ b/builtin/diff-tree.c
@@ -120,7 +120,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 	repo_init_revisions(the_repository, opt, prefix);
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 	opt->abbrev = 0;
 	opt->diff = 1;
diff --git a/builtin/fsck.c b/builtin/fsck.c
index cd47675e371..d6f9a31e216 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -958,7 +958,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 	if (keep_cache_objects) {
 		verify_index_checksum = 1;
 		verify_ce_order = 1;
-		read_cache();
+		repo_read_index(the_repository);
 		/* TODO: audit for interaction with sparse-index. */
 		ensure_full_index(&the_index);
 		for (i = 0; i < the_index.cache_nr; i++) {
diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index a334c513c9d..b1d98294cdd 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -47,7 +47,7 @@ static int merge_entry(int pos, const char *path)
 
 static void merge_one_path(const char *path)
 {
-	int pos = cache_name_pos(path, strlen(path));
+	int pos = index_name_pos(&the_index, path, strlen(path));
 
 	/*
 	 * If it already exists in the cache as stage0, it's
@@ -82,7 +82,7 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix)
 	if (argc < 3)
 		usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
 
-	read_cache();
+	repo_read_index(the_repository);
 
 	/* TODO: audit for interaction with sparse-index. */
 	ensure_full_index(&the_index);
diff --git a/builtin/merge-ours.c b/builtin/merge-ours.c
index 3583cff71c7..5d95acf52a6 100644
--- a/builtin/merge-ours.c
+++ b/builtin/merge-ours.c
@@ -25,7 +25,7 @@ int cmd_merge_ours(int argc, const char **argv, const char *prefix)
 	 * commit.  The index must match HEAD, or this merge cannot go
 	 * through.
 	 */
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die_errno("read_cache failed");
 	if (index_differs_from(the_repository, "HEAD", NULL, 0))
 		return 2;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 5d855fd8f51..345a55755a3 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -292,7 +292,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
 	if (ret)
 		error(_("could not generate todo list"));
 	else {
-		discard_cache();
+		discard_index(&the_index);
 		if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
 						&todo_list))
 			BUG("unusable todo list");
@@ -1268,7 +1268,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 		if (get_oid("HEAD", &head))
 			die(_("Cannot read HEAD"));
 
-		fd = hold_locked_index(&lock_file, 0);
+		fd = repo_hold_locked_index(the_repository, &lock_file, 0);
 		if (repo_read_index(the_repository) < 0)
 			die(_("could not read index"));
 		refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
diff --git a/builtin/reset.c b/builtin/reset.c
index 25ff76c17a3..5ab03e9616a 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -158,7 +158,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 		 * if this entry is outside the sparse cone - this is necessary
 		 * to properly construct the reset sparse directory.
 		 */
-		pos = cache_name_pos(one->path, strlen(one->path));
+		pos = index_name_pos(&the_index, one->path, strlen(one->path));
 		if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) ||
 		    (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
 			ce->ce_flags |= CE_SKIP_WORKTREE;
@@ -421,7 +421,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	/* Soft reset does not touch the index file nor the working tree
@@ -432,7 +432,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 
 	if (reset_type != SOFT) {
 		struct lock_file lock = LOCK_INIT;
-		hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &lock,
+				       LOCK_DIE_ON_ERROR);
 		if (reset_type == MIXED) {
 			int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
 			if (read_from_tree(&pathspec, &oid, intent_to_add))
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 8f61050bde8..bffdd27bd7e 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -997,7 +997,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--shared-index-path")) {
-				if (read_cache() < 0)
+				if (repo_read_index(the_repository) < 0)
 					die(_("Could not read the index"));
 				if (the_index.split_index) {
 					const struct object_id *oid = &the_index.split_index->base_oid;
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS"
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (10 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 11/12] cocci: apply "pending" index-compatibility to some "builtin/*.c" Ævar Arnfjörð Bjarmason
@ 2022-11-18 11:31 ` Ævar Arnfjörð Bjarmason
  2022-11-18 18:42   ` Taylor Blau
  2022-11-18 12:13 ` [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Phillip Wood
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
  13 siblings, 1 reply; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 11:31 UTC (permalink / raw)
  To: git; +Cc: Taylor Blau, Junio C Hamano,
	Ævar Arnfjörð Bjarmason

Follow-up the preceding commit and change
"USE_THE_INDEX_COMPATIBILITY_MACROS" to the more narrow
"USE_THE_INDEX_VARIABLE" in cases where we still use "the_index".

Then remove "USE_THE_INDEX_VARIABLE" entirely in the few cases where
we don't require any compatibility macros or variables anymore.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c            | 2 +-
 builtin/check-attr.c     | 2 +-
 builtin/check-ignore.c   | 2 +-
 builtin/checkout-index.c | 2 +-
 builtin/clean.c          | 2 +-
 builtin/clone.c          | 2 +-
 builtin/describe.c       | 2 +-
 builtin/diff-files.c     | 1 -
 builtin/diff-index.c     | 1 -
 builtin/diff-tree.c      | 2 +-
 builtin/fsck.c           | 2 +-
 builtin/merge-index.c    | 2 +-
 builtin/merge-ours.c     | 1 -
 builtin/rebase.c         | 2 +-
 builtin/reset.c          | 2 +-
 builtin/rev-parse.c      | 2 +-
 16 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 566c27e5311..76277df326b 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2006 Linus Torvalds
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/builtin/check-attr.c b/builtin/check-attr.c
index 2b9271f816c..0fef10eb6bc 100644
--- a/builtin/check-attr.c
+++ b/builtin/check-attr.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
diff --git a/builtin/check-ignore.c b/builtin/check-ignore.c
index 6ebd0169fc8..ab776061c7c 100644
--- a/builtin/check-ignore.c
+++ b/builtin/check-ignore.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 9087005b4fb..cf6fba97ba7 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -4,7 +4,7 @@
  * Copyright (C) 2005 Linus Torvalds
  *
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "dir.h"
diff --git a/builtin/clean.c b/builtin/clean.c
index 7bdeb01d30e..b2701a28158 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -6,7 +6,7 @@
  * Based on git-clean.sh by Pavel Roskin
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
diff --git a/builtin/clone.c b/builtin/clone.c
index 8ab0453a292..f518bb2dc1f 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -8,7 +8,7 @@
  * Clone a repository into a different directory that does not yet exist.
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
diff --git a/builtin/describe.c b/builtin/describe.c
index 4f4a58eef2b..eea1e330c00 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "lockfile.h"
diff --git a/builtin/diff-files.c b/builtin/diff-files.c
index 1f3deb13c28..b1e1cef7a76 100644
--- a/builtin/diff-files.c
+++ b/builtin/diff-files.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "diff.h"
diff --git a/builtin/diff-index.c b/builtin/diff-index.c
index a178e774a5a..91ad56af177 100644
--- a/builtin/diff-index.c
+++ b/builtin/diff-index.c
@@ -1,4 +1,3 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "diff.h"
diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c
index a724087261d..25b853b85ca 100644
--- a/builtin/diff-tree.c
+++ b/builtin/diff-tree.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "diff.h"
diff --git a/builtin/fsck.c b/builtin/fsck.c
index d6f9a31e216..d207bd909b4 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "repository.h"
diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index b1d98294cdd..452f833ac46 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "run-command.h"
 
diff --git a/builtin/merge-ours.c b/builtin/merge-ours.c
index 5d95acf52a6..284eb486098 100644
--- a/builtin/merge-ours.c
+++ b/builtin/merge-ours.c
@@ -7,7 +7,6 @@
  *
  * Pretend we resolved the heads, but declare our tree trumps everybody else.
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 #include "builtin.h"
 #include "diff.h"
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 345a55755a3..1384008fe09 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -4,7 +4,7 @@
  * Copyright (c) 2018 Pratik Karki
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "run-command.h"
 #include "exec-cmd.h"
diff --git a/builtin/reset.c b/builtin/reset.c
index 5ab03e9616a..52a1a9bd513 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -7,7 +7,7 @@
  *
  * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index bffdd27bd7e..d4c81352a65 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "commit.h"
-- 
2.38.0.1511.gcdcff1f1dc2


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (11 preceding siblings ...)
  2022-11-18 11:31 ` [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
@ 2022-11-18 12:13 ` Phillip Wood
  2022-11-18 12:21   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
  13 siblings, 1 reply; 32+ messages in thread
From: Phillip Wood @ 2022-11-18 12:13 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, git; +Cc: Taylor Blau, Junio C Hamano

Hi Ævar

On 18/11/2022 11:30, Ævar Arnfjörð Bjarmason wrote:
> As 3/12 here notes we've been undergoing a sloooow migration away from
> functions that provide "the_index" for you since 2007, with the last
> major move away from some of them being in 2019.

The commit cited in 3/12 actually introduced most of these macros. What 
it did was remove the dependency on `the_index` from cache.c. Maybe I'm 
missing some context but I struggling to see the benefit in removing the 
macros with out removing the dependency on `the_index`. Doing that would 
require much more manual work as it cannot be automated with coccinelle 
but it would provide a tangible benefit which I don't see here.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS
  2022-11-18 12:13 ` [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Phillip Wood
@ 2022-11-18 12:21   ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 12:21 UTC (permalink / raw)
  To: phillip.wood; +Cc: git, Taylor Blau, Junio C Hamano


On Fri, Nov 18 2022, Phillip Wood wrote:

> Hi Ævar
>
> On 18/11/2022 11:30, Ævar Arnfjörð Bjarmason wrote:
>> As 3/12 here notes we've been undergoing a sloooow migration away from
>> functions that provide "the_index" for you since 2007, with the last
>> major move away from some of them being in 2019.
>
> The commit cited in 3/12 actually introduced most of these
> macros. What it did was remove the dependency on `the_index` from
> cache.c. Maybe I'm missing some context but I struggling to see the
> benefit in removing the macros with out removing the dependency on
> `the_index`. Doing that would require much more manual work as it
> cannot be automated with coccinelle but it would provide a tangible
> benefit which I don't see here.

I didn't cover this myself as I figured the linked-to commits & context
covered it: But no, there is no great benefit to doing this, it's just
janitorial work.

My proximate motivation for this is having written this to libify some
code in a parallel series: [1].

Does that in itself justify this? No, far from it. But we've had this
supposed online migration for years, I tihnk we shouldn't rush to do
those sorts of things needlessly (e.g. let's avoid conflicts with other
in-flight stuff, if it can be helped), but leaving it for another 4-10
years would also suck.

1. https://lore.kernel.org/git/patch-v9-09.12-f29343197eb-20221118T110058Z-avarab@gmail.com/


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH 09/12] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE"
  2022-11-18 11:31 ` [PATCH 09/12] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
@ 2022-11-18 18:41   ` Taylor Blau
  0 siblings, 0 replies; 32+ messages in thread
From: Taylor Blau @ 2022-11-18 18:41 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Junio C Hamano

On Fri, Nov 18, 2022 at 12:31:03PM +0100, Ævar Arnfjörð Bjarmason wrote:
> Split up the "USE_THE_INDEX_COMPATIBILITY_MACROS" into that setting
> and a more narrow "USE_THE_INDEX_VARIABLE". In the case of these
> built-ins we only need "the_index" variable, but not the compatibility
> wrapper for functions we're not using.

OK. So the old "#define USE_THE_INDEX_COMPATIBILITY_MACROS" used to give
you the "the_index" extern, as well as all of the helper macros.

But now, USE_THE_INDEX_VARIABLE gives you just "the_index", and
USE_THE_INDEX_COMPATIBILITY_MACROS gives you that as well as the helper
macros, but you only get the latter if USE_THE_INDEX_VARIABLE is unset.

Makes sense.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH 10/12] cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
  2022-11-18 11:31 ` [PATCH 10/12] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
@ 2022-11-18 18:41   ` Taylor Blau
  0 siblings, 0 replies; 32+ messages in thread
From: Taylor Blau @ 2022-11-18 18:41 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Junio C Hamano

On Fri, Nov 18, 2022 at 12:31:04PM +0100, Ævar Arnfjörð Bjarmason wrote:
> In a preceding commit we fully applied the
> "index-compatibility.pending.cocci" rule to "t/helper/*". Let's now
> stop defining "USE_THE_INDEX_COMPATIBILITY_MACROS" in test-tool.h
> itself, and instead instead move the define to the individual test
> helpers that need it. This mirrors how we do the same thing in the
> "builtin/" directory.
>
> We could start using "USE_THE_INDEX_COMPATIBILITY_MACROS", but let's
> instead introduce a narrow version of it named
> "USE_THE_INDEX_VARIABLE". As the name suggests this doesn't give us
> any of the compatibility macros, but only "the_index" variable.
>
> Eventually we'll move other outstanding users of
> "USE_THE_INDEX_COMPATIBILITY_MACROS" over to using this more narrow
> and descriptive define.

The above two paragraphs look like they belong in the previous commit,
no?

Otherwise the rest of the changes here look reasonable.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS"
  2022-11-18 11:31 ` [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
@ 2022-11-18 18:42   ` Taylor Blau
  2022-11-18 19:27     ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 32+ messages in thread
From: Taylor Blau @ 2022-11-18 18:42 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Junio C Hamano

On Fri, Nov 18, 2022 at 12:31:06PM +0100, Ævar Arnfjörð Bjarmason wrote:
> Follow-up the preceding commit and change
> "USE_THE_INDEX_COMPATIBILITY_MACROS" to the more narrow
> "USE_THE_INDEX_VARIABLE" in cases where we still use "the_index".
>
> Then remove "USE_THE_INDEX_VARIABLE" entirely in the few cases where
> we don't require any compatibility macros or variables anymore.

Could this get squashed into the previous commit? IOW, is there any
reason to keep them separate?

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS"
  2022-11-18 18:42   ` Taylor Blau
@ 2022-11-18 19:27     ` Ævar Arnfjörð Bjarmason
  2022-11-18 23:04       ` Taylor Blau
  0 siblings, 1 reply; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-18 19:27 UTC (permalink / raw)
  To: Taylor Blau; +Cc: git, Junio C Hamano


On Fri, Nov 18 2022, Taylor Blau wrote:

> On Fri, Nov 18, 2022 at 12:31:06PM +0100, Ævar Arnfjörð Bjarmason wrote:
>> Follow-up the preceding commit and change
>> "USE_THE_INDEX_COMPATIBILITY_MACROS" to the more narrow
>> "USE_THE_INDEX_VARIABLE" in cases where we still use "the_index".
>>
>> Then remove "USE_THE_INDEX_VARIABLE" entirely in the few cases where
>> we don't require any compatibility macros or variables anymore.
>
> Could this get squashed into the previous commit? IOW, is there any
> reason to keep them separate?

I figured it would be easier to review if I split out the changes that
are coccinelle-created v.s. the ones I made manually.

But I don't have a preference at all, whatever you think is easier.

This *could* also be a 3 patch series, which I'd prefer, but figured it
would get push-back, so I thought I'd spoon-feed the list.

I.e. just:

 1. Fix discard_index() return value
 2. Add and apply the *.cocci rule to *ALL THE THINGS*"
 3. Narrow down the macro for t/helper/*

At that point we wouldn't need "USE_THE_INDEX_VARIABLE", as we'd have
nothing except "the_index", so "USE_THE_INDEX_COMPATIBILITY_MACROS"
would do exactly what "USE_THE_INDEX_VARIABLE" does in this topic.

(We could still rename it, but I think probably the churn isn't worth it
if it's not a stop-gap to new macro use being re-introduced)

It would rip the band-aid off, there's conflicts, but the --remerge-diff
with "seen" is rather easy, or:

	diff --git a/builtin/commit.c b/builtin/commit.c
	remerge CONFLICT (content): Merge conflict in builtin/commit.c
	index 4fb7604cc24..c7dbdb2a866 100644
	--- a/builtin/commit.c
	+++ b/builtin/commit.c
	@@ -991,16 +991,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
	 		struct object_id oid;
	 		const char *parent = "HEAD";
	 
	-<<<<<<< c23106a1d9f (hi)
	-		if (!the_index.cache_nr && repo_read_index(the_repository) < 0)
	-			die(_("Cannot read index"));
	-=======
	-		if (!active_nr) {
	-			discard_cache();
	-			if (read_cache() < 0)
	+		if (!the_index.cache_nr) {
	+			discard_index(&the_index);
	+			if (repo_read_index(the_repository) < 0)
	 				die(_("Cannot read index"));
	 		}
	->>>>>>> 75ae5b855fb (Merge branch 'rj/branch-copy-and-rename' into seen)
	 
	 		if (amend)
	 			parent = "HEAD^1";
	diff --git a/builtin/read-tree.c b/builtin/read-tree.c
	remerge CONFLICT (content): Merge conflict in builtin/read-tree.c
	index 5dcf0d09ed1..5d6e638c4b1 100644
	--- a/builtin/read-tree.c
	+++ b/builtin/read-tree.c
	@@ -250,15 +250,11 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
	 	if (opts.debug_unpack)
	 		opts.fn = debug_merge;
	 
	-<<<<<<< c23106a1d9f (hi)
	-	cache_tree_free(&the_index.cache_tree);
	-=======
	 	/* If we're going to prime_cache_tree later, skip cache tree update */
	 	if (nr_trees == 1 && !opts.prefix)
	 		opts.skip_cache_tree_update = 1;
	 
	-	cache_tree_free(&active_cache_tree);
	->>>>>>> 75ae5b855fb (Merge branch 'rj/branch-copy-and-rename' into seen)
	+	cache_tree_free(&the_index.cache_tree);
	 	for (i = 0; i < nr_trees; i++) {
	 		struct tree *tree = trees[i];
	 		parse_tree(tree);
	diff --git a/t/helper/test-cache-tree.c b/t/helper/test-cache-tree.c
	index 93051b25f56..5514afdfe7a 100644
	--- a/t/helper/test-cache-tree.c
	+++ b/t/helper/test-cache-tree.c
	@@ -30,7 +30,7 @@ int cmd__cache_tree(int argc, const char **argv)
	 
	 	argc = parse_options(argc, argv, NULL, options, test_cache_tree_usage, 0);
	 
	-	if (read_cache() < 0)
	+	if (repo_read_index(the_repository) < 0)
	 		die(_("unable to read index file"));
	 
	 	oidcpy(&oid, &the_index.cache_tree->oid);
	
...and can also be applied by a "take theirs", re-run the cocci, apply
cocci patch, so no manual resolution needed. Any future conflicts would
be solved with the same procedure.

The diff would be just under ~2k lines, but pretty trivial in terms of
non-cocci changes.

Anyway, just let me know. Right now I'm assuming I'll just re-roll this
(pending any further feedback for a bit) with squashing & some other
minor changes...

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS"
  2022-11-18 19:27     ` Ævar Arnfjörð Bjarmason
@ 2022-11-18 23:04       ` Taylor Blau
  0 siblings, 0 replies; 32+ messages in thread
From: Taylor Blau @ 2022-11-18 23:04 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: Taylor Blau, git, Junio C Hamano

On Fri, Nov 18, 2022 at 08:27:25PM +0100, Ævar Arnfjörð Bjarmason wrote:
>
> On Fri, Nov 18 2022, Taylor Blau wrote:
>
> > On Fri, Nov 18, 2022 at 12:31:06PM +0100, Ævar Arnfjörð Bjarmason wrote:
> >> Follow-up the preceding commit and change
> >> "USE_THE_INDEX_COMPATIBILITY_MACROS" to the more narrow
> >> "USE_THE_INDEX_VARIABLE" in cases where we still use "the_index".
> >>
> >> Then remove "USE_THE_INDEX_VARIABLE" entirely in the few cases where
> >> we don't require any compatibility macros or variables anymore.
> >
> > Could this get squashed into the previous commit? IOW, is there any
> > reason to keep them separate?
>
> I figured it would be easier to review if I split out the changes that
> are coccinelle-created v.s. the ones I made manually.
>
> But I don't have a preference at all, whatever you think is easier.

I think the 11-patch structure makes most sense to me (basically what
you have, with the last couple squashed together).

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH v2 00/11] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS
  2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
                   ` (12 preceding siblings ...)
  2022-11-18 12:13 ` [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Phillip Wood
@ 2022-11-19 13:07 ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 01/11] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
                     ` (10 more replies)
  13 siblings, 11 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

A re-roll that should address feedback on v1[1]. Change since v1:

* Since "vd/skip-cache-tree-update" has landed on "master" the
  "test-tool.h" can stop providing
  "USE_THE_INDEX_COMPATIBILITY_MACROS" to the test tools, which we're
  unlikely to have a semantic conflict for while this graduates.

  A change in 7/12 is due to rebasing on that topic, ditto applying
  the transformation in 8/12 to code it added.

* The 11/12 is now quite a bit larger, and now does the
  transformations that don't conflict with anything in-flight.
  I.e. (aside from needing to eject v1 of this topic) it'll merge
  cleanly with "seen"

  Hopfully that expanded scope isn't stepping too much outside the
  bounds here. But once "vd/skip-cache-tree-update" had landed just
  applying it to the rest that didn't conflict didn't seem too
  bad. It's a relatively large diffstat, but not that much on top of
  the v1, and it's almost entirely a change coccinelle is making".

1. https://lore.kernel.org/git/cover-00.12-00000000000-20221118T112205Z-avarab@gmail.com/

Branch & passing CI at:
https://github.com/avar/git/tree/avar/narrow-down-the-index-compatibilty-macros-2

Ævar Arnfjörð Bjarmason (11):
  cache.h: remove unused "the_index" compat macros
  builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS"
  cocci & cache.h: remove rarely used "the_index" compat macros
  read-cache API & users: make discard_index() return void
  cocci: add a index-compatibility.pending.cocci
  cocci & cache.h: apply a selection of "pending" index-compatibility
  cocci & cache.h: apply variable section of "pending"
    index-compatibility
  cocci: apply "pending" index-compatibility to "t/helper/*.c"
  {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE"
  cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
  cocci: apply "pending" index-compatibility to some "builtin/*.c"

 add-interactive.c                             |   8 +-
 add-patch.c                                   |   3 +-
 builtin/add.c                                 |  21 +--
 builtin/am.c                                  |  25 ++--
 builtin/cat-file.c                            |   2 +-
 builtin/check-attr.c                          |   4 +-
 builtin/check-ignore.c                        |   4 +-
 builtin/checkout-index.c                      |  19 +--
 builtin/checkout.c                            |  67 ++++-----
 builtin/clean.c                               |   6 +-
 builtin/clone.c                               |   4 +-
 builtin/commit.c                              |  60 ++++----
 builtin/describe.c                            |   7 +-
 builtin/diff-files.c                          |   5 +-
 builtin/diff-index.c                          |   9 +-
 builtin/diff-tree.c                           |   4 +-
 builtin/diff.c                                |  25 ++--
 builtin/difftool.c                            |   2 +-
 builtin/fsck.c                                |  16 +--
 builtin/grep.c                                |   1 -
 builtin/log.c                                 |   1 -
 builtin/merge-index.c                         |  16 +--
 builtin/merge-ours.c                          |   3 +-
 builtin/merge-tree.c                          |   2 +-
 builtin/merge.c                               |  32 +++--
 builtin/mv.c                                  |  41 +++---
 builtin/pull.c                                |   6 +-
 builtin/read-tree.c                           |  12 +-
 builtin/rebase.c                              |   6 +-
 builtin/reset.c                               |  20 +--
 builtin/rev-parse.c                           |   4 +-
 builtin/rm.c                                  |  26 ++--
 builtin/stash.c                               |  43 +++---
 builtin/submodule--helper.c                   |  28 ++--
 builtin/update-index.c                        |  91 ++++++------
 cache.h                                       |  36 +----
 contrib/coccinelle/index-compatibility.cocci  | 135 ++++++++++++++++++
 .../index-compatibility.pending.cocci         |  24 ++++
 read-cache.c                                  |   4 +-
 repository.c                                  |   2 +-
 sequencer.c                                   |  15 +-
 t/helper/test-cache-tree.c                    |   3 +-
 t/helper/test-dump-cache-tree.c               |   5 +-
 t/helper/test-dump-split-index.c              |   1 +
 t/helper/test-dump-untracked-cache.c          |   4 +-
 t/helper/test-fast-rebase.c                   |   4 +-
 t/helper/test-lazy-init-name-hash.c           |  21 +--
 t/helper/test-read-cache.c                    |   5 +-
 t/helper/test-scrap-cache-tree.c              |   9 +-
 t/helper/test-tool.h                          |   1 -
 t/helper/test-write-cache.c                   |   6 +-
 51 files changed, 532 insertions(+), 366 deletions(-)
 create mode 100644 contrib/coccinelle/index-compatibility.cocci
 create mode 100644 contrib/coccinelle/index-compatibility.pending.cocci

Range-diff against v1:
 1:  760b4ddf06a =  1:  3ddae1f5886 cache.h: remove unused "the_index" compat macros
 2:  9789509a902 =  2:  3dd3af7b7bb builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS"
 3:  faa7757cb32 =  3:  5c2776a28f7 cocci & cache.h: remove rarely used "the_index" compat macros
 4:  362a14a0539 =  4:  b4d6685585a read-cache API & users: make discard_index() return void
 5:  14bf6e7188c =  5:  ec740a81d9f cocci: add a index-compatibility.pending.cocci
 6:  dbf14ed23e6 =  6:  6849233bf2b cocci & cache.h: apply a selection of "pending" index-compatibility
 7:  bf507c79d07 !  7:  fff2d0459a0 cocci & cache.h: apply variable section of "pending" index-compatibility
    @@ Commit message
         In doing so move more of "index-compatibility.pending.cocci" to
         "index-compatibility.cocci".
     
    -    In the case of "active_cache_tree" we'd have a textual conflict with
    -    "vd/skip-cache-tree-update" in "next"[1], and in the case of
    -    "active_nr" with "ab/various-leak-fixes" in "seen". Let's exclude
    -    those specific cases while moving the rule over from "pending".
    +    In the case of "active_nr" we'd have a textual conflict with
    +    "ab/various-leak-fixes" in "next"[1]. Let's exclude that specific case
    +    while moving the rule over from "pending".
     
    -    1. dc5d40f5bc4 (read-tree: use 'skip_cache_tree_update' option,
    -       2022-11-10)
    +    1. 407b94280f8 (commit: discard partial cache before (re-)reading it,
    +       2022-11-08)
     
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
    @@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
      				/*
      				 * if src is clean, it will suffice to remove it
     
    + ## builtin/read-tree.c ##
    +@@ builtin/read-tree.c: int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
    + 	if (nr_trees == 1 && !opts.prefix)
    + 		opts.skip_cache_tree_update = 1;
    + 
    +-	cache_tree_free(&active_cache_tree);
    ++	cache_tree_free(&the_index.cache_tree);
    + 	for (i = 0; i < nr_trees; i++) {
    + 		struct tree *tree = trees[i];
    + 		parse_tree(tree);
    +
      ## builtin/reset.c ##
     @@ builtin/reset.c: static void update_index_from_diff(struct diff_queue_struct *q,
      		 * to properly construct the reset sparse directory.
    @@ cache.h: void prefetch_cache_entries(const struct index_state *istate,
     -#define active_cache (the_index.cache)
      #define active_nr (the_index.cache_nr)
     -#define active_cache_changed (the_index.cache_changed)
    - #define active_cache_tree (the_index.cache_tree)
    +-#define active_cache_tree (the_index.cache_tree)
      
      #define read_cache() repo_read_index(the_repository)
    + #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
     
      ## contrib/coccinelle/index-compatibility.cocci ##
     @@
    @@ contrib/coccinelle/index-compatibility.cocci
     +|
     +- active_cache_changed
     ++ the_index.cache_changed
    ++|
    ++- active_cache_tree
    +++ the_index.cache_tree
     +)
     +
     +@@
    @@ contrib/coccinelle/index-compatibility.cocci
     +- active_nr
     ++ the_index.cache_nr
     +  ...>}
    -+
    -+@@
    -+identifier f != cmd_read_tree;
    -+@@
    -+  f(...) {<...
    -+- active_cache_tree
    -++ the_index.cache_tree
    -+  ...>}
     +
      // "the_repository" simple cases
      @@
 8:  f3e265e6edb !  8:  6d57a383623 cocci: apply "pending" index-compatibility to "t/helper/*.c"
    @@ Commit message
     
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
    + ## t/helper/test-cache-tree.c ##
    +@@ t/helper/test-cache-tree.c: int cmd__cache_tree(int argc, const char **argv)
    + 
    + 	argc = parse_options(argc, argv, NULL, options, test_cache_tree_usage, 0);
    + 
    +-	if (read_cache() < 0)
    ++	if (repo_read_index(the_repository) < 0)
    + 		die(_("unable to read index file"));
    + 
    + 	oidcpy(&oid, &the_index.cache_tree->oid);
    +
      ## t/helper/test-dump-cache-tree.c ##
     @@ t/helper/test-dump-cache-tree.c: int cmd__dump_cache_tree(int ac, const char **av)
      	int ret;
 9:  5b69cfa17ad !  9:  429eecc78ec {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE"
    @@ Commit message
         built-ins we only need "the_index" variable, but not the compatibility
         wrapper for functions we're not using.
     
    +    Let's then have some users of "USE_THE_INDEX_COMPATIBILITY_MACROS" use
    +    this more narrow and descriptive define.
    +
    +    For context: The USE_THE_INDEX_COMPATIBILITY_MACROS macro was added to
    +    test-tool.h in f8adbec9fea (cache.h: flip
    +    NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24).
    +
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
      ## builtin/cat-file.c ##
    @@ cache.h: typedef int (*must_prefetch_predicate)(const struct cache_entry *);
     +#ifndef USE_THE_INDEX_VARIABLE
     +#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
      #define active_nr (the_index.cache_nr)
    - #define active_cache_tree (the_index.cache_tree)
      
    + #define read_cache() repo_read_index(the_repository)
     @@ cache.h: extern struct index_state the_index;
      #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
      #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
10:  9eda7916e75 ! 10:  5456859b244 cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
    @@ Commit message
         cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
     
         In a preceding commit we fully applied the
    -    "index-compatibility.pending.cocci" rule to "t/helper/*". Let's now
    -    stop defining "USE_THE_INDEX_COMPATIBILITY_MACROS" in test-tool.h
    -    itself, and instead instead move the define to the individual test
    -    helpers that need it. This mirrors how we do the same thing in the
    -    "builtin/" directory.
    +    "index-compatibility.pending.cocci" rule to "t/helper/*".
     
    -    We could start using "USE_THE_INDEX_COMPATIBILITY_MACROS", but let's
    -    instead introduce a narrow version of it named
    -    "USE_THE_INDEX_VARIABLE". As the name suggests this doesn't give us
    -    any of the compatibility macros, but only "the_index" variable.
    -
    -    Eventually we'll move other outstanding users of
    -    "USE_THE_INDEX_COMPATIBILITY_MACROS" over to using this more narrow
    -    and descriptive define.
    -
    -    For context: The USE_THE_INDEX_COMPATIBILITY_MACROS macro was added to
    -    test-tool.h in f8adbec9fea (cache.h: flip
    -    NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24).
    +    Let's now stop defining "USE_THE_INDEX_COMPATIBILITY_MACROS" in
    +    test-tool.h itself, and instead instead define
    +    "USE_THE_INDEX_VARIABLE" in the individual test helpers that need
    +    it. This mirrors how we do the same thing in the "builtin/" directory.
     
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
    + ## t/helper/test-cache-tree.c ##
    +@@
    ++#define USE_THE_INDEX_VARIABLE
    + #include "test-tool.h"
    + #include "cache.h"
    + #include "tree.h"
    +
      ## t/helper/test-dump-cache-tree.c ##
     @@
     +#define USE_THE_INDEX_VARIABLE
    @@ t/helper/test-scrap-cache-tree.c
      #include "cache.h"
      #include "lockfile.h"
     
    + ## t/helper/test-tool.h ##
    +@@
    + #ifndef TEST_TOOL_H
    + #define TEST_TOOL_H
    + 
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    + #include "git-compat-util.h"
    + 
    + int cmd__advise_if_enabled(int argc, const char **argv);
    +
      ## t/helper/test-write-cache.c ##
     @@
     +#define USE_THE_INDEX_VARIABLE
11:  3cb15ff40f6 ! 11:  b645cec189e cocci: apply "pending" index-compatibility to some "builtin/*.c"
    @@ Metadata
      ## Commit message ##
         cocci: apply "pending" index-compatibility to some "builtin/*.c"
     
    -    Apply "index-compatibility.pending.cocci" rule to those "builtin/*",
    -    and commit the ones that have 3 uses or less of the relevant
    -    macros.
    +    Apply "index-compatibility.pending.cocci" rule to "builtin/*", but
    +    exclude those where we conflict with in-flight changes.
    +
    +    As a result some of them end up using only "the_index", so let's have
    +    them use the more narrow "USE_THE_INDEX_VARIABLE" rather than
    +    "USE_THE_INDEX_COMPATIBILITY_MACROS".
    +
    +    Manual changes not made by coccinelle, that were squashed in:
    +
    +    * Whitespace-wrap argument lists for repo_hold_locked_index(),
    +      repo_read_index_preload() and repo_refresh_and_write_index(), in cases
    +      where the line became too long after the transformation.
    +    * Change "refresh_cache()" to "refresh_index()" in a comment in
    +      "builtin/update-index.c".
    +    * For those whose call was followed by perror("<macro-name>"), change
    +      it to perror("<function-name>"), referring to the new function.
     
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
      ## builtin/add.c ##
    +@@
    +  *
    +  * Copyright (C) 2006 Linus Torvalds
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "cache.h"
    + #include "config.h"
    + #include "builtin.h"
     @@ builtin/add.c: static int edit_patch(int argc, const char **argv, const char *prefix)
      
      	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
    @@ builtin/add.c: int cmd_add(int argc, const char **argv, const char *prefix)
      
      	die_in_unpopulated_submodule(&the_index, prefix);
     
    + ## builtin/am.c ##
    +@@ builtin/am.c: static int run_apply(const struct am_state *state, const char *index_file)
    + 
    + 	if (index_file) {
    + 		/* Reload index as apply_all_patches() will have modified it. */
    +-		discard_cache();
    +-		read_cache_from(index_file);
    ++		discard_index(&the_index);
    ++		read_index_from(&the_index, index_file, get_git_dir());
    + 	}
    + 
    + 	return 0;
    +@@ builtin/am.c: static int fall_back_threeway(const struct am_state *state, const char *index_pa
    + 	if (build_fake_ancestor(state, index_path))
    + 		return error("could not build fake ancestor");
    + 
    +-	discard_cache();
    +-	read_cache_from(index_path);
    ++	discard_index(&the_index);
    ++	read_index_from(&the_index, index_path, get_git_dir());
    + 
    + 	if (write_index_as_tree(&orig_tree, &the_index, index_path, 0, NULL))
    + 		return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
    +@@ builtin/am.c: static int fall_back_threeway(const struct am_state *state, const char *index_pa
    + 
    + 	say(state, stdout, _("Falling back to patching base and 3-way merge..."));
    + 
    +-	discard_cache();
    +-	read_cache();
    ++	discard_index(&the_index);
    ++	repo_read_index(the_repository);
    + 
    + 	/*
    + 	 * This is not so wrong. Depending on which base we picked, orig_tree
    +@@ builtin/am.c: static void am_run(struct am_state *state, int resume)
    + 
    + 	unlink(am_path(state, "dirtyindex"));
    + 
    +-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0)
    ++	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
    ++					 NULL, NULL, NULL) < 0)
    + 		die(_("unable to write index file"));
    + 
    + 	if (repo_index_has_changes(the_repository, NULL, &sb)) {
    +@@ builtin/am.c: static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
    + 	if (parse_tree(head) || parse_tree(remote))
    + 		return -1;
    + 
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    + 
    +-	refresh_cache(REFRESH_QUIET);
    ++	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
    + 
    + 	memset(&opts, 0, sizeof(opts));
    + 	opts.head_idx = 1;
    +@@ builtin/am.c: static int merge_tree(struct tree *tree)
    + 	if (parse_tree(tree))
    + 		return -1;
    + 
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    + 
    + 	memset(&opts, 0, sizeof(opts));
    + 	opts.head_idx = 1;
    +
      ## builtin/check-attr.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "cache.h"
    + #include "config.h"
     @@ builtin/check-attr.c: int cmd_check_attr(int argc, const char **argv, const char *prefix)
      	argc = parse_options(argc, argv, prefix, check_attr_options,
      			     check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
    @@ builtin/check-attr.c: int cmd_check_attr(int argc, const char **argv, const char
      
     
      ## builtin/check-ignore.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "cache.h"
    + #include "config.h"
     @@ builtin/check-ignore.c: int cmd_check_ignore(int argc, const char **argv, const char *prefix)
      		die(_("--non-matching is only valid with --verbose"));
      
    @@ builtin/check-ignore.c: int cmd_check_ignore(int argc, const char **argv, const
      	setup_standard_excludes(&dir);
     
      ## builtin/checkout-index.c ##
    +@@
    +  * Copyright (C) 2005 Linus Torvalds
    +  *
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "config.h"
    + #include "dir.h"
     @@ builtin/checkout-index.c: static void write_tempfile_record(const char *name, const char *prefix)
      static int checkout_file(const char *name, const char *prefix)
      {
    @@ builtin/checkout-index.c: int cmd_checkout_index(int argc, const char **argv, co
      
      	get_parallel_checkout_configs(&pc_workers, &pc_threshold);
     
    + ## builtin/checkout.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "advice.h"
    + #include "blob.h"
    +@@ builtin/checkout.c: static int update_some(const struct object_id *oid, struct strbuf *base,
    + 	 * entry in place. Whether it is UPTODATE or not, checkout_entry will
    + 	 * do the right thing.
    + 	 */
    +-	pos = cache_name_pos(ce->name, ce->ce_namelen);
    ++	pos = index_name_pos(&the_index, ce->name, ce->ce_namelen);
    + 	if (pos >= 0) {
    + 		struct cache_entry *old = the_index.cache[pos];
    + 		if (ce->ce_mode == old->ce_mode &&
    +@@ builtin/checkout.c: static int checkout_paths(const struct checkout_opts *opts,
    + 	}
    + 
    + 	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    +-	if (read_cache_preload(&opts->pathspec) < 0)
    ++	if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0)
    + 		return error(_("index file corrupt"));
    + 
    + 	if (opts->source_tree)
    +@@ builtin/checkout.c: static int merge_working_tree(const struct checkout_opts *opts,
    + 	struct lock_file lock_file = LOCK_INIT;
    + 	struct tree *new_tree;
    + 
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    +-	if (read_cache_preload(NULL) < 0)
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    ++	if (repo_read_index_preload(the_repository, NULL, 0) < 0)
    + 		return error(_("index file corrupt"));
    + 
    + 	resolve_undo_clear_index(&the_index);
    +@@ builtin/checkout.c: static int merge_working_tree(const struct checkout_opts *opts,
    + 		struct unpack_trees_options topts;
    + 		const struct object_id *old_commit_oid;
    + 
    +-		refresh_cache(REFRESH_QUIET);
    ++		refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
    + 
    + 		if (unmerged_index(&the_index)) {
    + 			error(_("you need to resolve your current index first"));
    +
      ## builtin/clean.c ##
    +@@
    +  * Based on git-clean.sh by Pavel Roskin
    +  */
    + 
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "cache.h"
    + #include "config.h"
     @@ builtin/clean.c: int cmd_clean(int argc, const char **argv, const char *prefix)
      	prepare_repo_settings(the_repository);
      	the_repository->settings.command_requires_full_index = 0;
    @@ builtin/clean.c: int cmd_clean(int argc, const char **argv, const char *prefix)
      	pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
     
      ## builtin/clone.c ##
    +@@
    +  * Clone a repository into a different directory that does not yet exist.
    +  */
    + 
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "config.h"
    + #include "lockfile.h"
     @@ builtin/clone.c: static int checkout(int submodule_progress, int filter_submodules)
      	/* We need to be in the new work tree for the checkout */
      	setup_work_tree();
    @@ builtin/clone.c: static int checkout(int submodule_progress, int filter_submodul
      	memset(&opts, 0, sizeof opts);
      	opts.update = 1;
     
    + ## builtin/commit.c ##
    +@@ builtin/commit.c: static void create_base_index(const struct commit *current_head)
    + 	struct tree_desc t;
    + 
    + 	if (!current_head) {
    +-		discard_cache();
    ++		discard_index(&the_index);
    + 		return;
    + 	}
    + 
    +@@ builtin/commit.c: static void refresh_cache_or_die(int refresh_flags)
    + 	 * refresh_flags contains REFRESH_QUIET, so the only errors
    + 	 * are for unmerged entries.
    + 	 */
    +-	if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
    ++	if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
    + 		die_resolve_conflict("commit");
    + }
    + 
    +@@ builtin/commit.c: static const char *prepare_index(const char **argv, const char *prefix,
    + 	    (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
    + 		die(_("No paths with --include/--only does not make sense."));
    + 
    +-	if (read_cache_preload(&pathspec) < 0)
    ++	if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
    + 		die(_("index file corrupt"));
    + 
    + 	if (interactive) {
    + 		char *old_index_env = NULL, *old_repo_index_file;
    +-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
    ++		repo_hold_locked_index(the_repository, &index_lock,
    ++				       LOCK_DIE_ON_ERROR);
    + 
    + 		refresh_cache_or_die(refresh_flags);
    + 
    +@@ builtin/commit.c: static const char *prepare_index(const char **argv, const char *prefix,
    + 			unsetenv(INDEX_ENVIRONMENT);
    + 		FREE_AND_NULL(old_index_env);
    + 
    +-		discard_cache();
    +-		read_cache_from(get_lock_file_path(&index_lock));
    ++		discard_index(&the_index);
    ++		read_index_from(&the_index, get_lock_file_path(&index_lock),
    ++				get_git_dir());
    + 		if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
    + 			if (reopen_lock_file(&index_lock) < 0)
    + 				die(_("unable to write index file"));
    +@@ builtin/commit.c: static const char *prepare_index(const char **argv, const char *prefix,
    + 	 * (B) on failure, rollback the real index.
    + 	 */
    + 	if (all || (also && pathspec.nr)) {
    +-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
    ++		repo_hold_locked_index(the_repository, &index_lock,
    ++				       LOCK_DIE_ON_ERROR);
    + 		add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
    + 		refresh_cache_or_die(refresh_flags);
    + 		update_main_cache_tree(WRITE_TREE_SILENT);
    +@@ builtin/commit.c: static const char *prepare_index(const char **argv, const char *prefix,
    + 	 * We still need to refresh the index here.
    + 	 */
    + 	if (!only && !pathspec.nr) {
    +-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
    ++		repo_hold_locked_index(the_repository, &index_lock,
    ++				       LOCK_DIE_ON_ERROR);
    + 		refresh_cache_or_die(refresh_flags);
    + 		if (the_index.cache_changed
    + 		    || !cache_tree_fully_valid(the_index.cache_tree))
    +@@ builtin/commit.c: static const char *prepare_index(const char **argv, const char *prefix,
    + 	if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
    + 		exit(1);
    + 
    +-	discard_cache();
    +-	if (read_cache() < 0)
    ++	discard_index(&the_index);
    ++	if (repo_read_index(the_repository) < 0)
    + 		die(_("cannot read the index"));
    + 
    +-	hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
    ++	repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
    + 	add_remove_files(&partial);
    +-	refresh_cache(REFRESH_QUIET);
    ++	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
    + 	update_main_cache_tree(WRITE_TREE_SILENT);
    + 	if (write_locked_index(&the_index, &index_lock, 0))
    + 		die(_("unable to write new_index file"));
    +@@ builtin/commit.c: static const char *prepare_index(const char **argv, const char *prefix,
    + 
    + 	create_base_index(current_head);
    + 	add_remove_files(&partial);
    +-	refresh_cache(REFRESH_QUIET);
    ++	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
    + 
    + 	if (write_locked_index(&the_index, &false_lock, 0))
    + 		die(_("unable to write temporary index file"));
    + 
    +-	discard_cache();
    ++	discard_index(&the_index);
    + 	ret = get_lock_file_path(&false_lock);
    +-	read_cache_from(ret);
    ++	read_index_from(&the_index, ret, get_git_dir());
    + out:
    + 	string_list_clear(&partial, 0);
    + 	clear_pathspec(&pathspec);
    +@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
    + 		 * and could have updated it. We must do this before we invoke
    + 		 * the editor and after we invoke run_status above.
    + 		 */
    +-		discard_cache();
    ++		discard_index(&the_index);
    + 	}
    +-	read_cache_from(index_file);
    ++	read_index_from(&the_index, index_file, get_git_dir());
    + 
    + 	if (update_main_cache_tree(0)) {
    + 		error(_("Error building trees"));
    +@@ builtin/commit.c: int cmd_status(int argc, const char **argv, const char *prefix)
    + 		      &s.pathspec, NULL, NULL);
    + 
    + 	if (use_optional_locks())
    +-		fd = hold_locked_index(&index_lock, 0);
    ++		fd = repo_hold_locked_index(the_repository, &index_lock, 0);
    + 	else
    + 		fd = -1;
    + 
    +
      ## builtin/describe.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "cache.h"
    + #include "config.h"
    + #include "lockfile.h"
     @@ builtin/describe.c: int cmd_describe(int argc, const char **argv, const char *prefix)
      			int fd, result;
      
    @@ builtin/describe.c: int cmd_describe(int argc, const char **argv, const char *pr
      
     
      ## builtin/diff-files.c ##
    +@@
    +  *
    +  * Copyright (C) Linus Torvalds, 2005
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    + #include "cache.h"
    + #include "config.h"
    + #include "diff.h"
     @@ builtin/diff-files.c: int cmd_diff_files(int argc, const char **argv, const char *prefix)
      	    (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
      		diff_merges_set_dense_combined_if_unset(&rev);
      
     -	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
    +-		perror("read_cache_preload");
     +	if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
    - 		perror("read_cache_preload");
    ++		perror("repo_read_index_preload");
      		result = -1;
      		goto cleanup;
    + 	}
     
      ## builtin/diff-index.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    + #include "cache.h"
    + #include "config.h"
    + #include "diff.h"
     @@ builtin/diff-index.c: int cmd_diff_index(int argc, const char **argv, const char *prefix)
      		usage(diff_cache_usage);
      	if (!(option & DIFF_INDEX_CACHED)) {
      		setup_work_tree();
     -		if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
    +-			perror("read_cache_preload");
     +		if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
    - 			perror("read_cache_preload");
    ++			perror("repo_read_index_preload");
      			return -1;
      		}
     -	} else if (read_cache() < 0) {
    +-		perror("read_cache");
     +	} else if (repo_read_index(the_repository) < 0) {
    - 		perror("read_cache");
    ++		perror("repo_read_index");
      		return -1;
      	}
    + 	result = run_diff_index(&rev, option);
     
      ## builtin/diff-tree.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "cache.h"
    + #include "config.h"
    + #include "diff.h"
     @@ builtin/diff-tree.c: int cmd_diff_tree(int argc, const char **argv, const char *prefix)
      
      	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
    @@ builtin/diff-tree.c: int cmd_diff_tree(int argc, const char **argv, const char *
      	opt->abbrev = 0;
      	opt->diff = 1;
     
    + ## builtin/diff.c ##
    +@@
    +  *
    +  * Copyright (c) 2006 Junio C Hamano
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "cache.h"
    + #include "config.h"
    + #include "ewah/ewok.h"
    +@@ builtin/diff.c: static int builtin_diff_index(struct rev_info *revs,
    + 		usage(builtin_diff_usage);
    + 	if (!(option & DIFF_INDEX_CACHED)) {
    + 		setup_work_tree();
    +-		if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
    +-			perror("read_cache_preload");
    ++		if (repo_read_index_preload(the_repository,
    ++					    &revs->diffopt.pathspec, 0) < 0) {
    ++			perror("repo_read_index_preload");
    + 			return -1;
    + 		}
    +-	} else if (read_cache() < 0) {
    +-		perror("read_cache");
    ++	} else if (repo_read_index(the_repository) < 0) {
    ++		perror("repo_read_cache");
    + 		return -1;
    + 	}
    + 	return run_diff_index(revs, option);
    +@@ builtin/diff.c: static void refresh_index_quietly(void)
    + 	struct lock_file lock_file = LOCK_INIT;
    + 	int fd;
    + 
    +-	fd = hold_locked_index(&lock_file, 0);
    ++	fd = repo_hold_locked_index(the_repository, &lock_file, 0);
    + 	if (fd < 0)
    + 		return;
    +-	discard_cache();
    +-	read_cache();
    +-	refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
    ++	discard_index(&the_index);
    ++	repo_read_index(the_repository);
    ++	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
    ++		      NULL);
    + 	repo_update_index_if_able(the_repository, &lock_file);
    + }
    + 
    +@@ builtin/diff.c: static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
    + 		diff_merges_set_dense_combined_if_unset(revs);
    + 
    + 	setup_work_tree();
    +-	if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
    +-		perror("read_cache_preload");
    ++	if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
    ++				    0) < 0) {
    ++		perror("repo_read_index_preload");
    + 		return -1;
    + 	}
    + 	return run_diff_files(revs, options);
    +
      ## builtin/fsck.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "cache.h"
    + #include "repository.h"
     @@ builtin/fsck.c: int cmd_fsck(int argc, const char **argv, const char *prefix)
      	if (keep_cache_objects) {
      		verify_index_checksum = 1;
    @@ builtin/fsck.c: int cmd_fsck(int argc, const char **argv, const char *prefix)
      		for (i = 0; i < the_index.cache_nr; i++) {
     
      ## builtin/merge-index.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "run-command.h"
    + 
     @@ builtin/merge-index.c: static int merge_entry(int pos, const char *path)
      
      static void merge_one_path(const char *path)
    @@ builtin/merge-index.c: int cmd_merge_index(int argc, const char **argv, const ch
      	ensure_full_index(&the_index);
     
      ## builtin/merge-ours.c ##
    +@@
    +  *
    +  * Pretend we resolved the heads, but declare our tree trumps everybody else.
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    + #include "git-compat-util.h"
    + #include "builtin.h"
    + #include "diff.h"
     @@ builtin/merge-ours.c: int cmd_merge_ours(int argc, const char **argv, const char *prefix)
      	 * commit.  The index must match HEAD, or this merge cannot go
      	 * through.
    @@ builtin/merge-ours.c: int cmd_merge_ours(int argc, const char **argv, const char
      	if (index_differs_from(the_repository, "HEAD", NULL, 0))
      		return 2;
     
    + ## builtin/merge.c ##
    +@@ builtin/merge.c: static int save_state(struct object_id *stash)
    + 	int rc = -1;
    + 
    + 	fd = repo_hold_locked_index(the_repository, &lock_file, 0);
    +-	refresh_cache(REFRESH_QUIET);
    ++	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
    + 	if (0 <= fd)
    + 		repo_update_index_if_able(the_repository, &lock_file);
    + 	rollback_lock_file(&lock_file);
    +@@ builtin/merge.c: static int try_merge_strategy(const char *strategy, struct commit_list *common,
    + {
    + 	const char *head_arg = "HEAD";
    + 
    +-	if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
    ++	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
    ++					 SKIP_IF_UNCHANGED, 0, NULL, NULL,
    ++					 NULL) < 0)
    + 		return error(_("Unable to write index."));
    + 
    + 	if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") ||
    +@@ builtin/merge.c: static int try_merge_strategy(const char *strategy, struct commit_list *common,
    + 		for (j = common; j; j = j->next)
    + 			commit_list_insert(j->item, &reversed);
    + 
    +-		hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
    ++		repo_hold_locked_index(the_repository, &lock,
    ++				       LOCK_DIE_ON_ERROR);
    + 		if (!strcmp(strategy, "ort"))
    + 			clean = merge_ort_recursive(&o, head, remoteheads->item,
    + 						    reversed, &result);
    +@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
    + 		 * the editor and after we invoke run_status above.
    + 		 */
    + 		if (invoked_hook)
    +-			discard_cache();
    ++			discard_index(&the_index);
    + 	}
    +-	read_cache_from(index_file);
    ++	read_index_from(&the_index, index_file, get_git_dir());
    + 	strbuf_addbuf(&msg, &merge_msg);
    + 	if (squash)
    + 		BUG("the control must not reach here under --squash");
    +@@ builtin/merge.c: static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
    + 	struct object_id result_tree, result_commit;
    + 	struct commit_list *parents, **pptr = &parents;
    + 
    +-	if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
    ++	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
    ++					 SKIP_IF_UNCHANGED, 0, NULL, NULL,
    ++					 NULL) < 0)
    + 		return error(_("Unable to write index."));
    + 
    + 	write_tree_trivial(&result_tree);
    +@@ builtin/merge.c: int cmd_merge(int argc, const char **argv, const char *prefix)
    + 		 * We are not doing octopus, not fast-forward, and have
    + 		 * only one common.
    + 		 */
    +-		refresh_cache(REFRESH_QUIET);
    ++		refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
    + 		if (allow_trivial && fast_forward != FF_ONLY) {
    + 			/*
    + 			 * Must first ensure that index matches HEAD before
    +
    + ## builtin/mv.c ##
    +@@ builtin/mv.c: static int index_range_of_same_dir(const char *src, int length,
    + 	const char *src_w_slash = add_slash(src);
    + 	int first, last, len_w_slash = length + 1;
    + 
    +-	first = cache_name_pos(src_w_slash, len_w_slash);
    ++	first = index_name_pos(&the_index, src_w_slash, len_w_slash);
    + 	if (first >= 0)
    + 		die(_("%.*s is in index"), len_w_slash, src_w_slash);
    + 
    +@@ builtin/mv.c: static int empty_dir_has_sparse_contents(const char *name)
    + 	const char *with_slash = add_slash(name);
    + 	int length = strlen(with_slash);
    + 
    +-	int pos = cache_name_pos(with_slash, length);
    ++	int pos = index_name_pos(&the_index, with_slash, length);
    + 	const struct cache_entry *ce;
    + 
    + 	if (pos < 0) {
    +@@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
    + 	if (--argc < 1)
    + 		usage_with_options(builtin_mv_usage, builtin_mv_options);
    + 
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    +-	if (read_cache() < 0)
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    ++	if (repo_read_index(the_repository) < 0)
    + 		die(_("index file corrupt"));
    + 
    + 	source = internal_prefix_pathspec(prefix, argv, argc, 0);
    +@@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
    + 			int pos;
    + 			const struct cache_entry *ce;
    + 
    +-			pos = cache_name_pos(src, length);
    ++			pos = index_name_pos(&the_index, src, length);
    + 			if (pos < 0) {
    + 				const char *src_w_slash = add_slash(src);
    + 				if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
    +@@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
    + 				goto act_on_entry;
    + 			}
    + 			/* Check if dst exists in index */
    +-			if (cache_name_pos(dst, strlen(dst)) < 0) {
    ++			if (index_name_pos(&the_index, dst, strlen(dst)) < 0) {
    + 				modes[i] |= SPARSE;
    + 				goto act_on_entry;
    + 			}
    +@@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
    + dir_check:
    + 		if (S_ISDIR(st.st_mode)) {
    + 			int j, dst_len, n;
    +-			int first = cache_name_pos(src, length), last;
    ++			int first = index_name_pos(&the_index, src, length), last;
    + 
    + 			if (first >= 0) {
    + 				prepare_move_submodule(src, first,
    +@@ builtin/mv.c: int cmd_mv(int argc, const char **argv, const char *prefix)
    + 		if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
    + 			continue;
    + 
    +-		pos = cache_name_pos(src, strlen(src));
    ++		pos = index_name_pos(&the_index, src, strlen(src));
    + 		assert(pos >= 0);
    + 		if (!(mode & SPARSE) && !lstat(src, &st))
    + 			sparse_and_dirty = ie_modified(&the_index,
    +
    + ## builtin/read-tree.c ##
    +@@
    +  * Copyright (C) Linus Torvalds, 2005
    +  */
    + 
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "cache.h"
    + #include "config.h"
    + #include "lockfile.h"
    +@@ builtin/read-tree.c: int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
    + 	prepare_repo_settings(the_repository);
    + 	the_repository->settings.command_requires_full_index = 0;
    + 
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    + 
    + 	/*
    + 	 * NEEDSWORK
    +
      ## builtin/rebase.c ##
    +@@
    +  * Copyright (c) 2018 Pratik Karki
    +  */
    + 
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "run-command.h"
    + #include "exec-cmd.h"
     @@ builtin/rebase.c: static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
      	if (ret)
      		error(_("could not generate todo list"));
    @@ builtin/rebase.c: int cmd_rebase(int argc, const char **argv, const char *prefix
      		refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
     
      ## builtin/reset.c ##
    +@@
    +  *
    +  * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "config.h"
    + #include "lockfile.h"
     @@ builtin/reset.c: static void update_index_from_diff(struct diff_queue_struct *q,
      		 * if this entry is outside the sparse cone - this is necessary
      		 * to properly construct the reset sparse directory.
    @@ builtin/reset.c: int cmd_reset(int argc, const char **argv, const char *prefix)
      			if (read_from_tree(&pathspec, &oid, intent_to_add))
     
      ## builtin/rev-parse.c ##
    +@@
    +  *
    +  * Copyright (C) Linus Torvalds, 2005
    +  */
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "cache.h"
    + #include "config.h"
    + #include "commit.h"
     @@ builtin/rev-parse.c: int cmd_rev_parse(int argc, const char **argv, const char *prefix)
      				continue;
      			}
    @@ builtin/rev-parse.c: int cmd_rev_parse(int argc, const char **argv, const char *
      					die(_("Could not read the index"));
      				if (the_index.split_index) {
      					const struct object_id *oid = &the_index.split_index->base_oid;
    +
    + ## builtin/rm.c ##
    +@@ builtin/rm.c: static void submodules_absorb_gitdir_if_needed(void)
    + 		int pos;
    + 		const struct cache_entry *ce;
    + 
    +-		pos = cache_name_pos(name, strlen(name));
    ++		pos = index_name_pos(&the_index, name, strlen(name));
    + 		if (pos < 0) {
    + 			pos = get_ours_cache_pos(name, pos);
    + 			if (pos < 0)
    +@@ builtin/rm.c: static int check_local_mod(struct object_id *head, int index_only)
    + 		int local_changes = 0;
    + 		int staged_changes = 0;
    + 
    +-		pos = cache_name_pos(name, strlen(name));
    ++		pos = index_name_pos(&the_index, name, strlen(name));
    + 		if (pos < 0) {
    + 			/*
    + 			 * Skip unmerged entries except for populated submodules
    +@@ builtin/rm.c: int cmd_rm(int argc, const char **argv, const char *prefix)
    + 
    + 	prepare_repo_settings(the_repository);
    + 	the_repository->settings.command_requires_full_index = 0;
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    + 
    +-	if (read_cache() < 0)
    ++	if (repo_read_index(the_repository) < 0)
    + 		die(_("index file corrupt"));
    + 
    + 	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
    +
    + ## builtin/stash.c ##
    +@@ builtin/stash.c: static int reset_tree(struct object_id *i_tree, int update, int reset)
    + 	struct tree *tree;
    + 	struct lock_file lock_file = LOCK_INIT;
    + 
    +-	read_cache_preload(NULL);
    +-	if (refresh_cache(REFRESH_QUIET))
    ++	repo_read_index_preload(the_repository, NULL, 0);
    ++	if (refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL))
    + 		return -1;
    + 
    +-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
    ++	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
    + 
    + 	memset(&opts, 0, sizeof(opts));
    + 
    +@@ builtin/stash.c: static int do_apply_stash(const char *prefix, struct stash_info *info,
    + 	struct tree *head, *merge, *merge_base;
    + 	struct lock_file lock = LOCK_INIT;
    + 
    +-	read_cache_preload(NULL);
    +-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
    ++	repo_read_index_preload(the_repository, NULL, 0);
    ++	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
    ++					 NULL, NULL, NULL))
    + 		return -1;
    + 
    + 	if (write_cache_as_tree(&c_tree, 0, NULL))
    +@@ builtin/stash.c: static int do_apply_stash(const char *prefix, struct stash_info *info,
    + 				return error(_("conflicts in index. "
    + 					       "Try without --index."));
    + 
    +-			discard_cache();
    +-			read_cache();
    ++			discard_index(&the_index);
    ++			repo_read_index(the_repository);
    + 			if (write_cache_as_tree(&index_tree, 0, NULL))
    + 				return error(_("could not save index tree"));
    + 
    + 			reset_head();
    +-			discard_cache();
    +-			read_cache();
    ++			discard_index(&the_index);
    ++			repo_read_index(the_repository);
    + 		}
    + 	}
    + 
    +@@ builtin/stash.c: static int check_changes_tracked_files(const struct pathspec *ps)
    + 	if (get_oid("HEAD", &dummy))
    + 		return -1;
    + 
    +-	if (read_cache() < 0)
    ++	if (repo_read_index(the_repository) < 0)
    + 		return -1;
    + 
    + 	init_revisions(&rev, NULL);
    +@@ builtin/stash.c: static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
    + 	rev.diffopt.format_callback = add_diff_to_buf;
    + 	rev.diffopt.format_callback_data = &diff_output;
    + 
    +-	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
    ++	if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
    + 		ret = -1;
    + 		goto done;
    + 	}
    +@@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
    + 
    + 	prepare_fallback_ident("git stash", "git@stash");
    + 
    +-	read_cache_preload(NULL);
    +-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
    ++	repo_read_index_preload(the_repository, NULL, 0);
    ++	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
    ++					 NULL, NULL, NULL) < 0) {
    + 		ret = -1;
    + 		goto done;
    + 	}
    +@@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
    + 		goto done;
    + 	}
    + 
    +-	read_cache_preload(NULL);
    ++	repo_read_index_preload(the_repository, NULL, 0);
    + 	if (!include_untracked && ps->nr) {
    + 		int i;
    + 		char *ps_matched = xcalloc(ps->nr, 1);
    +@@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
    + 		free(ps_matched);
    + 	}
    + 
    +-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
    ++	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
    ++					 NULL, NULL, NULL)) {
    + 		ret = -1;
    + 		goto done;
    + 	}
    +@@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
    + 				goto done;
    + 			}
    + 		}
    +-		discard_cache();
    ++		discard_index(&the_index);
    + 		if (ps->nr) {
    + 			struct child_process cp_add = CHILD_PROCESS_INIT;
    + 			struct child_process cp_diff = CHILD_PROCESS_INIT;
    +
    + ## builtin/submodule--helper.c ##
    +@@
    +-#define USE_THE_INDEX_COMPATIBILITY_MACROS
    ++#define USE_THE_INDEX_VARIABLE
    + #include "builtin.h"
    + #include "repository.h"
    + #include "cache.h"
    +@@ builtin/submodule--helper.c: static int module_list_compute(const char **argv,
    + 	if (pathspec->nr)
    + 		ps_matched = xcalloc(pathspec->nr, 1);
    + 
    +-	if (read_cache() < 0)
    ++	if (repo_read_index(the_repository) < 0)
    + 		die(_("index file corrupt"));
    + 
    + 	for (i = 0; i < the_index.cache_nr; i++) {
    +@@ builtin/submodule--helper.c: static int compute_summary_module_list(struct object_id *head_oid,
    + 	if (!info->cached) {
    + 		if (diff_cmd == DIFF_INDEX)
    + 			setup_work_tree();
    +-		if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
    +-			perror("read_cache_preload");
    ++		if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
    ++			perror("repo_read_index_preload");
    + 			ret = -1;
    + 			goto cleanup;
    + 		}
    +-	} else if (read_cache() < 0) {
    +-		perror("read_cache");
    ++	} else if (repo_read_index(the_repository) < 0) {
    ++		perror("repo_read_cache");
    + 		ret = -1;
    + 		goto cleanup;
    + 	}
    +@@ builtin/submodule--helper.c: static void die_on_index_match(const char *path, int force)
    + 	const char *args[] = { path, NULL };
    + 	parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
    + 
    +-	if (read_cache_preload(NULL) < 0)
    ++	if (repo_read_index_preload(the_repository, NULL, 0) < 0)
    + 		die(_("index file corrupt"));
    + 
    + 	if (ps.nr) {
    +
    + ## builtin/update-index.c ##
    +@@ builtin/update-index.c: static int test_if_untracked_cache_is_supported(void)
    + static int mark_ce_flags(const char *path, int flag, int mark)
    + {
    + 	int namelen = strlen(path);
    +-	int pos = cache_name_pos(path, namelen);
    ++	int pos = index_name_pos(&the_index, path, namelen);
    + 	if (0 <= pos) {
    + 		mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
    + 		if (mark)
    +@@ builtin/update-index.c: static int add_one_path(const struct cache_entry *old, const char *path, int len
    + static int process_directory(const char *path, int len, struct stat *st)
    + {
    + 	struct object_id oid;
    +-	int pos = cache_name_pos(path, len);
    ++	int pos = index_name_pos(&the_index, path, len);
    + 
    + 	/* Exact match: file or existing gitlink */
    + 	if (pos >= 0) {
    +@@ builtin/update-index.c: static void chmod_path(char flip, const char *path)
    + 	int pos;
    + 	struct cache_entry *ce;
    + 
    +-	pos = cache_name_pos(path, strlen(path));
    ++	pos = index_name_pos(&the_index, path, strlen(path));
    + 	if (pos < 0)
    + 		goto fail;
    + 	ce = the_index.cache[pos];
    +@@ builtin/update-index.c: static int unresolve_one(const char *path)
    + 	struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
    + 
    + 	/* See if there is such entry in the index. */
    +-	pos = cache_name_pos(path, namelen);
    ++	pos = index_name_pos(&the_index, path, namelen);
    + 	if (0 <= pos) {
    + 		/* already merged */
    + 		pos = unmerge_index_entry_at(&the_index, pos);
    +@@ builtin/update-index.c: struct refresh_params {
    + static int refresh(struct refresh_params *o, unsigned int flag)
    + {
    + 	setup_work_tree();
    +-	read_cache();
    +-	*o->has_errors |= refresh_cache(o->flags | flag);
    ++	repo_read_index(the_repository);
    ++	*o->has_errors |= refresh_index(&the_index, o->flags | flag, NULL,
    ++					NULL, NULL);
    + 	if (has_racy_timestamp(&the_index)) {
    + 		/*
    + 		 * Even if nothing else has changed, updating the file
    + 		 * increases the chance that racy timestamps become
    + 		 * non-racy, helping future run-time performance.
    + 		 * We do that even in case of "errors" returned by
    +-		 * refresh_cache() as these are no actual errors.
    ++		 * refresh_index() as these are no actual errors.
    + 		 * cmd_status() does the same.
    + 		 */
    + 		the_index.cache_changed |= SOMETHING_CHANGED;
    +@@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
    + 	the_repository->settings.command_requires_full_index = 0;
    + 
    + 	/* we will diagnose later if it turns out that we need to update it */
    +-	newfd = hold_locked_index(&lock_file, 0);
    ++	newfd = repo_hold_locked_index(the_repository, &lock_file, 0);
    + 	if (newfd < 0)
    + 		lock_error = errno;
    + 
    +-	entries = read_cache();
    ++	entries = repo_read_index(the_repository);
    + 	if (entries < 0)
    + 		die("cache corrupted");
    + 
    +
    + ## cache.h ##
    +@@ cache.h: extern struct index_state the_index;
    + #define active_nr (the_index.cache_nr)
    + 
    + #define read_cache() repo_read_index(the_repository)
    +-#define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
    +-#define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
    + #define discard_cache() discard_index(&the_index)
    + #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
    +-#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
    +-#define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
    +-#define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
    + #endif
    + #endif
    + #endif
    +
    + ## contrib/coccinelle/index-compatibility.cocci ##
    +@@ contrib/coccinelle/index-compatibility.cocci: identifier f != prepare_to_commit;
    + (
    + - read_cache_unmerged
    + + repo_read_index_unmerged
    ++|
    ++- hold_locked_index
    +++ repo_hold_locked_index
    + )
    +   (
    + + the_repository,
    +   ...)
    + 
    ++// "the_repository" special-cases
    ++@@
    ++@@
    ++(
    ++- read_cache_preload
    +++ repo_read_index_preload
    ++)
    ++  (
    +++ the_repository,
    ++  ...
    +++ , 0
    ++  )
    ++
    + // "the_index" simple cases
    + @@
    + @@
    +@@ contrib/coccinelle/index-compatibility.cocci: identifier f != prepare_to_commit;
    +   (
    + + &the_index,
    +   ...)
    ++
    ++@@
    ++@@
    ++(
    ++- refresh_and_write_cache
    +++ repo_refresh_and_write_index
    ++)
    ++  (
    +++ the_repository,
    ++  ...
    +++ , NULL, NULL, NULL
    ++  )
    ++
    ++// "the_index" special-cases
    ++@@
    ++@@
    ++(
    ++- read_cache_from
    +++ read_index_from
    ++)
    ++  (
    +++ &the_index,
    ++  ...
    +++ , get_git_dir()
    ++  )
    ++
    ++@@
    ++@@
    ++(
    ++- refresh_cache
    +++ refresh_index
    ++)
    ++  (
    +++ &the_index,
    ++  ...
    +++ , NULL, NULL, NULL
    ++  )
    +
    + ## contrib/coccinelle/index-compatibility.pending.cocci ##
    +@@
    + (
    + - read_cache
    + + repo_read_index
    +-|
    +-- hold_locked_index
    +-+ repo_hold_locked_index
    + )
    +   (
    + + the_repository,
    +@@
    +   (
    + + &the_index,
    +   ...)
    +-
    +-// "the_repository" special-cases
    +-@@
    +-@@
    +-(
    +-- read_cache_preload
    +-+ repo_read_index_preload
    +-)
    +-  (
    +-+ the_repository,
    +-  ...
    +-+ , 0
    +-  )
    +-
    +-@@
    +-@@
    +-(
    +-- refresh_and_write_cache
    +-+ repo_refresh_and_write_index
    +-)
    +-  (
    +-+ the_repository,
    +-  ...
    +-+ , NULL, NULL, NULL
    +-  )
    +-
    +-
    +-// "the_index" special-cases
    +-@@
    +-@@
    +-(
    +-- read_cache_from
    +-+ read_index_from
    +-)
    +-  (
    +-+ &the_index,
    +-  ...
    +-+ , get_git_dir()
    +-  )
    +-
    +-@@
    +-@@
    +-(
    +-- refresh_cache
    +-+ refresh_index
    +-)
    +-  (
    +-+ &the_index,
    +-  ...
    +-+ , NULL, NULL, NULL
    +-  )
12:  b69cf6d7600 <  -:  ----------- builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS"
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH v2 01/11] cache.h: remove unused "the_index" compat macros
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 02/11] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
                     ` (9 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

The "active_alloc" macro added in 228e94f9357 (Move index-related
variables into a structure., 2007-04-01) has not been used since
4aab5b46f44 (Make read-cache.c "the_index" free., 2007-04-01). Let's
remove it.

The rest of these are likewise unused, so let's not keep them
around. E.g. 12cd0bf9b02 (dir: stop using the index compatibility
macros, 2017-05-05) is the last use of "cache_dir_exists".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 cache.h | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/cache.h b/cache.h
index 26ed03bd6de..29bb0ad2b41 100644
--- a/cache.h
+++ b/cache.h
@@ -438,7 +438,6 @@ extern struct index_state the_index;
 
 #define active_cache (the_index.cache)
 #define active_nr (the_index.cache_nr)
-#define active_alloc (the_index.cache_alloc)
 #define active_cache_changed (the_index.cache_changed)
 #define active_cache_tree (the_index.cache_tree)
 
@@ -452,7 +451,6 @@ extern struct index_state the_index;
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
 #define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
-#define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
 #define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
 #define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
@@ -461,13 +459,10 @@ extern struct index_state the_index;
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
-#define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
 #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
 #define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
-#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
-#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 02/11] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS"
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 01/11] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 03/11] cocci & cache.h: remove rarely used "the_index" compat macros Ævar Arnfjörð Bjarmason
                     ` (8 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Adding "USE_THE_INDEX_COMPATIBILITY_MACROS" to these two appears to
have been unnecessary from the start, as going back and compiling
f8adbec9fea (cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch,
2019-01-24) without that addition works.

Let's not have these ask for the compatibility macros from cache.h
that they don't need.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/grep.c | 1 -
 builtin/log.c  | 1 -
 2 files changed, 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 5fa927d4e22..f7821c5fbba 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -3,7 +3,6 @@
  *
  * Copyright (c) 2006 Junio C Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "repository.h"
 #include "config.h"
diff --git a/builtin/log.c b/builtin/log.c
index 5eafcf26b49..89447a50838 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -4,7 +4,6 @@
  * (C) Copyright 2006 Linus Torvalds
  *		 2006 Junio Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "refs.h"
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 03/11] cocci & cache.h: remove rarely used "the_index" compat macros
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 01/11] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 02/11] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 04/11] read-cache API & users: make discard_index() return void Ævar Arnfjörð Bjarmason
                     ` (7 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Since 4aab5b46f44 (Make read-cache.c "the_index" free., 2007-04-01)
we've been undergoing a slow migration away from these macros, but
haven't made much progress since f8adbec9fea (cache.h: flip
NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24).

Let's move forward a bit by changing the users of those macros that
are rare enough that we can convert them in one go, and then remove
the compatibility shim.

The only manual change to the C code here is to "cache.h", the rest is
all the result of applying the new "index-compatibility.cocci".

Even though it's a one-off, let's keep the coccinelle rules for
now. We'll extend them in subsequent commits, and this will help
anything that's in-flight or out-of-tree to migrate.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c                                |  5 ++-
 builtin/am.c                                 |  4 +-
 builtin/checkout.c                           |  4 +-
 builtin/clean.c                              |  2 +-
 builtin/commit.c                             |  2 +-
 builtin/merge.c                              |  2 +-
 builtin/mv.c                                 |  4 +-
 builtin/pull.c                               |  4 +-
 builtin/read-tree.c                          |  4 +-
 builtin/reset.c                              |  4 +-
 builtin/update-index.c                       |  4 +-
 cache.h                                      | 10 -----
 contrib/coccinelle/index-compatibility.cocci | 45 ++++++++++++++++++++
 13 files changed, 65 insertions(+), 29 deletions(-)
 create mode 100644 contrib/coccinelle/index-compatibility.cocci

diff --git a/builtin/add.c b/builtin/add.c
index 626c71ec6aa..75be1d021ab 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -55,7 +55,7 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
 			continue;
 
 		if (!show_only)
-			err = chmod_cache_entry(ce, flip);
+			err = chmod_index_entry(&the_index, ce, flip);
 		else
 			err = S_ISREG(ce->ce_mode) ? 0 : -1;
 
@@ -172,7 +172,8 @@ static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
 			continue; /* do not touch non blobs */
 		if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
 			continue;
-		retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
+		retval |= add_file_to_index(&the_index, ce->name,
+					    flags | ADD_CACHE_RENORMALIZE);
 	}
 
 	return retval;
diff --git a/builtin/am.c b/builtin/am.c
index 20aea0d2487..f362a1c29fd 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1930,7 +1930,7 @@ static void am_resolve(struct am_state *state, int allow_empty)
 		}
 	}
 
-	if (unmerged_cache()) {
+	if (unmerged_index(&the_index)) {
 		printf_ln(_("You still have unmerged paths in your index.\n"
 			"You should 'git add' each file with resolved conflicts to mark them as such.\n"
 			"You might run `git rm` on a file to accept \"deleted by them\" for it."));
@@ -2045,7 +2045,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
 	if (!remote_tree)
 		return error(_("Could not parse object '%s'."), oid_to_hex(remote));
 
-	read_cache_unmerged();
+	repo_read_index_unmerged(the_repository);
 
 	if (fast_forward_to(head_tree, head_tree, 1))
 		return -1;
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2a132392fbe..aa610b274e3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -722,7 +722,7 @@ static void init_topts(struct unpack_trees_options *topts, int merge,
 
 	setup_unpack_trees_porcelain(topts, "checkout");
 
-	topts->initial_checkout = is_cache_unborn();
+	topts->initial_checkout = is_index_unborn(&the_index);
 	topts->update = 1;
 	topts->merge = 1;
 	topts->quiet = merge && old_commit;
@@ -763,7 +763,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 
 		refresh_cache(REFRESH_QUIET);
 
-		if (unmerged_cache()) {
+		if (unmerged_index(&the_index)) {
 			error(_("you need to resolve your current index first"));
 			return 1;
 		}
diff --git a/builtin/clean.c b/builtin/clean.c
index 40ff2c578de..7084e685f6f 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -1031,7 +1031,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		struct stat st;
 		const char *rel;
 
-		if (!cache_name_is_other(ent->name, ent->len))
+		if (!index_name_is_other(&the_index, ent->name, ent->len))
 			continue;
 
 		if (lstat(ent->name, &st))
diff --git a/builtin/commit.c b/builtin/commit.c
index e22bdf23f5f..985503d43a6 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -302,7 +302,7 @@ static void add_remove_files(struct string_list *list)
 			continue;
 
 		if (!lstat(p->string, &st)) {
-			if (add_to_cache(p->string, &st, 0))
+			if (add_to_index(&the_index, p->string, &st, 0))
 				die(_("updating files failed"));
 		} else
 			remove_file_from_cache(p->string);
diff --git a/builtin/merge.c b/builtin/merge.c
index b3f75f55c8a..da11dfae19e 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1375,7 +1375,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		goto done;
 	}
 
-	if (read_cache_unmerged())
+	if (repo_read_index_unmerged(the_repository))
 		die_resolve_conflict("merge");
 
 	if (file_exists(git_path_merge_head(the_repository))) {
diff --git a/builtin/mv.c b/builtin/mv.c
index 3413ad1c9b1..df6157fd6b5 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -343,7 +343,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			argc += last - first;
 			goto act_on_entry;
 		}
-		if (!(ce = cache_file_exists(src, length, 0))) {
+		if (!(ce = index_file_exists(&the_index, src, length, 0))) {
 			bad = _("not under version control");
 			goto act_on_entry;
 		}
@@ -472,7 +472,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
 			sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
-		rename_cache_entry_at(pos, dst);
+		rename_index_entry_at(&the_index, pos, dst);
 
 		if (ignore_sparse &&
 		    core_apply_sparse_checkout &&
diff --git a/builtin/pull.c b/builtin/pull.c
index b21edd767aa..4a2a6db2dce 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -1030,7 +1030,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 	if (opt_rebase < 0)
 		opt_rebase = config_get_rebase(&rebase_unspecified);
 
-	if (read_cache_unmerged())
+	if (repo_read_index_unmerged(the_repository))
 		die_resolve_conflict("pull");
 
 	if (file_exists(git_path_merge_head(the_repository)))
@@ -1043,7 +1043,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 		if (opt_autostash == -1)
 			opt_autostash = config_autostash;
 
-		if (is_null_oid(&orig_head) && !is_cache_unborn())
+		if (is_null_oid(&orig_head) && !is_index_unborn(&the_index))
 			die(_("Updating an unborn branch with changes added to the index."));
 
 		if (!opt_autostash)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 45c6652444b..fd708273101 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -188,7 +188,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 	 */
 
 	if (opts.reset || opts.merge || opts.prefix) {
-		if (read_cache_unmerged() && (opts.prefix || opts.merge))
+		if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
 			die(_("You need to resolve your current index first"));
 		stage = opts.merge = 1;
 	}
@@ -232,7 +232,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 			break;
 		case 2:
 			opts.fn = twoway_merge;
-			opts.initial_checkout = is_cache_unborn();
+			opts.initial_checkout = is_index_unborn(&the_index);
 			break;
 		case 3:
 		default:
diff --git a/builtin/reset.c b/builtin/reset.c
index ab027774824..e561180e8c4 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -84,7 +84,7 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t
 		BUG("invalid reset_type passed to reset_index");
 	}
 
-	read_cache_unmerged();
+	repo_read_index_unmerged(the_repository);
 
 	if (reset_type == KEEP) {
 		struct object_id head_oid;
@@ -220,7 +220,7 @@ static void set_reflog_message(struct strbuf *sb, const char *action,
 
 static void die_if_unmerged_cache(int reset_type)
 {
-	if (is_merge() || unmerged_cache())
+	if (is_merge() || unmerged_index(&the_index))
 		die(_("Cannot do a %s reset in the middle of a merge."),
 		    _(reset_type_names[reset_type]));
 
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 7b0c924d7d4..e1b2bb78e72 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -445,7 +445,7 @@ static void chmod_path(char flip, const char *path)
 	if (pos < 0)
 		goto fail;
 	ce = active_cache[pos];
-	if (chmod_cache_entry(ce, flip) < 0)
+	if (chmod_index_entry(&the_index, ce, flip) < 0)
 		goto fail;
 
 	report("chmod %cx '%s'", flip, path);
@@ -641,7 +641,7 @@ static int unresolve_one(const char *path)
 	pos = cache_name_pos(path, namelen);
 	if (0 <= pos) {
 		/* already merged */
-		pos = unmerge_cache_entry_at(pos);
+		pos = unmerge_index_entry_at(&the_index, pos);
 		if (pos < active_nr) {
 			const struct cache_entry *ce = active_cache[pos];
 			if (ce_stage(ce) &&
diff --git a/cache.h b/cache.h
index 29bb0ad2b41..53dc4341717 100644
--- a/cache.h
+++ b/cache.h
@@ -444,25 +444,15 @@ extern struct index_state the_index;
 #define read_cache() repo_read_index(the_repository)
 #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
 #define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
-#define is_cache_unborn() is_index_unborn(&the_index)
-#define read_cache_unmerged() repo_read_index_unmerged(the_repository)
 #define discard_cache() discard_index(&the_index)
-#define unmerged_cache() unmerged_index(&the_index)
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
-#define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
-#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
-#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
-#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
-#define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
-#define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
-#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
new file mode 100644
index 00000000000..04207c409e1
--- /dev/null
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -0,0 +1,45 @@
+// "the_repository" simple cases
+@@
+@@
+(
+- read_cache_unmerged
++ repo_read_index_unmerged
+)
+  (
++ the_repository,
+  ...)
+
+// "the_index" simple cases
+@@
+@@
+(
+- is_cache_unborn
++ is_index_unborn
+|
+- unmerged_cache
++ unmerged_index
+|
+- rename_cache_entry_at
++ rename_index_entry_at
+|
+- chmod_cache_entry
++ chmod_index_entry
+|
+- cache_file_exists
++ index_file_exists
+|
+- cache_name_is_other
++ index_name_is_other
+|
+- unmerge_cache_entry_at
++ unmerge_index_entry_at
+|
+- add_to_cache
++ add_to_index
+|
+- add_file_to_cache
++ add_file_to_index
+)
+  (
++ &the_index,
+  ...)
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 04/11] read-cache API & users: make discard_index() return void
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (2 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 03/11] cocci & cache.h: remove rarely used "the_index" compat macros Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 05/11] cocci: add a index-compatibility.pending.cocci Ævar Arnfjörð Bjarmason
                     ` (6 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

The discard_index() function has not returned non-zero since
7a51ed66f65 (Make on-disk index representation separate from in-core
one, 2008-01-14), but we've had various code in-tree still acting as
though that might be the case.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 add-interactive.c |  8 ++++----
 add-patch.c       |  3 ++-
 builtin/merge.c   |  3 ++-
 cache.h           |  2 +-
 read-cache.c      |  4 +---
 sequencer.c       | 15 +++++++++------
 6 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/add-interactive.c b/add-interactive.c
index ecc5ae1b249..ae1839c04a7 100644
--- a/add-interactive.c
+++ b/add-interactive.c
@@ -530,8 +530,8 @@ static int get_modified_files(struct repository *r,
 	struct collection_status s = { 0 };
 	int i;
 
-	if (discard_index(r->index) < 0 ||
-	    repo_read_index_preload(r, ps, 0) < 0)
+	discard_index(r->index);
+	if (repo_read_index_preload(r, ps, 0) < 0)
 		return error(_("could not read index"));
 
 	prefix_item_list_clear(files);
@@ -1156,8 +1156,8 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
 		    _("staged"), _("unstaged"), _("path"));
 	opts.list_opts.header = header.buf;
 
-	if (discard_index(r->index) < 0 ||
-	    repo_read_index(r) < 0 ||
+	discard_index(r->index);
+	if (repo_read_index(r) < 0 ||
 	    repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
 					 NULL, NULL, NULL) < 0)
 		warning(_("could not refresh index"));
diff --git a/add-patch.c b/add-patch.c
index 33ecd8398a1..a86a92e1646 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1750,7 +1750,8 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
 		s.mode = &patch_mode_add;
 	s.revision = revision;
 
-	if (discard_index(r->index) < 0 || repo_read_index(r) < 0 ||
+	discard_index(r->index);
+	if (repo_read_index(r) < 0 ||
 	    (!s.mode->index_only &&
 	     repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
 					  NULL, NULL, NULL) < 0) ||
diff --git a/builtin/merge.c b/builtin/merge.c
index da11dfae19e..89fdac24e40 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -390,7 +390,8 @@ static void restore_state(const struct object_id *head,
 	run_command(&cmd);
 
 refresh_cache:
-	if (discard_cache() < 0 || read_cache() < 0)
+	discard_cache();
+	if (read_cache() < 0)
 		die(_("could not read index"));
 }
 
diff --git a/cache.h b/cache.h
index 53dc4341717..489e9e1925b 100644
--- a/cache.h
+++ b/cache.h
@@ -774,7 +774,7 @@ void ensure_full_index(struct index_state *istate);
  */
 int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
 
-int discard_index(struct index_state *);
+void discard_index(struct index_state *);
 void move_index_extensions(struct index_state *dst, struct index_state *src);
 int unmerged_index(const struct index_state *);
 
diff --git a/read-cache.c b/read-cache.c
index 32024029274..46f5e497b14 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2531,7 +2531,7 @@ int is_index_unborn(struct index_state *istate)
 	return (!istate->cache_nr && !istate->timestamp.sec);
 }
 
-int discard_index(struct index_state *istate)
+void discard_index(struct index_state *istate)
 {
 	/*
 	 * Cache entries in istate->cache[] should have been allocated
@@ -2562,8 +2562,6 @@ int discard_index(struct index_state *istate)
 		mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
 		FREE_AND_NULL(istate->ce_mem_pool);
 	}
-
-	return 0;
 }
 
 /*
diff --git a/sequencer.c b/sequencer.c
index a46f3ad2e33..746a71cdc92 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -3564,7 +3564,8 @@ static int do_exec(struct repository *r, const char *command_line)
 	status = run_command(&cmd);
 
 	/* force re-reading of the cache */
-	if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
+	discard_index(r->index);
+	if (repo_read_index(r) < 0)
 		return error(_("could not read index"));
 
 	dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
@@ -4029,9 +4030,11 @@ static int do_merge(struct repository *r,
 		ret = run_command(&cmd);
 
 		/* force re-reading of the cache */
-		if (!ret && (discard_index(r->index) < 0 ||
-			     repo_read_index(r) < 0))
-			ret = error(_("could not read index"));
+		if (!ret) {
+			discard_index(r->index);
+			if (repo_read_index(r) < 0)
+				ret = error(_("could not read index"));
+		}
 		goto leave_merge;
 	}
 
@@ -4404,8 +4407,8 @@ void create_autostash(struct repository *r, const char *path)
 		printf(_("Created autostash: %s\n"), buf.buf);
 		if (reset_head(r, &ropts) < 0)
 			die(_("could not reset --hard"));
-		if (discard_index(r->index) < 0 ||
-			repo_read_index(r) < 0)
+		discard_index(r->index);
+		if (repo_read_index(r) < 0)
 			die(_("could not read index"));
 	}
 	strbuf_release(&buf);
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 05/11] cocci: add a index-compatibility.pending.cocci
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (3 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 04/11] read-cache API & users: make discard_index() return void Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 06/11] cocci & cache.h: apply a selection of "pending" index-compatibility Ævar Arnfjörð Bjarmason
                     ` (5 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Add a coccinelle rule which covers the rest of the macros guarded by
"USE_THE_INDEX_COMPATIBILITY_MACROS" cache.h. If the result of this
were applied it can be reduced down to just:

	#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
	extern struct index_state the_index;
	#endif

But that patch is just under 2000 lines, so let's first add this as a
"pending", and then incrementally pick changes from it in subsequent
commits. In doing that we'll migrate rules from this
"index-compatibility.pending.cocci" to the "index-compatibility.cocci"
created in a preceding commit.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 .../index-compatibility.pending.cocci         | 110 ++++++++++++++++++
 1 file changed, 110 insertions(+)
 create mode 100644 contrib/coccinelle/index-compatibility.pending.cocci

diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
new file mode 100644
index 00000000000..6e96de0f638
--- /dev/null
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -0,0 +1,110 @@
+// the_index.* variables
+@@
+@@
+(
+- active_cache
++ the_index.cache
+|
+- active_nr
++ the_index.cache_nr
+|
+- active_cache_changed
++ the_index.cache_changed
+|
+- active_cache_tree
++ the_index.cache_tree
+)
+
+// "the_repository" simple cases
+@@
+@@
+(
+- read_cache
++ repo_read_index
+|
+- hold_locked_index
++ repo_hold_locked_index
+)
+  (
++ the_repository,
+  ...)
+
+// "the_index" simple cases
+@@
+@@
+(
+- discard_cache
++ discard_index
+|
+- cache_name_pos
++ index_name_pos
+|
+- add_cache_entry
++ add_index_entry
+|
+- remove_file_from_cache
++ remove_file_from_index
+|
+- ce_match_stat
++ ie_match_stat
+|
+- ce_modified
++ ie_modified
+|
+- resolve_undo_clear
++ resolve_undo_clear_index
+)
+  (
++ &the_index,
+  ...)
+
+// "the_repository" special-cases
+@@
+@@
+(
+- read_cache_preload
++ repo_read_index_preload
+)
+  (
++ the_repository,
+  ...
++ , 0
+  )
+
+@@
+@@
+(
+- refresh_and_write_cache
++ repo_refresh_and_write_index
+)
+  (
++ the_repository,
+  ...
++ , NULL, NULL, NULL
+  )
+
+
+// "the_index" special-cases
+@@
+@@
+(
+- read_cache_from
++ read_index_from
+)
+  (
++ &the_index,
+  ...
++ , get_git_dir()
+  )
+
+@@
+@@
+(
+- refresh_cache
++ refresh_index
+)
+  (
++ &the_index,
+  ...
++ , NULL, NULL, NULL
+  )
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 06/11] cocci & cache.h: apply a selection of "pending" index-compatibility
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (4 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 05/11] cocci: add a index-compatibility.pending.cocci Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 07/11] cocci & cache.h: apply variable section " Ævar Arnfjörð Bjarmason
                     ` (4 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Apply a selection of rules in "index-compatibility.pending.cocci"
tree-wide, and in doing so migrate them to
"index-compatibility.cocci".

As in preceding commits the only manual changes here are the macro
removals in "cache.h", and the update to the '*.cocci" rules. The rest
of the C code changes are the result of applying those updated rules.

Move rules for some rarely used cache compatibility macros from
"index-compatibility.pending.cocci" to "index-compatibility.cocci" and
apply them.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/checkout.c                            |  5 +++--
 builtin/commit.c                              |  2 +-
 builtin/merge.c                               |  2 +-
 builtin/mv.c                                  |  4 +++-
 builtin/read-tree.c                           |  2 +-
 builtin/reset.c                               |  5 +++--
 builtin/rm.c                                  |  4 ++--
 builtin/update-index.c                        | 22 +++++++++----------
 cache.h                                       |  5 -----
 contrib/coccinelle/index-compatibility.cocci  | 15 +++++++++++++
 .../index-compatibility.pending.cocci         | 15 -------------
 11 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index aa610b274e3..afbd71764a3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -160,7 +160,8 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
 		}
 	}
 
-	add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
+	add_index_entry(&the_index, ce,
+			ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
 	return 0;
 }
 
@@ -744,7 +745,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 	if (read_cache_preload(NULL) < 0)
 		return error(_("index file corrupt"));
 
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 	if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
 		if (new_branch_info->commit)
 			BUG("'switch --orphan' should never accept a commit as starting point");
diff --git a/builtin/commit.c b/builtin/commit.c
index 985503d43a6..10a827b1f4a 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -305,7 +305,7 @@ static void add_remove_files(struct string_list *list)
 			if (add_to_index(&the_index, p->string, &st, 0))
 				die(_("updating files failed"));
 		} else
-			remove_file_from_cache(p->string);
+			remove_file_from_index(&the_index, p->string);
 	}
 }
 
diff --git a/builtin/merge.c b/builtin/merge.c
index 89fdac24e40..2046992d75a 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1397,7 +1397,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		else
 			die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
 	}
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 
 	if (option_edit < 0)
 		option_edit = default_edit_option();
diff --git a/builtin/mv.c b/builtin/mv.c
index df6157fd6b5..6f94c3b9238 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -471,7 +471,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		pos = cache_name_pos(src, strlen(src));
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
-			sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
+			sparse_and_dirty = ie_modified(&the_index,
+						       active_cache[pos], &st,
+						       0);
 		rename_index_entry_at(&the_index, pos, dst);
 
 		if (ignore_sparse &&
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index fd708273101..9ced755a6db 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -192,7 +192,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 			die(_("You need to resolve your current index first"));
 		stage = opts.merge = 1;
 	}
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
diff --git a/builtin/reset.c b/builtin/reset.c
index e561180e8c4..59f777a1da4 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -145,7 +145,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 		struct cache_entry *ce;
 
 		if (!is_in_reset_tree && !intent_to_add) {
-			remove_file_from_cache(one->path);
+			remove_file_from_index(&the_index, one->path);
 			continue;
 		}
 
@@ -172,7 +172,8 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 			ce->ce_flags |= CE_INTENT_TO_ADD;
 			set_object_name_for_intent_to_add_entry(ce);
 		}
-		add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
+		add_index_entry(&the_index, ce,
+				ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
 	}
 }
 
diff --git a/builtin/rm.c b/builtin/rm.c
index f0d025a4e23..c717cdc97f5 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -168,7 +168,7 @@ static int check_local_mod(struct object_id *head, int index_only)
 		 * Is the index different from the file in the work tree?
 		 * If it's a submodule, is its work tree modified?
 		 */
-		if (ce_match_stat(ce, &st, 0) ||
+		if (ie_match_stat(&the_index, ce, &st, 0) ||
 		    (S_ISGITLINK(ce->ce_mode) &&
 		     bad_to_remove_submodule(ce->name,
 				SUBMODULE_REMOVAL_DIE_ON_ERROR |
@@ -386,7 +386,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		if (!quiet)
 			printf("rm '%s'\n", path);
 
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(&the_index, path))
 			die(_("git rm: unable to remove %s"), path);
 	}
 
diff --git a/builtin/update-index.c b/builtin/update-index.c
index e1b2bb78e72..533ba66df05 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -256,7 +256,7 @@ static int remove_one_path(const char *path)
 {
 	if (!allow_remove)
 		return error("%s: does not exist and --remove not passed", path);
-	if (remove_file_from_cache(path))
+	if (remove_file_from_index(&the_index, path))
 		return error("%s: cannot remove from the index", path);
 	return 0;
 }
@@ -281,7 +281,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	struct cache_entry *ce;
 
 	/* Was the old index entry already up-to-date? */
-	if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
+	if (old && !ce_stage(old) && !ie_match_stat(&the_index, old, st, 0))
 		return 0;
 
 	ce = make_empty_cache_entry(&the_index, len);
@@ -298,7 +298,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	}
 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
-	if (add_cache_entry(ce, option)) {
+	if (add_index_entry(&the_index, ce, option)) {
 		discard_cache_entry(ce);
 		return error("%s: cannot add to the index - missing --add option?", path);
 	}
@@ -390,7 +390,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 		 * On the other hand, removing it from index should work
 		 */
 		if (!ignore_skip_worktree_entries && allow_remove &&
-		    remove_file_from_cache(path))
+		    remove_file_from_index(&the_index, path))
 			return error("%s: cannot remove from the index", path);
 		return 0;
 	}
@@ -429,7 +429,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 		ce->ce_flags |= CE_VALID;
 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
-	if (add_cache_entry(ce, option))
+	if (add_index_entry(&the_index, ce, option))
 		return error("%s: cannot add to the index - missing --add option?",
 			     path);
 	report("add '%s'", path);
@@ -488,7 +488,7 @@ static void update_one(const char *path)
 	}
 
 	if (force_remove) {
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(&the_index, path))
 			die("git update-index: unable to remove %s", path);
 		report("remove '%s'", path);
 		return;
@@ -571,7 +571,7 @@ static void read_index_info(int nul_term_line)
 
 		if (!mode) {
 			/* mode == 0 means there is no such path -- remove */
-			if (remove_file_from_cache(path_name))
+			if (remove_file_from_index(&the_index, path_name))
 				die("git update-index: unable to remove %s",
 				    ptr);
 		}
@@ -686,13 +686,13 @@ static int unresolve_one(const char *path)
 		goto free_return;
 	}
 
-	remove_file_from_cache(path);
-	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
+	remove_file_from_index(&the_index, path);
+	if (add_index_entry(&the_index, ce_2, ADD_CACHE_OK_TO_ADD)) {
 		error("%s: cannot add our version to the index.", path);
 		ret = -1;
 		goto free_return;
 	}
-	if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
+	if (!add_index_entry(&the_index, ce_3, ADD_CACHE_OK_TO_ADD))
 		return 0;
 	error("%s: cannot add their version to the index.", path);
 	ret = -1;
@@ -850,7 +850,7 @@ static int resolve_undo_clear_callback(const struct option *opt,
 {
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
-	resolve_undo_clear();
+	resolve_undo_clear_index(&the_index);
 	return 0;
 }
 
diff --git a/cache.h b/cache.h
index 489e9e1925b..79b8d479710 100644
--- a/cache.h
+++ b/cache.h
@@ -446,13 +446,8 @@ extern struct index_state the_index;
 #define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
 #define discard_cache() discard_index(&the_index)
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
-#define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
-#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
-#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
-#define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
-#define resolve_undo_clear() resolve_undo_clear_index(&the_index)
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
index 04207c409e1..89c18986160 100644
--- a/contrib/coccinelle/index-compatibility.cocci
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -39,6 +39,21 @@
 |
 - add_file_to_cache
 + add_file_to_index
+|
+- add_cache_entry
++ add_index_entry
+|
+- remove_file_from_cache
++ remove_file_from_index
+|
+- ce_match_stat
++ ie_match_stat
+|
+- ce_modified
++ ie_modified
+|
+- resolve_undo_clear
++ resolve_undo_clear_index
 )
   (
 + &the_index,
diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
index 6e96de0f638..cf1ea57d700 100644
--- a/contrib/coccinelle/index-compatibility.pending.cocci
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -38,21 +38,6 @@
 |
 - cache_name_pos
 + index_name_pos
-|
-- add_cache_entry
-+ add_index_entry
-|
-- remove_file_from_cache
-+ remove_file_from_index
-|
-- ce_match_stat
-+ ie_match_stat
-|
-- ce_modified
-+ ie_modified
-|
-- resolve_undo_clear
-+ resolve_undo_clear_index
 )
   (
 + &the_index,
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 07/11] cocci & cache.h: apply variable section of "pending" index-compatibility
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (5 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 06/11] cocci & cache.h: apply a selection of "pending" index-compatibility Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 08/11] cocci: apply "pending" index-compatibility to "t/helper/*.c" Ævar Arnfjörð Bjarmason
                     ` (3 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Mostly apply the part of "index-compatibility.pending.cocci" that
renames the global variables like "active_nr", which are a shorthand
to referencing (in that case) a struct member as "the_index.cache_nr".

In doing so move more of "index-compatibility.pending.cocci" to
"index-compatibility.cocci".

In the case of "active_nr" we'd have a textual conflict with
"ab/various-leak-fixes" in "next"[1]. Let's exclude that specific case
while moving the rule over from "pending".

1. 407b94280f8 (commit: discard partial cache before (re-)reading it,
   2022-11-08)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c                                 |  8 ++--
 builtin/checkout-index.c                      | 10 ++--
 builtin/checkout.c                            | 46 +++++++++----------
 builtin/commit.c                              | 16 +++----
 builtin/fsck.c                                | 12 ++---
 builtin/merge-index.c                         | 10 ++--
 builtin/merge.c                               |  6 +--
 builtin/mv.c                                  | 19 ++++----
 builtin/read-tree.c                           |  2 +-
 builtin/reset.c                               |  2 +-
 builtin/rm.c                                  | 14 +++---
 builtin/stash.c                               |  8 ++--
 builtin/submodule--helper.c                   | 14 +++---
 builtin/update-index.c                        | 46 +++++++++----------
 cache.h                                       |  3 --
 contrib/coccinelle/index-compatibility.cocci  | 22 +++++++++
 .../index-compatibility.pending.cocci         | 17 -------
 t/helper/test-dump-cache-tree.c               |  2 +-
 t/helper/test-scrap-cache-tree.c              |  4 +-
 19 files changed, 132 insertions(+), 129 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 75be1d021ab..23f9f0e08bb 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -42,8 +42,8 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
 {
 	int i, ret = 0;
 
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		struct cache_entry *ce = the_index.cache[i];
 		int err;
 
 		if (!include_sparse &&
@@ -159,8 +159,8 @@ static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
 {
 	int i, retval = 0;
 
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		struct cache_entry *ce = the_index.cache[i];
 
 		if (!include_sparse &&
 		    (ce_skip_worktree(ce) ||
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 97e06e8c52c..c2aee450c40 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -75,8 +75,8 @@ static int checkout_file(const char *name, const char *prefix)
 	if (pos < 0)
 		pos = -pos - 1;
 
-	while (pos < active_nr) {
-		struct cache_entry *ce = active_cache[pos];
+	while (pos < the_index.cache_nr) {
+		struct cache_entry *ce = the_index.cache[pos];
 		if (ce_namelen(ce) != namelen ||
 		    memcmp(ce->name, name, namelen))
 			break;
@@ -136,8 +136,8 @@ static int checkout_all(const char *prefix, int prefix_length)
 	int i, errs = 0;
 	struct cache_entry *last_ce = NULL;
 
-	for (i = 0; i < active_nr ; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr ; i++) {
+		struct cache_entry *ce = the_index.cache[i];
 
 		if (S_ISSPARSEDIR(ce->ce_mode)) {
 			if (!ce_skip_worktree(ce))
@@ -151,7 +151,7 @@ static int checkout_all(const char *prefix, int prefix_length)
 			 */
 			if (ignore_skip_worktree) {
 				ensure_full_index(&the_index);
-				ce = active_cache[i];
+				ce = the_index.cache[i];
 			}
 		}
 
diff --git a/builtin/checkout.c b/builtin/checkout.c
index afbd71764a3..3fd303f5604 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -150,7 +150,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
 	 */
 	pos = cache_name_pos(ce->name, ce->ce_namelen);
 	if (pos >= 0) {
-		struct cache_entry *old = active_cache[pos];
+		struct cache_entry *old = the_index.cache[pos];
 		if (ce->ce_mode == old->ce_mode &&
 		    !ce_intent_to_add(old) &&
 		    oideq(&ce->oid, &old->oid)) {
@@ -179,8 +179,8 @@ static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
 
 static int skip_same_name(const struct cache_entry *ce, int pos)
 {
-	while (++pos < active_nr &&
-	       !strcmp(active_cache[pos]->name, ce->name))
+	while (++pos < the_index.cache_nr &&
+	       !strcmp(the_index.cache[pos]->name, ce->name))
 		; /* skip */
 	return pos;
 }
@@ -188,9 +188,9 @@ static int skip_same_name(const struct cache_entry *ce, int pos)
 static int check_stage(int stage, const struct cache_entry *ce, int pos,
 		       int overlay_mode)
 {
-	while (pos < active_nr &&
-	       !strcmp(active_cache[pos]->name, ce->name)) {
-		if (ce_stage(active_cache[pos]) == stage)
+	while (pos < the_index.cache_nr &&
+	       !strcmp(the_index.cache[pos]->name, ce->name)) {
+		if (ce_stage(the_index.cache[pos]) == stage)
 			return 0;
 		pos++;
 	}
@@ -207,8 +207,8 @@ static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
 	unsigned seen = 0;
 	const char *name = ce->name;
 
-	while (pos < active_nr) {
-		ce = active_cache[pos];
+	while (pos < the_index.cache_nr) {
+		ce = the_index.cache[pos];
 		if (strcmp(name, ce->name))
 			break;
 		seen |= (1 << ce_stage(ce));
@@ -224,10 +224,10 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
 			  const struct checkout *state, int *nr_checkouts,
 			  int overlay_mode)
 {
-	while (pos < active_nr &&
-	       !strcmp(active_cache[pos]->name, ce->name)) {
-		if (ce_stage(active_cache[pos]) == stage)
-			return checkout_entry(active_cache[pos], state,
+	while (pos < the_index.cache_nr &&
+	       !strcmp(the_index.cache[pos]->name, ce->name)) {
+		if (ce_stage(the_index.cache[pos]) == stage)
+			return checkout_entry(the_index.cache[pos], state,
 					      NULL, nr_checkouts);
 		pos++;
 	}
@@ -244,7 +244,7 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
 static int checkout_merged(int pos, const struct checkout *state,
 			   int *nr_checkouts, struct mem_pool *ce_mem_pool)
 {
-	struct cache_entry *ce = active_cache[pos];
+	struct cache_entry *ce = the_index.cache[pos];
 	const char *path = ce->name;
 	mmfile_t ancestor, ours, theirs;
 	enum ll_merge_result merge_status;
@@ -257,7 +257,7 @@ static int checkout_merged(int pos, const struct checkout *state,
 	int renormalize = 0;
 
 	memset(threeway, 0, sizeof(threeway));
-	while (pos < active_nr) {
+	while (pos < the_index.cache_nr) {
 		int stage;
 		stage = ce_stage(ce);
 		if (!stage || strcmp(path, ce->name))
@@ -266,7 +266,7 @@ static int checkout_merged(int pos, const struct checkout *state,
 		if (stage == 2)
 			mode = create_ce_mode(ce->ce_mode);
 		pos++;
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 	}
 	if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2]))
 		return error(_("path '%s' does not have necessary versions"), path);
@@ -392,8 +392,8 @@ static int checkout_worktree(const struct checkout_opts *opts,
 	if (pc_workers > 1)
 		init_parallel_checkout();
 
-	for (pos = 0; pos < active_nr; pos++) {
-		struct cache_entry *ce = active_cache[pos];
+	for (pos = 0; pos < the_index.cache_nr; pos++) {
+		struct cache_entry *ce = the_index.cache[pos];
 		if (ce->ce_flags & CE_MATCHED) {
 			if (!ce_stage(ce)) {
 				errs |= checkout_entry(ce, &state,
@@ -541,13 +541,13 @@ static int checkout_paths(const struct checkout_opts *opts,
 	 * Make sure all pathspecs participated in locating the paths
 	 * to be checked out.
 	 */
-	for (pos = 0; pos < active_nr; pos++)
+	for (pos = 0; pos < the_index.cache_nr; pos++)
 		if (opts->overlay_mode)
-			mark_ce_for_checkout_overlay(active_cache[pos],
+			mark_ce_for_checkout_overlay(the_index.cache[pos],
 						     ps_matched,
 						     opts);
 		else
-			mark_ce_for_checkout_no_overlay(active_cache[pos],
+			mark_ce_for_checkout_no_overlay(the_index.cache[pos],
 							ps_matched,
 							opts);
 
@@ -562,8 +562,8 @@ static int checkout_paths(const struct checkout_opts *opts,
 		unmerge_marked_index(&the_index);
 
 	/* Any unmerged paths? */
-	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+	for (pos = 0; pos < the_index.cache_nr; pos++) {
+		const struct cache_entry *ce = the_index.cache[pos];
 		if (ce->ce_flags & CE_MATCHED) {
 			if (!ce_stage(ce))
 				continue;
@@ -868,7 +868,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 		}
 	}
 
-	if (!cache_tree_fully_valid(active_cache_tree))
+	if (!cache_tree_fully_valid(the_index.cache_tree))
 		cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
 
 	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
diff --git a/builtin/commit.c b/builtin/commit.c
index 10a827b1f4a..abbed750276 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -272,8 +272,8 @@ static int list_paths(struct string_list *list, const char *with_tree,
 
 	/* TODO: audit for interaction with sparse-index. */
 	ensure_full_index(&the_index);
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 		struct string_list_item *item;
 
 		if (ce->ce_flags & CE_UPDATE)
@@ -461,8 +461,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	if (!only && !pathspec.nr) {
 		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
 		refresh_cache_or_die(refresh_flags);
-		if (active_cache_changed
-		    || !cache_tree_fully_valid(active_cache_tree))
+		if (the_index.cache_changed
+		    || !cache_tree_fully_valid(the_index.cache_tree))
 			update_main_cache_tree(WRITE_TREE_SILENT);
 		if (write_locked_index(&the_index, &index_lock,
 				       COMMIT_LOCK | SKIP_IF_UNCHANGED))
@@ -998,10 +998,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 
 			/* TODO: audit for interaction with sparse-index. */
 			ensure_full_index(&the_index);
-			for (i = 0; i < active_nr; i++)
-				if (ce_intent_to_add(active_cache[i]))
+			for (i = 0; i < the_index.cache_nr; i++)
+				if (ce_intent_to_add(the_index.cache[i]))
 					ita_nr++;
-			committable = active_nr - ita_nr > 0;
+			committable = the_index.cache_nr - ita_nr > 0;
 		} else {
 			/*
 			 * Unless the user did explicitly request a submodule
@@ -1823,7 +1823,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		append_merge_tag_headers(parents, &tail);
 	}
 
-	if (commit_tree_extended(sb.buf, sb.len, &active_cache_tree->oid,
+	if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
 				 parents, &oid, author_ident.buf, NULL,
 				 sign_commit, extra)) {
 		rollback_index_files();
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 7436e1a68ef..cd47675e371 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -961,26 +961,26 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 		read_cache();
 		/* TODO: audit for interaction with sparse-index. */
 		ensure_full_index(&the_index);
-		for (i = 0; i < active_nr; i++) {
+		for (i = 0; i < the_index.cache_nr; i++) {
 			unsigned int mode;
 			struct blob *blob;
 			struct object *obj;
 
-			mode = active_cache[i]->ce_mode;
+			mode = the_index.cache[i]->ce_mode;
 			if (S_ISGITLINK(mode))
 				continue;
 			blob = lookup_blob(the_repository,
-					   &active_cache[i]->oid);
+					   &the_index.cache[i]->oid);
 			if (!blob)
 				continue;
 			obj = &blob->object;
 			obj->flags |= USED;
 			fsck_put_object_name(&fsck_walk_options, &obj->oid,
-					     ":%s", active_cache[i]->name);
+					     ":%s", the_index.cache[i]->name);
 			mark_object_reachable(obj);
 		}
-		if (active_cache_tree)
-			fsck_cache_tree(active_cache_tree);
+		if (the_index.cache_tree)
+			fsck_cache_tree(the_index.cache_tree);
 		fsck_resolve_undo(&the_index);
 	}
 
diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index 012f52bd007..a334c513c9d 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -14,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 >= active_nr)
+	if (pos >= the_index.cache_nr)
 		die("git merge-index: %s not in the cache", path);
 	found = 0;
 	do {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = the_index.cache[pos];
 		int stage = ce_stage(ce);
 
 		if (strcmp(ce->name, path))
@@ -28,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 < active_nr);
+	} while (++pos < the_index.cache_nr);
 	if (!found)
 		die("git merge-index: %s not in the cache", path);
 
@@ -62,8 +62,8 @@ static void merge_all(void)
 	int i;
 	/* TODO: audit for interaction with sparse-index. */
 	ensure_full_index(&the_index);
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 		if (!ce_stage(ce))
 			continue;
 		i += merge_entry(i, ce->name)-1;
diff --git a/builtin/merge.c b/builtin/merge.c
index 2046992d75a..27218ee93c9 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -694,7 +694,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
 	if (!trees[nr_trees++])
 		return -1;
 	opts.fn = threeway_merge;
-	cache_tree_free(&active_cache_tree);
+	cache_tree_free(&the_index.cache_tree);
 	for (i = 0; i < nr_trees; i++) {
 		parse_tree(trees[i]);
 		init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
@@ -784,8 +784,8 @@ static int count_unmerged_entries(void)
 {
 	int i, ret = 0;
 
-	for (i = 0; i < active_nr; i++)
-		if (ce_stage(active_cache[i]))
+	for (i = 0; i < the_index.cache_nr; i++)
+		if (ce_stage(the_index.cache[i]))
 			ret++;
 
 	return ret;
diff --git a/builtin/mv.c b/builtin/mv.c
index 6f94c3b9238..883cf3ff12b 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -87,7 +87,7 @@ static void prepare_move_submodule(const char *src, int first,
 				   const char **submodule_gitfile)
 {
 	struct strbuf submodule_dotgit = STRBUF_INIT;
-	if (!S_ISGITLINK(active_cache[first]->ce_mode))
+	if (!S_ISGITLINK(the_index.cache[first]->ce_mode))
 		die(_("Directory %s is in index and no submodule?"), src);
 	if (!is_staging_gitmodules_ok(&the_index))
 		die(_("Please stage your changes to .gitmodules or stash them to proceed"));
@@ -111,8 +111,8 @@ static int index_range_of_same_dir(const char *src, int length,
 		die(_("%.*s is in index"), len_w_slash, src_w_slash);
 
 	first = -1 - first;
-	for (last = first; last < active_nr; last++) {
-		const char *path = active_cache[last]->name;
+	for (last = first; last < the_index.cache_nr; last++) {
+		const char *path = the_index.cache[last]->name;
 		if (strncmp(path, src_w_slash, len_w_slash))
 			break;
 	}
@@ -143,7 +143,7 @@ static int empty_dir_has_sparse_contents(const char *name)
 		pos = -pos - 1;
 		if (pos >= the_index.cache_nr)
 			goto free_return;
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 		if (strncmp(with_slash, ce->name, length))
 			goto free_return;
 		if (ce_skip_worktree(ce))
@@ -268,7 +268,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 					bad = _("bad source");
 				goto act_on_entry;
 			}
-			ce = active_cache[pos];
+			ce = the_index.cache[pos];
 			if (!ce_skip_worktree(ce)) {
 				bad = _("bad source");
 				goto act_on_entry;
@@ -331,7 +331,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			dst_len = strlen(dst);
 
 			for (j = 0; j < last - first; j++) {
-				const struct cache_entry *ce = active_cache[first + j];
+				const struct cache_entry *ce = the_index.cache[first + j];
 				const char *path = ce->name;
 				source[argc + j] = path;
 				destination[argc + j] =
@@ -472,7 +472,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
 			sparse_and_dirty = ie_modified(&the_index,
-						       active_cache[pos], &st,
+						       the_index.cache[pos],
+						       &st,
 						       0);
 		rename_index_entry_at(&the_index, pos, dst);
 
@@ -489,7 +490,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			    path_in_sparse_checkout(dst, &the_index)) {
 				/* from out-of-cone to in-cone */
 				int dst_pos = cache_name_pos(dst, strlen(dst));
-				struct cache_entry *dst_ce = active_cache[dst_pos];
+				struct cache_entry *dst_ce = the_index.cache[dst_pos];
 
 				dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
 
@@ -500,7 +501,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				   !path_in_sparse_checkout(dst, &the_index)) {
 				/* from in-cone to out-of-cone */
 				int dst_pos = cache_name_pos(dst, strlen(dst));
-				struct cache_entry *dst_ce = active_cache[dst_pos];
+				struct cache_entry *dst_ce = the_index.cache[dst_pos];
 
 				/*
 				 * if src is clean, it will suffice to remove it
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 9ced755a6db..c50b563e2be 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -253,7 +253,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 	if (nr_trees == 1 && !opts.prefix)
 		opts.skip_cache_tree_update = 1;
 
-	cache_tree_free(&active_cache_tree);
+	cache_tree_free(&the_index.cache_tree);
 	for (i = 0; i < nr_trees; i++) {
 		struct tree *tree = trees[i];
 		parse_tree(tree);
diff --git a/builtin/reset.c b/builtin/reset.c
index 59f777a1da4..75e061f5dcf 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -161,7 +161,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 		 * to properly construct the reset sparse directory.
 		 */
 		pos = cache_name_pos(one->path, strlen(one->path));
-		if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
+		if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) ||
 		    (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
 			ce->ce_flags |= CE_SKIP_WORKTREE;
 
diff --git a/builtin/rm.c b/builtin/rm.c
index c717cdc97f5..2b23eb25438 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -35,8 +35,8 @@ static int get_ours_cache_pos(const char *path, int pos)
 {
 	int i = -pos - 1;
 
-	while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {
-		if (ce_stage(active_cache[i]) == 2)
+	while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) {
+		if (ce_stage(the_index.cache[i]) == 2)
 			return i;
 		i++;
 	}
@@ -78,7 +78,7 @@ static void submodules_absorb_gitdir_if_needed(void)
 			if (pos < 0)
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 
 		if (!S_ISGITLINK(ce->ce_mode) ||
 		    !file_exists(ce->name) ||
@@ -127,11 +127,11 @@ static int check_local_mod(struct object_id *head, int index_only)
 			if (pos < 0)
 				continue;
 
-			if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
+			if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) ||
 			    is_empty_dir(name))
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = the_index.cache[pos];
 
 		if (lstat(ce->name, &st) < 0) {
 			if (!is_missing_file_error(errno))
@@ -303,8 +303,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	if (pathspec_needs_expanded_index(&the_index, &pathspec))
 		ensure_full_index(&the_index);
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 
 		if (!include_sparse &&
 		    (ce_skip_worktree(ce) ||
diff --git a/builtin/stash.c b/builtin/stash.c
index bb5485b4095..c251419af7c 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -454,10 +454,10 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
 		 * path, but left it out of the working tree, then clear the
 		 * SKIP_WORKTREE bit and write it to the working tree.
 		 */
-		if (pos >= 0 && ce_skip_worktree(active_cache[pos])) {
+		if (pos >= 0 && ce_skip_worktree(the_index.cache[pos])) {
 			struct stat st;
 
-			ce = active_cache[pos];
+			ce = the_index.cache[pos];
 			if (!lstat(ce->name, &st)) {
 				/* Conflicting path present; relocate it */
 				struct strbuf new_path = STRBUF_INIT;
@@ -1520,8 +1520,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 
 		/* TODO: audit for interaction with sparse-index. */
 		ensure_full_index(&the_index);
-		for (i = 0; i < active_nr; i++)
-			ce_path_match(&the_index, active_cache[i], ps,
+		for (i = 0; i < the_index.cache_nr; i++)
+			ce_path_match(&the_index, the_index.cache[i], ps,
 				      ps_matched);
 
 		if (report_path_error(ps_matched, ps)) {
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index a7683d35299..808865a5e31 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -199,8 +199,8 @@ static int module_list_compute(const char **argv,
 	if (read_cache() < 0)
 		die(_("index file corrupt"));
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < the_index.cache_nr; i++) {
+		const struct cache_entry *ce = the_index.cache[i];
 
 		if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
 				    0, ps_matched, 1) ||
@@ -209,8 +209,8 @@ static int module_list_compute(const char **argv,
 
 		ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
 		list->entries[list->nr++] = ce;
-		while (i + 1 < active_nr &&
-		       !strcmp(ce->name, active_cache[i + 1]->name))
+		while (i + 1 < the_index.cache_nr &&
+		       !strcmp(ce->name, the_index.cache[i + 1]->name))
 			/*
 			 * Skip entries with the same name in different stages
 			 * to make sure an entry is returned only once.
@@ -3255,15 +3255,15 @@ static void die_on_index_match(const char *path, int force)
 		 * need to check ps_matched[0] to know if a cache
 		 * entry matched.
 		 */
-		for (i = 0; i < active_nr; i++) {
-			ce_path_match(&the_index, active_cache[i], &ps,
+		for (i = 0; i < the_index.cache_nr; i++) {
+			ce_path_match(&the_index, the_index.cache[i], &ps,
 				      ps_matched);
 
 			if (ps_matched[0]) {
 				if (!force)
 					die(_("'%s' already exists in the index"),
 					    path);
-				if (!S_ISGITLINK(active_cache[i]->ce_mode))
+				if (!S_ISGITLINK(the_index.cache[i]->ce_mode))
 					die(_("'%s' already exists in the index "
 					      "and is not a submodule"), path);
 				break;
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 533ba66df05..fec3da05abb 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -239,14 +239,14 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 	int namelen = strlen(path);
 	int pos = cache_name_pos(path, namelen);
 	if (0 <= pos) {
-		mark_fsmonitor_invalid(&the_index, active_cache[pos]);
+		mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
 		if (mark)
-			active_cache[pos]->ce_flags |= flag;
+			the_index.cache[pos]->ce_flags |= flag;
 		else
-			active_cache[pos]->ce_flags &= ~flag;
-		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
+			the_index.cache[pos]->ce_flags &= ~flag;
+		the_index.cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(&the_index, path);
-		active_cache_changed |= CE_ENTRY_CHANGED;
+		the_index.cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
 	return -1;
@@ -335,7 +335,7 @@ static int process_directory(const char *path, int len, struct stat *st)
 
 	/* Exact match: file or existing gitlink */
 	if (pos >= 0) {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = the_index.cache[pos];
 		if (S_ISGITLINK(ce->ce_mode)) {
 
 			/* Do nothing to the index if there is no HEAD! */
@@ -350,8 +350,8 @@ static int process_directory(const char *path, int len, struct stat *st)
 
 	/* Inexact match: is there perhaps a subdirectory match? */
 	pos = -pos-1;
-	while (pos < active_nr) {
-		const struct cache_entry *ce = active_cache[pos++];
+	while (pos < the_index.cache_nr) {
+		const struct cache_entry *ce = the_index.cache[pos++];
 
 		if (strncmp(ce->name, path, len))
 			break;
@@ -382,7 +382,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 		return error("'%s' is beyond a symbolic link", path);
 
 	pos = cache_name_pos(path, len);
-	ce = pos < 0 ? NULL : active_cache[pos];
+	ce = pos < 0 ? NULL : the_index.cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
 		 * working directory version is assumed "good"
@@ -444,7 +444,7 @@ static void chmod_path(char flip, const char *path)
 	pos = cache_name_pos(path, strlen(path));
 	if (pos < 0)
 		goto fail;
-	ce = active_cache[pos];
+	ce = the_index.cache[pos];
 	if (chmod_index_entry(&the_index, ce, flip) < 0)
 		goto fail;
 
@@ -642,8 +642,8 @@ static int unresolve_one(const char *path)
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_index_entry_at(&the_index, pos);
-		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+		if (pos < the_index.cache_nr) {
+			const struct cache_entry *ce = the_index.cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen))
@@ -656,8 +656,8 @@ static int unresolve_one(const char *path)
 		 * want to do anything in the former case.
 		 */
 		pos = -pos-1;
-		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+		if (pos < the_index.cache_nr) {
+			const struct cache_entry *ce = the_index.cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
 				fprintf(stderr,
@@ -752,8 +752,8 @@ static int do_reupdate(const char **paths,
 		 */
 		has_head = 0;
  redo:
-	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+	for (pos = 0; pos < the_index.cache_nr; pos++) {
+		const struct cache_entry *ce = the_index.cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
 		char *path;
@@ -782,12 +782,12 @@ static int do_reupdate(const char **paths,
 		 * path anymore, in which case, under 'allow_remove',
 		 * or worse yet 'allow_replace', active_nr may decrease.
 		 */
-		save_nr = active_nr;
+		save_nr = the_index.cache_nr;
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
 		discard_cache_entry(old);
-		if (save_nr != active_nr)
+		if (save_nr != the_index.cache_nr)
 			goto redo;
 	}
 	clear_pathspec(&pathspec);
@@ -813,7 +813,7 @@ static int refresh(struct refresh_params *o, unsigned int flag)
 		 * refresh_cache() as these are no actual errors.
 		 * cmd_status() does the same.
 		 */
-		active_cache_changed |= SOMETHING_CHANGED;
+		the_index.cache_changed |= SOMETHING_CHANGED;
 	}
 	return 0;
 }
@@ -951,7 +951,7 @@ static enum parse_opt_result unresolve_callback(
 	*has_errors = do_unresolve(ctx->argc, ctx->argv,
 				prefix, prefix ? strlen(prefix) : 0);
 	if (*has_errors)
-		active_cache_changed = 0;
+		the_index.cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -972,7 +972,7 @@ static enum parse_opt_result reupdate_callback(
 	setup_work_tree();
 	*has_errors = do_reupdate(ctx->argv + 1, prefix);
 	if (*has_errors)
-		active_cache_changed = 0;
+		the_index.cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -1178,7 +1178,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
 		if (the_index.version != preferred_index_format)
-			active_cache_changed |= SOMETHING_CHANGED;
+			the_index.cache_changed |= SOMETHING_CHANGED;
 		the_index.version = preferred_index_format;
 	}
 
@@ -1290,7 +1290,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		report(_("fsmonitor disabled"));
 	}
 
-	if (active_cache_changed || force_write) {
+	if (the_index.cache_changed || force_write) {
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
diff --git a/cache.h b/cache.h
index 79b8d479710..805e25f9a67 100644
--- a/cache.h
+++ b/cache.h
@@ -436,10 +436,7 @@ void prefetch_cache_entries(const struct index_state *istate,
 #ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
 extern struct index_state the_index;
 
-#define active_cache (the_index.cache)
 #define active_nr (the_index.cache_nr)
-#define active_cache_changed (the_index.cache_changed)
-#define active_cache_tree (the_index.cache_tree)
 
 #define read_cache() repo_read_index(the_repository)
 #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
index 89c18986160..8b1ed65e7b0 100644
--- a/contrib/coccinelle/index-compatibility.cocci
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -1,3 +1,25 @@
+// the_index.* variables
+@@
+@@
+(
+- active_cache
++ the_index.cache
+|
+- active_cache_changed
++ the_index.cache_changed
+|
+- active_cache_tree
++ the_index.cache_tree
+)
+
+@@
+identifier f != prepare_to_commit;
+@@
+  f(...) {<...
+- active_nr
++ the_index.cache_nr
+  ...>}
+
 // "the_repository" simple cases
 @@
 @@
diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
index cf1ea57d700..8207aee62c3 100644
--- a/contrib/coccinelle/index-compatibility.pending.cocci
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -1,20 +1,3 @@
-// the_index.* variables
-@@
-@@
-(
-- active_cache
-+ the_index.cache
-|
-- active_nr
-+ the_index.cache_nr
-|
-- active_cache_changed
-+ the_index.cache_changed
-|
-- active_cache_tree
-+ the_index.cache_tree
-)
-
 // "the_repository" simple cases
 @@
 @@
diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
index 0d6d7f1ecbf..8d88111b357 100644
--- a/t/helper/test-dump-cache-tree.c
+++ b/t/helper/test-dump-cache-tree.c
@@ -67,7 +67,7 @@ int cmd__dump_cache_tree(int ac, const char **av)
 	istate = the_index;
 	istate.cache_tree = another;
 	cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
-	ret = dump_cache_tree(active_cache_tree, another, "");
+	ret = dump_cache_tree(the_index.cache_tree, another, "");
 	cache_tree_free(&another);
 
 	return ret;
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
index 026c802479d..20e2e31f9e2 100644
--- a/t/helper/test-scrap-cache-tree.c
+++ b/t/helper/test-scrap-cache-tree.c
@@ -12,8 +12,8 @@ int cmd__scrap_cache_tree(int ac, const char **av)
 	hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
 	if (read_cache() < 0)
 		die("unable to read index file");
-	cache_tree_free(&active_cache_tree);
-	active_cache_tree = NULL;
+	cache_tree_free(&the_index.cache_tree);
+	the_index.cache_tree = NULL;
 	if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
 		die("unable to write index file");
 	return 0;
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 08/11] cocci: apply "pending" index-compatibility to "t/helper/*.c"
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (6 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 07/11] cocci & cache.h: apply variable section " Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 09/11] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
                     ` (2 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Apply the "index-compatibility.pending.cocci" rule to the "t/helper/*"
directory, a subsequent commit will extend cache.h to further narrow
down the use of "USE_THE_INDEX_COMPATIBILITY_MACROS" in this area.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/helper/test-cache-tree.c           |  2 +-
 t/helper/test-dump-cache-tree.c      |  2 +-
 t/helper/test-dump-untracked-cache.c |  2 +-
 t/helper/test-fast-rebase.c          |  2 +-
 t/helper/test-lazy-init-name-hash.c  | 20 ++++++++++----------
 t/helper/test-read-cache.c           |  4 ++--
 t/helper/test-scrap-cache-tree.c     |  4 ++--
 t/helper/test-write-cache.c          |  5 +++--
 8 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/t/helper/test-cache-tree.c b/t/helper/test-cache-tree.c
index 93051b25f56..5514afdfe7a 100644
--- a/t/helper/test-cache-tree.c
+++ b/t/helper/test-cache-tree.c
@@ -30,7 +30,7 @@ int cmd__cache_tree(int argc, const char **argv)
 
 	argc = parse_options(argc, argv, NULL, options, test_cache_tree_usage, 0);
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("unable to read index file"));
 
 	oidcpy(&oid, &the_index.cache_tree->oid);
diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
index 8d88111b357..4bf8bc3df6e 100644
--- a/t/helper/test-dump-cache-tree.c
+++ b/t/helper/test-dump-cache-tree.c
@@ -62,7 +62,7 @@ int cmd__dump_cache_tree(int ac, const char **av)
 	int ret;
 
 	setup_git_directory();
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die("unable to read index file");
 	istate = the_index;
 	istate.cache_tree = another;
diff --git a/t/helper/test-dump-untracked-cache.c b/t/helper/test-dump-untracked-cache.c
index 99010614f6d..ffe34736e53 100644
--- a/t/helper/test-dump-untracked-cache.c
+++ b/t/helper/test-dump-untracked-cache.c
@@ -51,7 +51,7 @@ int cmd__dump_untracked_cache(int ac, const char **av)
 	xsetenv("GIT_CONFIG_VALUE_0", "keep", 1);
 
 	setup_git_directory();
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die("unable to read index file");
 	uc = the_index.untracked;
 	if (!uc) {
diff --git a/t/helper/test-fast-rebase.c b/t/helper/test-fast-rebase.c
index 45665ec19a5..9def2ba6889 100644
--- a/t/helper/test-fast-rebase.c
+++ b/t/helper/test-fast-rebase.c
@@ -123,7 +123,7 @@ int cmd__fast_rebase(int argc, const char **argv)
 		die(_("Cannot read HEAD"));
 	assert(oideq(&onto->object.oid, &head));
 
-	hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
 	if (repo_read_index(the_repository) < 0)
 		BUG("Could not read index");
 
diff --git a/t/helper/test-lazy-init-name-hash.c b/t/helper/test-lazy-init-name-hash.c
index cd1b4c9736e..7e8723a1900 100644
--- a/t/helper/test-lazy-init-name-hash.c
+++ b/t/helper/test-lazy-init-name-hash.c
@@ -32,7 +32,7 @@ static void dump_run(void)
 	struct dir_entry *dir;
 	struct cache_entry *ce;
 
-	read_cache();
+	repo_read_index(the_repository);
 	if (single) {
 		test_lazy_init_name_hash(&the_index, 0);
 	} else {
@@ -49,7 +49,7 @@ static void dump_run(void)
 				ent /* member name */)
 		printf("name %08x %s\n", ce->ent.hash, ce->name);
 
-	discard_cache();
+	discard_index(&the_index);
 }
 
 /*
@@ -66,7 +66,7 @@ static uint64_t time_runs(int try_threaded)
 
 	for (i = 0; i < count; i++) {
 		t0 = getnanotime();
-		read_cache();
+		repo_read_index(the_repository);
 		t1 = getnanotime();
 		nr_threads_used = test_lazy_init_name_hash(&the_index, try_threaded);
 		t2 = getnanotime();
@@ -89,7 +89,7 @@ static uint64_t time_runs(int try_threaded)
 				   the_index.cache_nr);
 		fflush(stdout);
 
-		discard_cache();
+		discard_index(&the_index);
 	}
 
 	avg = sum / count;
@@ -113,9 +113,9 @@ static void analyze_run(void)
 	int i;
 	int nr;
 
-	read_cache();
+	repo_read_index(the_repository);
 	cache_nr_limit = the_index.cache_nr;
-	discard_cache();
+	discard_index(&the_index);
 
 	nr = analyze;
 	while (1) {
@@ -128,23 +128,23 @@ static void analyze_run(void)
 			nr = cache_nr_limit;
 
 		for (i = 0; i < count; i++) {
-			read_cache();
+			repo_read_index(the_repository);
 			the_index.cache_nr = nr; /* cheap truncate of index */
 			t1s = getnanotime();
 			test_lazy_init_name_hash(&the_index, 0);
 			t2s = getnanotime();
 			sum_single += (t2s - t1s);
 			the_index.cache_nr = cache_nr_limit;
-			discard_cache();
+			discard_index(&the_index);
 
-			read_cache();
+			repo_read_index(the_repository);
 			the_index.cache_nr = nr; /* cheap truncate of index */
 			t1m = getnanotime();
 			nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
 			t2m = getnanotime();
 			sum_multi += (t2m - t1m);
 			the_index.cache_nr = cache_nr_limit;
-			discard_cache();
+			discard_index(&the_index);
 
 			if (!nr_threads_used)
 				printf("    [size %8d] [single %f]   non-threaded code path used\n",
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
index b736ef16421..a089be9104c 100644
--- a/t/helper/test-read-cache.c
+++ b/t/helper/test-read-cache.c
@@ -20,7 +20,7 @@ int cmd__read_cache(int argc, const char **argv)
 	git_config(git_default_config, NULL);
 
 	for (i = 0; i < cnt; i++) {
-		read_cache();
+		repo_read_index(the_repository);
 		if (name) {
 			int pos;
 
@@ -33,7 +33,7 @@ int cmd__read_cache(int argc, const char **argv)
 			       ce_uptodate(the_index.cache[pos]) ? "" : " not");
 			write_file(name, "%d\n", i);
 		}
-		discard_cache();
+		discard_index(&the_index);
 	}
 	return 0;
 }
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
index 20e2e31f9e2..9206dee1fd2 100644
--- a/t/helper/test-scrap-cache-tree.c
+++ b/t/helper/test-scrap-cache-tree.c
@@ -9,8 +9,8 @@ int cmd__scrap_cache_tree(int ac, const char **av)
 	struct lock_file index_lock = LOCK_INIT;
 
 	setup_git_directory();
-	hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
-	if (read_cache() < 0)
+	repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
+	if (repo_read_index(the_repository) < 0)
 		die("unable to read index file");
 	cache_tree_free(&the_index.cache_tree);
 	the_index.cache_tree = NULL;
diff --git a/t/helper/test-write-cache.c b/t/helper/test-write-cache.c
index 8837717d36a..66287d6d47d 100644
--- a/t/helper/test-write-cache.c
+++ b/t/helper/test-write-cache.c
@@ -9,9 +9,10 @@ int cmd__write_cache(int argc, const char **argv)
 	if (argc == 2)
 		cnt = strtol(argv[1], NULL, 0);
 	setup_git_directory();
-	read_cache();
+	repo_read_index(the_repository);
 	for (i = 0; i < cnt; i++) {
-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &index_lock,
+				       LOCK_DIE_ON_ERROR);
 		if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
 			die("unable to write index file");
 	}
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 09/11] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE"
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (7 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 08/11] cocci: apply "pending" index-compatibility to "t/helper/*.c" Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 10/11] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 11/11] cocci: apply "pending" index-compatibility to some "builtin/*.c" Ævar Arnfjörð Bjarmason
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Split up the "USE_THE_INDEX_COMPATIBILITY_MACROS" into that setting
and a more narrow "USE_THE_INDEX_VARIABLE". In the case of these
built-ins we only need "the_index" variable, but not the compatibility
wrapper for functions we're not using.

Let's then have some users of "USE_THE_INDEX_COMPATIBILITY_MACROS" use
this more narrow and descriptive define.

For context: The USE_THE_INDEX_COMPATIBILITY_MACROS macro was added to
test-tool.h in f8adbec9fea (cache.h: flip
NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24).

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/cat-file.c   | 2 +-
 builtin/difftool.c   | 2 +-
 builtin/merge-tree.c | 2 +-
 builtin/pull.c       | 2 +-
 cache.h              | 6 +++++-
 repository.c         | 2 +-
 6 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index fa7bd891692..b3be58b1fb0 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/builtin/difftool.c b/builtin/difftool.c
index d7f08c8a7fa..d9b76226f6a 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -11,7 +11,7 @@
  *
  * Copyright (C) 2016 Johannes Schindelin
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index fe853aa8f91..e3767087bbf 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "tree-walk.h"
 #include "xdiff-interface.h"
diff --git a/builtin/pull.c b/builtin/pull.c
index 4a2a6db2dce..1ab4de0005d 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -5,7 +5,7 @@
  *
  * Fetch one or more remote refs and merge it/them into the current HEAD.
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
diff --git a/cache.h b/cache.h
index 805e25f9a67..707806d2125 100644
--- a/cache.h
+++ b/cache.h
@@ -433,9 +433,11 @@ typedef int (*must_prefetch_predicate)(const struct cache_entry *);
 void prefetch_cache_entries(const struct index_state *istate,
 			    must_prefetch_predicate must_prefetch);
 
-#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
+#if defined(USE_THE_INDEX_COMPATIBILITY_MACROS) || defined(USE_THE_INDEX_VARIABLE)
 extern struct index_state the_index;
 
+#ifndef USE_THE_INDEX_VARIABLE
+#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
 #define active_nr (the_index.cache_nr)
 
 #define read_cache() repo_read_index(the_repository)
@@ -447,6 +449,8 @@ extern struct index_state the_index;
 #define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
+#endif
+#endif
 
 #define TYPE_BITS 3
 
diff --git a/repository.c b/repository.c
index 5d166b692c8..3427085fd6d 100644
--- a/repository.c
+++ b/repository.c
@@ -2,7 +2,7 @@
  * not really _using_ the compat macros, just make sure the_index
  * declaration matches the definition in this file.
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "repository.h"
 #include "object-store.h"
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 10/11] cache.h & test-tool.h: add & use "USE_THE_INDEX_VARIABLE"
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (8 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 09/11] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  2022-11-19 13:07   ` [PATCH v2 11/11] cocci: apply "pending" index-compatibility to some "builtin/*.c" Ævar Arnfjörð Bjarmason
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

In a preceding commit we fully applied the
"index-compatibility.pending.cocci" rule to "t/helper/*".

Let's now stop defining "USE_THE_INDEX_COMPATIBILITY_MACROS" in
test-tool.h itself, and instead instead define
"USE_THE_INDEX_VARIABLE" in the individual test helpers that need
it. This mirrors how we do the same thing in the "builtin/" directory.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/helper/test-cache-tree.c           | 1 +
 t/helper/test-dump-cache-tree.c      | 1 +
 t/helper/test-dump-split-index.c     | 1 +
 t/helper/test-dump-untracked-cache.c | 2 +-
 t/helper/test-fast-rebase.c          | 2 +-
 t/helper/test-lazy-init-name-hash.c  | 1 +
 t/helper/test-read-cache.c           | 1 +
 t/helper/test-scrap-cache-tree.c     | 1 +
 t/helper/test-tool.h                 | 1 -
 t/helper/test-write-cache.c          | 1 +
 10 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/t/helper/test-cache-tree.c b/t/helper/test-cache-tree.c
index 5514afdfe7a..9159a173015 100644
--- a/t/helper/test-cache-tree.c
+++ b/t/helper/test-cache-tree.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "tree.h"
diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
index 4bf8bc3df6e..454f17b1a0c 100644
--- a/t/helper/test-dump-cache-tree.c
+++ b/t/helper/test-dump-cache-tree.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "tree.h"
diff --git a/t/helper/test-dump-split-index.c b/t/helper/test-dump-split-index.c
index a209880eb37..0ea97b84072 100644
--- a/t/helper/test-dump-split-index.c
+++ b/t/helper/test-dump-split-index.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "split-index.h"
diff --git a/t/helper/test-dump-untracked-cache.c b/t/helper/test-dump-untracked-cache.c
index ffe34736e53..6d53683f13b 100644
--- a/t/helper/test-dump-untracked-cache.c
+++ b/t/helper/test-dump-untracked-cache.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "dir.h"
diff --git a/t/helper/test-fast-rebase.c b/t/helper/test-fast-rebase.c
index 9def2ba6889..efc82dd80c5 100644
--- a/t/helper/test-fast-rebase.c
+++ b/t/helper/test-fast-rebase.c
@@ -10,7 +10,7 @@
  * refactoring is the better route).
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 
 #include "cache-tree.h"
diff --git a/t/helper/test-lazy-init-name-hash.c b/t/helper/test-lazy-init-name-hash.c
index 7e8723a1900..ab86c14c8ba 100644
--- a/t/helper/test-lazy-init-name-hash.c
+++ b/t/helper/test-lazy-init-name-hash.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "parse-options.h"
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
index a089be9104c..23e9e27109f 100644
--- a/t/helper/test-read-cache.c
+++ b/t/helper/test-read-cache.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "config.h"
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
index 9206dee1fd2..a26107ed70a 100644
--- a/t/helper/test-scrap-cache-tree.c
+++ b/t/helper/test-scrap-cache-tree.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "lockfile.h"
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index da7cd6351a6..2e20a16eb82 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -1,7 +1,6 @@
 #ifndef TEST_TOOL_H
 #define TEST_TOOL_H
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 
 int cmd__advise_if_enabled(int argc, const char **argv);
diff --git a/t/helper/test-write-cache.c b/t/helper/test-write-cache.c
index 66287d6d47d..7d45cd61e82 100644
--- a/t/helper/test-write-cache.c
+++ b/t/helper/test-write-cache.c
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_VARIABLE
 #include "test-tool.h"
 #include "cache.h"
 #include "lockfile.h"
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 11/11] cocci: apply "pending" index-compatibility to some "builtin/*.c"
  2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
                     ` (9 preceding siblings ...)
  2022-11-19 13:07   ` [PATCH v2 10/11] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
@ 2022-11-19 13:07   ` Ævar Arnfjörð Bjarmason
  10 siblings, 0 replies; 32+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-19 13:07 UTC (permalink / raw)
  To: git
  Cc: Taylor Blau, Junio C Hamano, Phillip Wood,
	Ævar Arnfjörð Bjarmason

Apply "index-compatibility.pending.cocci" rule to "builtin/*", but
exclude those where we conflict with in-flight changes.

As a result some of them end up using only "the_index", so let's have
them use the more narrow "USE_THE_INDEX_VARIABLE" rather than
"USE_THE_INDEX_COMPATIBILITY_MACROS".

Manual changes not made by coccinelle, that were squashed in:

* Whitespace-wrap argument lists for repo_hold_locked_index(),
  repo_read_index_preload() and repo_refresh_and_write_index(), in cases
  where the line became too long after the transformation.
* Change "refresh_cache()" to "refresh_index()" in a comment in
  "builtin/update-index.c".
* For those whose call was followed by perror("<macro-name>"), change
  it to perror("<function-name>"), referring to the new function.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/add.c                                 |  8 +--
 builtin/am.c                                  | 21 ++++----
 builtin/check-attr.c                          |  4 +-
 builtin/check-ignore.c                        |  4 +-
 builtin/checkout-index.c                      |  9 ++--
 builtin/checkout.c                            | 12 ++---
 builtin/clean.c                               |  4 +-
 builtin/clone.c                               |  4 +-
 builtin/commit.c                              | 40 +++++++-------
 builtin/describe.c                            |  7 +--
 builtin/diff-files.c                          |  5 +-
 builtin/diff-index.c                          |  9 ++--
 builtin/diff-tree.c                           |  4 +-
 builtin/diff.c                                | 25 +++++----
 builtin/fsck.c                                |  4 +-
 builtin/merge-index.c                         |  6 +--
 builtin/merge-ours.c                          |  3 +-
 builtin/merge.c                               | 19 ++++---
 builtin/mv.c                                  | 16 +++---
 builtin/read-tree.c                           |  4 +-
 builtin/rebase.c                              |  6 +--
 builtin/reset.c                               |  9 ++--
 builtin/rev-parse.c                           |  4 +-
 builtin/rm.c                                  |  8 +--
 builtin/stash.c                               | 35 ++++++------
 builtin/submodule--helper.c                   | 14 ++---
 builtin/update-index.c                        | 19 +++----
 cache.h                                       |  5 --
 contrib/coccinelle/index-compatibility.cocci  | 53 ++++++++++++++++++
 .../index-compatibility.pending.cocci         | 54 -------------------
 30 files changed, 213 insertions(+), 202 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 23f9f0e08bb..76277df326b 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2006 Linus Torvalds
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
@@ -312,7 +312,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
 
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("Could not read the index"));
 
 	repo_init_revisions(the_repository, &rev, prefix);
@@ -544,7 +544,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	/*
 	 * Check the "pathspec '%s' did not match any files" block
@@ -587,7 +587,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		 (!(addremove || take_worktree_changes)
 		  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 
-	if (read_cache_preload(&pathspec) < 0)
+	if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
 		die(_("index file corrupt"));
 
 	die_in_unpopulated_submodule(&the_index, prefix);
diff --git a/builtin/am.c b/builtin/am.c
index f362a1c29fd..30c9b3a9cd7 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1519,8 +1519,8 @@ static int run_apply(const struct am_state *state, const char *index_file)
 
 	if (index_file) {
 		/* Reload index as apply_all_patches() will have modified it. */
-		discard_cache();
-		read_cache_from(index_file);
+		discard_index(&the_index);
+		read_index_from(&the_index, index_file, get_git_dir());
 	}
 
 	return 0;
@@ -1562,8 +1562,8 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
 	if (build_fake_ancestor(state, index_path))
 		return error("could not build fake ancestor");
 
-	discard_cache();
-	read_cache_from(index_path);
+	discard_index(&the_index);
+	read_index_from(&the_index, index_path, get_git_dir());
 
 	if (write_index_as_tree(&orig_tree, &the_index, index_path, 0, NULL))
 		return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
@@ -1596,8 +1596,8 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
 
 	say(state, stdout, _("Falling back to patching base and 3-way merge..."));
 
-	discard_cache();
-	read_cache();
+	discard_index(&the_index);
+	repo_read_index(the_repository);
 
 	/*
 	 * This is not so wrong. Depending on which base we picked, orig_tree
@@ -1781,7 +1781,8 @@ static void am_run(struct am_state *state, int resume)
 
 	unlink(am_path(state, "dirtyindex"));
 
-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0)
+	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
+					 NULL, NULL, NULL) < 0)
 		die(_("unable to write index file"));
 
 	if (repo_index_has_changes(the_repository, NULL, &sb)) {
@@ -1967,9 +1968,9 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
 	if (parse_tree(head) || parse_tree(remote))
 		return -1;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
-	refresh_cache(REFRESH_QUIET);
+	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
 
 	memset(&opts, 0, sizeof(opts));
 	opts.head_idx = 1;
@@ -2007,7 +2008,7 @@ static int merge_tree(struct tree *tree)
 	if (parse_tree(tree))
 		return -1;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	memset(&opts, 0, sizeof(opts));
 	opts.head_idx = 1;
diff --git a/builtin/check-attr.c b/builtin/check-attr.c
index dd833977864..0fef10eb6bc 100644
--- a/builtin/check-attr.c
+++ b/builtin/check-attr.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
@@ -115,7 +115,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, check_attr_options,
 			     check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
 
-	if (read_cache() < 0) {
+	if (repo_read_index(the_repository) < 0) {
 		die("invalid cache");
 	}
 
diff --git a/builtin/check-ignore.c b/builtin/check-ignore.c
index 21912569650..ab776061c7c 100644
--- a/builtin/check-ignore.c
+++ b/builtin/check-ignore.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
@@ -179,7 +179,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
 		die(_("--non-matching is only valid with --verbose"));
 
 	/* read_cache() is only necessary so we can watch out for submodules. */
-	if (!no_index && read_cache() < 0)
+	if (!no_index && repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	setup_standard_excludes(&dir);
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index c2aee450c40..cf6fba97ba7 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -4,7 +4,7 @@
  * Copyright (C) 2005 Linus Torvalds
  *
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "dir.h"
@@ -65,7 +65,7 @@ static void write_tempfile_record(const char *name, const char *prefix)
 static int checkout_file(const char *name, const char *prefix)
 {
 	int namelen = strlen(name);
-	int pos = cache_name_pos(name, namelen);
+	int pos = index_name_pos(&the_index, name, namelen);
 	int has_same_name = 0;
 	int is_file = 0;
 	int is_skipped = 1;
@@ -249,7 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	if (read_cache() < 0) {
+	if (repo_read_index(the_repository) < 0) {
 		die("invalid cache");
 	}
 
@@ -270,7 +270,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	if (index_opt && !state.base_dir_len && !to_tempfile) {
 		state.refresh_cache = 1;
 		state.istate = &the_index;
-		hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &lock_file,
+				       LOCK_DIE_ON_ERROR);
 	}
 
 	get_parallel_checkout_configs(&pc_workers, &pc_threshold);
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 3fd303f5604..3fa29a08eea 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "advice.h"
 #include "blob.h"
@@ -148,7 +148,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
 	 * entry in place. Whether it is UPTODATE or not, checkout_entry will
 	 * do the right thing.
 	 */
-	pos = cache_name_pos(ce->name, ce->ce_namelen);
+	pos = index_name_pos(&the_index, ce->name, ce->ce_namelen);
 	if (pos >= 0) {
 		struct cache_entry *old = the_index.cache[pos];
 		if (ce->ce_mode == old->ce_mode &&
@@ -529,7 +529,7 @@ static int checkout_paths(const struct checkout_opts *opts,
 	}
 
 	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
-	if (read_cache_preload(&opts->pathspec) < 0)
+	if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0)
 		return error(_("index file corrupt"));
 
 	if (opts->source_tree)
@@ -741,8 +741,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
 	struct lock_file lock_file = LOCK_INIT;
 	struct tree *new_tree;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
-	if (read_cache_preload(NULL) < 0)
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
+	if (repo_read_index_preload(the_repository, NULL, 0) < 0)
 		return error(_("index file corrupt"));
 
 	resolve_undo_clear_index(&the_index);
@@ -762,7 +762,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
 		struct unpack_trees_options topts;
 		const struct object_id *old_commit_oid;
 
-		refresh_cache(REFRESH_QUIET);
+		refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
 
 		if (unmerged_index(&the_index)) {
 			error(_("you need to resolve your current index first"));
diff --git a/builtin/clean.c b/builtin/clean.c
index 7084e685f6f..b2701a28158 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -6,7 +6,7 @@
  * Based on git-clean.sh by Pavel Roskin
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
@@ -1012,7 +1012,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
diff --git a/builtin/clone.c b/builtin/clone.c
index 0e4348686b6..f518bb2dc1f 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -8,7 +8,7 @@
  * Clone a repository into a different directory that does not yet exist.
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
@@ -703,7 +703,7 @@ static int checkout(int submodule_progress, int filter_submodules)
 	/* We need to be in the new work tree for the checkout */
 	setup_work_tree();
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	memset(&opts, 0, sizeof opts);
 	opts.update = 1;
diff --git a/builtin/commit.c b/builtin/commit.c
index abbed750276..06b1330346f 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -316,7 +316,7 @@ static void create_base_index(const struct commit *current_head)
 	struct tree_desc t;
 
 	if (!current_head) {
-		discard_cache();
+		discard_index(&the_index);
 		return;
 	}
 
@@ -343,7 +343,7 @@ static void refresh_cache_or_die(int refresh_flags)
 	 * refresh_flags contains REFRESH_QUIET, so the only errors
 	 * are for unmerged entries.
 	 */
-	if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
+	if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
 		die_resolve_conflict("commit");
 }
 
@@ -382,12 +382,13 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	    (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
 		die(_("No paths with --include/--only does not make sense."));
 
-	if (read_cache_preload(&pathspec) < 0)
+	if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
 		die(_("index file corrupt"));
 
 	if (interactive) {
 		char *old_index_env = NULL, *old_repo_index_file;
-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &index_lock,
+				       LOCK_DIE_ON_ERROR);
 
 		refresh_cache_or_die(refresh_flags);
 
@@ -410,8 +411,9 @@ static const char *prepare_index(const char **argv, const char *prefix,
 			unsetenv(INDEX_ENVIRONMENT);
 		FREE_AND_NULL(old_index_env);
 
-		discard_cache();
-		read_cache_from(get_lock_file_path(&index_lock));
+		discard_index(&the_index);
+		read_index_from(&the_index, get_lock_file_path(&index_lock),
+				get_git_dir());
 		if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
 			if (reopen_lock_file(&index_lock) < 0)
 				die(_("unable to write index file"));
@@ -438,7 +440,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	 * (B) on failure, rollback the real index.
 	 */
 	if (all || (also && pathspec.nr)) {
-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &index_lock,
+				       LOCK_DIE_ON_ERROR);
 		add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
 		refresh_cache_or_die(refresh_flags);
 		update_main_cache_tree(WRITE_TREE_SILENT);
@@ -459,7 +462,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	 * We still need to refresh the index here.
 	 */
 	if (!only && !pathspec.nr) {
-		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &index_lock,
+				       LOCK_DIE_ON_ERROR);
 		refresh_cache_or_die(refresh_flags);
 		if (the_index.cache_changed
 		    || !cache_tree_fully_valid(the_index.cache_tree))
@@ -505,13 +509,13 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
 		exit(1);
 
-	discard_cache();
-	if (read_cache() < 0)
+	discard_index(&the_index);
+	if (repo_read_index(the_repository) < 0)
 		die(_("cannot read the index"));
 
-	hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
 	add_remove_files(&partial);
-	refresh_cache(REFRESH_QUIET);
+	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
 	update_main_cache_tree(WRITE_TREE_SILENT);
 	if (write_locked_index(&the_index, &index_lock, 0))
 		die(_("unable to write new_index file"));
@@ -523,14 +527,14 @@ static const char *prepare_index(const char **argv, const char *prefix,
 
 	create_base_index(current_head);
 	add_remove_files(&partial);
-	refresh_cache(REFRESH_QUIET);
+	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
 
 	if (write_locked_index(&the_index, &false_lock, 0))
 		die(_("unable to write temporary index file"));
 
-	discard_cache();
+	discard_index(&the_index);
 	ret = get_lock_file_path(&false_lock);
-	read_cache_from(ret);
+	read_index_from(&the_index, ret, get_git_dir());
 out:
 	string_list_clear(&partial, 0);
 	clear_pathspec(&pathspec);
@@ -1068,9 +1072,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		 * and could have updated it. We must do this before we invoke
 		 * the editor and after we invoke run_status above.
 		 */
-		discard_cache();
+		discard_index(&the_index);
 	}
-	read_cache_from(index_file);
+	read_index_from(&the_index, index_file, get_git_dir());
 
 	if (update_main_cache_tree(0)) {
 		error(_("Error building trees"));
@@ -1556,7 +1560,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 		      &s.pathspec, NULL, NULL);
 
 	if (use_optional_locks())
-		fd = hold_locked_index(&index_lock, 0);
+		fd = repo_hold_locked_index(the_repository, &index_lock, 0);
 	else
 		fd = -1;
 
diff --git a/builtin/describe.c b/builtin/describe.c
index 23e3f05fb10..eea1e330c00 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "lockfile.h"
@@ -653,10 +653,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			int fd, result;
 
 			setup_work_tree();
-			read_cache();
+			repo_read_index(the_repository);
 			refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
 				      NULL, NULL, NULL);
-			fd = hold_locked_index(&index_lock, 0);
+			fd = repo_hold_locked_index(the_repository,
+						    &index_lock, 0);
 			if (0 <= fd)
 				repo_update_index_if_able(the_repository, &index_lock);
 
diff --git a/builtin/diff-files.c b/builtin/diff-files.c
index 096ea2fedbc..dc991f753bb 100644
--- a/builtin/diff-files.c
+++ b/builtin/diff-files.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "diff.h"
@@ -76,8 +75,8 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
 	    (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
 		diff_merges_set_dense_combined_if_unset(&rev);
 
-	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
-		perror("read_cache_preload");
+	if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
+		perror("repo_read_index_preload");
 		result = -1;
 		goto cleanup;
 	}
diff --git a/builtin/diff-index.c b/builtin/diff-index.c
index aea139b9d8f..35dc9b23eef 100644
--- a/builtin/diff-index.c
+++ b/builtin/diff-index.c
@@ -1,4 +1,3 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "diff.h"
@@ -62,12 +61,12 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 		usage(diff_cache_usage);
 	if (!(option & DIFF_INDEX_CACHED)) {
 		setup_work_tree();
-		if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
-			perror("read_cache_preload");
+		if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
+			perror("repo_read_index_preload");
 			return -1;
 		}
-	} else if (read_cache() < 0) {
-		perror("read_cache");
+	} else if (repo_read_index(the_repository) < 0) {
+		perror("repo_read_index");
 		return -1;
 	}
 	result = run_diff_index(&rev, option);
diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c
index 85e8c81e594..25b853b85ca 100644
--- a/builtin/diff-tree.c
+++ b/builtin/diff-tree.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "diff.h"
@@ -120,7 +120,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 	repo_init_revisions(the_repository, opt, prefix);
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 	opt->abbrev = 0;
 	opt->diff = 1;
diff --git a/builtin/diff.c b/builtin/diff.c
index 854d2c5a5c4..163f2c6a874 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2006 Junio C Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "ewah/ewok.h"
@@ -157,12 +157,13 @@ static int builtin_diff_index(struct rev_info *revs,
 		usage(builtin_diff_usage);
 	if (!(option & DIFF_INDEX_CACHED)) {
 		setup_work_tree();
-		if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
-			perror("read_cache_preload");
+		if (repo_read_index_preload(the_repository,
+					    &revs->diffopt.pathspec, 0) < 0) {
+			perror("repo_read_index_preload");
 			return -1;
 		}
-	} else if (read_cache() < 0) {
-		perror("read_cache");
+	} else if (repo_read_index(the_repository) < 0) {
+		perror("repo_read_cache");
 		return -1;
 	}
 	return run_diff_index(revs, option);
@@ -239,12 +240,13 @@ static void refresh_index_quietly(void)
 	struct lock_file lock_file = LOCK_INIT;
 	int fd;
 
-	fd = hold_locked_index(&lock_file, 0);
+	fd = repo_hold_locked_index(the_repository, &lock_file, 0);
 	if (fd < 0)
 		return;
-	discard_cache();
-	read_cache();
-	refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
+	discard_index(&the_index);
+	repo_read_index(the_repository);
+	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL,
+		      NULL);
 	repo_update_index_if_able(the_repository, &lock_file);
 }
 
@@ -279,8 +281,9 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
 		diff_merges_set_dense_combined_if_unset(revs);
 
 	setup_work_tree();
-	if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
-		perror("read_cache_preload");
+	if (repo_read_index_preload(the_repository, &revs->diffopt.pathspec,
+				    0) < 0) {
+		perror("repo_read_index_preload");
 		return -1;
 	}
 	return run_diff_files(revs, options);
diff --git a/builtin/fsck.c b/builtin/fsck.c
index cd47675e371..d207bd909b4 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "repository.h"
@@ -958,7 +958,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 	if (keep_cache_objects) {
 		verify_index_checksum = 1;
 		verify_ce_order = 1;
-		read_cache();
+		repo_read_index(the_repository);
 		/* TODO: audit for interaction with sparse-index. */
 		ensure_full_index(&the_index);
 		for (i = 0; i < the_index.cache_nr; i++) {
diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index a334c513c9d..452f833ac46 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "run-command.h"
 
@@ -47,7 +47,7 @@ static int merge_entry(int pos, const char *path)
 
 static void merge_one_path(const char *path)
 {
-	int pos = cache_name_pos(path, strlen(path));
+	int pos = index_name_pos(&the_index, path, strlen(path));
 
 	/*
 	 * If it already exists in the cache as stage0, it's
@@ -82,7 +82,7 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix)
 	if (argc < 3)
 		usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
 
-	read_cache();
+	repo_read_index(the_repository);
 
 	/* TODO: audit for interaction with sparse-index. */
 	ensure_full_index(&the_index);
diff --git a/builtin/merge-ours.c b/builtin/merge-ours.c
index 3583cff71c7..284eb486098 100644
--- a/builtin/merge-ours.c
+++ b/builtin/merge-ours.c
@@ -7,7 +7,6 @@
  *
  * Pretend we resolved the heads, but declare our tree trumps everybody else.
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 #include "builtin.h"
 #include "diff.h"
@@ -25,7 +24,7 @@ int cmd_merge_ours(int argc, const char **argv, const char *prefix)
 	 * commit.  The index must match HEAD, or this merge cannot go
 	 * through.
 	 */
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die_errno("read_cache failed");
 	if (index_differs_from(the_repository, "HEAD", NULL, 0))
 		return 2;
diff --git a/builtin/merge.c b/builtin/merge.c
index 27218ee93c9..dd474371a25 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -318,7 +318,7 @@ static int save_state(struct object_id *stash)
 	int rc = -1;
 
 	fd = repo_hold_locked_index(the_repository, &lock_file, 0);
-	refresh_cache(REFRESH_QUIET);
+	refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
 	if (0 <= fd)
 		repo_update_index_if_able(the_repository, &lock_file);
 	rollback_lock_file(&lock_file);
@@ -716,7 +716,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 {
 	const char *head_arg = "HEAD";
 
-	if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
+	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
+					 SKIP_IF_UNCHANGED, 0, NULL, NULL,
+					 NULL) < 0)
 		return error(_("Unable to write index."));
 
 	if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") ||
@@ -750,7 +752,8 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 		for (j = common; j; j = j->next)
 			commit_list_insert(j->item, &reversed);
 
-		hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &lock,
+				       LOCK_DIE_ON_ERROR);
 		if (!strcmp(strategy, "ort"))
 			clean = merge_ort_recursive(&o, head, remoteheads->item,
 						    reversed, &result);
@@ -859,9 +862,9 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		 * the editor and after we invoke run_status above.
 		 */
 		if (invoked_hook)
-			discard_cache();
+			discard_index(&the_index);
 	}
-	read_cache_from(index_file);
+	read_index_from(&the_index, index_file, get_git_dir());
 	strbuf_addbuf(&msg, &merge_msg);
 	if (squash)
 		BUG("the control must not reach here under --squash");
@@ -910,7 +913,9 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
 	struct object_id result_tree, result_commit;
 	struct commit_list *parents, **pptr = &parents;
 
-	if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
+	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
+					 SKIP_IF_UNCHANGED, 0, NULL, NULL,
+					 NULL) < 0)
 		return error(_("Unable to write index."));
 
 	write_tree_trivial(&result_tree);
@@ -1602,7 +1607,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		 * We are not doing octopus, not fast-forward, and have
 		 * only one common.
 		 */
-		refresh_cache(REFRESH_QUIET);
+		refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
 		if (allow_trivial && fast_forward != FF_ONLY) {
 			/*
 			 * Must first ensure that index matches HEAD before
diff --git a/builtin/mv.c b/builtin/mv.c
index 883cf3ff12b..19790ce38fa 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -106,7 +106,7 @@ static int index_range_of_same_dir(const char *src, int length,
 	const char *src_w_slash = add_slash(src);
 	int first, last, len_w_slash = length + 1;
 
-	first = cache_name_pos(src_w_slash, len_w_slash);
+	first = index_name_pos(&the_index, src_w_slash, len_w_slash);
 	if (first >= 0)
 		die(_("%.*s is in index"), len_w_slash, src_w_slash);
 
@@ -136,7 +136,7 @@ static int empty_dir_has_sparse_contents(const char *name)
 	const char *with_slash = add_slash(name);
 	int length = strlen(with_slash);
 
-	int pos = cache_name_pos(with_slash, length);
+	int pos = index_name_pos(&the_index, with_slash, length);
 	const struct cache_entry *ce;
 
 	if (pos < 0) {
@@ -189,8 +189,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	if (--argc < 1)
 		usage_with_options(builtin_mv_usage, builtin_mv_options);
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
-	if (read_cache() < 0)
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	source = internal_prefix_pathspec(prefix, argv, argc, 0);
@@ -255,7 +255,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 			int pos;
 			const struct cache_entry *ce;
 
-			pos = cache_name_pos(src, length);
+			pos = index_name_pos(&the_index, src, length);
 			if (pos < 0) {
 				const char *src_w_slash = add_slash(src);
 				if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
@@ -278,7 +278,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				goto act_on_entry;
 			}
 			/* Check if dst exists in index */
-			if (cache_name_pos(dst, strlen(dst)) < 0) {
+			if (index_name_pos(&the_index, dst, strlen(dst)) < 0) {
 				modes[i] |= SPARSE;
 				goto act_on_entry;
 			}
@@ -303,7 +303,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 dir_check:
 		if (S_ISDIR(st.st_mode)) {
 			int j, dst_len, n;
-			int first = cache_name_pos(src, length), last;
+			int first = index_name_pos(&the_index, src, length), last;
 
 			if (first >= 0) {
 				prepare_move_submodule(src, first,
@@ -468,7 +468,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
 			continue;
 
-		pos = cache_name_pos(src, strlen(src));
+		pos = index_name_pos(&the_index, src, strlen(src));
 		assert(pos >= 0);
 		if (!(mode & SPARSE) && !lstat(src, &st))
 			sparse_and_dirty = ie_modified(&the_index,
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index c50b563e2be..f702f9d47bb 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -4,7 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "lockfile.h"
@@ -176,7 +176,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	/*
 	 * NEEDSWORK
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 5d855fd8f51..1384008fe09 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -4,7 +4,7 @@
  * Copyright (c) 2018 Pratik Karki
  */
 
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "run-command.h"
 #include "exec-cmd.h"
@@ -292,7 +292,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
 	if (ret)
 		error(_("could not generate todo list"));
 	else {
-		discard_cache();
+		discard_index(&the_index);
 		if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
 						&todo_list))
 			BUG("unusable todo list");
@@ -1268,7 +1268,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 		if (get_oid("HEAD", &head))
 			die(_("Cannot read HEAD"));
 
-		fd = hold_locked_index(&lock_file, 0);
+		fd = repo_hold_locked_index(the_repository, &lock_file, 0);
 		if (repo_read_index(the_repository) < 0)
 			die(_("could not read index"));
 		refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
diff --git a/builtin/reset.c b/builtin/reset.c
index 75e061f5dcf..1fa86edb4c9 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -7,7 +7,7 @@
  *
  * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
@@ -160,7 +160,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
 		 * if this entry is outside the sparse cone - this is necessary
 		 * to properly construct the reset sparse directory.
 		 */
-		pos = cache_name_pos(one->path, strlen(one->path));
+		pos = index_name_pos(&the_index, one->path, strlen(one->path));
 		if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) ||
 		    (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
 			ce->ce_flags |= CE_SKIP_WORKTREE;
@@ -423,7 +423,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	/* Soft reset does not touch the index file nor the working tree
@@ -434,7 +434,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 
 	if (reset_type != SOFT) {
 		struct lock_file lock = LOCK_INIT;
-		hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
+		repo_hold_locked_index(the_repository, &lock,
+				       LOCK_DIE_ON_ERROR);
 		if (reset_type == MIXED) {
 			int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
 			if (read_from_tree(&pathspec, &oid, intent_to_add))
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 8f61050bde8..d4c81352a65 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "cache.h"
 #include "config.h"
 #include "commit.h"
@@ -997,7 +997,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--shared-index-path")) {
-				if (read_cache() < 0)
+				if (repo_read_index(the_repository) < 0)
 					die(_("Could not read the index"));
 				if (the_index.split_index) {
 					const struct object_id *oid = &the_index.split_index->base_oid;
diff --git a/builtin/rm.c b/builtin/rm.c
index 2b23eb25438..40b7990de11 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -72,7 +72,7 @@ static void submodules_absorb_gitdir_if_needed(void)
 		int pos;
 		const struct cache_entry *ce;
 
-		pos = cache_name_pos(name, strlen(name));
+		pos = index_name_pos(&the_index, name, strlen(name));
 		if (pos < 0) {
 			pos = get_ours_cache_pos(name, pos);
 			if (pos < 0)
@@ -117,7 +117,7 @@ static int check_local_mod(struct object_id *head, int index_only)
 		int local_changes = 0;
 		int staged_changes = 0;
 
-		pos = cache_name_pos(name, strlen(name));
+		pos = index_name_pos(&the_index, name, strlen(name));
 		if (pos < 0) {
 			/*
 			 * Skip unmerged entries except for populated submodules
@@ -291,9 +291,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
diff --git a/builtin/stash.c b/builtin/stash.c
index c251419af7c..62e36718d38 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -261,11 +261,11 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
 	struct tree *tree;
 	struct lock_file lock_file = LOCK_INIT;
 
-	read_cache_preload(NULL);
-	if (refresh_cache(REFRESH_QUIET))
+	repo_read_index_preload(the_repository, NULL, 0);
+	if (refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL))
 		return -1;
 
-	hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
+	repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
 
 	memset(&opts, 0, sizeof(opts));
 
@@ -523,8 +523,9 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
 	struct tree *head, *merge, *merge_base;
 	struct lock_file lock = LOCK_INIT;
 
-	read_cache_preload(NULL);
-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
+	repo_read_index_preload(the_repository, NULL, 0);
+	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
+					 NULL, NULL, NULL))
 		return -1;
 
 	if (write_cache_as_tree(&c_tree, 0, NULL))
@@ -549,14 +550,14 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
 				return error(_("conflicts in index. "
 					       "Try without --index."));
 
-			discard_cache();
-			read_cache();
+			discard_index(&the_index);
+			repo_read_index(the_repository);
 			if (write_cache_as_tree(&index_tree, 0, NULL))
 				return error(_("could not save index tree"));
 
 			reset_head();
-			discard_cache();
-			read_cache();
+			discard_index(&the_index);
+			repo_read_index(the_repository);
 		}
 	}
 
@@ -1082,7 +1083,7 @@ static int check_changes_tracked_files(const struct pathspec *ps)
 	if (get_oid("HEAD", &dummy))
 		return -1;
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		return -1;
 
 	init_revisions(&rev, NULL);
@@ -1286,7 +1287,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
 	rev.diffopt.format_callback = add_diff_to_buf;
 	rev.diffopt.format_callback_data = &diff_output;
 
-	if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
+	if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
 		ret = -1;
 		goto done;
 	}
@@ -1344,8 +1345,9 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 
 	prepare_fallback_ident("git stash", "git@stash");
 
-	read_cache_preload(NULL);
-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
+	repo_read_index_preload(the_repository, NULL, 0);
+	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
+					 NULL, NULL, NULL) < 0) {
 		ret = -1;
 		goto done;
 	}
@@ -1513,7 +1515,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 		goto done;
 	}
 
-	read_cache_preload(NULL);
+	repo_read_index_preload(the_repository, NULL, 0);
 	if (!include_untracked && ps->nr) {
 		int i;
 		char *ps_matched = xcalloc(ps->nr, 1);
@@ -1533,7 +1535,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 		free(ps_matched);
 	}
 
-	if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
+	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
+					 NULL, NULL, NULL)) {
 		ret = -1;
 		goto done;
 	}
@@ -1590,7 +1593,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 				goto done;
 			}
 		}
-		discard_cache();
+		discard_index(&the_index);
 		if (ps->nr) {
 			struct child_process cp_add = CHILD_PROCESS_INIT;
 			struct child_process cp_diff = CHILD_PROCESS_INIT;
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 808865a5e31..ebe86e68176 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1,4 +1,4 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
+#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "repository.h"
 #include "cache.h"
@@ -196,7 +196,7 @@ static int module_list_compute(const char **argv,
 	if (pathspec->nr)
 		ps_matched = xcalloc(pathspec->nr, 1);
 
-	if (read_cache() < 0)
+	if (repo_read_index(the_repository) < 0)
 		die(_("index file corrupt"));
 
 	for (i = 0; i < the_index.cache_nr; i++) {
@@ -1109,13 +1109,13 @@ static int compute_summary_module_list(struct object_id *head_oid,
 	if (!info->cached) {
 		if (diff_cmd == DIFF_INDEX)
 			setup_work_tree();
-		if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
-			perror("read_cache_preload");
+		if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
+			perror("repo_read_index_preload");
 			ret = -1;
 			goto cleanup;
 		}
-	} else if (read_cache() < 0) {
-		perror("read_cache");
+	} else if (repo_read_index(the_repository) < 0) {
+		perror("repo_read_cache");
 		ret = -1;
 		goto cleanup;
 	}
@@ -3240,7 +3240,7 @@ static void die_on_index_match(const char *path, int force)
 	const char *args[] = { path, NULL };
 	parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
 
-	if (read_cache_preload(NULL) < 0)
+	if (repo_read_index_preload(the_repository, NULL, 0) < 0)
 		die(_("index file corrupt"));
 
 	if (ps.nr) {
diff --git a/builtin/update-index.c b/builtin/update-index.c
index fec3da05abb..82d5902cc8b 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -237,7 +237,7 @@ static int test_if_untracked_cache_is_supported(void)
 static int mark_ce_flags(const char *path, int flag, int mark)
 {
 	int namelen = strlen(path);
-	int pos = cache_name_pos(path, namelen);
+	int pos = index_name_pos(&the_index, path, namelen);
 	if (0 <= pos) {
 		mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
 		if (mark)
@@ -331,7 +331,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 static int process_directory(const char *path, int len, struct stat *st)
 {
 	struct object_id oid;
-	int pos = cache_name_pos(path, len);
+	int pos = index_name_pos(&the_index, path, len);
 
 	/* Exact match: file or existing gitlink */
 	if (pos >= 0) {
@@ -441,7 +441,7 @@ static void chmod_path(char flip, const char *path)
 	int pos;
 	struct cache_entry *ce;
 
-	pos = cache_name_pos(path, strlen(path));
+	pos = index_name_pos(&the_index, path, strlen(path));
 	if (pos < 0)
 		goto fail;
 	ce = the_index.cache[pos];
@@ -638,7 +638,7 @@ static int unresolve_one(const char *path)
 	struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
 
 	/* See if there is such entry in the index. */
-	pos = cache_name_pos(path, namelen);
+	pos = index_name_pos(&the_index, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_index_entry_at(&the_index, pos);
@@ -802,15 +802,16 @@ struct refresh_params {
 static int refresh(struct refresh_params *o, unsigned int flag)
 {
 	setup_work_tree();
-	read_cache();
-	*o->has_errors |= refresh_cache(o->flags | flag);
+	repo_read_index(the_repository);
+	*o->has_errors |= refresh_index(&the_index, o->flags | flag, NULL,
+					NULL, NULL);
 	if (has_racy_timestamp(&the_index)) {
 		/*
 		 * Even if nothing else has changed, updating the file
 		 * increases the chance that racy timestamps become
 		 * non-racy, helping future run-time performance.
 		 * We do that even in case of "errors" returned by
-		 * refresh_cache() as these are no actual errors.
+		 * refresh_index() as these are no actual errors.
 		 * cmd_status() does the same.
 		 */
 		the_index.cache_changed |= SOMETHING_CHANGED;
@@ -1109,11 +1110,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	the_repository->settings.command_requires_full_index = 0;
 
 	/* we will diagnose later if it turns out that we need to update it */
-	newfd = hold_locked_index(&lock_file, 0);
+	newfd = repo_hold_locked_index(the_repository, &lock_file, 0);
 	if (newfd < 0)
 		lock_error = errno;
 
-	entries = read_cache();
+	entries = repo_read_index(the_repository);
 	if (entries < 0)
 		die("cache corrupted");
 
diff --git a/cache.h b/cache.h
index 707806d2125..07d40b0964b 100644
--- a/cache.h
+++ b/cache.h
@@ -441,13 +441,8 @@ extern struct index_state the_index;
 #define active_nr (the_index.cache_nr)
 
 #define read_cache() repo_read_index(the_repository)
-#define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
-#define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
 #define discard_cache() discard_index(&the_index)
 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
-#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
-#define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
-#define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
 #endif
 #endif
 #endif
diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci
index 8b1ed65e7b0..4c1b890c3ef 100644
--- a/contrib/coccinelle/index-compatibility.cocci
+++ b/contrib/coccinelle/index-compatibility.cocci
@@ -26,11 +26,27 @@ identifier f != prepare_to_commit;
 (
 - read_cache_unmerged
 + repo_read_index_unmerged
+|
+- hold_locked_index
++ repo_hold_locked_index
 )
   (
 + the_repository,
   ...)
 
+// "the_repository" special-cases
+@@
+@@
+(
+- read_cache_preload
++ repo_read_index_preload
+)
+  (
++ the_repository,
+  ...
++ , 0
+  )
+
 // "the_index" simple cases
 @@
 @@
@@ -80,3 +96,40 @@ identifier f != prepare_to_commit;
   (
 + &the_index,
   ...)
+
+@@
+@@
+(
+- refresh_and_write_cache
++ repo_refresh_and_write_index
+)
+  (
++ the_repository,
+  ...
++ , NULL, NULL, NULL
+  )
+
+// "the_index" special-cases
+@@
+@@
+(
+- read_cache_from
++ read_index_from
+)
+  (
++ &the_index,
+  ...
++ , get_git_dir()
+  )
+
+@@
+@@
+(
+- refresh_cache
++ refresh_index
+)
+  (
++ &the_index,
+  ...
++ , NULL, NULL, NULL
+  )
diff --git a/contrib/coccinelle/index-compatibility.pending.cocci b/contrib/coccinelle/index-compatibility.pending.cocci
index 8207aee62c3..01f875d0060 100644
--- a/contrib/coccinelle/index-compatibility.pending.cocci
+++ b/contrib/coccinelle/index-compatibility.pending.cocci
@@ -4,9 +4,6 @@
 (
 - read_cache
 + repo_read_index
-|
-- hold_locked_index
-+ repo_hold_locked_index
 )
   (
 + the_repository,
@@ -25,54 +22,3 @@
   (
 + &the_index,
   ...)
-
-// "the_repository" special-cases
-@@
-@@
-(
-- read_cache_preload
-+ repo_read_index_preload
-)
-  (
-+ the_repository,
-  ...
-+ , 0
-  )
-
-@@
-@@
-(
-- refresh_and_write_cache
-+ repo_refresh_and_write_index
-)
-  (
-+ the_repository,
-  ...
-+ , NULL, NULL, NULL
-  )
-
-
-// "the_index" special-cases
-@@
-@@
-(
-- read_cache_from
-+ read_index_from
-)
-  (
-+ &the_index,
-  ...
-+ , get_git_dir()
-  )
-
-@@
-@@
-(
-- refresh_cache
-+ refresh_index
-)
-  (
-+ &the_index,
-  ...
-+ , NULL, NULL, NULL
-  )
-- 
2.38.0.1509.g9445af83948


^ permalink raw reply related	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2022-11-19 13:08 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-18 11:30 [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Ævar Arnfjörð Bjarmason
2022-11-18 11:30 ` [PATCH 01/12] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
2022-11-18 11:30 ` [PATCH 02/12] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
2022-11-18 11:30 ` [PATCH 03/12] cocci & cache.h: remove rarely used "the_index" compat macros Ævar Arnfjörð Bjarmason
2022-11-18 11:30 ` [PATCH 04/12] read-cache API & users: make discard_index() return void Ævar Arnfjörð Bjarmason
2022-11-18 11:30 ` [PATCH 05/12] cocci: add a index-compatibility.pending.cocci Ævar Arnfjörð Bjarmason
2022-11-18 11:31 ` [PATCH 06/12] cocci & cache.h: apply a selection of "pending" index-compatibility Ævar Arnfjörð Bjarmason
2022-11-18 11:31 ` [PATCH 07/12] cocci & cache.h: apply variable section " Ævar Arnfjörð Bjarmason
2022-11-18 11:31 ` [PATCH 08/12] cocci: apply "pending" index-compatibility to "t/helper/*.c" Ævar Arnfjörð Bjarmason
2022-11-18 11:31 ` [PATCH 09/12] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
2022-11-18 18:41   ` Taylor Blau
2022-11-18 11:31 ` [PATCH 10/12] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
2022-11-18 18:41   ` Taylor Blau
2022-11-18 11:31 ` [PATCH 11/12] cocci: apply "pending" index-compatibility to some "builtin/*.c" Ævar Arnfjörð Bjarmason
2022-11-18 11:31 ` [PATCH 12/12] builtin/*: remove or amend "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
2022-11-18 18:42   ` Taylor Blau
2022-11-18 19:27     ` Ævar Arnfjörð Bjarmason
2022-11-18 23:04       ` Taylor Blau
2022-11-18 12:13 ` [PATCH 00/12] tree-wide: chip away at USE_THE_INDEX_COMPATIBILITY_MACROS Phillip Wood
2022-11-18 12:21   ` Ævar Arnfjörð Bjarmason
2022-11-19 13:07 ` [PATCH v2 00/11] " Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 01/11] cache.h: remove unused "the_index" compat macros Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 02/11] builtin/{grep,log}.: don't define "USE_THE_INDEX_COMPATIBILITY_MACROS" Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 03/11] cocci & cache.h: remove rarely used "the_index" compat macros Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 04/11] read-cache API & users: make discard_index() return void Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 05/11] cocci: add a index-compatibility.pending.cocci Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 06/11] cocci & cache.h: apply a selection of "pending" index-compatibility Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 07/11] cocci & cache.h: apply variable section " Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 08/11] cocci: apply "pending" index-compatibility to "t/helper/*.c" Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 09/11] {builtin/*,repository}.c: add & use "USE_THE_INDEX_VARIABLE" Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 10/11] cache.h & test-tool.h: " Ævar Arnfjörð Bjarmason
2022-11-19 13:07   ` [PATCH v2 11/11] cocci: apply "pending" index-compatibility to some "builtin/*.c" Ævar Arnfjörð Bjarmason

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).