#!/bin/sh

set -e

echo % init
hg init

echo % committing changes
count=0
echo > a
while test $count -lt 32 ; do
    echo 'a' >> a
    test $count -eq 0 && hg add
    hg ci -m "msg $count" -d "$count 0"
    echo % committed changeset $count
    count=`expr $count + 1`
done

echo % log
hg log

echo % hg up -C
hg up -C

echo % bisect test
hg bisect -r
hg bisect -b
hg bisect -g 1
hg bisect -g
echo skip
hg bisect -s
hg bisect -g
hg bisect -g
hg bisect -b
hg bisect -g

echo % bisect reverse test
hg bisect -r
hg bisect -b null
hg bisect -g tip
hg bisect -g
echo skip
hg bisect -s
hg bisect -g
hg bisect -g
hg bisect -b
hg bisect -g

hg bisect -r
hg bisect -g tip
hg bisect -b tip || echo error

hg bisect -r
hg bisect -g null
hg bisect -bU tip
hg id

echo % reproduce AssertionError, issue1228 and issue1182
hg bisect -r
hg bisect -b 4
hg bisect -g 0
hg bisect -s
hg bisect -s
hg bisect -s

echo % reproduce non converging bisect, issue1182
hg bisect -r
hg bisect -g 0
hg bisect -b 2
hg bisect -s

echo % test no action
hg bisect -r
hg bisect || echo failure

echo % reproduce AssertionError, issue1445
hg bisect -r
hg bisect -b 6
hg bisect -g 0
hg bisect -s
hg bisect -s
hg bisect -s
hg bisect -s 
hg bisect -g

set +e

echo % test invalid command
hg bisect -r
hg bisect --command './foobar' 2>&1 | sed 's|\(/bin/sh: \./foobar:\).*|\1 not found|'

echo % test bisecting command
cat > script.py <<EOF
#!/usr/bin/env python
import sys
from mercurial import ui, hg
repo = hg.repository(ui.ui(), '.')
if repo['.'].rev() < 6:
    sys.exit(1)
EOF
chmod +x script.py
hg bisect -r
hg bisect --good tip
hg bisect --bad 0
hg bisect --command "`pwd`/script.py"
true
