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")
215 local ids="$id"
216 local alpha_ids="$(get_blob_id "$repo" "" alpha)"
218 set -- "$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 ids="$ids $id"
244 alpha_ids="$alpha_ids $(get_blob_id "$repo" "" alpha)"
245 set -- "$@" $(git_show_author_time "$repo")
246 done
248 cat <<-EOF >$TOG_TEST_SCRIPT
249 SCREENDUMP
250 EOF
252 # diff consecutive commits with keywords
253 local lhs_id=$(pop_idx 1 $ids)
254 local rhs_id=$(pop_idx 2 $ids)
255 local date=$(date -u -r $(pop_idx 2 $@) +"%a %b %e %X %Y UTC")
257 cat <<-EOF >$testroot/view.expected
258 [1/20] diff $lhs_id $rhs_id
259 commit $rhs_id
260 from: Flan Hacker <flan_hacker@openbsd.org>
261 date: $date
263 commit 1
265 M alpha | 1+ 1-
267 1 file changed, 1 insertion(+), 1 deletion(-)
269 commit - $lhs_id
270 commit + $rhs_id
271 blob - $(pop_idx 1 $alpha_ids)
272 blob + $(pop_idx 2 $alpha_ids)
273 --- alpha
274 +++ alpha
275 @@ -1 +1 @@
276 -alpha
277 +alpha 1
281 (END)
282 EOF
284 tog diff :base:-99 :head:-7
285 cmp -s "$testroot/view.expected" "$testroot/view"
286 ret=$?
287 if [ $ret -ne 0 ]; then
288 diff -u "$testroot/view.expected" "$testroot/view"
289 test_done "$testroot" "$ret"
290 return 1
291 fi
293 # diff arbitrary commits with keywords
294 lhs_id=$(pop_idx 5 $ids)
295 rhs_id=$(pop_idx 8 $ids)
297 cat <<-EOF >$testroot/view.expected
298 [1/10] diff $lhs_id $rhs_id
299 commit - $lhs_id
300 commit + $rhs_id
301 blob - $(pop_idx 5 $alpha_ids)
302 blob + $(pop_idx 8 $alpha_ids)
303 --- alpha
304 +++ alpha
305 @@ -1 +1 @@
306 -alpha 4
307 +alpha 7
321 (END)
322 EOF
324 tog diff master:-4 :head:-
325 cmp -s "$testroot/view.expected" "$testroot/view"
326 ret=$?
327 if [ $ret -ne 0 ]; then
328 diff -u "$testroot/view.expected" "$testroot/view"
329 test_done "$testroot" "$ret"
330 return 1
331 fi
333 # diff consecutive commits using keywords with -r repository
334 lhs_id=$(pop_idx 8 $ids)
335 rhs_id=$(pop_idx 9 $ids)
336 date=$(date -u -r $(pop_idx 9 $@) +"%a %b %e %X %Y UTC")
338 cat <<-EOF >$testroot/view.expected
339 [1/20] diff $lhs_id refs/heads/master
340 commit $rhs_id (master)
341 from: Flan Hacker <flan_hacker@openbsd.org>
342 date: $date
344 commit 8
346 M alpha | 1+ 1-
348 1 file changed, 1 insertion(+), 1 deletion(-)
350 commit - $lhs_id
351 commit + $rhs_id
352 blob - $(pop_idx 8 $alpha_ids)
353 blob + $(pop_idx 9 $alpha_ids)
354 --- alpha
355 +++ alpha
356 @@ -1 +1 @@
357 -alpha 7
358 +alpha 8
362 (END)
363 EOF
365 tog diff -r "$repo" :head:- master
366 cmp -s "$testroot/view.expected" "$testroot/view"
367 ret=$?
368 if [ $ret -ne 0 ]; then
369 diff -u "$testroot/view.expected" "$testroot/view"
370 test_done "$testroot" "$ret"
371 return 1
372 fi
374 test_done "$testroot" "$ret"
377 test_parseargs "$@"
378 run_test test_diff_contiguous_commits
379 run_test test_diff_arbitrary_commits
380 run_test test_diff_J_keymap_on_last_loaded_commit
381 run_test test_diff_commit_keywords