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

t-restrict x-dgit-skip-suite,bullseye
t-restrict x-dgit-skip-suite,buster
t-dependencies T2U DEBORIG pristine-tar git-buildpackage

t-setup-import upstreamtar
t-t2u-settings
t-debpolicy

: "initialise the pristine-tar branch so that fetch doesn't fail"

cd $p
t-t2u-setup-repo

git checkout --orphan pristine-tar
git rm -rf .
git commit --allow-empty -m 'Start empty pristine-tar branch'
git push salsa pristine-tar
git checkout master
cd ..

: "Make an additional component to test multiple tarballs"

mkdir $p-addit
touch $p-addit/ADDITIONAL
tar zcf ${p}_${v%-*}.orig-addit.tar.gz $p-addit

: "Sort out the maintainer branch for a quilty workflow"

cd $p

echo '3.0 (quilt)' >debian/source/format
git rm debian/source/options
git add debian/source/format
git commit -m 'Switch to 3.0 (quilt)'

mkdir addit
touch addit/ADDITIONAL
git add addit/ADDITIONAL
git commit -m 'add addit in git maintainer branch too'

: "Import tarballs"

gbp import-orig					\
    --pristine-tar				\
    --upstream-version=1.0			\
    --component=addit				\
    ../${p}_${v%-*}.orig.tar.gz			\
    </dev/null

: 'sabotage the pristine-tar branch to make sure t2u has to get right commit'

git checkout -b pristine-tar-sabotaged pristine-tar
touch sabotaged
git add sabotaged
git rm ${p}_*
git commit -m sabotaged
git push salsa pristine-tar-sabotaged:pristine-tar
git checkout master

: "test the 'didn't get the pristine-tar branch'" case

git branch -m pristine-tar{,.aside}
t-expect-fail E:"no local pristine-tar .*, but .* possibly-relevant .* at" \
t-git-debpush					\
    --upstream=upstream/${v%-*}			\
    --quilt=gbp
git branch -m pristine-tar{.aside,}

: "run tag2upload with the pristine-tar orig"

t-refs-same-start
t-t2u-test					\
    --force=pristine-tar-behind			\
    --upstream=upstream/${v%-*}			\
    --quilt=gbp
t-t2u-succeeded

: "check that the upload has all the expected pieces"

t-archive-process-incoming sid
cd $tmp
ls -1 mirror/pool/main/ >uploaded.got
diff -u <<END - uploaded.got
example_1.0-1.debian.tar.xz
example_1.0-1.dsc
example_1.0-1.git.tar.xz
example_1.0-1_source.buildinfo
example_1.0-1_source.changes
example_1.0.orig-addit.tar.gz
example_1.0.orig.tar.gz
example_1.0.orig.tar.gz.asc
END

t-ok
