cocci & cache.h: remove rarely used "the_index" compat macros

Since 4aab5b46f44 (Make read-cache.c "the_index" free., 2007-04-01) we've been undergoing a slow migration away from these macros, but haven't made much progress since f8adbec9fea (cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch, 2019-01-24). Let's move forward a bit by changing the users of those macros that are rare enough that we can convert them in one go, and then remove the compatibility shim. The only manual change to the C code here is to "cache.h", the rest is all the result of applying the new "index-compatibility.cocci". Even though it's a one-off, let's keep the coccinelle rules for now. We'll extend them in subsequent commits, and this will help anything that's in-flight or out-of-tree to migrate. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Nov 19, 2022 at 14:07 UTC fbc1ed629e3c71b47a04ab86b88497984bc6acbb
13 files changed +65 -29
builtin/add.c
+3 -2
@@ -55,7 +55,7 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
55 continue;
56
57 if (!show_only)
58 - err = chmod_cache_entry(ce, flip);
58 + err = chmod_index_entry(&the_index, ce, flip);
59 else
60 err = S_ISREG(ce->ce_mode) ? 0 : -1;
61
@@ -172,7 +172,8 @@ static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
172 continue; /* do not touch non blobs */
173 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
174 continue;
175 - retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
175 + retval |= add_file_to_index(&the_index, ce->name,
176 + flags | ADD_CACHE_RENORMALIZE);
177 }
178
179 return retval;
builtin/am.c
+2 -2
@@ -1930,7 +1930,7 @@ static void am_resolve(struct am_state *state, int allow_empty)
1930 }
1931 }
1932
1933 - if (unmerged_cache()) {
1933 + if (unmerged_index(&the_index)) {
1934 printf_ln(_("You still have unmerged paths in your index.\n"
1935 "You should 'git add' each file with resolved conflicts to mark them as such.\n"
1936 "You might run `git rm` on a file to accept \"deleted by them\" for it."));
@@ -2045,7 +2045,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
2045 if (!remote_tree)
2046 return error(_("Could not parse object '%s'."), oid_to_hex(remote));
2047
2048 - read_cache_unmerged();
2048 + repo_read_index_unmerged(the_repository);
2049
2050 if (fast_forward_to(head_tree, head_tree, 1))
2051 return -1;
builtin/checkout.c
+2 -2
@@ -722,7 +722,7 @@ static void init_topts(struct unpack_trees_options *topts, int merge,
722
723 setup_unpack_trees_porcelain(topts, "checkout");
724
725 - topts->initial_checkout = is_cache_unborn();
725 + topts->initial_checkout = is_index_unborn(&the_index);
726 topts->update = 1;
727 topts->merge = 1;
728 topts->quiet = merge && old_commit;
@@ -763,7 +763,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
763
764 refresh_cache(REFRESH_QUIET);
765
766 - if (unmerged_cache()) {
766 + if (unmerged_index(&the_index)) {
767 error(_("you need to resolve your current index first"));
768 return 1;
769 }
builtin/clean.c
+1 -1
@@ -1031,7 +1031,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
1031 struct stat st;
1032 const char *rel;
1033
1034 - if (!cache_name_is_other(ent->name, ent->len))
1034 + if (!index_name_is_other(&the_index, ent->name, ent->len))
1035 continue;
1036
1037 if (lstat(ent->name, &st))
builtin/commit.c
+1 -1
@@ -302,7 +302,7 @@ static void add_remove_files(struct string_list *list)
302 continue;
303
304 if (!lstat(p->string, &st)) {
305 - if (add_to_cache(p->string, &st, 0))
305 + if (add_to_index(&the_index, p->string, &st, 0))
306 die(_("updating files failed"));
307 } else
308 remove_file_from_cache(p->string);
builtin/merge.c
+1 -1
@@ -1375,7 +1375,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1375 goto done;
1376 }
1377
1378 - if (read_cache_unmerged())
1378 + if (repo_read_index_unmerged(the_repository))
1379 die_resolve_conflict("merge");
1380
1381 if (file_exists(git_path_merge_head(the_repository))) {
builtin/mv.c
+2 -2
@@ -343,7 +343,7 @@ dir_check:
343 argc += last - first;
344 goto act_on_entry;
345 }
346 - if (!(ce = cache_file_exists(src, length, 0))) {
346 + if (!(ce = index_file_exists(&the_index, src, length, 0))) {
347 bad = _("not under version control");
348 goto act_on_entry;
349 }
@@ -472,7 +472,7 @@ remove_entry:
472 assert(pos >= 0);
473 if (!(mode & SPARSE) && !lstat(src, &st))
474 sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
475 - rename_cache_entry_at(pos, dst);
475 + rename_index_entry_at(&the_index, pos, dst);
476
477 if (ignore_sparse &&
478 core_apply_sparse_checkout &&
builtin/pull.c
+2 -2
@@ -1030,7 +1030,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1030 if (opt_rebase < 0)
1031 opt_rebase = config_get_rebase(&rebase_unspecified);
1032
1033 - if (read_cache_unmerged())
1033 + if (repo_read_index_unmerged(the_repository))
1034 die_resolve_conflict("pull");
1035
1036 if (file_exists(git_path_merge_head(the_repository)))
@@ -1043,7 +1043,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1043 if (opt_autostash == -1)
1044 opt_autostash = config_autostash;
1045
1046 - if (is_null_oid(&orig_head) && !is_cache_unborn())
1046 + if (is_null_oid(&orig_head) && !is_index_unborn(&the_index))
1047 die(_("Updating an unborn branch with changes added to the index."));
1048
1049 if (!opt_autostash)
builtin/read-tree.c
+2 -2
@@ -188,7 +188,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
188 */
189
190 if (opts.reset || opts.merge || opts.prefix) {
191 - if (read_cache_unmerged() && (opts.prefix || opts.merge))
191 + if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
192 die(_("You need to resolve your current index first"));
193 stage = opts.merge = 1;
194 }
@@ -232,7 +232,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
232 break;
233 case 2:
234 opts.fn = twoway_merge;
235 - opts.initial_checkout = is_cache_unborn();
235 + opts.initial_checkout = is_index_unborn(&the_index);
236 break;
237 case 3:
238 default:
builtin/reset.c
+2 -2
@@ -84,7 +84,7 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t
84 BUG("invalid reset_type passed to reset_index");
85 }
86
87 - read_cache_unmerged();
87 + repo_read_index_unmerged(the_repository);
88
89 if (reset_type == KEEP) {
90 struct object_id head_oid;
@@ -220,7 +220,7 @@ static void set_reflog_message(struct strbuf *sb, const char *action,
220
221 static void die_if_unmerged_cache(int reset_type)
222 {
223 - if (is_merge() || unmerged_cache())
223 + if (is_merge() || unmerged_index(&the_index))
224 die(_("Cannot do a %s reset in the middle of a merge."),
225 _(reset_type_names[reset_type]));
226
builtin/update-index.c
+2 -2
@@ -445,7 +445,7 @@ static void chmod_path(char flip, const char *path)
445 if (pos < 0)
446 goto fail;
447 ce = active_cache[pos];
448 - if (chmod_cache_entry(ce, flip) < 0)
448 + if (chmod_index_entry(&the_index, ce, flip) < 0)
449 goto fail;
450
451 report("chmod %cx '%s'", flip, path);
@@ -641,7 +641,7 @@ static int unresolve_one(const char *path)
641 pos = cache_name_pos(path, namelen);
642 if (0 <= pos) {
643 /* already merged */
644 - pos = unmerge_cache_entry_at(pos);
644 + pos = unmerge_index_entry_at(&the_index, pos);
645 if (pos < active_nr) {
646 const struct cache_entry *ce = active_cache[pos];
647 if (ce_stage(ce) &&
cache.h
-10
@@ -444,25 +444,15 @@ extern struct index_state the_index;
444 #define read_cache() repo_read_index(the_repository)
445 #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
446 #define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
447 -#define is_cache_unborn() is_index_unborn(&the_index)
448 -#define read_cache_unmerged() repo_read_index_unmerged(the_repository)
447 #define discard_cache() discard_index(&the_index)
450 -#define unmerged_cache() unmerged_index(&the_index)
448 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
449 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
453 -#define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
450 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
455 -#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
456 -#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
457 -#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
451 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
452 #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)
453 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
454 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
462 -#define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
463 -#define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
455 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
465 -#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
456 #define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
457 #endif
458
contrib/coccinelle/index-compatibility.cocci new
+45
@@ -0,0 +1,45 @@
1 +// "the_repository" simple cases
2 +@@
3 +@@
4 +(
5 +- read_cache_unmerged
6 ++ repo_read_index_unmerged
7 +)
8 + (
9 ++ the_repository,
10 + ...)
11 +
12 +// "the_index" simple cases
13 +@@
14 +@@
15 +(
16 +- is_cache_unborn
17 ++ is_index_unborn
18 +|
19 +- unmerged_cache
20 ++ unmerged_index
21 +|
22 +- rename_cache_entry_at
23 ++ rename_index_entry_at
24 +|
25 +- chmod_cache_entry
26 ++ chmod_index_entry
27 +|
28 +- cache_file_exists
29 ++ index_file_exists
30 +|
31 +- cache_name_is_other
32 ++ index_name_is_other
33 +|
34 +- unmerge_cache_entry_at
35 ++ unmerge_index_entry_at
36 +|
37 +- add_to_cache
38 ++ add_to_index
39 +|
40 +- add_file_to_cache
41 ++ add_file_to_index
42 +)
43 + (
44 ++ &the_index,
45 + ...)