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