Merge branch 'ab/the-index-compatibility'

Remove more remaining uses of macros that relies on the_index
singleton instance without explicitly spelling it out.

* ab/the-index-compatibility:
  cocci & cache.h: remove "USE_THE_INDEX_COMPATIBILITY_MACROS"
  cache-tree API: remove redundant update_main_cache_tree()
  cocci & cache-tree.h: migrate "write_cache_as_tree" to "*_index_*"
  cocci & cache.h: apply pending "index_cache_pos" rule
  cocci & cache.h: fully apply "active_nr" part of index-compatibility
  builtin/rm.c: use narrower "USE_THE_INDEX_VARIABLE"
This commit is contained in:
Junio C Hamano 2023-02-22 14:55:44 -08:00
commit 24fb150dcd
12 changed files with 62 additions and 87 deletions

View File

@ -3,7 +3,7 @@
*
* Based on git-am.sh by Junio C Hamano.
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "config.h"
#include "builtin.h"
@ -1655,7 +1655,7 @@ static void do_commit(const struct am_state *state)
if (!state->no_verify && run_hooks("pre-applypatch"))
exit(1);
if (write_cache_as_tree(&tree, 0, NULL))
if (write_index_as_tree(&tree, &the_index, get_index_file(), 0, NULL))
die(_("git write-tree failed to write a tree"));
if (!get_oid_commit("HEAD", &parent)) {
@ -2063,7 +2063,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
if (fast_forward_to(head_tree, head_tree, 1))
return -1;
if (write_cache_as_tree(&index, 0, NULL))
if (write_index_as_tree(&index, &the_index, get_index_file(), 0, NULL))
return -1;
index_tree = parse_tree_indirect(&index);

View File

@ -5,7 +5,7 @@
* Based on git-commit.sh by Junio C Hamano and Linus Torvalds
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "config.h"
#include "lockfile.h"
@ -414,7 +414,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
discard_index(&the_index);
read_index_from(&the_index, get_lock_file_path(&index_lock),
get_git_dir());
if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) {
if (reopen_lock_file(&index_lock) < 0)
die(_("unable to write index file"));
if (write_locked_index(&the_index, &index_lock, 0))
@ -444,7 +444,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
LOCK_DIE_ON_ERROR);
add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
refresh_cache_or_die(refresh_flags);
update_main_cache_tree(WRITE_TREE_SILENT);
cache_tree_update(&the_index, WRITE_TREE_SILENT);
if (write_locked_index(&the_index, &index_lock, 0))
die(_("unable to write new_index file"));
commit_style = COMMIT_NORMAL;
@ -467,7 +467,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
refresh_cache_or_die(refresh_flags);
if (the_index.cache_changed
|| !cache_tree_fully_valid(the_index.cache_tree))
update_main_cache_tree(WRITE_TREE_SILENT);
cache_tree_update(&the_index, WRITE_TREE_SILENT);
if (write_locked_index(&the_index, &index_lock,
COMMIT_LOCK | SKIP_IF_UNCHANGED))
die(_("unable to write new_index file"));
@ -516,7 +516,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
add_remove_files(&partial);
refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
update_main_cache_tree(WRITE_TREE_SILENT);
cache_tree_update(&the_index, WRITE_TREE_SILENT);
if (write_locked_index(&the_index, &index_lock, 0))
die(_("unable to write new_index file"));
@ -991,9 +991,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
struct object_id oid;
const char *parent = "HEAD";
if (!active_nr) {
discard_cache();
if (read_cache() < 0)
if (!the_index.cache_nr) {
discard_index(&the_index);
if (repo_read_index(the_repository) < 0)
die(_("Cannot read index"));
}
@ -1079,7 +1079,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
}
read_index_from(&the_index, index_file, get_git_dir());
if (update_main_cache_tree(0)) {
if (cache_tree_update(&the_index, 0)) {
error(_("Error building trees"));
return 0;
}

View File

@ -6,7 +6,7 @@
* Based on git-merge.sh by Junio C Hamano.
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "config.h"
#include "parse-options.h"
@ -390,8 +390,8 @@ static void restore_state(const struct object_id *head,
run_command(&cmd);
refresh_cache:
discard_cache();
if (read_cache() < 0)
discard_index(&the_index);
if (repo_read_index(the_repository) < 0)
die(_("could not read index"));
}
@ -706,7 +706,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
static void write_tree_trivial(struct object_id *oid)
{
if (write_cache_as_tree(oid, 0, NULL))
if (write_index_as_tree(oid, &the_index, get_index_file(), 0, NULL))
die(_("git write-tree failed to write a tree"));
}

View File

@ -3,7 +3,7 @@
*
* Copyright (C) 2006 Johannes Schindelin
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "config.h"
#include "pathspec.h"
@ -489,7 +489,8 @@ remove_entry:
if ((mode & SPARSE) &&
path_in_sparse_checkout(dst, &the_index)) {
/* from out-of-cone to in-cone */
int dst_pos = cache_name_pos(dst, strlen(dst));
int dst_pos = index_name_pos(&the_index, dst,
strlen(dst));
struct cache_entry *dst_ce = the_index.cache[dst_pos];
dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
@ -500,7 +501,8 @@ remove_entry:
!(mode & SPARSE) &&
!path_in_sparse_checkout(dst, &the_index)) {
/* from in-cone to out-of-cone */
int dst_pos = cache_name_pos(dst, strlen(dst));
int dst_pos = index_name_pos(&the_index, dst,
strlen(dst));
struct cache_entry *dst_ce = the_index.cache[dst_pos];
/*

View File

@ -3,7 +3,7 @@
*
* Copyright (C) Linus Torvalds 2006
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "advice.h"
#include "config.h"

View File

@ -1,4 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "config.h"
#include "parse-options.h"
@ -529,7 +529,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
NULL, NULL, NULL))
return -1;
if (write_cache_as_tree(&c_tree, 0, NULL))
if (write_index_as_tree(&c_tree, &the_index, get_index_file(), 0,
NULL))
return error(_("cannot apply a stash in the middle of a merge"));
if (index) {
@ -553,7 +554,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
discard_index(&the_index);
repo_read_index(the_repository);
if (write_cache_as_tree(&index_tree, 0, NULL))
if (write_index_as_tree(&index_tree, &the_index,
get_index_file(), 0, NULL))
return error(_("could not save index tree"));
reset_head();
@ -1378,7 +1380,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
commit_list_insert(head_commit, &parents);
if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
if (write_index_as_tree(&info->i_tree, &the_index, get_index_file(), 0,
NULL) ||
commit_tree(commit_tree_label.buf, commit_tree_label.len,
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
if (!quiet)

View File

@ -3,7 +3,7 @@
*
* Copyright (C) Linus Torvalds, 2005
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "bulk-checkin.h"
#include "config.h"
@ -381,7 +381,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(&the_index, path, len);
ce = pos < 0 ? NULL : the_index.cache[pos];
if (ce && ce_skip_worktree(ce)) {
/*

View File

@ -3,7 +3,7 @@
*
* Copyright (C) Linus Torvalds, 2005
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "cache.h"
#include "config.h"
@ -38,7 +38,8 @@ int cmd_write_tree(int argc, const char **argv, const char *cmd_prefix)
argc = parse_options(argc, argv, cmd_prefix, write_tree_options,
write_tree_usage, 0);
ret = write_cache_as_tree(&oid, flags, tree_prefix);
ret = write_index_as_tree(&oid, &the_index, get_index_file(), flags,
tree_prefix);
switch (ret) {
case 0:
printf("%s\n", oid_to_hex(&oid));

View File

@ -53,19 +53,4 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
void prime_cache_tree(struct repository *, struct index_state *, struct tree *);
int cache_tree_matches_traversal(struct cache_tree *, struct name_entry *ent, struct traverse_info *info);
#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
static inline int write_cache_as_tree(struct object_id *oid, int flags, const char *prefix)
{
return write_index_as_tree(oid, &the_index, get_index_file(), flags, prefix);
}
static inline int update_main_cache_tree(int flags)
{
if (!the_index.cache_tree)
the_index.cache_tree = cache_tree();
return cache_tree_update(&the_index, flags);
}
#endif
#endif

12
cache.h
View File

@ -449,18 +449,8 @@ typedef int (*must_prefetch_predicate)(const struct cache_entry *);
void prefetch_cache_entries(const struct index_state *istate,
must_prefetch_predicate must_prefetch);
#if defined(USE_THE_INDEX_COMPATIBILITY_MACROS) || defined(USE_THE_INDEX_VARIABLE)
#ifdef USE_THE_INDEX_VARIABLE
extern struct index_state the_index;
#ifndef USE_THE_INDEX_VARIABLE
#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
#define active_nr (the_index.cache_nr)
#define read_cache() repo_read_index(the_repository)
#define discard_cache() discard_index(&the_index)
#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
#endif
#endif
#endif
#define TYPE_BITS 3

View File

@ -1,6 +1,7 @@
// the_index.* variables
@@
identifier AC = active_cache;
identifier AN = active_nr;
identifier ACC = active_cache_changed;
identifier ACT = active_cache_tree;
@@
@ -8,6 +9,9 @@ identifier ACT = active_cache_tree;
- AC
+ the_index.cache
|
- AN
+ the_index.cache_nr
|
- ACC
+ the_index.cache_changed
|
@ -15,19 +19,13 @@ identifier ACT = active_cache_tree;
+ the_index.cache_tree
)
@@
identifier AN = active_nr;
identifier f != prepare_to_commit;
@@
f(...) {<...
- AN
+ the_index.cache_nr
...>}
// "the_repository" simple cases
@@
@@
(
- read_cache
+ repo_read_index
|
- read_cache_unmerged
+ repo_read_index_unmerged
|
@ -96,6 +94,15 @@ identifier f != prepare_to_commit;
|
- resolve_undo_clear
+ resolve_undo_clear_index
|
- cache_name_pos
+ index_name_pos
|
- update_main_cache_tree
+ cache_tree_update
|
- discard_cache
+ discard_index
)
(
+ &the_index,
@ -137,3 +144,14 @@ identifier f != prepare_to_commit;
...
+ , NULL, NULL, NULL
)
@@
expression O;
@@
- write_cache_as_tree
+ write_index_as_tree
(
- O,
+ O, &the_index, get_index_file(),
...
)

View File

@ -1,24 +0,0 @@
// "the_repository" simple cases
@@
@@
(
- read_cache
+ repo_read_index
)
(
+ the_repository,
...)
// "the_index" simple cases
@@
@@
(
- discard_cache
+ discard_index
|
- cache_name_pos
+ index_name_pos
)
(
+ &the_index,
...)