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_blame_basic()
20 {
21 test_init blame_basic 80 8
23 local commit_id1=`git_show_head $testroot/repo`
25 got checkout $testroot/repo $testroot/wt > /dev/null
26 ret=$?
27 if [ $ret -ne 0 ]; then
28 test_done "$testroot" "$ret"
29 return 1
30 fi
32 echo aaaa >> $testroot/wt/alpha
33 (cd $testroot/wt && got commit -m "a change" > /dev/null)
34 local commit_id2=`git_show_head $testroot/repo`
36 echo bbbb >> $testroot/wt/alpha
37 (cd $testroot/wt && got commit -m "b change" > /dev/null)
38 local commit_id3=`git_show_head $testroot/repo`
40 echo cccc >> $testroot/wt/alpha
41 (cd $testroot/wt && got commit -m "c change" > /dev/null)
42 local commit_id4=`git_show_head $testroot/repo`
43 local author_time=`git_show_author_time $testroot/repo`
44 local ymd=`date -u -r $author_time +"%F"`
46 cat <<EOF >$TOG_TEST_SCRIPT
47 WAIT_FOR_UI wait for blame to finish
48 SCREENDUMP
49 EOF
51 local commit_id1_short=`trim_obj_id 32 $commit_id1`
52 local commit_id2_short=`trim_obj_id 32 $commit_id2`
53 local commit_id3_short=`trim_obj_id 32 $commit_id3`
54 local commit_id4_short=`trim_obj_id 32 $commit_id4`
56 cat <<EOF >$testroot/view.expected
57 commit $commit_id4
58 [1/4] /alpha
59 $commit_id1_short alpha
60 $commit_id2_short aaaa
61 $commit_id3_short bbbb
62 $commit_id4_short cccc
65 EOF
67 cd $testroot/wt && tog blame alpha
68 cmp -s $testroot/view.expected $testroot/view
69 ret=$?
70 if [ $ret -ne 0 ]; then
71 diff -u $testroot/view.expected $testroot/view
72 test_done "$testroot" "$ret"
73 return 1
74 fi
76 test_done "$testroot" "$ret"
77 }
79 test_blame_commit_keywords()
80 {
81 test_init blame_commit_keywords 80 10
82 local repo="$testroot/repo"
83 local wt="$testroot/wt"
84 local id=$(git_show_head "$repo")
85 local author_time=$(git_show_author_time "$repo")
86 local ymd=$(date -u -r $author_time +"%F")
88 set -- "$id"
90 cat <<-EOF >$TOG_TEST_SCRIPT
91 WAIT_FOR_UI wait for blame to finish
92 SCREENDUMP
93 EOF
95 # :base requires work tree
96 echo "tog: '-c :base' requires work tree" > "$testroot/stderr.expected"
97 tog blame -r "$repo" -c:base alpha 2> "$testroot/stderr"
98 ret=$?
99 if [ $ret -eq 0 ]; then
100 echo "blame command succeeded unexpectedly" >&2
101 test_done "$testroot" "1"
102 return 1
103 fi
105 cmp -s "$testroot/stderr.expected" "$testroot/stderr"
106 ret=$?
107 if [ $ret -ne 0 ]; then
108 diff -u "$testroot/stderr.expected" "$testroot/stderr"
109 test_done "$testroot" "$ret"
110 return 1
111 fi
113 # :head keyword in repo
114 cat <<-EOF >$testroot/view.expected
115 commit $id
116 [1/1] /alpha
117 $(trim_obj_id 32 $(pop_idx 1 $@)) alpha
125 EOF
127 tog blame -r "$repo" -c:head alpha
128 ret=$?
129 if [ $ret -ne 0 ]; then
130 echo "blame command failed unexpectedly" >&2
131 test_done "$testroot" "1"
132 return 1
133 fi
135 cmp -s $testroot/view.expected $testroot/view
136 ret=$?
137 if [ $ret -ne 0 ]; then
138 diff -u $testroot/view.expected $testroot/view
139 test_done "$testroot" "$ret"
140 return 1
141 fi
143 got checkout "$repo" "$wt" > /dev/null
144 ret=$?
145 if [ $ret -ne 0 ]; then
146 echo "got checkout failed unexpectedly"
147 test_done "$testroot" "$ret"
148 return 1
149 fi
151 # move into the work tree (test is run in a subshell)
152 cd "$wt"
153 echo -n > alpha
155 for i in $(seq 8); do
156 echo "alpha $i" >> alpha
158 got ci -m "commit $i" > /dev/null
159 ret=$?
160 if [ $ret -ne 0 ]; then
161 echo "commit failed unexpectedly" >&2
162 test_done "$testroot" "$ret"
163 return 1
164 fi
166 id=$(git_show_head "$repo")
167 set -- "$@" "$id"
168 done
170 author_time=$(git_show_author_time "$repo")
171 ymd=$(date -u -r $author_time +"%F")
173 # :base:- keyword in work tree
174 cat <<-EOF >$testroot/view.expected
175 commit $(pop_idx 8 $@)
176 [1/7] /alpha
177 $(trim_obj_id 32 $(pop_idx 2 $@)) alpha 1
178 $(trim_obj_id 32 $(pop_idx 3 $@)) alpha 2
179 $(trim_obj_id 32 $(pop_idx 4 $@)) alpha 3
180 $(trim_obj_id 32 $(pop_idx 5 $@)) alpha 4
181 $(trim_obj_id 32 $(pop_idx 6 $@)) alpha 5
182 $(trim_obj_id 32 $(pop_idx 7 $@)) alpha 6
183 $(trim_obj_id 32 $(pop_idx 8 $@)) alpha 7
185 EOF
187 tog blame -c:base:- alpha
188 ret=$?
189 if [ $ret -ne 0 ]; then
190 echo "blame command failed unexpectedly" >&2
191 test_done "$testroot" "1"
192 return 1
193 fi
195 cmp -s $testroot/view.expected $testroot/view
196 ret=$?
197 if [ $ret -ne 0 ]; then
198 diff -u $testroot/view.expected $testroot/view
199 test_done "$testroot" "$ret"
200 return 1
201 fi
203 # :head:-4 keyword in work tree
204 cat <<-EOF >$testroot/view.expected
205 commit $(pop_idx 5 $@)
206 [1/4] /alpha
207 $(trim_obj_id 32 $(pop_idx 2 $@)) alpha 1
208 $(trim_obj_id 32 $(pop_idx 3 $@)) alpha 2
209 $(trim_obj_id 32 $(pop_idx 4 $@)) alpha 3
210 $(trim_obj_id 32 $(pop_idx 5 $@)) alpha 4
215 EOF
217 tog blame -c:head:-4 alpha
218 ret=$?
219 if [ $ret -ne 0 ]; then
220 echo "blame command failed unexpectedly" >&2
221 test_done "$testroot" "1"
222 return 1
223 fi
225 cmp -s $testroot/view.expected $testroot/view
226 ret=$?
227 if [ $ret -ne 0 ]; then
228 diff -u $testroot/view.expected $testroot/view
229 test_done "$testroot" "$ret"
230 return 1
231 fi
233 # :base:+2 keyword in work tree
234 cat <<-EOF >$testroot/view.expected
235 commit $(pop_idx 5 $@)
236 [1/4] /alpha
237 $(trim_obj_id 32 $(pop_idx 2 $@)) alpha 1
238 $(trim_obj_id 32 $(pop_idx 3 $@)) alpha 2
239 $(trim_obj_id 32 $(pop_idx 4 $@)) alpha 3
240 $(trim_obj_id 32 $(pop_idx 5 $@)) alpha 4
245 EOF
247 got up -c:head:-6 > /dev/null
248 if [ $ret -ne 0 ]; then
249 echo "update command failed unexpectedly" >&2
250 test_done "$testroot" "1"
251 return 1
252 fi
254 tog blame -c:base:+2 alpha
255 ret=$?
256 if [ $ret -ne 0 ]; then
257 echo "blame command failed unexpectedly" >&2
258 test_done "$testroot" "1"
259 return 1
260 fi
262 cmp -s $testroot/view.expected $testroot/view
263 ret=$?
264 if [ $ret -ne 0 ]; then
265 diff -u $testroot/view.expected $testroot/view
266 test_done "$testroot" "$ret"
267 return 1
268 fi
270 # master:-99 keyword in work tree
271 cat <<-EOF >$testroot/view.expected
272 commit $(pop_idx 1 $@)
273 [1/1] /alpha
274 $(trim_obj_id 32 $(pop_idx 1 $@)) alpha
282 EOF
284 tog blame -cmaster:-99 alpha
285 ret=$?
286 if [ $ret -ne 0 ]; then
287 echo "blame command failed unexpectedly" >&2
288 test_done "$testroot" "1"
289 return 1
290 fi
292 cmp -s $testroot/view.expected $testroot/view
293 ret=$?
294 if [ $ret -ne 0 ]; then
295 diff -u $testroot/view.expected $testroot/view
296 test_done "$testroot" "$ret"
297 return 1
298 fi
300 test_done "$testroot" "$ret"
303 test_parseargs "$@"
304 run_test test_blame_basic
305 run_test test_blame_commit_keywords