Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2023 Mark Jamsek <mark@jamsek.dev>
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_diff_contiguous_commits()
20 {
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 13 $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 [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
72 fi
74 test_done "$testroot" "$ret"
75 }
77 test_diff_arbitrary_commits()
78 {
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 13 $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 [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 blob + $new_id (mode 644)
114 --- /dev/null
115 +++ new
116 @@ -0,0 +1 @@
117 +new file
119 (END)
120 EOF
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
129 fi
131 test_done "$testroot" "$ret"
134 test_diff_J_keymap_on_last_loaded_commit()
136 test_init diff_J_keymap_on_last_loaded_commit 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 if [ $i -eq 6 ]; then
146 local id6=$(git_show_head .)
147 local blobid6=$(get_blob_id . "" alpha)
148 elif [ $i -eq 7 ]; then
149 local id7=$(git_show_head .)
150 local blobid7=$(get_blob_id . "" alpha)
151 local author_time=$(git_show_author_time .)
152 fi
153 i=$(( i + 1 ))
154 done
156 local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
158 cat <<EOF >$TOG_TEST_SCRIPT
159 KEY_ENTER open diff view of selected commit
160 S toggle horizontal split
161 TAB tab back to log view
162 23j move to last loaded commit
163 KEY_ENTER select last loaded commit
164 F toggle fullscreen
165 J move down to next commit in the log
166 SCREENDUMP
167 EOF
169 cat <<EOF >$testroot/view.expected
170 [1/20] diff $id6 $id7
171 commit $id7
172 from: Flan Hacker <flan_hacker@openbsd.org>
173 date: $date
177 M alpha | 1+ 1-
179 1 file changed, 1 insertion(+), 1 deletion(-)
181 commit - $id6
182 commit + $id7
183 blob - $blobid6
184 blob + $blobid7
185 --- alpha
186 +++ alpha
187 @@ -1 +1 @@
188 -6
189 +7
193 (END)
194 EOF
196 tog log
197 cmp -s $testroot/view.expected $testroot/view
198 ret=$?
199 if [ $ret -ne 0 ]; then
200 diff -u $testroot/view.expected $testroot/view
201 test_done "$testroot" "$ret"
202 return 1
203 fi
205 test_done "$testroot" "$ret"
208 test_diff_commit_keywords()
210 test_init diff_commit_keywords 120 24
211 local repo="$testroot/repo"
212 local wt="$testroot/wt"
213 local id=$(git_show_head "$repo")
214 local author_time=$(git_show_author_time "$repo")
216 set -A ids "$id"
217 set -A alpha_ids $(get_blob_id "$repo" "" alpha)
218 set -A dates "$author_time"
220 got checkout "$repo" "$wt" > /dev/null
221 ret=$?
222 if [ $ret -ne 0 ]; then
223 echo "got checkout failed unexpectedly"
224 test_done "$testroot" "$ret"
225 return 1
226 fi
228 # move into the work tree (test is run in a subshell)
229 cd "$wt"
231 for i in $(seq 8); do
232 echo "alpha $i" > alpha
234 got ci -m "commit $i" > /dev/null
235 ret=$?
236 if [ $ret -ne 0 ]; then
237 echo "commit failed unexpectedly" >&2
238 test_done "$testroot" "$ret"
239 return 1
240 fi
242 id=$(git_show_head "$repo")
243 set -- "$ids" "$id"
244 ids=$*
245 set -- "$alpha_ids" "$(get_blob_id "$repo" "" alpha)"
246 alpha_ids=$*
247 set -- "$dates" $(git_show_author_time "$repo")
248 dates=$*
249 done
251 cat <<-EOF >$TOG_TEST_SCRIPT
252 SCREENDUMP
253 EOF
255 # diff consecutive commits with keywords
256 local lhs_id=$(pop_id 1 $ids)
257 local rhs_id=$(pop_id 2 $ids)
258 local date=$(date -u -r $(pop_id 2 $dates) +"%a %b %e %X %Y UTC")
260 cat <<-EOF >$testroot/view.expected
261 [1/20] diff $lhs_id $rhs_id
262 commit $rhs_id
263 from: Flan Hacker <flan_hacker@openbsd.org>
264 date: $date
266 commit 1
268 M alpha | 1+ 1-
270 1 file changed, 1 insertion(+), 1 deletion(-)
272 commit - $lhs_id
273 commit + $rhs_id
274 blob - $(pop_id 1 $alpha_ids)
275 blob + $(pop_id 2 $alpha_ids)
276 --- alpha
277 +++ alpha
278 @@ -1 +1 @@
279 -alpha
280 +alpha 1
284 (END)
285 EOF
287 tog diff :base:-99 :head:-7
288 cmp -s "$testroot/view.expected" "$testroot/view"
289 ret=$?
290 if [ $ret -ne 0 ]; then
291 diff -u "$testroot/view.expected" "$testroot/view"
292 test_done "$testroot" "$ret"
293 return 1
294 fi
296 # diff arbitrary commits with keywords
297 lhs_id=$(pop_id 5 $ids)
298 rhs_id=$(pop_id 8 $ids)
300 cat <<-EOF >$testroot/view.expected
301 [1/10] diff $lhs_id $rhs_id
302 commit - $lhs_id
303 commit + $rhs_id
304 blob - $(pop_id 5 $alpha_ids)
305 blob + $(pop_id 8 $alpha_ids)
306 --- alpha
307 +++ alpha
308 @@ -1 +1 @@
309 -alpha 4
310 +alpha 7
324 (END)
325 EOF
327 tog diff master:-4 :head:-
328 cmp -s "$testroot/view.expected" "$testroot/view"
329 ret=$?
330 if [ $ret -ne 0 ]; then
331 diff -u "$testroot/view.expected" "$testroot/view"
332 test_done "$testroot" "$ret"
333 return 1
334 fi
336 # diff consecutive commits using keywords with -r repository
337 lhs_id=$(pop_id 8 $ids)
338 rhs_id=$(pop_id 9 $ids)
339 date=$(date -u -r $(pop_id 9 $dates) +"%a %b %e %X %Y UTC")
341 cat <<-EOF >$testroot/view.expected
342 [1/20] diff $lhs_id refs/heads/master
343 commit $rhs_id (master)
344 from: Flan Hacker <flan_hacker@openbsd.org>
345 date: $date
347 commit 8
349 M alpha | 1+ 1-
351 1 file changed, 1 insertion(+), 1 deletion(-)
353 commit - $lhs_id
354 commit + $rhs_id
355 blob - $(pop_id 8 $alpha_ids)
356 blob + $(pop_id 9 $alpha_ids)
357 --- alpha
358 +++ alpha
359 @@ -1 +1 @@
360 -alpha 7
361 +alpha 8
365 (END)
366 EOF
368 tog diff -r "$repo" :head:- master
369 cmp -s "$testroot/view.expected" "$testroot/view"
370 ret=$?
371 if [ $ret -ne 0 ]; then
372 diff -u "$testroot/view.expected" "$testroot/view"
373 test_done "$testroot" "$ret"
374 return 1
375 fi
377 test_done "$testroot" "$ret"
380 test_parseargs "$@"
381 run_test test_diff_contiguous_commits
382 run_test test_diff_arbitrary_commits
383 run_test test_diff_J_keymap_on_last_loaded_commit
384 run_test test_diff_commit_keywords