]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6436-merge-overwrite.sh
The second batch
[thirdparty/git.git] / t / t6436-merge-overwrite.sh
CommitLineData
7bb1fcc6
CB
1#!/bin/sh
2
3test_description='git-merge
4
5Do not overwrite changes.'
6
5902f5f4 7GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
8export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
7bb1fcc6
CB
10. ./test-lib.sh
11
12test_expect_success 'setup' '
52a0a1bd
CB
13 test_commit c0 c0.c &&
14 test_commit c1 c1.c &&
15 test_commit c1a c1.c "c1 a" &&
7bb1fcc6 16 git reset --hard c0 &&
52a0a1bd 17 test_commit c2 c2.c &&
189645ca
CB
18 git reset --hard c0 &&
19 mkdir sub &&
20 echo "sub/f" > sub/f &&
7980872d
CB
21 mkdir sub2 &&
22 echo "sub2/f" > sub2/f &&
23 git add sub/f sub2/f &&
189645ca
CB
24 git commit -m sub &&
25 git tag sub &&
7bb1fcc6
CB
26 echo "VERY IMPORTANT CHANGES" > important
27'
28
29test_expect_success 'will not overwrite untracked file' '
30 git reset --hard c1 &&
52a0a1bd 31 cp important c2.c &&
ce14e0b2 32 test_must_fail git merge c2 &&
52a0a1bd 33 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
34 test_cmp important c2.c
35'
36
189645ca
CB
37test_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
7bb1fcc6
CB
45test_expect_success 'will not overwrite new file' '
46 git reset --hard c1 &&
52a0a1bd 47 cp important c2.c &&
7bb1fcc6 48 git add c2.c &&
ce14e0b2 49 test_must_fail git merge c2 &&
52a0a1bd 50 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
51 test_cmp important c2.c
52'
53
54test_expect_success 'will not overwrite staged changes' '
55 git reset --hard c1 &&
52a0a1bd 56 cp important c2.c &&
7bb1fcc6
CB
57 git add c2.c &&
58 rm c2.c &&
ce14e0b2 59 test_must_fail git merge c2 &&
52a0a1bd 60 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
61 git checkout c2.c &&
62 test_cmp important c2.c
63'
64
c5ab03f2 65test_expect_success 'will not overwrite removed file' '
7bb1fcc6
CB
66 git reset --hard c1 &&
67 git rm c1.c &&
68 git commit -m "rm c1.c" &&
52a0a1bd 69 cp important c1.c &&
ce14e0b2 70 test_must_fail git merge c1a &&
f222bd34
EN
71 test_cmp important c1.c &&
72 rm c1.c # Do not leave untracked file in way of future tests
7bb1fcc6
CB
73'
74
75test_expect_success 'will not overwrite re-added file' '
76 git reset --hard c1 &&
77 git rm c1.c &&
78 git commit -m "rm c1.c" &&
52a0a1bd 79 cp important c1.c &&
7bb1fcc6 80 git add c1.c &&
ce14e0b2 81 test_must_fail git merge c1a &&
52a0a1bd 82 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
83 test_cmp important c1.c
84'
85
86test_expect_success 'will not overwrite removed file with staged changes' '
87 git reset --hard c1 &&
88 git rm c1.c &&
89 git commit -m "rm c1.c" &&
52a0a1bd 90 cp important c1.c &&
7bb1fcc6
CB
91 git add c1.c &&
92 rm c1.c &&
ce14e0b2 93 test_must_fail git merge c1a &&
52a0a1bd 94 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
95 git checkout c1.c &&
96 test_cmp important c1.c
97'
98
64b1abe9 99test_expect_success 'will not overwrite unstaged changes in renamed file' '
30fd3a54
CB
100 git reset --hard c1 &&
101 git mv c1.c other.c &&
102 git commit -m rename &&
103 cp important other.c &&
23bef2e3
EN
104 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
105 then
106 test_must_fail git merge c1a >out 2>err &&
6789275d 107 test_grep "would be overwritten by merge" err &&
23bef2e3
EN
108 test_cmp important other.c &&
109 test_path_is_missing .git/MERGE_HEAD
110 else
111 test_must_fail git merge c1a >out &&
6789275d 112 test_grep "Refusing to lose dirty file at other.c" out &&
23bef2e3
EN
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
116 fi
30fd3a54
CB
117'
118
189645ca
CB
119test_expect_success 'will not overwrite untracked subtree' '
120 git reset --hard c0 &&
121 rm -rf sub &&
122 mkdir -p sub/f &&
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
127'
128
7980872d
CB
129cat >expect <<\EOF
130error: The following untracked working tree files would be overwritten by merge:
131 sub
132 sub2
c2691e2a 133Please move or remove them before you merge.
6f90969b 134Aborting
7980872d
CB
135EOF
136
f66caaf9 137test_expect_success 'will not overwrite untracked file in leading path' '
189645ca
CB
138 git reset --hard c0 &&
139 rm -rf sub &&
140 cp important sub &&
7980872d
CB
141 cp important sub2 &&
142 test_must_fail git merge sub 2>out &&
1108cea7 143 test_cmp out expect &&
189645ca 144 test_path_is_missing .git/MERGE_HEAD &&
7980872d
CB
145 test_cmp important sub &&
146 test_cmp important sub2 &&
147 rm -f sub sub2
189645ca
CB
148'
149
8523d071 150test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
189645ca
CB
151 git reset --hard c0 &&
152 rm -rf sub &&
153 mkdir sub2 &&
154 ln -s sub2 sub &&
155 test_must_fail git merge sub &&
156 test_path_is_missing .git/MERGE_HEAD
157'
158
889c6f0e 159test_expect_success 'will not be confused by symlink in leading path' '
189645ca
CB
160 git reset --hard c0 &&
161 rm -rf sub &&
889c6f0e 162 test_ln_s_add sub2 sub &&
189645ca
CB
163 git commit -m ln &&
164 git checkout sub
165'
166
172b6428
CB
167cat >expect <<\EOF
168error: Untracked working tree file 'c0.c' would be overwritten by merge.
169fatal: read-tree failed
170EOF
171
172test_expect_success 'will not overwrite untracked file on unborn branch' '
173 git reset --hard c0 &&
174 git rm -fr . &&
175 git checkout --orphan new &&
176 cp important c0.c &&
c9ea118e 177 test_must_fail git merge c0 2>out &&
1108cea7 178 test_cmp out expect
bacec478
ÆAB
179'
180
181test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
d6d9e76d 182 test_when_finished "rm c0.c" &&
172b6428
CB
183 test_path_is_missing .git/MERGE_HEAD &&
184 test_cmp important c0.c
185'
186
97b1b4f3
JK
187test_expect_success 'failed merge leaves unborn branch in the womb' '
188 test_must_fail git rev-parse --verify HEAD
189'
190
5b327081
TR
191test_expect_success 'set up unborn branch and content' '
192 git symbolic-ref HEAD refs/heads/unborn &&
193 rm -f .git/index &&
194 echo foo > tracked-file &&
195 git add tracked-file &&
196 echo bar > untracked-file
197'
198
d6d9e76d 199test_expect_success 'will not clobber WT/index when merging into unborn' '
5902f5f4 200 git merge main &&
5b327081
TR
201 grep foo tracked-file &&
202 git show :tracked-file >expect &&
203 grep foo expect &&
204 grep bar untracked-file
205'
206
7bb1fcc6 207test_done