Skip to content

Commit

Permalink
cocci: apply "pending" index-compatibility to some "builtin/*.c"
Browse files Browse the repository at this point in the history
Apply "index-compatibility.pending.cocci" rule to "builtin/*", but
exclude those where we conflict with in-flight changes.

As a result some of them end up using only "the_index", so let's have
them use the more narrow "USE_THE_INDEX_VARIABLE" rather than
"USE_THE_INDEX_COMPATIBILITY_MACROS".

Manual changes not made by coccinelle, that were squashed in:

* Whitespace-wrap argument lists for repo_hold_locked_index(),
  repo_read_index_preload() and repo_refresh_and_write_index(), in cases
  where the line became too long after the transformation.
* Change "refresh_cache()" to "refresh_index()" in a comment in
  "builtin/update-index.c".
* For those whose call was followed by perror("<macro-name>"), change
  it to perror("<function-name>"), referring to the new function.

Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
avar authored and gitster committed Nov 21, 2022
1 parent bdafeae commit 07047d6
Show file tree
Hide file tree
Showing 30 changed files with 213 additions and 202 deletions.
8 changes: 4 additions & 4 deletions builtin/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright (C) 2006 Linus Torvalds
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "config.h"
#include "builtin.h"
Expand Down Expand Up @@ -312,7 +312,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)

git_config(git_diff_basic_config, NULL); /* no "diff" UI options */

if (read_cache() < 0)
if (repo_read_index(the_repository) < 0)
die(_("Could not read the index"));

repo_init_revisions(the_repository, &rev, prefix);
Expand Down Expand Up @@ -544,7 +544,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);

/*
* Check the "pathspec '%s' did not match any files" block
Expand Down Expand Up @@ -587,7 +587,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
(!(addremove || take_worktree_changes)
? ADD_CACHE_IGNORE_REMOVAL : 0));

if (read_cache_preload(&pathspec) < 0)
if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
die(_("index file corrupt"));

die_in_unpopulated_submodule(&the_index, prefix);
Expand Down
21 changes: 11 additions & 10 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -1519,8 +1519,8 @@ static int run_apply(const struct am_state *state, const char *index_file)

if (index_file) {
/* Reload index as apply_all_patches() will have modified it. */
discard_cache();
read_cache_from(index_file);
discard_index(&the_index);
read_index_from(&the_index, index_file, get_git_dir());
}

return 0;
Expand Down Expand Up @@ -1562,8 +1562,8 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
if (build_fake_ancestor(state, index_path))
return error("could not build fake ancestor");

discard_cache();
read_cache_from(index_path);
discard_index(&the_index);
read_index_from(&the_index, index_path, get_git_dir());

if (write_index_as_tree(&orig_tree, &the_index, index_path, 0, NULL))
return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
Expand Down Expand Up @@ -1596,8 +1596,8 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa

say(state, stdout, _("Falling back to patching base and 3-way merge..."));

discard_cache();
read_cache();
discard_index(&the_index);
repo_read_index(the_repository);

/*
* This is not so wrong. Depending on which base we picked, orig_tree
Expand Down Expand Up @@ -1781,7 +1781,8 @@ static void am_run(struct am_state *state, int resume)

unlink(am_path(state, "dirtyindex"));

if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0)
if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
NULL, NULL, NULL) < 0)
die(_("unable to write index file"));

if (repo_index_has_changes(the_repository, NULL, &sb)) {
Expand Down Expand Up @@ -1967,9 +1968,9 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
if (parse_tree(head) || parse_tree(remote))
return -1;

hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);

refresh_cache(REFRESH_QUIET);
refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);

memset(&opts, 0, sizeof(opts));
opts.head_idx = 1;
Expand Down Expand Up @@ -2007,7 +2008,7 @@ static int merge_tree(struct tree *tree)
if (parse_tree(tree))
return -1;

hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);

memset(&opts, 0, sizeof(opts));
opts.head_idx = 1;
Expand Down
4 changes: 2 additions & 2 deletions builtin/check-attr.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "cache.h"
#include "config.h"
Expand Down Expand Up @@ -115,7 +115,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, check_attr_options,
check_attr_usage, PARSE_OPT_KEEP_DASHDASH);

if (read_cache() < 0) {
if (repo_read_index(the_repository) < 0) {
die("invalid cache");
}

Expand Down
4 changes: 2 additions & 2 deletions builtin/check-ignore.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "cache.h"
#include "config.h"
Expand Down Expand Up @@ -179,7 +179,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
die(_("--non-matching is only valid with --verbose"));

/* read_cache() is only necessary so we can watch out for submodules. */
if (!no_index && read_cache() < 0)
if (!no_index && repo_read_index(the_repository) < 0)
die(_("index file corrupt"));

setup_standard_excludes(&dir);
Expand Down
9 changes: 5 additions & 4 deletions builtin/checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (C) 2005 Linus Torvalds
*
*/
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "config.h"
#include "dir.h"
Expand Down Expand Up @@ -65,7 +65,7 @@ static void write_tempfile_record(const char *name, const char *prefix)
static int checkout_file(const char *name, const char *prefix)
{
int namelen = strlen(name);
int pos = cache_name_pos(name, namelen);
int pos = index_name_pos(&the_index, name, namelen);
int has_same_name = 0;
int is_file = 0;
int is_skipped = 1;
Expand Down Expand Up @@ -249,7 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

if (read_cache() < 0) {
if (repo_read_index(the_repository) < 0) {
die("invalid cache");
}

Expand All @@ -270,7 +270,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
if (index_opt && !state.base_dir_len && !to_tempfile) {
state.refresh_cache = 1;
state.istate = &the_index;
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &lock_file,
LOCK_DIE_ON_ERROR);
}

get_parallel_checkout_configs(&pc_workers, &pc_threshold);
Expand Down
12 changes: 6 additions & 6 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "advice.h"
#include "blob.h"
Expand Down Expand Up @@ -148,7 +148,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
* entry in place. Whether it is UPTODATE or not, checkout_entry will
* do the right thing.
*/
pos = cache_name_pos(ce->name, ce->ce_namelen);
pos = index_name_pos(&the_index, ce->name, ce->ce_namelen);
if (pos >= 0) {
struct cache_entry *old = the_index.cache[pos];
if (ce->ce_mode == old->ce_mode &&
Expand Down Expand Up @@ -529,7 +529,7 @@ static int checkout_paths(const struct checkout_opts *opts,
}

repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
if (read_cache_preload(&opts->pathspec) < 0)
if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0)
return error(_("index file corrupt"));

if (opts->source_tree)
Expand Down Expand Up @@ -741,8 +741,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
struct lock_file lock_file = LOCK_INIT;
struct tree *new_tree;

hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
if (read_cache_preload(NULL) < 0)
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
if (repo_read_index_preload(the_repository, NULL, 0) < 0)
return error(_("index file corrupt"));

resolve_undo_clear_index(&the_index);
Expand All @@ -762,7 +762,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
struct unpack_trees_options topts;
const struct object_id *old_commit_oid;

refresh_cache(REFRESH_QUIET);
refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);

if (unmerged_index(&the_index)) {
error(_("you need to resolve your current index first"));
Expand Down
4 changes: 2 additions & 2 deletions builtin/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Based on git-clean.sh by Pavel Roskin
*/

#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "cache.h"
#include "config.h"
Expand Down Expand Up @@ -1012,7 +1012,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

if (read_cache() < 0)
if (repo_read_index(the_repository) < 0)
die(_("index file corrupt"));

pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
Expand Down
4 changes: 2 additions & 2 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Clone a repository into a different directory that does not yet exist.
*/

#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "config.h"
#include "lockfile.h"
Expand Down Expand Up @@ -703,7 +703,7 @@ static int checkout(int submodule_progress, int filter_submodules)
/* We need to be in the new work tree for the checkout */
setup_work_tree();

hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);

memset(&opts, 0, sizeof opts);
opts.update = 1;
Expand Down
40 changes: 22 additions & 18 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static void create_base_index(const struct commit *current_head)
struct tree_desc t;

if (!current_head) {
discard_cache();
discard_index(&the_index);
return;
}

Expand All @@ -343,7 +343,7 @@ static void refresh_cache_or_die(int refresh_flags)
* refresh_flags contains REFRESH_QUIET, so the only errors
* are for unmerged entries.
*/
if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
die_resolve_conflict("commit");
}

Expand Down Expand Up @@ -382,12 +382,13 @@ static const char *prepare_index(const char **argv, const char *prefix,
(!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
die(_("No paths with --include/--only does not make sense."));

if (read_cache_preload(&pathspec) < 0)
if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
die(_("index file corrupt"));

if (interactive) {
char *old_index_env = NULL, *old_repo_index_file;
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &index_lock,
LOCK_DIE_ON_ERROR);

refresh_cache_or_die(refresh_flags);

Expand All @@ -410,8 +411,9 @@ static const char *prepare_index(const char **argv, const char *prefix,
unsetenv(INDEX_ENVIRONMENT);
FREE_AND_NULL(old_index_env);

discard_cache();
read_cache_from(get_lock_file_path(&index_lock));
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 (reopen_lock_file(&index_lock) < 0)
die(_("unable to write index file"));
Expand All @@ -438,7 +440,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
* (B) on failure, rollback the real index.
*/
if (all || (also && pathspec.nr)) {
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &index_lock,
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);
Expand All @@ -459,7 +462,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
* We still need to refresh the index here.
*/
if (!only && !pathspec.nr) {
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &index_lock,
LOCK_DIE_ON_ERROR);
refresh_cache_or_die(refresh_flags);
if (the_index.cache_changed
|| !cache_tree_fully_valid(the_index.cache_tree))
Expand Down Expand Up @@ -505,13 +509,13 @@ static const char *prepare_index(const char **argv, const char *prefix,
if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
exit(1);

discard_cache();
if (read_cache() < 0)
discard_index(&the_index);
if (repo_read_index(the_repository) < 0)
die(_("cannot read the index"));

hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
add_remove_files(&partial);
refresh_cache(REFRESH_QUIET);
refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
update_main_cache_tree(WRITE_TREE_SILENT);
if (write_locked_index(&the_index, &index_lock, 0))
die(_("unable to write new_index file"));
Expand All @@ -523,14 +527,14 @@ static const char *prepare_index(const char **argv, const char *prefix,

create_base_index(current_head);
add_remove_files(&partial);
refresh_cache(REFRESH_QUIET);
refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);

if (write_locked_index(&the_index, &false_lock, 0))
die(_("unable to write temporary index file"));

discard_cache();
discard_index(&the_index);
ret = get_lock_file_path(&false_lock);
read_cache_from(ret);
read_index_from(&the_index, ret, get_git_dir());
out:
string_list_clear(&partial, 0);
clear_pathspec(&pathspec);
Expand Down Expand Up @@ -1068,9 +1072,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
* and could have updated it. We must do this before we invoke
* the editor and after we invoke run_status above.
*/
discard_cache();
discard_index(&the_index);
}
read_cache_from(index_file);
read_index_from(&the_index, index_file, get_git_dir());

if (update_main_cache_tree(0)) {
error(_("Error building trees"));
Expand Down Expand Up @@ -1556,7 +1560,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
&s.pathspec, NULL, NULL);

if (use_optional_locks())
fd = hold_locked_index(&index_lock, 0);
fd = repo_hold_locked_index(the_repository, &index_lock, 0);
else
fd = -1;

Expand Down
7 changes: 4 additions & 3 deletions builtin/describe.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "config.h"
#include "lockfile.h"
Expand Down Expand Up @@ -653,10 +653,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
int fd, result;

setup_work_tree();
read_cache();
repo_read_index(the_repository);
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
NULL, NULL, NULL);
fd = hold_locked_index(&index_lock, 0);
fd = repo_hold_locked_index(the_repository,
&index_lock, 0);
if (0 <= fd)
repo_update_index_if_able(the_repository, &index_lock);

Expand Down
Loading

0 comments on commit 07047d6

Please sign in to comment.