]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6436-merge-overwrite.sh
Merge branch 'en/ort-perf-batch-9'
[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 &&
7bb1fcc6
CB
71 test_cmp important c1.c
72'
73
74test_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" &&
52a0a1bd 78 cp important c1.c &&
7bb1fcc6 79 git add c1.c &&
ce14e0b2 80 test_must_fail git merge c1a &&
52a0a1bd 81 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
82 test_cmp important c1.c
83'
84
85test_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" &&
52a0a1bd 89 cp important c1.c &&
7bb1fcc6
CB
90 git add c1.c &&
91 rm c1.c &&
ce14e0b2 92 test_must_fail git merge c1a &&
52a0a1bd 93 test_path_is_missing .git/MERGE_HEAD &&
7bb1fcc6
CB
94 git checkout c1.c &&
95 test_cmp important c1.c
96'
97
64b1abe9 98test_expect_success 'will not overwrite unstaged changes in renamed file' '
30fd3a54
CB
99 git reset --hard c1 &&
100 git mv c1.c other.c &&
101 git commit -m rename &&
102 cp important other.c &&
23bef2e3
EN
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
30fd3a54
CB
116'
117
189645ca
CB
118test_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
7980872d
CB
128cat >expect <<\EOF
129error: The following untracked working tree files would be overwritten by merge:
130 sub
131 sub2
c2691e2a 132Please move or remove them before you merge.
6f90969b 133Aborting
7980872d
CB
134EOF
135
f66caaf9 136test_expect_success 'will not overwrite untracked file in leading path' '
189645ca
CB
137 git reset --hard c0 &&
138 rm -rf sub &&
139 cp important sub &&
7980872d
CB
140 cp important sub2 &&
141 test_must_fail git merge sub 2>out &&
1108cea7 142 test_cmp out expect &&
189645ca 143 test_path_is_missing .git/MERGE_HEAD &&
7980872d
CB
144 test_cmp important sub &&
145 test_cmp important sub2 &&
146 rm -f sub sub2
189645ca
CB
147'
148
8523d071 149test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
189645ca
CB
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
889c6f0e 158test_expect_success 'will not be confused by symlink in leading path' '
189645ca
CB
159 git reset --hard c0 &&
160 rm -rf sub &&
889c6f0e 161 test_ln_s_add sub2 sub &&
189645ca
CB
162 git commit -m ln &&
163 git checkout sub
164'
165
172b6428
CB
166cat >expect <<\EOF
167error: Untracked working tree file 'c0.c' would be overwritten by merge.
168fatal: read-tree failed
169EOF
170
171test_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 &&
c9ea118e 176 test_must_fail git merge c0 2>out &&
1108cea7 177 test_cmp out expect
bacec478
ÆAB
178'
179
180test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
d6d9e76d 181 test_when_finished "rm c0.c" &&
172b6428
CB
182 test_path_is_missing .git/MERGE_HEAD &&
183 test_cmp important c0.c
184'
185
97b1b4f3
JK
186test_expect_success 'failed merge leaves unborn branch in the womb' '
187 test_must_fail git rev-parse --verify HEAD
188'
189
5b327081
TR
190test_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
d6d9e76d 198test_expect_success 'will not clobber WT/index when merging into unborn' '
5902f5f4 199 git merge main &&
5b327081
TR
200 grep foo tracked-file &&
201 git show :tracked-file >expect &&
202 grep foo expect &&
203 grep bar untracked-file
204'
205
7bb1fcc6 206test_done