| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git-merge |
| 4 | |
| 5 | Do not overwrite changes.' |
| 6 | |
| 7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 8 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 9 | |
| 10 | . ./test-lib.sh |
| 11 | |
| 12 | test_expect_success 'setup' ' |
| 13 | test_commit c0 c0.c && |
| 14 | test_commit c1 c1.c && |
| 15 | test_commit c1a c1.c "c1 a" && |
| 16 | git reset --hard c0 && |
| 17 | test_commit c2 c2.c && |
| 18 | git reset --hard c0 && |
| 19 | mkdir sub && |
| 20 | echo "sub/f" > sub/f && |
| 21 | mkdir sub2 && |
| 22 | echo "sub2/f" > sub2/f && |
| 23 | git add sub/f sub2/f && |
| 24 | git commit -m sub && |
| 25 | git tag sub && |
| 26 | echo "VERY IMPORTANT CHANGES" > important |
| 27 | ' |
| 28 | |
| 29 | test_expect_success 'will not overwrite untracked file' ' |
| 30 | git reset --hard c1 && |
| 31 | cp important c2.c && |
| 32 | test_must_fail git merge c2 && |
| 33 | test_path_is_missing .git/MERGE_HEAD && |
| 34 | test_cmp important c2.c |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'will overwrite tracked file' ' |
| 38 | git reset --hard c1 && |
| 39 | cp important c2.c && |
| 40 | git add c2.c && |
| 41 | git commit -m important && |
| 42 | git checkout c2 |
| 43 | ' |
| 44 | |
| 45 | test_expect_success 'will not overwrite new file' ' |
| 46 | git reset --hard c1 && |
| 47 | cp important c2.c && |
| 48 | git add c2.c && |
| 49 | test_must_fail git merge c2 && |
| 50 | test_path_is_missing .git/MERGE_HEAD && |
| 51 | test_cmp important c2.c |
| 52 | ' |
| 53 | |
| 54 | test_expect_success 'will not overwrite staged changes' ' |
| 55 | git reset --hard c1 && |
| 56 | cp important c2.c && |
| 57 | git add c2.c && |
| 58 | rm c2.c && |
| 59 | test_must_fail git merge c2 && |
| 60 | test_path_is_missing .git/MERGE_HEAD && |
| 61 | git checkout c2.c && |
| 62 | test_cmp important c2.c |
| 63 | ' |
| 64 | |
| 65 | test_expect_success 'will not overwrite removed file' ' |
| 66 | git reset --hard c1 && |
| 67 | git rm c1.c && |
| 68 | git commit -m "rm c1.c" && |
| 69 | cp important c1.c && |
| 70 | test_must_fail git merge c1a && |
| 71 | test_cmp important c1.c && |
| 72 | rm c1.c # Do not leave untracked file in way of future tests |
| 73 | ' |
| 74 | |
| 75 | test_expect_success 'will not overwrite re-added file' ' |
| 76 | git reset --hard c1 && |
| 77 | git rm c1.c && |
| 78 | git commit -m "rm c1.c" && |
| 79 | cp important c1.c && |
| 80 | git add c1.c && |
| 81 | test_must_fail git merge c1a && |
| 82 | test_path_is_missing .git/MERGE_HEAD && |
| 83 | test_cmp important c1.c |
| 84 | ' |
| 85 | |
| 86 | test_expect_success 'will not overwrite removed file with staged changes' ' |
| 87 | git reset --hard c1 && |
| 88 | git rm c1.c && |
| 89 | git commit -m "rm c1.c" && |
| 90 | cp important c1.c && |
| 91 | git add c1.c && |
| 92 | rm c1.c && |
| 93 | test_must_fail git merge c1a && |
| 94 | test_path_is_missing .git/MERGE_HEAD && |
| 95 | git checkout c1.c && |
| 96 | test_cmp important c1.c |
| 97 | ' |
| 98 | |
| 99 | test_expect_success 'will not overwrite unstaged changes in renamed file' ' |
| 100 | git reset --hard c1 && |
| 101 | git mv c1.c other.c && |
| 102 | git commit -m rename && |
| 103 | cp important other.c && |
| 104 | test_must_fail git merge c1a >out 2>err && |
| 105 | test_grep "would be overwritten by merge" err && |
| 106 | test_cmp important other.c && |
| 107 | test_path_is_missing .git/MERGE_HEAD |
| 108 | ' |
| 109 | |
| 110 | test_expect_success 'will not overwrite untracked subtree' ' |
| 111 | git reset --hard c0 && |
| 112 | rm -rf sub && |
| 113 | mkdir -p sub/f && |
| 114 | cp important sub/f/important && |
| 115 | test_must_fail git merge sub && |
| 116 | test_path_is_missing .git/MERGE_HEAD && |
| 117 | test_cmp important sub/f/important |
| 118 | ' |
| 119 | |
| 120 | cat >expect <<\EOF |
| 121 | error: The following untracked working tree files would be overwritten by merge: |
| 122 | sub |
| 123 | sub2 |
| 124 | Please move or remove them before you merge. |
| 125 | Aborting |
| 126 | EOF |
| 127 | |
| 128 | test_expect_success 'will not overwrite untracked file in leading path' ' |
| 129 | git reset --hard c0 && |
| 130 | rm -rf sub && |
| 131 | cp important sub && |
| 132 | cp important sub2 && |
| 133 | test_must_fail git merge sub 2>out && |
| 134 | test_cmp out expect && |
| 135 | test_path_is_missing .git/MERGE_HEAD && |
| 136 | test_cmp important sub && |
| 137 | test_cmp important sub2 && |
| 138 | rm -f sub sub2 |
| 139 | ' |
| 140 | |
| 141 | test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' ' |
| 142 | git reset --hard c0 && |
| 143 | rm -rf sub && |
| 144 | mkdir sub2 && |
| 145 | ln -s sub2 sub && |
| 146 | test_must_fail git merge sub && |
| 147 | test_path_is_missing .git/MERGE_HEAD |
| 148 | ' |
| 149 | |
| 150 | test_expect_success 'will not be confused by symlink in leading path' ' |
| 151 | git reset --hard c0 && |
| 152 | rm -rf sub && |
| 153 | test_ln_s_add sub2 sub && |
| 154 | git commit -m ln && |
| 155 | git checkout sub |
| 156 | ' |
| 157 | |
| 158 | cat >expect <<\EOF |
| 159 | error: Untracked working tree file 'c0.c' would be overwritten by merge. |
| 160 | fatal: read-tree failed |
| 161 | EOF |
| 162 | |
| 163 | test_expect_success 'will not overwrite untracked file on unborn branch' ' |
| 164 | git reset --hard c0 && |
| 165 | git rm -fr . && |
| 166 | git checkout --orphan new && |
| 167 | cp important c0.c && |
| 168 | test_must_fail git merge c0 2>out && |
| 169 | test_cmp out expect |
| 170 | ' |
| 171 | |
| 172 | test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' ' |
| 173 | test_when_finished "rm c0.c" && |
| 174 | test_path_is_missing .git/MERGE_HEAD && |
| 175 | test_cmp important c0.c |
| 176 | ' |
| 177 | |
| 178 | test_expect_success 'failed merge leaves unborn branch in the womb' ' |
| 179 | test_must_fail git rev-parse --verify HEAD |
| 180 | ' |
| 181 | |
| 182 | test_expect_success 'set up unborn branch and content' ' |
| 183 | git symbolic-ref HEAD refs/heads/unborn && |
| 184 | rm -f .git/index && |
| 185 | echo foo > tracked-file && |
| 186 | git add tracked-file && |
| 187 | echo bar > untracked-file |
| 188 | ' |
| 189 | |
| 190 | test_expect_success 'will not clobber WT/index when merging into unborn' ' |
| 191 | git merge main && |
| 192 | grep foo tracked-file && |
| 193 | git show :tracked-file >expect && |
| 194 | grep foo expect && |
| 195 | grep bar untracked-file |
| 196 | ' |
| 197 | |
| 198 | test_done |