#!/bin/bash
set -e
. tests/lib

t-dependencies DEBORIG

t-setup-import gbp

# TODO Test defusing of export-subst and export-ignore Git attributes.

uv=${v%-*}
otz=../${p}_${uv}.orig.tar.xz

cd $p

mv debian/changelog debian/change
t-expect-fail "pwd doesn't look like a Debian source package" \
t-git-deborig
mv debian/change debian/changelog

t-git-deborig
test -e $otz

rm debian/source/format
t-git-deborig
test -e ../${p}_${uv}.orig.tar.gz
git checkout debian/source/format

t-dch-commit -v2.0 bump
t-expect-fail "this looks like a native package" \
t-git-deborig
t-git-deborig HEAD~ --version=2.0
test -e ../${p}_2.0.orig.tar.xz
git revert --no-edit HEAD

t-expect-fail "already exists" \
t-git-deborig
t-git-deborig -f
test -e $otz

git-deborig-just-print-check () {
    local tag=$1; shift
    cat <<EOF >../just-print-expected
refs/tags/$tag
$otz
git -c 'tar.tar.xz.command=xz -c' archive --prefix=$p-$uv/ --worktree-attributes -o $otz refs/tags/$tag
EOF
    t-git-deborig --just-print "$@" >../just-print-actual
    diff -u ../just-print-{expected,actual}

    cat <<EOF >../just-print-tag-names-expected
$tag
$uv
v$uv
EOF
    t-git-deborig --just-print-tag-names "$@" >../just-print-tag-names-actual
    diff -u ../just-print-tag-names-{expected,actual}
}

git-deborig-just-print-check upstream/$uv

t-expect-fail "version number 1 2 is not valid" \
t-git-deborig --version="1 2"

t-expect-fail "no upstream tag found; looked for upstream/14%15_16 " \
t-git-deborig --version="13:14:15~16"
test $rc = 6

rm ../${p}_${uv}.orig.tar.*

git tag -a -m $uv $uv
git tag -a -m v$uv v$uv
git tag -d upstream/$uv
t-expect-fail "multiple upstream tags found: 1.0 v1.0" \
t-git-deborig
test $rc = 6
t-grep-mpat 'See UPSTREAM TAG SELECTION' $tmp/t.output

git tag -a -m example-$uv example-$uv
git tag -d $uv v$uv
t-expect-fail "no upstream tag found; looked for upstream/$uv $uv v$uv" \
t-git-deborig
test $rc = 6
t-grep-mpat 'See UPSTREAM TAG SELECTION' $tmp/t.output

cat >debian/gbp.conf <<'END'
[export-orig]
upstream-tag = example-%(version)s
END
git add debian/gbp.conf
git commit -m gbp.conf
git-deborig-just-print-check example-$uv
t-git-deborig

t-ok
