Merge branch 'en/abort-df-conflict-fixes'
authorJunio C Hamano <gitster@pobox.com>
Fri, 17 Aug 2018 20:09:57 +0000 (13:09 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Aug 2018 20:09:57 +0000 (13:09 -0700)
"git merge --abort" etc. did not clean things up properly when
there were conflicted entries in the index in certain order that
are involved in D/F conflicts. This has been corrected.

* en/abort-df-conflict-fixes:
read-cache: fix directory/file conflict handling in read_index_unmerged()
t1015: demonstrate directory/file conflict recovery failures

read-cache.c
t/t1015-read-index-unmerged.sh [new file with mode: 0755]
t/t6020-merge-df.sh
t/t6042-merge-rename-corner-cases.sh
index 880849fc8ad8454a756ac0849dff288ad8f38eaa..c5fabc844aad68f60a30d0ff7498ff7e5d692a7a 100644 (file)
@@ -2808,10 +2808,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)
 {
@@ -2833,7 +2836,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
new file mode 100755 (executable)
index 0000000..55d22da
--- /dev/null
@@ -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_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" &&
+       (
+               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_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" &&
+       (
+               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_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" &&
+       (
+               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_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" &&
+       (
+               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
index 2af1beec5f6f330072b3739e3c8b855f988173a9..46b506b3b7b6f6308994082abda2fcaddaaadf0e 100755 (executable)
@@ -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 &&
index 07dd09d985fad600c6f21d335ccfb30e3626db13..b97aca7fa263f79520013b14d90fa0b49031484b 100755 (executable)
@@ -323,7 +323,6 @@ test_expect_success 'rename/directory conflict + content merge conflict' '
        (
                cd rename-directory-1 &&
 
-               git reset --hard &&
                git reset --hard &&
                git clean -fdqx &&