blob: 758a623cdbb5f7e367bcfce5c239d79258c46e84 [file] [log] [blame]
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +01001#!/bin/sh
2
3test_description='git-merge
4
5Do not overwrite changes.'
6
7. ./test-lib.sh
8
9test_expect_success 'setup' '
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020010 test_commit c0 c0.c &&
11 test_commit c1 c1.c &&
12 test_commit c1a c1.c "c1 a" &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010013 git reset --hard c0 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020014 test_commit c2 c2.c &&
Clemens Buchacher189645c2010-10-10 10:38:58 +020015 git reset --hard c0 &&
16 mkdir sub &&
17 echo "sub/f" > sub/f &&
Clemens Buchacher79808722010-11-15 20:52:19 +010018 mkdir sub2 &&
19 echo "sub2/f" > sub2/f &&
20 git add sub/f sub2/f &&
Clemens Buchacher189645c2010-10-10 10:38:58 +020021 git commit -m sub &&
22 git tag sub &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010023 echo "VERY IMPORTANT CHANGES" > important
24'
25
26test_expect_success 'will not overwrite untracked file' '
27 git reset --hard c1 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020028 cp important c2.c &&
Jared Hancece14e0b2010-07-20 19:18:34 -040029 test_must_fail git merge c2 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020030 test_path_is_missing .git/MERGE_HEAD &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010031 test_cmp important c2.c
32'
33
Clemens Buchacher189645c2010-10-10 10:38:58 +020034test_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
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010042test_expect_success 'will not overwrite new file' '
43 git reset --hard c1 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020044 cp important c2.c &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010045 git add c2.c &&
Jared Hancece14e0b2010-07-20 19:18:34 -040046 test_must_fail git merge c2 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020047 test_path_is_missing .git/MERGE_HEAD &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010048 test_cmp important c2.c
49'
50
51test_expect_success 'will not overwrite staged changes' '
52 git reset --hard c1 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020053 cp important c2.c &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010054 git add c2.c &&
55 rm c2.c &&
Jared Hancece14e0b2010-07-20 19:18:34 -040056 test_must_fail git merge c2 &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020057 test_path_is_missing .git/MERGE_HEAD &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010058 git checkout c2.c &&
59 test_cmp important c2.c
60'
61
Junio C Hamanoc5ab03f2008-12-14 19:40:09 -080062test_expect_success 'will not overwrite removed file' '
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010063 git reset --hard c1 &&
64 git rm c1.c &&
65 git commit -m "rm c1.c" &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020066 cp important c1.c &&
Jared Hancece14e0b2010-07-20 19:18:34 -040067 test_must_fail git merge c1a &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010068 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" &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020075 cp important c1.c &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010076 git add c1.c &&
Jared Hancece14e0b2010-07-20 19:18:34 -040077 test_must_fail git merge c1a &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020078 test_path_is_missing .git/MERGE_HEAD &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010079 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" &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020086 cp important c1.c &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010087 git add c1.c &&
88 rm c1.c &&
Jared Hancece14e0b2010-07-20 19:18:34 -040089 test_must_fail git merge c1a &&
Clemens Buchacher52a0a1b2010-10-10 10:35:43 +020090 test_path_is_missing .git/MERGE_HEAD &&
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +010091 git checkout c1.c &&
92 test_cmp important c1.c
93'
94
Clemens Buchacher30fd3a52012-04-15 01:15:17 +020095test_expect_failure 'will not overwrite unstaged changes in renamed file' '
96 git reset --hard c1 &&
97 git mv c1.c other.c &&
98 git commit -m rename &&
99 cp important other.c &&
100 git merge c1a &&
101 test_cmp important other.c
102'
103
Clemens Buchacher189645c2010-10-10 10:38:58 +0200104test_expect_success 'will not overwrite untracked subtree' '
105 git reset --hard c0 &&
106 rm -rf sub &&
107 mkdir -p sub/f &&
108 cp important sub/f/important &&
109 test_must_fail git merge sub &&
110 test_path_is_missing .git/MERGE_HEAD &&
111 test_cmp important sub/f/important
112'
113
Clemens Buchacher79808722010-11-15 20:52:19 +0100114cat >expect <<\EOF
115error: The following untracked working tree files would be overwritten by merge:
116 sub
117 sub2
118Please move or remove them before you can merge.
Michael J Gruber6f909692011-09-21 09:48:36 +0200119Aborting
Clemens Buchacher79808722010-11-15 20:52:19 +0100120EOF
121
Clemens Buchacherf66caaf2010-10-09 15:53:00 +0200122test_expect_success 'will not overwrite untracked file in leading path' '
Clemens Buchacher189645c2010-10-10 10:38:58 +0200123 git reset --hard c0 &&
124 rm -rf sub &&
125 cp important sub &&
Clemens Buchacher79808722010-11-15 20:52:19 +0100126 cp important sub2 &&
127 test_must_fail git merge sub 2>out &&
128 test_cmp out expect &&
Clemens Buchacher189645c2010-10-10 10:38:58 +0200129 test_path_is_missing .git/MERGE_HEAD &&
Clemens Buchacher79808722010-11-15 20:52:19 +0100130 test_cmp important sub &&
131 test_cmp important sub2 &&
132 rm -f sub sub2
Clemens Buchacher189645c2010-10-10 10:38:58 +0200133'
134
Jeff King8523d072011-03-25 14:08:36 -0400135test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
Clemens Buchacher189645c2010-10-10 10:38:58 +0200136 git reset --hard c0 &&
137 rm -rf sub &&
138 mkdir sub2 &&
139 ln -s sub2 sub &&
140 test_must_fail git merge sub &&
141 test_path_is_missing .git/MERGE_HEAD
142'
143
Johannes Sixt889c6f02013-06-07 22:53:28 +0200144test_expect_success 'will not be confused by symlink in leading path' '
Clemens Buchacher189645c2010-10-10 10:38:58 +0200145 git reset --hard c0 &&
146 rm -rf sub &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200147 test_ln_s_add sub2 sub &&
Clemens Buchacher189645c2010-10-10 10:38:58 +0200148 git commit -m ln &&
149 git checkout sub
150'
151
Clemens Buchacher172b6422010-11-14 23:07:49 +0100152cat >expect <<\EOF
153error: Untracked working tree file 'c0.c' would be overwritten by merge.
154fatal: read-tree failed
155EOF
156
157test_expect_success 'will not overwrite untracked file on unborn branch' '
158 git reset --hard c0 &&
159 git rm -fr . &&
160 git checkout --orphan new &&
161 cp important c0.c &&
Junio C Hamanoc9ea1182011-04-14 14:36:14 -0700162 test_must_fail git merge c0 2>out &&
163 test_i18ncmp out expect
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000164'
165
166test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
Jeff Kingd6d9e762011-03-25 14:09:03 -0400167 test_when_finished "rm c0.c" &&
Clemens Buchacher172b6422010-11-14 23:07:49 +0100168 test_path_is_missing .git/MERGE_HEAD &&
169 test_cmp important c0.c
170'
171
Jeff King97b1b4f2011-03-25 14:10:38 -0400172test_expect_success 'failed merge leaves unborn branch in the womb' '
173 test_must_fail git rev-parse --verify HEAD
174'
175
Thomas Rast5b327082010-08-22 23:06:29 +0200176test_expect_success 'set up unborn branch and content' '
177 git symbolic-ref HEAD refs/heads/unborn &&
178 rm -f .git/index &&
179 echo foo > tracked-file &&
180 git add tracked-file &&
181 echo bar > untracked-file
182'
183
Jeff Kingd6d9e762011-03-25 14:09:03 -0400184test_expect_success 'will not clobber WT/index when merging into unborn' '
Thomas Rast5b327082010-08-22 23:06:29 +0200185 git merge master &&
186 grep foo tracked-file &&
187 git show :tracked-file >expect &&
188 grep foo expect &&
189 grep bar untracked-file
190'
191
Clemens Buchacher7bb1fcc2008-12-10 21:12:59 +0100192test_done