Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ./common.sh
19 function test_cherrypick_basic {
20 local testroot=`test_init cherrypick_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
23 ret="$?"
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
26 return 1
27 fi
29 (cd $testroot/repo && git checkout -q -b newbranch)
30 echo "modified delta on branch" > $testroot/repo/gamma/delta
31 git_commit $testroot/repo -m "committing to delta on newbranch"
33 echo "modified alpha on branch" > $testroot/repo/alpha
34 (cd $testroot/repo && git rm -q beta)
35 echo "new file on branch" > $testroot/repo/epsilon/new
36 (cd $testroot/repo && git add epsilon/new)
37 git_commit $testroot/repo -m "committing more changes on newbranch"
39 local branch_rev=`git_show_head $testroot/repo`
41 (cd $testroot/wt && got cherrypick $branch_rev > $testroot/stdout)
43 echo "G alpha" > $testroot/stdout.expected
44 echo "D beta" >> $testroot/stdout.expected
45 echo "A epsilon/new" >> $testroot/stdout.expected
46 echo "Merged commit $branch_rev" >> $testroot/stdout.expected
48 cmp -s $testroot/stdout.expected $testroot/stdout
49 ret="$?"
50 if [ "$ret" != "0" ]; then
51 diff -u $testroot/stdout.expected $testroot/stdout
52 test_done "$testroot" "$ret"
53 return 1
54 fi
56 echo "modified alpha on branch" > $testroot/content.expected
57 cat $testroot/wt/alpha > $testroot/content
58 cmp -s $testroot/content.expected $testroot/content
59 ret="$?"
60 if [ "$ret" != "0" ]; then
61 diff -u $testroot/content.expected $testroot/content
62 test_done "$testroot" "$ret"
63 return 1
64 fi
66 if [ -e $testroot/wt/beta ]; then
67 echo "removed file beta still exists on disk" >&2
68 test_done "$testroot" "1"
69 return 1
70 fi
72 echo "new file on branch" > $testroot/content.expected
73 cat $testroot/wt/epsilon/new > $testroot/content
74 cmp -s $testroot/content.expected $testroot/content
75 ret="$?"
76 if [ "$ret" != "0" ]; then
77 diff -u $testroot/content.expected $testroot/content
78 test_done "$testroot" "$ret"
79 return 1
80 fi
82 echo 'M alpha' > $testroot/stdout.expected
83 echo 'D beta' >> $testroot/stdout.expected
84 echo 'A epsilon/new' >> $testroot/stdout.expected
86 (cd $testroot/wt && got status > $testroot/stdout)
88 cmp -s $testroot/stdout.expected $testroot/stdout
89 ret="$?"
90 if [ "$ret" != "0" ]; then
91 diff -u $testroot/stdout.expected $testroot/stdout
92 fi
93 test_done "$testroot" "$ret"
94 }
96 function test_cherrypick_root_commit {
97 local testroot=`test_init cherrypick_root_commit`
99 got checkout $testroot/repo $testroot/wt > /dev/null
100 ret="$?"
101 if [ "$ret" != "0" ]; then
102 test_done "$testroot" "$ret"
103 return 1
104 fi
106 (cd $testroot/repo && git checkout -q -b newbranch)
107 (cd $testroot/repo && git rm -q alpha)
108 (cd $testroot/repo && git rm -q beta)
109 (cd $testroot/repo && git rm -q epsilon/zeta)
110 (cd $testroot/repo && git rm -q gamma/delta)
111 mkdir -p $testroot/repo/epsilon
112 echo "new file on branch" > $testroot/repo/epsilon/new
113 (cd $testroot/repo && git add epsilon/new)
114 git_commit $testroot/repo -m "committing on newbranch"
116 echo "modified new file on branch" >> $testroot/repo/epsilon/new
117 git_commit $testroot/repo -m "committing on newbranch again"
119 tree=`git_show_tree $testroot/repo`
120 root_commit=`git_commit_tree $testroot/repo "new root commit" $tree`
122 (cd $testroot/wt && got cherrypick $root_commit > $testroot/stdout)
124 echo "A epsilon/new" > $testroot/stdout.expected
125 echo "Merged commit $root_commit" >> $testroot/stdout.expected
127 cmp -s $testroot/stdout.expected $testroot/stdout
128 ret="$?"
129 if [ "$ret" != "0" ]; then
130 diff -u $testroot/stdout.expected $testroot/stdout
131 test_done "$testroot" "$ret"
132 return 1
133 fi
135 echo "new file on branch" > $testroot/content.expected
136 echo "modified new file on branch" >> $testroot/content.expected
137 cat $testroot/wt/epsilon/new > $testroot/content
138 cmp -s $testroot/content.expected $testroot/content
139 ret="$?"
140 if [ "$ret" != "0" ]; then
141 diff -u $testroot/content.expected $testroot/content
142 test_done "$testroot" "$ret"
143 return 1
144 fi
146 echo 'A epsilon/new' > $testroot/stdout.expected
148 (cd $testroot/wt && got status > $testroot/stdout)
150 cmp -s $testroot/stdout.expected $testroot/stdout
151 ret="$?"
152 if [ "$ret" != "0" ]; then
153 diff -u $testroot/stdout.expected $testroot/stdout
154 fi
155 test_done "$testroot" "$ret"
158 function test_cherrypick_into_work_tree_with_conflicts {
159 local testroot=`test_init cherrypick_into_work_tree_with_conflicts`
161 got checkout $testroot/repo $testroot/wt > /dev/null
162 ret="$?"
163 if [ "$ret" != "0" ]; then
164 test_done "$testroot" "$ret"
165 return 1
166 fi
168 (cd $testroot/repo && git checkout -q -b newbranch)
169 echo "modified delta on branch" > $testroot/repo/gamma/delta
170 git_commit $testroot/repo -m "committing to delta on newbranch"
172 echo "modified alpha on branch" > $testroot/repo/alpha
173 (cd $testroot/repo && git rm -q beta)
174 echo "new file on branch" > $testroot/repo/epsilon/new
175 (cd $testroot/repo && git add epsilon/new)
176 git_commit $testroot/repo -m "committing more changes on newbranch"
178 local branch_rev=`git_show_head $testroot/repo`
180 # fake a merge conflict
181 echo '<<<<<<<' > $testroot/wt/alpha
182 echo 'alpha' >> $testroot/wt/alpha
183 echo '=======' >> $testroot/wt/alpha
184 echo 'alpha, too' >> $testroot/wt/alpha
185 echo '>>>>>>>' >> $testroot/wt/alpha
186 cp $testroot/wt/alpha $testroot/content.expected
188 echo "C alpha" > $testroot/stdout.expected
189 (cd $testroot/wt && got status > $testroot/stdout)
190 cmp -s $testroot/stdout.expected $testroot/stdout
191 ret="$?"
192 if [ "$ret" != "0" ]; then
193 diff -u $testroot/stdout.expected $testroot/stdout
194 test_done "$testroot" "$ret"
195 return 1
196 fi
198 (cd $testroot/wt && got cherrypick $branch_rev \
199 > $testroot/stdout 2> $testroot/stderr)
200 ret="$?"
201 if [ "$ret" == "0" ]; then
202 echo "cherrypick succeeded unexpectedly" >&2
203 test_done "$testroot" "1"
204 return 1
205 fi
207 echo -n > $testroot/stdout.expected
208 echo -n "got: work tree contains conflicted files; " \
209 > $testroot/stderr.expected
210 echo "these conflicts must be resolved first" \
211 >> $testroot/stderr.expected
213 cmp -s $testroot/stdout.expected $testroot/stdout
214 ret="$?"
215 if [ "$ret" != "0" ]; then
216 diff -u $testroot/stdout.expected $testroot/stdout
217 test_done "$testroot" "$ret"
218 return 1
219 fi
221 cmp -s $testroot/stderr.expected $testroot/stderr
222 ret="$?"
223 if [ "$ret" != "0" ]; then
224 diff -u $testroot/stderr.expected $testroot/stderr
225 test_done "$testroot" "$ret"
226 return 1
227 fi
229 cmp -s $testroot/content.expected $testroot/wt/alpha
230 ret="$?"
231 if [ "$ret" != "0" ]; then
232 diff -u $testroot/content.expected $testroot/wt/alpha
233 fi
234 test_done "$testroot" "$ret"
237 function test_cherrypick_modified_submodule {
238 local testroot=`test_init cherrypick_modified_submodules`
240 make_single_file_repo $testroot/repo2 foo
242 (cd $testroot/repo && git submodule -q add ../repo2)
243 (cd $testroot/repo && git commit -q -m 'adding submodule')
245 got checkout $testroot/repo $testroot/wt > /dev/null
247 echo "modified foo" > $testroot/repo2/foo
248 (cd $testroot/repo2 && git commit -q -a -m 'modified a submodule')
250 (cd $testroot/repo && git checkout -q -b newbranch)
251 # Update the repo/repo2 submodule link on newbranch
252 (cd $testroot/repo && git -C repo2 pull -q)
253 (cd $testroot/repo && git add repo2)
254 git_commit $testroot/repo -m "modified submodule link"
255 local commit_id=`git_show_head $testroot/repo`
257 # This cherrypick is a no-op because Got's file index
258 # does not track submodules.
259 (cd $testroot/wt && got cherrypick $commit_id > $testroot/stdout)
261 echo -n > $testroot/stdout.expected
262 cmp -s $testroot/stdout.expected $testroot/stdout
263 ret="$?"
264 if [ "$ret" != "0" ]; then
265 diff -u $testroot/stdout.expected $testroot/stdout
266 fi
267 test_done "$testroot" "$ret"
270 function test_cherrypick_added_submodule {
271 local testroot=`test_init cherrypick_added_submodules`
273 got checkout $testroot/repo $testroot/wt > /dev/null
275 make_single_file_repo $testroot/repo2 foo
277 # Add the repo/repo2 submodule on newbranch
278 (cd $testroot/repo && git checkout -q -b newbranch)
279 (cd $testroot/repo && git submodule -q add ../repo2)
280 (cd $testroot/repo && git commit -q -m 'adding submodule')
281 local commit_id=`git_show_head $testroot/repo`
283 (cd $testroot/wt && got cherrypick $commit_id > $testroot/stdout)
285 echo "A .gitmodules" > $testroot/stdout.expected
286 echo "Merged commit $commit_id" >> $testroot/stdout.expected
287 cmp -s $testroot/stdout.expected $testroot/stdout
288 ret="$?"
289 if [ "$ret" != "0" ]; then
290 diff -u $testroot/stdout.expected $testroot/stdout
291 fi
292 test_done "$testroot" "$ret"
295 function test_cherrypick_conflict_wt_file_vs_repo_submodule {
296 local testroot=`test_init cherrypick_conflict_wt_file_vs_repo_submodule`
298 got checkout $testroot/repo $testroot/wt > /dev/null
300 # Add a file which will clash with the submodule
301 echo "This is a file called repo2" > $testroot/wt/repo2
302 (cd $testroot/wt && got add repo2 > /dev/null)
303 (cd $testroot/wt && got commit -m 'add file repo2' > /dev/null)
304 ret="$?"
305 if [ "$ret" != "0" ]; then
306 echo "commit failed unexpectedly" >&2
307 test_done "$testroot" "1"
308 return 1
309 fi
311 make_single_file_repo $testroot/repo2 foo
313 # Add the repo/repo2 submodule on newbranch
314 (cd $testroot/repo && git checkout -q -b newbranch)
315 (cd $testroot/repo && git submodule -q add ../repo2)
316 (cd $testroot/repo && git commit -q -m 'adding submodule')
317 local commit_id=`git_show_head $testroot/repo`
319 # Modify the clashing file such that any modifications brought
320 # in by 'got cherrypick' would require a merge.
321 echo "This file was changed" > $testroot/wt/repo2
323 (cd $testroot/wt && got update >/dev/null)
324 (cd $testroot/wt && got cherrypick $commit_id > $testroot/stdout)
326 echo "A .gitmodules" > $testroot/stdout.expected
327 echo "Merged commit $commit_id" >> $testroot/stdout.expected
328 cmp -s $testroot/stdout.expected $testroot/stdout
329 ret="$?"
330 if [ "$ret" != "0" ]; then
331 diff -u $testroot/stdout.expected $testroot/stdout
332 test_done "$testroot" "$ret"
333 return 1
334 fi
336 (cd $testroot/wt && got status > $testroot/stdout)
338 echo "A .gitmodules" > $testroot/stdout.expected
339 echo "M repo2" >> $testroot/stdout.expected
340 cmp -s $testroot/stdout.expected $testroot/stdout
341 ret="$?"
342 if [ "$ret" != "0" ]; then
343 diff -u $testroot/stdout.expected $testroot/stdout
344 fi
345 test_done "$testroot" "$ret"
348 run_test test_cherrypick_basic
349 run_test test_cherrypick_root_commit
350 run_test test_cherrypick_into_work_tree_with_conflicts
351 run_test test_cherrypick_modified_submodule
352 run_test test_cherrypick_added_submodule
353 run_test test_cherrypick_conflict_wt_file_vs_repo_submodule