threads / patch / 54916

patch, 12 partsRemove more index compatibility macros

Subject: [PATCH 00/12] Remove more index compatibility macros

## tl;dr

65 messages between Jan 1, 2021 and Jan 26, 2021. Diffs are folded; open one to read it.

replies: 64people: 5as markdown or json

Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:06 UTC · lore

I noticed that Duy's project around USE_THE_INDEX_COMPATIBILITY_MACROS has been on pause for a while. Here is my attempt to continue that project a little.

I started going through the builtins that still use cache_name_pos() and the first few were easy: merge-inex, mv, rm.

Then I hit update-index and it was a bit bigger. It's included here as well.

My strategy for update-index was to create static globals "repo" and "istate" that point to the_repository and the_index, respectively. Then, I was able to remove macros one-by-one without changing method prototypes within the file.

I had started trying to keep everything local to the method signatures, but I hit a snag when reaching the command-line parsing callbacks, which I could not modify their call signature. At that point, I had something that was already much more complicated than what I present now. Outside of the first update-index commit, everything was a mechanical find/replace.

In total, this allows us to remove four of the compatibility macros because they are no longer used.

Thanks, -Stolee
Derrick Stolee (12):
  merge-index: drop index compatibility macros
  mv: remove index compatibility macros
  rm: remove compatilibity macros
  update-index: drop the_index, the_repository
  update-index: use istate->cache over active_cache
  update-index: use index->cache_nr over active_nr
  update-index: use istate->cache_changed
  update-index: use index_name_pos() over cache_name_pos()
  update-index: use remove_file_from_index()
  update-index: use add_index_entry()
  update-index: replace several compatibility macros
  update-index: remove ce_match_stat(), all macros
 Documentation/technical/racy-git.txt |   6 +-
 builtin/merge-index.c                |  33 +++---
 builtin/mv.c                         |  42 ++++----
 builtin/rm.c                         |  56 ++++++-----
 builtin/update-index.c               | 145 ++++++++++++++-------------
 cache.h                              |   4 -
 6 files changed, 149 insertions(+), 137 deletions(-)
base-commit: 71ca53e8125e36efbda17293c50027d31681a41f
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-830%2Fderrickstolee%2Findex-compatibility-1-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-830/derrickstolee/index-compatibility-1-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/830
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:06 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 01/12] merge-index: drop index compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

Replace uses of the old macros for the_index and instead pass around a 'struct index_state' pointer. This allows dropping the compatibility flag.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/merge-index.c | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)
Show changes to builtin/merge-index.c +18 −15
diff --git a/builtin/merge-index.c b/builtin/merge-index.c
index 38ea6ad6ca2..8c7e6b0e6a2 100644
--- a/builtin/merge-index.c
+++ b/builtin/merge-index.c
@@ -1,4 +1,3 @@
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "run-command.h"
 
@@ -6,18 +5,19 @@ static const char *pgm;
 static int one_shot, quiet;
 static int err;
 
-static int merge_entry(int pos, const char *path)
+static int merge_entry(struct index_state *istate,
+		       int pos, const char *path)
 {
 	int found;
 	const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
 	char hexbuf[4][GIT_MAX_HEXSZ + 1];
 	char ownbuf[4][60];
 
-	if (pos >= active_nr)
+	if (pos >= istate->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 = istate->cache[pos];
 		int stage = ce_stage(ce);
 
 		if (strcmp(ce->name, path))
@@ -27,7 +27,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 < istate->cache_nr);
 	if (!found)
 		die("git merge-index: %s not in the cache", path);
 
@@ -43,32 +43,34 @@ static int merge_entry(int pos, const char *path)
 	return found;
 }
 
-static void merge_one_path(const char *path)
+static void merge_one_path(struct index_state *istate,
+			   const char *path)
 {
-	int pos = cache_name_pos(path, strlen(path));
+	int pos = index_name_pos(istate, path, strlen(path));
 
 	/*
 	 * If it already exists in the cache as stage0, it's
 	 * already merged and there is nothing to do.
 	 */
 	if (pos < 0)
-		merge_entry(-pos-1, path);
+		merge_entry(istate, -pos - 1, path);
 }
 
-static void merge_all(void)
+static void merge_all(struct index_state *istate)
 {
 	int i;
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
 		if (!ce_stage(ce))
 			continue;
-		i += merge_entry(i, ce->name)-1;
+		i += merge_entry(istate, i, ce->name)-1;
 	}
 }
 
 int cmd_merge_index(int argc, const char **argv, const char *prefix)
 {
 	int i, force_file = 0;
+	struct index_state *istate;
 
 	/* Without this we cannot rely on waitpid() to tell
 	 * what happened to our children.
@@ -78,7 +80,8 @@ 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);
+	istate = the_repository->index;
 
 	i = 1;
 	if (!strcmp(argv[i], "-o")) {
@@ -98,12 +101,12 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "-a")) {
-				merge_all();
+				merge_all(istate);
 				continue;
 			}
 			die("git merge-index: unknown option %s", arg);
 		}
-		merge_one_path(arg);
+		merge_one_path(istate, arg);
 	}
 	if (err && !quiet)
 		die("merge program failed");
-- 
gitgitgadget
Alban Gruin· Jan 3, 2021, 23:31 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH 01/12] merge-index: drop index compatibility macros

Hi Derrick,
Le 01/01/2021 à 14:06, Derrick Stolee via GitGitGadget a écrit :
Show 7 quoted lines
> From: Derrick Stolee <dstolee@microsoft.com>
> 
> Replace uses of the old macros for the_index and instead pass around a
> 'struct index_state' pointer. This allows dropping the compatibility
> flag.
> 
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>

I already libified builtin/merge-index.c in ag/merge-strategies-in-c, and such dropped the_index. I modified merge_entry(), merge_one_path() and merge_all() to take a callback, itself taking a repository. As such, in my series, these functions take a `struct repository *' instead of an index state.

I'm not sure how we should proceed with our respective patches.

Cheers, Alban

Derrick Stolee· Jan 4, 2021, 11:08 UTC · re: Alban Gruin · lore

Re: [PATCH 01/12] merge-index: drop index compatibility macros

On 1/3/2021 6:31 PM, Alban Gruin wrote:
Show 18 quoted lines
> Hi Derrick,
> 
> Le 01/01/2021 à 14:06, Derrick Stolee via GitGitGadget a écrit :
>> From: Derrick Stolee <dstolee@microsoft.com>
>>
>> Replace uses of the old macros for the_index and instead pass around a
>> 'struct index_state' pointer. This allows dropping the compatibility
>> flag.
>>
>> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> 
> I already libified builtin/merge-index.c in ag/merge-strategies-in-c,
> and such dropped the_index.  I modified merge_entry(), merge_one_path()
> and merge_all() to take a callback, itself taking a repository.  As
> such, in my series, these functions take a `struct repository *' instead
> of an index state.
> 
> I'm not sure how we should proceed with our respective patches.
Hi Alban,

Sorry I didn't realize that. I'll drop this patch. Thanks for letting me know!

Thanks, -Stolee

Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:06 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 02/12] mv: remove index compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

The mv builtin uses the compatibility macros to interact with the index. Update these to use modern methods referring to a 'struct index_state' pointer. Several helper methods need to be updated to consider such a pointer, but the modifications are rudimentary.

Two macros can be deleted from cache.h because these are the last uses.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/mv.c | 42 +++++++++++++++++++++++-------------------
 cache.h      |  2 --
 2 files changed, 23 insertions(+), 21 deletions(-)
Show changes to 2 files +23 −21

builtin/mv.c, cache.h

diff --git a/builtin/mv.c b/builtin/mv.c
index 7dac714af90..0055d49a8a7 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) 2006 Johannes Schindelin
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "config.h"
 #include "pathspec.h"
@@ -75,13 +74,14 @@ static const char *add_slash(const char *path)
 
 #define SUBMODULE_WITH_GITDIR ((const char *)1)
 
-static void prepare_move_submodule(const char *src, int first,
+static void prepare_move_submodule(struct index_state *istate,
+				   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(istate->cache[first]->ce_mode))
 		die(_("Directory %s is in index and no submodule?"), src);
-	if (!is_staging_gitmodules_ok(&the_index))
+	if (!is_staging_gitmodules_ok(istate))
 		die(_("Please stage your changes to .gitmodules or stash them to proceed"));
 	strbuf_addf(&submodule_dotgit, "%s/.git", src);
 	*submodule_gitfile = read_gitfile(submodule_dotgit.buf);
@@ -92,19 +92,20 @@ static void prepare_move_submodule(const char *src, int first,
 	strbuf_release(&submodule_dotgit);
 }
 
-static int index_range_of_same_dir(const char *src, int length,
+static int index_range_of_same_dir(struct index_state *istate,
+				   const char *src, int length,
 				   int *first_p, int *last_p)
 {
 	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(istate, src_w_slash, len_w_slash);
 	if (first >= 0)
 		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 < istate->cache_nr; last++) {
+		const char *path = istate->cache[last]->name;
 		if (strncmp(path, src_w_slash, len_w_slash))
 			break;
 	}
@@ -133,6 +134,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 	struct lock_file lock_file = LOCK_INIT;
 	struct cache_entry *ce;
+	struct index_state *istate;
 
 	git_config(git_default_config, NULL);
 
@@ -141,9 +143,10 @@ 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"));
+	istate = the_repository->index;
 
 	source = internal_prefix_pathspec(prefix, argv, argc, 0);
 	modes = xcalloc(argc, sizeof(enum update_mode));
@@ -190,12 +193,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				&& lstat(dst, &st) == 0)
 			bad = _("cannot move directory over file");
 		else if (src_is_dir) {
-			int first = cache_name_pos(src, length), last;
+			int first = index_name_pos(istate, src, length);
+			int last;
 
 			if (first >= 0)
-				prepare_move_submodule(src, first,
+				prepare_move_submodule(istate, src, first,
 						       submodule_gitfile + i);
-			else if (index_range_of_same_dir(src, length,
+			else if (index_range_of_same_dir(istate, src, length,
 							 &first, &last) < 1)
 				bad = _("source directory is empty");
 			else { /* last - first >= 1 */
@@ -212,7 +216,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				dst_len = strlen(dst);
 
 				for (j = 0; j < last - first; j++) {
-					const char *path = active_cache[first + j]->name;
+					const char *path = istate->cache[first + j]->name;
 					source[argc + j] = path;
 					destination[argc + j] =
 						prefix_path(dst, dst_len, path + length + 1);
@@ -221,7 +225,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				}
 				argc += last - first;
 			}
-		} else if (!(ce = cache_file_exists(src, length, ignore_case))) {
+		} else if (!(ce = index_file_exists(istate, src, length, ignore_case))) {
 			bad = _("not under version control");
 		} else if (ce_stage(ce)) {
 			bad = _("conflicted");
@@ -291,15 +295,15 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		if (mode == WORKING_DIRECTORY)
 			continue;
 
-		pos = cache_name_pos(src, strlen(src));
+		pos = index_name_pos(istate, src, strlen(src));
 		assert(pos >= 0);
-		rename_cache_entry_at(pos, dst);
+		rename_index_entry_at(istate, pos, dst);
 	}
 
 	if (gitmodules_modified)
-		stage_updated_gitmodules(&the_index);
+		stage_updated_gitmodules(istate);
 
-	if (write_locked_index(&the_index, &lock_file,
+	if (write_locked_index(istate, &lock_file,
 			       COMMIT_LOCK | SKIP_IF_UNCHANGED))
 		die(_("Unable to write new index file"));
 
diff --git a/cache.h b/cache.h
index 71097657489..7bfb8195d97 100644
--- a/cache.h
+++ b/cache.h
@@ -409,7 +409,6 @@ extern struct index_state 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_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))
@@ -420,7 +419,6 @@ extern struct index_state the_index;
 #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)
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:06 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 03/12] rm: remove compatilibity macros

From: Derrick Stolee <dstolee@microsoft.com>

The rm builtin still uses the antiquated compatibility macros for interacting with the index. Update these to the more modern uses by passing around a 'struct index_state' pointer.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/rm.c | 56 ++++++++++++++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
Show changes to builtin/rm.c +30 −26
diff --git a/builtin/rm.c b/builtin/rm.c
index 4858631e0f0..767df8d6b25 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds 2006
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
@@ -28,12 +27,14 @@ static struct {
 	} *entry;
 } list;
 
-static int get_ours_cache_pos(const char *path, int pos)
+static int get_ours_cache_pos(struct index_state *istate,
+			      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 < istate->cache_nr) &&
+	       !strcmp(istate->cache[i]->name, path)) {
+		if (ce_stage(istate->cache[i]) == 2)
 			return i;
 		i++;
 	}
@@ -61,7 +62,7 @@ static void print_error_files(struct string_list *files_list,
 	}
 }
 
-static void submodules_absorb_gitdir_if_needed(void)
+static void submodules_absorb_gitdir_if_needed(struct index_state *istate)
 {
 	int i;
 	for (i = 0; i < list.nr; i++) {
@@ -69,13 +70,13 @@ 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(istate, name, strlen(name));
 		if (pos < 0) {
-			pos = get_ours_cache_pos(name, pos);
+			pos = get_ours_cache_pos(istate, name, pos);
 			if (pos < 0)
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = istate->cache[pos];
 
 		if (!S_ISGITLINK(ce->ce_mode) ||
 		    !file_exists(ce->name) ||
@@ -88,7 +89,8 @@ static void submodules_absorb_gitdir_if_needed(void)
 	}
 }
 
-static int check_local_mod(struct object_id *head, int index_only)
+static int check_local_mod(struct index_state *istate,
+			   struct object_id *head, int index_only)
 {
 	/*
 	 * Items in list are already sorted in the cache order,
@@ -114,21 +116,21 @@ 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(istate, name, strlen(name));
 		if (pos < 0) {
 			/*
 			 * Skip unmerged entries except for populated submodules
 			 * that could lose history when removed.
 			 */
-			pos = get_ours_cache_pos(name, pos);
+			pos = get_ours_cache_pos(istate, name, pos);
 			if (pos < 0)
 				continue;
 
-			if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
+			if (!S_ISGITLINK(istate->cache[pos]->ce_mode) ||
 			    is_empty_dir(name))
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = istate->cache[pos];
 
 		if (lstat(ce->name, &st) < 0) {
 			if (!is_missing_file_error(errno))
@@ -165,7 +167,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(istate, ce, &st, 0) ||
 		    (S_ISGITLINK(ce->ce_mode) &&
 		     bad_to_remove_submodule(ce->name,
 				SUBMODULE_REMOVAL_DIE_ON_ERROR |
@@ -257,6 +259,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	int i;
 	struct pathspec pathspec;
 	char *seen;
+	struct index_state *istate;
 
 	git_config(git_default_config, NULL);
 
@@ -284,24 +287,25 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	if (!index_only)
 		setup_work_tree();
 
-	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);
+	istate = the_repository->index;
+	refresh_index(istate, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
 
 	seen = xcalloc(pathspec.nr, 1);
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
-		if (!ce_path_match(&the_index, ce, &pathspec, seen))
+	for (i = 0; i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
+		if (!ce_path_match(istate, ce, &pathspec, seen))
 			continue;
 		ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
 		list.entry[list.nr].name = xstrdup(ce->name);
 		list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
 		if (list.entry[list.nr++].is_submodule &&
-		    !is_staging_gitmodules_ok(&the_index))
+		    !is_staging_gitmodules_ok(istate))
 			die(_("please stage your changes to .gitmodules or stash them to proceed"));
 	}
 
@@ -329,7 +333,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	}
 
 	if (!index_only)
-		submodules_absorb_gitdir_if_needed();
+		submodules_absorb_gitdir_if_needed(istate);
 
 	/*
 	 * If not forced, the file, the index and the HEAD (if exists)
@@ -345,7 +349,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		struct object_id oid;
 		if (get_oid("HEAD", &oid))
 			oidclr(&oid);
-		if (check_local_mod(&oid, index_only))
+		if (check_local_mod(istate, &oid, index_only))
 			exit(1);
 	}
 
@@ -358,7 +362,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(istate, path))
 			die(_("git rm: unable to remove %s"), path);
 	}
 
@@ -398,10 +402,10 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		}
 		strbuf_release(&buf);
 		if (gitmodules_modified)
-			stage_updated_gitmodules(&the_index);
+			stage_updated_gitmodules(istate);
 	}
 
-	if (write_locked_index(&the_index, &lock_file,
+	if (write_locked_index(istate, &lock_file,
 			       COMMIT_LOCK | SKIP_IF_UNCHANGED))
 		die(_("Unable to write new index file"));
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 04/12] update-index: drop the_index, the_repository

From: Derrick Stolee <dstolee@microsoft.com>

To reduce the need for the index compatibility macros, we will replace their uses in update-index mechanically. This is the most interesting change, which creates global "repo" and "istate" pointers. The macros can then be mechanically replaced by instances that use the istate pointer instead of the version that autocompletes to use the_index.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 59 +++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 27 deletions(-)
Show changes to builtin/update-index.c +32 −27
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 79087bccea4..c9a6cde97da 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -40,6 +40,9 @@ static int ignore_skip_worktree_entries;
 #define UNMARK_FLAG 2
 static struct strbuf mtime_dir = STRBUF_INIT;
 
+static struct repository *repo;
+static struct index_state *istate;
+
 /* Untracked cache mode */
 enum uc_mode {
 	UC_UNSPECIFIED = -1,
@@ -232,13 +235,13 @@ 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(istate, active_cache[pos]);
 		if (mark)
 			active_cache[pos]->ce_flags |= flag;
 		else
 			active_cache[pos]->ce_flags &= ~flag;
 		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
-		cache_tree_invalidate_path(&the_index, path);
+		cache_tree_invalidate_path(istate, path);
 		active_cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
@@ -277,14 +280,14 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
 		return 0;
 
-	ce = make_empty_cache_entry(&the_index, len);
+	ce = make_empty_cache_entry(istate, len);
 	memcpy(ce->name, path, len);
 	ce->ce_flags = create_ce_flags(0);
 	ce->ce_namelen = len;
-	fill_stat_cache_info(&the_index, ce, st);
+	fill_stat_cache_info(istate, ce, st);
 	ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
 
-	if (index_path(&the_index, &ce->oid, path, st,
+	if (index_path(istate, &ce->oid, path, st,
 		       info_only ? 0 : HASH_WRITE_OBJECT)) {
 		discard_cache_entry(ce);
 		return -1;
@@ -411,7 +414,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 		return error("Invalid path '%s'", path);
 
 	len = strlen(path);
-	ce = make_empty_cache_entry(&the_index, len);
+	ce = make_empty_cache_entry(istate, len);
 
 	oidcpy(&ce->oid, oid);
 	memcpy(ce->name, path, len);
@@ -603,7 +606,7 @@ static struct cache_entry *read_one_ent(const char *which,
 	struct object_id oid;
 	struct cache_entry *ce;
 
-	if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
+	if (get_tree_entry(repo, ent, path, &oid, &mode)) {
 		if (which)
 			error("%s: not in %s branch.", path, which);
 		return NULL;
@@ -613,7 +616,7 @@ static struct cache_entry *read_one_ent(const char *which,
 			error("%s: not a blob in %s branch.", path, which);
 		return NULL;
 	}
-	ce = make_empty_cache_entry(&the_index, namelen);
+	ce = make_empty_cache_entry(istate, namelen);
 
 	oidcpy(&ce->oid, &oid);
 	memcpy(ce->name, path, namelen);
@@ -751,7 +754,7 @@ static int do_reupdate(int ac, const char **av,
 		int save_nr;
 		char *path;
 
-		if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
+		if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
 			continue;
 		if (has_head)
 			old = read_one_ent(NULL, &head_oid,
@@ -968,7 +971,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	struct parse_opt_ctx_t ctx;
 	strbuf_getline_fn getline_fn;
 	int parseopt_state = PARSE_OPT_UNKNOWN;
-	struct repository *r = the_repository;
 	struct option options[] = {
 		OPT_BIT('q', NULL, &refresh_args.flags,
 			N_("continue refresh even when index needs update"),
@@ -1077,16 +1079,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	git_config(git_default_config, NULL);
 
+	repo = the_repository;
+
 	/* 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(repo, &lock_file, 0);
 	if (newfd < 0)
 		lock_error = errno;
 
-	entries = read_cache();
+	entries = repo_read_index(repo);
 	if (entries < 0)
 		die("cache corrupted");
 
-	the_index.updated_skipworktree = 1;
+	istate = repo->index;
+	istate->updated_skipworktree = 1;
 
 	/*
 	 * Custom copy of parse_options() because we want to handle
@@ -1140,9 +1145,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    preferred_index_format,
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
-		if (the_index.version != preferred_index_format)
+		if (istate->version != preferred_index_format)
 			active_cache_changed |= SOMETHING_CHANGED;
-		the_index.version = preferred_index_format;
+		istate->version = preferred_index_format;
 	}
 
 	if (read_from_stdin) {
@@ -1173,28 +1178,28 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			warning(_("core.splitIndex is set to false; "
 				  "remove or change it, if you really want to "
 				  "enable split index"));
-		if (the_index.split_index)
-			the_index.cache_changed |= SPLIT_INDEX_ORDERED;
+		if (istate->split_index)
+			istate->cache_changed |= SPLIT_INDEX_ORDERED;
 		else
-			add_split_index(&the_index);
+			add_split_index(istate);
 	} else if (!split_index) {
 		if (git_config_get_split_index() == 1)
 			warning(_("core.splitIndex is set to true; "
 				  "remove or change it, if you really want to "
 				  "disable split index"));
-		remove_split_index(&the_index);
+		remove_split_index(istate);
 	}
 
-	prepare_repo_settings(r);
+	prepare_repo_settings(repo);
 	switch (untracked_cache) {
 	case UC_UNSPECIFIED:
 		break;
 	case UC_DISABLE:
-		if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
+		if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
 			warning(_("core.untrackedCache is set to true; "
 				  "remove or change it, if you really want to "
 				  "disable the untracked cache"));
-		remove_untracked_cache(&the_index);
+		remove_untracked_cache(istate);
 		report(_("Untracked cache disabled"));
 		break;
 	case UC_TEST:
@@ -1202,11 +1207,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		return !test_if_untracked_cache_is_supported();
 	case UC_ENABLE:
 	case UC_FORCE:
-		if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
+		if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
 			warning(_("core.untrackedCache is set to false; "
 				  "remove or change it, if you really want to "
 				  "enable the untracked cache"));
-		add_untracked_cache(&the_index);
+		add_untracked_cache(istate);
 		report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
 		break;
 	default:
@@ -1218,14 +1223,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			warning(_("core.fsmonitor is unset; "
 				"set it if you really want to "
 				"enable fsmonitor"));
-		add_fsmonitor(&the_index);
+		add_fsmonitor(istate);
 		report(_("fsmonitor enabled"));
 	} else if (!fsmonitor) {
 		if (git_config_get_fsmonitor() == 1)
 			warning(_("core.fsmonitor is set; "
 				"remove it if you really want to "
 				"disable fsmonitor"));
-		remove_fsmonitor(&the_index);
+		remove_fsmonitor(istate);
 		report(_("fsmonitor disabled"));
 	}
 
@@ -1235,7 +1240,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 				exit(128);
 			unable_to_lock_die(get_index_file(), lock_error);
 		}
-		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		if (write_locked_index(istate, &lock_file, COMMIT_LOCK))
 			die("Unable to write new index file");
 	}
 
-- 
gitgitgadget
Elijah Newren· Jan 1, 2021, 21:05 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH 04/12] update-index: drop the_index, the_repository

On Fri, Jan 1, 2021 at 5:10 AM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 8 quoted lines
>
> From: Derrick Stolee <dstolee@microsoft.com>
>
> To reduce the need for the index compatibility macros, we will replace
> their uses in update-index mechanically. This is the most interesting
> change, which creates global "repo" and "istate" pointers. The macros
> can then be mechanically replaced by instances that use the istate
> pointer instead of the version that autocompletes to use the_index.

autocompletes seems a bit weird to me here. Perhaps s/autocompletes to use/implicitly uses/ ?

Also, it seems like in the last few patches you just used the_repository whereas here you're trying to avoid it. Is that because there are more uses here and only one in the other patches?

Otherwise, all the changes in this patch (and the other ones I've read so far; going through them in order) seem like the obvious mechanical changes necessary to update to avoid the index compatibility macros. So, looking good so far.

Show 212 quoted lines
>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  builtin/update-index.c | 59 +++++++++++++++++++++++-------------------
>  1 file changed, 32 insertions(+), 27 deletions(-)
>
> diff --git a/builtin/update-index.c b/builtin/update-index.c
> index 79087bccea4..c9a6cde97da 100644
> --- a/builtin/update-index.c
> +++ b/builtin/update-index.c
> @@ -40,6 +40,9 @@ static int ignore_skip_worktree_entries;
>  #define UNMARK_FLAG 2
>  static struct strbuf mtime_dir = STRBUF_INIT;
>
> +static struct repository *repo;
> +static struct index_state *istate;
> +
>  /* Untracked cache mode */
>  enum uc_mode {
>         UC_UNSPECIFIED = -1,
> @@ -232,13 +235,13 @@ 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(istate, active_cache[pos]);
>                 if (mark)
>                         active_cache[pos]->ce_flags |= flag;
>                 else
>                         active_cache[pos]->ce_flags &= ~flag;
>                 active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
> -               cache_tree_invalidate_path(&the_index, path);
> +               cache_tree_invalidate_path(istate, path);
>                 active_cache_changed |= CE_ENTRY_CHANGED;
>                 return 0;
>         }
> @@ -277,14 +280,14 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
>         if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
>                 return 0;
>
> -       ce = make_empty_cache_entry(&the_index, len);
> +       ce = make_empty_cache_entry(istate, len);
>         memcpy(ce->name, path, len);
>         ce->ce_flags = create_ce_flags(0);
>         ce->ce_namelen = len;
> -       fill_stat_cache_info(&the_index, ce, st);
> +       fill_stat_cache_info(istate, ce, st);
>         ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
>
> -       if (index_path(&the_index, &ce->oid, path, st,
> +       if (index_path(istate, &ce->oid, path, st,
>                        info_only ? 0 : HASH_WRITE_OBJECT)) {
>                 discard_cache_entry(ce);
>                 return -1;
> @@ -411,7 +414,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
>                 return error("Invalid path '%s'", path);
>
>         len = strlen(path);
> -       ce = make_empty_cache_entry(&the_index, len);
> +       ce = make_empty_cache_entry(istate, len);
>
>         oidcpy(&ce->oid, oid);
>         memcpy(ce->name, path, len);
> @@ -603,7 +606,7 @@ static struct cache_entry *read_one_ent(const char *which,
>         struct object_id oid;
>         struct cache_entry *ce;
>
> -       if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
> +       if (get_tree_entry(repo, ent, path, &oid, &mode)) {
>                 if (which)
>                         error("%s: not in %s branch.", path, which);
>                 return NULL;
> @@ -613,7 +616,7 @@ static struct cache_entry *read_one_ent(const char *which,
>                         error("%s: not a blob in %s branch.", path, which);
>                 return NULL;
>         }
> -       ce = make_empty_cache_entry(&the_index, namelen);
> +       ce = make_empty_cache_entry(istate, namelen);
>
>         oidcpy(&ce->oid, &oid);
>         memcpy(ce->name, path, namelen);
> @@ -751,7 +754,7 @@ static int do_reupdate(int ac, const char **av,
>                 int save_nr;
>                 char *path;
>
> -               if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
> +               if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
>                         continue;
>                 if (has_head)
>                         old = read_one_ent(NULL, &head_oid,
> @@ -968,7 +971,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>         struct parse_opt_ctx_t ctx;
>         strbuf_getline_fn getline_fn;
>         int parseopt_state = PARSE_OPT_UNKNOWN;
> -       struct repository *r = the_repository;
>         struct option options[] = {
>                 OPT_BIT('q', NULL, &refresh_args.flags,
>                         N_("continue refresh even when index needs update"),
> @@ -1077,16 +1079,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>
>         git_config(git_default_config, NULL);
>
> +       repo = the_repository;
> +
>         /* 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(repo, &lock_file, 0);
>         if (newfd < 0)
>                 lock_error = errno;
>
> -       entries = read_cache();
> +       entries = repo_read_index(repo);
>         if (entries < 0)
>                 die("cache corrupted");
>
> -       the_index.updated_skipworktree = 1;
> +       istate = repo->index;
> +       istate->updated_skipworktree = 1;
>
>         /*
>          * Custom copy of parse_options() because we want to handle
> @@ -1140,9 +1145,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>                             preferred_index_format,
>                             INDEX_FORMAT_LB, INDEX_FORMAT_UB);
>
> -               if (the_index.version != preferred_index_format)
> +               if (istate->version != preferred_index_format)
>                         active_cache_changed |= SOMETHING_CHANGED;
> -               the_index.version = preferred_index_format;
> +               istate->version = preferred_index_format;
>         }
>
>         if (read_from_stdin) {
> @@ -1173,28 +1178,28 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>                         warning(_("core.splitIndex is set to false; "
>                                   "remove or change it, if you really want to "
>                                   "enable split index"));
> -               if (the_index.split_index)
> -                       the_index.cache_changed |= SPLIT_INDEX_ORDERED;
> +               if (istate->split_index)
> +                       istate->cache_changed |= SPLIT_INDEX_ORDERED;
>                 else
> -                       add_split_index(&the_index);
> +                       add_split_index(istate);
>         } else if (!split_index) {
>                 if (git_config_get_split_index() == 1)
>                         warning(_("core.splitIndex is set to true; "
>                                   "remove or change it, if you really want to "
>                                   "disable split index"));
> -               remove_split_index(&the_index);
> +               remove_split_index(istate);
>         }
>
> -       prepare_repo_settings(r);
> +       prepare_repo_settings(repo);
>         switch (untracked_cache) {
>         case UC_UNSPECIFIED:
>                 break;
>         case UC_DISABLE:
> -               if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
> +               if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
>                         warning(_("core.untrackedCache is set to true; "
>                                   "remove or change it, if you really want to "
>                                   "disable the untracked cache"));
> -               remove_untracked_cache(&the_index);
> +               remove_untracked_cache(istate);
>                 report(_("Untracked cache disabled"));
>                 break;
>         case UC_TEST:
> @@ -1202,11 +1207,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>                 return !test_if_untracked_cache_is_supported();
>         case UC_ENABLE:
>         case UC_FORCE:
> -               if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
> +               if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
>                         warning(_("core.untrackedCache is set to false; "
>                                   "remove or change it, if you really want to "
>                                   "enable the untracked cache"));
> -               add_untracked_cache(&the_index);
> +               add_untracked_cache(istate);
>                 report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
>                 break;
>         default:
> @@ -1218,14 +1223,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>                         warning(_("core.fsmonitor is unset; "
>                                 "set it if you really want to "
>                                 "enable fsmonitor"));
> -               add_fsmonitor(&the_index);
> +               add_fsmonitor(istate);
>                 report(_("fsmonitor enabled"));
>         } else if (!fsmonitor) {
>                 if (git_config_get_fsmonitor() == 1)
>                         warning(_("core.fsmonitor is set; "
>                                 "remove it if you really want to "
>                                 "disable fsmonitor"));
> -               remove_fsmonitor(&the_index);
> +               remove_fsmonitor(istate);
>                 report(_("fsmonitor disabled"));
>         }
>
> @@ -1235,7 +1240,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>                                 exit(128);
>                         unable_to_lock_die(get_index_file(), lock_error);
>                 }
> -               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
> +               if (write_locked_index(istate, &lock_file, COMMIT_LOCK))
>                         die("Unable to write new index file");
>         }
>
> --
> gitgitgadget
>
Derrick Stolee· Jan 4, 2021, 00:56 UTC · re: Elijah Newren · lore

Re: [PATCH 04/12] update-index: drop the_index, the_repository

On 1/1/2021 4:05 PM, Elijah Newren wrote:
Show 13 quoted lines
> On Fri, Jan 1, 2021 at 5:10 AM Derrick Stolee via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>>
>> From: Derrick Stolee <dstolee@microsoft.com>
>>
>> To reduce the need for the index compatibility macros, we will replace
>> their uses in update-index mechanically. This is the most interesting
>> change, which creates global "repo" and "istate" pointers. The macros
>> can then be mechanically replaced by instances that use the istate
>> pointer instead of the version that autocompletes to use the_index.
> 
> autocompletes seems a bit weird to me here.  Perhaps s/autocompletes
> to use/implicitly uses/ ?

My intention was "instead of the macro expansion that uses the_index". The preprocessor is really just an early version of autocomplete, right? Thanks.

> Also, it seems like in the last few patches you just used
> the_repository whereas here you're trying to avoid it.  Is that
> because there are more uses here and only one in the other patches?

My goal isn't to remove the_repository, but the earlier patches also avoided static globals in favor of method parameters. I needed to change the strategy for update-index because of the vast number of methods needing an update. Since I was making a static global for the current index, it was not a huge step to also add one for the current repository.

Further, the cmd_update_index() already had a local pointer that replaced using the_repository, giving me some reason to include the_repository in these updates.

> Otherwise, all the changes in this patch (and the other ones I've read
> so far; going through them in order) seem like the obvious mechanical
> changes necessary to update to avoid the index compatibility macros.
> So, looking good so far.

Thanks, -Stolee

Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 07/12] update-index: use istate->cache_changed

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of active_cache_changed.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index f1657a66496..a64f2f5a8f4 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -242,7 +242,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 			istate->cache[pos]->ce_flags &= ~flag;
 		istate->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(istate, path);
-		active_cache_changed |= CE_ENTRY_CHANGED;
+		istate->cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
 	return -1;
@@ -926,7 +926,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;
+		istate->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -947,7 +947,7 @@ static enum parse_opt_result reupdate_callback(
 	setup_work_tree();
 	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
 	if (*has_errors)
-		active_cache_changed = 0;
+		istate->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -1146,7 +1146,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
 		if (istate->version != preferred_index_format)
-			active_cache_changed |= SOMETHING_CHANGED;
+			istate->cache_changed |= SOMETHING_CHANGED;
 		istate->version = preferred_index_format;
 	}
 
@@ -1234,7 +1234,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		report(_("fsmonitor disabled"));
 	}
 
-	if (active_cache_changed || force_write) {
+	if (istate->cache_changed || force_write) {
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 06/12] update-index: use index->cache_nr over active_nr

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
Show changes to builtin/update-index.c +7 −7
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 0616c786410..f1657a66496 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -346,7 +346,7 @@ 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) {
+	while (pos < istate->cache_nr) {
 		const struct cache_entry *ce = istate->cache[pos++];
 
 		if (strncmp(ce->name, path, len))
@@ -638,7 +638,7 @@ static int unresolve_one(const char *path)
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
-		if (pos < active_nr) {
+		if (pos < istate->cache_nr) {
 			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
@@ -652,7 +652,7 @@ static int unresolve_one(const char *path)
 		 * want to do anything in the former case.
 		 */
 		pos = -pos-1;
-		if (pos < active_nr) {
+		if (pos < istate->cache_nr) {
 			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
@@ -748,7 +748,7 @@ static int do_reupdate(int ac, const char **av,
 		 */
 		has_head = 0;
  redo:
-	for (pos = 0; pos < active_nr; pos++) {
+	for (pos = 0; pos < istate->cache_nr; pos++) {
 		const struct cache_entry *ce = istate->cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
@@ -766,14 +766,14 @@ static int do_reupdate(int ac, const char **av,
 		}
 		/* Be careful.  The working tree may not have the
 		 * path anymore, in which case, under 'allow_remove',
-		 * or worse yet 'allow_replace', active_nr may decrease.
+		 * or worse yet 'allow_replace', istate->cache_nr may decrease.
 		 */
-		save_nr = active_nr;
+		save_nr = istate->cache_nr;
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
 		discard_cache_entry(old);
-		if (save_nr != active_nr)
+		if (save_nr != istate->cache_nr)
 			goto redo;
 	}
 	clear_pathspec(&pathspec);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 10/12] update-index: use add_index_entry()

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of add_cache_entry().
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to builtin/update-index.c +4 −4
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 6f51fb4a14e..577ae2391b7 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -294,7 +294,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(istate, ce, option)) {
 		discard_cache_entry(ce);
 		return error("%s: cannot add to the index - missing --add option?", path);
 	}
@@ -425,7 +425,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(istate, ce, option))
 		return error("%s: cannot add to the index - missing --add option?",
 			     path);
 	report("add '%s'", path);
@@ -683,12 +683,12 @@ static int unresolve_one(const char *path)
 	}
 
 	remove_file_from_index(istate, path);
-	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
+	if (add_index_entry(istate, 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(istate, ce_3, ADD_CACHE_OK_TO_ADD))
 		return 0;
 	error("%s: cannot add their version to the index.", path);
 	ret = -1;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 11/12] update-index: replace several compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

This is also the last usage of unmerge_cache_entry_at(), so it can be removed from cache.h.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 11 ++++++-----
 cache.h                |  1 -
 2 files changed, 6 insertions(+), 6 deletions(-)
Show changes to 2 files +6 −6

builtin/update-index.c, cache.h

diff --git a/builtin/update-index.c b/builtin/update-index.c
index 577ae2391b7..4da0c169dc7 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -441,7 +441,7 @@ static void chmod_path(char flip, const char *path)
 	if (pos < 0)
 		goto fail;
 	ce = istate->cache[pos];
-	if (chmod_cache_entry(ce, flip) < 0)
+	if (chmod_index_entry(istate, ce, flip) < 0)
 		goto fail;
 
 	report("chmod %cx '%s'", flip, path);
@@ -637,7 +637,7 @@ static int unresolve_one(const char *path)
 	pos = index_name_pos(istate, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
-		pos = unmerge_cache_entry_at(pos);
+		pos = unmerge_index_entry_at(istate, pos);
 		if (pos < istate->cache_nr) {
 			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_stage(ce) &&
@@ -788,8 +788,9 @@ 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(repo);
+	*o->has_errors |= refresh_index(istate, o->flags | flag,
+					NULL, NULL, NULL);
 	return 0;
 }
 
@@ -825,7 +826,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(istate);
 	return 0;
 }
 
diff --git a/cache.h b/cache.h
index 7bfb8195d97..dfcbc4923e2 100644
--- a/cache.h
+++ b/cache.h
@@ -421,7 +421,6 @@ extern struct index_state the_index;
 #define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #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))
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 09/12] update-index: use remove_file_from_index()

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of remove_file_from_cache().
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 9e57779731f..6f51fb4a14e 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -252,7 +252,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(istate, path))
 		return error("%s: cannot remove from the index", path);
 	return 0;
 }
@@ -386,7 +386,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(istate, path))
 			return error("%s: cannot remove from the index", path);
 		return 0;
 	}
@@ -484,7 +484,7 @@ static void update_one(const char *path)
 	}
 
 	if (force_remove) {
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(istate, path))
 			die("git update-index: unable to remove %s", path);
 		report("remove '%s'", path);
 		return;
@@ -567,7 +567,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(istate, path_name))
 				die("git update-index: unable to remove %s",
 				    ptr);
 		}
@@ -682,7 +682,7 @@ static int unresolve_one(const char *path)
 		goto free_return;
 	}
 
-	remove_file_from_cache(path);
+	remove_file_from_index(istate, path);
 	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
 		error("%s: cannot add our version to the index.", path);
 		ret = -1;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 08/12] update-index: use index_name_pos() over cache_name_pos()

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index a64f2f5a8f4..9e57779731f 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -233,7 +233,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(istate, path, namelen);
 	if (0 <= pos) {
 		mark_fsmonitor_invalid(istate, istate->cache[pos]);
 		if (mark)
@@ -327,7 +327,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(istate, path, len);
 
 	/* Exact match: file or existing gitlink */
 	if (pos >= 0) {
@@ -377,7 +377,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 	if (has_symlink_leading_path(path, len))
 		return error("'%s' is beyond a symbolic link", path);
 
-	pos = cache_name_pos(path, len);
+	pos = index_name_pos(istate, path, len);
 	ce = pos < 0 ? NULL : istate->cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
@@ -437,7 +437,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(istate, path, strlen(path));
 	if (pos < 0)
 		goto fail;
 	ce = istate->cache[pos];
@@ -634,7 +634,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(istate, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 05/12] update-index: use istate->cache over active_cache

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
Show changes to builtin/update-index.c +11 −11
diff --git a/builtin/update-index.c b/builtin/update-index.c
index c9a6cde97da..0616c786410 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -235,12 +235,12 @@ 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(istate, active_cache[pos]);
+		mark_fsmonitor_invalid(istate, istate->cache[pos]);
 		if (mark)
-			active_cache[pos]->ce_flags |= flag;
+			istate->cache[pos]->ce_flags |= flag;
 		else
-			active_cache[pos]->ce_flags &= ~flag;
-		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
+			istate->cache[pos]->ce_flags &= ~flag;
+		istate->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(istate, path);
 		active_cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
@@ -331,7 +331,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 = istate->cache[pos];
 		if (S_ISGITLINK(ce->ce_mode)) {
 
 			/* Do nothing to the index if there is no HEAD! */
@@ -347,7 +347,7 @@ 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++];
+		const struct cache_entry *ce = istate->cache[pos++];
 
 		if (strncmp(ce->name, path, len))
 			break;
@@ -378,7 +378,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 : istate->cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
 		 * working directory version is assumed "good"
@@ -440,7 +440,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 = istate->cache[pos];
 	if (chmod_cache_entry(ce, flip) < 0)
 		goto fail;
 
@@ -639,7 +639,7 @@ static int unresolve_one(const char *path)
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
 		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen))
@@ -653,7 +653,7 @@ static int unresolve_one(const char *path)
 		 */
 		pos = -pos-1;
 		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
 				fprintf(stderr,
@@ -749,7 +749,7 @@ static int do_reupdate(int ac, const char **av,
 		has_head = 0;
  redo:
 	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = istate->cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
 		char *path;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 1, 2021, 13:07 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH 12/12] update-index: remove ce_match_stat(), all macros

From: Derrick Stolee <dstolee@microsoft.com>

The final index compatibility macro to remove from the update-index builtin is ce_match_state(). Further, this is the last use of that macro anywhere, so it should be removed.

There are some remaining references in the racy-git.txt technical document that are updated to ie_match_stat().

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Documentation/technical/racy-git.txt | 6 +++---
 builtin/update-index.c               | 3 +--
 cache.h                              | 1 -
 3 files changed, 4 insertions(+), 6 deletions(-)
Show changes to 3 files +4 −6

Documentation/technical/racy-git.txt, builtin/update-index.c, cache.h

diff --git a/Documentation/technical/racy-git.txt b/Documentation/technical/racy-git.txt
index ceda4bbfda4..65188e04559 100644
--- a/Documentation/technical/racy-git.txt
+++ b/Documentation/technical/racy-git.txt
@@ -26,7 +26,7 @@ information obtained from the filesystem via `lstat(2)` system
 call when they were last updated.  When checking if they differ,
 Git first runs `lstat(2)` on the files and compares the result
 with this information (this is what was originally done by the
-`ce_match_stat()` function, but the current code does it in
+`ie_match_stat()` function, but the current code does it in
 `ce_match_stat_basic()` function).  If some of these "cached
 stat information" fields do not match, Git can tell that the
 files are modified without even looking at their contents.
@@ -102,7 +102,7 @@ timestamp as the index file itself.
 
 The callers that want to check if an index entry matches the
 corresponding file in the working tree continue to call
-`ce_match_stat()`, but with this change, `ce_match_stat()` uses
+`ie_match_stat()`, but with this change, `ie_match_stat()` uses
 `ce_modified_check_fs()` to see if racily clean ones are
 actually clean after comparing the cached stat information using
 `ce_match_stat_basic()`.
@@ -128,7 +128,7 @@ Runtime penalty
 ---------------
 
 The runtime penalty of falling back to `ce_modified_check_fs()`
-from `ce_match_stat()` can be very expensive when there are many
+from `ie_match_stat()` can be very expensive when there are many
 racily clean entries.  An obvious way to artificially create
 this situation is to give the same timestamp to all the files in
 the working tree in a large project, run `git update-index` on
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 4da0c169dc7..256df43ecbd 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "lockfile.h"
@@ -277,7 +276,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(istate, old, st, 0))
 		return 0;
 
 	ce = make_empty_cache_entry(istate, len);
diff --git a/cache.h b/cache.h
index dfcbc4923e2..2925bf050b8 100644
--- a/cache.h
+++ b/cache.h
@@ -416,7 +416,6 @@ extern struct index_state the_index;
 #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_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
-- 
gitgitgadget
Elijah Newren· Jan 1, 2021, 21:12 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH 12/12] update-index: remove ce_match_stat(), all macros

On Fri, Jan 1, 2021 at 5:12 AM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 5 quoted lines
>
> From: Derrick Stolee <dstolee@microsoft.com>
>
> The final index compatibility macro to remove from the update-index
> builtin is ce_match_state(). Further, this is the last use of that macro
I think you mean ce_match_stat(); no trailing 'e'.
Show 22 quoted lines
> anywhere, so it should be removed.
>
> There are some remaining references in the racy-git.txt technical
> document that are updated to ie_match_stat().
>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  Documentation/technical/racy-git.txt | 6 +++---
>  builtin/update-index.c               | 3 +--
>  cache.h                              | 1 -
>  3 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/Documentation/technical/racy-git.txt b/Documentation/technical/racy-git.txt
> index ceda4bbfda4..65188e04559 100644
> --- a/Documentation/technical/racy-git.txt
> +++ b/Documentation/technical/racy-git.txt
> @@ -26,7 +26,7 @@ information obtained from the filesystem via `lstat(2)` system
>  call when they were last updated.  When checking if they differ,
>  Git first runs `lstat(2)` on the files and compares the result
>  with this information (this is what was originally done by the
> -`ce_match_stat()` function, but the current code does it in
> +`ie_match_stat()` function, but the current code does it in
Even updating the documentation... :-)

Arguably, this change should have been done years ago when ce_match_stat was turned into a macro, but certainly becomes more important with your patch that removes the macro.

Show 54 quoted lines
>  `ce_match_stat_basic()` function).  If some of these "cached
>  stat information" fields do not match, Git can tell that the
>  files are modified without even looking at their contents.
> @@ -102,7 +102,7 @@ timestamp as the index file itself.
>
>  The callers that want to check if an index entry matches the
>  corresponding file in the working tree continue to call
> -`ce_match_stat()`, but with this change, `ce_match_stat()` uses
> +`ie_match_stat()`, but with this change, `ie_match_stat()` uses
>  `ce_modified_check_fs()` to see if racily clean ones are
>  actually clean after comparing the cached stat information using
>  `ce_match_stat_basic()`.
> @@ -128,7 +128,7 @@ Runtime penalty
>  ---------------
>
>  The runtime penalty of falling back to `ce_modified_check_fs()`
> -from `ce_match_stat()` can be very expensive when there are many
> +from `ie_match_stat()` can be very expensive when there are many
>  racily clean entries.  An obvious way to artificially create
>  this situation is to give the same timestamp to all the files in
>  the working tree in a large project, run `git update-index` on
> diff --git a/builtin/update-index.c b/builtin/update-index.c
> index 4da0c169dc7..256df43ecbd 100644
> --- a/builtin/update-index.c
> +++ b/builtin/update-index.c
> @@ -3,7 +3,6 @@
>   *
>   * Copyright (C) Linus Torvalds, 2005
>   */
> -#define USE_THE_INDEX_COMPATIBILITY_MACROS
>  #include "cache.h"
>  #include "config.h"
>  #include "lockfile.h"
> @@ -277,7 +276,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(istate, old, st, 0))
>                 return 0;
>
>         ce = make_empty_cache_entry(istate, len);
> diff --git a/cache.h b/cache.h
> index dfcbc4923e2..2925bf050b8 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -416,7 +416,6 @@ extern struct index_state the_index;
>  #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_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
>  #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
The rest looks good.
Elijah Newren· Jan 1, 2021, 21:16 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH 00/12] Remove more index compatibility macros

On Fri, Jan 1, 2021 at 5:10 AM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 23 quoted lines
>
> I noticed that Duy's project around USE_THE_INDEX_COMPATIBILITY_MACROS has
> been on pause for a while. Here is my attempt to continue that project a
> little.
>
> I started going through the builtins that still use cache_name_pos() and the
> first few were easy: merge-inex, mv, rm.
>
> Then I hit update-index and it was a bit bigger. It's included here as well.
>
> My strategy for update-index was to create static globals "repo" and
> "istate" that point to the_repository and the_index, respectively. Then, I
> was able to remove macros one-by-one without changing method prototypes
> within the file.
>
> I had started trying to keep everything local to the method signatures, but
> I hit a snag when reaching the command-line parsing callbacks, which I could
> not modify their call signature. At that point, I had something that was
> already much more complicated than what I present now. Outside of the first
> update-index commit, everything was a mechanical find/replace.
>
> In total, this allows us to remove four of the compatibility macros because
> they are no longer used.

This series is divided nicely in a way that makes review easy. I've made some of these same types of changes elsewhere, and the whole series is really just a long sequence of mechanical changes (plus a case or two of fixing formatting on a line you were changing anyway, such as adding spaces around an operator). I think the work to continue dropping the implicit dependency on the_index is helpful.

I only found two minor suggestions for improving the commit messages; the patches all look good to me.

Eric Sunshine· Jan 2, 2021, 06:12 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH 00/12] Remove more index compatibility macros

On Fri, Jan 1, 2021 at 8:09 AM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 8 quoted lines
> My strategy for update-index was to create static globals "repo" and
> "istate" that point to the_repository and the_index, respectively. Then, I
> was able to remove macros one-by-one without changing method prototypes
> within the file.
>
> I had started trying to keep everything local to the method signatures, but
> I hit a snag when reaching the command-line parsing callbacks, which I could
> not modify their call signature. [...]

You should be able to do this, not by modifying the callback signature, but by taking advantage of the `extra` member of `struct option` which is available to callback functions or arbitrary use. If you need to access the index in a callback, then assign a `struct index_state *` to `extra`; likewise assign a `struct repository *` to `extra` to access the repository. If you need access to both the index and the repository, then just store the repository in `extra` since the repository has an `index` field.

You won't be able to use any of the canned OPT_FOO() macros to initialize an entry in the update-index.c `options[]` array which needs `extra`-initialization since the macros don't let you specify `extra`, but you can easily bypass the macro and initialize the `struct option` manually. (After all, the macros exist for convenience; they are not a hard requirement.)

Within the callback, extract the `repository` or `index_state` as you would any other field. For instance:

    const struct repository *repo = opt->extra;

This should allow you to get rid of the globals introduced by patch [4/12] (assuming passing the index and repo arguments around everywhere doesn't get overly hairy).

Derrick Stolee· Jan 4, 2021, 01:01 UTC · re: Eric Sunshine · lore

Re: [PATCH 00/12] Remove more index compatibility macros

On 1/2/2021 1:12 AM, Eric Sunshine wrote:
Show 31 quoted lines
> On Fri, Jan 1, 2021 at 8:09 AM Derrick Stolee via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>> My strategy for update-index was to create static globals "repo" and
>> "istate" that point to the_repository and the_index, respectively. Then, I
>> was able to remove macros one-by-one without changing method prototypes
>> within the file.
>>
>> I had started trying to keep everything local to the method signatures, but
>> I hit a snag when reaching the command-line parsing callbacks, which I could
>> not modify their call signature. [...]
> 
> You should be able to do this, not by modifying the callback
> signature, but by taking advantage of the `extra` member of `struct
> option` which is available to callback functions or arbitrary use. If
> you need to access the index in a callback, then assign a `struct
> index_state *` to `extra`; likewise assign a `struct repository *` to
> `extra` to access the repository. If you need access to both the index
> and the repository, then just store the repository in `extra` since
> the repository has an `index` field.
> 
> You won't be able to use any of the canned OPT_FOO() macros to
> initialize an entry in the update-index.c `options[]` array which
> needs `extra`-initialization since the macros don't let you specify
> `extra`, but you can easily bypass the macro and initialize the
> `struct option` manually. (After all, the macros exist for
> convenience; they are not a hard requirement.)
> 
> Within the callback, extract the `repository` or `index_state` as you
> would any other field. For instance:
> 
>     const struct repository *repo = opt->extra;
Yes, this is definitely the way to make it possible.
> This should allow you to get rid of the globals introduced by patch
> [4/12] (assuming passing the index and repo arguments around
> everywhere doesn't get overly hairy).

My attempts just getting to the point of hitting these callbacks was already making me frustrated with how complicated the code became with that approach.

Perhaps now that I've removed the compatibility macros, it would be easier to insert the method parameters since most of the lines that need to change would be method prototypes and the calls to those methods (plus the callback function details).

Is that a valuable effort? I could give it a try, but I want to be sure that adjusting all of those helper methods in the builtin would indeed have valuable improvements over the static globals used here.

Thanks, -Stolee

Eric Sunshine· Jan 4, 2021, 06:22 UTC · re: Derrick Stolee · lore

Re: [PATCH 00/12] Remove more index compatibility macros

On Sun, Jan 3, 2021 at 8:01 PM Derrick Stolee <stolee@gmail.com> wrote:
Show 13 quoted lines
> On 1/2/2021 1:12 AM, Eric Sunshine wrote:
> > This should allow you to get rid of the globals introduced by patch
> > [4/12] (assuming passing the index and repo arguments around
> > everywhere doesn't get overly hairy).
>
> Perhaps now that I've removed the compatibility macros, it would be
> easier to insert the method parameters since most of the lines that
> need to change would be method prototypes and the calls to those methods
> (plus the callback function details).
>
> Is that a valuable effort? I could give it a try, but I want to be sure
> that adjusting all of those helper methods in the builtin would indeed
> have valuable improvements over the static globals used here.

My impression was that the goal of the earlier work was to pass the index and repository to each function specifically to avoid tying the function to a particular index or repository. This helps in cases in which client code needs to operate on a different index or repository (for instance, a submodule). Generally speaking, making the index and repository file-static rather than global does not help reach that goal since functions are still tied to state which is not local to the function itself.

Would the extra effort be valuable in this particular case? I'm not familiar with this code, but given that `update-index` is a builtin, such effort may not be too meaningful. If, however, any of the code from `buildin/update-index.c` ever gets "libified" and moved into the core library, then that would be a good time to update the functions to take those values as arguments rather than relying on file-static or globals. But that's not something that this series necessarily needs to do; the task can wait until the code needs to be shared by other modules, I would think.

Derrick Stolee· Jan 5, 2021, 04:41 UTC · re: Eric Sunshine · lore

Re: [PATCH 00/12] Remove more index compatibility macros

On 1/4/2021 1:22 AM, Eric Sunshine wrote:
Show 33 quoted lines
> On Sun, Jan 3, 2021 at 8:01 PM Derrick Stolee <stolee@gmail.com> wrote:
>> On 1/2/2021 1:12 AM, Eric Sunshine wrote:
>>> This should allow you to get rid of the globals introduced by patch
>>> [4/12] (assuming passing the index and repo arguments around
>>> everywhere doesn't get overly hairy).
>>
>> Perhaps now that I've removed the compatibility macros, it would be
>> easier to insert the method parameters since most of the lines that
>> need to change would be method prototypes and the calls to those methods
>> (plus the callback function details).
>>
>> Is that a valuable effort? I could give it a try, but I want to be sure
>> that adjusting all of those helper methods in the builtin would indeed
>> have valuable improvements over the static globals used here.
> 
> My impression was that the goal of the earlier work was to pass the
> index and repository to each function specifically to avoid tying the
> function to a particular index or repository. This helps in cases in
> which client code needs to operate on a different index or repository
> (for instance, a submodule). Generally speaking, making the index and
> repository file-static rather than global does not help reach that
> goal since functions are still tied to state which is not local to the
> function itself.
> 
> Would the extra effort be valuable in this particular case? I'm not
> familiar with this code, but given that `update-index` is a builtin,
> such effort may not be too meaningful. If, however, any of the code
> from `buildin/update-index.c` ever gets "libified" and moved into the
> core library, then that would be a good time to update the functions
> to take those values as arguments rather than relying on file-static
> or globals. But that's not something that this series necessarily
> needs to do; the task can wait until the code needs to be shared by
> other modules, I would think.

I tried again tonight, and it started getting messy, but then I realized that I could group the callbacks that need the repo and index to use a common struct that holds the other parameters they need. It's still a bigger patch than I'd like, but it is more reasonable.

v2 is incoming with my attempt at this.

Thanks, -Stolee

Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 00/14] Remove more index compatibility macros

UPDATE: this is now based on ag/merge-strategies-in-c to avoid conflicts in
'seen'. The changes in builtin/rm.c still conflict with
mt/rm-sparse-checkout, but that branch seems to be waiting for a clearer
plan on some corner cases. I thought about ejecting it, but 'rm' still uses
ce_match_stat(), so just dropping the patch gives less of a final stake at
the end of the series. (I'm still open to it, if necessary.)

I noticed that Duy's project around USE_THE_INDEX_COMPATIBILITY_MACROS has been on pause for a while. Here is my attempt to continue that project a little.

I started going through the builtins that still use cache_name_pos() and the first was easy: mv and rm.

Then I hit update-index and it was a bit bigger.

My strategy for update-index was to create static globals "repo" and "istate" that point to the_repository and the_index, respectively. Then, I was able to remove macros one-by-one without changing method prototypes within the file. Then, these static globals were also removed by systematically updating the local method prototypes, plus some fancy structure stuff for the option parsing callbacks.

I had started trying to keep everything local to the method signatures, but I hit a snag when reaching the command-line parsing callbacks, which I could not modify their call signature. At that point, I had something that was already much more complicated than what I present now. Outside of the first update-index commit, everything was a mechanical find/replace.

In total, this allows us to remove four of the compatibility macros because they are no longer used.

Updates in V2 =============

 * newly based on ag/merge-strategies-in-c, as there were some interesting
   conflicts in buitin/update-index.c.
 * Patch to update builtin/merge-index.c was dropped as that is already
   handled in ag/merge-strategies-in-c
 * I added patches that remove the static globals that I injected to make
   the compatibility macros easy to delete. I do this in three parts.
 * Commit messages improved.
Thanks, -Stolee
Derrick Stolee (14):
  mv: remove index compatibility macros
  rm: remove compatilibity macros
  update-index: drop the_index, the_repository
  update-index: use istate->cache over active_cache
  update-index: use index->cache_nr over active_nr
  update-index: use istate->cache_changed
  update-index: use index_name_pos() over cache_name_pos()
  update-index: use remove_file_from_index()
  update-index: use add_index_entry()
  update-index: replace several compatibility macros
  update-index: remove ce_match_stat(), all macros
  update-index: reduce static globals, part 1
  update-index: reduce static globals, part 2
  update-index: remove static globals from callbacks
 Documentation/technical/racy-git.txt |   6 +-
 builtin/mv.c                         |  42 ++--
 builtin/rm.c                         |  56 ++---
 builtin/update-index.c               | 320 +++++++++++++++------------
 cache.h                              |   4 -
 5 files changed, 233 insertions(+), 195 deletions(-)
base-commit: 3da8920d38a007157ccf8e53382e5206b909dafe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-830%2Fderrickstolee%2Findex-compatibility-1-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-830/derrickstolee/index-compatibility-1-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/830
Range-diff vs v1:
  2:  84330533d4f =  1:  5ccc464cf26 mv: remove index compatibility macros
  3:  54e167d5872 =  2:  e715c703cb8 rm: remove compatilibity macros
  4:  77f6510bb68 !  3:  4bf3c582f9d update-index: drop the_index, the_repository
     @@ Commit message
          To reduce the need for the index compatibility macros, we will replace
          their uses in update-index mechanically. This is the most interesting
          change, which creates global "repo" and "istate" pointers. The macros
     -    can then be mechanically replaced by instances that use the istate
     -    pointer instead of the version that autocompletes to use the_index.
     +    that expand to use the_index can then be mechanically replaced by
     +    references to the istate pointer.
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
     @@ builtin/update-index.c: static int add_one_path(const struct cache_entry *old, c
       		discard_cache_entry(ce);
       		return -1;
      @@ builtin/update-index.c: static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
     - 		return error("Invalid path '%s'", path);
     - 
     - 	len = strlen(path);
     --	ce = make_empty_cache_entry(&the_index, len);
     -+	ce = make_empty_cache_entry(istate, len);
     - 
     - 	oidcpy(&ce->oid, oid);
     - 	memcpy(ce->name, path, len);
     + {
     + 	int res;
     + 
     +-	res = add_to_index_cacheinfo(&the_index, mode, oid, path, stage,
     ++	res = add_to_index_cacheinfo(istate, mode, oid, path, stage,
     + 				     allow_add, allow_replace, NULL);
     + 	if (res == -1)
     + 		return res;
      @@ builtin/update-index.c: static struct cache_entry *read_one_ent(const char *which,
       	struct object_id oid;
       	struct cache_entry *ce;
  5:  cc5df3566df =  4:  4b509ba5fa2 update-index: use istate->cache over active_cache
  6:  e99b8bddb3a =  5:  6c0e019f91c update-index: use index->cache_nr over active_nr
  7:  f841500c663 =  6:  5091e2661d1 update-index: use istate->cache_changed
  8:  d2af7e21ca1 =  7:  5b14fa10a4b update-index: use index_name_pos() over cache_name_pos()
  9:  c576e2d9676 =  8:  a1a9fb01b07 update-index: use remove_file_from_index()
 10:  cf091799cae !  9:  620e300ad6b update-index: use add_index_entry()
     @@ builtin/update-index.c: static int add_one_path(const struct cache_entry *old, c
       		discard_cache_entry(ce);
       		return error("%s: cannot add to the index - missing --add option?", path);
       	}
     -@@ builtin/update-index.c: 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(istate, ce, option))
     - 		return error("%s: cannot add to the index - missing --add option?",
     - 			     path);
     - 	report("add '%s'", path);
      @@ builtin/update-index.c: static int unresolve_one(const char *path)
       	}
       
 11:  d7856e2e772 = 10:  68b26a11d77 update-index: replace several compatibility macros
 12:  8fb307c3bee ! 11:  f1cffe2b455 update-index: remove ce_match_stat(), all macros
     @@ Commit message
          update-index: remove ce_match_stat(), all macros
      
          The final index compatibility macro to remove from the update-index
     -    builtin is ce_match_state(). Further, this is the last use of that macro
     +    builtin is ce_match_stat(). Further, this is the last use of that macro
          anywhere, so it should be removed.
      
          There are some remaining references in the racy-git.txt technical
  -:  ----------- > 12:  79e267f39ec update-index: reduce static globals, part 1
  1:  68d88b651c7 ! 13:  457402b4fdc merge-index: drop index compatibility macros
     @@ Metadata
      Author: Derrick Stolee <dstolee@microsoft.com>
      
       ## Commit message ##
     -    merge-index: drop index compatibility macros
     +    update-index: reduce static globals, part 2
      
     -    Replace uses of the old macros for the_index and instead pass around a
     -    'struct index_state' pointer. This allows dropping the compatibility
     -    flag.
     +    In order to remove index compatibility macros cleanly, we relied upon
     +    static globals 'repo' and 'istate' to be pointers to the_repository and
     +    the_index, respectively. We can continue reducing the need for these
     +    static globals by modifying method prototypes to use them when
     +    necessary.
     +
     +    Move these static globals further down the file so we can identify which
     +    methods need both 'struct repository *repo' and 'struct index_state
     +    *istate' parameters. The only changes included here adjust method
     +    prototypes and their call locations.
     +
     +    The only remaining change is to remove the static globals entirely, but
     +    that requires updating the parse-opt callbacks, which need a different
     +    solution.
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
     - ## builtin/merge-index.c ##
     -@@
     --#define USE_THE_INDEX_COMPATIBILITY_MACROS
     - #include "builtin.h"
     - #include "run-command.h"
     + ## builtin/update-index.c ##
     +@@ builtin/update-index.c: static const char * const update_index_usage[] = {
     + static struct object_id head_oid;
     + static struct object_id merge_head_oid;
       
     -@@ builtin/merge-index.c: static const char *pgm;
     - static int one_shot, quiet;
     - static int err;
     - 
     --static int merge_entry(int pos, const char *path)
     -+static int merge_entry(struct index_state *istate,
     -+		       int pos, const char *path)
     +-static struct repository *repo;
     +-static struct index_state *istate;
     +-
     +-static struct cache_entry *read_one_ent(const char *which,
     ++static struct cache_entry *read_one_ent(struct repository *repo,
     ++					struct index_state *istate,
     ++					const char *which,
     + 					struct object_id *ent, const char *path,
     + 					int namelen, int stage)
       {
     - 	int found;
     - 	const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
     - 	char hexbuf[4][GIT_MAX_HEXSZ + 1];
     - 	char ownbuf[4][60];
     - 
     --	if (pos >= active_nr)
     -+	if (pos >= istate->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 = istate->cache[pos];
     - 		int stage = ce_stage(ce);
     - 
     - 		if (strcmp(ce->name, path))
     -@@ builtin/merge-index.c: 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 < istate->cache_nr);
     - 	if (!found)
     - 		die("git merge-index: %s not in the cache", path);
     - 
     -@@ builtin/merge-index.c: static int merge_entry(int pos, const char *path)
     - 	return found;
     +@@ builtin/update-index.c: static struct cache_entry *read_one_ent(const char *which,
     + 	return ce;
       }
       
     --static void merge_one_path(const char *path)
     -+static void merge_one_path(struct index_state *istate,
     -+			   const char *path)
     +-static int unresolve_one(const char *path)
     ++static int unresolve_one(struct repository *repo,
     ++			 struct index_state *istate,
     ++			 const char *path)
       {
     --	int pos = cache_name_pos(path, strlen(path));
     -+	int pos = index_name_pos(istate, path, strlen(path));
     - 
     - 	/*
     - 	 * If it already exists in the cache as stage0, it's
     - 	 * already merged and there is nothing to do.
     + 	int namelen = strlen(path);
     + 	int pos;
     +@@ builtin/update-index.c: static int unresolve_one(const char *path)
     + 	 * stuff HEAD version in stage #2,
     + 	 * stuff MERGE_HEAD version in stage #3.
       	 */
     - 	if (pos < 0)
     --		merge_entry(-pos-1, path);
     -+		merge_entry(istate, -pos - 1, path);
     +-	ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
     +-	ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
     ++	ce_2 = read_one_ent(repo, istate, "our", &head_oid, path, namelen, 2);
     ++	ce_3 = read_one_ent(repo, istate, "their", &merge_head_oid, path, namelen, 3);
     + 
     + 	if (!ce_2 || !ce_3) {
     + 		ret = -1;
     +@@ builtin/update-index.c: static void read_head_pointers(void)
     + 	}
       }
       
     --static void merge_all(void)
     -+static void merge_all(struct index_state *istate)
     +-static int do_unresolve(int ac, const char **av,
     ++static int do_unresolve(struct repository *repo,
     ++			struct index_state *istate,
     ++			int ac, const char **av,
     + 			const char *prefix, int prefix_length)
       {
       	int i;
     --	for (i = 0; i < active_nr; i++) {
     --		const struct cache_entry *ce = active_cache[i];
     -+	for (i = 0; i < istate->cache_nr; i++) {
     -+		const struct cache_entry *ce = istate->cache[i];
     - 		if (!ce_stage(ce))
     - 			continue;
     --		i += merge_entry(i, ce->name)-1;
     -+		i += merge_entry(istate, i, ce->name)-1;
     +@@ builtin/update-index.c: static int do_unresolve(int ac, const char **av,
     + 	for (i = 1; i < ac; i++) {
     + 		const char *arg = av[i];
     + 		char *p = prefix_path(prefix, prefix_length, arg);
     +-		err |= unresolve_one(p);
     ++		err |= unresolve_one(repo, istate, p);
     + 		free(p);
       	}
     + 	return err;
       }
       
     - int cmd_merge_index(int argc, const char **argv, const char *prefix)
     +-static int do_reupdate(int ac, const char **av,
     ++static int do_reupdate(struct repository *repo,
     ++		       struct index_state *istate,
     ++		       int ac, const char **av,
     + 		       const char *prefix)
       {
     - 	int i, force_file = 0;
     -+	struct index_state *istate;
     + 	/* Read HEAD and run update-index on paths that are
     +@@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
     + 		if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
     + 			continue;
     + 		if (has_head)
     +-			old = read_one_ent(NULL, &head_oid,
     ++			old = read_one_ent(repo, istate, NULL, &head_oid,
     + 					   ce->name, ce_namelen(ce), 0);
     + 		if (old && ce->ce_mode == old->ce_mode &&
     + 		    oideq(&ce->oid, &old->oid)) {
     +@@ builtin/update-index.c: struct refresh_params {
     + 	int *has_errors;
     + };
       
     - 	/* Without this we cannot rely on waitpid() to tell
     - 	 * what happened to our children.
     -@@ builtin/merge-index.c: 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>...])");
     ++static struct repository *repo;
     ++static struct index_state *istate;
     ++
     + static int refresh(struct refresh_params *o, unsigned int flag)
     + {
     + 	setup_work_tree();
     +@@ builtin/update-index.c: static enum parse_opt_result unresolve_callback(
     + 	BUG_ON_OPT_ARG(arg);
       
     --	read_cache();
     -+	repo_read_index(the_repository);
     -+	istate = the_repository->index;
     + 	/* consume remaining arguments. */
     +-	*has_errors = do_unresolve(ctx->argc, ctx->argv,
     +-				prefix, prefix ? strlen(prefix) : 0);
     ++	*has_errors = do_unresolve(repo, istate, ctx->argc, ctx->argv,
     ++				   prefix, prefix ? strlen(prefix) : 0);
     + 	if (*has_errors)
     + 		istate->cache_changed = 0;
     + 
     +@@ builtin/update-index.c: static enum parse_opt_result reupdate_callback(
     + 
     + 	/* consume remaining arguments. */
     + 	setup_work_tree();
     +-	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
     ++	*has_errors = do_reupdate(repo, istate, ctx->argc, ctx->argv, prefix);
     + 	if (*has_errors)
     + 		istate->cache_changed = 0;
       
     - 	i = 1;
     - 	if (!strcmp(argv[i], "-o")) {
     -@@ builtin/merge-index.c: int cmd_merge_index(int argc, const char **argv, const char *prefix)
     - 				continue;
     - 			}
     - 			if (!strcmp(arg, "-a")) {
     --				merge_all();
     -+				merge_all(istate);
     - 				continue;
     - 			}
     - 			die("git merge-index: unknown option %s", arg);
     - 		}
     --		merge_one_path(arg);
     -+		merge_one_path(istate, arg);
     - 	}
     - 	if (err && !quiet)
     - 		die("merge program failed");
  -:  ----------- > 14:  2b171a142b3 update-index: remove static globals from callbacks
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 03/14] update-index: drop the_index, the_repository

From: Derrick Stolee <dstolee@microsoft.com>

To reduce the need for the index compatibility macros, we will replace their uses in update-index mechanically. This is the most interesting change, which creates global "repo" and "istate" pointers. The macros that expand to use the_index can then be mechanically replaced by references to the istate pointer.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 59 +++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 27 deletions(-)
Show changes to builtin/update-index.c +32 −27
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 44862f5e1de..dde5b01a949 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -40,6 +40,9 @@ static int ignore_skip_worktree_entries;
 #define UNMARK_FLAG 2
 static struct strbuf mtime_dir = STRBUF_INIT;
 
+static struct repository *repo;
+static struct index_state *istate;
+
 /* Untracked cache mode */
 enum uc_mode {
 	UC_UNSPECIFIED = -1,
@@ -232,13 +235,13 @@ 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(istate, active_cache[pos]);
 		if (mark)
 			active_cache[pos]->ce_flags |= flag;
 		else
 			active_cache[pos]->ce_flags &= ~flag;
 		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
-		cache_tree_invalidate_path(&the_index, path);
+		cache_tree_invalidate_path(istate, path);
 		active_cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
@@ -277,14 +280,14 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
 		return 0;
 
-	ce = make_empty_cache_entry(&the_index, len);
+	ce = make_empty_cache_entry(istate, len);
 	memcpy(ce->name, path, len);
 	ce->ce_flags = create_ce_flags(0);
 	ce->ce_namelen = len;
-	fill_stat_cache_info(&the_index, ce, st);
+	fill_stat_cache_info(istate, ce, st);
 	ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
 
-	if (index_path(&the_index, &ce->oid, path, st,
+	if (index_path(istate, &ce->oid, path, st,
 		       info_only ? 0 : HASH_WRITE_OBJECT)) {
 		discard_cache_entry(ce);
 		return -1;
@@ -406,7 +409,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 {
 	int res;
 
-	res = add_to_index_cacheinfo(&the_index, mode, oid, path, stage,
+	res = add_to_index_cacheinfo(istate, mode, oid, path, stage,
 				     allow_add, allow_replace, NULL);
 	if (res == -1)
 		return res;
@@ -592,7 +595,7 @@ static struct cache_entry *read_one_ent(const char *which,
 	struct object_id oid;
 	struct cache_entry *ce;
 
-	if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
+	if (get_tree_entry(repo, ent, path, &oid, &mode)) {
 		if (which)
 			error("%s: not in %s branch.", path, which);
 		return NULL;
@@ -602,7 +605,7 @@ static struct cache_entry *read_one_ent(const char *which,
 			error("%s: not a blob in %s branch.", path, which);
 		return NULL;
 	}
-	ce = make_empty_cache_entry(&the_index, namelen);
+	ce = make_empty_cache_entry(istate, namelen);
 
 	oidcpy(&ce->oid, &oid);
 	memcpy(ce->name, path, namelen);
@@ -740,7 +743,7 @@ static int do_reupdate(int ac, const char **av,
 		int save_nr;
 		char *path;
 
-		if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
+		if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
 			continue;
 		if (has_head)
 			old = read_one_ent(NULL, &head_oid,
@@ -957,7 +960,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	struct parse_opt_ctx_t ctx;
 	strbuf_getline_fn getline_fn;
 	int parseopt_state = PARSE_OPT_UNKNOWN;
-	struct repository *r = the_repository;
 	struct option options[] = {
 		OPT_BIT('q', NULL, &refresh_args.flags,
 			N_("continue refresh even when index needs update"),
@@ -1066,16 +1068,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	git_config(git_default_config, NULL);
 
+	repo = the_repository;
+
 	/* 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(repo, &lock_file, 0);
 	if (newfd < 0)
 		lock_error = errno;
 
-	entries = read_cache();
+	entries = repo_read_index(repo);
 	if (entries < 0)
 		die("cache corrupted");
 
-	the_index.updated_skipworktree = 1;
+	istate = repo->index;
+	istate->updated_skipworktree = 1;
 
 	/*
 	 * Custom copy of parse_options() because we want to handle
@@ -1129,9 +1134,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    preferred_index_format,
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
-		if (the_index.version != preferred_index_format)
+		if (istate->version != preferred_index_format)
 			active_cache_changed |= SOMETHING_CHANGED;
-		the_index.version = preferred_index_format;
+		istate->version = preferred_index_format;
 	}
 
 	if (read_from_stdin) {
@@ -1162,28 +1167,28 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			warning(_("core.splitIndex is set to false; "
 				  "remove or change it, if you really want to "
 				  "enable split index"));
-		if (the_index.split_index)
-			the_index.cache_changed |= SPLIT_INDEX_ORDERED;
+		if (istate->split_index)
+			istate->cache_changed |= SPLIT_INDEX_ORDERED;
 		else
-			add_split_index(&the_index);
+			add_split_index(istate);
 	} else if (!split_index) {
 		if (git_config_get_split_index() == 1)
 			warning(_("core.splitIndex is set to true; "
 				  "remove or change it, if you really want to "
 				  "disable split index"));
-		remove_split_index(&the_index);
+		remove_split_index(istate);
 	}
 
-	prepare_repo_settings(r);
+	prepare_repo_settings(repo);
 	switch (untracked_cache) {
 	case UC_UNSPECIFIED:
 		break;
 	case UC_DISABLE:
-		if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
+		if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
 			warning(_("core.untrackedCache is set to true; "
 				  "remove or change it, if you really want to "
 				  "disable the untracked cache"));
-		remove_untracked_cache(&the_index);
+		remove_untracked_cache(istate);
 		report(_("Untracked cache disabled"));
 		break;
 	case UC_TEST:
@@ -1191,11 +1196,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		return !test_if_untracked_cache_is_supported();
 	case UC_ENABLE:
 	case UC_FORCE:
-		if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
+		if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
 			warning(_("core.untrackedCache is set to false; "
 				  "remove or change it, if you really want to "
 				  "enable the untracked cache"));
-		add_untracked_cache(&the_index);
+		add_untracked_cache(istate);
 		report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
 		break;
 	default:
@@ -1207,14 +1212,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			warning(_("core.fsmonitor is unset; "
 				"set it if you really want to "
 				"enable fsmonitor"));
-		add_fsmonitor(&the_index);
+		add_fsmonitor(istate);
 		report(_("fsmonitor enabled"));
 	} else if (!fsmonitor) {
 		if (git_config_get_fsmonitor() == 1)
 			warning(_("core.fsmonitor is set; "
 				"remove it if you really want to "
 				"disable fsmonitor"));
-		remove_fsmonitor(&the_index);
+		remove_fsmonitor(istate);
 		report(_("fsmonitor disabled"));
 	}
 
@@ -1224,7 +1229,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 				exit(128);
 			unable_to_lock_die(get_index_file(), lock_error);
 		}
-		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		if (write_locked_index(istate, &lock_file, COMMIT_LOCK))
 			die("Unable to write new index file");
 	}
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 02/14] rm: remove compatilibity macros

From: Derrick Stolee <dstolee@microsoft.com>

The rm builtin still uses the antiquated compatibility macros for interacting with the index. Update these to the more modern uses by passing around a 'struct index_state' pointer.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/rm.c | 56 ++++++++++++++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
Show changes to builtin/rm.c +30 −26
diff --git a/builtin/rm.c b/builtin/rm.c
index 4858631e0f0..767df8d6b25 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds 2006
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
@@ -28,12 +27,14 @@ static struct {
 	} *entry;
 } list;
 
-static int get_ours_cache_pos(const char *path, int pos)
+static int get_ours_cache_pos(struct index_state *istate,
+			      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 < istate->cache_nr) &&
+	       !strcmp(istate->cache[i]->name, path)) {
+		if (ce_stage(istate->cache[i]) == 2)
 			return i;
 		i++;
 	}
@@ -61,7 +62,7 @@ static void print_error_files(struct string_list *files_list,
 	}
 }
 
-static void submodules_absorb_gitdir_if_needed(void)
+static void submodules_absorb_gitdir_if_needed(struct index_state *istate)
 {
 	int i;
 	for (i = 0; i < list.nr; i++) {
@@ -69,13 +70,13 @@ 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(istate, name, strlen(name));
 		if (pos < 0) {
-			pos = get_ours_cache_pos(name, pos);
+			pos = get_ours_cache_pos(istate, name, pos);
 			if (pos < 0)
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = istate->cache[pos];
 
 		if (!S_ISGITLINK(ce->ce_mode) ||
 		    !file_exists(ce->name) ||
@@ -88,7 +89,8 @@ static void submodules_absorb_gitdir_if_needed(void)
 	}
 }
 
-static int check_local_mod(struct object_id *head, int index_only)
+static int check_local_mod(struct index_state *istate,
+			   struct object_id *head, int index_only)
 {
 	/*
 	 * Items in list are already sorted in the cache order,
@@ -114,21 +116,21 @@ 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(istate, name, strlen(name));
 		if (pos < 0) {
 			/*
 			 * Skip unmerged entries except for populated submodules
 			 * that could lose history when removed.
 			 */
-			pos = get_ours_cache_pos(name, pos);
+			pos = get_ours_cache_pos(istate, name, pos);
 			if (pos < 0)
 				continue;
 
-			if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
+			if (!S_ISGITLINK(istate->cache[pos]->ce_mode) ||
 			    is_empty_dir(name))
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = istate->cache[pos];
 
 		if (lstat(ce->name, &st) < 0) {
 			if (!is_missing_file_error(errno))
@@ -165,7 +167,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(istate, ce, &st, 0) ||
 		    (S_ISGITLINK(ce->ce_mode) &&
 		     bad_to_remove_submodule(ce->name,
 				SUBMODULE_REMOVAL_DIE_ON_ERROR |
@@ -257,6 +259,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	int i;
 	struct pathspec pathspec;
 	char *seen;
+	struct index_state *istate;
 
 	git_config(git_default_config, NULL);
 
@@ -284,24 +287,25 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	if (!index_only)
 		setup_work_tree();
 
-	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);
+	istate = the_repository->index;
+	refresh_index(istate, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
 
 	seen = xcalloc(pathspec.nr, 1);
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
-		if (!ce_path_match(&the_index, ce, &pathspec, seen))
+	for (i = 0; i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
+		if (!ce_path_match(istate, ce, &pathspec, seen))
 			continue;
 		ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
 		list.entry[list.nr].name = xstrdup(ce->name);
 		list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
 		if (list.entry[list.nr++].is_submodule &&
-		    !is_staging_gitmodules_ok(&the_index))
+		    !is_staging_gitmodules_ok(istate))
 			die(_("please stage your changes to .gitmodules or stash them to proceed"));
 	}
 
@@ -329,7 +333,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	}
 
 	if (!index_only)
-		submodules_absorb_gitdir_if_needed();
+		submodules_absorb_gitdir_if_needed(istate);
 
 	/*
 	 * If not forced, the file, the index and the HEAD (if exists)
@@ -345,7 +349,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		struct object_id oid;
 		if (get_oid("HEAD", &oid))
 			oidclr(&oid);
-		if (check_local_mod(&oid, index_only))
+		if (check_local_mod(istate, &oid, index_only))
 			exit(1);
 	}
 
@@ -358,7 +362,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(istate, path))
 			die(_("git rm: unable to remove %s"), path);
 	}
 
@@ -398,10 +402,10 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		}
 		strbuf_release(&buf);
 		if (gitmodules_modified)
-			stage_updated_gitmodules(&the_index);
+			stage_updated_gitmodules(istate);
 	}
 
-	if (write_locked_index(&the_index, &lock_file,
+	if (write_locked_index(istate, &lock_file,
 			       COMMIT_LOCK | SKIP_IF_UNCHANGED))
 		die(_("Unable to write new index file"));
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 01/14] mv: remove index compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

The mv builtin uses the compatibility macros to interact with the index. Update these to use modern methods referring to a 'struct index_state' pointer. Several helper methods need to be updated to consider such a pointer, but the modifications are rudimentary.

Two macros can be deleted from cache.h because these are the last uses.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/mv.c | 42 +++++++++++++++++++++++-------------------
 cache.h      |  2 --
 2 files changed, 23 insertions(+), 21 deletions(-)
Show changes to 2 files +23 −21

builtin/mv.c, cache.h

diff --git a/builtin/mv.c b/builtin/mv.c
index 7dac714af90..0055d49a8a7 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) 2006 Johannes Schindelin
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "config.h"
 #include "pathspec.h"
@@ -75,13 +74,14 @@ static const char *add_slash(const char *path)
 
 #define SUBMODULE_WITH_GITDIR ((const char *)1)
 
-static void prepare_move_submodule(const char *src, int first,
+static void prepare_move_submodule(struct index_state *istate,
+				   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(istate->cache[first]->ce_mode))
 		die(_("Directory %s is in index and no submodule?"), src);
-	if (!is_staging_gitmodules_ok(&the_index))
+	if (!is_staging_gitmodules_ok(istate))
 		die(_("Please stage your changes to .gitmodules or stash them to proceed"));
 	strbuf_addf(&submodule_dotgit, "%s/.git", src);
 	*submodule_gitfile = read_gitfile(submodule_dotgit.buf);
@@ -92,19 +92,20 @@ static void prepare_move_submodule(const char *src, int first,
 	strbuf_release(&submodule_dotgit);
 }
 
-static int index_range_of_same_dir(const char *src, int length,
+static int index_range_of_same_dir(struct index_state *istate,
+				   const char *src, int length,
 				   int *first_p, int *last_p)
 {
 	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(istate, src_w_slash, len_w_slash);
 	if (first >= 0)
 		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 < istate->cache_nr; last++) {
+		const char *path = istate->cache[last]->name;
 		if (strncmp(path, src_w_slash, len_w_slash))
 			break;
 	}
@@ -133,6 +134,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 	struct lock_file lock_file = LOCK_INIT;
 	struct cache_entry *ce;
+	struct index_state *istate;
 
 	git_config(git_default_config, NULL);
 
@@ -141,9 +143,10 @@ 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"));
+	istate = the_repository->index;
 
 	source = internal_prefix_pathspec(prefix, argv, argc, 0);
 	modes = xcalloc(argc, sizeof(enum update_mode));
@@ -190,12 +193,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				&& lstat(dst, &st) == 0)
 			bad = _("cannot move directory over file");
 		else if (src_is_dir) {
-			int first = cache_name_pos(src, length), last;
+			int first = index_name_pos(istate, src, length);
+			int last;
 
 			if (first >= 0)
-				prepare_move_submodule(src, first,
+				prepare_move_submodule(istate, src, first,
 						       submodule_gitfile + i);
-			else if (index_range_of_same_dir(src, length,
+			else if (index_range_of_same_dir(istate, src, length,
 							 &first, &last) < 1)
 				bad = _("source directory is empty");
 			else { /* last - first >= 1 */
@@ -212,7 +216,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				dst_len = strlen(dst);
 
 				for (j = 0; j < last - first; j++) {
-					const char *path = active_cache[first + j]->name;
+					const char *path = istate->cache[first + j]->name;
 					source[argc + j] = path;
 					destination[argc + j] =
 						prefix_path(dst, dst_len, path + length + 1);
@@ -221,7 +225,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				}
 				argc += last - first;
 			}
-		} else if (!(ce = cache_file_exists(src, length, ignore_case))) {
+		} else if (!(ce = index_file_exists(istate, src, length, ignore_case))) {
 			bad = _("not under version control");
 		} else if (ce_stage(ce)) {
 			bad = _("conflicted");
@@ -291,15 +295,15 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		if (mode == WORKING_DIRECTORY)
 			continue;
 
-		pos = cache_name_pos(src, strlen(src));
+		pos = index_name_pos(istate, src, strlen(src));
 		assert(pos >= 0);
-		rename_cache_entry_at(pos, dst);
+		rename_index_entry_at(istate, pos, dst);
 	}
 
 	if (gitmodules_modified)
-		stage_updated_gitmodules(&the_index);
+		stage_updated_gitmodules(istate);
 
-	if (write_locked_index(&the_index, &lock_file,
+	if (write_locked_index(istate, &lock_file,
 			       COMMIT_LOCK | SKIP_IF_UNCHANGED))
 		die(_("Unable to write new index file"));
 
diff --git a/cache.h b/cache.h
index 2d844576ead..fdf061cac56 100644
--- a/cache.h
+++ b/cache.h
@@ -409,7 +409,6 @@ extern struct index_state 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_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))
@@ -420,7 +419,6 @@ extern struct index_state the_index;
 #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)
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 04/14] update-index: use istate->cache over active_cache

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
Show changes to builtin/update-index.c +11 −11
diff --git a/builtin/update-index.c b/builtin/update-index.c
index dde5b01a949..3e459d2b9de 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -235,12 +235,12 @@ 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(istate, active_cache[pos]);
+		mark_fsmonitor_invalid(istate, istate->cache[pos]);
 		if (mark)
-			active_cache[pos]->ce_flags |= flag;
+			istate->cache[pos]->ce_flags |= flag;
 		else
-			active_cache[pos]->ce_flags &= ~flag;
-		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
+			istate->cache[pos]->ce_flags &= ~flag;
+		istate->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(istate, path);
 		active_cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
@@ -331,7 +331,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 = istate->cache[pos];
 		if (S_ISGITLINK(ce->ce_mode)) {
 
 			/* Do nothing to the index if there is no HEAD! */
@@ -347,7 +347,7 @@ 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++];
+		const struct cache_entry *ce = istate->cache[pos++];
 
 		if (strncmp(ce->name, path, len))
 			break;
@@ -378,7 +378,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 : istate->cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
 		 * working directory version is assumed "good"
@@ -429,7 +429,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 = istate->cache[pos];
 	if (chmod_cache_entry(ce, flip) < 0)
 		goto fail;
 
@@ -628,7 +628,7 @@ static int unresolve_one(const char *path)
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
 		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen))
@@ -642,7 +642,7 @@ static int unresolve_one(const char *path)
 		 */
 		pos = -pos-1;
 		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
 				fprintf(stderr,
@@ -738,7 +738,7 @@ static int do_reupdate(int ac, const char **av,
 		has_head = 0;
  redo:
 	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = istate->cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
 		char *path;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 05/14] update-index: use index->cache_nr over active_nr

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
Show changes to builtin/update-index.c +7 −7
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 3e459d2b9de..106bfdab78e 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -346,7 +346,7 @@ 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) {
+	while (pos < istate->cache_nr) {
 		const struct cache_entry *ce = istate->cache[pos++];
 
 		if (strncmp(ce->name, path, len))
@@ -627,7 +627,7 @@ static int unresolve_one(const char *path)
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
-		if (pos < active_nr) {
+		if (pos < istate->cache_nr) {
 			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
@@ -641,7 +641,7 @@ static int unresolve_one(const char *path)
 		 * want to do anything in the former case.
 		 */
 		pos = -pos-1;
-		if (pos < active_nr) {
+		if (pos < istate->cache_nr) {
 			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
@@ -737,7 +737,7 @@ static int do_reupdate(int ac, const char **av,
 		 */
 		has_head = 0;
  redo:
-	for (pos = 0; pos < active_nr; pos++) {
+	for (pos = 0; pos < istate->cache_nr; pos++) {
 		const struct cache_entry *ce = istate->cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
@@ -755,14 +755,14 @@ static int do_reupdate(int ac, const char **av,
 		}
 		/* Be careful.  The working tree may not have the
 		 * path anymore, in which case, under 'allow_remove',
-		 * or worse yet 'allow_replace', active_nr may decrease.
+		 * or worse yet 'allow_replace', istate->cache_nr may decrease.
 		 */
-		save_nr = active_nr;
+		save_nr = istate->cache_nr;
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
 		discard_cache_entry(old);
-		if (save_nr != active_nr)
+		if (save_nr != istate->cache_nr)
 			goto redo;
 	}
 	clear_pathspec(&pathspec);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 07/14] update-index: use index_name_pos() over cache_name_pos()

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 346d798c60b..096cbdfa8ac 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -233,7 +233,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(istate, path, namelen);
 	if (0 <= pos) {
 		mark_fsmonitor_invalid(istate, istate->cache[pos]);
 		if (mark)
@@ -327,7 +327,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(istate, path, len);
 
 	/* Exact match: file or existing gitlink */
 	if (pos >= 0) {
@@ -377,7 +377,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 	if (has_symlink_leading_path(path, len))
 		return error("'%s' is beyond a symbolic link", path);
 
-	pos = cache_name_pos(path, len);
+	pos = index_name_pos(istate, path, len);
 	ce = pos < 0 ? NULL : istate->cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
@@ -426,7 +426,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(istate, path, strlen(path));
 	if (pos < 0)
 		goto fail;
 	ce = istate->cache[pos];
@@ -623,7 +623,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(istate, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 09/14] update-index: use add_index_entry()

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of add_cache_entry().
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Show changes to builtin/update-index.c +3 −3
diff --git a/builtin/update-index.c b/builtin/update-index.c
index aa859c4e018..ffa42813370 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -294,7 +294,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(istate, ce, option)) {
 		discard_cache_entry(ce);
 		return error("%s: cannot add to the index - missing --add option?", path);
 	}
@@ -672,12 +672,12 @@ static int unresolve_one(const char *path)
 	}
 
 	remove_file_from_index(istate, path);
-	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
+	if (add_index_entry(istate, 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(istate, ce_3, ADD_CACHE_OK_TO_ADD))
 		return 0;
 	error("%s: cannot add their version to the index.", path);
 	ret = -1;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:43 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 12/14] update-index: reduce static globals, part 1

From: Derrick Stolee <dstolee@microsoft.com>

In order to remove index compatibility macros cleanly, we relied upon static globals 'repo' and 'istate' to be pointers to the_repository and the_index, respectively. We can now start reducing the need for these static globals by modifying method prototypes to use them when necessary.

Move these static globals further down in the file so we can identify which method only need to add a 'struct index_state *istate' parameter. The only changes included here adjust method prototypes and their call locations.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 77 ++++++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 33 deletions(-)
Show changes to builtin/update-index.c +44 −33
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 8fc680090be..6b585fb8ede 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -39,9 +39,6 @@ static int ignore_skip_worktree_entries;
 #define UNMARK_FLAG 2
 static struct strbuf mtime_dir = STRBUF_INIT;
 
-static struct repository *repo;
-static struct index_state *istate;
-
 /* Untracked cache mode */
 enum uc_mode {
 	UC_UNSPECIFIED = -1,
@@ -229,7 +226,8 @@ static int test_if_untracked_cache_is_supported(void)
 	return ret;
 }
 
-static int mark_ce_flags(const char *path, int flag, int mark)
+static int mark_ce_flags(struct index_state *istate,
+			 const char *path, int flag, int mark)
 {
 	int namelen = strlen(path);
 	int pos = index_name_pos(istate, path, namelen);
@@ -247,7 +245,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 	return -1;
 }
 
-static int remove_one_path(const char *path)
+static int remove_one_path(struct index_state *istate, const char *path)
 {
 	if (!allow_remove)
 		return error("%s: does not exist and --remove not passed", path);
@@ -263,14 +261,17 @@ static int remove_one_path(const char *path)
  *    succeeds.
  *  - permission error. That's never ok.
  */
-static int process_lstat_error(const char *path, int err)
+static int process_lstat_error(struct index_state *istate,
+			       const char *path, int err)
 {
 	if (is_missing_file_error(err))
-		return remove_one_path(path);
+		return remove_one_path(istate, path);
 	return error("lstat(\"%s\"): %s", path, strerror(err));
 }
 
-static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
+static int add_one_path(struct index_state *istate,
+			const struct cache_entry *old,
+			const char *path, int len, struct stat *st)
 {
 	int option;
 	struct cache_entry *ce;
@@ -323,7 +324,8 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
  *  - it doesn't exist at all in the index, but it is a valid
  *    git directory, and it should be *added* as a gitlink.
  */
-static int process_directory(const char *path, int len, struct stat *st)
+static int process_directory(struct index_state *istate,
+			     const char *path, int len, struct stat *st)
 {
 	struct object_id oid;
 	int pos = index_name_pos(istate, path, len);
@@ -337,10 +339,10 @@ static int process_directory(const char *path, int len, struct stat *st)
 			if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
 				return 0;
 
-			return add_one_path(ce, path, len, st);
+			return add_one_path(istate, ce, path, len, st);
 		}
 		/* Should this be an unconditional error? */
-		return remove_one_path(path);
+		return remove_one_path(istate, path);
 	}
 
 	/* Inexact match: is there perhaps a subdirectory match? */
@@ -361,13 +363,14 @@ static int process_directory(const char *path, int len, struct stat *st)
 
 	/* No match - should we add it as a gitlink? */
 	if (!resolve_gitlink_ref(path, "HEAD", &oid))
-		return add_one_path(NULL, path, len, st);
+		return add_one_path(istate, NULL, path, len, st);
 
 	/* Error out. */
 	return error("%s: is a directory - add files inside instead", path);
 }
 
-static int process_path(const char *path, struct stat *st, int stat_errno)
+static int process_path(struct index_state *istate,
+			const char *path, struct stat *st, int stat_errno)
 {
 	int pos, len;
 	const struct cache_entry *ce;
@@ -395,15 +398,16 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 	 * what to do about the pathname!
 	 */
 	if (stat_errno)
-		return process_lstat_error(path, stat_errno);
+		return process_lstat_error(istate, path, stat_errno);
 
 	if (S_ISDIR(st->st_mode))
-		return process_directory(path, len, st);
+		return process_directory(istate, path, len, st);
 
-	return add_one_path(ce, path, len, st);
+	return add_one_path(istate, ce, path, len, st);
 }
 
-static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
+static int add_cacheinfo(struct index_state *istate,
+			 unsigned int mode, const struct object_id *oid,
 			 const char *path, int stage)
 {
 	int res;
@@ -420,7 +424,8 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 	return 0;
 }
 
-static void chmod_path(char flip, const char *path)
+static void chmod_path(struct index_state *istate,
+		       char flip, const char *path)
 {
 	int pos;
 	struct cache_entry *ce;
@@ -438,7 +443,7 @@ static void chmod_path(char flip, const char *path)
 	die("git update-index: cannot chmod %cx '%s'", flip, path);
 }
 
-static void update_one(const char *path)
+static void update_one(struct index_state *istate, const char *path)
 {
 	int stat_errno = 0;
 	struct stat st;
@@ -456,17 +461,20 @@ static void update_one(const char *path)
 		return;
 	}
 	if (mark_valid_only) {
-		if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
+		if (mark_ce_flags(istate, path, CE_VALID,
+				  mark_valid_only == MARK_FLAG))
 			die("Unable to mark file %s", path);
 		return;
 	}
 	if (mark_skip_worktree_only) {
-		if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
+		if (mark_ce_flags(istate, path, CE_SKIP_WORKTREE,
+				  mark_skip_worktree_only == MARK_FLAG))
 			die("Unable to mark file %s", path);
 		return;
 	}
 	if (mark_fsmonitor_only) {
-		if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG))
+		if (mark_ce_flags(istate, path, CE_FSMONITOR_VALID,
+				  mark_fsmonitor_only == MARK_FLAG))
 			die("Unable to mark file %s", path);
 		return;
 	}
@@ -477,12 +485,12 @@ static void update_one(const char *path)
 		report("remove '%s'", path);
 		return;
 	}
-	if (process_path(path, &st, stat_errno))
+	if (process_path(istate, path, &st, stat_errno))
 		die("Unable to process path %s", path);
 	report("add '%s'", path);
 }
 
-static void read_index_info(int nul_term_line)
+static void read_index_info(struct index_state *istate, int nul_term_line)
 {
 	const int hexsz = the_hash_algo->hexsz;
 	struct strbuf buf = STRBUF_INIT;
@@ -565,7 +573,7 @@ static void read_index_info(int nul_term_line)
 			 * ptr[-41] is at the beginning of sha1
 			 */
 			ptr[-(hexsz + 2)] = ptr[-1] = 0;
-			if (add_cacheinfo(mode, &oid, path_name, stage))
+			if (add_cacheinfo(istate, mode, &oid, path_name, stage))
 				die("git update-index: unable to update %s",
 				    path_name);
 		}
@@ -586,6 +594,9 @@ static const char * const update_index_usage[] = {
 static struct object_id head_oid;
 static struct object_id merge_head_oid;
 
+static struct repository *repo;
+static struct index_state *istate;
+
 static struct cache_entry *read_one_ent(const char *which,
 					struct object_id *ent, const char *path,
 					int namelen, int stage)
@@ -758,7 +769,7 @@ static int do_reupdate(int ac, const char **av,
 		 */
 		save_nr = istate->cache_nr;
 		path = xstrdup(ce->name);
-		update_one(path);
+		update_one(istate, path);
 		free(path);
 		discard_cache_entry(old);
 		if (save_nr != istate->cache_nr)
@@ -854,7 +865,7 @@ static enum parse_opt_result cacheinfo_callback(
 	BUG_ON_OPT_ARG(arg);
 
 	if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
-		if (add_cacheinfo(mode, &oid, path, 0))
+		if (add_cacheinfo(istate, mode, &oid, path, 0))
 			die("git update-index: --cacheinfo cannot add %s", path);
 		ctx->argv++;
 		ctx->argc--;
@@ -864,7 +875,7 @@ static enum parse_opt_result cacheinfo_callback(
 		return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
 	if (strtoul_ui(*++ctx->argv, 8, &mode) ||
 	    get_oid_hex(*++ctx->argv, &oid) ||
-	    add_cacheinfo(mode, &oid, *++ctx->argv, 0))
+	    add_cacheinfo(istate, mode, &oid, *++ctx->argv, 0))
 		die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
 	ctx->argc -= 3;
 	return 0;
@@ -882,7 +893,7 @@ static enum parse_opt_result stdin_cacheinfo_callback(
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
 	allow_add = allow_replace = allow_remove = 1;
-	read_index_info(*nul_term_line);
+	read_index_info(istate, *nul_term_line);
 	return 0;
 }
 
@@ -1108,9 +1119,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 			setup_work_tree();
 			p = prefix_path(prefix, prefix_length, path);
-			update_one(p);
+			update_one(istate, p);
 			if (set_executable_bit)
-				chmod_path(set_executable_bit, p);
+				chmod_path(istate, set_executable_bit, p);
 			free(p);
 			ctx.argc--;
 			ctx.argv++;
@@ -1153,9 +1164,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 				strbuf_swap(&buf, &unquoted);
 			}
 			p = prefix_path(prefix, prefix_length, buf.buf);
-			update_one(p);
+			update_one(istate, p);
 			if (set_executable_bit)
-				chmod_path(set_executable_bit, p);
+				chmod_path(istate, set_executable_bit, p);
 			free(p);
 		}
 		strbuf_release(&unquoted);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:43 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 14/14] update-index: remove static globals from callbacks

From: Derrick Stolee <dstolee@microsoft.com>

In order to remove index compatibility macros cleanly, we relied upon static globals 'repo' and 'istate' to be pointers to the_repository and the_index, respectively. We remove these static globals inside the option parsing callbacks, which are the final uses in update-index.

The callbacks cannot change their method signature, so we must use the value member of 'struct option', assigned in the array of option macros. There are several callback methods that require at least one of 'repo' and 'istate', but they use a variety of different data types for the callback value.

Unify these callback methods to use a consistent 'struct callback_data' that contains 'repo' and 'istate', ready to use. This takes the place of the previous 'struct refresh_params' which served only to group the 'flags' and 'has_errors' ints. We also collect other one-off settings, but only those that require access to the index or repository in their operation.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 110 ++++++++++++++++++++++-------------------
 1 file changed, 60 insertions(+), 50 deletions(-)
Show changes to builtin/update-index.c +60 −50
diff --git a/builtin/update-index.c b/builtin/update-index.c
index fb8d7879783..258db619a40 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -784,19 +784,21 @@ static int do_reupdate(struct repository *repo,
 	return 0;
 }
 
-struct refresh_params {
+struct callback_data {
+	struct repository *repo;
+	struct index_state *istate;
+
 	unsigned int flags;
-	int *has_errors;
+	unsigned int has_errors;
+	unsigned nul_term_line;
+	unsigned read_from_stdin;
 };
 
-static struct repository *repo;
-static struct index_state *istate;
-
-static int refresh(struct refresh_params *o, unsigned int flag)
+static int refresh(struct callback_data *cd, unsigned int flag)
 {
 	setup_work_tree();
-	repo_read_index(repo);
-	*o->has_errors |= refresh_index(istate, o->flags | flag,
+	repo_read_index(cd->repo);
+	cd->has_errors |= refresh_index(cd->istate, cd->flags | flag,
 					NULL, NULL, NULL);
 	return 0;
 }
@@ -818,7 +820,7 @@ static int really_refresh_callback(const struct option *opt,
 }
 
 static int chmod_callback(const struct option *opt,
-				const char *arg, int unset)
+			  const char *arg, int unset)
 {
 	char *flip = opt->value;
 	BUG_ON_OPT_NEG(unset);
@@ -829,11 +831,12 @@ static int chmod_callback(const struct option *opt,
 }
 
 static int resolve_undo_clear_callback(const struct option *opt,
-				const char *arg, int unset)
+				       const char *arg, int unset)
 {
+	struct callback_data *cd = opt->value;
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
-	resolve_undo_clear_index(istate);
+	resolve_undo_clear_index(cd->istate);
 	return 0;
 }
 
@@ -868,12 +871,13 @@ static enum parse_opt_result cacheinfo_callback(
 	struct object_id oid;
 	unsigned int mode;
 	const char *path;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
-		if (add_cacheinfo(istate, mode, &oid, path, 0))
+		if (add_cacheinfo(cd->istate, mode, &oid, path, 0))
 			die("git update-index: --cacheinfo cannot add %s", path);
 		ctx->argv++;
 		ctx->argc--;
@@ -883,7 +887,7 @@ static enum parse_opt_result cacheinfo_callback(
 		return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
 	if (strtoul_ui(*++ctx->argv, 8, &mode) ||
 	    get_oid_hex(*++ctx->argv, &oid) ||
-	    add_cacheinfo(istate, mode, &oid, *++ctx->argv, 0))
+	    add_cacheinfo(cd->istate, mode, &oid, *++ctx->argv, 0))
 		die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
 	ctx->argc -= 3;
 	return 0;
@@ -893,7 +897,7 @@ static enum parse_opt_result stdin_cacheinfo_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *nul_term_line = opt->value;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
@@ -901,7 +905,7 @@ static enum parse_opt_result stdin_cacheinfo_callback(
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
 	allow_add = allow_replace = allow_remove = 1;
-	read_index_info(istate, *nul_term_line);
+	read_index_info(cd->istate, cd->nul_term_line);
 	return 0;
 }
 
@@ -909,14 +913,14 @@ static enum parse_opt_result stdin_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *read_from_stdin = opt->value;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
-	*read_from_stdin = 1;
+	cd->read_from_stdin = 1;
 	return 0;
 }
 
@@ -924,17 +928,17 @@ static enum parse_opt_result unresolve_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *has_errors = opt->value;
 	const char *prefix = startup_info->prefix;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	/* consume remaining arguments. */
-	*has_errors = do_unresolve(repo, istate, ctx->argc, ctx->argv,
-				   prefix, prefix ? strlen(prefix) : 0);
-	if (*has_errors)
-		istate->cache_changed = 0;
+	cd->has_errors = do_unresolve(cd->repo, cd->istate, ctx->argc, ctx->argv,
+				      prefix, prefix ? strlen(prefix) : 0);
+	if (cd->has_errors)
+		cd->istate->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -945,17 +949,18 @@ static enum parse_opt_result reupdate_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *has_errors = opt->value;
 	const char *prefix = startup_info->prefix;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	/* consume remaining arguments. */
 	setup_work_tree();
-	*has_errors = do_reupdate(repo, istate, ctx->argc, ctx->argv, prefix);
-	if (*has_errors)
-		istate->cache_changed = 0;
+	cd->has_errors = do_reupdate(cd->repo, cd->istate,
+				     ctx->argc, ctx->argv, prefix);
+	if (cd->has_errors)
+		cd->istate->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -964,13 +969,13 @@ static enum parse_opt_result reupdate_callback(
 
 int cmd_update_index(int argc, const char **argv, const char *prefix)
 {
-	int newfd, entries, has_errors = 0, nul_term_line = 0;
+	struct repository *repo = the_repository;
+	struct callback_data cd;
+	int newfd, entries;
 	enum uc_mode untracked_cache = UC_UNSPECIFIED;
-	int read_from_stdin = 0;
 	int prefix_length = prefix ? strlen(prefix) : 0;
 	int preferred_index_format = 0;
 	char set_executable_bit = 0;
-	struct refresh_params refresh_args = {0, &has_errors};
 	int lock_error = 0;
 	int split_index = -1;
 	int force_write = 0;
@@ -979,11 +984,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	struct parse_opt_ctx_t ctx;
 	strbuf_getline_fn getline_fn;
 	int parseopt_state = PARSE_OPT_UNKNOWN;
+	struct index_state *istate;
+
 	struct option options[] = {
-		OPT_BIT('q', NULL, &refresh_args.flags,
+		OPT_BIT('q', NULL, &cd.flags,
 			N_("continue refresh even when index needs update"),
 			REFRESH_QUIET),
-		OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
+		OPT_BIT(0, "ignore-submodules", &cd.flags,
 			N_("refresh: ignore submodules"),
 			REFRESH_IGNORE_SUBMODULES),
 		OPT_SET_INT(0, "add", &allow_add,
@@ -992,18 +999,18 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			N_("let files replace directories and vice-versa"), 1),
 		OPT_SET_INT(0, "remove", &allow_remove,
 			N_("notice files missing from worktree"), 1),
-		OPT_BIT(0, "unmerged", &refresh_args.flags,
+		OPT_BIT(0, "unmerged", &cd.flags,
 			N_("refresh even if index contains unmerged entries"),
 			REFRESH_UNMERGED),
-		OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
+		OPT_CALLBACK_F(0, "refresh", &cd, NULL,
 			N_("refresh stat information"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 			refresh_callback),
-		OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
+		OPT_CALLBACK_F(0, "really-refresh", &cd, NULL,
 			N_("like --refresh, but ignore assume-unchanged setting"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 			really_refresh_callback),
-		{OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", &cd,
 			N_("<mode>,<object>,<path>"),
 			N_("add the specified entry to the index"),
 			PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
@@ -1032,30 +1039,30 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			N_("add to index only; do not add content to object database"), 1),
 		OPT_SET_INT(0, "force-remove", &force_remove,
 			N_("remove named paths even if present in worktree"), 1),
-		OPT_BOOL('z', NULL, &nul_term_line,
+		OPT_BOOL('z', NULL, &cd.nul_term_line,
 			 N_("with --stdin: input lines are terminated by null bytes")),
-		{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &cd, NULL,
 			N_("read list of paths to be updated from standard input"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, stdin_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &cd, NULL,
 			N_("add entries from standard input to the index"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, stdin_cacheinfo_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &cd, NULL,
 			N_("repopulate stages #2 and #3 for the listed paths"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, unresolve_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 'g', "again", &cd, NULL,
 			N_("only update entries that differ from HEAD"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, reupdate_callback},
-		OPT_BIT(0, "ignore-missing", &refresh_args.flags,
+		OPT_BIT(0, "ignore-missing", &cd.flags,
 			N_("ignore files missing from worktree"),
 			REFRESH_IGNORE_MISSING),
 		OPT_SET_INT(0, "verbose", &verbose,
 			N_("report actions to standard output"), 1),
-		OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
+		OPT_CALLBACK_F(0, "clear-resolve-undo", &cd, NULL,
 			N_("(for porcelains) forget saved unresolved conflicts"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 			resolve_undo_clear_callback),
@@ -1087,8 +1094,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	git_config(git_default_config, NULL);
 
-	repo = the_repository;
-
 	/* we will diagnose later if it turns out that we need to update it */
 	newfd = repo_hold_locked_index(repo, &lock_file, 0);
 	if (newfd < 0)
@@ -1098,8 +1103,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	if (entries < 0)
 		die("cache corrupted");
 
-	istate = repo->index;
+	cd.repo = repo;
+	cd.istate = istate = repo->index;
 	istate->updated_skipworktree = 1;
+	cd.flags = 0;
+	cd.has_errors = 0;
+	cd.nul_term_line = 0;
+	cd.read_from_stdin = 0;
 
 	/*
 	 * Custom copy of parse_options() because we want to handle
@@ -1145,7 +1155,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	}
 	argc = parse_options_end(&ctx);
 
-	getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
+	getline_fn = cd.nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
 	if (preferred_index_format) {
 		if (preferred_index_format < INDEX_FORMAT_LB ||
 		    INDEX_FORMAT_UB < preferred_index_format)
@@ -1158,14 +1168,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		istate->version = preferred_index_format;
 	}
 
-	if (read_from_stdin) {
+	if (cd.read_from_stdin) {
 		struct strbuf buf = STRBUF_INIT;
 		struct strbuf unquoted = STRBUF_INIT;
 
 		setup_work_tree();
 		while (getline_fn(&buf, stdin) != EOF) {
 			char *p;
-			if (!nul_term_line && buf.buf[0] == '"') {
+			if (!cd.nul_term_line && buf.buf[0] == '"') {
 				strbuf_reset(&unquoted);
 				if (unquote_c_style(&unquoted, buf.buf, NULL))
 					die("line is badly quoted");
@@ -1244,7 +1254,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	if (istate->cache_changed || force_write) {
 		if (newfd < 0) {
-			if (refresh_args.flags & REFRESH_QUIET)
+			if (cd.flags & REFRESH_QUIET)
 				exit(128);
 			unable_to_lock_die(get_index_file(), lock_error);
 		}
@@ -1254,5 +1264,5 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	rollback_lock_file(&lock_file);
 
-	return has_errors ? 1 : 0;
+	return cd.has_errors ? 1 : 0;
 }
-- 
gitgitgadget
Eric Sunshine· Jan 7, 2021, 05:09 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH v2 14/14] update-index: remove static globals from callbacks

On Mon, Jan 4, 2021 at 11:43 PM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 17 quoted lines
> In order to remove index compatibility macros cleanly, we relied upon
> static globals 'repo' and 'istate' to be pointers to the_repository and
> the_index, respectively. We remove these static globals inside the
> option parsing callbacks, which are the final uses in update-index.
>
> The callbacks cannot change their method signature, so we must use the
> value member of 'struct option', assigned in the array of option macros.
> There are several callback methods that require at least one of 'repo'
> and 'istate', but they use a variety of different data types for the
> callback value.
>
> Unify these callback methods to use a consistent 'struct callback_data'
> that contains 'repo' and 'istate', ready to use. This takes the place of
> the previous 'struct refresh_params' which served only to group the
> 'flags' and 'has_errors' ints. We also collect other one-off settings,
> but only those that require access to the index or repository in their
> operation.

Makes sense. The patch itself is necessarily a bit noisy, but there's nothing particularly complicated in that noise.

Show 15 quoted lines
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
> diff --git a/builtin/update-index.c b/builtin/update-index.c
> @@ -784,19 +784,21 @@ static int do_reupdate(struct repository *repo,
> -struct refresh_params {
> +struct callback_data {
> +       struct repository *repo;
> +       struct index_state *istate;
> +
>         unsigned int flags;
> -       int *has_errors;
> +       unsigned int has_errors;
> +       unsigned nul_term_line;
> +       unsigned read_from_stdin;
>  };

The only mildly unexpected thing here is that `has_errors` is now a simple value rather than a pointer to a value, but you handle that easily enough by always accessing `has_error` directly from the structure, even within the function in which `has_error` used to be a local variable. Fine.

Show 8 quoted lines
> @@ -818,7 +820,7 @@ static int really_refresh_callback(const struct option *opt,
>  static int chmod_callback(const struct option *opt,
> -                               const char *arg, int unset)
> +                         const char *arg, int unset)
> @@ -829,11 +831,12 @@ static int chmod_callback(const struct option *opt,
>  static int resolve_undo_clear_callback(const struct option *opt,
> -                               const char *arg, int unset)
> +                                      const char *arg, int unset)
A couple drive-by indentation fixes. Okay.
> @@ -1098,8 +1103,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
> -       istate = repo->index;
> +       cd.repo = repo;
> +       cd.istate = istate = repo->index;

Will there ever be a case in which `cd.istate` will be different from `cd.repo->index`? If not, then we could get by with having only `cd.repo`; callers requiring access to `istate` can fetch it from `cd.repo`. If, on the other hand, `cd.istate` can be different from `cd.repo->istate` -- or if that might become a possibility in the future -- then having `cd.istate` makes sense. Not a big deal, though. Just generally curious about it.

Derrick Stolee· Jan 7, 2021, 11:19 UTC · re: Eric Sunshine · lore

Re: [PATCH v2 14/14] update-index: remove static globals from callbacks

On 1/7/2021 12:09 AM, Eric Sunshine wrote:
Show 14 quoted lines
> On Mon, Jan 4, 2021 at 11:43 PM Derrick Stolee via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>> @@ -1098,8 +1103,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>> -       istate = repo->index;
>> +       cd.repo = repo;
>> +       cd.istate = istate = repo->index;
> 
> Will there ever be a case in which `cd.istate` will be different from
> `cd.repo->index`? If not, then we could get by with having only
> `cd.repo`; callers requiring access to `istate` can fetch it from
> `cd.repo`. If, on the other hand, `cd.istate` can be different from
> `cd.repo->istate` -- or if that might become a possibility in the
> future -- then having `cd.istate` makes sense. Not a big deal, though.
> Just generally curious about it.
 
I don't believe that 'istate' and 'repo->index' will ever be
different in this file. This includes the members of the
callback_data struct, but also the method parameters throughout.
Mostly, this could be seen as an artifact of how we got here:
1. References to the_index or other compatibility macros were
   converted to use the static global 'istate'.
2. References to the static global 'istate' were replaced with
   method parameters for everything except these callbacks.
3. These callbacks were updated to use 'cd.istate' instead of
   the (now defunct) static global 'istate'.

It could be possible to replace all references to 'istate' with 'repo->index' but the patches get slightly more messy. I also think the code looks messier, but you do make a good point that there is no concrete reason to separate the two.

Thanks, -Stolee

Eric Sunshine· Jan 7, 2021, 18:53 UTC · re: Derrick Stolee · lore

Re: [PATCH v2 14/14] update-index: remove static globals from callbacks

On Thu, Jan 7, 2021 at 6:19 AM Derrick Stolee <stolee@gmail.com> wrote:
Show 17 quoted lines
> On 1/7/2021 12:09 AM, Eric Sunshine wrote:
> > Will there ever be a case in which `cd.istate` will be different from
> > `cd.repo->index`? If not, then we could get by with having only
> > `cd.repo`; callers requiring access to `istate` can fetch it from
> > `cd.repo`. If, on the other hand, `cd.istate` can be different from
> > `cd.repo->istate` -- or if that might become a possibility in the
> > future -- then having `cd.istate` makes sense. Not a big deal, though.
> > Just generally curious about it.
>
> I don't believe that 'istate' and 'repo->index' will ever be
> different in this file. This includes the members of the
> callback_data struct, but also the method parameters throughout.
>
> It could be possible to replace all references to 'istate' with
> 'repo->index' but the patches get slightly more messy. I also
> think the code looks messier, but you do make a good point that
> there is no concrete reason to separate the two.

I agree that it would make the code a bit noisier (to read) if `istate` is eliminated from the callback structure, however, even though I didn't originally feel strongly one way or the other about having both `repo` and `istate` in the structure, I'm now leaning more toward seeing `istate` eliminated. My one (big) concern with `istate` is that it confuses readers into wondering whether `istate` and `repo->istate` will ever be different. One way to avoid such confusion would be to leave a comment in the code stating that the two values will always be the same. The other way, of course, is to eliminate `istate` from the structure altogether. I don't want to make more work for you, but the more I think about it, the more I feel that removing `istate` is the sensible thing to do. (And it doesn't require an extra patch -- it can just be how this patch is crafted -- without ever introducing `istate` to the structure in the first place.)

Junio C Hamano· Jan 7, 2021, 19:57 UTC · re: Eric Sunshine · lore

Re: [PATCH v2 14/14] update-index: remove static globals from callbacks

Eric Sunshine <sunshine@sunshineco.com> writes:
Show 12 quoted lines
>> It could be possible to replace all references to 'istate' with
>> 'repo->index' but the patches get slightly more messy. I also
>> think the code looks messier, but you do make a good point that
>> there is no concrete reason to separate the two.
>
> I agree that it would make the code a bit noisier (to read) if
> `istate` is eliminated from the callback structure, however, even
> though I didn't originally feel strongly one way or the other about
> having both `repo` and `istate` in the structure, I'm now leaning more
> toward seeing `istate` eliminated. My one (big) concern with `istate`
> is that it confuses readers into wondering whether `istate` and
> `repo->istate` will ever be different.

Some applications may want to work on more than one in-core index at the same time (perhaps a merge strategy may want to keep a copy of the original index and update a second copy with the result of the merge), and it may be useful for such applications if 'repo->istate' does not have to be the in-core index instance to be worked on. So things that go in libgit.a may want to allow such distinction.

But what goes in builtin/ is a different story. As long as this application has no need for such a feature and will always work on the primary in-core index, prepared for the in-core repository structure for convenience, it may not worth it to support such a feature that no callers benefit from.

Thanks.
Derrick Stolee· Jan 8, 2021, 01:52 UTC · re: Junio C Hamano · lore

Re: [PATCH v2 14/14] update-index: remove static globals from callbacks

On 1/7/2021 2:57 PM, Junio C Hamano wrote:
Show 27 quoted lines
> Eric Sunshine <sunshine@sunshineco.com> writes:
> 
>>> It could be possible to replace all references to 'istate' with
>>> 'repo->index' but the patches get slightly more messy. I also
>>> think the code looks messier, but you do make a good point that
>>> there is no concrete reason to separate the two.
>>
>> I agree that it would make the code a bit noisier (to read) if
>> `istate` is eliminated from the callback structure, however, even
>> though I didn't originally feel strongly one way or the other about
>> having both `repo` and `istate` in the structure, I'm now leaning more
>> toward seeing `istate` eliminated. My one (big) concern with `istate`
>> is that it confuses readers into wondering whether `istate` and
>> `repo->istate` will ever be different.
> 
> Some applications may want to work on more than one in-core index at
> the same time (perhaps a merge strategy may want to keep a copy of
> the original index and update a second copy with the result of the
> merge), and it may be useful for such applications if 'repo->istate'
> does not have to be the in-core index instance to be worked on.  So
> things that go in libgit.a may want to allow such distinction.
> 
> But what goes in builtin/ is a different story.  As long as this
> application has no need for such a feature and will always work on
> the primary in-core index, prepared for the in-core repository
> structure for convenience, it may not worth it to support such a
> feature that no callers benefit from.
I'll try to restructure my patches to do the following order:
1. replace compatibility macros with static global references, except
   i. use 'istate' in the methods that don't need a repository.
  ii. use 'repo->index' in the methods that need a repository.
2. replace static globals with method parameters.
   i. drop 'istate' static global with method parameters. Methods that
      have a repo will pass 'repo->index' to these methods.
  ii. drop 'repo' static global with method parameters.
3. replace static globals in callback methods using 'repo->index',
   where 'repo' is a member of the callback_data struct.

That should keep the structure as presented in v2 while also avoiding this question of "can istate differ from repo->index?"

Thanks, -Stolee

Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 10/14] update-index: replace several compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

This is also the last usage of unmerge_cache_entry_at(), so it can be removed from cache.h.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 11 ++++++-----
 cache.h                |  1 -
 2 files changed, 6 insertions(+), 6 deletions(-)
Show changes to 2 files +6 −6

builtin/update-index.c, cache.h

diff --git a/builtin/update-index.c b/builtin/update-index.c
index ffa42813370..dd8e0acc52e 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -430,7 +430,7 @@ static void chmod_path(char flip, const char *path)
 	if (pos < 0)
 		goto fail;
 	ce = istate->cache[pos];
-	if (chmod_cache_entry(ce, flip) < 0)
+	if (chmod_index_entry(istate, ce, flip) < 0)
 		goto fail;
 
 	report("chmod %cx '%s'", flip, path);
@@ -626,7 +626,7 @@ static int unresolve_one(const char *path)
 	pos = index_name_pos(istate, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
-		pos = unmerge_cache_entry_at(pos);
+		pos = unmerge_index_entry_at(istate, pos);
 		if (pos < istate->cache_nr) {
 			const struct cache_entry *ce = istate->cache[pos];
 			if (ce_stage(ce) &&
@@ -777,8 +777,9 @@ 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(repo);
+	*o->has_errors |= refresh_index(istate, o->flags | flag,
+					NULL, NULL, NULL);
 	return 0;
 }
 
@@ -814,7 +815,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(istate);
 	return 0;
 }
 
diff --git a/cache.h b/cache.h
index fdf061cac56..8c091be6256 100644
--- a/cache.h
+++ b/cache.h
@@ -421,7 +421,6 @@ extern struct index_state the_index;
 #define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #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))
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 08/14] update-index: use remove_file_from_index()

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of remove_file_from_cache().
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 096cbdfa8ac..aa859c4e018 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -252,7 +252,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(istate, path))
 		return error("%s: cannot remove from the index", path);
 	return 0;
 }
@@ -386,7 +386,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(istate, path))
 			return error("%s: cannot remove from the index", path);
 		return 0;
 	}
@@ -473,7 +473,7 @@ static void update_one(const char *path)
 	}
 
 	if (force_remove) {
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(istate, path))
 			die("git update-index: unable to remove %s", path);
 		report("remove '%s'", path);
 		return;
@@ -556,7 +556,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(istate, path_name))
 				die("git update-index: unable to remove %s",
 				    ptr);
 		}
@@ -671,7 +671,7 @@ static int unresolve_one(const char *path)
 		goto free_return;
 	}
 
-	remove_file_from_cache(path);
+	remove_file_from_index(istate, path);
 	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
 		error("%s: cannot add our version to the index.", path);
 		ret = -1;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:42 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 06/14] update-index: use istate->cache_changed

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of active_cache_changed.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 106bfdab78e..346d798c60b 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -242,7 +242,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 			istate->cache[pos]->ce_flags &= ~flag;
 		istate->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(istate, path);
-		active_cache_changed |= CE_ENTRY_CHANGED;
+		istate->cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
 	return -1;
@@ -915,7 +915,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;
+		istate->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -936,7 +936,7 @@ static enum parse_opt_result reupdate_callback(
 	setup_work_tree();
 	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
 	if (*has_errors)
-		active_cache_changed = 0;
+		istate->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -1135,7 +1135,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
 		if (istate->version != preferred_index_format)
-			active_cache_changed |= SOMETHING_CHANGED;
+			istate->cache_changed |= SOMETHING_CHANGED;
 		istate->version = preferred_index_format;
 	}
 
@@ -1223,7 +1223,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		report(_("fsmonitor disabled"));
 	}
 
-	if (active_cache_changed || force_write) {
+	if (istate->cache_changed || force_write) {
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:43 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 13/14] update-index: reduce static globals, part 2

From: Derrick Stolee <dstolee@microsoft.com>

In order to remove index compatibility macros cleanly, we relied upon static globals 'repo' and 'istate' to be pointers to the_repository and the_index, respectively. We can continue reducing the need for these static globals by modifying method prototypes to use them when necessary.

Move these static globals further down the file so we can identify which methods need both 'struct repository *repo' and 'struct index_state *istate' parameters. The only changes included here adjust method prototypes and their call locations.

The only remaining change is to remove the static globals entirely, but that requires updating the parse-opt callbacks, which need a different solution.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)
Show changes to builtin/update-index.c +22 −14
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 6b585fb8ede..fb8d7879783 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -594,10 +594,9 @@ static const char * const update_index_usage[] = {
 static struct object_id head_oid;
 static struct object_id merge_head_oid;
 
-static struct repository *repo;
-static struct index_state *istate;
-
-static struct cache_entry *read_one_ent(const char *which,
+static struct cache_entry *read_one_ent(struct repository *repo,
+					struct index_state *istate,
+					const char *which,
 					struct object_id *ent, const char *path,
 					int namelen, int stage)
 {
@@ -625,7 +624,9 @@ static struct cache_entry *read_one_ent(const char *which,
 	return ce;
 }
 
-static int unresolve_one(const char *path)
+static int unresolve_one(struct repository *repo,
+			 struct index_state *istate,
+			 const char *path)
 {
 	int namelen = strlen(path);
 	int pos;
@@ -667,8 +668,8 @@ static int unresolve_one(const char *path)
 	 * stuff HEAD version in stage #2,
 	 * stuff MERGE_HEAD version in stage #3.
 	 */
-	ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
-	ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
+	ce_2 = read_one_ent(repo, istate, "our", &head_oid, path, namelen, 2);
+	ce_3 = read_one_ent(repo, istate, "their", &merge_head_oid, path, namelen, 3);
 
 	if (!ce_2 || !ce_3) {
 		ret = -1;
@@ -707,7 +708,9 @@ static void read_head_pointers(void)
 	}
 }
 
-static int do_unresolve(int ac, const char **av,
+static int do_unresolve(struct repository *repo,
+			struct index_state *istate,
+			int ac, const char **av,
 			const char *prefix, int prefix_length)
 {
 	int i;
@@ -721,13 +724,15 @@ static int do_unresolve(int ac, const char **av,
 	for (i = 1; i < ac; i++) {
 		const char *arg = av[i];
 		char *p = prefix_path(prefix, prefix_length, arg);
-		err |= unresolve_one(p);
+		err |= unresolve_one(repo, istate, p);
 		free(p);
 	}
 	return err;
 }
 
-static int do_reupdate(int ac, const char **av,
+static int do_reupdate(struct repository *repo,
+		       struct index_state *istate,
+		       int ac, const char **av,
 		       const char *prefix)
 {
 	/* Read HEAD and run update-index on paths that are
@@ -756,7 +761,7 @@ static int do_reupdate(int ac, const char **av,
 		if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
 			continue;
 		if (has_head)
-			old = read_one_ent(NULL, &head_oid,
+			old = read_one_ent(repo, istate, NULL, &head_oid,
 					   ce->name, ce_namelen(ce), 0);
 		if (old && ce->ce_mode == old->ce_mode &&
 		    oideq(&ce->oid, &old->oid)) {
@@ -784,6 +789,9 @@ struct refresh_params {
 	int *has_errors;
 };
 
+static struct repository *repo;
+static struct index_state *istate;
+
 static int refresh(struct refresh_params *o, unsigned int flag)
 {
 	setup_work_tree();
@@ -923,8 +931,8 @@ static enum parse_opt_result unresolve_callback(
 	BUG_ON_OPT_ARG(arg);
 
 	/* consume remaining arguments. */
-	*has_errors = do_unresolve(ctx->argc, ctx->argv,
-				prefix, prefix ? strlen(prefix) : 0);
+	*has_errors = do_unresolve(repo, istate, ctx->argc, ctx->argv,
+				   prefix, prefix ? strlen(prefix) : 0);
 	if (*has_errors)
 		istate->cache_changed = 0;
 
@@ -945,7 +953,7 @@ static enum parse_opt_result reupdate_callback(
 
 	/* consume remaining arguments. */
 	setup_work_tree();
-	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
+	*has_errors = do_reupdate(repo, istate, ctx->argc, ctx->argv, prefix);
 	if (*has_errors)
 		istate->cache_changed = 0;
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 5, 2021, 04:43 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v2 11/14] update-index: remove ce_match_stat(), all macros

From: Derrick Stolee <dstolee@microsoft.com>

The final index compatibility macro to remove from the update-index builtin is ce_match_stat(). Further, this is the last use of that macro anywhere, so it should be removed.

There are some remaining references in the racy-git.txt technical document that are updated to ie_match_stat().

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Documentation/technical/racy-git.txt | 6 +++---
 builtin/update-index.c               | 3 +--
 cache.h                              | 1 -
 3 files changed, 4 insertions(+), 6 deletions(-)
Show changes to 3 files +4 −6

Documentation/technical/racy-git.txt, builtin/update-index.c, cache.h

diff --git a/Documentation/technical/racy-git.txt b/Documentation/technical/racy-git.txt
index ceda4bbfda4..65188e04559 100644
--- a/Documentation/technical/racy-git.txt
+++ b/Documentation/technical/racy-git.txt
@@ -26,7 +26,7 @@ information obtained from the filesystem via `lstat(2)` system
 call when they were last updated.  When checking if they differ,
 Git first runs `lstat(2)` on the files and compares the result
 with this information (this is what was originally done by the
-`ce_match_stat()` function, but the current code does it in
+`ie_match_stat()` function, but the current code does it in
 `ce_match_stat_basic()` function).  If some of these "cached
 stat information" fields do not match, Git can tell that the
 files are modified without even looking at their contents.
@@ -102,7 +102,7 @@ timestamp as the index file itself.
 
 The callers that want to check if an index entry matches the
 corresponding file in the working tree continue to call
-`ce_match_stat()`, but with this change, `ce_match_stat()` uses
+`ie_match_stat()`, but with this change, `ie_match_stat()` uses
 `ce_modified_check_fs()` to see if racily clean ones are
 actually clean after comparing the cached stat information using
 `ce_match_stat_basic()`.
@@ -128,7 +128,7 @@ Runtime penalty
 ---------------
 
 The runtime penalty of falling back to `ce_modified_check_fs()`
-from `ce_match_stat()` can be very expensive when there are many
+from `ie_match_stat()` can be very expensive when there are many
 racily clean entries.  An obvious way to artificially create
 this situation is to give the same timestamp to all the files in
 the working tree in a large project, run `git update-index` on
diff --git a/builtin/update-index.c b/builtin/update-index.c
index dd8e0acc52e..8fc680090be 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "lockfile.h"
@@ -277,7 +276,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(istate, old, st, 0))
 		return 0;
 
 	ce = make_empty_cache_entry(istate, len);
diff --git a/cache.h b/cache.h
index 8c091be6256..740bd0aa1dd 100644
--- a/cache.h
+++ b/cache.h
@@ -416,7 +416,6 @@ extern struct index_state the_index;
 #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_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 00/14] Remove more index compatibility macros

UPDATE: this is now based on ag/merge-strategies-in-c to avoid conflicts in
'seen'. The changes in builtin/rm.c still conflict with
mt/rm-sparse-checkout, but that branch seems to be waiting for a clearer
plan on some corner cases. I thought about ejecting it, but 'rm' still uses
ce_match_stat(), so just dropping the patch gives less of a final stake at
the end of the series. (I'm still open to it, if necessary.)

I noticed that Duy's project around USE_THE_INDEX_COMPATIBILITY_MACROS has been on pause for a while. Here is my attempt to continue that project a little.

I started going through the builtins that still use cache_name_pos() and the first was easy: mv and rm.

Then I hit update-index and it was a bit bigger.

My strategy for update-index was to create static globals "repo" and "istate" that point to the_repository and the_index, respectively. Then, I was able to remove macros one-by-one without changing method prototypes within the file. Then, these static globals were also removed by systematically updating the local method prototypes, plus some fancy structure stuff for the option parsing callbacks.

I had started trying to keep everything local to the method signatures, but I hit a snag when reaching the command-line parsing callbacks, which I could not modify their call signature. At that point, I had something that was already much more complicated than what I present now. Outside of the first update-index commit, everything was a mechanical find/replace.

In total, this allows us to remove four of the compatibility macros because they are no longer used.

Updates in V3 =============

 * Methods that know about the 'repo' pointer no longer also have an
   'istate' pointer and instead prefer 'repo->index'
 * This includes the callback_data struct which only has a 'repo' member, no
   'istate'.
Thanks, -Stolee
Derrick Stolee (14):
  mv: remove index compatibility macros
  rm: remove compatilibity macros
  update-index: drop the_index, the_repository
  update-index: use istate->cache over active_cache
  update-index: use istate->cache_nr over active_nr
  update-index: use istate->cache_changed
  update-index: use index_name_pos() over cache_name_pos()
  update-index: use remove_file_from_index()
  update-index: use add_index_entry()
  update-index: replace several compatibility macros
  update-index: remove ce_match_stat(), all macros
  update-index: reduce static globals, part 1
  update-index: reduce static globals, part 2
  update-index: remove static globals from callbacks
 Documentation/technical/racy-git.txt |   6 +-
 builtin/mv.c                         |  42 ++--
 builtin/rm.c                         |  56 ++---
 builtin/update-index.c               | 312 +++++++++++++++------------
 cache.h                              |   4 -
 5 files changed, 225 insertions(+), 195 deletions(-)
base-commit: 3da8920d38a007157ccf8e53382e5206b909dafe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-830%2Fderrickstolee%2Findex-compatibility-1-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-830/derrickstolee/index-compatibility-1-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/830
Range-diff vs v2:
  1:  5ccc464cf26 =  1:  5ccc464cf26 mv: remove index compatibility macros
  2:  e715c703cb8 =  2:  e715c703cb8 rm: remove compatilibity macros
  3:  4bf3c582f9d !  3:  f60f34ac1f5 update-index: drop the_index, the_repository
     @@ Commit message
          that expand to use the_index can then be mechanically replaced by
          references to the istate pointer.
      
     +    We will be careful to use "repo->index" over "istate" whenever repo is
     +    needed by a method.
     +
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
       ## builtin/update-index.c ##
     -@@ builtin/update-index.c: static int ignore_skip_worktree_entries;
     - #define UNMARK_FLAG 2
     - static struct strbuf mtime_dir = STRBUF_INIT;
     +@@ builtin/update-index.c: static int test_if_untracked_cache_is_supported(void)
     + 	return ret;
     + }
       
     -+static struct repository *repo;
      +static struct index_state *istate;
      +
     - /* Untracked cache mode */
     - enum uc_mode {
     - 	UC_UNSPECIFIED = -1,
     -@@ builtin/update-index.c: static int mark_ce_flags(const char *path, int flag, int mark)
     + 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) {
     @@ builtin/update-index.c: static int add_cacheinfo(unsigned int mode, const struct
       				     allow_add, allow_replace, NULL);
       	if (res == -1)
       		return res;
     +@@ builtin/update-index.c: static const char * const update_index_usage[] = {
     + 
     + static struct object_id head_oid;
     + static struct object_id merge_head_oid;
     ++static struct repository *repo;
     + 
     + static struct cache_entry *read_one_ent(const char *which,
     + 					struct object_id *ent, const char *path,
      @@ builtin/update-index.c: static struct cache_entry *read_one_ent(const char *which,
       	struct object_id oid;
       	struct cache_entry *ce;
     @@ builtin/update-index.c: static struct cache_entry *read_one_ent(const char *whic
       		return NULL;
       	}
      -	ce = make_empty_cache_entry(&the_index, namelen);
     -+	ce = make_empty_cache_entry(istate, namelen);
     ++	ce = make_empty_cache_entry(repo->index, namelen);
       
       	oidcpy(&ce->oid, &oid);
       	memcpy(ce->name, path, namelen);
     @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
       		char *path;
       
      -		if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
     -+		if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
     ++		if (ce_stage(ce) || !ce_path_match(repo->index, ce, &pathspec, NULL))
       			continue;
       		if (has_head)
       			old = read_one_ent(NULL, &head_oid,
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       
      -	the_index.updated_skipworktree = 1;
      +	istate = repo->index;
     -+	istate->updated_skipworktree = 1;
     ++	repo->index->updated_skipworktree = 1;
       
       	/*
       	 * Custom copy of parse_options() because we want to handle
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
       
      -		if (the_index.version != preferred_index_format)
     -+		if (istate->version != preferred_index_format)
     ++		if (repo->index->version != preferred_index_format)
       			active_cache_changed |= SOMETHING_CHANGED;
      -		the_index.version = preferred_index_format;
     -+		istate->version = preferred_index_format;
     ++		repo->index->version = preferred_index_format;
       	}
       
       	if (read_from_stdin) {
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       				  "enable split index"));
      -		if (the_index.split_index)
      -			the_index.cache_changed |= SPLIT_INDEX_ORDERED;
     -+		if (istate->split_index)
     -+			istate->cache_changed |= SPLIT_INDEX_ORDERED;
     ++		if (repo->index->split_index)
     ++			repo->index->cache_changed |= SPLIT_INDEX_ORDERED;
       		else
      -			add_split_index(&the_index);
     -+			add_split_index(istate);
     ++			add_split_index(repo->index);
       	} else if (!split_index) {
       		if (git_config_get_split_index() == 1)
       			warning(_("core.splitIndex is set to true; "
       				  "remove or change it, if you really want to "
       				  "disable split index"));
      -		remove_split_index(&the_index);
     -+		remove_split_index(istate);
     ++		remove_split_index(repo->index);
       	}
       
      -	prepare_repo_settings(r);
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       				  "remove or change it, if you really want to "
       				  "disable the untracked cache"));
      -		remove_untracked_cache(&the_index);
     -+		remove_untracked_cache(istate);
     ++		remove_untracked_cache(repo->index);
       		report(_("Untracked cache disabled"));
       		break;
       	case UC_TEST:
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       				  "remove or change it, if you really want to "
       				  "enable the untracked cache"));
      -		add_untracked_cache(&the_index);
     -+		add_untracked_cache(istate);
     ++		add_untracked_cache(repo->index);
       		report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
       		break;
       	default:
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       				"set it if you really want to "
       				"enable fsmonitor"));
      -		add_fsmonitor(&the_index);
     -+		add_fsmonitor(istate);
     ++		add_fsmonitor(repo->index);
       		report(_("fsmonitor enabled"));
       	} else if (!fsmonitor) {
       		if (git_config_get_fsmonitor() == 1)
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       				"remove it if you really want to "
       				"disable fsmonitor"));
      -		remove_fsmonitor(&the_index);
     -+		remove_fsmonitor(istate);
     ++		remove_fsmonitor(repo->index);
       		report(_("fsmonitor disabled"));
       	}
       
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       			unable_to_lock_die(get_index_file(), lock_error);
       		}
      -		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
     -+		if (write_locked_index(istate, &lock_file, COMMIT_LOCK))
     ++		if (write_locked_index(repo->index, &lock_file, COMMIT_LOCK))
       			die("Unable to write new index file");
       	}
       
  4:  4b509ba5fa2 !  4:  b8fcdd8b3da update-index: use istate->cache over active_cache
     @@ Metadata
       ## Commit message ##
          update-index: use istate->cache over active_cache
      
     +    Also use repo->index over istate, when possible.
     +
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
       ## builtin/update-index.c ##
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       		pos = unmerge_cache_entry_at(pos);
       		if (pos < active_nr) {
      -			const struct cache_entry *ce = active_cache[pos];
     -+			const struct cache_entry *ce = istate->cache[pos];
     ++			const struct cache_entry *ce = repo->index->cache[pos];
       			if (ce_stage(ce) &&
       			    ce_namelen(ce) == namelen &&
       			    !memcmp(ce->name, path, namelen))
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       		pos = -pos-1;
       		if (pos < active_nr) {
      -			const struct cache_entry *ce = active_cache[pos];
     -+			const struct cache_entry *ce = istate->cache[pos];
     ++			const struct cache_entry *ce = repo->index->cache[pos];
       			if (ce_namelen(ce) == namelen &&
       			    !memcmp(ce->name, path, namelen)) {
       				fprintf(stderr,
     @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
        redo:
       	for (pos = 0; pos < active_nr; pos++) {
      -		const struct cache_entry *ce = active_cache[pos];
     -+		const struct cache_entry *ce = istate->cache[pos];
     ++		const struct cache_entry *ce = repo->index->cache[pos];
       		struct cache_entry *old = NULL;
       		int save_nr;
       		char *path;
  5:  6c0e019f91c !  5:  586371ee769 update-index: use index->cache_nr over active_nr
     @@ Metadata
      Author: Derrick Stolee <dstolee@microsoft.com>
      
       ## Commit message ##
     -    update-index: use index->cache_nr over active_nr
     +    update-index: use istate->cache_nr over active_nr
     +
     +    Also use "repo->index" over "istate" when possible.
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       		/* already merged */
       		pos = unmerge_cache_entry_at(pos);
      -		if (pos < active_nr) {
     -+		if (pos < istate->cache_nr) {
     - 			const struct cache_entry *ce = istate->cache[pos];
     ++		if (pos < repo->index->cache_nr) {
     + 			const struct cache_entry *ce = repo->index->cache[pos];
       			if (ce_stage(ce) &&
       			    ce_namelen(ce) == namelen &&
      @@ builtin/update-index.c: static int unresolve_one(const char *path)
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       		 */
       		pos = -pos-1;
      -		if (pos < active_nr) {
     -+		if (pos < istate->cache_nr) {
     - 			const struct cache_entry *ce = istate->cache[pos];
     ++		if (pos < repo->index->cache_nr) {
     + 			const struct cache_entry *ce = repo->index->cache[pos];
       			if (ce_namelen(ce) == namelen &&
       			    !memcmp(ce->name, path, namelen)) {
      @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
     @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
       		has_head = 0;
        redo:
      -	for (pos = 0; pos < active_nr; pos++) {
     -+	for (pos = 0; pos < istate->cache_nr; pos++) {
     - 		const struct cache_entry *ce = istate->cache[pos];
     ++	for (pos = 0; pos < repo->index->cache_nr; pos++) {
     + 		const struct cache_entry *ce = repo->index->cache[pos];
       		struct cache_entry *old = NULL;
       		int save_nr;
      @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
     @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
       		/* Be careful.  The working tree may not have the
       		 * path anymore, in which case, under 'allow_remove',
      -		 * or worse yet 'allow_replace', active_nr may decrease.
     -+		 * or worse yet 'allow_replace', istate->cache_nr may decrease.
     ++		 * or worse yet 'allow_replace', repo->index->cache_nr may decrease.
       		 */
      -		save_nr = active_nr;
     -+		save_nr = istate->cache_nr;
     ++		save_nr = repo->index->cache_nr;
       		path = xstrdup(ce->name);
       		update_one(path);
       		free(path);
       		discard_cache_entry(old);
      -		if (save_nr != active_nr)
     -+		if (save_nr != istate->cache_nr)
     ++		if (save_nr != repo->index->cache_nr)
       			goto redo;
       	}
       	clear_pathspec(&pathspec);
  6:  5091e2661d1 !  6:  f450f43cd0d update-index: use istate->cache_changed
     @@ builtin/update-index.c: static enum parse_opt_result unresolve_callback(
       				prefix, prefix ? strlen(prefix) : 0);
       	if (*has_errors)
      -		active_cache_changed = 0;
     -+		istate->cache_changed = 0;
     ++		repo->index->cache_changed = 0;
       
       	ctx->argv += ctx->argc - 1;
       	ctx->argc = 1;
     @@ builtin/update-index.c: static enum parse_opt_result reupdate_callback(
       	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
       	if (*has_errors)
      -		active_cache_changed = 0;
     -+		istate->cache_changed = 0;
     ++		repo->index->cache_changed = 0;
       
       	ctx->argv += ctx->argc - 1;
       	ctx->argc = 1;
      @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
       			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
       
     - 		if (istate->version != preferred_index_format)
     + 		if (repo->index->version != preferred_index_format)
      -			active_cache_changed |= SOMETHING_CHANGED;
     -+			istate->cache_changed |= SOMETHING_CHANGED;
     - 		istate->version = preferred_index_format;
     ++			repo->index->cache_changed |= SOMETHING_CHANGED;
     + 		repo->index->version = preferred_index_format;
       	}
       
      @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       	}
       
      -	if (active_cache_changed || force_write) {
     -+	if (istate->cache_changed || force_write) {
     ++	if (repo->index->cache_changed || force_write) {
       		if (newfd < 0) {
       			if (refresh_args.flags & REFRESH_QUIET)
       				exit(128);
  7:  5b14fa10a4b !  7:  fc640ae6e65 update-index: use index_name_pos() over cache_name_pos()
     @@ Commit message
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
       ## builtin/update-index.c ##
     -@@ builtin/update-index.c: static int test_if_untracked_cache_is_supported(void)
     +@@ builtin/update-index.c: static struct index_state *istate;
       static int mark_ce_flags(const char *path, int flag, int mark)
       {
       	int namelen = strlen(path);
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       
       	/* See if there is such entry in the index. */
      -	pos = cache_name_pos(path, namelen);
     -+	pos = index_name_pos(istate, path, namelen);
     ++	pos = index_name_pos(repo->index, path, namelen);
       	if (0 <= pos) {
       		/* already merged */
       		pos = unmerge_cache_entry_at(pos);
  8:  a1a9fb01b07 !  8:  2eead833fba update-index: use remove_file_from_index()
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       	}
       
      -	remove_file_from_cache(path);
     -+	remove_file_from_index(istate, path);
     ++	remove_file_from_index(repo->index, path);
       	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
       		error("%s: cannot add our version to the index.", path);
       		ret = -1;
  9:  620e300ad6b !  9:  8016b089556 update-index: use add_index_entry()
     @@ builtin/update-index.c: static int add_one_path(const struct cache_entry *old, c
      @@ builtin/update-index.c: static int unresolve_one(const char *path)
       	}
       
     - 	remove_file_from_index(istate, path);
     + 	remove_file_from_index(repo->index, path);
      -	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
     -+	if (add_index_entry(istate, ce_2, ADD_CACHE_OK_TO_ADD)) {
     ++	if (add_index_entry(repo->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(istate, ce_3, ADD_CACHE_OK_TO_ADD))
     ++	if (!add_index_entry(repo->index, ce_3, ADD_CACHE_OK_TO_ADD))
       		return 0;
       	error("%s: cannot add their version to the index.", path);
       	ret = -1;
 10:  68b26a11d77 ! 10:  efe9fddd0a9 update-index: replace several compatibility macros
     @@ builtin/update-index.c: static void chmod_path(char flip, const char *path)
       
       	report("chmod %cx '%s'", flip, path);
      @@ builtin/update-index.c: static int unresolve_one(const char *path)
     - 	pos = index_name_pos(istate, path, namelen);
     + 	pos = index_name_pos(repo->index, path, namelen);
       	if (0 <= pos) {
       		/* already merged */
      -		pos = unmerge_cache_entry_at(pos);
     -+		pos = unmerge_index_entry_at(istate, pos);
     - 		if (pos < istate->cache_nr) {
     - 			const struct cache_entry *ce = istate->cache[pos];
     ++		pos = unmerge_index_entry_at(repo->index, pos);
     + 		if (pos < repo->index->cache_nr) {
     + 			const struct cache_entry *ce = repo->index->cache[pos];
       			if (ce_stage(ce) &&
      @@ builtin/update-index.c: struct refresh_params {
       static int refresh(struct refresh_params *o, unsigned int flag)
     @@ builtin/update-index.c: struct refresh_params {
      -	read_cache();
      -	*o->has_errors |= refresh_cache(o->flags | flag);
      +	repo_read_index(repo);
     -+	*o->has_errors |= refresh_index(istate, o->flags | flag,
     ++	*o->has_errors |= refresh_index(repo->index, o->flags | flag,
      +					NULL, NULL, NULL);
       	return 0;
       }
     @@ builtin/update-index.c: static int resolve_undo_clear_callback(const struct opti
       	BUG_ON_OPT_NEG(unset);
       	BUG_ON_OPT_ARG(arg);
      -	resolve_undo_clear();
     -+	resolve_undo_clear_index(istate);
     ++	resolve_undo_clear_index(repo->index);
       	return 0;
       }
       
 11:  f1cffe2b455 = 11:  e9d4fa613a6 update-index: remove ce_match_stat(), all macros
 12:  79e267f39ec ! 12:  4754a9214ee update-index: reduce static globals, part 1
     @@ Commit message
          static globals by modifying method prototypes to use them when
          necessary.
      
     -    Move these static globals further down in the file so we can identify
     -    which method only need to add a 'struct index_state *istate' parameter.
     -    The only changes included here adjust method prototypes and their call
     -    locations.
     +    Remove the 'istate' static global in favor of method parameters. This
     +    adjusts some callers, which either use their own 'istate' parameter or
     +    'repo->index'.
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
      
       ## builtin/update-index.c ##
     -@@ builtin/update-index.c: static int ignore_skip_worktree_entries;
     - #define UNMARK_FLAG 2
     - static struct strbuf mtime_dir = STRBUF_INIT;
     - 
     --static struct repository *repo;
     --static struct index_state *istate;
     --
     - /* Untracked cache mode */
     - enum uc_mode {
     - 	UC_UNSPECIFIED = -1,
      @@ builtin/update-index.c: static int test_if_untracked_cache_is_supported(void)
       	return ret;
       }
       
     +-static struct index_state *istate;
     +-
      -static int mark_ce_flags(const char *path, int flag, int mark)
      +static int mark_ce_flags(struct index_state *istate,
      +			 const char *path, int flag, int mark)
     @@ builtin/update-index.c: static void read_index_info(int nul_term_line)
       				die("git update-index: unable to update %s",
       				    path_name);
       		}
     -@@ builtin/update-index.c: static const char * const update_index_usage[] = {
     - static struct object_id head_oid;
     - static struct object_id merge_head_oid;
     - 
     -+static struct repository *repo;
     -+static struct index_state *istate;
     -+
     - static struct cache_entry *read_one_ent(const char *which,
     - 					struct object_id *ent, const char *path,
     - 					int namelen, int stage)
      @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
       		 */
     - 		save_nr = istate->cache_nr;
     + 		save_nr = repo->index->cache_nr;
       		path = xstrdup(ce->name);
      -		update_one(path);
     -+		update_one(istate, path);
     ++		update_one(repo->index, path);
       		free(path);
       		discard_cache_entry(old);
     - 		if (save_nr != istate->cache_nr)
     + 		if (save_nr != repo->index->cache_nr)
      @@ builtin/update-index.c: static enum parse_opt_result cacheinfo_callback(
       	BUG_ON_OPT_ARG(arg);
       
       	if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
      -		if (add_cacheinfo(mode, &oid, path, 0))
     -+		if (add_cacheinfo(istate, mode, &oid, path, 0))
     ++		if (add_cacheinfo(repo->index, mode, &oid, path, 0))
       			die("git update-index: --cacheinfo cannot add %s", path);
       		ctx->argv++;
       		ctx->argc--;
     @@ builtin/update-index.c: static enum parse_opt_result cacheinfo_callback(
       	if (strtoul_ui(*++ctx->argv, 8, &mode) ||
       	    get_oid_hex(*++ctx->argv, &oid) ||
      -	    add_cacheinfo(mode, &oid, *++ctx->argv, 0))
     -+	    add_cacheinfo(istate, mode, &oid, *++ctx->argv, 0))
     ++	    add_cacheinfo(repo->index, mode, &oid, *++ctx->argv, 0))
       		die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
       	ctx->argc -= 3;
       	return 0;
     @@ builtin/update-index.c: static enum parse_opt_result stdin_cacheinfo_callback(
       		return error("option '%s' must be the last argument", opt->long_name);
       	allow_add = allow_replace = allow_remove = 1;
      -	read_index_info(*nul_term_line);
     -+	read_index_info(istate, *nul_term_line);
     ++	read_index_info(repo->index, *nul_term_line);
       	return 0;
       }
       
     +@@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
     + 	if (entries < 0)
     + 		die("cache corrupted");
     + 
     +-	istate = repo->index;
     + 	repo->index->updated_skipworktree = 1;
     + 
     + 	/*
      @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
       
       			setup_work_tree();
       			p = prefix_path(prefix, prefix_length, path);
      -			update_one(p);
     -+			update_one(istate, p);
     ++			update_one(repo->index, p);
       			if (set_executable_bit)
      -				chmod_path(set_executable_bit, p);
     -+				chmod_path(istate, set_executable_bit, p);
     ++				chmod_path(repo->index, set_executable_bit, p);
       			free(p);
       			ctx.argc--;
       			ctx.argv++;
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       			}
       			p = prefix_path(prefix, prefix_length, buf.buf);
      -			update_one(p);
     -+			update_one(istate, p);
     ++			update_one(repo->index, p);
       			if (set_executable_bit)
      -				chmod_path(set_executable_bit, p);
     -+				chmod_path(istate, set_executable_bit, p);
     ++				chmod_path(repo->index, set_executable_bit, p);
       			free(p);
       		}
       		strbuf_release(&unquoted);
 13:  457402b4fdc ! 13:  a9185af4740 update-index: reduce static globals, part 2
     @@ Commit message
          static globals by modifying method prototypes to use them when
          necessary.
      
     -    Move these static globals further down the file so we can identify which
     -    methods need both 'struct repository *repo' and 'struct index_state
     -    *istate' parameters. The only changes included here adjust method
     -    prototypes and their call locations.
     +    Move the remaining 'struct repository *repo' further down the file and
     +    use method parameters to pass it around instead.
      
     -    The only remaining change is to remove the static globals entirely, but
     +    The only remaining change is to remove the static global entirely, but
          that requires updating the parse-opt callbacks, which need a different
          solution.
      
     @@ Commit message
      
       ## builtin/update-index.c ##
      @@ builtin/update-index.c: static const char * const update_index_usage[] = {
     + 
       static struct object_id head_oid;
       static struct object_id merge_head_oid;
     - 
      -static struct repository *repo;
     --static struct index_state *istate;
     --
     + 
      -static struct cache_entry *read_one_ent(const char *which,
      +static struct cache_entry *read_one_ent(struct repository *repo,
     -+					struct index_state *istate,
      +					const char *which,
       					struct object_id *ent, const char *path,
       					int namelen, int stage)
     @@ builtin/update-index.c: static struct cache_entry *read_one_ent(const char *whic
       
      -static int unresolve_one(const char *path)
      +static int unresolve_one(struct repository *repo,
     -+			 struct index_state *istate,
      +			 const char *path)
       {
       	int namelen = strlen(path);
     @@ builtin/update-index.c: static int unresolve_one(const char *path)
       	 */
      -	ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
      -	ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
     -+	ce_2 = read_one_ent(repo, istate, "our", &head_oid, path, namelen, 2);
     -+	ce_3 = read_one_ent(repo, istate, "their", &merge_head_oid, path, namelen, 3);
     ++	ce_2 = read_one_ent(repo, "our", &head_oid, path, namelen, 2);
     ++	ce_3 = read_one_ent(repo, "their", &merge_head_oid, path, namelen, 3);
       
       	if (!ce_2 || !ce_3) {
       		ret = -1;
     @@ builtin/update-index.c: static void read_head_pointers(void)
       
      -static int do_unresolve(int ac, const char **av,
      +static int do_unresolve(struct repository *repo,
     -+			struct index_state *istate,
      +			int ac, const char **av,
       			const char *prefix, int prefix_length)
       {
     @@ builtin/update-index.c: static int do_unresolve(int ac, const char **av,
       		const char *arg = av[i];
       		char *p = prefix_path(prefix, prefix_length, arg);
      -		err |= unresolve_one(p);
     -+		err |= unresolve_one(repo, istate, p);
     ++		err |= unresolve_one(repo, p);
       		free(p);
       	}
       	return err;
     @@ builtin/update-index.c: static int do_unresolve(int ac, const char **av,
       
      -static int do_reupdate(int ac, const char **av,
      +static int do_reupdate(struct repository *repo,
     -+		       struct index_state *istate,
      +		       int ac, const char **av,
       		       const char *prefix)
       {
       	/* Read HEAD and run update-index on paths that are
      @@ builtin/update-index.c: static int do_reupdate(int ac, const char **av,
     - 		if (ce_stage(ce) || !ce_path_match(istate, ce, &pathspec, NULL))
     + 		if (ce_stage(ce) || !ce_path_match(repo->index, ce, &pathspec, NULL))
       			continue;
       		if (has_head)
      -			old = read_one_ent(NULL, &head_oid,
     -+			old = read_one_ent(repo, istate, NULL, &head_oid,
     ++			old = read_one_ent(repo, NULL, &head_oid,
       					   ce->name, ce_namelen(ce), 0);
       		if (old && ce->ce_mode == old->ce_mode &&
       		    oideq(&ce->oid, &old->oid)) {
     @@ builtin/update-index.c: struct refresh_params {
       };
       
      +static struct repository *repo;
     -+static struct index_state *istate;
      +
       static int refresh(struct refresh_params *o, unsigned int flag)
       {
     @@ builtin/update-index.c: static enum parse_opt_result unresolve_callback(
       	/* consume remaining arguments. */
      -	*has_errors = do_unresolve(ctx->argc, ctx->argv,
      -				prefix, prefix ? strlen(prefix) : 0);
     -+	*has_errors = do_unresolve(repo, istate, ctx->argc, ctx->argv,
     ++	*has_errors = do_unresolve(repo, ctx->argc, ctx->argv,
      +				   prefix, prefix ? strlen(prefix) : 0);
       	if (*has_errors)
     - 		istate->cache_changed = 0;
     + 		repo->index->cache_changed = 0;
       
      @@ builtin/update-index.c: static enum parse_opt_result reupdate_callback(
       
       	/* consume remaining arguments. */
       	setup_work_tree();
      -	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
     -+	*has_errors = do_reupdate(repo, istate, ctx->argc, ctx->argv, prefix);
     ++	*has_errors = do_reupdate(repo, ctx->argc, ctx->argv, prefix);
       	if (*has_errors)
     - 		istate->cache_changed = 0;
     + 		repo->index->cache_changed = 0;
       
 14:  2b171a142b3 ! 14:  414ef816845 update-index: remove static globals from callbacks
     @@ Commit message
          callback value.
      
          Unify these callback methods to use a consistent 'struct callback_data'
     -    that contains 'repo' and 'istate', ready to use. This takes the place of
     +    that contains a 'repo' member, ready to use. This takes the place of
          the previous 'struct refresh_params' which served only to group the
          'flags' and 'has_errors' ints. We also collect other one-off settings,
          but only those that require access to the index or repository in their
     @@ builtin/update-index.c: static int do_reupdate(struct repository *repo,
      -struct refresh_params {
      +struct callback_data {
      +	struct repository *repo;
     -+	struct index_state *istate;
      +
       	unsigned int flags;
      -	int *has_errors;
     @@ builtin/update-index.c: static int do_reupdate(struct repository *repo,
       };
       
      -static struct repository *repo;
     --static struct index_state *istate;
      -
      -static int refresh(struct refresh_params *o, unsigned int flag)
      +static int refresh(struct callback_data *cd, unsigned int flag)
       {
       	setup_work_tree();
      -	repo_read_index(repo);
     --	*o->has_errors |= refresh_index(istate, o->flags | flag,
     +-	*o->has_errors |= refresh_index(repo->index, o->flags | flag,
      +	repo_read_index(cd->repo);
     -+	cd->has_errors |= refresh_index(cd->istate, cd->flags | flag,
     ++	cd->has_errors |= refresh_index(cd->repo->index, cd->flags | flag,
       					NULL, NULL, NULL);
       	return 0;
       }
     @@ builtin/update-index.c: static int chmod_callback(const struct option *opt,
      +	struct callback_data *cd = opt->value;
       	BUG_ON_OPT_NEG(unset);
       	BUG_ON_OPT_ARG(arg);
     --	resolve_undo_clear_index(istate);
     -+	resolve_undo_clear_index(cd->istate);
     +-	resolve_undo_clear_index(repo->index);
     ++	resolve_undo_clear_index(cd->repo->index);
       	return 0;
       }
       
     @@ builtin/update-index.c: static enum parse_opt_result cacheinfo_callback(
       	BUG_ON_OPT_ARG(arg);
       
       	if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
     --		if (add_cacheinfo(istate, mode, &oid, path, 0))
     -+		if (add_cacheinfo(cd->istate, mode, &oid, path, 0))
     +-		if (add_cacheinfo(repo->index, mode, &oid, path, 0))
     ++		if (add_cacheinfo(cd->repo->index, mode, &oid, path, 0))
       			die("git update-index: --cacheinfo cannot add %s", path);
       		ctx->argv++;
       		ctx->argc--;
     @@ builtin/update-index.c: static enum parse_opt_result cacheinfo_callback(
       		return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
       	if (strtoul_ui(*++ctx->argv, 8, &mode) ||
       	    get_oid_hex(*++ctx->argv, &oid) ||
     --	    add_cacheinfo(istate, mode, &oid, *++ctx->argv, 0))
     -+	    add_cacheinfo(cd->istate, mode, &oid, *++ctx->argv, 0))
     +-	    add_cacheinfo(repo->index, mode, &oid, *++ctx->argv, 0))
     ++	    add_cacheinfo(cd->repo->index, mode, &oid, *++ctx->argv, 0))
       		die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
       	ctx->argc -= 3;
       	return 0;
     @@ builtin/update-index.c: static enum parse_opt_result stdin_cacheinfo_callback(
       	if (ctx->argc != 1)
       		return error("option '%s' must be the last argument", opt->long_name);
       	allow_add = allow_replace = allow_remove = 1;
     --	read_index_info(istate, *nul_term_line);
     -+	read_index_info(cd->istate, cd->nul_term_line);
     +-	read_index_info(repo->index, *nul_term_line);
     ++	read_index_info(cd->repo->index, cd->nul_term_line);
       	return 0;
       }
       
     @@ builtin/update-index.c: static enum parse_opt_result unresolve_callback(
       	BUG_ON_OPT_ARG(arg);
       
       	/* consume remaining arguments. */
     --	*has_errors = do_unresolve(repo, istate, ctx->argc, ctx->argv,
     +-	*has_errors = do_unresolve(repo, ctx->argc, ctx->argv,
      -				   prefix, prefix ? strlen(prefix) : 0);
      -	if (*has_errors)
     --		istate->cache_changed = 0;
     -+	cd->has_errors = do_unresolve(cd->repo, cd->istate, ctx->argc, ctx->argv,
     +-		repo->index->cache_changed = 0;
     ++	cd->has_errors = do_unresolve(cd->repo, ctx->argc, ctx->argv,
      +				      prefix, prefix ? strlen(prefix) : 0);
      +	if (cd->has_errors)
     -+		cd->istate->cache_changed = 0;
     ++		cd->repo->index->cache_changed = 0;
       
       	ctx->argv += ctx->argc - 1;
       	ctx->argc = 1;
     @@ builtin/update-index.c: static enum parse_opt_result reupdate_callback(
       
       	/* consume remaining arguments. */
       	setup_work_tree();
     --	*has_errors = do_reupdate(repo, istate, ctx->argc, ctx->argv, prefix);
     +-	*has_errors = do_reupdate(repo, ctx->argc, ctx->argv, prefix);
      -	if (*has_errors)
     --		istate->cache_changed = 0;
     -+	cd->has_errors = do_reupdate(cd->repo, cd->istate,
     -+				     ctx->argc, ctx->argv, prefix);
     +-		repo->index->cache_changed = 0;
     ++	cd->has_errors = do_reupdate(cd->repo, ctx->argc, ctx->argv, prefix);
      +	if (cd->has_errors)
     -+		cd->istate->cache_changed = 0;
     ++		cd->repo->index->cache_changed = 0;
       
       	ctx->argv += ctx->argc - 1;
       	ctx->argc = 1;
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       	struct parse_opt_ctx_t ctx;
       	strbuf_getline_fn getline_fn;
       	int parseopt_state = PARSE_OPT_UNKNOWN;
     -+	struct index_state *istate;
      +
       	struct option options[] = {
      -		OPT_BIT('q', NULL, &refresh_args.flags,
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       	newfd = repo_hold_locked_index(repo, &lock_file, 0);
       	if (newfd < 0)
      @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
     - 	if (entries < 0)
       		die("cache corrupted");
       
     --	istate = repo->index;
     + 	repo->index->updated_skipworktree = 1;
      +	cd.repo = repo;
     -+	cd.istate = istate = repo->index;
     - 	istate->updated_skipworktree = 1;
      +	cd.flags = 0;
      +	cd.has_errors = 0;
      +	cd.nul_term_line = 0;
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       		if (preferred_index_format < INDEX_FORMAT_LB ||
       		    INDEX_FORMAT_UB < preferred_index_format)
      @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
     - 		istate->version = preferred_index_format;
     + 		repo->index->version = preferred_index_format;
       	}
       
      -	if (read_from_stdin) {
     @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const
       					die("line is badly quoted");
      @@ builtin/update-index.c: int cmd_update_index(int argc, const char **argv, const char *prefix)
       
     - 	if (istate->cache_changed || force_write) {
     + 	if (repo->index->cache_changed || force_write) {
       		if (newfd < 0) {
      -			if (refresh_args.flags & REFRESH_QUIET)
      +			if (cd.flags & REFRESH_QUIET)
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 01/14] mv: remove index compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

The mv builtin uses the compatibility macros to interact with the index. Update these to use modern methods referring to a 'struct index_state' pointer. Several helper methods need to be updated to consider such a pointer, but the modifications are rudimentary.

Two macros can be deleted from cache.h because these are the last uses.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/mv.c | 42 +++++++++++++++++++++++-------------------
 cache.h      |  2 --
 2 files changed, 23 insertions(+), 21 deletions(-)
Show changes to 2 files +23 −21

builtin/mv.c, cache.h

diff --git a/builtin/mv.c b/builtin/mv.c
index 7dac714af90..0055d49a8a7 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) 2006 Johannes Schindelin
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "config.h"
 #include "pathspec.h"
@@ -75,13 +74,14 @@ static const char *add_slash(const char *path)
 
 #define SUBMODULE_WITH_GITDIR ((const char *)1)
 
-static void prepare_move_submodule(const char *src, int first,
+static void prepare_move_submodule(struct index_state *istate,
+				   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(istate->cache[first]->ce_mode))
 		die(_("Directory %s is in index and no submodule?"), src);
-	if (!is_staging_gitmodules_ok(&the_index))
+	if (!is_staging_gitmodules_ok(istate))
 		die(_("Please stage your changes to .gitmodules or stash them to proceed"));
 	strbuf_addf(&submodule_dotgit, "%s/.git", src);
 	*submodule_gitfile = read_gitfile(submodule_dotgit.buf);
@@ -92,19 +92,20 @@ static void prepare_move_submodule(const char *src, int first,
 	strbuf_release(&submodule_dotgit);
 }
 
-static int index_range_of_same_dir(const char *src, int length,
+static int index_range_of_same_dir(struct index_state *istate,
+				   const char *src, int length,
 				   int *first_p, int *last_p)
 {
 	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(istate, src_w_slash, len_w_slash);
 	if (first >= 0)
 		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 < istate->cache_nr; last++) {
+		const char *path = istate->cache[last]->name;
 		if (strncmp(path, src_w_slash, len_w_slash))
 			break;
 	}
@@ -133,6 +134,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 	struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 	struct lock_file lock_file = LOCK_INIT;
 	struct cache_entry *ce;
+	struct index_state *istate;
 
 	git_config(git_default_config, NULL);
 
@@ -141,9 +143,10 @@ 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"));
+	istate = the_repository->index;
 
 	source = internal_prefix_pathspec(prefix, argv, argc, 0);
 	modes = xcalloc(argc, sizeof(enum update_mode));
@@ -190,12 +193,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				&& lstat(dst, &st) == 0)
 			bad = _("cannot move directory over file");
 		else if (src_is_dir) {
-			int first = cache_name_pos(src, length), last;
+			int first = index_name_pos(istate, src, length);
+			int last;
 
 			if (first >= 0)
-				prepare_move_submodule(src, first,
+				prepare_move_submodule(istate, src, first,
 						       submodule_gitfile + i);
-			else if (index_range_of_same_dir(src, length,
+			else if (index_range_of_same_dir(istate, src, length,
 							 &first, &last) < 1)
 				bad = _("source directory is empty");
 			else { /* last - first >= 1 */
@@ -212,7 +216,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				dst_len = strlen(dst);
 
 				for (j = 0; j < last - first; j++) {
-					const char *path = active_cache[first + j]->name;
+					const char *path = istate->cache[first + j]->name;
 					source[argc + j] = path;
 					destination[argc + j] =
 						prefix_path(dst, dst_len, path + length + 1);
@@ -221,7 +225,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 				}
 				argc += last - first;
 			}
-		} else if (!(ce = cache_file_exists(src, length, ignore_case))) {
+		} else if (!(ce = index_file_exists(istate, src, length, ignore_case))) {
 			bad = _("not under version control");
 		} else if (ce_stage(ce)) {
 			bad = _("conflicted");
@@ -291,15 +295,15 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		if (mode == WORKING_DIRECTORY)
 			continue;
 
-		pos = cache_name_pos(src, strlen(src));
+		pos = index_name_pos(istate, src, strlen(src));
 		assert(pos >= 0);
-		rename_cache_entry_at(pos, dst);
+		rename_index_entry_at(istate, pos, dst);
 	}
 
 	if (gitmodules_modified)
-		stage_updated_gitmodules(&the_index);
+		stage_updated_gitmodules(istate);
 
-	if (write_locked_index(&the_index, &lock_file,
+	if (write_locked_index(istate, &lock_file,
 			       COMMIT_LOCK | SKIP_IF_UNCHANGED))
 		die(_("Unable to write new index file"));
 
diff --git a/cache.h b/cache.h
index 2d844576ead..fdf061cac56 100644
--- a/cache.h
+++ b/cache.h
@@ -409,7 +409,6 @@ extern struct index_state 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_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))
@@ -420,7 +419,6 @@ extern struct index_state the_index;
 #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)
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 02/14] rm: remove compatilibity macros

From: Derrick Stolee <dstolee@microsoft.com>

The rm builtin still uses the antiquated compatibility macros for interacting with the index. Update these to the more modern uses by passing around a 'struct index_state' pointer.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/rm.c | 56 ++++++++++++++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 26 deletions(-)
Show changes to builtin/rm.c +30 −26
diff --git a/builtin/rm.c b/builtin/rm.c
index 4858631e0f0..767df8d6b25 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds 2006
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "config.h"
 #include "lockfile.h"
@@ -28,12 +27,14 @@ static struct {
 	} *entry;
 } list;
 
-static int get_ours_cache_pos(const char *path, int pos)
+static int get_ours_cache_pos(struct index_state *istate,
+			      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 < istate->cache_nr) &&
+	       !strcmp(istate->cache[i]->name, path)) {
+		if (ce_stage(istate->cache[i]) == 2)
 			return i;
 		i++;
 	}
@@ -61,7 +62,7 @@ static void print_error_files(struct string_list *files_list,
 	}
 }
 
-static void submodules_absorb_gitdir_if_needed(void)
+static void submodules_absorb_gitdir_if_needed(struct index_state *istate)
 {
 	int i;
 	for (i = 0; i < list.nr; i++) {
@@ -69,13 +70,13 @@ 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(istate, name, strlen(name));
 		if (pos < 0) {
-			pos = get_ours_cache_pos(name, pos);
+			pos = get_ours_cache_pos(istate, name, pos);
 			if (pos < 0)
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = istate->cache[pos];
 
 		if (!S_ISGITLINK(ce->ce_mode) ||
 		    !file_exists(ce->name) ||
@@ -88,7 +89,8 @@ static void submodules_absorb_gitdir_if_needed(void)
 	}
 }
 
-static int check_local_mod(struct object_id *head, int index_only)
+static int check_local_mod(struct index_state *istate,
+			   struct object_id *head, int index_only)
 {
 	/*
 	 * Items in list are already sorted in the cache order,
@@ -114,21 +116,21 @@ 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(istate, name, strlen(name));
 		if (pos < 0) {
 			/*
 			 * Skip unmerged entries except for populated submodules
 			 * that could lose history when removed.
 			 */
-			pos = get_ours_cache_pos(name, pos);
+			pos = get_ours_cache_pos(istate, name, pos);
 			if (pos < 0)
 				continue;
 
-			if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
+			if (!S_ISGITLINK(istate->cache[pos]->ce_mode) ||
 			    is_empty_dir(name))
 				continue;
 		}
-		ce = active_cache[pos];
+		ce = istate->cache[pos];
 
 		if (lstat(ce->name, &st) < 0) {
 			if (!is_missing_file_error(errno))
@@ -165,7 +167,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(istate, ce, &st, 0) ||
 		    (S_ISGITLINK(ce->ce_mode) &&
 		     bad_to_remove_submodule(ce->name,
 				SUBMODULE_REMOVAL_DIE_ON_ERROR |
@@ -257,6 +259,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	int i;
 	struct pathspec pathspec;
 	char *seen;
+	struct index_state *istate;
 
 	git_config(git_default_config, NULL);
 
@@ -284,24 +287,25 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	if (!index_only)
 		setup_work_tree();
 
-	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);
+	istate = the_repository->index;
+	refresh_index(istate, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
 
 	seen = xcalloc(pathspec.nr, 1);
 
-	for (i = 0; i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
-		if (!ce_path_match(&the_index, ce, &pathspec, seen))
+	for (i = 0; i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
+		if (!ce_path_match(istate, ce, &pathspec, seen))
 			continue;
 		ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
 		list.entry[list.nr].name = xstrdup(ce->name);
 		list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
 		if (list.entry[list.nr++].is_submodule &&
-		    !is_staging_gitmodules_ok(&the_index))
+		    !is_staging_gitmodules_ok(istate))
 			die(_("please stage your changes to .gitmodules or stash them to proceed"));
 	}
 
@@ -329,7 +333,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	}
 
 	if (!index_only)
-		submodules_absorb_gitdir_if_needed();
+		submodules_absorb_gitdir_if_needed(istate);
 
 	/*
 	 * If not forced, the file, the index and the HEAD (if exists)
@@ -345,7 +349,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		struct object_id oid;
 		if (get_oid("HEAD", &oid))
 			oidclr(&oid);
-		if (check_local_mod(&oid, index_only))
+		if (check_local_mod(istate, &oid, index_only))
 			exit(1);
 	}
 
@@ -358,7 +362,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(istate, path))
 			die(_("git rm: unable to remove %s"), path);
 	}
 
@@ -398,10 +402,10 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		}
 		strbuf_release(&buf);
 		if (gitmodules_modified)
-			stage_updated_gitmodules(&the_index);
+			stage_updated_gitmodules(istate);
 	}
 
-	if (write_locked_index(&the_index, &lock_file,
+	if (write_locked_index(istate, &lock_file,
 			       COMMIT_LOCK | SKIP_IF_UNCHANGED))
 		die(_("Unable to write new index file"));
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 04/14] update-index: use istate->cache over active_cache

From: Derrick Stolee <dstolee@microsoft.com>
Also use repo->index over istate, when possible.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
Show changes to builtin/update-index.c +11 −11
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 22284f301dc..0c5a10f5dba 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -234,12 +234,12 @@ 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(istate, active_cache[pos]);
+		mark_fsmonitor_invalid(istate, istate->cache[pos]);
 		if (mark)
-			active_cache[pos]->ce_flags |= flag;
+			istate->cache[pos]->ce_flags |= flag;
 		else
-			active_cache[pos]->ce_flags &= ~flag;
-		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
+			istate->cache[pos]->ce_flags &= ~flag;
+		istate->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(istate, path);
 		active_cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
@@ -330,7 +330,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 = istate->cache[pos];
 		if (S_ISGITLINK(ce->ce_mode)) {
 
 			/* Do nothing to the index if there is no HEAD! */
@@ -346,7 +346,7 @@ 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++];
+		const struct cache_entry *ce = istate->cache[pos++];
 
 		if (strncmp(ce->name, path, len))
 			break;
@@ -377,7 +377,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 : istate->cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
 		 * working directory version is assumed "good"
@@ -428,7 +428,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 = istate->cache[pos];
 	if (chmod_cache_entry(ce, flip) < 0)
 		goto fail;
 
@@ -628,7 +628,7 @@ static int unresolve_one(const char *path)
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
 		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+			const struct cache_entry *ce = repo->index->cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen))
@@ -642,7 +642,7 @@ static int unresolve_one(const char *path)
 		 */
 		pos = -pos-1;
 		if (pos < active_nr) {
-			const struct cache_entry *ce = active_cache[pos];
+			const struct cache_entry *ce = repo->index->cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
 				fprintf(stderr,
@@ -738,7 +738,7 @@ static int do_reupdate(int ac, const char **av,
 		has_head = 0;
  redo:
 	for (pos = 0; pos < active_nr; pos++) {
-		const struct cache_entry *ce = active_cache[pos];
+		const struct cache_entry *ce = repo->index->cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
 		char *path;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 03/14] update-index: drop the_index, the_repository

From: Derrick Stolee <dstolee@microsoft.com>

To reduce the need for the index compatibility macros, we will replace their uses in update-index mechanically. This is the most interesting change, which creates global "repo" and "istate" pointers. The macros that expand to use the_index can then be mechanically replaced by references to the istate pointer.

We will be careful to use "repo->index" over "istate" whenever repo is needed by a method.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 59 +++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 27 deletions(-)
Show changes to builtin/update-index.c +32 −27
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 44862f5e1de..22284f301dc 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -227,18 +227,20 @@ static int test_if_untracked_cache_is_supported(void)
 	return ret;
 }
 
+static struct index_state *istate;
+
 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(istate, active_cache[pos]);
 		if (mark)
 			active_cache[pos]->ce_flags |= flag;
 		else
 			active_cache[pos]->ce_flags &= ~flag;
 		active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
-		cache_tree_invalidate_path(&the_index, path);
+		cache_tree_invalidate_path(istate, path);
 		active_cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
@@ -277,14 +279,14 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
 	if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
 		return 0;
 
-	ce = make_empty_cache_entry(&the_index, len);
+	ce = make_empty_cache_entry(istate, len);
 	memcpy(ce->name, path, len);
 	ce->ce_flags = create_ce_flags(0);
 	ce->ce_namelen = len;
-	fill_stat_cache_info(&the_index, ce, st);
+	fill_stat_cache_info(istate, ce, st);
 	ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
 
-	if (index_path(&the_index, &ce->oid, path, st,
+	if (index_path(istate, &ce->oid, path, st,
 		       info_only ? 0 : HASH_WRITE_OBJECT)) {
 		discard_cache_entry(ce);
 		return -1;
@@ -406,7 +408,7 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 {
 	int res;
 
-	res = add_to_index_cacheinfo(&the_index, mode, oid, path, stage,
+	res = add_to_index_cacheinfo(istate, mode, oid, path, stage,
 				     allow_add, allow_replace, NULL);
 	if (res == -1)
 		return res;
@@ -583,6 +585,7 @@ static const char * const update_index_usage[] = {
 
 static struct object_id head_oid;
 static struct object_id merge_head_oid;
+static struct repository *repo;
 
 static struct cache_entry *read_one_ent(const char *which,
 					struct object_id *ent, const char *path,
@@ -592,7 +595,7 @@ static struct cache_entry *read_one_ent(const char *which,
 	struct object_id oid;
 	struct cache_entry *ce;
 
-	if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
+	if (get_tree_entry(repo, ent, path, &oid, &mode)) {
 		if (which)
 			error("%s: not in %s branch.", path, which);
 		return NULL;
@@ -602,7 +605,7 @@ static struct cache_entry *read_one_ent(const char *which,
 			error("%s: not a blob in %s branch.", path, which);
 		return NULL;
 	}
-	ce = make_empty_cache_entry(&the_index, namelen);
+	ce = make_empty_cache_entry(repo->index, namelen);
 
 	oidcpy(&ce->oid, &oid);
 	memcpy(ce->name, path, namelen);
@@ -740,7 +743,7 @@ static int do_reupdate(int ac, const char **av,
 		int save_nr;
 		char *path;
 
-		if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
+		if (ce_stage(ce) || !ce_path_match(repo->index, ce, &pathspec, NULL))
 			continue;
 		if (has_head)
 			old = read_one_ent(NULL, &head_oid,
@@ -957,7 +960,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	struct parse_opt_ctx_t ctx;
 	strbuf_getline_fn getline_fn;
 	int parseopt_state = PARSE_OPT_UNKNOWN;
-	struct repository *r = the_repository;
 	struct option options[] = {
 		OPT_BIT('q', NULL, &refresh_args.flags,
 			N_("continue refresh even when index needs update"),
@@ -1066,16 +1068,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	git_config(git_default_config, NULL);
 
+	repo = the_repository;
+
 	/* 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(repo, &lock_file, 0);
 	if (newfd < 0)
 		lock_error = errno;
 
-	entries = read_cache();
+	entries = repo_read_index(repo);
 	if (entries < 0)
 		die("cache corrupted");
 
-	the_index.updated_skipworktree = 1;
+	istate = repo->index;
+	repo->index->updated_skipworktree = 1;
 
 	/*
 	 * Custom copy of parse_options() because we want to handle
@@ -1129,9 +1134,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    preferred_index_format,
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
-		if (the_index.version != preferred_index_format)
+		if (repo->index->version != preferred_index_format)
 			active_cache_changed |= SOMETHING_CHANGED;
-		the_index.version = preferred_index_format;
+		repo->index->version = preferred_index_format;
 	}
 
 	if (read_from_stdin) {
@@ -1162,28 +1167,28 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			warning(_("core.splitIndex is set to false; "
 				  "remove or change it, if you really want to "
 				  "enable split index"));
-		if (the_index.split_index)
-			the_index.cache_changed |= SPLIT_INDEX_ORDERED;
+		if (repo->index->split_index)
+			repo->index->cache_changed |= SPLIT_INDEX_ORDERED;
 		else
-			add_split_index(&the_index);
+			add_split_index(repo->index);
 	} else if (!split_index) {
 		if (git_config_get_split_index() == 1)
 			warning(_("core.splitIndex is set to true; "
 				  "remove or change it, if you really want to "
 				  "disable split index"));
-		remove_split_index(&the_index);
+		remove_split_index(repo->index);
 	}
 
-	prepare_repo_settings(r);
+	prepare_repo_settings(repo);
 	switch (untracked_cache) {
 	case UC_UNSPECIFIED:
 		break;
 	case UC_DISABLE:
-		if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
+		if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
 			warning(_("core.untrackedCache is set to true; "
 				  "remove or change it, if you really want to "
 				  "disable the untracked cache"));
-		remove_untracked_cache(&the_index);
+		remove_untracked_cache(repo->index);
 		report(_("Untracked cache disabled"));
 		break;
 	case UC_TEST:
@@ -1191,11 +1196,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		return !test_if_untracked_cache_is_supported();
 	case UC_ENABLE:
 	case UC_FORCE:
-		if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
+		if (repo->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
 			warning(_("core.untrackedCache is set to false; "
 				  "remove or change it, if you really want to "
 				  "enable the untracked cache"));
-		add_untracked_cache(&the_index);
+		add_untracked_cache(repo->index);
 		report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
 		break;
 	default:
@@ -1207,14 +1212,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			warning(_("core.fsmonitor is unset; "
 				"set it if you really want to "
 				"enable fsmonitor"));
-		add_fsmonitor(&the_index);
+		add_fsmonitor(repo->index);
 		report(_("fsmonitor enabled"));
 	} else if (!fsmonitor) {
 		if (git_config_get_fsmonitor() == 1)
 			warning(_("core.fsmonitor is set; "
 				"remove it if you really want to "
 				"disable fsmonitor"));
-		remove_fsmonitor(&the_index);
+		remove_fsmonitor(repo->index);
 		report(_("fsmonitor disabled"));
 	}
 
@@ -1224,7 +1229,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 				exit(128);
 			unable_to_lock_die(get_index_file(), lock_error);
 		}
-		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		if (write_locked_index(repo->index, &lock_file, COMMIT_LOCK))
 			die("Unable to write new index file");
 	}
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 12/14] update-index: reduce static globals, part 1

From: Derrick Stolee <dstolee@microsoft.com>

In order to remove index compatibility macros cleanly, we relied upon static globals 'repo' and 'istate' to be pointers to the_repository and the_index, respectively. We can now start reducing the need for these static globals by modifying method prototypes to use them when necessary.

Remove the 'istate' static global in favor of method parameters. This adjusts some callers, which either use their own 'istate' parameter or 'repo->index'.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 74 +++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 33 deletions(-)
Show changes to builtin/update-index.c +41 −33
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 1c1cb8f8d4a..9a83603c0db 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -226,9 +226,8 @@ static int test_if_untracked_cache_is_supported(void)
 	return ret;
 }
 
-static struct index_state *istate;
-
-static int mark_ce_flags(const char *path, int flag, int mark)
+static int mark_ce_flags(struct index_state *istate,
+			 const char *path, int flag, int mark)
 {
 	int namelen = strlen(path);
 	int pos = index_name_pos(istate, path, namelen);
@@ -246,7 +245,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 	return -1;
 }
 
-static int remove_one_path(const char *path)
+static int remove_one_path(struct index_state *istate, const char *path)
 {
 	if (!allow_remove)
 		return error("%s: does not exist and --remove not passed", path);
@@ -262,14 +261,17 @@ static int remove_one_path(const char *path)
  *    succeeds.
  *  - permission error. That's never ok.
  */
-static int process_lstat_error(const char *path, int err)
+static int process_lstat_error(struct index_state *istate,
+			       const char *path, int err)
 {
 	if (is_missing_file_error(err))
-		return remove_one_path(path);
+		return remove_one_path(istate, path);
 	return error("lstat(\"%s\"): %s", path, strerror(err));
 }
 
-static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
+static int add_one_path(struct index_state *istate,
+			const struct cache_entry *old,
+			const char *path, int len, struct stat *st)
 {
 	int option;
 	struct cache_entry *ce;
@@ -322,7 +324,8 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
  *  - it doesn't exist at all in the index, but it is a valid
  *    git directory, and it should be *added* as a gitlink.
  */
-static int process_directory(const char *path, int len, struct stat *st)
+static int process_directory(struct index_state *istate,
+			     const char *path, int len, struct stat *st)
 {
 	struct object_id oid;
 	int pos = index_name_pos(istate, path, len);
@@ -336,10 +339,10 @@ static int process_directory(const char *path, int len, struct stat *st)
 			if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
 				return 0;
 
-			return add_one_path(ce, path, len, st);
+			return add_one_path(istate, ce, path, len, st);
 		}
 		/* Should this be an unconditional error? */
-		return remove_one_path(path);
+		return remove_one_path(istate, path);
 	}
 
 	/* Inexact match: is there perhaps a subdirectory match? */
@@ -360,13 +363,14 @@ static int process_directory(const char *path, int len, struct stat *st)
 
 	/* No match - should we add it as a gitlink? */
 	if (!resolve_gitlink_ref(path, "HEAD", &oid))
-		return add_one_path(NULL, path, len, st);
+		return add_one_path(istate, NULL, path, len, st);
 
 	/* Error out. */
 	return error("%s: is a directory - add files inside instead", path);
 }
 
-static int process_path(const char *path, struct stat *st, int stat_errno)
+static int process_path(struct index_state *istate,
+			const char *path, struct stat *st, int stat_errno)
 {
 	int pos, len;
 	const struct cache_entry *ce;
@@ -394,15 +398,16 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 	 * what to do about the pathname!
 	 */
 	if (stat_errno)
-		return process_lstat_error(path, stat_errno);
+		return process_lstat_error(istate, path, stat_errno);
 
 	if (S_ISDIR(st->st_mode))
-		return process_directory(path, len, st);
+		return process_directory(istate, path, len, st);
 
-	return add_one_path(ce, path, len, st);
+	return add_one_path(istate, ce, path, len, st);
 }
 
-static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
+static int add_cacheinfo(struct index_state *istate,
+			 unsigned int mode, const struct object_id *oid,
 			 const char *path, int stage)
 {
 	int res;
@@ -419,7 +424,8 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
 	return 0;
 }
 
-static void chmod_path(char flip, const char *path)
+static void chmod_path(struct index_state *istate,
+		       char flip, const char *path)
 {
 	int pos;
 	struct cache_entry *ce;
@@ -437,7 +443,7 @@ static void chmod_path(char flip, const char *path)
 	die("git update-index: cannot chmod %cx '%s'", flip, path);
 }
 
-static void update_one(const char *path)
+static void update_one(struct index_state *istate, const char *path)
 {
 	int stat_errno = 0;
 	struct stat st;
@@ -455,17 +461,20 @@ static void update_one(const char *path)
 		return;
 	}
 	if (mark_valid_only) {
-		if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
+		if (mark_ce_flags(istate, path, CE_VALID,
+				  mark_valid_only == MARK_FLAG))
 			die("Unable to mark file %s", path);
 		return;
 	}
 	if (mark_skip_worktree_only) {
-		if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
+		if (mark_ce_flags(istate, path, CE_SKIP_WORKTREE,
+				  mark_skip_worktree_only == MARK_FLAG))
 			die("Unable to mark file %s", path);
 		return;
 	}
 	if (mark_fsmonitor_only) {
-		if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG))
+		if (mark_ce_flags(istate, path, CE_FSMONITOR_VALID,
+				  mark_fsmonitor_only == MARK_FLAG))
 			die("Unable to mark file %s", path);
 		return;
 	}
@@ -476,12 +485,12 @@ static void update_one(const char *path)
 		report("remove '%s'", path);
 		return;
 	}
-	if (process_path(path, &st, stat_errno))
+	if (process_path(istate, path, &st, stat_errno))
 		die("Unable to process path %s", path);
 	report("add '%s'", path);
 }
 
-static void read_index_info(int nul_term_line)
+static void read_index_info(struct index_state *istate, int nul_term_line)
 {
 	const int hexsz = the_hash_algo->hexsz;
 	struct strbuf buf = STRBUF_INIT;
@@ -564,7 +573,7 @@ static void read_index_info(int nul_term_line)
 			 * ptr[-41] is at the beginning of sha1
 			 */
 			ptr[-(hexsz + 2)] = ptr[-1] = 0;
-			if (add_cacheinfo(mode, &oid, path_name, stage))
+			if (add_cacheinfo(istate, mode, &oid, path_name, stage))
 				die("git update-index: unable to update %s",
 				    path_name);
 		}
@@ -758,7 +767,7 @@ static int do_reupdate(int ac, const char **av,
 		 */
 		save_nr = repo->index->cache_nr;
 		path = xstrdup(ce->name);
-		update_one(path);
+		update_one(repo->index, path);
 		free(path);
 		discard_cache_entry(old);
 		if (save_nr != repo->index->cache_nr)
@@ -854,7 +863,7 @@ static enum parse_opt_result cacheinfo_callback(
 	BUG_ON_OPT_ARG(arg);
 
 	if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
-		if (add_cacheinfo(mode, &oid, path, 0))
+		if (add_cacheinfo(repo->index, mode, &oid, path, 0))
 			die("git update-index: --cacheinfo cannot add %s", path);
 		ctx->argv++;
 		ctx->argc--;
@@ -864,7 +873,7 @@ static enum parse_opt_result cacheinfo_callback(
 		return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
 	if (strtoul_ui(*++ctx->argv, 8, &mode) ||
 	    get_oid_hex(*++ctx->argv, &oid) ||
-	    add_cacheinfo(mode, &oid, *++ctx->argv, 0))
+	    add_cacheinfo(repo->index, mode, &oid, *++ctx->argv, 0))
 		die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
 	ctx->argc -= 3;
 	return 0;
@@ -882,7 +891,7 @@ static enum parse_opt_result stdin_cacheinfo_callback(
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
 	allow_add = allow_replace = allow_remove = 1;
-	read_index_info(*nul_term_line);
+	read_index_info(repo->index, *nul_term_line);
 	return 0;
 }
 
@@ -1079,7 +1088,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	if (entries < 0)
 		die("cache corrupted");
 
-	istate = repo->index;
 	repo->index->updated_skipworktree = 1;
 
 	/*
@@ -1108,9 +1116,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 			setup_work_tree();
 			p = prefix_path(prefix, prefix_length, path);
-			update_one(p);
+			update_one(repo->index, p);
 			if (set_executable_bit)
-				chmod_path(set_executable_bit, p);
+				chmod_path(repo->index, set_executable_bit, p);
 			free(p);
 			ctx.argc--;
 			ctx.argv++;
@@ -1153,9 +1161,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 				strbuf_swap(&buf, &unquoted);
 			}
 			p = prefix_path(prefix, prefix_length, buf.buf);
-			update_one(p);
+			update_one(repo->index, p);
 			if (set_executable_bit)
-				chmod_path(set_executable_bit, p);
+				chmod_path(repo->index, set_executable_bit, p);
 			free(p);
 		}
 		strbuf_release(&unquoted);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 05/14] update-index: use istate->cache_nr over active_nr

From: Derrick Stolee <dstolee@microsoft.com>
Also use "repo->index" over "istate" when possible.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
Show changes to builtin/update-index.c +7 −7
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 0c5a10f5dba..2b03b29261b 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -345,7 +345,7 @@ 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) {
+	while (pos < istate->cache_nr) {
 		const struct cache_entry *ce = istate->cache[pos++];
 
 		if (strncmp(ce->name, path, len))
@@ -627,7 +627,7 @@ static int unresolve_one(const char *path)
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
-		if (pos < active_nr) {
+		if (pos < repo->index->cache_nr) {
 			const struct cache_entry *ce = repo->index->cache[pos];
 			if (ce_stage(ce) &&
 			    ce_namelen(ce) == namelen &&
@@ -641,7 +641,7 @@ static int unresolve_one(const char *path)
 		 * want to do anything in the former case.
 		 */
 		pos = -pos-1;
-		if (pos < active_nr) {
+		if (pos < repo->index->cache_nr) {
 			const struct cache_entry *ce = repo->index->cache[pos];
 			if (ce_namelen(ce) == namelen &&
 			    !memcmp(ce->name, path, namelen)) {
@@ -737,7 +737,7 @@ static int do_reupdate(int ac, const char **av,
 		 */
 		has_head = 0;
  redo:
-	for (pos = 0; pos < active_nr; pos++) {
+	for (pos = 0; pos < repo->index->cache_nr; pos++) {
 		const struct cache_entry *ce = repo->index->cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
@@ -755,14 +755,14 @@ static int do_reupdate(int ac, const char **av,
 		}
 		/* Be careful.  The working tree may not have the
 		 * path anymore, in which case, under 'allow_remove',
-		 * or worse yet 'allow_replace', active_nr may decrease.
+		 * or worse yet 'allow_replace', repo->index->cache_nr may decrease.
 		 */
-		save_nr = active_nr;
+		save_nr = repo->index->cache_nr;
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
 		discard_cache_entry(old);
-		if (save_nr != active_nr)
+		if (save_nr != repo->index->cache_nr)
 			goto redo;
 	}
 	clear_pathspec(&pathspec);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 07/14] update-index: use index_name_pos() over cache_name_pos()

From: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 70ca47e712c..a24b1fc90e4 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -232,7 +232,7 @@ static struct index_state *istate;
 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(istate, path, namelen);
 	if (0 <= pos) {
 		mark_fsmonitor_invalid(istate, istate->cache[pos]);
 		if (mark)
@@ -326,7 +326,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(istate, path, len);
 
 	/* Exact match: file or existing gitlink */
 	if (pos >= 0) {
@@ -376,7 +376,7 @@ static int process_path(const char *path, struct stat *st, int stat_errno)
 	if (has_symlink_leading_path(path, len))
 		return error("'%s' is beyond a symbolic link", path);
 
-	pos = cache_name_pos(path, len);
+	pos = index_name_pos(istate, path, len);
 	ce = pos < 0 ? NULL : istate->cache[pos];
 	if (ce && ce_skip_worktree(ce)) {
 		/*
@@ -425,7 +425,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(istate, path, strlen(path));
 	if (pos < 0)
 		goto fail;
 	ce = istate->cache[pos];
@@ -623,7 +623,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(repo->index, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
 		pos = unmerge_cache_entry_at(pos);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 09/14] update-index: use add_index_entry()

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of add_cache_entry().
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Show changes to builtin/update-index.c +3 −3
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 87fbc580032..a1e4ee89056 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -293,7 +293,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(istate, ce, option)) {
 		discard_cache_entry(ce);
 		return error("%s: cannot add to the index - missing --add option?", path);
 	}
@@ -672,12 +672,12 @@ static int unresolve_one(const char *path)
 	}
 
 	remove_file_from_index(repo->index, path);
-	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
+	if (add_index_entry(repo->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(repo->index, ce_3, ADD_CACHE_OK_TO_ADD))
 		return 0;
 	error("%s: cannot add their version to the index.", path);
 	ret = -1;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 11/14] update-index: remove ce_match_stat(), all macros

From: Derrick Stolee <dstolee@microsoft.com>

The final index compatibility macro to remove from the update-index builtin is ce_match_stat(). Further, this is the last use of that macro anywhere, so it should be removed.

There are some remaining references in the racy-git.txt technical document that are updated to ie_match_stat().

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Documentation/technical/racy-git.txt | 6 +++---
 builtin/update-index.c               | 3 +--
 cache.h                              | 1 -
 3 files changed, 4 insertions(+), 6 deletions(-)
Show changes to 3 files +4 −6

Documentation/technical/racy-git.txt, builtin/update-index.c, cache.h

diff --git a/Documentation/technical/racy-git.txt b/Documentation/technical/racy-git.txt
index ceda4bbfda4..65188e04559 100644
--- a/Documentation/technical/racy-git.txt
+++ b/Documentation/technical/racy-git.txt
@@ -26,7 +26,7 @@ information obtained from the filesystem via `lstat(2)` system
 call when they were last updated.  When checking if they differ,
 Git first runs `lstat(2)` on the files and compares the result
 with this information (this is what was originally done by the
-`ce_match_stat()` function, but the current code does it in
+`ie_match_stat()` function, but the current code does it in
 `ce_match_stat_basic()` function).  If some of these "cached
 stat information" fields do not match, Git can tell that the
 files are modified without even looking at their contents.
@@ -102,7 +102,7 @@ timestamp as the index file itself.
 
 The callers that want to check if an index entry matches the
 corresponding file in the working tree continue to call
-`ce_match_stat()`, but with this change, `ce_match_stat()` uses
+`ie_match_stat()`, but with this change, `ie_match_stat()` uses
 `ce_modified_check_fs()` to see if racily clean ones are
 actually clean after comparing the cached stat information using
 `ce_match_stat_basic()`.
@@ -128,7 +128,7 @@ Runtime penalty
 ---------------
 
 The runtime penalty of falling back to `ce_modified_check_fs()`
-from `ce_match_stat()` can be very expensive when there are many
+from `ie_match_stat()` can be very expensive when there are many
 racily clean entries.  An obvious way to artificially create
 this situation is to give the same timestamp to all the files in
 the working tree in a large project, run `git update-index` on
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 64feb47c97f..1c1cb8f8d4a 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -3,7 +3,6 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "lockfile.h"
@@ -276,7 +275,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(istate, old, st, 0))
 		return 0;
 
 	ce = make_empty_cache_entry(istate, len);
diff --git a/cache.h b/cache.h
index 8c091be6256..740bd0aa1dd 100644
--- a/cache.h
+++ b/cache.h
@@ -416,7 +416,6 @@ extern struct index_state the_index;
 #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_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 06/14] update-index: use istate->cache_changed

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of active_cache_changed.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 2b03b29261b..70ca47e712c 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -241,7 +241,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)
 			istate->cache[pos]->ce_flags &= ~flag;
 		istate->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
 		cache_tree_invalidate_path(istate, path);
-		active_cache_changed |= CE_ENTRY_CHANGED;
+		istate->cache_changed |= CE_ENTRY_CHANGED;
 		return 0;
 	}
 	return -1;
@@ -915,7 +915,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;
+		repo->index->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -936,7 +936,7 @@ static enum parse_opt_result reupdate_callback(
 	setup_work_tree();
 	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
 	if (*has_errors)
-		active_cache_changed = 0;
+		repo->index->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -1135,7 +1135,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			    INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 
 		if (repo->index->version != preferred_index_format)
-			active_cache_changed |= SOMETHING_CHANGED;
+			repo->index->cache_changed |= SOMETHING_CHANGED;
 		repo->index->version = preferred_index_format;
 	}
 
@@ -1223,7 +1223,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		report(_("fsmonitor disabled"));
 	}
 
-	if (active_cache_changed || force_write) {
+	if (repo->index->cache_changed || force_write) {
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 10/14] update-index: replace several compatibility macros

From: Derrick Stolee <dstolee@microsoft.com>

This is also the last usage of unmerge_cache_entry_at(), so it can be removed from cache.h.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 11 ++++++-----
 cache.h                |  1 -
 2 files changed, 6 insertions(+), 6 deletions(-)
Show changes to 2 files +6 −6

builtin/update-index.c, cache.h

diff --git a/builtin/update-index.c b/builtin/update-index.c
index a1e4ee89056..64feb47c97f 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -429,7 +429,7 @@ static void chmod_path(char flip, const char *path)
 	if (pos < 0)
 		goto fail;
 	ce = istate->cache[pos];
-	if (chmod_cache_entry(ce, flip) < 0)
+	if (chmod_index_entry(istate, ce, flip) < 0)
 		goto fail;
 
 	report("chmod %cx '%s'", flip, path);
@@ -626,7 +626,7 @@ static int unresolve_one(const char *path)
 	pos = index_name_pos(repo->index, path, namelen);
 	if (0 <= pos) {
 		/* already merged */
-		pos = unmerge_cache_entry_at(pos);
+		pos = unmerge_index_entry_at(repo->index, pos);
 		if (pos < repo->index->cache_nr) {
 			const struct cache_entry *ce = repo->index->cache[pos];
 			if (ce_stage(ce) &&
@@ -777,8 +777,9 @@ 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(repo);
+	*o->has_errors |= refresh_index(repo->index, o->flags | flag,
+					NULL, NULL, NULL);
 	return 0;
 }
 
@@ -814,7 +815,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(repo->index);
 	return 0;
 }
 
diff --git a/cache.h b/cache.h
index fdf061cac56..8c091be6256 100644
--- a/cache.h
+++ b/cache.h
@@ -421,7 +421,6 @@ extern struct index_state the_index;
 #define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
 #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))
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 08/14] update-index: use remove_file_from_index()

From: Derrick Stolee <dstolee@microsoft.com>
This is a mechanical replacement of remove_file_from_cache().
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to builtin/update-index.c +5 −5
diff --git a/builtin/update-index.c b/builtin/update-index.c
index a24b1fc90e4..87fbc580032 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -251,7 +251,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(istate, path))
 		return error("%s: cannot remove from the index", path);
 	return 0;
 }
@@ -385,7 +385,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(istate, path))
 			return error("%s: cannot remove from the index", path);
 		return 0;
 	}
@@ -472,7 +472,7 @@ static void update_one(const char *path)
 	}
 
 	if (force_remove) {
-		if (remove_file_from_cache(path))
+		if (remove_file_from_index(istate, path))
 			die("git update-index: unable to remove %s", path);
 		report("remove '%s'", path);
 		return;
@@ -555,7 +555,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(istate, path_name))
 				die("git update-index: unable to remove %s",
 				    ptr);
 		}
@@ -671,7 +671,7 @@ static int unresolve_one(const char *path)
 		goto free_return;
 	}
 
-	remove_file_from_cache(path);
+	remove_file_from_index(repo->index, path);
 	if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
 		error("%s: cannot add our version to the index.", path);
 		ret = -1;
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 13/14] update-index: reduce static globals, part 2

From: Derrick Stolee <dstolee@microsoft.com>

In order to remove index compatibility macros cleanly, we relied upon static globals 'repo' and 'istate' to be pointers to the_repository and the_index, respectively. We can continue reducing the need for these static globals by modifying method prototypes to use them when necessary.

Move the remaining 'struct repository *repo' further down the file and use method parameters to pass it around instead.

The only remaining change is to remove the static global entirely, but that requires updating the parse-opt callbacks, which need a different solution.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
Show changes to builtin/update-index.c +17 −12
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 9a83603c0db..3e01d62865f 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -593,9 +593,9 @@ static const char * const update_index_usage[] = {
 
 static struct object_id head_oid;
 static struct object_id merge_head_oid;
-static struct repository *repo;
 
-static struct cache_entry *read_one_ent(const char *which,
+static struct cache_entry *read_one_ent(struct repository *repo,
+					const char *which,
 					struct object_id *ent, const char *path,
 					int namelen, int stage)
 {
@@ -623,7 +623,8 @@ static struct cache_entry *read_one_ent(const char *which,
 	return ce;
 }
 
-static int unresolve_one(const char *path)
+static int unresolve_one(struct repository *repo,
+			 const char *path)
 {
 	int namelen = strlen(path);
 	int pos;
@@ -665,8 +666,8 @@ static int unresolve_one(const char *path)
 	 * stuff HEAD version in stage #2,
 	 * stuff MERGE_HEAD version in stage #3.
 	 */
-	ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
-	ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
+	ce_2 = read_one_ent(repo, "our", &head_oid, path, namelen, 2);
+	ce_3 = read_one_ent(repo, "their", &merge_head_oid, path, namelen, 3);
 
 	if (!ce_2 || !ce_3) {
 		ret = -1;
@@ -705,7 +706,8 @@ static void read_head_pointers(void)
 	}
 }
 
-static int do_unresolve(int ac, const char **av,
+static int do_unresolve(struct repository *repo,
+			int ac, const char **av,
 			const char *prefix, int prefix_length)
 {
 	int i;
@@ -719,13 +721,14 @@ static int do_unresolve(int ac, const char **av,
 	for (i = 1; i < ac; i++) {
 		const char *arg = av[i];
 		char *p = prefix_path(prefix, prefix_length, arg);
-		err |= unresolve_one(p);
+		err |= unresolve_one(repo, p);
 		free(p);
 	}
 	return err;
 }
 
-static int do_reupdate(int ac, const char **av,
+static int do_reupdate(struct repository *repo,
+		       int ac, const char **av,
 		       const char *prefix)
 {
 	/* Read HEAD and run update-index on paths that are
@@ -754,7 +757,7 @@ static int do_reupdate(int ac, const char **av,
 		if (ce_stage(ce) || !ce_path_match(repo->index, ce, &pathspec, NULL))
 			continue;
 		if (has_head)
-			old = read_one_ent(NULL, &head_oid,
+			old = read_one_ent(repo, NULL, &head_oid,
 					   ce->name, ce_namelen(ce), 0);
 		if (old && ce->ce_mode == old->ce_mode &&
 		    oideq(&ce->oid, &old->oid)) {
@@ -782,6 +785,8 @@ struct refresh_params {
 	int *has_errors;
 };
 
+static struct repository *repo;
+
 static int refresh(struct refresh_params *o, unsigned int flag)
 {
 	setup_work_tree();
@@ -921,8 +926,8 @@ static enum parse_opt_result unresolve_callback(
 	BUG_ON_OPT_ARG(arg);
 
 	/* consume remaining arguments. */
-	*has_errors = do_unresolve(ctx->argc, ctx->argv,
-				prefix, prefix ? strlen(prefix) : 0);
+	*has_errors = do_unresolve(repo, ctx->argc, ctx->argv,
+				   prefix, prefix ? strlen(prefix) : 0);
 	if (*has_errors)
 		repo->index->cache_changed = 0;
 
@@ -943,7 +948,7 @@ static enum parse_opt_result reupdate_callback(
 
 	/* consume remaining arguments. */
 	setup_work_tree();
-	*has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
+	*has_errors = do_reupdate(repo, ctx->argc, ctx->argv, prefix);
 	if (*has_errors)
 		repo->index->cache_changed = 0;
 
-- 
gitgitgadget
Derrick Stolee via GitGitGadget· Jan 8, 2021, 20:02 UTC · re: Derrick Stolee via GitGitGadget · lore

[PATCH v3 14/14] update-index: remove static globals from callbacks

From: Derrick Stolee <dstolee@microsoft.com>

In order to remove index compatibility macros cleanly, we relied upon static globals 'repo' and 'istate' to be pointers to the_repository and the_index, respectively. We remove these static globals inside the option parsing callbacks, which are the final uses in update-index.

The callbacks cannot change their method signature, so we must use the value member of 'struct option', assigned in the array of option macros. There are several callback methods that require at least one of 'repo' and 'istate', but they use a variety of different data types for the callback value.

Unify these callback methods to use a consistent 'struct callback_data' that contains a 'repo' member, ready to use. This takes the place of the previous 'struct refresh_params' which served only to group the 'flags' and 'has_errors' ints. We also collect other one-off settings, but only those that require access to the index or repository in their operation.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/update-index.c | 104 ++++++++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 48 deletions(-)
Show changes to builtin/update-index.c +56 −48
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 3e01d62865f..2c67a870cdc 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -780,18 +780,20 @@ static int do_reupdate(struct repository *repo,
 	return 0;
 }
 
-struct refresh_params {
+struct callback_data {
+	struct repository *repo;
+
 	unsigned int flags;
-	int *has_errors;
+	unsigned int has_errors;
+	unsigned nul_term_line;
+	unsigned read_from_stdin;
 };
 
-static struct repository *repo;
-
-static int refresh(struct refresh_params *o, unsigned int flag)
+static int refresh(struct callback_data *cd, unsigned int flag)
 {
 	setup_work_tree();
-	repo_read_index(repo);
-	*o->has_errors |= refresh_index(repo->index, o->flags | flag,
+	repo_read_index(cd->repo);
+	cd->has_errors |= refresh_index(cd->repo->index, cd->flags | flag,
 					NULL, NULL, NULL);
 	return 0;
 }
@@ -813,7 +815,7 @@ static int really_refresh_callback(const struct option *opt,
 }
 
 static int chmod_callback(const struct option *opt,
-				const char *arg, int unset)
+			  const char *arg, int unset)
 {
 	char *flip = opt->value;
 	BUG_ON_OPT_NEG(unset);
@@ -824,11 +826,12 @@ static int chmod_callback(const struct option *opt,
 }
 
 static int resolve_undo_clear_callback(const struct option *opt,
-				const char *arg, int unset)
+				       const char *arg, int unset)
 {
+	struct callback_data *cd = opt->value;
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
-	resolve_undo_clear_index(repo->index);
+	resolve_undo_clear_index(cd->repo->index);
 	return 0;
 }
 
@@ -863,12 +866,13 @@ static enum parse_opt_result cacheinfo_callback(
 	struct object_id oid;
 	unsigned int mode;
 	const char *path;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
-		if (add_cacheinfo(repo->index, mode, &oid, path, 0))
+		if (add_cacheinfo(cd->repo->index, mode, &oid, path, 0))
 			die("git update-index: --cacheinfo cannot add %s", path);
 		ctx->argv++;
 		ctx->argc--;
@@ -878,7 +882,7 @@ static enum parse_opt_result cacheinfo_callback(
 		return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
 	if (strtoul_ui(*++ctx->argv, 8, &mode) ||
 	    get_oid_hex(*++ctx->argv, &oid) ||
-	    add_cacheinfo(repo->index, mode, &oid, *++ctx->argv, 0))
+	    add_cacheinfo(cd->repo->index, mode, &oid, *++ctx->argv, 0))
 		die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
 	ctx->argc -= 3;
 	return 0;
@@ -888,7 +892,7 @@ static enum parse_opt_result stdin_cacheinfo_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *nul_term_line = opt->value;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
@@ -896,7 +900,7 @@ static enum parse_opt_result stdin_cacheinfo_callback(
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
 	allow_add = allow_replace = allow_remove = 1;
-	read_index_info(repo->index, *nul_term_line);
+	read_index_info(cd->repo->index, cd->nul_term_line);
 	return 0;
 }
 
@@ -904,14 +908,14 @@ static enum parse_opt_result stdin_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *read_from_stdin = opt->value;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
-	*read_from_stdin = 1;
+	cd->read_from_stdin = 1;
 	return 0;
 }
 
@@ -919,17 +923,17 @@ static enum parse_opt_result unresolve_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *has_errors = opt->value;
 	const char *prefix = startup_info->prefix;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	/* consume remaining arguments. */
-	*has_errors = do_unresolve(repo, ctx->argc, ctx->argv,
-				   prefix, prefix ? strlen(prefix) : 0);
-	if (*has_errors)
-		repo->index->cache_changed = 0;
+	cd->has_errors = do_unresolve(cd->repo, ctx->argc, ctx->argv,
+				      prefix, prefix ? strlen(prefix) : 0);
+	if (cd->has_errors)
+		cd->repo->index->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -940,17 +944,17 @@ static enum parse_opt_result reupdate_callback(
 	struct parse_opt_ctx_t *ctx, const struct option *opt,
 	const char *arg, int unset)
 {
-	int *has_errors = opt->value;
 	const char *prefix = startup_info->prefix;
+	struct callback_data *cd = opt->value;
 
 	BUG_ON_OPT_NEG(unset);
 	BUG_ON_OPT_ARG(arg);
 
 	/* consume remaining arguments. */
 	setup_work_tree();
-	*has_errors = do_reupdate(repo, ctx->argc, ctx->argv, prefix);
-	if (*has_errors)
-		repo->index->cache_changed = 0;
+	cd->has_errors = do_reupdate(cd->repo, ctx->argc, ctx->argv, prefix);
+	if (cd->has_errors)
+		cd->repo->index->cache_changed = 0;
 
 	ctx->argv += ctx->argc - 1;
 	ctx->argc = 1;
@@ -959,13 +963,13 @@ static enum parse_opt_result reupdate_callback(
 
 int cmd_update_index(int argc, const char **argv, const char *prefix)
 {
-	int newfd, entries, has_errors = 0, nul_term_line = 0;
+	struct repository *repo = the_repository;
+	struct callback_data cd;
+	int newfd, entries;
 	enum uc_mode untracked_cache = UC_UNSPECIFIED;
-	int read_from_stdin = 0;
 	int prefix_length = prefix ? strlen(prefix) : 0;
 	int preferred_index_format = 0;
 	char set_executable_bit = 0;
-	struct refresh_params refresh_args = {0, &has_errors};
 	int lock_error = 0;
 	int split_index = -1;
 	int force_write = 0;
@@ -974,11 +978,12 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	struct parse_opt_ctx_t ctx;
 	strbuf_getline_fn getline_fn;
 	int parseopt_state = PARSE_OPT_UNKNOWN;
+
 	struct option options[] = {
-		OPT_BIT('q', NULL, &refresh_args.flags,
+		OPT_BIT('q', NULL, &cd.flags,
 			N_("continue refresh even when index needs update"),
 			REFRESH_QUIET),
-		OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
+		OPT_BIT(0, "ignore-submodules", &cd.flags,
 			N_("refresh: ignore submodules"),
 			REFRESH_IGNORE_SUBMODULES),
 		OPT_SET_INT(0, "add", &allow_add,
@@ -987,18 +992,18 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			N_("let files replace directories and vice-versa"), 1),
 		OPT_SET_INT(0, "remove", &allow_remove,
 			N_("notice files missing from worktree"), 1),
-		OPT_BIT(0, "unmerged", &refresh_args.flags,
+		OPT_BIT(0, "unmerged", &cd.flags,
 			N_("refresh even if index contains unmerged entries"),
 			REFRESH_UNMERGED),
-		OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
+		OPT_CALLBACK_F(0, "refresh", &cd, NULL,
 			N_("refresh stat information"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 			refresh_callback),
-		OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
+		OPT_CALLBACK_F(0, "really-refresh", &cd, NULL,
 			N_("like --refresh, but ignore assume-unchanged setting"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 			really_refresh_callback),
-		{OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", &cd,
 			N_("<mode>,<object>,<path>"),
 			N_("add the specified entry to the index"),
 			PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
@@ -1027,30 +1032,30 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			N_("add to index only; do not add content to object database"), 1),
 		OPT_SET_INT(0, "force-remove", &force_remove,
 			N_("remove named paths even if present in worktree"), 1),
-		OPT_BOOL('z', NULL, &nul_term_line,
+		OPT_BOOL('z', NULL, &cd.nul_term_line,
 			 N_("with --stdin: input lines are terminated by null bytes")),
-		{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &cd, NULL,
 			N_("read list of paths to be updated from standard input"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, stdin_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &cd, NULL,
 			N_("add entries from standard input to the index"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, stdin_cacheinfo_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &cd, NULL,
 			N_("repopulate stages #2 and #3 for the listed paths"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, unresolve_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 'g', "again", &cd, NULL,
 			N_("only update entries that differ from HEAD"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			NULL, 0, reupdate_callback},
-		OPT_BIT(0, "ignore-missing", &refresh_args.flags,
+		OPT_BIT(0, "ignore-missing", &cd.flags,
 			N_("ignore files missing from worktree"),
 			REFRESH_IGNORE_MISSING),
 		OPT_SET_INT(0, "verbose", &verbose,
 			N_("report actions to standard output"), 1),
-		OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
+		OPT_CALLBACK_F(0, "clear-resolve-undo", &cd, NULL,
 			N_("(for porcelains) forget saved unresolved conflicts"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 			resolve_undo_clear_callback),
@@ -1082,8 +1087,6 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	git_config(git_default_config, NULL);
 
-	repo = the_repository;
-
 	/* we will diagnose later if it turns out that we need to update it */
 	newfd = repo_hold_locked_index(repo, &lock_file, 0);
 	if (newfd < 0)
@@ -1094,6 +1097,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		die("cache corrupted");
 
 	repo->index->updated_skipworktree = 1;
+	cd.repo = repo;
+	cd.flags = 0;
+	cd.has_errors = 0;
+	cd.nul_term_line = 0;
+	cd.read_from_stdin = 0;
 
 	/*
 	 * Custom copy of parse_options() because we want to handle
@@ -1139,7 +1147,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	}
 	argc = parse_options_end(&ctx);
 
-	getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
+	getline_fn = cd.nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
 	if (preferred_index_format) {
 		if (preferred_index_format < INDEX_FORMAT_LB ||
 		    INDEX_FORMAT_UB < preferred_index_format)
@@ -1152,14 +1160,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		repo->index->version = preferred_index_format;
 	}
 
-	if (read_from_stdin) {
+	if (cd.read_from_stdin) {
 		struct strbuf buf = STRBUF_INIT;
 		struct strbuf unquoted = STRBUF_INIT;
 
 		setup_work_tree();
 		while (getline_fn(&buf, stdin) != EOF) {
 			char *p;
-			if (!nul_term_line && buf.buf[0] == '"') {
+			if (!cd.nul_term_line && buf.buf[0] == '"') {
 				strbuf_reset(&unquoted);
 				if (unquote_c_style(&unquoted, buf.buf, NULL))
 					die("line is badly quoted");
@@ -1238,7 +1246,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	if (repo->index->cache_changed || force_write) {
 		if (newfd < 0) {
-			if (refresh_args.flags & REFRESH_QUIET)
+			if (cd.flags & REFRESH_QUIET)
 				exit(128);
 			unable_to_lock_die(get_index_file(), lock_error);
 		}
@@ -1248,5 +1256,5 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	rollback_lock_file(&lock_file);
 
-	return has_errors ? 1 : 0;
+	return cd.has_errors ? 1 : 0;
 }
-- 
gitgitgadget
Junio C Hamano· Jan 10, 2021, 07:03 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH v3 00/14] Remove more index compatibility macros

"Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> UPDATE: this is now based on ag/merge-strategies-in-c to avoid conflicts in
> 'seen'. The changes in builtin/rm.c still conflict with
> mt/rm-sparse-checkout, but that branch seems to be waiting for a clearer
> plan on some corner cases. I thought about ejecting it, but 'rm' still uses
> ce_match_stat(), so just dropping the patch gives less of a final stake at
> the end of the series. (I'm still open to it, if necessary.)

I haven't read this latest iteration myself yet beyond the cover letter, but tonight's 'seen' has this queued near its tip. I expect it would either stay there or occasionally ejected, until the base topic solidifies a bit more.

Show 5 quoted lines
>  * Methods that know about the 'repo' pointer no longer also have an
>    'istate' pointer and instead prefer 'repo->index'
>
>  * This includes the callback_data struct which only has a 'repo' member, no
>    'istate'.
OK.
Eric Sunshine· Jan 10, 2021, 07:32 UTC · re: Junio C Hamano · lore

Re: [PATCH v3 00/14] Remove more index compatibility macros

On Sun, Jan 10, 2021 at 2:03 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 20 quoted lines
> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
> > UPDATE: this is now based on ag/merge-strategies-in-c to avoid conflicts in
> > 'seen'. The changes in builtin/rm.c still conflict with
> > mt/rm-sparse-checkout, but that branch seems to be waiting for a clearer
> > plan on some corner cases. I thought about ejecting it, but 'rm' still uses
> > ce_match_stat(), so just dropping the patch gives less of a final stake at
> > the end of the series. (I'm still open to it, if necessary.)
>
> I haven't read this latest iteration myself yet beyond the cover
> letter, but tonight's 'seen' has this queued near its tip.  I expect
> it would either stay there or occasionally ejected, until the base
> topic solidifies a bit more.
>
> >  * Methods that know about the 'repo' pointer no longer also have an
> >    'istate' pointer and instead prefer 'repo->index'
> >
> >  * This includes the callback_data struct which only has a 'repo' member, no
> >    'istate'.
>
> OK.

I looked this version of the series over and did not find anything else about which to comment.

Derrick Stolee· Jan 10, 2021, 11:57 UTC · re: Junio C Hamano · lore

Re: [PATCH v3 00/14] Remove more index compatibility macros

On 1/10/2021 2:03 AM, Junio C Hamano wrote:
Show 13 quoted lines
> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>> UPDATE: this is now based on ag/merge-strategies-in-c to avoid conflicts in
>> 'seen'. The changes in builtin/rm.c still conflict with
>> mt/rm-sparse-checkout, but that branch seems to be waiting for a clearer
>> plan on some corner cases. I thought about ejecting it, but 'rm' still uses
>> ce_match_stat(), so just dropping the patch gives less of a final stake at
>> the end of the series. (I'm still open to it, if necessary.)
> 
> I haven't read this latest iteration myself yet beyond the cover
> letter, but tonight's 'seen' has this queued near its tip.  I expect
> it would either stay there or occasionally ejected, until the base
> topic solidifies a bit more.

Thanks. I'll continue to watch that topic and provide review as new versions come out.

-Stolee
Derrick Stolee· Jan 25, 2021, 13:04 UTC · re: Junio C Hamano · lore

Re: [PATCH v3 00/14] Remove more index compatibility macros

On 1/10/2021 2:03 AM, Junio C Hamano wrote:
Show 13 quoted lines
> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>> UPDATE: this is now based on ag/merge-strategies-in-c to avoid conflicts in
>> 'seen'. The changes in builtin/rm.c still conflict with
>> mt/rm-sparse-checkout, but that branch seems to be waiting for a clearer
>> plan on some corner cases. I thought about ejecting it, but 'rm' still uses
>> ce_match_stat(), so just dropping the patch gives less of a final stake at
>> the end of the series. (I'm still open to it, if necessary.)
> 
> I haven't read this latest iteration myself yet beyond the cover
> letter, but tonight's 'seen' has this queued near its tip.  I expect
> it would either stay there or occasionally ejected, until the base
> topic solidifies a bit more.
Junio,

Please drop this series for now. I'll be introducing a new series soon that will collide with it and this is a lower priority.

I'll probably come back to revisit removing these macros, but I'll do so one builtin at a time when others are not modifying them at the same time.

Thanks, -Stolee

Junio C Hamano· Jan 6, 2021, 03:55 UTC · re: Derrick Stolee via GitGitGadget · lore

Re: [PATCH 00/12] Remove more index compatibility macros

"Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
> My strategy for update-index was to create static globals "repo" and
> "istate" that point to the_repository and the_index, respectively. Then, I
> was able to remove macros one-by-one without changing method prototypes
> within the file.

Knee-jerk reaction: swapping one pair of global with another? Would that give us enough upside? It may allow some codepaths involved to work on an in-core index instance that is different from the_index, but does not make them reentrant.

Do we now have callers that actually pass an in-core index instance that is different from the_index, and more importantly, that fail loudly if the codepaths involved in this conversion forgets to update some accesses to the_index not to the specified one?

If not, ... 
> In total, this allows us to remove four of the compatibility macros because
> they are no longer used.

... a conversion like this, removing the use of the compatibility macros for the sake of removing them, invites future headaches by leaving untested code churn behind with potential bugs that will only get discovered after somebody actually starts making calls with the non-default in-core index instances.

I've come to know the competence of you well enough to trust your patches like patches from other proficient, prolific and prominent contributors (I won't name names, but you know who you are), but we are all human and are prone to introduce bugs.

That's all my knee-jerk impression before actually reading the series through, though. I'll certainloy know more after reading them.

Thanks.
Show 27 quoted lines
> Derrick Stolee (12):
>   merge-index: drop index compatibility macros
>   mv: remove index compatibility macros
>   rm: remove compatilibity macros
>   update-index: drop the_index, the_repository
>   update-index: use istate->cache over active_cache
>   update-index: use index->cache_nr over active_nr
>   update-index: use istate->cache_changed
>   update-index: use index_name_pos() over cache_name_pos()
>   update-index: use remove_file_from_index()
>   update-index: use add_index_entry()
>   update-index: replace several compatibility macros
>   update-index: remove ce_match_stat(), all macros
>
>  Documentation/technical/racy-git.txt |   6 +-
>  builtin/merge-index.c                |  33 +++---
>  builtin/mv.c                         |  42 ++++----
>  builtin/rm.c                         |  56 ++++++-----
>  builtin/update-index.c               | 145 ++++++++++++++-------------
>  cache.h                              |   4 -
>  6 files changed, 149 insertions(+), 137 deletions(-)
>
>
> base-commit: 71ca53e8125e36efbda17293c50027d31681a41f
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-830%2Fderrickstolee%2Findex-compatibility-1-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-830/derrickstolee/index-compatibility-1-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/830
Derrick Stolee· Jan 6, 2021, 11:35 UTC · re: Junio C Hamano · lore

Re: [PATCH 00/12] Remove more index compatibility macros

On 1/5/2021 10:55 PM, Junio C Hamano wrote:
Show 11 quoted lines
> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>> My strategy for update-index was to create static globals "repo" and
>> "istate" that point to the_repository and the_index, respectively. Then, I
>> was able to remove macros one-by-one without changing method prototypes
>> within the file.
> 
> Knee-jerk reaction: swapping one pair of global with another?  Would
> that give us enough upside?  It may allow some codepaths involved to
> work on an in-core index instance that is different from the_index,
> but does not make them reentrant.

My intention was to reduce the use of globals in libgit.a while keeping with existing patterns of static globals in the builtin code. While this can be thought of "module variables" instead of true globals, they aren't exactly desirable. In v2, these static globals are temporary to the series and are completely removed by the end.

The new patch sequence can hopefully be seen as "this preprocessor macro was expanded" and then "static globals are replaced with method parameters" which are pretty straightforward.

Show 15 quoted lines
> Do we now have callers that actually pass an in-core index instance
> that is different from the_index, and more importantly, that fail
> loudly if the codepaths involved in this conversion forgets to
> update some accesses to the_index not to the specified one?
> 
> If not, ... 
> 
>> In total, this allows us to remove four of the compatibility macros because
>> they are no longer used.
> 
> ... a conversion like this, removing the use of the compatibility
> macros for the sake of removing them, invites future headaches by
> leaving untested code churn behind with potential bugs that will
> only get discovered after somebody actually starts making calls
> with the non-default in-core index instances.

Perhaps I had misunderstood the state of the conversion project. I thought that the full conversion was just paused because Duy moved on to other things. I thought it might be valuable to pick up the baton while also thinking about the space.

If this is _not_ a valuable project to continue, then I can hold off for now.

Unfortunately, we'll never know if everything is safe from assuming the_index until the macro itself is gone. It helps that libgit.a doesn't use it at

> I've come to know the competence of you well enough to trust your
> patches like patches from other proficient, prolific and prominent
> contributors (I won't name names, but you know who you are), but we
> are all human and are prone to introduce bugs.

That means a lot, thanks. And yes, I'm well aware that bugs can be introduced. I've added my share.

> That's all my knee-jerk impression before actually reading the
> series through, though.  I'll certainloy know more after reading
> them.
I look forward to your thoughts.

Thanks, -Stolee

Junio C Hamano· Jan 6, 2021, 20:52 UTC · re: Derrick Stolee · lore

Re: [PATCH 00/12] Remove more index compatibility macros

Derrick Stolee <stolee@gmail.com> writes:
> My intention was to reduce the use of globals in libgit.a while keeping
> with existing patterns of static globals in the builtin code.

The above (without having thought about it too hard or long enough yet) sounds a sensible guideline to go by. Thanks for a helpful hint to keep in mind while reading the series.

> While
> this can be thought of "module variables" instead of true globals, they
> aren't exactly desirable. In v2, these static globals are temporary to
> the series and are completely removed by the end.
;-)

← back to recent threads