regress for tog diff view 'p' keymap; ok stsp@
[got-portable.git] / regress / tog / diff.sh
blob9e9c1f1986ac507f2ccf5449d989cd59ff4bf43c
1 #!/bin/sh
3 # Copyright (c) 2023 Mark Jamsek <mark@jamsek.dev>
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.
17 . ./common.sh
19 test_diff_contiguous_commits()
21 test_init diff_contiguous_commits
23 local commit_id1=`git_show_head $testroot/repo`
24 local alpha_id_old=`get_blob_id $testroot/repo "" alpha`
26 echo "modified alpha" > $testroot/repo/alpha
27 git_commit $testroot/repo -m "changed alpha"
28 local author_time=`git_show_author_time $testroot/repo`
29 local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
30 local head_id=`git_show_head $testroot/repo`
31 local head_id_truncated=`trim_obj_id 27 $head_id`
32 local alpha_id=`get_blob_id $testroot/repo "" alpha`
34 cat <<EOF >$TOG_TEST_SCRIPT
35 SCREENDUMP
36 EOF
38 cat <<EOF >$testroot/view.expected
39 $(trim 80 "[1/20] diff $commit_id1 $head_id_truncated")
40 commit $head_id (master)
41 from: Flan Hacker <flan_hacker@openbsd.org>
42 date: $date
44 changed alpha
46 M alpha | 1+ 1-
48 1 file changed, 1 insertion(+), 1 deletion(-)
50 commit - $commit_id1
51 commit + $head_id
52 blob - $alpha_id_old
53 blob + $alpha_id
54 --- alpha
55 +++ alpha
56 @@ -1 +1 @@
57 -alpha
58 +modified alpha
62 (END)
63 EOF
65 cd $testroot/repo && tog diff $commit_id1 $head_id
66 cmp -s $testroot/view.expected $testroot/view
67 ret=$?
68 if [ $ret -ne 0 ]; then
69 diff -u $testroot/view.expected $testroot/view
70 test_done "$testroot" "$ret"
71 return 1
74 test_done "$testroot" "$ret"
77 test_diff_arbitrary_commits()
79 test_init diff_arbitrary_commits 80 18
81 local commit_id1=`git_show_head $testroot/repo`
82 local alpha_id_old=`get_blob_id $testroot/repo "" alpha`
84 echo "modified alpha" > $testroot/repo/alpha
85 git_commit $testroot/repo -m "changed alpha"
86 local commit_id2=`git_show_head $testroot/repo`
88 echo "modified alpha again" > $testroot/repo/alpha
89 echo "new file" > $testroot/repo/new
90 (cd $testroot/repo && git add new)
91 git_commit $testroot/repo -m "new file"
92 local head_id=`git_show_head $testroot/repo`
93 local head_id_truncated=`trim_obj_id 27 $head_id`
94 local alpha_id=`get_blob_id $testroot/repo "" alpha`
95 local new_id=`get_blob_id $testroot/repo "" new`
97 cat <<EOF >$TOG_TEST_SCRIPT
98 SCREENDUMP
99 EOF
101 cat <<EOF >$testroot/view.expected
102 $(trim 80 "[1/16] diff $commit_id1 $head_id_truncated")
103 commit - $commit_id1
104 commit + $head_id
105 blob - $alpha_id_old
106 blob + $alpha_id
107 --- alpha
108 +++ alpha
109 @@ -1 +1 @@
110 -alpha
111 +modified alpha again
112 blob - /dev/null
113 $(trim 80 "blob + $new_id (mode 644)")
114 --- /dev/null
115 +++ new
116 @@ -0,0 +1 @@
117 +new file
119 (END)
122 cd $testroot/repo && tog diff $commit_id1 $head_id
123 cmp -s $testroot/view.expected $testroot/view
124 ret=$?
125 if [ $ret -ne 0 ]; then
126 diff -u $testroot/view.expected $testroot/view
127 test_done "$testroot" "$ret"
128 return 1
131 test_done "$testroot" "$ret"
134 test_diff_J_keymap()
136 test_init diff_J_keymap 94 24
138 local i=0
140 cd $testroot/repo
142 while [ "$i" -lt 32 ]; do
143 echo $i > alpha
144 git commit -aqm $i
145 # Get timestamp, and blob and commit IDs
146 # of the diff views to be screendumped.
147 if [ $i -eq 6 ]; then
148 local id6=$(git_show_head .)
149 local blobid6=$(get_blob_id . "" alpha)
150 elif [ $i -eq 7 ]; then
151 local id7=$(git_show_head .)
152 local blobid7=$(get_blob_id . "" alpha)
153 local author_time7=$(git_show_author_time .)
154 elif [ $i -eq 25 ]; then
155 local id25=$(git_show_head .)
156 local blobid25=$(get_blob_id . "" alpha)
157 elif [ $i -eq 26 ]; then
158 local id26=$(git_show_head .)
159 local blobid26=$(get_blob_id . "" alpha)
160 local author_time26=$(git_show_author_time .)
162 i=$(( i + 1 ))
163 done
165 local date7=`date -u -r $author_time7 +"%a %b %e %X %Y UTC"`
166 local date26=`date -u -r $author_time26 +"%a %b %e %X %Y UTC"`
168 # Test that J loads the diff view of the next commit when
169 # currently viewing the last commit loaded in the log view.
171 cat <<EOF >$TOG_TEST_SCRIPT
172 KEY_ENTER open diff view of selected commit
173 S toggle horizontal split
174 TAB tab back to log view
175 23j move to last loaded commit
176 KEY_ENTER select last loaded commit
177 F toggle fullscreen
178 J move down to next commit in the log
179 SCREENDUMP
182 cat <<EOF >$testroot/view.expected
183 $(trim 94 "[1/20] diff $id6 $id7")
184 commit $id7
185 from: Flan Hacker <flan_hacker@openbsd.org>
186 date: $date7
190 M alpha | 1+ 1-
192 1 file changed, 1 insertion(+), 1 deletion(-)
194 commit - $id6
195 commit + $id7
196 blob - $blobid6
197 blob + $blobid7
198 --- alpha
199 +++ alpha
200 @@ -1 +1 @@
206 (END)
209 tog log
210 cmp -s $testroot/view.expected $testroot/view
211 ret=$?
212 if [ $ret -ne 0 ]; then
213 diff -u $testroot/view.expected $testroot/view
214 test_done "$testroot" "$ret"
215 return 1
218 # Test that J loads the diff view of the next commit when currently
219 # viewing the last visible commit in the horizontally split log view.
221 cat <<EOF >$TOG_TEST_SCRIPT
222 S toggle horizontal split
223 4j move to last visible commit when in horizontal split
224 KEY_ENTER open diff view of selected commit
225 F toggle fullscreen
226 J move down to next commit in the log
227 SCREENDUMP
230 cat <<EOF >$testroot/view.expected
231 $(trim 94 "[1/20] diff $id25 $id26")
232 commit $id26
233 from: Flan Hacker <flan_hacker@openbsd.org>
234 date: $date26
238 M alpha | 1+ 1-
240 1 file changed, 1 insertion(+), 1 deletion(-)
242 commit - $id25
243 commit + $id26
244 blob - $blobid25
245 blob + $blobid26
246 --- alpha
247 +++ alpha
248 @@ -1 +1 @@
254 (END)
257 tog log
258 cmp -s $testroot/view.expected $testroot/view
259 ret=$?
260 if [ $ret -ne 0 ]; then
261 diff -u $testroot/view.expected $testroot/view
262 test_done "$testroot" "$ret"
263 return 1
266 # Test J correctly requests the log to load more commits when
267 # scrolling beyond the last loaded commit from the diff view.
269 cat <<EOF >$TOG_TEST_SCRIPT
270 S toggle horizontal split
271 4j move to the 5th commit
272 KEY_ENTER open diff view of selected commit
273 F toggle fullscreen
274 20J scroll down and load diff of the 25th commit
275 SCREENDUMP
278 cat <<EOF >$testroot/view.expected
279 $(trim 94 "[1/20] diff $id6 $id7")
280 commit $id7
281 from: Flan Hacker <flan_hacker@openbsd.org>
282 date: $date7
286 M alpha | 1+ 1-
288 1 file changed, 1 insertion(+), 1 deletion(-)
290 commit - $id6
291 commit + $id7
292 blob - $blobid6
293 blob + $blobid7
294 --- alpha
295 +++ alpha
296 @@ -1 +1 @@
302 (END)
305 tog log
306 cmp -s $testroot/view.expected $testroot/view
307 ret=$?
308 if [ $ret -ne 0 ]; then
309 diff -u $testroot/view.expected $testroot/view
310 test_done "$testroot" "$ret"
311 return 1
314 test_done "$testroot" "$ret"
317 test_diff_commit_keywords()
319 test_init diff_commit_keywords 120 24
320 local repo="$testroot/repo"
321 local wt="$testroot/wt"
322 local id=$(git_show_head "$repo")
323 local author_time=$(git_show_author_time "$repo")
324 local ids="$id"
325 local alpha_ids="$(get_blob_id "$repo" "" alpha)"
327 set -- "$author_time"
329 got checkout "$repo" "$wt" > /dev/null
330 ret=$?
331 if [ $ret -ne 0 ]; then
332 echo "got checkout failed unexpectedly"
333 test_done "$testroot" "$ret"
334 return 1
337 # move into the work tree (test is run in a subshell)
338 cd "$wt"
340 for i in $(seq 8); do
341 echo "alpha $i" > alpha
343 got ci -m "commit $i" > /dev/null
344 ret=$?
345 if [ $ret -ne 0 ]; then
346 echo "commit failed unexpectedly" >&2
347 test_done "$testroot" "$ret"
348 return 1
351 id=$(git_show_head "$repo")
352 ids="$ids $id"
353 alpha_ids="$alpha_ids $(get_blob_id "$repo" "" alpha)"
354 set -- "$@" $(git_show_author_time "$repo")
355 done
357 cat <<-EOF >$TOG_TEST_SCRIPT
358 SCREENDUMP
361 # diff consecutive commits with keywords
362 local lhs_id=$(pop_idx 1 $ids)
363 local rhs_id=$(pop_idx 2 $ids)
364 local date=$(date -u -r $(pop_idx 2 $@) +"%a %b %e %X %Y UTC")
366 cat <<-EOF >$testroot/view.expected
367 $(trim 120 "[1/20] diff $lhs_id $rhs_id")
368 commit $rhs_id
369 from: Flan Hacker <flan_hacker@openbsd.org>
370 date: $date
372 commit 1
374 M alpha | 1+ 1-
376 1 file changed, 1 insertion(+), 1 deletion(-)
378 commit - $lhs_id
379 commit + $rhs_id
380 blob - $(pop_idx 1 $alpha_ids)
381 blob + $(pop_idx 2 $alpha_ids)
382 --- alpha
383 +++ alpha
384 @@ -1 +1 @@
385 -alpha
386 +alpha 1
390 (END)
393 tog diff :base:-99 :head:-7
394 cmp -s "$testroot/view.expected" "$testroot/view"
395 ret=$?
396 if [ $ret -ne 0 ]; then
397 diff -u "$testroot/view.expected" "$testroot/view"
398 test_done "$testroot" "$ret"
399 return 1
402 # diff arbitrary commits with keywords
403 lhs_id=$(pop_idx 5 $ids)
404 rhs_id=$(pop_idx 8 $ids)
406 cat <<-EOF >$testroot/view.expected
407 $(trim 120 "[1/10] diff $lhs_id $rhs_id")
408 commit - $lhs_id
409 commit + $rhs_id
410 blob - $(pop_idx 5 $alpha_ids)
411 blob + $(pop_idx 8 $alpha_ids)
412 --- alpha
413 +++ alpha
414 @@ -1 +1 @@
415 -alpha 4
416 +alpha 7
430 (END)
433 tog diff master:-4 :head:-
434 cmp -s "$testroot/view.expected" "$testroot/view"
435 ret=$?
436 if [ $ret -ne 0 ]; then
437 diff -u "$testroot/view.expected" "$testroot/view"
438 test_done "$testroot" "$ret"
439 return 1
442 # diff consecutive commits using keywords with -r repository
443 lhs_id=$(pop_idx 8 $ids)
444 rhs_id=$(pop_idx 9 $ids)
445 date=$(date -u -r $(pop_idx 9 $@) +"%a %b %e %X %Y UTC")
447 cat <<-EOF >$testroot/view.expected
448 [1/20] diff $lhs_id refs/heads/master
449 commit $rhs_id (master)
450 from: Flan Hacker <flan_hacker@openbsd.org>
451 date: $date
453 commit 8
455 M alpha | 1+ 1-
457 1 file changed, 1 insertion(+), 1 deletion(-)
459 commit - $lhs_id
460 commit + $rhs_id
461 blob - $(pop_idx 8 $alpha_ids)
462 blob + $(pop_idx 9 $alpha_ids)
463 --- alpha
464 +++ alpha
465 @@ -1 +1 @@
466 -alpha 7
467 +alpha 8
471 (END)
474 tog diff -r "$repo" :head:- master
475 cmp -s "$testroot/view.expected" "$testroot/view"
476 ret=$?
477 if [ $ret -ne 0 ]; then
478 diff -u "$testroot/view.expected" "$testroot/view"
479 test_done "$testroot" "$ret"
480 return 1
483 test_done "$testroot" "$ret"
486 test_diff_horizontal_scroll()
488 test_init diff_horizontal_scroll
490 local commit_id1=`git_show_head $testroot/repo`
491 local alpha_id_old=`get_blob_id $testroot/repo "" alpha`
494 echo -n "01234567890123456789012345678901234567890123456789"
495 echo "0123456789012345678901234567890123"
496 } >> $testroot/repo/alpha
498 git_commit $testroot/repo -m "scroll"
499 local author_time=`git_show_author_time $testroot/repo`
500 local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
501 local head_id=`git_show_head $testroot/repo`
502 local head_id_truncated=`trim_obj_id 27 $head_id`
503 local alpha_id=`get_blob_id $testroot/repo "" alpha`
505 cat <<EOF >$TOG_TEST_SCRIPT
507 SCREENDUMP
510 cat <<EOF >$testroot/view.expected
511 $(trim 80 "[1/20] diff $commit_id1 $head_id_truncated")
512 $head_id (master)
513 Flan Hacker <flan_hacker@openbsd.org>
514 $date
518 ha | 1+ 0-
520 changed, 1 insertion(+), 0 deletions(-)
522 - $commit_id1
523 + $head_id
524 $alpha_id_old
525 $alpha_id
528 +1,2 @@
530 5678901234567890123456789012345678901234567890123456789012345678901234567890123
534 (END)
537 cd $testroot/repo && tog diff $commit_id1 $head_id
538 cmp -s $testroot/view.expected $testroot/view
539 ret=$?
540 if [ $ret -ne 0 ]; then
541 diff -u $testroot/view.expected $testroot/view
542 test_done "$testroot" "$ret"
543 return 1
546 test_done "$testroot" "$ret"
549 test_diff_p_keymap()
551 test_init diff_p_keymap
553 local id_root=$(git_show_head $testroot/repo)
554 local alpha_root=$(get_blob_id $testroot/repo "" alpha)
555 local beta_root=$(get_blob_id $testroot/repo "" beta)
557 echo "modified alpha" > $testroot/repo/alpha
558 git_commit $testroot/repo -m "modified alpha"
559 local id_mid=$(git_show_head $testroot/repo)
560 local alpha_mid=$(get_blob_id $testroot/repo "" alpha)
561 local beta_mid=$(get_blob_id $testroot/repo "" beta)
563 echo "modified beta" > $testroot/repo/beta
564 git_commit $testroot/repo -m "modified beta"
565 local id_head=$(git_show_head $testroot/repo)
566 local beta_head=$(get_blob_id $testroot/repo "" beta)
567 local author_time=$(git_show_author_time $testroot/repo)
568 local date_head=$(date -u -r $author_time +"%a %b %e %X %Y UTC")
570 cat <<-EOF >$TOG_TEST_SCRIPT
572 SCREENDUMP
575 cat <<EOF >$testroot/content.expected
576 commit $id_head (master)
577 from: Flan Hacker <flan_hacker@openbsd.org>
578 date: $date_head
580 modified beta
582 M beta | 1+ 1-
584 1 file changed, 1 insertion(+), 1 deletion(-)
586 commit - $id_mid
587 commit + $id_head
588 blob - $beta_mid
589 blob + $beta_head
590 --- beta
591 +++ beta
592 @@ -1 +1 @@
593 -beta
594 +modified beta
597 # test diff of parent:child commit has commit info in patch file
598 cd $testroot/repo && tog diff :head:- :head
599 local patchpath=$(tail -1 $testroot/view | cut -d' ' -f 5)
601 if [ ! -e "$patchpath" ]; then
602 echo "tog diff 'p' keymap failed to write patch file" >&2
603 test_done "$testroot" 1
604 return 1
607 mv $patchpath $testroot/content
608 ret=$?
609 if [ $ret -ne 0 ]; then
610 echo "renaming patch file failed unexpectedly" >&2
611 test_done "$testroot" $ret
612 return 1
615 cmp -s $testroot/content.expected $testroot/content
616 ret=$?
617 if [ $ret -ne 0 ]; then
618 diff -u $testroot/content.expected $testroot/content
619 test_done "$testroot" $ret
620 return 1
623 cat <<EOF >$testroot/content.expected
624 commit - $id_root
625 commit + $id_head
626 blob - $alpha_root
627 blob + $alpha_mid
628 --- alpha
629 +++ alpha
630 @@ -1 +1 @@
631 -alpha
632 +modified alpha
633 blob - $beta_root
634 blob + $beta_head
635 --- beta
636 +++ beta
637 @@ -1 +1 @@
638 -beta
639 +modified beta
642 # test diff of arbitrary commits
643 cd $testroot/repo && tog diff :head:-2 :head
644 patchpath=$(tail -1 $testroot/view | cut -d' ' -f 5)
646 if [ ! -e "$patchpath" ]; then
647 echo "tog diff 'p' keymap failed to write patch file" >&2
648 test_done "$testroot" 1
649 return 1
652 mv -f $patchpath $testroot/content
653 ret=$?
654 if [ $ret -ne 0 ]; then
655 echo "renaming patch file failed unexpectedly" >&2
656 test_done "$testroot" $ret
657 return 1
660 cmp -s $testroot/content.expected $testroot/content
661 ret=$?
662 if [ $ret -ne 0 ]; then
663 diff -u $testroot/content.expected $testroot/content
664 test_done "$testroot" $ret
665 return 1
668 test_done "$testroot" $ret
671 test_parseargs "$@"
672 run_test test_diff_contiguous_commits
673 run_test test_diff_arbitrary_commits
674 run_test test_diff_J_keymap
675 run_test test_diff_commit_keywords
676 run_test test_diff_horizontal_scroll
677 run_test test_diff_p_keymap