3 test_description
='git-merge
5 Do not overwrite changes.'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 TEST_PASSES_SANITIZE_LEAK
=true
13 test_expect_success
'setup' '
14 test_commit c0 c0.c &&
15 test_commit c1 c1.c &&
16 test_commit c1a c1.c "c1 a" &&
17 git reset --hard c0 &&
18 test_commit c2 c2.c &&
19 git reset --hard c0 &&
21 echo "sub/f" > sub/f &&
23 echo "sub2/f" > sub2/f &&
24 git add sub/f sub2/f &&
27 echo "VERY IMPORTANT CHANGES" > important
30 test_expect_success
'will not overwrite untracked file' '
31 git reset --hard c1 &&
33 test_must_fail git merge c2 &&
34 test_path_is_missing .git/MERGE_HEAD &&
35 test_cmp important c2.c
38 test_expect_success
'will overwrite tracked file' '
39 git reset --hard c1 &&
42 git commit -m important &&
46 test_expect_success
'will not overwrite new file' '
47 git reset --hard c1 &&
50 test_must_fail git merge c2 &&
51 test_path_is_missing .git/MERGE_HEAD &&
52 test_cmp important c2.c
55 test_expect_success
'will not overwrite staged changes' '
56 git reset --hard c1 &&
60 test_must_fail git merge c2 &&
61 test_path_is_missing .git/MERGE_HEAD &&
63 test_cmp important c2.c
66 test_expect_success
'will not overwrite removed file' '
67 git reset --hard c1 &&
69 git commit -m "rm c1.c" &&
71 test_must_fail git merge c1a &&
72 test_cmp important c1.c &&
73 rm c1.c # Do not leave untracked file in way of future tests
76 test_expect_success
'will not overwrite re-added file' '
77 git reset --hard c1 &&
79 git commit -m "rm c1.c" &&
82 test_must_fail git merge c1a &&
83 test_path_is_missing .git/MERGE_HEAD &&
84 test_cmp important c1.c
87 test_expect_success
'will not overwrite removed file with staged changes' '
88 git reset --hard c1 &&
90 git commit -m "rm c1.c" &&
94 test_must_fail git merge c1a &&
95 test_path_is_missing .git/MERGE_HEAD &&
97 test_cmp important c1.c
100 test_expect_success
'will not overwrite unstaged changes in renamed file' '
101 git reset --hard c1 &&
102 git mv c1.c other.c &&
103 git commit -m rename &&
104 cp important other.c &&
105 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
107 test_must_fail git merge c1a >out 2>err &&
108 test_grep "would be overwritten by merge" err &&
109 test_cmp important other.c &&
110 test_path_is_missing .git/MERGE_HEAD
112 test_must_fail git merge c1a >out &&
113 test_grep "Refusing to lose dirty file at other.c" out &&
114 test_path_is_file other.c~HEAD &&
115 test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
116 test_cmp important other.c
120 test_expect_success
'will not overwrite untracked subtree' '
121 git reset --hard c0 &&
124 cp important sub/f/important &&
125 test_must_fail git merge sub &&
126 test_path_is_missing .git/MERGE_HEAD &&
127 test_cmp important sub/f/important
131 error
: The following untracked working tree files would be overwritten by merge
:
134 Please move or remove them before you merge.
138 test_expect_success
'will not overwrite untracked file in leading path' '
139 git reset --hard c0 &&
143 test_must_fail git merge sub 2>out &&
144 test_cmp out expect &&
145 test_path_is_missing .git/MERGE_HEAD &&
146 test_cmp important sub &&
147 test_cmp important sub2 &&
151 test_expect_success SYMLINKS
'will not overwrite untracked symlink in leading path' '
152 git reset --hard c0 &&
156 test_must_fail git merge sub &&
157 test_path_is_missing .git/MERGE_HEAD
160 test_expect_success
'will not be confused by symlink in leading path' '
161 git reset --hard c0 &&
163 test_ln_s_add sub2 sub &&
169 error
: Untracked working tree
file 'c0.c' would be overwritten by merge.
170 fatal
: read-tree failed
173 test_expect_success
'will not overwrite untracked file on unborn branch' '
174 git reset --hard c0 &&
176 git checkout --orphan new &&
178 test_must_fail git merge c0 2>out &&
182 test_expect_success
'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
183 test_when_finished "rm c0.c" &&
184 test_path_is_missing .git/MERGE_HEAD &&
185 test_cmp important c0.c
188 test_expect_success
'failed merge leaves unborn branch in the womb' '
189 test_must_fail git rev-parse --verify HEAD
192 test_expect_success
'set up unborn branch and content' '
193 git symbolic-ref HEAD refs/heads/unborn &&
195 echo foo > tracked-file &&
196 git add tracked-file &&
197 echo bar > untracked-file
200 test_expect_success
'will not clobber WT/index when merging into unborn' '
202 grep foo tracked-file &&
203 git show :tracked-file >expect &&
205 grep bar untracked-file