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 test_branch_create() {
20 local testroot=`test_init branch_create`
21 local commit_id0=`git_show_head $testroot/repo`
23 # Create a branch based on repository's HEAD reference
24 got branch -r $testroot/repo newbranch
26 if [ $ret -ne 0 ]; then
27 echo "got branch command failed unexpectedly"
28 test_done "$testroot" "$ret"
32 # Ensure that Git recognizes the branch Got has created
33 git -C $testroot/repo checkout -q newbranch
35 if [ $ret -ne 0 ]; then
36 echo "git checkout command failed unexpectedly"
37 test_done "$testroot" "$ret"
40 echo "modified delta on branch" > $testroot/repo/gamma/delta
41 git_commit $testroot/repo -m "committing to delta on newbranch"
43 got checkout -b newbranch $testroot/repo $testroot/wt >/dev/null
45 if [ $ret -ne 0 ]; then
46 echo "got checkout command failed unexpectedly"
47 test_done "$testroot" "$ret"
51 echo "modified delta on branch" > $testroot/content.expected
52 cat $testroot/wt/gamma/delta > $testroot/content
53 cmp -s $testroot/content.expected $testroot/content
55 if [ $ret -ne 0 ]; then
56 diff -u $testroot/content.expected $testroot/content
57 test_done "$testroot" "$ret"
61 # Create a branch based on the work tree's branch
62 (cd $testroot/wt && got branch -n refs/heads/anotherbranch)
64 if [ $ret -ne 0 ]; then
65 test_done "$testroot" "$ret"
69 git -C $testroot/repo checkout -q anotherbranch
71 if [ $ret -ne 0 ]; then
72 echo "git checkout command failed unexpectedly"
73 test_done "$testroot" "$ret"
77 # Create a branch based on another specific branch
78 (cd $testroot/wt && got branch -n -c master yetanotherbranch)
80 if [ $ret -ne 0 ]; then
81 test_done "$testroot" "$ret"
85 git -C $testroot/repo checkout -q yetanotherbranch
87 if [ $ret -ne 0 ]; then
88 echo "git checkout command failed unexpectedly"
89 test_done "$testroot" "$ret"
93 # Create a branch based on a specific commit
94 local commit_id=`git_show_head $testroot/repo`
95 got branch -r $testroot/repo -c $commit_id commitbranch
97 if [ $ret -ne 0 ]; then
98 echo "got branch command failed unexpectedly"
99 test_done "$testroot" "$ret"
103 git -C $testroot/repo checkout -q commitbranch
105 if [ $ret -ne 0 ]; then
106 echo "git checkout command failed unexpectedly"
107 test_done "$testroot" "$ret"
111 # Create a branch and let the work tree be updated to it
112 (cd $testroot/wt && got branch -c $commit_id0 updatebranch \
115 echo -n "Switching work tree from refs/heads/newbranch to " \
116 > $testroot/stdout.expected
117 echo "refs/heads/updatebranch" >> $testroot/stdout.expected
118 echo "U gamma/delta" >> $testroot/stdout.expected
119 echo "Updated to refs/heads/updatebranch: $commit_id0" \
120 >> $testroot/stdout.expected
122 cmp -s $testroot/stdout.expected $testroot/stdout
124 if [ $ret -ne 0 ]; then
125 diff -u $testroot/stdout.expected $testroot/stdout
127 test_done "$testroot" "$ret"
131 local testroot=`test_init branch_list`
132 local commit_id=`git_show_head $testroot/repo`
134 for b in branch1 branch2 branch3; do
135 got branch -r $testroot/repo $b
137 if [ $ret -ne 0 ]; then
138 echo "got branch command failed unexpectedly"
139 test_done "$testroot" "$ret"
144 got branch -l -r $testroot/repo > $testroot/stdout
145 echo " branch1: $commit_id" > $testroot/stdout.expected
146 echo " branch2: $commit_id" >> $testroot/stdout.expected
147 echo " branch3: $commit_id" >> $testroot/stdout.expected
148 echo " master: $commit_id" >> $testroot/stdout.expected
149 cmp -s $testroot/stdout $testroot/stdout.expected
151 if [ $ret -ne 0 ]; then
152 diff -u $testroot/stdout.expected $testroot/stdout
153 test_done "$testroot" "$ret"
157 got checkout $testroot/repo $testroot/wt >/dev/null
159 if [ $ret -ne 0 ]; then
160 echo "got checkout command failed unexpectedly"
161 test_done "$testroot" "$ret"
165 (cd $testroot/wt && got branch -l > $testroot/stdout)
166 echo " branch1: $commit_id" > $testroot/stdout.expected
167 echo " branch2: $commit_id" >> $testroot/stdout.expected
168 echo " branch3: $commit_id" >> $testroot/stdout.expected
169 echo "* master: $commit_id" >> $testroot/stdout.expected
170 cmp -s $testroot/stdout $testroot/stdout.expected
172 if [ $ret -ne 0 ]; then
173 diff -u $testroot/stdout.expected $testroot/stdout
174 test_done "$testroot" "$ret"
178 echo "modified delta" > $testroot/repo/gamma/delta
179 git_commit $testroot/repo -m "committing to delta"
180 local commit_id2=`git_show_head $testroot/repo`
182 (cd $testroot/wt && got branch -l > $testroot/stdout)
183 echo " branch1: $commit_id" > $testroot/stdout.expected
184 echo " branch2: $commit_id" >> $testroot/stdout.expected
185 echo " branch3: $commit_id" >> $testroot/stdout.expected
186 echo "~ master: $commit_id2" >> $testroot/stdout.expected
187 cmp -s $testroot/stdout $testroot/stdout.expected
189 if [ $ret -ne 0 ]; then
190 diff -u $testroot/stdout.expected $testroot/stdout
191 test_done "$testroot" "$ret"
195 (cd $testroot/wt && got update > /dev/null)
197 if [ $ret -ne 0 ]; then
198 echo "got update command failed unexpectedly"
199 test_done "$testroot" "$ret"
203 (cd $testroot/wt && got branch -l > $testroot/stdout)
204 echo " branch1: $commit_id" > $testroot/stdout.expected
205 echo " branch2: $commit_id" >> $testroot/stdout.expected
206 echo " branch3: $commit_id" >> $testroot/stdout.expected
207 echo "* master: $commit_id2" >> $testroot/stdout.expected
208 cmp -s $testroot/stdout $testroot/stdout.expected
210 if [ $ret -ne 0 ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
212 test_done "$testroot" "$ret"
216 (cd $testroot/wt && got update -b branch1 > /dev/null)
218 if [ $ret -ne 0 ]; then
219 echo "got update command failed unexpectedly"
220 test_done "$testroot" "$ret"
224 (cd $testroot/wt && got branch -l > $testroot/stdout)
225 echo "* branch1: $commit_id" > $testroot/stdout.expected
226 echo " branch2: $commit_id" >> $testroot/stdout.expected
227 echo " branch3: $commit_id" >> $testroot/stdout.expected
228 echo " master: $commit_id2" >> $testroot/stdout.expected
229 cmp -s $testroot/stdout $testroot/stdout.expected
231 if [ $ret -ne 0 ]; then
232 diff -u $testroot/stdout.expected $testroot/stdout
234 test_done "$testroot" "$ret"
237 test_branch_delete() {
238 local testroot=`test_init branch_delete`
239 local commit_id=`git_show_head $testroot/repo`
241 for b in branch1 branch2 branch3; do
242 got branch -r $testroot/repo $b
244 if [ $ret -ne 0 ]; then
245 echo "got branch command failed unexpectedly"
246 test_done "$testroot" "$ret"
251 got branch -d branch2 -r $testroot/repo > $testroot/stdout
253 if [ $ret -ne 0 ]; then
254 echo "got branch command failed unexpectedly"
255 test_done "$testroot" "$ret"
259 got branch -l -r $testroot/repo > $testroot/stdout
260 echo " branch1: $commit_id" > $testroot/stdout.expected
261 echo " branch3: $commit_id" >> $testroot/stdout.expected
262 echo " master: $commit_id" >> $testroot/stdout.expected
263 cmp -s $testroot/stdout $testroot/stdout.expected
265 if [ $ret -ne 0 ]; then
266 diff -u $testroot/stdout.expected $testroot/stdout
267 test_done "$testroot" "$ret"
271 got ref -l -r $testroot/repo > $testroot/stdout
272 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
273 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
274 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
275 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
276 cmp -s $testroot/stdout $testroot/stdout.expected
278 if [ $ret -ne 0 ]; then
279 diff -u $testroot/stdout.expected $testroot/stdout
280 test_done "$testroot" "$ret"
284 got branch -d bogus_branch_name -r $testroot/repo \
285 > $testroot/stdout 2> $testroot/stderr
287 if [ $ret -eq 0 ]; then
288 echo "got branch succeeded unexpectedly"
289 test_done "$testroot" "1"
293 echo "got: reference refs/heads/bogus_branch_name not found" \
294 > $testroot/stderr.expected
295 cmp -s $testroot/stderr $testroot/stderr.expected
297 if [ $ret -ne 0 ]; then
298 diff -u $testroot/stderr.expected $testroot/stderr
299 test_done "$testroot" "$ret"
303 got ref -r $testroot/repo -c master refs/remotes/origin/master
304 got ref -r $testroot/repo -c branch1 refs/remotes/origin/branch1
305 got ref -r $testroot/repo -c branch3 refs/remotes/origin/branch3
307 got ref -l -r $testroot/repo > $testroot/stdout
308 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
309 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
310 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
311 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
312 echo "refs/remotes/origin/branch1: $commit_id" \
313 >> $testroot/stdout.expected
314 echo "refs/remotes/origin/branch3: $commit_id" \
315 >> $testroot/stdout.expected
316 echo "refs/remotes/origin/master: $commit_id" \
317 >> $testroot/stdout.expected
318 cmp -s $testroot/stdout $testroot/stdout.expected
320 if [ $ret -ne 0 ]; then
321 diff -u $testroot/stdout.expected $testroot/stdout
322 test_done "$testroot" "$ret"
326 got branch -d origin/branch1 -r $testroot/repo > $testroot/stdout
328 if [ $ret -ne 0 ]; then
329 echo "got branch command failed unexpectedly"
330 test_done "$testroot" "$ret"
334 got branch -d refs/remotes/origin/branch3 -r $testroot/repo \
337 if [ $ret -ne 0 ]; then
338 echo "got branch command failed unexpectedly"
339 test_done "$testroot" "$ret"
343 got ref -l -r $testroot/repo > $testroot/stdout
344 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
345 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
346 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
347 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
348 echo "refs/remotes/origin/master: $commit_id" \
349 >> $testroot/stdout.expected
350 cmp -s $testroot/stdout $testroot/stdout.expected
352 if [ $ret -ne 0 ]; then
353 diff -u $testroot/stdout.expected $testroot/stdout
355 test_done "$testroot" "$ret"
358 test_branch_delete_current_branch() {
359 local testroot=`test_init branch_delete_current_branch`
360 local commit_id=`git_show_head $testroot/repo`
362 got checkout $testroot/repo $testroot/wt >/dev/null
364 if [ $ret -ne 0 ]; then
365 echo "got checkout command failed unexpectedly"
366 test_done "$testroot" "$ret"
370 (cd $testroot/wt && got branch -d master > $testroot/stdout \
373 echo "got: will not delete this work tree's current branch" \
374 > $testroot/stderr.expected
375 cmp -s $testroot/stderr $testroot/stderr.expected
377 if [ $ret -ne 0 ]; then
378 diff -u $testroot/stderr.expected $testroot/stderr
380 test_done "$testroot" "$ret"
383 test_branch_delete_packed() {
384 local testroot=`test_init branch_delete_packed`
385 local commit_id=`git_show_head $testroot/repo`
387 for b in branch1 branch2 branch3; do
388 got branch -r $testroot/repo $b
390 if [ $ret -ne 0 ]; then
391 echo "got branch command failed unexpectedly"
392 test_done "$testroot" "$ret"
397 git -C $testroot/repo pack-refs --all
399 got branch -d refs/heads/branch2 -r $testroot/repo > $testroot/stdout
401 if [ $ret -ne 0 ]; then
402 echo "got update command failed unexpectedly"
403 test_done "$testroot" "$ret"
407 got branch -l -r $testroot/repo > $testroot/stdout
408 echo " branch1: $commit_id" > $testroot/stdout.expected
409 echo " branch3: $commit_id" >> $testroot/stdout.expected
410 echo " master: $commit_id" >> $testroot/stdout.expected
411 cmp -s $testroot/stdout $testroot/stdout.expected
413 if [ $ret -ne 0 ]; then
414 diff -u $testroot/stdout.expected $testroot/stdout
415 test_done "$testroot" "$ret"
419 got ref -l -r $testroot/repo > $testroot/stdout
420 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
421 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
422 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
423 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
424 cmp -s $testroot/stdout $testroot/stdout.expected
426 if [ $ret -ne 0 ]; then
427 diff -u $testroot/stdout.expected $testroot/stdout
428 test_done "$testroot" "$ret"
432 got branch -d bogus_branch_name -r $testroot/repo \
433 > $testroot/stdout 2> $testroot/stderr
435 if [ $ret -eq 0 ]; then
436 echo "got update succeeded unexpectedly"
437 test_done "$testroot" "1"
441 echo "got: reference refs/heads/bogus_branch_name not found" \
442 > $testroot/stderr.expected
443 cmp -s $testroot/stderr $testroot/stderr.expected
445 if [ $ret -ne 0 ]; then
446 diff -u $testroot/stderr.expected $testroot/stderr
448 test_done "$testroot" "$ret"
452 local testroot=`test_init branch_show`
453 local commit_id=`git_show_head $testroot/repo`
455 for b in branch1 branch2 branch3; do
456 got branch -r $testroot/repo $b
458 if [ $ret -ne 0 ]; then
459 echo "got branch command failed unexpectedly"
460 test_done "$testroot" "$ret"
465 got checkout $testroot/repo $testroot/wt >/dev/null
467 if [ $ret -ne 0 ]; then
468 echo "got checkout command failed unexpectedly"
469 test_done "$testroot" "$ret"
473 (cd $testroot/wt && got branch > $testroot/stdout)
474 echo "master" > $testroot/stdout.expected
475 cmp -s $testroot/stdout $testroot/stdout.expected
477 if [ $ret -ne 0 ]; then
478 diff -u $testroot/stdout.expected $testroot/stdout
479 test_done "$testroot" "$ret"
483 (cd $testroot/wt && got update -b branch1 > /dev/null)
485 if [ $ret -ne 0 ]; then
486 echo "got update command failed unexpectedly"
487 test_done "$testroot" "$ret"
491 (cd $testroot/wt && got branch > $testroot/stdout)
492 echo "branch1" > $testroot/stdout.expected
493 cmp -s $testroot/stdout $testroot/stdout.expected
495 if [ $ret -ne 0 ]; then
496 diff -u $testroot/stdout.expected $testroot/stdout
498 test_done "$testroot" "$ret"
502 test_branch_packed_ref_collision() {
503 local testroot=`test_init branch_packed_ref_collision`
504 local commit_id=`git_show_head $testroot/repo`
506 got br -r $testroot/repo zoo > $testroot/stdout
507 got co -b zoo $testroot/repo $testroot/wt > /dev/null
508 echo "modified alpha" > $testroot/wt/alpha
510 # sleep in order to ensure that a significant fraction of time
511 # passes between commits; required for got branch -t option below
514 (cd $testroot/wt && got commit -m "modified alpha" >/dev/null)
515 local commit_id2=`git_show_branch_head $testroot/repo zoo`
517 # Fabricate a packed reference which points to an older commit
518 # and collides with the existing on-disk reference
519 echo '# pack-refs with: peeled fully-peeled sorted' > \
520 $testroot/repo/.git/packed-refs
521 echo "$commit_id refs/heads/zoo" >> $testroot/repo/.git/packed-refs
523 # Bug: This command used to show both packed and on-disk
524 # variants of ref/heads/zoo:
525 (cd $testroot/wt && got br -lt > $testroot/stdout)
527 echo "~ zoo: $commit_id2" > $testroot/stdout.expected
528 echo " master: $commit_id" >> $testroot/stdout.expected
529 cmp -s $testroot/stdout $testroot/stdout.expected
531 if [ $ret -ne 0 ]; then
532 diff -u $testroot/stdout.expected $testroot/stdout
533 test_done "$testroot" "$ret"
537 test_done "$testroot" "$ret"
540 test_branch_commit_keywords() {
541 local testroot=$(test_init branch_commit_keywords)
543 set -- "$(git_show_head $testroot/repo)"
545 got checkout $testroot/repo $testroot/wt > /dev/null
547 if [ $ret -ne 0 ]; then
548 echo "checkout failed unexpectedly" >&2
549 test_done "$testroot" "$ret"
553 for i in $(seq 4); do
554 echo "beta change $i" > "$testroot/wt/beta"
556 (cd "$testroot/wt" && got ci -m "commit number $i" > /dev/null)
558 if [ $ret -ne 0 ]; then
559 echo "commit failed unexpectedly" >&2
560 test_done "$testroot" "$ret"
563 set -- "$@" "$(git_show_head $testroot/repo)"
566 (cd "$testroot/wt" && got up > /dev/null)
568 echo " kwbranch: $(pop_idx 3 $@)" > $testroot/stdout.expected
569 echo " master: $(pop_idx 5 $@)" >> $testroot/stdout.expected
571 (cd "$testroot/wt" && got br -nc :head:-2 kwbranch > /dev/null)
572 got br -r "$testroot/repo" -l > "$testroot/stdout"
574 cmp -s $testroot/stdout.expected $testroot/stdout
576 if [ $ret -ne 0 ]; then
577 diff -u $testroot/stdout.expected $testroot/stdout
578 test_done "$testroot" "$ret"
582 echo " kwbranch2: $(pop_idx 4 $@)" > $testroot/stdout.expected
584 got br -r "$testroot/repo" -c master:- kwbranch2 > /dev/null
585 got br -r "$testroot/repo" -l | grep kwbranch2 > "$testroot/stdout"
587 cmp -s $testroot/stdout.expected $testroot/stdout
589 if [ $ret -ne 0 ]; then
590 diff -u $testroot/stdout.expected $testroot/stdout
593 test_done "$testroot" "$ret"
596 test_branch_list_worktree_state() {
597 local testroot=$(test_init branch_list_worktree_state)
598 local wt="$testroot/wt"
600 set -- "$(git_show_head "$testroot/repo")"
602 got checkout "$testroot/repo" "$wt" > /dev/null
604 if [ $ret -ne 0 ]; then
605 echo "checkout failed unexpectedly" >&2
606 test_done "$testroot" "$ret"
610 (cd "$wt" && got br -n newbranch > /dev/null)
612 if [ $ret -ne 0 ]; then
613 echo "branch failed unexpectedly" >&2
614 test_done "$testroot" "$ret"
618 # check up-to-date marker is shown with fresh checkout
619 (cd "$wt" && got br -l > "$testroot/stdout")
620 echo "* master: $(pop_idx 1 $@)" > $testroot/stdout.expected
621 echo " newbranch: $(pop_idx 1 $@)" >> $testroot/stdout.expected
622 cmp -s $testroot/stdout $testroot/stdout.expected
624 if [ $ret -ne 0 ]; then
625 diff -u $testroot/stdout.expected $testroot/stdout
626 test_done "$testroot" "$ret"
630 # check out-of-date marker is shown with mixed-commit state
631 echo "mixed-commit" > "$wt/alpha"
632 (cd "$wt" && got commit -m "mixed-commit" > "$testroot/stdout")
633 set -- "$@" "$(git_show_head "$testroot/repo")"
635 (cd "$wt" && got br -l > "$testroot/stdout")
636 echo "~ master: $(pop_idx 2 $@)" > $testroot/stdout.expected
637 echo " newbranch: $(pop_idx 1 $@)" >> $testroot/stdout.expected
638 cmp -s $testroot/stdout $testroot/stdout.expected
640 if [ $ret -ne 0 ]; then
641 diff -u $testroot/stdout.expected $testroot/stdout
642 test_done "$testroot" "$ret"
646 # check up-to-date marker is shown after 'got update'
647 (cd "$wt" && got up > /dev/null)
648 (cd "$wt" && got br -l > "$testroot/stdout")
649 echo "* master: $(pop_idx 2 $@)" > $testroot/stdout.expected
650 echo " newbranch: $(pop_idx 1 $@)" >> $testroot/stdout.expected
651 cmp -s $testroot/stdout $testroot/stdout.expected
653 if [ $ret -ne 0 ]; then
654 diff -u $testroot/stdout.expected $testroot/stdout
655 test_done "$testroot" "$ret"
659 # check out-of-date marker is shown with out-of-date base commit
660 (cd "$wt" && got up -c:head:- > /dev/null)
661 (cd "$wt" && got br -l > "$testroot/stdout")
662 echo "~ master: $(pop_idx 2 $@)" > $testroot/stdout.expected
663 echo " newbranch: $(pop_idx 1 $@)" >> $testroot/stdout.expected
664 cmp -s $testroot/stdout $testroot/stdout.expected
666 if [ $ret -ne 0 ]; then
667 diff -u $testroot/stdout.expected $testroot/stdout
668 test_done "$testroot" "$ret"
672 test_done "$testroot" "$ret"
676 run_test test_branch_create
677 run_test test_branch_list
678 run_test test_branch_delete
679 run_test test_branch_delete_current_branch
680 run_test test_branch_delete_packed
681 run_test test_branch_show
682 run_test test_branch_packed_ref_collision
683 run_test test_branch_commit_keywords
684 run_test test_branch_list_worktree_state