]> git.ipfire.org Git - thirdparty/git.git/blob - t/t6436-merge-overwrite.sh
Merge branch 'en/ort-perf-batch-9'
[thirdparty/git.git] / t / t6436-merge-overwrite.sh
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 '
73
74 test_expect_success 'will not overwrite re-added file' '
75 git reset --hard c1 &&
76 git rm c1.c &&
77 git commit -m "rm c1.c" &&
78 cp important c1.c &&
79 git add c1.c &&
80 test_must_fail git merge c1a &&
81 test_path_is_missing .git/MERGE_HEAD &&
82 test_cmp important c1.c
83 '
84
85 test_expect_success 'will not overwrite removed file with staged changes' '
86 git reset --hard c1 &&
87 git rm c1.c &&
88 git commit -m "rm c1.c" &&
89 cp important c1.c &&
90 git add c1.c &&
91 rm c1.c &&
92 test_must_fail git merge c1a &&
93 test_path_is_missing .git/MERGE_HEAD &&
94 git checkout c1.c &&
95 test_cmp important c1.c
96 '
97
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
104 then
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
109 else
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
115 fi
116 '
117
118 test_expect_success 'will not overwrite untracked subtree' '
119 git reset --hard c0 &&
120 rm -rf sub &&
121 mkdir -p sub/f &&
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
126 '
127
128 cat >expect <<\EOF
129 error: The following untracked working tree files would be overwritten by merge:
130 sub
131 sub2
132 Please move or remove them before you merge.
133 Aborting
134 EOF
135
136 test_expect_success 'will not overwrite untracked file in leading path' '
137 git reset --hard c0 &&
138 rm -rf sub &&
139 cp important sub &&
140 cp important sub2 &&
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 &&
146 rm -f sub sub2
147 '
148
149 test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
150 git reset --hard c0 &&
151 rm -rf sub &&
152 mkdir sub2 &&
153 ln -s sub2 sub &&
154 test_must_fail git merge sub &&
155 test_path_is_missing .git/MERGE_HEAD
156 '
157
158 test_expect_success 'will not be confused by symlink in leading path' '
159 git reset --hard c0 &&
160 rm -rf sub &&
161 test_ln_s_add sub2 sub &&
162 git commit -m ln &&
163 git checkout sub
164 '
165
166 cat >expect <<\EOF
167 error: Untracked working tree file 'c0.c' would be overwritten by merge.
168 fatal: read-tree failed
169 EOF
170
171 test_expect_success 'will not overwrite untracked file on unborn branch' '
172 git reset --hard c0 &&
173 git rm -fr . &&
174 git checkout --orphan new &&
175 cp important c0.c &&
176 test_must_fail git merge c0 2>out &&
177 test_cmp out expect
178 '
179
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
184 '
185
186 test_expect_success 'failed merge leaves unborn branch in the womb' '
187 test_must_fail git rev-parse --verify HEAD
188 '
189
190 test_expect_success 'set up unborn branch and content' '
191 git symbolic-ref HEAD refs/heads/unborn &&
192 rm -f .git/index &&
193 echo foo > tracked-file &&
194 git add tracked-file &&
195 echo bar > untracked-file
196 '
197
198 test_expect_success 'will not clobber WT/index when merging into unborn' '
199 git merge main &&
200 grep foo tracked-file &&
201 git show :tracked-file >expect &&
202 grep foo expect &&
203 grep bar untracked-file
204 '
205
206 test_done