Skip to content

Commit

Permalink
sparse-index.c: fix use of index hashes in expand_index
Browse files Browse the repository at this point in the history
In ac8acb4 (sparse-index: complete partial expansion, 2022-05-23),
'expand_index()' was updated to expand the index to a given pathspec.
However, the 'path_matches_pattern_list()' method used to facilitate this
has the side effect of initializing or updating the index hash variables
('name_hash', 'dir_hash', and 'name_hash_initialized'). This operation is
performed on 'istate', though, not 'full'; as a result, the initialized
hashes are later overwritten when copied from 'full'. To ensure the correct
hashes are in 'istate' after the index expansion, change the arg used in
'path_matches_pattern_list()' from 'istate' to 'full'.

Note that this does not fully solve the problem. If 'istate' does not have
an initialized 'name_hash' when its contents are copied to 'full',
initialized hashes will be copied back into 'istate' but
'name_hash_initialized' will be 0. Therefore, we also need to copy
'full->name_hash_initialized' back to 'istate' after the index expansion is
complete.

Signed-off-by: Victoria Dye <vdye@github.com>
  • Loading branch information
vdye authored and dscho committed Nov 14, 2023
1 parent 02f19e8 commit 1f88a2c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sparse-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
if (pl &&
path_matches_pattern_list(ce->name, ce->ce_namelen,
NULL, &dtype,
pl, istate) == NOT_MATCHED) {
pl, full) == NOT_MATCHED) {
set_index_entry(full, full->cache_nr++, ce);
continue;
}
Expand Down Expand Up @@ -399,6 +399,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
}

/* Copy back into original index. */
istate->name_hash_initialized = full->name_hash_initialized;
memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash));
istate->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
Expand Down

0 comments on commit 1f88a2c

Please sign in to comment.