Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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 function test_log_in_repo {
20 local testroot=`test_init log_in_repo`
21 local head_rev=`git_show_head $testroot/repo`
23 echo "commit $head_rev (master)" > $testroot/stdout.expected
25 for p in "" "." alpha epsilon epsilon/zeta; do
26 (cd $testroot/repo && got log $p | \
27 grep ^commit > $testroot/stdout)
28 cmp -s $testroot/stdout.expected $testroot/stdout
29 ret="$?"
30 if [ "$ret" != "0" ]; then
31 diff -u $testroot/stdout.expected $testroot/stdout
32 test_done "$testroot" "$ret"
33 return 1
34 fi
35 done
37 for p in "" "." zeta; do
38 (cd $testroot/repo/epsilon && got log $p | \
39 grep ^commit > $testroot/stdout)
40 cmp -s $testroot/stdout.expected $testroot/stdout
41 ret="$?"
42 if [ "$ret" != "0" ]; then
43 diff -u $testroot/stdout.expected $testroot/stdout
44 test_done "$testroot" "$ret"
45 return 1
46 fi
47 done
49 test_done "$testroot" "0"
50 }
52 function test_log_in_bare_repo {
53 local testroot=`test_init log_in_bare_repo`
54 local head_rev=`git_show_head $testroot/repo`
56 echo "commit $head_rev (master)" > $testroot/stdout.expected
58 for p in "" "." alpha epsilon epsilon/zeta; do
59 (cd $testroot/repo/.git && got log $p | \
60 grep ^commit > $testroot/stdout)
61 cmp -s $testroot/stdout.expected $testroot/stdout
62 ret="$?"
63 if [ "$ret" != "0" ]; then
64 diff -u $testroot/stdout.expected $testroot/stdout
65 test_done "$testroot" "$ret"
66 return 1
67 fi
68 done
70 test_done "$testroot" "0"
71 }
73 function test_log_in_worktree {
74 local testroot=`test_init log_in_worktree 1`
76 make_test_tree $testroot/repo
77 mkdir -p $testroot/repo/epsilon/d
78 echo foo > $testroot/repo/epsilon/d/foo
79 (cd $testroot/repo && git add .)
80 git_commit $testroot/repo -m "adding the test tree"
81 local head_commit=`git_show_head $testroot/repo`
83 got checkout $testroot/repo $testroot/wt > /dev/null
84 ret="$?"
85 if [ "$ret" != "0" ]; then
86 test_done "$testroot" "$ret"
87 return 1
88 fi
90 echo "commit $head_commit (master)" > $testroot/stdout.expected
92 for p in "" "." alpha epsilon; do
93 (cd $testroot/wt && got log $p | \
94 grep ^commit > $testroot/stdout)
95 cmp -s $testroot/stdout.expected $testroot/stdout
96 ret="$?"
97 if [ "$ret" != "0" ]; then
98 diff -u $testroot/stdout.expected $testroot/stdout
99 test_done "$testroot" "$ret"
100 return 1
101 fi
102 done
104 for p in "" "." zeta; do
105 (cd $testroot/wt/epsilon && got log $p | \
106 grep ^commit > $testroot/stdout)
107 cmp -s $testroot/stdout.expected $testroot/stdout
108 ret="$?"
109 if [ "$ret" != "0" ]; then
110 diff -u $testroot/stdout.expected $testroot/stdout
111 test_done "$testroot" "$ret"
112 return 1
113 fi
114 done
116 for p in "" "." foo; do
117 (cd $testroot/wt/epsilon && got log d/$p | \
118 grep ^commit > $testroot/stdout)
119 cmp -s $testroot/stdout.expected $testroot/stdout
120 ret="$?"
121 if [ "$ret" != "0" ]; then
122 diff -u $testroot/stdout.expected $testroot/stdout
123 test_done "$testroot" "$ret"
124 return 1
125 fi
126 done
128 test_done "$testroot" "0"
131 function test_log_in_worktree_with_path_prefix {
132 local testroot=`test_init log_in_prefixed_worktree`
133 local head_rev=`git_show_head $testroot/repo`
135 echo "modified zeta" > $testroot/repo/epsilon/zeta
136 git_commit $testroot/repo -m "modified zeta"
137 local zeta_rev=`git_show_head $testroot/repo`
139 echo "modified delta" > $testroot/repo/gamma/delta
140 git_commit $testroot/repo -m "modified delta"
142 got checkout -p epsilon $testroot/repo $testroot/wt > /dev/null
143 ret="$?"
144 if [ "$ret" != "0" ]; then
145 test_done "$testroot" "$ret"
146 return 1
147 fi
149 echo "commit $zeta_rev" > $testroot/stdout.expected
150 echo "commit $head_rev" >> $testroot/stdout.expected
152 for p in "" "." zeta; do
153 (cd $testroot/wt && got log $p | \
154 grep ^commit > $testroot/stdout)
155 cmp -s $testroot/stdout.expected $testroot/stdout
156 ret="$?"
157 if [ "$ret" != "0" ]; then
158 diff -u $testroot/stdout.expected $testroot/stdout
159 test_done "$testroot" "$ret"
160 return 1
161 fi
162 done
164 test_done "$testroot" "0"
167 function test_log_tag {
168 local testroot=`test_init log_tag`
169 local commit_id=`git_show_head $testroot/repo`
170 local tag="1.0.0"
171 local tag2="2.0.0"
173 got checkout $testroot/repo $testroot/wt > /dev/null
174 ret="$?"
175 if [ "$ret" != "0" ]; then
176 test_done "$testroot" "$ret"
177 return 1
178 fi
180 (cd $testroot/repo && git tag -a -m "test" $tag)
182 echo "commit $commit_id (master, tags/$tag)" > $testroot/stdout.expected
183 (cd $testroot/wt && got log -l1 -c $tag | grep ^commit \
184 > $testroot/stdout)
185 cmp -s $testroot/stdout.expected $testroot/stdout
186 ret="$?"
187 if [ "$ret" != "0" ]; then
188 diff -u $testroot/stdout.expected $testroot/stdout
189 test_done "$testroot" "$ret"
190 return 1
191 fi
193 # test a "lightweight" tag
194 (cd $testroot/repo && git tag $tag2)
196 echo "commit $commit_id (master, tags/$tag, tags/$tag2)" \
197 > $testroot/stdout.expected
198 (cd $testroot/wt && got log -l1 -c $tag2 | grep ^commit \
199 > $testroot/stdout)
200 cmp -s $testroot/stdout.expected $testroot/stdout
201 ret="$?"
202 if [ "$ret" != "0" ]; then
203 diff -u $testroot/stdout.expected $testroot/stdout
204 fi
205 test_done "$testroot" "$ret"
208 function test_log_limit {
209 local testroot=`test_init log_limit`
210 local commit_id0=`git_show_head $testroot/repo`
212 got checkout $testroot/repo $testroot/wt > /dev/null
213 ret="$?"
214 if [ "$ret" != "0" ]; then
215 test_done "$testroot" "$ret"
216 return 1
217 fi
219 echo "modified alpha" > $testroot/wt/alpha
220 (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
221 local commit_id1=`git_show_head $testroot/repo`
223 (cd $testroot/wt && got rm beta >/dev/null)
224 (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
225 local commit_id2=`git_show_head $testroot/repo`
227 echo "new file" > $testroot/wt/new
228 (cd $testroot/wt && got add new >/dev/null)
229 (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
230 local commit_id3=`git_show_head $testroot/repo`
232 # -l1 should print the first commit only
233 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
234 (cd $testroot/wt && got log -l1 | grep ^commit > $testroot/stdout)
235 cmp -s $testroot/stdout.expected $testroot/stdout
236 ret="$?"
237 if [ "$ret" != "0" ]; then
238 diff -u $testroot/stdout.expected $testroot/stdout
239 test_done "$testroot" "$ret"
240 return 1
241 fi
243 # env var can be used to set a log limit without -l option
244 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
245 echo "commit $commit_id2" >> $testroot/stdout.expected
246 (cd $testroot/wt && env GOT_LOG_DEFAULT_LIMIT=2 got log | \
247 grep ^commit > $testroot/stdout)
248 cmp -s $testroot/stdout.expected $testroot/stdout
249 ret="$?"
250 if [ "$ret" != "0" ]; then
251 diff -u $testroot/stdout.expected $testroot/stdout
252 test_done "$testroot" "$ret"
253 return 1
254 fi
256 # non-numeric env var is ignored
257 (cd $testroot/wt && env GOT_LOG_DEFAULT_LIMIT=foobar got log | \
258 grep ^commit > $testroot/stdout)
259 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
260 echo "commit $commit_id2" >> $testroot/stdout.expected
261 echo "commit $commit_id1" >> $testroot/stdout.expected
262 echo "commit $commit_id0" >> $testroot/stdout.expected
263 cmp -s $testroot/stdout.expected $testroot/stdout
264 ret="$?"
265 if [ "$ret" != "0" ]; then
266 diff -u $testroot/stdout.expected $testroot/stdout
267 test_done "$testroot" "$ret"
268 return 1
269 fi
271 # -l option takes precedence over env var
272 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
273 echo "commit $commit_id2" >> $testroot/stdout.expected
274 echo "commit $commit_id1" >> $testroot/stdout.expected
275 echo "commit $commit_id0" >> $testroot/stdout.expected
276 (cd $testroot/wt && env GOT_LOG_DEFAULT_LIMIT=1 got log -l0 | \
277 grep ^commit > $testroot/stdout)
278 cmp -s $testroot/stdout.expected $testroot/stdout
279 ret="$?"
280 if [ "$ret" != "0" ]; then
281 diff -u $testroot/stdout.expected $testroot/stdout
282 fi
283 test_done "$testroot" "0"
286 function test_log_nonexistent_path {
287 local testroot=`test_init log_nonexistent_path`
288 local head_rev=`git_show_head $testroot/repo`
290 echo "commit $head_rev (master)" > $testroot/stdout.expected
292 (cd $testroot/repo && got log this/does/not/exist \
293 > $testroot/stdout 2> $testroot/stderr)
294 ret="$?"
295 if [ "$ret" == "0" ]; then
296 echo "log command succeeded unexpectedly" >&2
297 test_done "$testroot" "1"
298 return 1
299 fi
301 echo -n > $testroot/stdout.expected
302 cmp -s $testroot/stdout.expected $testroot/stdout
303 ret="$?"
304 if [ "$ret" != "0" ]; then
305 diff -u $testroot/stdout.expected $testroot/stdout
306 test_done "$testroot" "$ret"
307 return 1
308 fi
310 echo "got: this/does/not/exist: no such entry found in tree" \
311 > $testroot/stderr.expected
312 cmp -s $testroot/stderr.expected $testroot/stderr
313 ret="$?"
314 if [ "$ret" != "0" ]; then
315 diff -u $testroot/stderr.expected $testroot/stderr
316 fi
317 test_done "$testroot" "$ret"
320 function test_log_end_at_commit {
321 local testroot=`test_init log_end_at_commit`
322 local commit_id0=`git_show_head $testroot/repo`
324 got checkout $testroot/repo $testroot/wt > /dev/null
325 ret="$?"
326 if [ "$ret" != "0" ]; then
327 test_done "$testroot" "$ret"
328 return 1
329 fi
331 echo "modified alpha" > $testroot/wt/alpha
332 (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
333 local commit_id1=`git_show_head $testroot/repo`
335 (cd $testroot/wt && got rm beta >/dev/null)
336 (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
337 local commit_id2=`git_show_head $testroot/repo`
339 echo "new file" > $testroot/wt/new
340 (cd $testroot/wt && got add new >/dev/null)
341 (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
342 local commit_id3=`git_show_head $testroot/repo`
344 # Print commit 3 only
345 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
346 (cd $testroot/wt && got log -x $commit_id3 | grep ^commit \
347 > $testroot/stdout)
348 cmp -s $testroot/stdout.expected $testroot/stdout
349 ret="$?"
350 if [ "$ret" != "0" ]; then
351 diff -u $testroot/stdout.expected $testroot/stdout
352 test_done "$testroot" "$ret"
353 return 1
354 fi
356 # Print commit 3 up to commit 1 inclusive
357 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
358 echo "commit $commit_id2" >> $testroot/stdout.expected
359 echo "commit $commit_id1" >> $testroot/stdout.expected
360 (cd $testroot/wt && got log -c $commit_id3 -x $commit_id1 | \
361 grep ^commit > $testroot/stdout)
362 cmp -s $testroot/stdout.expected $testroot/stdout
363 ret="$?"
364 if [ "$ret" != "0" ]; then
365 diff -u $testroot/stdout.expected $testroot/stdout
366 test_done "$testroot" "$ret"
367 return 1
368 fi
370 # Create commits on an unrelated branch
371 (cd $testroot/wt && got br foo > /dev/null)
372 echo bar >> $testroot/wt/alpha
373 (cd $testroot/wt && got commit -m "change on branch foo" >/dev/null)
374 local commit_id4=`git_show_branch_head $testroot/repo foo`
376 # Print commit 4 only (in work tree)
377 echo "commit $commit_id4 (foo)" > $testroot/stdout.expected
378 (cd $testroot/wt && got log -x foo | grep ^commit \
379 > $testroot/stdout)
380 cmp -s $testroot/stdout.expected $testroot/stdout
381 ret="$?"
382 if [ "$ret" != "0" ]; then
383 diff -u $testroot/stdout.expected $testroot/stdout
384 test_done "$testroot" "$ret"
385 return 1
386 fi
388 # Print commit 4 only (in repository)
389 echo "commit $commit_id4 (foo)" > $testroot/stdout.expected
390 (cd $testroot/repo && got log -c foo -x foo | grep ^commit \
391 > $testroot/stdout)
392 cmp -s $testroot/stdout.expected $testroot/stdout
393 ret="$?"
394 if [ "$ret" != "0" ]; then
395 diff -u $testroot/stdout.expected $testroot/stdout
396 test_done "$testroot" "$ret"
397 return 1
398 fi
400 # Repository's HEAD is on master branch so -x foo without an explicit
401 # '-c foo' start commit has no effect there
402 echo "commit $commit_id3 (master)" > $testroot/stdout.expected
403 echo "commit $commit_id2" >> $testroot/stdout.expected
404 echo "commit $commit_id1" >> $testroot/stdout.expected
405 echo "commit $commit_id0" >> $testroot/stdout.expected
406 (cd $testroot/repo && got log -x foo | grep ^commit \
407 > $testroot/stdout)
408 cmp -s $testroot/stdout.expected $testroot/stdout
409 ret="$?"
410 if [ "$ret" != "0" ]; then
411 diff -u $testroot/stdout.expected $testroot/stdout
412 test_done "$testroot" "$ret"
413 return 1
414 fi
416 # got will refuse -x with a non-existent commit
417 (cd $testroot/wt && got log -x nonexistent \
418 > $testroot/stdout 2> $testroot/stderr)
419 ret="$?"
420 if [ "$ret" == "0" ]; then
421 echo "log command succeeded unexpectedly" >&2
422 test_done "$testroot" "1"
423 return 1
424 fi
425 echo -n > $testroot/stdout.expected
426 echo "got: nonexistent: bad object id string" \
427 > $testroot/stderr.expected
428 cmp -s $testroot/stderr.expected $testroot/stderr
429 ret="$?"
430 if [ "$ret" != "0" ]; then
431 diff -u $testroot/stderr.expected $testroot/stderr
432 test_done "$testroot" "$ret"
433 return 1
434 fi
435 cmp -s $testroot/stdout.expected $testroot/stdout
436 ret="$?"
437 if [ "$ret" != "0" ]; then
438 diff -u $testroot/stdout.expected $testroot/stdout
439 test_done "$testroot" "$ret"
440 return 1
441 fi
443 # try the same with the hash of an empty string which is very
444 # unlikely to match any object
445 local empty_sha1=da39a3ee5e6b4b0d3255bfef95601890afd80709
446 (cd $testroot/wt && got log -x $empty_sha1 \
447 > $testroot/stdout 2> $testroot/stderr)
448 ret="$?"
449 if [ "$ret" == "0" ]; then
450 echo "log command succeeded unexpectedly" >&2
451 test_done "$testroot" "1"
452 return 1
453 fi
454 echo -n > $testroot/stdout.expected
455 echo "got: $empty_sha1: object not found" > $testroot/stderr.expected
456 cmp -s $testroot/stderr.expected $testroot/stderr
457 ret="$?"
458 if [ "$ret" != "0" ]; then
459 diff -u $testroot/stderr.expected $testroot/stderr
460 test_done "$testroot" "$ret"
461 return 1
462 fi
463 cmp -s $testroot/stdout.expected $testroot/stdout
464 ret="$?"
465 if [ "$ret" != "0" ]; then
466 diff -u $testroot/stdout.expected $testroot/stdout
467 test_done "$testroot" "$ret"
468 return 1
469 fi
471 test_done "$testroot" "0"
474 function test_log_reverse_display {
475 local testroot=`test_init log_reverse_display`
476 local commit_id0=`git_show_head $testroot/repo`
478 got checkout $testroot/repo $testroot/wt > /dev/null
479 ret="$?"
480 if [ "$ret" != "0" ]; then
481 test_done "$testroot" "$ret"
482 return 1
483 fi
485 echo "modified alpha" > $testroot/wt/alpha
486 (cd $testroot/wt && got commit -m 'commit1' > /dev/null)
487 local commit_id1=`git_show_head $testroot/repo`
489 (cd $testroot/wt && got rm beta >/dev/null)
490 (cd $testroot/wt && got commit -m 'commit2' > /dev/null)
491 local commit_id2=`git_show_head $testroot/repo`
493 echo "new file" > $testroot/wt/new
494 (cd $testroot/wt && got add new >/dev/null)
495 (cd $testroot/wt && got commit -m 'commit3' > /dev/null)
496 local commit_id3=`git_show_head $testroot/repo`
498 # -R alone should display all commits in reverse
499 echo "commit $commit_id0" > $testroot/stdout.expected
500 echo "commit $commit_id1" >> $testroot/stdout.expected
501 echo "commit $commit_id2" >> $testroot/stdout.expected
502 echo "commit $commit_id3 (master)" >> $testroot/stdout.expected
503 (cd $testroot/wt && got log -R | grep ^commit > $testroot/stdout)
504 cmp -s $testroot/stdout.expected $testroot/stdout
505 ret="$?"
506 if [ "$ret" != "0" ]; then
507 diff -u $testroot/stdout.expected $testroot/stdout
508 test_done "$testroot" "$ret"
509 return 1
510 fi
512 # -R takes effect after the -l commit traversal limit
513 echo "commit $commit_id2" > $testroot/stdout.expected
514 echo "commit $commit_id3 (master)" >> $testroot/stdout.expected
515 (cd $testroot/wt && got log -R -l2 | grep ^commit > $testroot/stdout)
516 cmp -s $testroot/stdout.expected $testroot/stdout
517 ret="$?"
518 if [ "$ret" != "0" ]; then
519 diff -u $testroot/stdout.expected $testroot/stdout
520 test_done "$testroot" "$ret"
521 return 1
522 fi
524 # -R works with commit ranges specified via -c and -x
525 echo "commit $commit_id1" > $testroot/stdout.expected
526 echo "commit $commit_id2" >> $testroot/stdout.expected
527 echo "commit $commit_id3 (master)" >> $testroot/stdout.expected
528 (cd $testroot/wt && got log -R -c $commit_id3 -x $commit_id1 | \
529 grep ^commit > $testroot/stdout)
530 cmp -s $testroot/stdout.expected $testroot/stdout
531 ret="$?"
532 if [ "$ret" != "0" ]; then
533 diff -u $testroot/stdout.expected $testroot/stdout
534 test_done "$testroot" "$ret"
535 fi
537 # commit matching with -s applies before -R
538 echo "commit $commit_id1" > $testroot/stdout.expected
539 echo "commit $commit_id2" >> $testroot/stdout.expected
540 (cd $testroot/wt && got log -R -s 'commit[12]' | \
541 grep ^commit > $testroot/stdout)
542 cmp -s $testroot/stdout.expected $testroot/stdout
543 ret="$?"
544 if [ "$ret" != "0" ]; then
545 diff -u $testroot/stdout.expected $testroot/stdout
546 fi
547 test_done "$testroot" "$ret"
550 run_test test_log_in_repo
551 run_test test_log_in_bare_repo
552 run_test test_log_in_worktree
553 run_test test_log_in_worktree_with_path_prefix
554 run_test test_log_tag
555 run_test test_log_limit
556 run_test test_log_nonexistent_path
557 run_test test_log_end_at_commit
558 run_test test_log_reverse_display