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.
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
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>
48 1 file changed, 1 insertion(+), 1 deletion(-)
65 cd $testroot/repo
&& tog
diff $commit_id1 $head_id
66 cmp -s $testroot/view.expected
$testroot/view
68 if [ $ret -ne 0 ]; then
69 diff -u $testroot/view.expected
$testroot/view
70 test_done
"$testroot" "$ret"
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
101 cat <<EOF >$testroot/view.expected
102 $(trim 80 "[1/16] diff $commit_id1 $head_id_truncated")
111 +modified alpha again
113 $(trim 80 "blob + $new_id (mode 644)")
122 cd $testroot/repo
&& tog
diff $commit_id1 $head_id
123 cmp -s $testroot/view.expected
$testroot/view
125 if [ $ret -ne 0 ]; then
126 diff -u $testroot/view.expected
$testroot/view
127 test_done
"$testroot" "$ret"
131 test_done
"$testroot" "$ret"
136 test_init diff_J_keymap
94 24
142 while [ "$i" -lt 32 ]; do
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 .
)
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
178 J move down to next commit in the log
182 cat <<EOF >$testroot/view.expected
183 $(trim 94 "[1/20] diff $id6 $id7")
185 from: Flan Hacker <flan_hacker@openbsd.org>
192 1 file changed, 1 insertion(+), 1 deletion(-)
210 cmp -s $testroot/view.expected
$testroot/view
212 if [ $ret -ne 0 ]; then
213 diff -u $testroot/view.expected
$testroot/view
214 test_done
"$testroot" "$ret"
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
226 J move down to next commit in the log
230 cat <<EOF >$testroot/view.expected
231 $(trim 94 "[1/20] diff $id25 $id26")
233 from: Flan Hacker <flan_hacker@openbsd.org>
240 1 file changed, 1 insertion(+), 1 deletion(-)
258 cmp -s $testroot/view.expected
$testroot/view
260 if [ $ret -ne 0 ]; then
261 diff -u $testroot/view.expected
$testroot/view
262 test_done
"$testroot" "$ret"
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
274 20J scroll down and load diff of the 25th commit
278 cat <<EOF >$testroot/view.expected
279 $(trim 94 "[1/20] diff $id6 $id7")
281 from: Flan Hacker <flan_hacker@openbsd.org>
288 1 file changed, 1 insertion(+), 1 deletion(-)
306 cmp -s $testroot/view.expected
$testroot/view
308 if [ $ret -ne 0 ]; then
309 diff -u $testroot/view.expected
$testroot/view
310 test_done
"$testroot" "$ret"
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")
325 local alpha_ids
="$(get_blob_id "$repo" "" alpha)"
327 set -- "$author_time"
329 got checkout
"$repo" "$wt" > /dev
/null
331 if [ $ret -ne 0 ]; then
332 echo "got checkout failed unexpectedly"
333 test_done
"$testroot" "$ret"
337 # move into the work tree (test is run in a subshell)
340 for i
in $
(seq 8); do
341 echo "alpha $i" > alpha
343 got ci
-m "commit $i" > /dev
/null
345 if [ $ret -ne 0 ]; then
346 echo "commit failed unexpectedly" >&2
347 test_done
"$testroot" "$ret"
351 id
=$
(git_show_head
"$repo")
353 alpha_ids
="$alpha_ids $(get_blob_id "$repo" "" alpha)"
354 set -- "$@" $
(git_show_author_time
"$repo")
357 cat <<-EOF >$TOG_TEST_SCRIPT
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")
369 from: Flan Hacker <flan_hacker@openbsd.org>
376 1 file changed, 1 insertion(+), 1 deletion(-)
380 blob - $(pop_idx 1 $alpha_ids)
381 blob + $(pop_idx 2 $alpha_ids)
393 tog
diff :base
:-99 :head:-7
394 cmp -s "$testroot/view.expected" "$testroot/view"
396 if [ $ret -ne 0 ]; then
397 diff -u "$testroot/view.expected" "$testroot/view"
398 test_done
"$testroot" "$ret"
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")
410 blob - $(pop_idx 5 $alpha_ids)
411 blob + $(pop_idx 8 $alpha_ids)
433 tog
diff master
:-4 :head:-
434 cmp -s "$testroot/view.expected" "$testroot/view"
436 if [ $ret -ne 0 ]; then
437 diff -u "$testroot/view.expected" "$testroot/view"
438 test_done
"$testroot" "$ret"
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>
457 1 file changed, 1 insertion(+), 1 deletion(-)
461 blob - $(pop_idx 8 $alpha_ids)
462 blob + $(pop_idx 9 $alpha_ids)
474 tog
diff -r "$repo" :head:- master
475 cmp -s "$testroot/view.expected" "$testroot/view"
477 if [ $ret -ne 0 ]; then
478 diff -u "$testroot/view.expected" "$testroot/view"
479 test_done
"$testroot" "$ret"
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
510 cat <<EOF >$testroot/view.expected
511 $(trim 80 "[1/20] diff $commit_id1 $head_id_truncated")
513 Flan Hacker <flan_hacker@openbsd.org>
520 changed, 1 insertion(+), 0 deletions(-)
530 5678901234567890123456789012345678901234567890123456789012345678901234567890123
537 cd $testroot/repo
&& tog
diff $commit_id1 $head_id
538 cmp -s $testroot/view.expected
$testroot/view
540 if [ $ret -ne 0 ]; then
541 diff -u $testroot/view.expected
$testroot/view
542 test_done
"$testroot" "$ret"
546 test_done
"$testroot" "$ret"
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
575 cat <<EOF >$testroot/content.expected
576 commit $id_head (master)
577 from: Flan Hacker <flan_hacker@openbsd.org>
584 1 file changed, 1 insertion(+), 1 deletion(-)
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
607 mv $patchpath $testroot/content
609 if [ $ret -ne 0 ]; then
610 echo "renaming patch file failed unexpectedly" >&2
611 test_done
"$testroot" $ret
615 cmp -s $testroot/content.expected
$testroot/content
617 if [ $ret -ne 0 ]; then
618 diff -u $testroot/content.expected
$testroot/content
619 test_done
"$testroot" $ret
623 cat <<EOF >$testroot/content.expected
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
652 mv -f $patchpath $testroot/content
654 if [ $ret -ne 0 ]; then
655 echo "renaming patch file failed unexpectedly" >&2
656 test_done
"$testroot" $ret
660 cmp -s $testroot/content.expected
$testroot/content
662 if [ $ret -ne 0 ]; then
663 diff -u $testroot/content.expected
$testroot/content
664 test_done
"$testroot" $ret
668 test_done
"$testroot" $ret
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