#!/bin/sh
# The commit page. Most checks read the markup for the tree link, the parent
# link, the subject and the diffstat.
test_description='Check content on commit page'
. ./setup.sh
test_expect_success 'generate foo/commit' 'cgit_ok_url "foo/commit" >tmp'
test_expect_success 'find tree link' '
tree=$(git -C repos/foo rev-parse master^{tree}) &&
test_grep "
" tmp'
test_expect_success 'find diff summary' '
grep "1 file changed, 1 insertion, 0 deletions" tmp
'
# A strict CSP refuses an inline style, so the bar takes its widths from SVG
# attributes, which also leave the page valid.
test_expect_success 'the diffstat bar is an SVG sized by its attributes' '
grep "
" tmp
'
test_expect_success 'a commit that changes no files draws no diffstat table' '
(
cd repos/foo &&
git update-ref refs/heads/empty $(git commit-tree HEAD^{tree} -p HEAD -m empty)
) &&
cgit_ok_url "foo/commit/&h=empty" >tmp &&
grep "0 files changed, 0 insertions, 0 deletions" tmp &&
test_grep ! "
" tmp
'
# A root commit has no parent, so it diffs against the empty tree.
test_expect_success 'a root commit diffs against the empty tree' '
root=$(git -C repos/foo rev-list --max-parents=0 HEAD) &&
cgit_ok_url "foo/commit&id=$root" >tmp &&
test_grep "file-1" tmp &&
test_grep ">diff --git a/file-1 b/file-1<" tmp &&
test_grep "
+1
" tmp
'
# A message that is not UTF-8, because its encoding header names something
# iconv cannot convert or is missing altogether, would otherwise reach a
# page declared UTF-8 as it is, so its stray bytes become question marks.
test_expect_success 'a message that is not UTF-8 has its stray bytes replaced' '
(
cd repos/foo &&
tree=$(git rev-parse HEAD^{tree}) &&
printf "tree %s\nauthor A U Thor 1735689600 +0000\n" "$tree" >raw &&
printf "committer A U Thor 1735689600 +0000\n" >>raw &&
printf "encoding no-such-charset\n\ncaf\351 subject\n\nbody \377 end\n" >>raw &&
cid=$(git hash-object --literally -t commit -w raw) &&
git update-ref refs/heads/latin "$cid" &&
printf "tree %s\nauthor A U Thor 1735689600 +0000\n" "$tree" >raw &&
printf "committer A U Thor 1735689600 +0000\n" >>raw &&
printf "\nbare caf\351 subject\n" >>raw &&
cid=$(git hash-object --literally -t commit -w raw) &&
git update-ref refs/heads/bare "$cid"
) &&
cgit_ok_url "foo/commit&h=latin" >tmp &&
grep "
bare caf? subject<" tmp &&
test_grep ! "caf$(printf "\351")" tmp
'
# The diff is taken against the first parent alone, which says little about
# a merge of three branches or more, so such a merge draws none and offers
# no diff options. A merge of two still draws its diff. Both merges take a
# tree their first parent lacks a file of, so a diff would have a row.
test_expect_success 'an octopus merge draws no diff and offers no diff options' '
(
cd repos/foo &&
tree=$(git rev-parse HEAD~1^{tree}) &&
cid=$(git commit-tree -p HEAD -p HEAD~1 -p HEAD~2 -m octopus $tree) &&
git update-ref refs/heads/octopus "$cid" &&
cid=$(git commit-tree -p HEAD -p HEAD~2 -m twofold $tree) &&
git update-ref refs/heads/twofold "$cid"
) &&
cgit_ok_url "foo/commit&h=octopus" >tmp &&
test_grep "