Skip to content

Commit 7f5ad0c

Browse files
committed
Merge branch 'js/empty-index-fixes'
A few places failed to differenciate the case where the index is truly empty (nothing added) and we haven't yet read from the on-disk index file, which have been corrected. * js/empty-index-fixes: commit -a -m: allow the top-level tree to become empty again split-index: accept that a base index can be empty do_read_index(): always mark index as initialized unless erroring out
2 parents d52a45c + 2ee045e commit 7f5ad0c

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

builtin/commit.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,11 +1002,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10021002
struct object_id oid;
10031003
const char *parent = "HEAD";
10041004

1005-
if (!the_index.cache_nr) {
1006-
discard_index(&the_index);
1007-
if (repo_read_index(the_repository) < 0)
1008-
die(_("Cannot read index"));
1009-
}
1005+
if (!the_index.initialized && repo_read_index(the_repository) < 0)
1006+
die(_("Cannot read index"));
10101007

10111008
if (amend)
10121009
parent = "HEAD^1";

read-cache.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
22362236
if (fd < 0) {
22372237
if (!must_exist && errno == ENOENT) {
22382238
set_new_index_sparsity(istate);
2239+
istate->initialized = 1;
22392240
return 0;
22402241
}
22412242
die_errno(_("%s: index file open failed"), path);
@@ -2405,12 +2406,14 @@ int read_index_from(struct index_state *istate, const char *path,
24052406

24062407
base_oid_hex = oid_to_hex(&split_index->base_oid);
24072408
base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
2408-
trace2_region_enter_printf("index", "shared/do_read_index",
2409-
the_repository, "%s", base_path);
2410-
ret = do_read_index(split_index->base, base_path, 0);
2411-
trace2_region_leave_printf("index", "shared/do_read_index",
2412-
the_repository, "%s", base_path);
2413-
if (!ret) {
2409+
if (file_exists(base_path)) {
2410+
trace2_region_enter_printf("index", "shared/do_read_index",
2411+
the_repository, "%s", base_path);
2412+
2413+
ret = do_read_index(split_index->base, base_path, 0);
2414+
trace2_region_leave_printf("index", "shared/do_read_index",
2415+
the_repository, "%s", base_path);
2416+
} else {
24142417
char *path_copy = xstrdup(path);
24152418
char *base_path2 = xstrfmt("%s/sharedindex.%s",
24162419
dirname(path_copy), base_oid_hex);

t/t2200-add-update.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,15 @@ test_expect_success '"add -u non-existent" should fail' '
197197
! grep "non-existent" actual
198198
'
199199

200+
test_expect_success '"commit -a" implies "add -u" if index becomes empty' '
201+
git rm -rf \* &&
202+
git commit -m clean-slate &&
203+
test_commit file1 &&
204+
rm file1.t &&
205+
test_tick &&
206+
git commit -a -m remove &&
207+
git ls-tree HEAD: >out &&
208+
test_must_be_empty out
209+
'
210+
200211
test_done

0 commit comments

Comments
 (0)