--- v3
+++ v1
@@ -1,226 +1,195 @@
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.
+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/update-index.c | 59 +++++++++++++++++++++++-------------------
- 1 file changed, 32 insertions(+), 27 deletions(-)
+ builtin/rm.c | 56 ++++++++++++++++++++++++++++------------------------
+ 1 file changed, 30 insertions(+), 26 deletions(-)
-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;
+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 struct index_state *istate;
-+
- static int mark_ce_flags(const char *path, int flag, int mark)
+-static void submodules_absorb_gitdir_if_needed(void)
++static void submodules_absorb_gitdir_if_needed(struct index_state *istate)
{
- 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;
+ 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)
}
-@@ -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);
+-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;
-- 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;
+- 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;
-- 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[] = {
+- 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];
- 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)
+ 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);
-+ 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;
+@@ -284,24 +287,25 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
+ if (!index_only)
+ setup_work_tree();
-- entries = read_cache();
-+ entries = repo_read_index(repo);
- if (entries < 0)
- die("cache corrupted");
+- hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
++ repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
-- the_index.updated_skipworktree = 1;
-+ istate = repo->index;
-+ repo->index->updated_skipworktree = 1;
+- 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);
/*
- * 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 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);
}
- 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);
+@@ -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);
}
-- 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"));
+@@ -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);
}
-@@ -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");
- }
+- 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