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 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 echo "modified new file on branch" > $testroot/repo/epsilon/new
42 git_commit $testroot/repo -m "committing more changes on newbranch"
43 local branch_rev2=`git_show_head $testroot/repo`
45 (cd $testroot/wt && got cherrypick $branch_rev > $testroot/stdout)
47 echo "G alpha" > $testroot/stdout.expected
48 echo "D beta" >> $testroot/stdout.expected
49 echo "A epsilon/new" >> $testroot/stdout.expected
50 echo "Merged commit $branch_rev" >> $testroot/stdout.expected
52 cmp -s $testroot/stdout.expected $testroot/stdout
53 ret="$?"
54 if [ "$ret" != "0" ]; then
55 diff -u $testroot/stdout.expected $testroot/stdout
56 test_done "$testroot" "$ret"
57 return 1
58 fi
60 echo "modified alpha on branch" > $testroot/content.expected
61 cat $testroot/wt/alpha > $testroot/content
62 cmp -s $testroot/content.expected $testroot/content
63 ret="$?"
64 if [ "$ret" != "0" ]; then
65 diff -u $testroot/content.expected $testroot/content
66 test_done "$testroot" "$ret"
67 return 1
68 fi
70 if [ -e $testroot/wt/beta ]; then
71 echo "removed file beta still exists on disk" >&2
72 test_done "$testroot" "1"
73 return 1
74 fi
76 echo "new file on branch" > $testroot/content.expected
77 cat $testroot/wt/epsilon/new > $testroot/content
78 cmp -s $testroot/content.expected $testroot/content
79 ret="$?"
80 if [ "$ret" != "0" ]; then
81 diff -u $testroot/content.expected $testroot/content
82 test_done "$testroot" "$ret"
83 return 1
84 fi
86 echo 'M alpha' > $testroot/stdout.expected
87 echo 'D beta' >> $testroot/stdout.expected
88 echo 'A epsilon/new' >> $testroot/stdout.expected
90 (cd $testroot/wt && got status > $testroot/stdout)
92 cmp -s $testroot/stdout.expected $testroot/stdout
93 ret="$?"
94 if [ "$ret" != "0" ]; then
95 diff -u $testroot/stdout.expected $testroot/stdout
96 test_done "$testroot" "$ret"
97 return 1
98 fi
100 (cd $testroot/wt && got cherrypick $branch_rev2 > $testroot/stdout)
102 echo "G epsilon/new" > $testroot/stdout.expected
103 echo "Merged commit $branch_rev2" >> $testroot/stdout.expected
105 cmp -s $testroot/stdout.expected $testroot/stdout
106 ret="$?"
107 if [ "$ret" != "0" ]; then
108 diff -u $testroot/stdout.expected $testroot/stdout
109 test_done "$testroot" "$ret"
110 return 1
111 fi
113 echo 'M alpha' > $testroot/stdout.expected
114 echo 'D beta' >> $testroot/stdout.expected
115 echo 'A epsilon/new' >> $testroot/stdout.expected
117 (cd $testroot/wt && got status > $testroot/stdout)
119 cmp -s $testroot/stdout.expected $testroot/stdout
120 ret="$?"
121 if [ "$ret" != "0" ]; then
122 diff -u $testroot/stdout.expected $testroot/stdout
123 fi
124 test_done "$testroot" "$ret"
127 test_cherrypick_root_commit() {
128 local testroot=`test_init cherrypick_root_commit`
130 got checkout $testroot/repo $testroot/wt > /dev/null
131 ret="$?"
132 if [ "$ret" != "0" ]; then
133 test_done "$testroot" "$ret"
134 return 1
135 fi
137 (cd $testroot/repo && git checkout -q -b newbranch)
138 (cd $testroot/repo && git rm -q alpha)
139 (cd $testroot/repo && git rm -q beta)
140 (cd $testroot/repo && git rm -q epsilon/zeta)
141 (cd $testroot/repo && git rm -q gamma/delta)
142 mkdir -p $testroot/repo/epsilon
143 echo "new file on branch" > $testroot/repo/epsilon/new
144 (cd $testroot/repo && git add epsilon/new)
145 git_commit $testroot/repo -m "committing on newbranch"
147 echo "modified new file on branch" >> $testroot/repo/epsilon/new
148 git_commit $testroot/repo -m "committing on newbranch again"
150 tree=`git_show_tree $testroot/repo`
151 root_commit=`git_commit_tree $testroot/repo "new root commit" $tree`
153 (cd $testroot/wt && got cherrypick $root_commit > $testroot/stdout)
155 echo "A epsilon/new" > $testroot/stdout.expected
156 echo "Merged commit $root_commit" >> $testroot/stdout.expected
158 cmp -s $testroot/stdout.expected $testroot/stdout
159 ret="$?"
160 if [ "$ret" != "0" ]; then
161 diff -u $testroot/stdout.expected $testroot/stdout
162 test_done "$testroot" "$ret"
163 return 1
164 fi
166 echo "new file on branch" > $testroot/content.expected
167 echo "modified new file on branch" >> $testroot/content.expected
168 cat $testroot/wt/epsilon/new > $testroot/content
169 cmp -s $testroot/content.expected $testroot/content
170 ret="$?"
171 if [ "$ret" != "0" ]; then
172 diff -u $testroot/content.expected $testroot/content
173 test_done "$testroot" "$ret"
174 return 1
175 fi
177 echo 'A epsilon/new' > $testroot/stdout.expected
179 (cd $testroot/wt && got status > $testroot/stdout)
181 cmp -s $testroot/stdout.expected $testroot/stdout
182 ret="$?"
183 if [ "$ret" != "0" ]; then
184 diff -u $testroot/stdout.expected $testroot/stdout
185 fi
186 test_done "$testroot" "$ret"
189 test_cherrypick_into_work_tree_with_conflicts() {
190 local testroot=`test_init cherrypick_into_work_tree_with_conflicts`
192 got checkout $testroot/repo $testroot/wt > /dev/null
193 ret="$?"
194 if [ "$ret" != "0" ]; then
195 test_done "$testroot" "$ret"
196 return 1
197 fi
199 (cd $testroot/repo && git checkout -q -b newbranch)
200 echo "modified delta on branch" > $testroot/repo/gamma/delta
201 git_commit $testroot/repo -m "committing to delta on newbranch"
203 echo "modified alpha on branch" > $testroot/repo/alpha
204 (cd $testroot/repo && git rm -q beta)
205 echo "new file on branch" > $testroot/repo/epsilon/new
206 (cd $testroot/repo && git add epsilon/new)
207 git_commit $testroot/repo -m "committing more changes on newbranch"
209 local branch_rev=`git_show_head $testroot/repo`
211 # fake a merge conflict
212 echo '<<<<<<<' > $testroot/wt/alpha
213 echo 'alpha' >> $testroot/wt/alpha
214 echo '=======' >> $testroot/wt/alpha
215 echo 'alpha, too' >> $testroot/wt/alpha
216 echo '>>>>>>>' >> $testroot/wt/alpha
217 cp $testroot/wt/alpha $testroot/content.expected
219 echo "C alpha" > $testroot/stdout.expected
220 (cd $testroot/wt && got status > $testroot/stdout)
221 cmp -s $testroot/stdout.expected $testroot/stdout
222 ret="$?"
223 if [ "$ret" != "0" ]; then
224 diff -u $testroot/stdout.expected $testroot/stdout
225 test_done "$testroot" "$ret"
226 return 1
227 fi
229 (cd $testroot/wt && got cherrypick $branch_rev \
230 > $testroot/stdout 2> $testroot/stderr)
231 ret="$?"
232 if [ "$ret" = "0" ]; then
233 echo "cherrypick succeeded unexpectedly" >&2
234 test_done "$testroot" "1"
235 return 1
236 fi
238 echo -n > $testroot/stdout.expected
239 echo -n "got: work tree contains conflicted files; " \
240 > $testroot/stderr.expected
241 echo "these conflicts must be resolved first" \
242 >> $testroot/stderr.expected
244 cmp -s $testroot/stdout.expected $testroot/stdout
245 ret="$?"
246 if [ "$ret" != "0" ]; then
247 diff -u $testroot/stdout.expected $testroot/stdout
248 test_done "$testroot" "$ret"
249 return 1
250 fi
252 cmp -s $testroot/stderr.expected $testroot/stderr
253 ret="$?"
254 if [ "$ret" != "0" ]; then
255 diff -u $testroot/stderr.expected $testroot/stderr
256 test_done "$testroot" "$ret"
257 return 1
258 fi
260 cmp -s $testroot/content.expected $testroot/wt/alpha
261 ret="$?"
262 if [ "$ret" != "0" ]; then
263 diff -u $testroot/content.expected $testroot/wt/alpha
264 fi
265 test_done "$testroot" "$ret"
268 test_cherrypick_into_work_tree_with_mixed_commits() {
269 local testroot=`test_init cherrypick_into_work_tree_with_mixed_commits`
270 local first_rev=`git_show_head $testroot/repo`
272 echo "modified alpha" > $testroot/repo/alpha
273 git_commit $testroot/repo -m "committing to alpha"
274 local second_rev=`git_show_head $testroot/repo`
276 got checkout $testroot/repo $testroot/wt > /dev/null
277 ret="$?"
278 if [ "$ret" != "0" ]; then
279 test_done "$testroot" "$ret"
280 return 1
281 fi
283 (cd $testroot/repo && git checkout -q -b newbranch)
284 echo "modified delta on branch" > $testroot/repo/gamma/delta
285 git_commit $testroot/repo -m "committing to delta on newbranch"
287 (cd $testroot/repo && git rm -q beta)
288 echo "new file on branch" > $testroot/repo/epsilon/new
289 (cd $testroot/repo && git add epsilon/new)
290 git_commit $testroot/repo -m "committing more changes on newbranch"
292 local branch_rev=`git_show_head $testroot/repo`
294 (cd $testroot/wt && got update -c $first_rev alpha >/dev/null)
296 (cd $testroot/wt && got cherrypick $branch_rev \
297 > $testroot/stdout 2> $testroot/stderr)
298 ret="$?"
299 if [ "$ret" = "0" ]; then
300 echo "cherrypick succeeded unexpectedly" >&2
301 test_done "$testroot" "1"
302 return 1
303 fi
305 echo -n > $testroot/stdout.expected
306 echo -n "got: work tree contains files from multiple base commits; " \
307 > $testroot/stderr.expected
308 echo "the entire work tree must be updated first" \
309 >> $testroot/stderr.expected
311 cmp -s $testroot/stdout.expected $testroot/stdout
312 ret="$?"
313 if [ "$ret" != "0" ]; then
314 diff -u $testroot/stdout.expected $testroot/stdout
315 test_done "$testroot" "$ret"
316 return 1
317 fi
319 cmp -s $testroot/stderr.expected $testroot/stderr
320 ret="$?"
321 if [ "$ret" != "0" ]; then
322 diff -u $testroot/stderr.expected $testroot/stderr
323 fi
324 test_done "$testroot" "$ret"
328 test_cherrypick_modified_submodule() {
329 local testroot=`test_init cherrypick_modified_submodules`
331 make_single_file_repo $testroot/repo2 foo
333 (cd $testroot/repo && git submodule -q add ../repo2)
334 (cd $testroot/repo && git commit -q -m 'adding submodule')
336 got checkout $testroot/repo $testroot/wt > /dev/null
338 echo "modified foo" > $testroot/repo2/foo
339 (cd $testroot/repo2 && git commit -q -a -m 'modified a submodule')
341 (cd $testroot/repo && git checkout -q -b newbranch)
342 # Update the repo/repo2 submodule link on newbranch
343 (cd $testroot/repo && git -C repo2 pull -q)
344 (cd $testroot/repo && git add repo2)
345 git_commit $testroot/repo -m "modified submodule link"
346 local commit_id=`git_show_head $testroot/repo`
348 # This cherrypick is a no-op because Got's file index
349 # does not track submodules.
350 (cd $testroot/wt && got cherrypick $commit_id > $testroot/stdout)
352 echo -n > $testroot/stdout.expected
353 cmp -s $testroot/stdout.expected $testroot/stdout
354 ret="$?"
355 if [ "$ret" != "0" ]; then
356 diff -u $testroot/stdout.expected $testroot/stdout
357 fi
358 test_done "$testroot" "$ret"
361 test_cherrypick_added_submodule() {
362 local testroot=`test_init cherrypick_added_submodules`
364 got checkout $testroot/repo $testroot/wt > /dev/null
366 make_single_file_repo $testroot/repo2 foo
368 # Add the repo/repo2 submodule on newbranch
369 (cd $testroot/repo && git checkout -q -b newbranch)
370 (cd $testroot/repo && git submodule -q add ../repo2)
371 (cd $testroot/repo && git commit -q -m 'adding submodule')
372 local commit_id=`git_show_head $testroot/repo`
374 (cd $testroot/wt && got cherrypick $commit_id > $testroot/stdout)
376 echo "A .gitmodules" > $testroot/stdout.expected
377 echo "Merged commit $commit_id" >> $testroot/stdout.expected
378 cmp -s $testroot/stdout.expected $testroot/stdout
379 ret="$?"
380 if [ "$ret" != "0" ]; then
381 diff -u $testroot/stdout.expected $testroot/stdout
382 fi
383 test_done "$testroot" "$ret"
386 test_cherrypick_conflict_wt_file_vs_repo_submodule() {
387 local testroot=`test_init cherrypick_conflict_wt_file_vs_repo_submodule`
389 got checkout $testroot/repo $testroot/wt > /dev/null
391 # Add a file which will clash with the submodule
392 echo "This is a file called repo2" > $testroot/wt/repo2
393 (cd $testroot/wt && got add repo2 > /dev/null)
394 (cd $testroot/wt && got commit -m 'add file repo2' > /dev/null)
395 ret="$?"
396 if [ "$ret" != "0" ]; then
397 echo "commit failed unexpectedly" >&2
398 test_done "$testroot" "1"
399 return 1
400 fi
402 make_single_file_repo $testroot/repo2 foo
404 # Add the repo/repo2 submodule on newbranch
405 (cd $testroot/repo && git checkout -q -b newbranch)
406 (cd $testroot/repo && git submodule -q add ../repo2)
407 (cd $testroot/repo && git commit -q -m 'adding submodule')
408 local commit_id=`git_show_head $testroot/repo`
410 # Modify the clashing file such that any modifications brought
411 # in by 'got cherrypick' would require a merge.
412 echo "This file was changed" > $testroot/wt/repo2
414 (cd $testroot/wt && got update >/dev/null)
415 (cd $testroot/wt && got cherrypick $commit_id > $testroot/stdout)
417 echo "A .gitmodules" > $testroot/stdout.expected
418 echo "Merged commit $commit_id" >> $testroot/stdout.expected
419 cmp -s $testroot/stdout.expected $testroot/stdout
420 ret="$?"
421 if [ "$ret" != "0" ]; then
422 diff -u $testroot/stdout.expected $testroot/stdout
423 test_done "$testroot" "$ret"
424 return 1
425 fi
427 (cd $testroot/wt && got status > $testroot/stdout)
429 echo "A .gitmodules" > $testroot/stdout.expected
430 echo "M repo2" >> $testroot/stdout.expected
431 cmp -s $testroot/stdout.expected $testroot/stdout
432 ret="$?"
433 if [ "$ret" != "0" ]; then
434 diff -u $testroot/stdout.expected $testroot/stdout
435 fi
436 test_done "$testroot" "$ret"
439 test_cherrypick_modified_symlinks() {
440 local testroot=`test_init cherrypick_modified_symlinks`
442 (cd $testroot/repo && ln -s alpha alpha.link)
443 (cd $testroot/repo && ln -s epsilon epsilon.link)
444 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
445 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
446 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
447 (cd $testroot/repo && git add .)
448 git_commit $testroot/repo -m "add symlinks"
449 local commit_id1=`git_show_head $testroot/repo`
451 got tree -r $testroot/repo -R -c $commit_id1 \
452 > $testroot/stdout
453 cat > $testroot/stdout.expected <<EOF
454 alpha
455 alpha.link@ -> alpha
456 beta
457 epsilon/
458 epsilon/beta.link@ -> ../beta
459 epsilon/zeta
460 epsilon.link@ -> epsilon
461 gamma/
462 gamma/delta
463 nonexistent.link@ -> nonexistent
464 passwd.link@ -> /etc/passwd
465 EOF
466 cmp -s $testroot/stdout.expected $testroot/stdout
467 ret="$?"
468 if [ "$ret" != "0" ]; then
469 diff -u $testroot/stdout.expected $testroot/stdout
470 test_done "$testroot" "$ret"
471 return 1
472 fi
474 got branch -r $testroot/repo foo
476 got checkout -b foo $testroot/repo $testroot/wt > /dev/null
478 (cd $testroot/repo && ln -sf beta alpha.link)
479 (cd $testroot/repo && ln -sfh gamma epsilon.link)
480 (cd $testroot/repo && ln -sf ../gamma/delta epsilon/beta.link)
481 (cd $testroot/repo && ln -sf .got/foo $testroot/repo/dotgotfoo.link)
482 (cd $testroot/repo && git rm -q nonexistent.link)
483 (cd $testroot/repo && ln -sf epsilon/zeta zeta.link)
484 (cd $testroot/repo && git add .)
485 git_commit $testroot/repo -m "change symlinks"
486 local commit_id2=`git_show_head $testroot/repo`
488 (cd $testroot/wt && got cherrypick $commit_id2 > $testroot/stdout)
490 echo "G alpha.link" > $testroot/stdout.expected
491 echo "G epsilon/beta.link" >> $testroot/stdout.expected
492 echo "A dotgotfoo.link" >> $testroot/stdout.expected
493 echo "G epsilon.link" >> $testroot/stdout.expected
494 echo "D nonexistent.link" >> $testroot/stdout.expected
495 echo "A zeta.link" >> $testroot/stdout.expected
496 echo "Merged commit $commit_id2" >> $testroot/stdout.expected
497 cmp -s $testroot/stdout.expected $testroot/stdout
498 ret="$?"
499 if [ "$ret" != "0" ]; then
500 diff -u $testroot/stdout.expected $testroot/stdout
501 test_done "$testroot" "$ret"
502 return 1
503 fi
505 if ! [ -h $testroot/wt/alpha.link ]; then
506 echo "alpha.link is not a symlink"
507 test_done "$testroot" "1"
508 return 1
509 fi
511 readlink $testroot/wt/alpha.link > $testroot/stdout
512 echo "beta" > $testroot/stdout.expected
513 cmp -s $testroot/stdout.expected $testroot/stdout
514 ret="$?"
515 if [ "$ret" != "0" ]; then
516 diff -u $testroot/stdout.expected $testroot/stdout
517 test_done "$testroot" "$ret"
518 return 1
519 fi
521 if ! [ -h $testroot/wt/dotgotfoo.link ]; then
522 echo "dotgotfoo.link is not a symlink"
523 test_done "$testroot" "1"
524 return 1
525 fi
527 readlink $testroot/wt/dotgotfoo.link > $testroot/stdout
528 echo ".got/foo" > $testroot/stdout.expected
529 cmp -s $testroot/stdout.expected $testroot/stdout
530 ret="$?"
531 if [ "$ret" != "0" ]; then
532 diff -u $testroot/stdout.expected $testroot/stdout
533 test_done "$testroot" "$ret"
534 return 1
535 fi
537 if ! [ -h $testroot/wt/epsilon.link ]; then
538 echo "epsilon.link is not a symlink"
539 test_done "$testroot" "1"
540 return 1
541 fi
543 readlink $testroot/wt/epsilon.link > $testroot/stdout
544 echo "gamma" > $testroot/stdout.expected
545 cmp -s $testroot/stdout.expected $testroot/stdout
546 ret="$?"
547 if [ "$ret" != "0" ]; then
548 diff -u $testroot/stdout.expected $testroot/stdout
549 test_done "$testroot" "$ret"
550 return 1
551 fi
553 if [ -h $testroot/wt/passwd.link ]; then
554 echo -n "passwd.link symlink points outside of work tree: " >&2
555 readlink $testroot/wt/passwd.link >&2
556 test_done "$testroot" "1"
557 return 1
558 fi
560 echo -n "/etc/passwd" > $testroot/content.expected
561 cp $testroot/wt/passwd.link $testroot/content
563 cmp -s $testroot/content.expected $testroot/content
564 ret="$?"
565 if [ "$ret" != "0" ]; then
566 diff -u $testroot/content.expected $testroot/content
567 test_done "$testroot" "$ret"
568 return 1
569 fi
571 readlink $testroot/wt/epsilon/beta.link > $testroot/stdout
572 echo "../gamma/delta" > $testroot/stdout.expected
573 cmp -s $testroot/stdout.expected $testroot/stdout
574 ret="$?"
575 if [ "$ret" != "0" ]; then
576 diff -u $testroot/stdout.expected $testroot/stdout
577 test_done "$testroot" "$ret"
578 return 1
579 fi
581 if [ -h $testroot/wt/nonexistent.link ]; then
582 echo -n "nonexistent.link still exists on disk: " >&2
583 readlink $testroot/wt/nonexistent.link >&2
584 test_done "$testroot" "1"
585 return 1
586 fi
588 (cd $testroot/wt && got commit -m 'commit cherrypick result' \
589 > /dev/null 2>$testroot/stderr)
590 ret="$?"
591 if [ "$ret" == "0" ]; then
592 echo "got commit succeeded unexpectedly" >&2
593 test_done "$testroot" "$ret"
594 return 1
595 fi
596 echo -n "got: $testroot/wt/dotgotfoo.link: symbolic link points " \
597 > $testroot/stderr.expected
598 echo "outside of paths under version control" \
599 >> $testroot/stderr.expected
600 cmp -s $testroot/stderr.expected $testroot/stderr
601 ret="$?"
602 if [ "$ret" != "0" ]; then
603 diff -u $testroot/stderr.expected $testroot/stderr
604 test_done "$testroot" "$ret"
605 return 1
606 fi
608 (cd $testroot/wt && got commit -S -m 'commit cherrypick result' \
609 > /dev/null)
610 ret="$?"
611 if [ "$ret" != "0" ]; then
612 echo "got commit failed unexpectedly" >&2
613 test_done "$testroot" "$ret"
614 return 1
615 fi
616 local commit_id2=`git_show_head $testroot/repo`
618 got tree -r $testroot/repo -R -c $commit_id2 \
619 > $testroot/stdout
620 cat > $testroot/stdout.expected <<EOF
621 alpha
622 alpha.link@ -> beta
623 beta
624 dotgotfoo.link@ -> .got/foo
625 epsilon/
626 epsilon/beta.link@ -> ../gamma/delta
627 epsilon/zeta
628 epsilon.link@ -> gamma
629 gamma/
630 gamma/delta
631 passwd.link@ -> /etc/passwd
632 zeta.link@ -> epsilon/zeta
633 EOF
634 cmp -s $testroot/stdout.expected $testroot/stdout
635 ret="$?"
636 if [ "$ret" != "0" ]; then
637 diff -u $testroot/stdout.expected $testroot/stdout
638 fi
639 test_done "$testroot" "$ret"
642 test_cherrypick_symlink_conflicts() {
643 local testroot=`test_init cherrypick_symlink_conflicts`
645 (cd $testroot/repo && ln -s alpha alpha.link)
646 (cd $testroot/repo && ln -s epsilon epsilon.link)
647 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
648 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
649 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
650 (cd $testroot/repo && ln -sf epsilon/zeta zeta.link)
651 (cd $testroot/repo && git add .)
652 git_commit $testroot/repo -m "add symlinks"
653 local commit_id1=`git_show_head $testroot/repo`
655 (cd $testroot/repo && ln -sf beta alpha.link)
656 (cd $testroot/repo && ln -sf beta boo.link)
657 (cd $testroot/repo && ln -sfh gamma epsilon.link)
658 (cd $testroot/repo && ln -sf ../gamma/delta epsilon/beta.link)
659 echo 'this is regular file foo' > $testroot/repo/dotgotfoo.link
660 (cd $testroot/repo && ln -sf .got/bar dotgotbar.link)
661 (cd $testroot/repo && git rm -q nonexistent.link)
662 (cd $testroot/repo && ln -sf gamma/delta zeta.link)
663 (cd $testroot/repo && ln -sf alpha new.link)
664 (cd $testroot/repo && git add .)
665 git_commit $testroot/repo -m "change symlinks"
666 local commit_id2=`git_show_head $testroot/repo`
668 got branch -r $testroot/repo -c $commit_id1 foo
669 got checkout -b foo $testroot/repo $testroot/wt > /dev/null
671 # modified symlink to file A vs modified symlink to file B
672 (cd $testroot/wt && ln -sf gamma/delta alpha.link)
673 # modified symlink to dir A vs modified symlink to file B
674 (cd $testroot/wt && ln -sfh beta epsilon.link)
675 # modeified symlink to file A vs modified symlink to dir B
676 (cd $testroot/wt && ln -sfh ../gamma epsilon/beta.link)
677 # added regular file A vs added bad symlink to file A
678 (cd $testroot/wt && ln -sf .got/foo dotgotfoo.link)
679 (cd $testroot/wt && got add dotgotfoo.link > /dev/null)
680 # added bad symlink to file A vs added regular file A
681 echo 'this is regular file bar' > $testroot/wt/dotgotbar.link
682 (cd $testroot/wt && got add dotgotbar.link > /dev/null)
683 # added symlink to file A vs unversioned file A
684 echo 'this is unversioned file boo' > $testroot/wt/boo.link
685 # removed symlink to non-existent file A vs modified symlink
686 # to nonexistent file B
687 (cd $testroot/wt && ln -sf nonexistent2 nonexistent.link)
688 # modified symlink to file A vs removed symlink to file A
689 (cd $testroot/wt && got rm zeta.link > /dev/null)
690 # added symlink to file A vs added symlink to file B
691 (cd $testroot/wt && ln -sf beta new.link)
692 (cd $testroot/wt && got add new.link > /dev/null)
693 (cd $testroot/wt && got commit -S -m "change symlinks on foo" \
694 > /dev/null)
696 (cd $testroot/wt && got update >/dev/null)
697 (cd $testroot/wt && got cherrypick $commit_id2 > $testroot/stdout)
699 echo -n > $testroot/stdout.expected
700 echo "C alpha.link" >> $testroot/stdout.expected
701 echo "C epsilon/beta.link" >> $testroot/stdout.expected
702 echo "? boo.link" >> $testroot/stdout.expected
703 echo "C epsilon.link" >> $testroot/stdout.expected
704 echo "C dotgotbar.link" >> $testroot/stdout.expected
705 echo "C dotgotfoo.link" >> $testroot/stdout.expected
706 echo "D nonexistent.link" >> $testroot/stdout.expected
707 echo "! zeta.link" >> $testroot/stdout.expected
708 echo "C new.link" >> $testroot/stdout.expected
709 echo "Merged commit $commit_id2" >> $testroot/stdout.expected
710 echo "Files with new merge conflicts: 6" >> $testroot/stdout.expected
711 cmp -s $testroot/stdout.expected $testroot/stdout
712 ret="$?"
713 if [ "$ret" != "0" ]; then
714 diff -u $testroot/stdout.expected $testroot/stdout
715 test_done "$testroot" "$ret"
716 return 1
717 fi
719 if [ -h $testroot/wt/alpha.link ]; then
720 echo "alpha.link is a symlink"
721 test_done "$testroot" "1"
722 return 1
723 fi
725 echo "<<<<<<< merged change: commit $commit_id2" \
726 > $testroot/content.expected
727 echo "beta" >> $testroot/content.expected
728 echo "3-way merge base: commit $commit_id1" \
729 >> $testroot/content.expected
730 echo "alpha" >> $testroot/content.expected
731 echo "=======" >> $testroot/content.expected
732 echo "gamma/delta" >> $testroot/content.expected
733 echo '>>>>>>>' >> $testroot/content.expected
734 echo -n "" >> $testroot/content.expected
736 cp $testroot/wt/alpha.link $testroot/content
737 cmp -s $testroot/content.expected $testroot/content
738 ret="$?"
739 if [ "$ret" != "0" ]; then
740 diff -u $testroot/content.expected $testroot/content
741 test_done "$testroot" "$ret"
742 return 1
743 fi
745 if [ -h $testroot/wt/boo.link ]; then
746 echo "boo.link is a symlink"
747 test_done "$testroot" "1"
748 return 1
749 fi
751 echo "this is unversioned file boo" > $testroot/content.expected
752 cp $testroot/wt/boo.link $testroot/content
753 cmp -s $testroot/content.expected $testroot/content
754 ret="$?"
755 if [ "$ret" != "0" ]; then
756 diff -u $testroot/content.expected $testroot/content
757 test_done "$testroot" "$ret"
758 return 1
759 fi
761 if [ -h $testroot/wt/epsilon.link ]; then
762 echo "epsilon.link is a symlink"
763 test_done "$testroot" "1"
764 return 1
765 fi
767 echo "<<<<<<< merged change: commit $commit_id2" \
768 > $testroot/content.expected
769 echo "gamma" >> $testroot/content.expected
770 echo "3-way merge base: commit $commit_id1" \
771 >> $testroot/content.expected
772 echo "epsilon" >> $testroot/content.expected
773 echo "=======" >> $testroot/content.expected
774 echo "beta" >> $testroot/content.expected
775 echo '>>>>>>>' >> $testroot/content.expected
776 echo -n "" >> $testroot/content.expected
778 cp $testroot/wt/epsilon.link $testroot/content
779 cmp -s $testroot/content.expected $testroot/content
780 ret="$?"
781 if [ "$ret" != "0" ]; then
782 diff -u $testroot/content.expected $testroot/content
783 test_done "$testroot" "$ret"
784 return 1
785 fi
787 if [ -h $testroot/wt/passwd.link ]; then
788 echo -n "passwd.link symlink points outside of work tree: " >&2
789 readlink $testroot/wt/passwd.link >&2
790 test_done "$testroot" "1"
791 return 1
792 fi
794 echo -n "/etc/passwd" > $testroot/content.expected
795 cp $testroot/wt/passwd.link $testroot/content
797 cmp -s $testroot/content.expected $testroot/content
798 ret="$?"
799 if [ "$ret" != "0" ]; then
800 diff -u $testroot/content.expected $testroot/content
801 test_done "$testroot" "$ret"
802 return 1
803 fi
805 if [ -h $testroot/wt/epsilon/beta.link ]; then
806 echo "epsilon/beta.link is a symlink"
807 test_done "$testroot" "1"
808 return 1
809 fi
811 echo "<<<<<<< merged change: commit $commit_id2" \
812 > $testroot/content.expected
813 echo "../gamma/delta" >> $testroot/content.expected
814 echo "3-way merge base: commit $commit_id1" \
815 >> $testroot/content.expected
816 echo "../beta" >> $testroot/content.expected
817 echo "=======" >> $testroot/content.expected
818 echo "../gamma" >> $testroot/content.expected
819 echo '>>>>>>>' >> $testroot/content.expected
820 echo -n "" >> $testroot/content.expected
822 cp $testroot/wt/epsilon/beta.link $testroot/content
823 cmp -s $testroot/content.expected $testroot/content
824 ret="$?"
825 if [ "$ret" != "0" ]; then
826 diff -u $testroot/content.expected $testroot/content
827 test_done "$testroot" "$ret"
828 return 1
829 fi
831 if [ -h $testroot/wt/nonexistent.link ]; then
832 echo -n "nonexistent.link still exists on disk: " >&2
833 readlink $testroot/wt/nonexistent.link >&2
834 test_done "$testroot" "1"
835 return 1
836 fi
838 if [ -h $testroot/wt/dotgotfoo.link ]; then
839 echo "dotgotfoo.link is a symlink"
840 test_done "$testroot" "1"
841 return 1
842 fi
844 echo "<<<<<<< merged change: commit $commit_id2" \
845 > $testroot/content.expected
846 echo "this is regular file foo" >> $testroot/content.expected
847 echo "=======" >> $testroot/content.expected
848 echo -n ".got/foo" >> $testroot/content.expected
849 echo '>>>>>>>' >> $testroot/content.expected
850 cp $testroot/wt/dotgotfoo.link $testroot/content
851 cmp -s $testroot/content.expected $testroot/content
852 ret="$?"
853 if [ "$ret" != "0" ]; then
854 diff -u $testroot/content.expected $testroot/content
855 test_done "$testroot" "$ret"
856 return 1
857 fi
859 if [ -h $testroot/wt/dotgotbar.link ]; then
860 echo "dotgotbar.link is a symlink"
861 test_done "$testroot" "1"
862 return 1
863 fi
864 echo "<<<<<<< merged change: commit $commit_id2" \
865 > $testroot/content.expected
866 echo -n ".got/bar" >> $testroot/content.expected
867 echo "=======" >> $testroot/content.expected
868 echo "this is regular file bar" >> $testroot/content.expected
869 echo '>>>>>>>' >> $testroot/content.expected
870 echo -n "" >> $testroot/content.expected
871 cp $testroot/wt/dotgotbar.link $testroot/content
872 cmp -s $testroot/content.expected $testroot/content
873 ret="$?"
874 if [ "$ret" != "0" ]; then
875 diff -u $testroot/content.expected $testroot/content
876 test_done "$testroot" "$ret"
877 return 1
878 fi
880 if [ -h $testroot/wt/new.link ]; then
881 echo "new.link is a symlink"
882 test_done "$testroot" "1"
883 return 1
884 fi
886 echo "<<<<<<< merged change: commit $commit_id2" \
887 > $testroot/content.expected
888 echo "alpha" >> $testroot/content.expected
889 echo "=======" >> $testroot/content.expected
890 echo "beta" >> $testroot/content.expected
891 echo '>>>>>>>' >> $testroot/content.expected
892 echo -n "" >> $testroot/content.expected
894 cp $testroot/wt/new.link $testroot/content
895 cmp -s $testroot/content.expected $testroot/content
896 ret="$?"
897 if [ "$ret" != "0" ]; then
898 diff -u $testroot/content.expected $testroot/content
899 test_done "$testroot" "$ret"
900 return 1
901 fi
903 echo "A dotgotfoo.link" > $testroot/stdout.expected
904 echo "M new.link" >> $testroot/stdout.expected
905 echo "D nonexistent.link" >> $testroot/stdout.expected
906 (cd $testroot/wt && got status > $testroot/stdout)
907 if [ "$ret" != "0" ]; then
908 diff -u $testroot/stdout.expected $testroot/stdout
909 test_done "$testroot" "$ret"
910 return 1
911 fi
913 test_done "$testroot" "0"
916 test_cherrypick_with_path_prefix_and_empty_tree() {
917 local testroot=`test_init cherrypick_with_path_prefix_and_empty_tree 1`
919 (cd $testroot/repo && git commit --allow-empty \
920 -m "initial empty commit" >/dev/null)
922 (cd $testroot/repo && got br bar >/dev/null)
924 mkdir -p $testroot/repo/epsilon
925 echo "file foo" > $testroot/repo/epsilon/foo
926 (cd $testroot/repo && git add .)
927 git_commit $testroot/repo -m "add file foo"
928 local commit_id=`git_show_head $testroot/repo`
930 got checkout -b bar $testroot/repo $testroot/wt > /dev/null
931 ret="$?"
932 if [ "$ret" != "0" ]; then
933 echo "got checkout failed unexpectedly" >&2
934 test_done "$testroot" "$ret"
935 return 1
936 fi
938 mkdir -p $testroot/wt/epsilon
939 echo "new file" > $testroot/wt/epsilon/new
940 (cd $testroot/wt && got add epsilon/new >/dev/null)
941 (cd $testroot/wt && got commit -m "add file on branch bar" > /dev/null)
943 got checkout -b bar -p epsilon $testroot/repo $testroot/wt2 > /dev/null
944 ret="$?"
945 if [ "$ret" != "0" ]; then
946 echo "got checkout failed unexpectedly" >&2
947 test_done "$testroot" "$ret"
948 return 1
949 fi
950 (cd $testroot/wt2 && got cherrypick $commit_id > $testroot/stdout)
952 echo "A foo" > $testroot/stdout.expected
953 echo "Merged commit $commit_id" >> $testroot/stdout.expected
955 cmp -s $testroot/stdout.expected $testroot/stdout
956 ret="$?"
957 if [ "$ret" != "0" ]; then
958 diff -u $testroot/stdout.expected $testroot/stdout
959 fi
960 test_done "$testroot" "$ret"
963 test_cherrypick_conflict_no_eol() {
964 local testroot=`test_init cherrypick_conflict_no_eol 1`
965 local content_a="aaa\naaa\naaa\naaa\naaa\naaa\n"
966 local content_b="aaa\naaa\nbbb\naaa\naaa\naaa\naaa"
967 local content_c="aaa\naaa\nccc\naaa\naaa\naaa\naaa"
969 printf "$content_a" > $testroot/repo/a
970 (cd $testroot/repo && git add a)
971 git_commit $testroot/repo -m "initial commit"
973 (cd $testroot/repo && got branch newbranch)
975 printf "$content_b" > $testroot/repo/a
976 git_commit $testroot/repo -m "change bbb"
978 printf "$content_c" > $testroot/repo/a
979 git_commit $testroot/repo -m "change ccc"
980 local ccc_commit=`git_show_head $testroot/repo`
982 got checkout -b newbranch $testroot/repo $testroot/wt > /dev/null
983 ret="$?"
984 if [ "$ret" != "0" ]; then
985 test_done "$testroot" "$ret"
986 return 1
987 fi
989 (cd $testroot/wt && got cherrypick $ccc_commit > $testroot/stdout)
991 echo "C a" > $testroot/stdout.expected
992 echo "Merged commit $ccc_commit" >> $testroot/stdout.expected
993 echo "Files with new merge conflicts: 1" >> $testroot/stdout.expected
995 cmp -s $testroot/stdout.expected $testroot/stdout
996 ret="$?"
997 if [ "$ret" != "0" ]; then
998 diff -u $testroot/stdout.expected $testroot/stdout
999 fi
1000 test_done "$testroot" "$ret"
1003 test_cherrypick_conflict_no_eol2() {
1004 local testroot=`test_init cherrypick_conflict_no_eol2 1`
1005 local content_a="aaa\naaa\naaa\naaa\naaa\naaa"
1006 local content_b="aaa\naaa\nbbb\naaa\naaa\naaa"
1007 local content_c="aaa\naaa\nbbb\naaa\naaa\naaa\n"
1009 printf "$content_a" > $testroot/repo/a
1010 (cd $testroot/repo && git add a)
1011 git_commit $testroot/repo -m "initial commit"
1013 (cd $testroot/repo && got branch newbranch)
1015 printf "$content_b" > $testroot/repo/a
1016 git_commit $testroot/repo -m "change bbb"
1018 printf "$content_c" > $testroot/repo/a
1019 git_commit $testroot/repo -m "change ccc"
1020 local ccc_commit=`git_show_head $testroot/repo`
1022 got checkout -b newbranch $testroot/repo $testroot/wt > /dev/null
1023 ret="$?"
1024 if [ "$ret" != "0" ]; then
1025 test_done "$testroot" "$ret"
1026 return 1
1029 (cd $testroot/wt && got cherrypick $ccc_commit \
1030 > $testroot/stdout 2> $testroot/stderr)
1032 echo "C a" > $testroot/stdout.expected
1033 echo "Merged commit $ccc_commit" >> $testroot/stdout.expected
1034 echo "Files with new merge conflicts: 1" >> $testroot/stdout.expected
1036 cmp -s $testroot/stdout.expected $testroot/stdout
1037 ret="$?"
1038 if [ "$ret" != "0" ]; then
1039 diff -u $testroot/stdout.expected $testroot/stdout
1041 test_done "$testroot" "$ret"
1044 test_cherrypick_unrelated_changes() {
1045 local testroot=`test_init cherrypick_unrelated_changes`
1047 # Sorry about the large HERE document but I have not found
1048 # a smaller reproduction recipe yet...
1049 cat > $testroot/repo/reference.c <<EOF
1050 const struct got_error *
1051 got_ref_alloc(struct got_reference **ref, const char *name,
1052 struct got_object_id *id)
1054 if (!is_valid_ref_name(name))
1055 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
1057 return alloc_ref(ref, name, id, 0);
1060 static const struct got_error *
1061 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
1062 const char *line)
1064 struct got_object_id id;
1065 const char *name;
1067 *ref = NULL;
1069 if (line[0] == '#' || line[0] == '^')
1070 return NULL;
1072 if (!got_parse_sha1_digest(id.sha1, line))
1073 return got_error(GOT_ERR_BAD_REF_DATA);
1075 if (abs_refname) {
1076 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
1077 return NULL;
1078 name = abs_refname;
1079 } else
1080 name = line + SHA1_DIGEST_STRING_LENGTH;
1082 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
1085 static const struct got_error *
1086 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
1087 int nsubdirs, const char *refname)
1089 const struct got_error *err = NULL;
1090 char *abs_refname;
1091 char *line = NULL;
1092 size_t linesize = 0;
1093 ssize_t linelen;
1094 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
1096 *ref = NULL;
1098 if (ref_is_absolute)
1099 abs_refname = (char *)refname;
1100 do {
1101 linelen = getline(&line, &linesize, f);
1102 if (linelen == -1) {
1103 if (feof(f))
1104 break;
1105 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1106 break;
1108 if (linelen > 0 && line[linelen - 1] == '\n')
1109 line[linelen - 1] = '\0';
1110 for (i = 0; i < nsubdirs; i++) {
1111 if (!ref_is_absolute &&
1112 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
1113 refname) == -1)
1114 return got_error_from_errno("asprintf");
1115 err = parse_packed_ref_line(ref, abs_refname, line);
1116 if (!ref_is_absolute)
1117 free(abs_refname);
1118 if (err || *ref != NULL)
1119 break;
1121 if (err)
1122 break;
1123 } while (*ref == NULL);
1124 free(line);
1126 return err;
1129 static const struct got_error *
1130 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
1131 const char *name, int lock)
1133 const struct got_error *err = NULL;
1134 char *path = NULL;
1135 char *absname = NULL;
1136 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
1137 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
1139 *ref = NULL;
1141 if (ref_is_absolute || ref_is_well_known) {
1142 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
1143 return got_error_from_errno("asprintf");
1144 absname = (char *)name;
1145 } else {
1146 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
1147 subdir[0] ? "/" : "", name) == -1)
1148 return got_error_from_errno("asprintf");
1150 if (asprintf(&absname, "refs/%s%s%s",
1151 subdir, subdir[0] ? "/" : "", name) == -1) {
1152 err = got_error_from_errno("asprintf");
1153 goto done;
1157 err = parse_ref_file(ref, name, absname, path, lock);
1158 done:
1159 if (!ref_is_absolute && !ref_is_well_known)
1160 free(absname);
1161 free(path);
1162 return err;
1165 const struct got_error *
1166 got_ref_open(struct got_reference **ref, struct got_repository *repo,
1167 const char *refname, int lock)
1169 const struct got_error *err = NULL;
1170 char *path_refs = NULL;
1171 const char *subdirs[] = {
1172 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
1174 size_t i;
1175 int well_known = is_well_known_ref(refname);
1176 struct got_lockfile *lf = NULL;
1178 *ref = NULL;
1180 path_refs = get_refs_dir_path(repo, refname);
1181 if (path_refs == NULL) {
1182 err = got_error_from_errno2("get_refs_dir_path", refname);
1183 goto done;
1186 if (well_known) {
1187 err = open_ref(ref, path_refs, "", refname, lock);
1188 } else {
1189 char *packed_refs_path;
1190 FILE *f;
1192 /* Search on-disk refs before packed refs! */
1193 for (i = 0; i < nitems(subdirs); i++) {
1194 err = open_ref(ref, path_refs, subdirs[i], refname,
1195 lock);
1196 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
1197 goto done;
1200 packed_refs_path = got_repo_get_path_packed_refs(repo);
1201 if (packed_refs_path == NULL) {
1202 err = got_error_from_errno(
1203 "got_repo_get_path_packed_refs");
1204 goto done;
1207 if (lock) {
1208 err = got_lockfile_lock(&lf, packed_refs_path);
1209 if (err)
1210 goto done;
1212 f = fopen(packed_refs_path, "rb");
1213 free(packed_refs_path);
1214 if (f != NULL) {
1215 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
1216 refname);
1217 if (!err) {
1218 if (fclose(f) == EOF) {
1219 err = got_error_from_errno("fclose");
1220 got_ref_close(*ref);
1221 *ref = NULL;
1222 } else if (*ref)
1223 (*ref)->lf = lf;
1227 done:
1228 if (!err && *ref == NULL)
1229 err = got_error_not_ref(refname);
1230 if (err && lf)
1231 got_lockfile_unlock(lf);
1232 free(path_refs);
1233 return err;
1236 struct got_reference *
1237 got_ref_dup(struct got_reference *ref)
1239 struct got_reference *ret;
1241 ret = calloc(1, sizeof(*ret));
1242 if (ret == NULL)
1243 return NULL;
1245 ret->flags = ref->flags;
1246 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1247 ret->ref.symref.name = strdup(ref->ref.symref.name);
1248 if (ret->ref.symref.name == NULL) {
1249 free(ret);
1250 return NULL;
1252 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
1253 if (ret->ref.symref.ref == NULL) {
1254 free(ret->ref.symref.name);
1255 free(ret);
1256 return NULL;
1258 } else {
1259 ret->ref.ref.name = strdup(ref->ref.ref.name);
1260 if (ret->ref.ref.name == NULL) {
1261 free(ret);
1262 return NULL;
1264 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
1265 sizeof(ret->ref.ref.sha1));
1268 return ret;
1271 const struct got_error *
1272 got_reflist_entry_dup(struct got_reflist_entry **newp,
1273 struct got_reflist_entry *re)
1275 const struct got_error *err = NULL;
1276 struct got_reflist_entry *new;
1278 *newp = NULL;
1280 new = malloc(sizeof(*new));
1281 if (new == NULL)
1282 return got_error_from_errno("malloc");
1284 new->ref = got_ref_dup(re->ref);
1285 if (new->ref == NULL) {
1286 err = got_error_from_errno("got_ref_dup");
1287 free(new);
1288 return err;
1291 *newp = new;
1292 return NULL;
1295 void
1296 got_ref_list_free(struct got_reflist_head *refs)
1298 struct got_reflist_entry *re;
1300 while ((re = TAILQ_FIRST(refs))) {
1301 TAILQ_REMOVE(refs, re, entry);
1302 free(re);
1306 EOF
1307 (cd $testroot/repo && git add reference.c)
1308 git_commit $testroot/repo -m "added reference.c file"
1309 local base_commit=`git_show_head $testroot/repo`
1311 got checkout $testroot/repo $testroot/wt > /dev/null
1312 ret="$?"
1313 if [ "$ret" != "0" ]; then
1314 test_done "$testroot" "$ret"
1315 return 1
1318 (cd $testroot/repo && git checkout -q -b newbranch)
1319 ed -s $testroot/repo/reference.c <<EOF
1320 91a
1321 if (!is_valid_ref_name(name))
1322 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
1327 EOF
1328 git_commit $testroot/repo -m "added lines on newbranch"
1329 local branch_rev1=`git_show_head $testroot/repo`
1331 ed -s $testroot/repo/reference.c <<EOF
1332 255a
1333 got_ref_close(re->ref);
1337 EOF
1338 git_commit $testroot/repo -m "more lines on newbranch"
1340 local branch_rev2=`git_show_head $testroot/repo`
1342 (cd $testroot/wt && got cherrypick $branch_rev2 > $testroot/stdout)
1344 echo "G reference.c" > $testroot/stdout.expected
1345 echo "Merged commit $branch_rev2" >> $testroot/stdout.expected
1347 cmp -s $testroot/stdout.expected $testroot/stdout
1348 ret="$?"
1349 if [ "$ret" != "0" ]; then
1350 diff -u $testroot/stdout.expected $testroot/stdout
1351 test_done "$testroot" "$ret"
1352 return 1
1355 cat > $testroot/diff.expected <<EOF
1356 --- reference.c
1357 +++ reference.c
1358 @@ -250,6 +250,7 @@ got_ref_list_free(struct got_reflist_head *refs)
1360 while ((re = TAILQ_FIRST(refs))) {
1361 TAILQ_REMOVE(refs, re, entry);
1362 + got_ref_close(re->ref);
1363 free(re);
1366 EOF
1367 (cd $testroot/wt && got diff |
1368 egrep -v '^(diff|blob|file)' > $testroot/diff)
1369 cmp -s $testroot/diff.expected $testroot/diff
1370 ret="$?"
1371 if [ "$ret" != "0" ]; then
1372 diff -u $testroot/diff.expected $testroot/diff
1375 test_done "$testroot" "$ret"
1378 test_cherrypick_same_branch() {
1379 local testroot=`test_init cherrypick_same_branch`
1381 got checkout $testroot/repo $testroot/wt > /dev/null
1382 ret="$?"
1383 if [ "$ret" != "0" ]; then
1384 test_done "$testroot" "$ret"
1385 return 1
1388 (cd $testroot/repo && git checkout -q -b newbranch)
1389 echo "modified delta on branch" > $testroot/repo/gamma/delta
1390 git_commit $testroot/repo -m "committing to delta on newbranch"
1392 echo "modified alpha on branch" > $testroot/repo/alpha
1393 (cd $testroot/repo && git rm -q beta)
1394 echo "new file on branch" > $testroot/repo/epsilon/new
1395 (cd $testroot/repo && git add epsilon/new)
1396 git_commit $testroot/repo -m "committing more changes on newbranch"
1398 local branch_rev=`git_show_head $testroot/repo`
1400 # picking a commit from the branch's own history does not make
1401 # sense but we should have test coverage for this case regardless
1402 (cd $testroot/wt && got up -b newbranch > /dev/null)
1403 (cd $testroot/wt && got cherrypick $branch_rev > $testroot/stdout)
1405 echo "G alpha" > $testroot/stdout.expected
1406 echo "! beta" >> $testroot/stdout.expected
1407 echo "G epsilon/new" >> $testroot/stdout.expected
1408 echo "Merged commit $branch_rev" >> $testroot/stdout.expected
1410 cmp -s $testroot/stdout.expected $testroot/stdout
1411 ret="$?"
1412 if [ "$ret" != "0" ]; then
1413 diff -u $testroot/stdout.expected $testroot/stdout
1415 test_done "$testroot" "$ret"
1419 test_parseargs "$@"
1420 run_test test_cherrypick_basic
1421 run_test test_cherrypick_root_commit
1422 run_test test_cherrypick_into_work_tree_with_conflicts
1423 run_test test_cherrypick_into_work_tree_with_mixed_commits
1424 run_test test_cherrypick_modified_submodule
1425 run_test test_cherrypick_added_submodule
1426 run_test test_cherrypick_conflict_wt_file_vs_repo_submodule
1427 run_test test_cherrypick_modified_symlinks
1428 run_test test_cherrypick_symlink_conflicts
1429 run_test test_cherrypick_with_path_prefix_and_empty_tree
1430 run_test test_cherrypick_conflict_no_eol
1431 run_test test_cherrypick_conflict_no_eol2
1432 run_test test_cherrypick_unrelated_changes
1433 run_test test_cherrypick_same_branch