cocci & cache-tree.h: migrate "write_cache_as_tree" to "*_index_*"

Add a trivial rule for "write_cache_as_tree" to "index-compatibility.cocci", and apply it. This was left out of the rules added in 0e6550a2c63 (cocci: add a index-compatibility.pending.cocci, 2022-11-19) because this compatibility wrapper lived in "cache-tree.h", not "cache.h" But it's like the other "USE_THE_INDEX_COMPATIBILITY_MACROS", so let's migrate it too. The replacement of "USE_THE_INDEX_COMPATIBILITY_MACROS" here with "USE_THE_INDEX_VARIABLE" is a manual change on top, now that these files only use "&the_index", and don't need any compatibility macros (or functions). The wrapping of some argument lists is likewise manual, as coccinelle would otherwise give us overly long argument lists. The reason for putting the "O" in the cocci rule on the "-" and "+" lines is because I couldn't get correct whitespacing otherwise, i.e. I'd end up with "oid,&the_index", not "oid, &the_index". Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 10, 2023 at 11:28 UTC 99370863e20131627f0ea0481b7006d0d2125c93
6 files changed +25 -15
builtin/am.c
+3 -3
@@ -3,7 +3,7 @@
3 *
4 * Based on git-am.sh by Junio C Hamano.
5 */
6 -#define USE_THE_INDEX_COMPATIBILITY_MACROS
6 +#define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "config.h"
9 #include "builtin.h"
@@ -1655,7 +1655,7 @@ static void do_commit(const struct am_state *state)
1655 if (!state->no_verify && run_hooks("pre-applypatch"))
1656 exit(1);
1657
1658 - if (write_cache_as_tree(&tree, 0, NULL))
1658 + if (write_index_as_tree(&tree, &the_index, get_index_file(), 0, NULL))
1659 die(_("git write-tree failed to write a tree"));
1660
1661 if (!get_oid_commit("HEAD", &parent)) {
@@ -2063,7 +2063,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
2063 if (fast_forward_to(head_tree, head_tree, 1))
2064 return -1;
2065
2066 - if (write_cache_as_tree(&index, 0, NULL))
2066 + if (write_index_as_tree(&index, &the_index, get_index_file(), 0, NULL))
2067 return -1;
2068
2069 index_tree = parse_tree_indirect(&index);
builtin/merge.c
+1 -1
@@ -706,7 +706,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
706
707 static void write_tree_trivial(struct object_id *oid)
708 {
709 - if (write_cache_as_tree(oid, 0, NULL))
709 + if (write_index_as_tree(oid, &the_index, get_index_file(), 0, NULL))
710 die(_("git write-tree failed to write a tree"));
711 }
712
builtin/stash.c
+7 -4
@@ -1,4 +1,4 @@
1 -#define USE_THE_INDEX_COMPATIBILITY_MACROS
1 +#define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "parse-options.h"
@@ -528,7 +528,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
528 NULL, NULL, NULL))
529 return -1;
530
531 - if (write_cache_as_tree(&c_tree, 0, NULL))
531 + if (write_index_as_tree(&c_tree, &the_index, get_index_file(), 0,
532 + NULL))
533 return error(_("cannot apply a stash in the middle of a merge"));
534
535 if (index) {
@@ -552,7 +553,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
553
554 discard_index(&the_index);
555 repo_read_index(the_repository);
555 - if (write_cache_as_tree(&index_tree, 0, NULL))
556 + if (write_index_as_tree(&index_tree, &the_index,
557 + get_index_file(), 0, NULL))
558 return error(_("could not save index tree"));
559
560 reset_head();
@@ -1377,7 +1379,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
1379
1380 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1381 commit_list_insert(head_commit, &parents);
1380 - if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1382 + if (write_index_as_tree(&info->i_tree, &the_index, get_index_file(), 0,
1383 + NULL) ||
1384 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1385 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1386 if (!quiet)
builtin/write-tree.c
+3 -2
@@ -3,7 +3,7 @@
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 -#define USE_THE_INDEX_COMPATIBILITY_MACROS
6 +#define USE_THE_INDEX_VARIABLE
7 #include "builtin.h"
8 #include "cache.h"
9 #include "config.h"
@@ -38,7 +38,8 @@ int cmd_write_tree(int argc, const char **argv, const char *cmd_prefix)
38 argc = parse_options(argc, argv, cmd_prefix, write_tree_options,
39 write_tree_usage, 0);
40
41 - ret = write_cache_as_tree(&oid, flags, tree_prefix);
41 + ret = write_index_as_tree(&oid, &the_index, get_index_file(), flags,
42 + tree_prefix);
43 switch (ret) {
44 case 0:
45 printf("%s\n", oid_to_hex(&oid));
cache-tree.h
-5
@@ -55,11 +55,6 @@ void prime_cache_tree(struct repository *, struct index_state *, struct tree *);
55 int cache_tree_matches_traversal(struct cache_tree *, struct name_entry *ent, struct traverse_info *info);
56
57 #ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
58 -static inline int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix)
59 -{
60 - return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix);
61 -}
62 -
58 static inline int update_main_cache_tree(int flags)
59 {
60 if (!the_index.cache_tree)
contrib/coccinelle/index-compatibility.cocci
+11
@@ -135,3 +135,14 @@ identifier ACT = active_cache_tree;
135 ...
136 + , NULL, NULL, NULL
137 )
138 +
139 +@@
140 +expression O;
141 +@@
142 +- write_cache_as_tree
143 ++ write_index_as_tree
144 + (
145 +- O,
146 ++ O, &the_index, get_index_file(),
147 + ...
148 + )