Skip to content

Commit 045113a

Browse files
pcloudsgitster
authored andcommitted
read-cache: save deleted entries in split index
Entries that belong to the base index should not be freed. Mark CE_REMOVE to track them. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e0cf0d7 commit 045113a

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

read-cache.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
3939

4040
/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
4141
#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
42-
CE_ENTRY_ADDED)
42+
CE_ENTRY_ADDED | CE_ENTRY_REMOVED)
4343

4444
struct index_state the_index;
4545
static const char *alternate_index_output;
@@ -488,7 +488,7 @@ int remove_index_entry_at(struct index_state *istate, int pos)
488488

489489
record_resolve_undo(istate, ce);
490490
remove_name_hash(istate, ce);
491-
free(ce);
491+
save_or_free_index_entry(istate, ce);
492492
istate->cache_changed |= CE_ENTRY_REMOVED;
493493
istate->cache_nr--;
494494
if (pos >= istate->cache_nr)
@@ -512,7 +512,7 @@ void remove_marked_cache_entries(struct index_state *istate)
512512
for (i = j = 0; i < istate->cache_nr; i++) {
513513
if (ce_array[i]->ce_flags & CE_REMOVE) {
514514
remove_name_hash(istate, ce_array[i]);
515-
free(ce_array[i]);
515+
save_or_free_index_entry(istate, ce_array[i]);
516516
}
517517
else
518518
ce_array[j++] = ce_array[i];
@@ -577,7 +577,9 @@ static int different_name(struct cache_entry *ce, struct cache_entry *alias)
577577
* So we use the CE_ADDED flag to verify that the alias was an old
578578
* one before we accept it as
579579
*/
580-
static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_entry *alias)
580+
static struct cache_entry *create_alias_ce(struct index_state *istate,
581+
struct cache_entry *ce,
582+
struct cache_entry *alias)
581583
{
582584
int len;
583585
struct cache_entry *new;
@@ -590,7 +592,7 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_
590592
new = xcalloc(1, cache_entry_size(len));
591593
memcpy(new->name, alias->name, len);
592594
copy_cache_entry(new, ce);
593-
free(ce);
595+
save_or_free_index_entry(istate, ce);
594596
return new;
595597
}
596598

@@ -683,7 +685,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
683685
set_object_name_for_intent_to_add_entry(ce);
684686

685687
if (ignore_case && alias && different_name(ce, alias))
686-
ce = create_alias_ce(ce, alias);
688+
ce = create_alias_ce(istate, ce, alias);
687689
ce->ce_flags |= CE_ADDED;
688690

689691
/* It was suspected to be racily clean, but it turns out to be Ok */

split-index.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,15 @@ void discard_split_index(struct index_state *istate)
8888
}
8989
free(si);
9090
}
91+
92+
void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
93+
{
94+
if (ce->index &&
95+
istate->split_index &&
96+
istate->split_index->base &&
97+
ce->index <= istate->split_index->base->cache_nr &&
98+
ce == istate->split_index->base->cache[ce->index - 1])
99+
ce->ce_flags |= CE_REMOVE;
100+
else
101+
free(ce);
102+
}

split-index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct split_index {
1212
};
1313

1414
struct split_index *init_split_index(struct index_state *istate);
15+
void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce);
1516
int read_link_extension(struct index_state *istate,
1617
const void *data, unsigned long sz);
1718
int write_link_extension(struct strbuf *sb,

0 commit comments

Comments
 (0)