Start the 2.46 cycle
[git.git] / t / t6436-merge-overwrite.sh
blob4f4376421e7da28e22f58258061bf58f3a8bc0c8
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-lib.sh
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
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
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
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
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
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 rm c1.c # Do not leave untracked file in way of future tests
75 test_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" &&
79 cp important c1.c &&
80 git add c1.c &&
81 test_must_fail git merge c1a &&
82 test_path_is_missing .git/MERGE_HEAD &&
83 test_cmp important c1.c
86 test_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" &&
90 cp important c1.c &&
91 git add c1.c &&
92 rm c1.c &&
93 test_must_fail git merge c1a &&
94 test_path_is_missing .git/MERGE_HEAD &&
95 git checkout c1.c &&
96 test_cmp important c1.c
99 test_expect_success 'will not overwrite unstaged changes in renamed file' '
100 git reset --hard c1 &&
101 git mv c1.c other.c &&
102 git commit -m rename &&
103 cp important other.c &&
104 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
105 then
106 test_must_fail git merge c1a >out 2>err &&
107 test_grep "would be overwritten by merge" err &&
108 test_cmp important other.c &&
109 test_path_is_missing .git/MERGE_HEAD
110 else
111 test_must_fail git merge c1a >out &&
112 test_grep "Refusing to lose dirty file at other.c" out &&
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
119 test_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
129 cat >expect <<\EOF
130 error: The following untracked working tree files would be overwritten by merge:
132 sub2
133 Please move or remove them before you merge.
134 Aborting
137 test_expect_success 'will not overwrite untracked file in leading path' '
138 git reset --hard c0 &&
139 rm -rf sub &&
140 cp important sub &&
141 cp important sub2 &&
142 test_must_fail git merge sub 2>out &&
143 test_cmp out expect &&
144 test_path_is_missing .git/MERGE_HEAD &&
145 test_cmp important sub &&
146 test_cmp important sub2 &&
147 rm -f sub sub2
150 test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
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
159 test_expect_success 'will not be confused by symlink in leading path' '
160 git reset --hard c0 &&
161 rm -rf sub &&
162 test_ln_s_add sub2 sub &&
163 git commit -m ln &&
164 git checkout sub
167 cat >expect <<\EOF
168 error: Untracked working tree file 'c0.c' would be overwritten by merge.
169 fatal: read-tree failed
172 test_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 &&
177 test_must_fail git merge c0 2>out &&
178 test_cmp out expect
181 test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
182 test_when_finished "rm c0.c" &&
183 test_path_is_missing .git/MERGE_HEAD &&
184 test_cmp important c0.c
187 test_expect_success 'failed merge leaves unborn branch in the womb' '
188 test_must_fail git rev-parse --verify HEAD
191 test_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
199 test_expect_success 'will not clobber WT/index when merging into unborn' '
200 git merge main &&
201 grep foo tracked-file &&
202 git show :tracked-file >expect &&
203 grep foo expect &&
204 grep bar untracked-file
207 test_done