3 test_description='git-merge
5 Do not overwrite changes.'
9 test_expect_success 'setup' '
10 test_commit c0 c0.c &&
11 test_commit c1 c1.c &&
12 test_commit c1a c1.c "c1 a" &&
13 git reset --hard c0 &&
14 test_commit c2 c2.c &&
15 git reset --hard c0 &&
17 echo "sub/f" > sub/f &&
19 echo "sub2/f" > sub2/f &&
20 git add sub/f sub2/f &&
23 echo "VERY IMPORTANT CHANGES" > important
26 test_expect_success 'will not overwrite untracked file' '
27 git reset --hard c1 &&
29 test_must_fail git merge c2 &&
30 test_path_is_missing .git/MERGE_HEAD &&
31 test_cmp important c2.c
34 test_expect_success 'will overwrite tracked file' '
35 git reset --hard c1 &&
38 git commit -m important &&
42 test_expect_success 'will not overwrite new file' '
43 git reset --hard c1 &&
46 test_must_fail git merge c2 &&
47 test_path_is_missing .git/MERGE_HEAD &&
48 test_cmp important c2.c
51 test_expect_success 'will not overwrite staged changes' '
52 git reset --hard c1 &&
56 test_must_fail git merge c2 &&
57 test_path_is_missing .git/MERGE_HEAD &&
59 test_cmp important c2.c
62 test_expect_success 'will not overwrite removed file' '
63 git reset --hard c1 &&
65 git commit -m "rm c1.c" &&
67 test_must_fail git merge c1a &&
68 test_cmp important c1.c
71 test_expect_success 'will not overwrite re-added file' '
72 git reset --hard c1 &&
74 git commit -m "rm c1.c" &&
77 test_must_fail git merge c1a &&
78 test_path_is_missing .git/MERGE_HEAD &&
79 test_cmp important c1.c
82 test_expect_success 'will not overwrite removed file with staged changes' '
83 git reset --hard c1 &&
85 git commit -m "rm c1.c" &&
89 test_must_fail git merge c1a &&
90 test_path_is_missing .git/MERGE_HEAD &&
92 test_cmp important c1.c
95 test_expect_success 'will not overwrite unstaged changes in renamed file' '
96 git reset --hard c1 &&
97 git mv c1.c other.c &&
98 git commit -m rename &&
99 cp important other.c &&
100 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
102 test_must_fail git merge c1a >out 2>err &&
103 test_i18ngrep "would be overwritten by merge" err &&
104 test_cmp important other.c &&
105 test_path_is_missing .git/MERGE_HEAD
107 test_must_fail git merge c1a >out &&
108 test_i18ngrep "Refusing to lose dirty file at other.c" out &&
109 test_path_is_file other.c~HEAD &&
110 test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
111 test_cmp important other.c
115 test_expect_success 'will not overwrite untracked subtree' '
116 git reset --hard c0 &&
119 cp important sub/f/important &&
120 test_must_fail git merge sub &&
121 test_path_is_missing .git/MERGE_HEAD &&
122 test_cmp important sub/f/important
126 error: The following untracked working tree files would be overwritten by merge:
129 Please move or remove them before you merge.
133 test_expect_success 'will not overwrite untracked file in leading path' '
134 git reset --hard c0 &&
138 test_must_fail git merge sub 2>out &&
139 test_i18ncmp out expect &&
140 test_path_is_missing .git/MERGE_HEAD &&
141 test_cmp important sub &&
142 test_cmp important sub2 &&
146 test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
147 git reset --hard c0 &&
151 test_must_fail git merge sub &&
152 test_path_is_missing .git/MERGE_HEAD
155 test_expect_success 'will not be confused by symlink in leading path' '
156 git reset --hard c0 &&
158 test_ln_s_add sub2 sub &&
164 error: Untracked working tree file 'c0.c' would be overwritten by merge.
165 fatal: read-tree failed
168 test_expect_success 'will not overwrite untracked file on unborn branch' '
169 git reset --hard c0 &&
171 git checkout --orphan new &&
173 test_must_fail git merge c0 2>out &&
174 test_i18ncmp out expect
177 test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
178 test_when_finished "rm c0.c" &&
179 test_path_is_missing .git/MERGE_HEAD &&
180 test_cmp important c0.c
183 test_expect_success 'failed merge leaves unborn branch in the womb' '
184 test_must_fail git rev-parse --verify HEAD
187 test_expect_success 'set up unborn branch and content' '
188 git symbolic-ref HEAD refs/heads/unborn &&
190 echo foo > tracked-file &&
191 git add tracked-file &&
192 echo bar > untracked-file
195 test_expect_success 'will not clobber WT/index when merging into unborn' '
197 grep foo tracked-file &&
198 git show :tracked-file >expect &&
200 grep bar untracked-file