split-index: accept that a base index can be empty

We are about to fix an ancient bug where `do_read_index()` pretended that the index was not initialized when there are no index entries. Before the `index_state` structure gained the `initialized` flag in 913e0e99b6a (unpack_trees(): protect the handcrafted in-core index from read_cache(), 2008-08-23), that was the best we could do (even if it was incorrect: it is totally possible to read a Git index file that contains no index entries). This pattern was repeated also in 998330ac2e7 (read-cache: look for shared index files next to the index, too, 2021-08-26), which we fix here by _not_ mistaking an empty base index for a missing `sharedindex.*` file. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 29, 2023 at 13:23 UTC 7667f4f0a3c2002940c0b03930597fddc8599277
1 file changed +8 -6
read-cache.c
+8 -6
@@ -2500,12 +2500,14 @@ int read_index_from(struct index_state *istate, const char *path,
2500
2501 base_oid_hex = oid_to_hex(&split_index->base_oid);
2502 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
2503 - trace2_region_enter_printf("index", "shared/do_read_index",
2504 - the_repository, "%s", base_path);
2505 - ret = do_read_index(split_index->base, base_path, 0);
2506 - trace2_region_leave_printf("index", "shared/do_read_index",
2507 - the_repository, "%s", base_path);
2508 - if (!ret) {
2503 + if (file_exists(base_path)) {
2504 + trace2_region_enter_printf("index", "shared/do_read_index",
2505 + the_repository, "%s", base_path);
2506 +
2507 + ret = do_read_index(split_index->base, base_path, 0);
2508 + trace2_region_leave_printf("index", "shared/do_read_index",
2509 + the_repository, "%s", base_path);
2510 + } else {
2511 char *path_copy = xstrdup(path);
2512 char *base_path2 = xstrfmt("%s/sharedindex.%s",
2513 dirname(path_copy), base_oid_hex);