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
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 &&
20 echo "sub/f" > sub/f &&
22 echo "sub2/f" > sub2/f &&
23 git add sub/f sub2/f &&
26 echo "VERY IMPORTANT CHANGES" > important
29 test_expect_success 'will not overwrite untracked file' '
30 git reset --hard c1 &&
32 test_must_fail git merge c2 &&
33 test_path_is_missing .git/MERGE_HEAD &&
34 test_cmp important c2.c
37 test_expect_success 'will overwrite tracked file' '
38 git reset --hard c1 &&
41 git commit -m important &&
45 test_expect_success 'will not overwrite new file' '
46 git reset --hard c1 &&
49 test_must_fail git merge c2 &&
50 test_path_is_missing .git/MERGE_HEAD &&
51 test_cmp important c2.c
54 test_expect_success 'will not overwrite staged changes' '
55 git reset --hard c1 &&
59 test_must_fail git merge c2 &&
60 test_path_is_missing .git/MERGE_HEAD &&
62 test_cmp important c2.c
65 test_expect_success 'will not overwrite removed file' '
66 git reset --hard c1 &&
68 git commit -m "rm c1.c" &&
70 test_must_fail git merge c1a &&
71 test_cmp important c1.c
74 test_expect_success 'will not overwrite re-added file' '
75 git reset --hard c1 &&
77 git commit -m "rm c1.c" &&
80 test_must_fail git merge c1a &&
81 test_path_is_missing .git/MERGE_HEAD &&
82 test_cmp important c1.c
85 test_expect_success 'will not overwrite removed file with staged changes' '
86 git reset --hard c1 &&
88 git commit -m "rm c1.c" &&
92 test_must_fail git merge c1a &&
93 test_path_is_missing .git/MERGE_HEAD &&
95 test_cmp important c1.c
98 test_expect_success 'will not overwrite unstaged changes in renamed file' '
99 git reset --hard c1 &&
100 git mv c1.c other.c &&
101 git commit -m rename &&
102 cp important other.c &&
103 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
105 test_must_fail git merge c1a >out 2>err &&
106 test_i18ngrep "would be overwritten by merge" err &&
107 test_cmp important other.c &&
108 test_path_is_missing .git/MERGE_HEAD
110 test_must_fail git merge c1a >out &&
111 test_i18ngrep "Refusing to lose dirty file at other.c" out &&
112 test_path_is_file other.c~HEAD &&
113 test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
114 test_cmp important other.c
118 test_expect_success 'will not overwrite untracked subtree' '
119 git reset --hard c0 &&
122 cp important sub/f/important &&
123 test_must_fail git merge sub &&
124 test_path_is_missing .git/MERGE_HEAD &&
125 test_cmp important sub/f/important
129 error: The following untracked working tree files would be overwritten by merge:
132 Please move or remove them before you merge.
136 test_expect_success 'will not overwrite untracked file in leading path' '
137 git reset --hard c0 &&
141 test_must_fail git merge sub 2>out &&
142 test_cmp out expect &&
143 test_path_is_missing .git/MERGE_HEAD &&
144 test_cmp important sub &&
145 test_cmp important sub2 &&
149 test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
150 git reset --hard c0 &&
154 test_must_fail git merge sub &&
155 test_path_is_missing .git/MERGE_HEAD
158 test_expect_success 'will not be confused by symlink in leading path' '
159 git reset --hard c0 &&
161 test_ln_s_add sub2 sub &&
167 error: Untracked working tree file 'c0.c' would be overwritten by merge.
168 fatal: read-tree failed
171 test_expect_success 'will not overwrite untracked file on unborn branch' '
172 git reset --hard c0 &&
174 git checkout --orphan new &&
176 test_must_fail git merge c0 2>out &&
180 test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
181 test_when_finished "rm c0.c" &&
182 test_path_is_missing .git/MERGE_HEAD &&
183 test_cmp important c0.c
186 test_expect_success 'failed merge leaves unborn branch in the womb' '
187 test_must_fail git rev-parse --verify HEAD
190 test_expect_success 'set up unborn branch and content' '
191 git symbolic-ref HEAD refs/heads/unborn &&
193 echo foo > tracked-file &&
194 git add tracked-file &&
195 echo bar > untracked-file
198 test_expect_success 'will not clobber WT/index when merging into unborn' '
200 grep foo tracked-file &&
201 git show :tracked-file >expect &&
203 grep bar untracked-file