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 &&
72 rm c1.c # Do not leave untracked file in way of future tests
75 test_expect_success
'will not overwrite re-added file' '
76 git reset --hard c1 &&
78 git commit -m "rm c1.c" &&
81 test_must_fail git merge c1a &&
82 test_path_is_missing .git/MERGE_HEAD &&
83 test_cmp important c1.c
86 test_expect_success
'will not overwrite removed file with staged changes' '
87 git reset --hard c1 &&
89 git commit -m "rm c1.c" &&
93 test_must_fail git merge c1a &&
94 test_path_is_missing .git/MERGE_HEAD &&
96 test_cmp important c1.c
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 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
106 test_must_fail git merge c1a >out 2>err &&
107 test_grep "would be overwritten by merge" err &&
108 test_cmp important other.c &&
109 test_path_is_missing .git/MERGE_HEAD
111 test_must_fail git merge c1a >out &&
112 test_grep "Refusing to lose dirty file at other.c" out &&
113 test_path_is_file other.c~HEAD &&
114 test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
115 test_cmp important other.c
119 test_expect_success
'will not overwrite untracked subtree' '
120 git reset --hard c0 &&
123 cp important sub/f/important &&
124 test_must_fail git merge sub &&
125 test_path_is_missing .git/MERGE_HEAD &&
126 test_cmp important sub/f/important
130 error
: The following untracked working tree files would be overwritten by merge
:
133 Please move or remove them before you merge.
137 test_expect_success
'will not overwrite untracked file in leading path' '
138 git reset --hard c0 &&
142 test_must_fail git merge sub 2>out &&
143 test_cmp out expect &&
144 test_path_is_missing .git/MERGE_HEAD &&
145 test_cmp important sub &&
146 test_cmp important sub2 &&
150 test_expect_success SYMLINKS
'will not overwrite untracked symlink in leading path' '
151 git reset --hard c0 &&
155 test_must_fail git merge sub &&
156 test_path_is_missing .git/MERGE_HEAD
159 test_expect_success
'will not be confused by symlink in leading path' '
160 git reset --hard c0 &&
162 test_ln_s_add sub2 sub &&
168 error
: Untracked working tree
file 'c0.c' would be overwritten by merge.
169 fatal
: read-tree failed
172 test_expect_success
'will not overwrite untracked file on unborn branch' '
173 git reset --hard c0 &&
175 git checkout --orphan new &&
177 test_must_fail git merge c0 2>out &&
181 test_expect_success
'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
182 test_when_finished "rm c0.c" &&
183 test_path_is_missing .git/MERGE_HEAD &&
184 test_cmp important c0.c
187 test_expect_success
'failed merge leaves unborn branch in the womb' '
188 test_must_fail git rev-parse --verify HEAD
191 test_expect_success
'set up unborn branch and content' '
192 git symbolic-ref HEAD refs/heads/unborn &&
194 echo foo > tracked-file &&
195 git add tracked-file &&
196 echo bar > untracked-file
199 test_expect_success
'will not clobber WT/index when merging into unborn' '
201 grep foo tracked-file &&
202 git show :tracked-file >expect &&
204 grep bar untracked-file