Skip to content

Commit 09d5dc3

Browse files
committed
Simplify cache API
Earlier, add_file_to_index() invalidated the path in the cache-tree but remove_file_from_cache() did not, and the user of the latter needed to invalidate the entry himself. This led to a few bugs due to missed invalidate calls already. This patch makes the management of cache-tree less error prone by making more invalidate calls from lower level cache API functions. The rules are: - If you are going to write the index, you should either maintain cache_tree correctly. - If you cannot, alternatively you can remove the entire cache_tree by calling cache_tree_free() before you call write_cache(). - When you modify the index, cache_tree_invalidate_path() should be called with the path you are modifying, to discard the entry from the cache-tree structure. - The following cache API functions exported from read-cache.c (and the macro whose names have "cache" instead of "index") automatically call cache_tree_invalidate_path() for you: - remove_file_from_index(); - add_file_to_index(); - add_index_entry(); You can modify the index bypassing the above API functions (e.g. find an existing cache entry from the index and modify it in place). You need to call cache_tree_invalidate_path() yourself in such a case. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 611d813 commit 09d5dc3

File tree

6 files changed

+4
-19
lines changed

6 files changed

+4
-19
lines changed

builtin-add.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ static void update_callback(struct diff_queue_struct *q,
103103
break;
104104
case DIFF_STATUS_DELETED:
105105
remove_file_from_cache(path);
106-
cache_tree_invalidate_path(active_cache_tree, path);
107106
if (verbose)
108107
printf("remove '%s'\n", path);
109108
break;

builtin-apply.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,6 @@ static void remove_file(struct patch *patch, int rmdir_empty)
23942394
if (update_index) {
23952395
if (remove_file_from_cache(patch->old_name) < 0)
23962396
die("unable to remove %s from index", patch->old_name);
2397-
cache_tree_invalidate_path(active_cache_tree, patch->old_name);
23982397
}
23992398
if (!cached) {
24002399
if (S_ISGITLINK(patch->old_mode)) {
@@ -2549,7 +2548,6 @@ static void create_file(struct patch *patch)
25492548
mode = S_IFREG | 0644;
25502549
create_one_file(path, mode, buf, size);
25512550
add_index_file(path, mode, buf, size);
2552-
cache_tree_invalidate_path(active_cache_tree, path);
25532551
}
25542552

25552553
/* phase zero is to remove, phase one is to create */

builtin-mv.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
276276
add_file_to_cache(path, verbose);
277277
}
278278

279-
for (i = 0; i < deleted.nr; i++) {
280-
const char *path = deleted.items[i].path;
281-
remove_file_from_cache(path);
282-
cache_tree_invalidate_path(active_cache_tree, path);
283-
}
279+
for (i = 0; i < deleted.nr; i++)
280+
remove_file_from_cache(deleted.items[i].path);
284281

285282
if (active_cache_changed) {
286283
if (write_cache(newfd, active_cache, active_nr) ||

builtin-rm.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
227227

228228
if (remove_file_from_cache(path))
229229
die("git-rm: unable to remove %s", path);
230-
cache_tree_invalidate_path(active_cache_tree, path);
231230
}
232231

233232
if (show_only)

builtin-update-index.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,6 @@ static int process_path(const char *path)
195195
int len;
196196
struct stat st;
197197

198-
/* We probably want to do this in remove_file_from_cache() and
199-
* add_cache_entry() instead...
200-
*/
201-
cache_tree_invalidate_path(active_cache_tree, path);
202-
203198
/*
204199
* First things first: get the stat information, to decide
205200
* what to do about the pathname!
@@ -239,7 +234,6 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
239234
return error("%s: cannot add to the index - missing --add option?",
240235
path);
241236
report("add '%s'", path);
242-
cache_tree_invalidate_path(active_cache_tree, path);
243237
return 0;
244238
}
245239

@@ -284,7 +278,6 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
284278
die("Unable to mark file %s", path);
285279
goto free_return;
286280
}
287-
cache_tree_invalidate_path(active_cache_tree, path);
288281

289282
if (force_remove) {
290283
if (remove_file_from_cache(p))
@@ -367,7 +360,6 @@ static void read_index_info(int line_termination)
367360
free(path_name);
368361
continue;
369362
}
370-
cache_tree_invalidate_path(active_cache_tree, path_name);
371363

372364
if (!mode) {
373365
/* mode == 0 means there is no such path -- remove */
@@ -474,7 +466,6 @@ static int unresolve_one(const char *path)
474466
goto free_return;
475467
}
476468

477-
cache_tree_invalidate_path(active_cache_tree, path);
478469
remove_file_from_cache(path);
479470
if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
480471
error("%s: cannot add our version to the index.", path);

read-cache.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ int remove_file_from_index(struct index_state *istate, const char *path)
346346
int pos = index_name_pos(istate, path, strlen(path));
347347
if (pos < 0)
348348
pos = -pos-1;
349+
cache_tree_invalidate_path(istate->cache_tree, path);
349350
while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
350351
remove_index_entry_at(istate, pos);
351352
return 0;
@@ -430,7 +431,6 @@ int add_file_to_index(struct index_state *istate, const char *path, int verbose)
430431
die("unable to add %s to index",path);
431432
if (verbose)
432433
printf("add '%s'\n", path);
433-
cache_tree_invalidate_path(istate->cache_tree, path);
434434
return 0;
435435
}
436436

@@ -673,6 +673,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
673673
int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
674674
int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
675675

676+
cache_tree_invalidate_path(istate->cache_tree, ce->name);
676677
pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
677678

678679
/* existing match? Just replace it. */

0 commit comments

Comments
 (0)