#!/bin/bash

shellfu import jat_dump

mkdir myrepo || {
    jat__log_error "error creating repo: myrepo at $PWD"
    return 3
}

jat__pstarts -C myrepo -h "create test repo 'myrepo'"
    jat__cmd git init
    jat__cmd git config user.name 'Alice Original'
    jat__cmd git config user.email 'alice@example.com'
    jat__cmd echo 'Test repo' > README
    jat__cmd git add README
    jat__cmd git commit -m 'Initital commit'
    jat__cmd echo 'echo Hello worls' >run.sh
    jat__cmd git add run.sh
    jat__cmd git commit -m 'Add launcher'
    jat__cmd chmod +x run.sh
    jat__cmd git add run.sh
    jat__cmd git commit -m 'Make launcher executable'
    jat__cmd git tag v0.0.1
    jat__cmd sed -i -e s/s/d/ run.sh
    jat__cmd git add run.sh
    jat__cmd git commit -m 'Fix typo'
jat__pend

jat__pstarts -h "create fork"
    jat__cmd git clone "file://$PWD/myrepo" bobsclone
jat__pend

jat__pstarts -C bobsclone -h "create pull request"
    jat__cmd git config user.name 'Bob Useful'
    jat__cmd git config user.email 'bob@example.com'
    jat__cmd echo Cloner was here. >> README
    jat__cmd git add README
    jat__cmd git commit -m 'Make useful change'
    jat__cmd echo echo ..also here. >> run.sh
    jat__cmd git add run.sh
    jat__cmd git commit -m 'Make other useful change'
    jat__cmd -o ../git.out -e ../git.err \
        git request-pull origin/master "file://${PWD}"
jat__pend

O_REPOLINK="file://$PWD/bobsclone"
case $(git --version | cut -d' ' -f3) in
    1.*) O_REPOLINK+=" master" ;;
esac

jat__pstartt
    jat__cmd -h "no err were seen" -S 1 \
        test -s git.err
    jat__cmd -h "some output was seen" \
        test -s git.out
    jat__cmd -h "Bob is mentioned" \
        grep -qF 'Bob Useful (2)' git.out
    jat__cmd -h "First commit is mentioned" \
        grep -qF 'Make useful change' git.out
    jat__cmd -h "Second commit is mentioned" \
        grep -qF 'Make other useful change' git.out
    jat__cmd -h "Commit summmary is correct" \
        grep -qF '2 files changed, 2 insertions(+)' git.out
    jat__cmd -h "Commit summmary is correct" \
        grep -qFe "$O_REPOLINK" git.out
jat__pend

jat__pstartd
    jat_dump__file git.out git.err
jat__pend
