From 25c200a70078054b59331d50509481d00647dcbf Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Tue, 31 Jul 2018 10:12:04 -0700 Subject: [PATCH 1/2] t1015: demonstrate directory/file conflict recovery failures Several "recovery" commands outright fail or do not fully recover when directory-file conflicts are present. This includes: * git read-tree --reset HEAD * git am --skip * git am --abort * git merge --abort * git reset --hard Add testcases documenting these shortcomings. Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- t/t1015-read-index-unmerged.sh | 123 +++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 t/t1015-read-index-unmerged.sh diff --git a/t/t1015-read-index-unmerged.sh b/t/t1015-read-index-unmerged.sh new file mode 100755 index 0000000000..32ef6bdcfa --- /dev/null +++ b/t/t1015-read-index-unmerged.sh @@ -0,0 +1,123 @@ +#!/bin/sh + +test_description='Test various callers of read_index_unmerged' +. ./test-lib.sh + +test_expect_success 'setup modify/delete + directory/file conflict' ' + test_create_repo df_plus_modify_delete && + ( + cd df_plus_modify_delete && + + test_write_lines a b c d e f g h >letters && + git add letters && + git commit -m initial && + + git checkout -b modify && + # Throw in letters.txt for sorting order fun + # ("letters.txt" sorts between "letters" and "letters/file") + echo i >>letters && + echo "version 2" >letters.txt && + git add letters letters.txt && + git commit -m modified && + + git checkout -b delete HEAD^ && + git rm letters && + mkdir letters && + >letters/file && + echo "version 1" >letters.txt && + git add letters letters.txt && + git commit -m deleted + ) +' + +test_expect_failure 'read-tree --reset cleans unmerged entries' ' + test_when_finished "git -C df_plus_modify_delete clean -f" && + test_when_finished "git -C df_plus_modify_delete reset --hard" && + ( + cd df_plus_modify_delete && + + git checkout delete^0 && + test_must_fail git merge modify && + + git read-tree --reset HEAD && + git ls-files -u >conflicts && + test_must_be_empty conflicts + ) +' + +test_expect_failure 'One reset --hard cleans unmerged entries' ' + test_when_finished "git -C df_plus_modify_delete clean -f" && + test_when_finished "git -C df_plus_modify_delete reset --hard" && + ( + cd df_plus_modify_delete && + + git checkout delete^0 && + test_must_fail git merge modify && + + git reset --hard && + test_path_is_missing .git/MERGE_HEAD && + git ls-files -u >conflicts && + test_must_be_empty conflicts + ) +' + +test_expect_success 'setup directory/file conflict + simple edit/edit' ' + test_create_repo df_plus_edit_edit && + ( + cd df_plus_edit_edit && + + test_seq 1 10 >numbers && + git add numbers && + git commit -m initial && + + git checkout -b d-edit && + mkdir foo && + echo content >foo/bar && + git add foo && + echo 11 >>numbers && + git add numbers && + git commit -m "directory and edit" && + + git checkout -b f-edit d-edit^1 && + echo content >foo && + git add foo && + echo eleven >>numbers && + git add numbers && + git commit -m "file and edit" + ) +' + +test_expect_failure 'git merge --abort succeeds despite D/F conflict' ' + test_when_finished "git -C df_plus_edit_edit clean -f" && + test_when_finished "git -C df_plus_edit_edit reset --hard" && + ( + cd df_plus_edit_edit && + + git checkout f-edit^0 && + test_must_fail git merge d-edit^0 && + + git merge --abort && + test_path_is_missing .git/MERGE_HEAD && + git ls-files -u >conflicts && + test_must_be_empty conflicts + ) +' + +test_expect_failure 'git am --skip succeeds despite D/F conflict' ' + test_when_finished "git -C df_plus_edit_edit clean -f" && + test_when_finished "git -C df_plus_edit_edit reset --hard" && + ( + cd df_plus_edit_edit && + + git checkout f-edit^0 && + git format-patch -1 d-edit && + test_must_fail git am -3 0001*.patch && + + git am --skip && + test_path_is_missing .git/rebase-apply && + git ls-files -u >conflicts && + test_must_be_empty conflicts + ) +' + +test_done From ad3762042a8d0029ff16bcbfbb2656c36cf3d662 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Tue, 31 Jul 2018 10:12:05 -0700 Subject: [PATCH 2/2] read-cache: fix directory/file conflict handling in read_index_unmerged() read_index_unmerged() has two intended purposes: * return 1 if there are any unmerged entries, 0 otherwise * drops any higher-stage entries down to stage #0 There are several callers of read_index_unmerged() that check the return value to see if it is non-zero, all of which then die() if that condition is met. For these callers, dropping higher-stage entries down to stage #0 is a waste of resources, and returning immediately on first unmerged entry would be better. But it's probably only a very minor difference and isn't the focus of this series. The remaining callers ignore the return value and call this function for the side effect of dropping higher-stage entries down to stage #0. As mentioned in commit e11d7b596970 ("'reset --merge': fix unmerged case", 2009-12-31), The _only_ reason we want to keep a previously unmerged entry in the index at stage #0 is so that we don't forget the fact that we have corresponding file in the work tree in order to be able to remove it when the tree we are resetting to does not have the path. In fact, prior to commit d1a43f2aa4bf ("reset --hard/read-tree --reset -u: remove unmerged new paths", 2008-10-15), read_index_unmerged() did just remove unmerged entries from the cache immediately but that had the unwanted effect of leaving around new untracked files in the tree from aborted merges. So, that's the intended purpose of this function. The problem is that when directory/files conflicts are present, trying to add the file to the index at stage 0 fails (because there is still a directory in the way), and the function returns early with a -1 return code to signify the error. As noted above, none of the callers who want the drop-to-stage-0 behavior check the return status, though, so this means all remaining unmerged entries remain in the index and the callers proceed assuming otherwise. Users then see errors of the form: error: 'DIR-OR-FILE' appears as both a file and as a directory error: DIR-OR-FILE: cannot drop to stage #0 and potentially also messages about other unmerged entries which came lexicographically later than whatever pathname was both a file and a directory. Google finds a few hits searching for those messages, suggesting there were probably a couple people who hit this besides me. Luckily, calling `git reset --hard` multiple times would workaround this bug. Since the whole purpose here is to just put the entry *temporarily* into the index so that any associated file in the working copy can be removed, we can just skip the DFCHECK and allow both the file and directory to appear in the index. The temporary simultaneous appearance of the directory and file entries in the index will be removed by the callers by calling unpack_trees(), which excludes these unmerged entries marked with CE_CONFLICTED flag from the resulting index, before they attempt to write the index anywhere. Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- read-cache.c | 13 ++++++++----- t/t1015-read-index-unmerged.sh | 8 ++++---- t/t6020-merge-df.sh | 3 --- t/t6042-merge-rename-corner-cases.sh | 2 -- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/read-cache.c b/read-cache.c index 372588260e..666d295a5a 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2632,10 +2632,13 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock, /* * Read the index file that is potentially unmerged into given - * index_state, dropping any unmerged entries. Returns true if - * the index is unmerged. Callers who want to refuse to work - * from an unmerged state can call this and check its return value, - * instead of calling read_cache(). + * index_state, dropping any unmerged entries to stage #0 (potentially + * resulting in a path appearing as both a file and a directory in the + * index; the caller is responsible to clear out the extra entries + * before writing the index to a tree). Returns true if the index is + * unmerged. Callers who want to refuse to work from an unmerged + * state can call this and check its return value, instead of calling + * read_cache(). */ int read_index_unmerged(struct index_state *istate) { @@ -2658,7 +2661,7 @@ int read_index_unmerged(struct index_state *istate) new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED; new_ce->ce_namelen = len; new_ce->ce_mode = ce->ce_mode; - if (add_index_entry(istate, new_ce, 0)) + if (add_index_entry(istate, new_ce, ADD_CACHE_SKIP_DFCHECK)) return error("%s: cannot drop to stage #0", new_ce->name); } diff --git a/t/t1015-read-index-unmerged.sh b/t/t1015-read-index-unmerged.sh index 32ef6bdcfa..55d22da32c 100755 --- a/t/t1015-read-index-unmerged.sh +++ b/t/t1015-read-index-unmerged.sh @@ -30,7 +30,7 @@ test_expect_success 'setup modify/delete + directory/file conflict' ' ) ' -test_expect_failure 'read-tree --reset cleans unmerged entries' ' +test_expect_success 'read-tree --reset cleans unmerged entries' ' test_when_finished "git -C df_plus_modify_delete clean -f" && test_when_finished "git -C df_plus_modify_delete reset --hard" && ( @@ -45,7 +45,7 @@ test_expect_failure 'read-tree --reset cleans unmerged entries' ' ) ' -test_expect_failure 'One reset --hard cleans unmerged entries' ' +test_expect_success 'One reset --hard cleans unmerged entries' ' test_when_finished "git -C df_plus_modify_delete clean -f" && test_when_finished "git -C df_plus_modify_delete reset --hard" && ( @@ -87,7 +87,7 @@ test_expect_success 'setup directory/file conflict + simple edit/edit' ' ) ' -test_expect_failure 'git merge --abort succeeds despite D/F conflict' ' +test_expect_success 'git merge --abort succeeds despite D/F conflict' ' test_when_finished "git -C df_plus_edit_edit clean -f" && test_when_finished "git -C df_plus_edit_edit reset --hard" && ( @@ -103,7 +103,7 @@ test_expect_failure 'git merge --abort succeeds despite D/F conflict' ' ) ' -test_expect_failure 'git am --skip succeeds despite D/F conflict' ' +test_expect_success 'git am --skip succeeds despite D/F conflict' ' test_when_finished "git -C df_plus_edit_edit clean -f" && test_when_finished "git -C df_plus_edit_edit reset --hard" && ( diff --git a/t/t6020-merge-df.sh b/t/t6020-merge-df.sh index 2af1beec5f..46b506b3b7 100755 --- a/t/t6020-merge-df.sh +++ b/t/t6020-merge-df.sh @@ -89,9 +89,6 @@ test_expect_success 'modify/delete + directory/file conflict' ' ' test_expect_success 'modify/delete + directory/file conflict; other way' ' - # Yes, we really need the double reset since "letters" appears as - # both a file and a directory. - git reset --hard && git reset --hard && git clean -f && git checkout modify^0 && diff --git a/t/t6042-merge-rename-corner-cases.sh b/t/t6042-merge-rename-corner-cases.sh index 411550d2b6..de77bfaf4f 100755 --- a/t/t6042-merge-rename-corner-cases.sh +++ b/t/t6042-merge-rename-corner-cases.sh @@ -235,7 +235,6 @@ test_expect_success 'setup content merge + rename/directory conflict' ' ' test_expect_success 'rename/directory conflict + clean content merge' ' - git reset --hard && git reset --hard && git clean -fdqx && @@ -259,7 +258,6 @@ test_expect_success 'rename/directory conflict + clean content merge' ' ' test_expect_success 'rename/directory conflict + content merge conflict' ' - git reset --hard && git reset --hard && git clean -fdqx &&