Merge branch 'js/var-git-shell-path'
[git/debian.git] / t / t6436-merge-overwrite.sh
blobccc620477d494b20588ba87bf83e9fb73a90539d
1 #!/bin/sh
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
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
13 test_expect_success 'setup' '
14 test_commit c0 c0.c &&
15 test_commit c1 c1.c &&
16 test_commit c1a c1.c "c1 a" &&
17 git reset --hard c0 &&
18 test_commit c2 c2.c &&
19 git reset --hard c0 &&
20 mkdir sub &&
21 echo "sub/f" > sub/f &&
22 mkdir sub2 &&
23 echo "sub2/f" > sub2/f &&
24 git add sub/f sub2/f &&
25 git commit -m sub &&
26 git tag sub &&
27 echo "VERY IMPORTANT CHANGES" > important
30 test_expect_success 'will not overwrite untracked file' '
31 git reset --hard c1 &&
32 cp important c2.c &&
33 test_must_fail git merge c2 &&
34 test_path_is_missing .git/MERGE_HEAD &&
35 test_cmp important c2.c
38 test_expect_success 'will overwrite tracked file' '
39 git reset --hard c1 &&
40 cp important c2.c &&
41 git add c2.c &&
42 git commit -m important &&
43 git checkout c2
46 test_expect_success 'will not overwrite new file' '
47 git reset --hard c1 &&
48 cp important c2.c &&
49 git add c2.c &&
50 test_must_fail git merge c2 &&
51 test_path_is_missing .git/MERGE_HEAD &&
52 test_cmp important c2.c
55 test_expect_success 'will not overwrite staged changes' '
56 git reset --hard c1 &&
57 cp important c2.c &&
58 git add c2.c &&
59 rm c2.c &&
60 test_must_fail git merge c2 &&
61 test_path_is_missing .git/MERGE_HEAD &&
62 git checkout c2.c &&
63 test_cmp important c2.c
66 test_expect_success 'will not overwrite removed file' '
67 git reset --hard c1 &&
68 git rm c1.c &&
69 git commit -m "rm c1.c" &&
70 cp important c1.c &&
71 test_must_fail git merge c1a &&
72 test_cmp important c1.c &&
73 rm c1.c # Do not leave untracked file in way of future tests
76 test_expect_success 'will not overwrite re-added file' '
77 git reset --hard c1 &&
78 git rm c1.c &&
79 git commit -m "rm c1.c" &&
80 cp important c1.c &&
81 git add c1.c &&
82 test_must_fail git merge c1a &&
83 test_path_is_missing .git/MERGE_HEAD &&
84 test_cmp important c1.c
87 test_expect_success 'will not overwrite removed file with staged changes' '
88 git reset --hard c1 &&
89 git rm c1.c &&
90 git commit -m "rm c1.c" &&
91 cp important c1.c &&
92 git add c1.c &&
93 rm c1.c &&
94 test_must_fail git merge c1a &&
95 test_path_is_missing .git/MERGE_HEAD &&
96 git checkout c1.c &&
97 test_cmp important c1.c
100 test_expect_success 'will not overwrite unstaged changes in renamed file' '
101 git reset --hard c1 &&
102 git mv c1.c other.c &&
103 git commit -m rename &&
104 cp important other.c &&
105 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
106 then
107 test_must_fail git merge c1a >out 2>err &&
108 test_grep "would be overwritten by merge" err &&
109 test_cmp important other.c &&
110 test_path_is_missing .git/MERGE_HEAD
111 else
112 test_must_fail git merge c1a >out &&
113 test_grep "Refusing to lose dirty file at other.c" out &&
114 test_path_is_file other.c~HEAD &&
115 test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
116 test_cmp important other.c
120 test_expect_success 'will not overwrite untracked subtree' '
121 git reset --hard c0 &&
122 rm -rf sub &&
123 mkdir -p sub/f &&
124 cp important sub/f/important &&
125 test_must_fail git merge sub &&
126 test_path_is_missing .git/MERGE_HEAD &&
127 test_cmp important sub/f/important
130 cat >expect <<\EOF
131 error: The following untracked working tree files would be overwritten by merge:
133 sub2
134 Please move or remove them before you merge.
135 Aborting
138 test_expect_success 'will not overwrite untracked file in leading path' '
139 git reset --hard c0 &&
140 rm -rf sub &&
141 cp important sub &&
142 cp important sub2 &&
143 test_must_fail git merge sub 2>out &&
144 test_cmp out expect &&
145 test_path_is_missing .git/MERGE_HEAD &&
146 test_cmp important sub &&
147 test_cmp important sub2 &&
148 rm -f sub sub2
151 test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
152 git reset --hard c0 &&
153 rm -rf sub &&
154 mkdir sub2 &&
155 ln -s sub2 sub &&
156 test_must_fail git merge sub &&
157 test_path_is_missing .git/MERGE_HEAD
160 test_expect_success 'will not be confused by symlink in leading path' '
161 git reset --hard c0 &&
162 rm -rf sub &&
163 test_ln_s_add sub2 sub &&
164 git commit -m ln &&
165 git checkout sub
168 cat >expect <<\EOF
169 error: Untracked working tree file 'c0.c' would be overwritten by merge.
170 fatal: read-tree failed
173 test_expect_success 'will not overwrite untracked file on unborn branch' '
174 git reset --hard c0 &&
175 git rm -fr . &&
176 git checkout --orphan new &&
177 cp important c0.c &&
178 test_must_fail git merge c0 2>out &&
179 test_cmp out expect
182 test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
183 test_when_finished "rm c0.c" &&
184 test_path_is_missing .git/MERGE_HEAD &&
185 test_cmp important c0.c
188 test_expect_success 'failed merge leaves unborn branch in the womb' '
189 test_must_fail git rev-parse --verify HEAD
192 test_expect_success 'set up unborn branch and content' '
193 git symbolic-ref HEAD refs/heads/unborn &&
194 rm -f .git/index &&
195 echo foo > tracked-file &&
196 git add tracked-file &&
197 echo bar > untracked-file
200 test_expect_success 'will not clobber WT/index when merging into unborn' '
201 git merge main &&
202 grep foo tracked-file &&
203 git show :tracked-file >expect &&
204 grep foo expect &&
205 grep bar untracked-file
208 test_done