Skip to content

Commit

Permalink
sparse-index: add macro for unaudited expansions
Browse files Browse the repository at this point in the history
For safety, areas of code that iterate over the cache entries in the
index were guarded with ensure_full_index() and labeled with a comment.
Replace these with a macro that calls ensure_full_index_with_reason()
using the line number of the caller to help identify the situation that
is causing the index expansion.

Signed-off-by: Derrick Stolee <[email protected]>
  • Loading branch information
derrickstolee authored and dscho committed Jan 1, 2025
1 parent 2332092 commit e6220cf
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
}

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
ensure_full_index_unaudited(the_repository->index);
for (i = 0; i < the_repository->index->cache_nr; i++) {
const struct cache_entry *ce = the_repository->index->cache[i];
struct string_list_item *item;
Expand Down Expand Up @@ -1133,7 +1133,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
int i, ita_nr = 0;

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
ensure_full_index_unaudited(the_repository->index);
for (i = 0; i < the_repository->index->cache_nr; i++)
if (ce_intent_to_add(the_repository->index->cache[i]))
ita_nr++;
Expand Down
2 changes: 1 addition & 1 deletion builtin/difftool.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
ret = run_command(&cmd);

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(&wtindex);
ensure_full_index_unaudited(&wtindex);

/*
* If the diff includes working copy files and those
Expand Down
2 changes: 1 addition & 1 deletion builtin/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ static void fsck_index(struct index_state *istate, const char *index_path,
unsigned int i;

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(istate);
ensure_full_index_unaudited(istate);
for (i = 0; i < istate->cache_nr; i++) {
unsigned int mode;
struct blob *blob;
Expand Down
4 changes: 2 additions & 2 deletions builtin/merge-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void merge_all(void)
{
int i;
/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
ensure_full_index_unaudited(the_repository->index);
for (i = 0; i < the_repository->index->cache_nr; i++) {
const struct cache_entry *ce = the_repository->index->cache[i];
if (!ce_stage(ce))
Expand All @@ -93,7 +93,7 @@ int cmd_merge_index(int argc,
repo_read_index(the_repository);

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
ensure_full_index_unaudited(the_repository->index);

i = 1;
if (!strcmp(argv[i], "-o")) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
char *ps_matched = xcalloc(ps->nr, 1);

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
ensure_full_index_unaudited(the_repository->index);
for (size_t i = 0; i < the_repository->index->cache_nr; i++)
ce_path_match(the_repository->index, the_repository->index->cache[i], ps,
ps_matched);
Expand Down
2 changes: 1 addition & 1 deletion builtin/submodule--helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -3400,7 +3400,7 @@ static void die_on_index_match(const char *path, int force)
char *ps_matched = xcalloc(ps.nr, 1);

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
ensure_full_index_unaudited(the_repository->index);

/*
* Since there is only one pathspec, we just need to
Expand Down
2 changes: 1 addition & 1 deletion entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ static void mark_colliding_entries(const struct checkout *state,
ce->ce_flags |= CE_MATCHED;

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(state->istate);
ensure_full_index_unaudited(state->istate);
for (size_t i = 0; i < state->istate->cache_nr; i++) {
struct cache_entry *dup = state->istate->cache[i];

Expand Down
2 changes: 1 addition & 1 deletion merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ static struct string_list *get_unmerged(struct index_state *istate)
string_list_init_dup(unmerged);

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(istate);
ensure_full_index_unaudited(istate);
for (i = 0; i < istate->cache_nr; i++) {
struct string_list_item *item;
struct stage_data *e;
Expand Down
4 changes: 2 additions & 2 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -2590,7 +2590,7 @@ int repo_index_has_changes(struct repository *repo,
return opt.flags.has_changes != 0;
} else {
/* TODO: audit for interaction with sparse-index. */
ensure_full_index(istate);
ensure_full_index_unaudited(istate);
for (i = 0; sb && i < istate->cache_nr; i++) {
if (i)
strbuf_addch(sb, ' ');
Expand Down Expand Up @@ -3872,7 +3872,7 @@ void overlay_tree_on_index(struct index_state *istate,

/* Hoist the unmerged entries up to stage #3 to make room */
/* TODO: audit for interaction with sparse-index. */
ensure_full_index(istate);
ensure_full_index_unaudited(istate);
for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = istate->cache[i];
if (!ce_stage(ce))
Expand Down
2 changes: 1 addition & 1 deletion resolve-undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void unmerge_index(struct index_state *istate, const struct pathspec *pathspec,
return;

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(istate);
ensure_full_index_unaudited(istate);

for_each_string_list_item(item, istate->resolve_undo) {
const char *path = item->string;
Expand Down
2 changes: 1 addition & 1 deletion revision.c
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ static void do_add_index_objects_to_pending(struct rev_info *revs,
int i;

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(istate);
ensure_full_index_unaudited(istate);
for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = istate->cache[i];
struct blob *blob;
Expand Down
6 changes: 6 additions & 0 deletions sparse-index.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SPARSE_INDEX_H__
#define SPARSE_INDEX_H__

#include "strbuf.h"

/*
* If performing an operation where the index is supposed to expand to a
* full index, then disable the advice message by setting this global to
Expand Down Expand Up @@ -54,4 +56,8 @@ void ensure_full_index_with_reason(struct index_state *istate,
const char *fmt,
...);

#define ensure_full_index_unaudited(i) \
ensure_full_index_with_reason((i), \
"unaudited call (%s.%d)", __FILE__, __LINE__);

#endif

0 comments on commit e6220cf

Please sign in to comment.