3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
19 function test_cherrypick_basic {
20 local testroot=`test_init cherrypick_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
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
50 if [ "$ret" != "0" ]; then
51 diff -u $testroot/stdout.expected $testroot/stdout
52 test_done "$testroot" "$ret"
56 echo "modified alpha on branch" > $testroot/content.expected
57 cat $testroot/wt/alpha > $testroot/content
58 cmp -s $testroot/content.expected $testroot/content
60 if [ "$ret" != "0" ]; then
61 diff -u $testroot/content.expected $testroot/content
62 test_done "$testroot" "$ret"
66 if [ -e $testroot/wt/beta ]; then
67 echo "removed file beta still exists on disk" >&2
68 test_done "$testroot" "1"
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
76 if [ "$ret" != "0" ]; then
77 diff -u $testroot/content.expected $testroot/content
78 test_done "$testroot" "$ret"
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
90 if [ "$ret" != "0" ]; then
91 diff -u $testroot/stdout.expected $testroot/stdout
93 test_done "$testroot" "$ret"
96 function test_cherrypick_root_commit {
97 local testroot=`test_init cherrypick_root_commit`
99 got checkout $testroot/repo $testroot/wt > /dev/null
101 if [ "$ret" != "0" ]; then
102 test_done "$testroot" "$ret"
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
129 if [ "$ret" != "0" ]; then
130 diff -u $testroot/stdout.expected $testroot/stdout
131 test_done "$testroot" "$ret"
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
140 if [ "$ret" != "0" ]; then
141 diff -u $testroot/content.expected $testroot/content
142 test_done "$testroot" "$ret"
146 echo 'A epsilon/new' > $testroot/stdout.expected
148 (cd $testroot/wt && got status > $testroot/stdout)
150 cmp -s $testroot/stdout.expected $testroot/stdout
152 if [ "$ret" != "0" ]; then
153 diff -u $testroot/stdout.expected $testroot/stdout
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
163 if [ "$ret" != "0" ]; then
164 test_done "$testroot" "$ret"
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
192 if [ "$ret" != "0" ]; then
193 diff -u $testroot/stdout.expected $testroot/stdout
194 test_done "$testroot" "$ret"
198 (cd $testroot/wt && got cherrypick $branch_rev \
199 > $testroot/stdout 2> $testroot/stderr)
201 if [ "$ret" == "0" ]; then
202 echo "cherrypick succeeded unexpectedly" >&2
203 test_done "$testroot" "1"
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
215 if [ "$ret" != "0" ]; then
216 diff -u $testroot/stdout.expected $testroot/stdout
217 test_done "$testroot" "$ret"
221 cmp -s $testroot/stderr.expected $testroot/stderr
223 if [ "$ret" != "0" ]; then
224 diff -u $testroot/stderr.expected $testroot/stderr
225 test_done "$testroot" "$ret"
229 cmp -s $testroot/content.expected $testroot/wt/alpha
231 if [ "$ret" != "0" ]; then
232 diff -u $testroot/content.expected $testroot/wt/alpha
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
264 if [ "$ret" != "0" ]; then
265 diff -u $testroot/stdout.expected $testroot/stdout
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
289 if [ "$ret" != "0" ]; then
290 diff -u $testroot/stdout.expected $testroot/stdout
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)
305 if [ "$ret" != "0" ]; then
306 echo "commit failed unexpectedly" >&2
307 test_done "$testroot" "1"
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
330 if [ "$ret" != "0" ]; then
331 diff -u $testroot/stdout.expected $testroot/stdout
332 test_done "$testroot" "$ret"
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
342 if [ "$ret" != "0" ]; then
343 diff -u $testroot/stdout.expected $testroot/stdout
345 test_done "$testroot" "$ret"
348 function test_cherrypick_modified_symlinks {
349 local testroot=`test_init cherrypick_modified_symlinks`
351 (cd $testroot/repo && ln -s alpha alpha.link)
352 (cd $testroot/repo && ln -s epsilon epsilon.link)
353 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
354 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
355 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
356 (cd $testroot/repo && git add .)
357 git_commit $testroot/repo -m "add symlinks"
358 local commit_id1=`git_show_head $testroot/repo`
360 got branch -r $testroot/repo foo
362 got checkout -b foo $testroot/repo $testroot/wt > /dev/null
364 (cd $testroot/repo && ln -sf beta alpha.link)
365 (cd $testroot/repo && ln -sfh gamma epsilon.link)
366 (cd $testroot/repo && ln -sf ../gamma/delta epsilon/beta.link)
367 (cd $testroot/repo && ln -sf .got/bar $testroot/repo/dotgotfoo.link)
368 (cd $testroot/repo && git rm -q nonexistent.link)
369 (cd $testroot/repo && ln -sf epsilon/zeta zeta.link)
370 (cd $testroot/repo && git add .)
371 git_commit $testroot/repo -m "change symlinks"
372 local commit_id2=`git_show_head $testroot/repo`
374 (cd $testroot/wt && got cherrypick $commit_id2 > $testroot/stdout)
376 echo "G alpha.link" > $testroot/stdout.expected
377 echo "G epsilon/beta.link" >> $testroot/stdout.expected
378 echo "A dotgotfoo.link" >> $testroot/stdout.expected
379 echo "G epsilon.link" >> $testroot/stdout.expected
380 echo "D nonexistent.link" >> $testroot/stdout.expected
381 echo "A zeta.link" >> $testroot/stdout.expected
382 echo "Merged commit $commit_id2" >> $testroot/stdout.expected
383 cmp -s $testroot/stdout.expected $testroot/stdout
385 if [ "$ret" != "0" ]; then
386 diff -u $testroot/stdout.expected $testroot/stdout
387 test_done "$testroot" "$ret"
391 if ! [ -h $testroot/wt/alpha.link ]; then
392 echo "alpha.link is not a symlink"
393 test_done "$testroot" "1"
397 readlink $testroot/wt/alpha.link > $testroot/stdout
398 echo "beta" > $testroot/stdout.expected
399 cmp -s $testroot/stdout.expected $testroot/stdout
401 if [ "$ret" != "0" ]; then
402 diff -u $testroot/stdout.expected $testroot/stdout
403 test_done "$testroot" "$ret"
407 if ! [ -h $testroot/wt/epsilon.link ]; then
408 echo "epsilon.link is not a symlink"
409 test_done "$testroot" "1"
413 readlink $testroot/wt/epsilon.link > $testroot/stdout
414 echo "gamma" > $testroot/stdout.expected
415 cmp -s $testroot/stdout.expected $testroot/stdout
417 if [ "$ret" != "0" ]; then
418 diff -u $testroot/stdout.expected $testroot/stdout
419 test_done "$testroot" "$ret"
423 if [ -h $testroot/wt/passwd.link ]; then
424 echo -n "passwd.link symlink points outside of work tree: " >&2
425 readlink $testroot/wt/passwd.link >&2
426 test_done "$testroot" "1"
430 echo -n "/etc/passwd" > $testroot/content.expected
431 cp $testroot/wt/passwd.link $testroot/content
433 cmp -s $testroot/content.expected $testroot/content
435 if [ "$ret" != "0" ]; then
436 diff -u $testroot/content.expected $testroot/content
437 test_done "$testroot" "$ret"
441 readlink $testroot/wt/epsilon/beta.link > $testroot/stdout
442 echo "../gamma/delta" > $testroot/stdout.expected
443 cmp -s $testroot/stdout.expected $testroot/stdout
445 if [ "$ret" != "0" ]; then
446 diff -u $testroot/stdout.expected $testroot/stdout
447 test_done "$testroot" "$ret"
451 if [ -h $testroot/wt/nonexistent.link ]; then
452 echo -n "nonexistent.link still exists on disk: " >&2
453 readlink $testroot/wt/nonexistent.link >&2
454 test_done "$testroot" "1"
458 test_done "$testroot" "0"
461 run_test test_cherrypick_basic
462 run_test test_cherrypick_root_commit
463 run_test test_cherrypick_into_work_tree_with_conflicts
464 run_test test_cherrypick_modified_submodule
465 run_test test_cherrypick_added_submodule
466 run_test test_cherrypick_conflict_wt_file_vs_repo_submodule
467 run_test test_cherrypick_modified_symlinks