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

t-restrict x-dgit-skip-suite,bullseye
t-restrict x-dgit-skip-suite,buster
t-dependencies T2U DEBORIG

t-setup-import gbp

t-t2u-settings
t-debpolicy

export XZ_OPT=-0

cd $p

t-git-deborig

t-t2u-setup-repo

t-expect-fail "please supply a --quilt= argument" t-t2u-test

: "Seed a quilt mode with an upload that never was."
: "Also test git-debpush's --print-tag-text option."
t-git-debpush --tag-only --print-tag-text \
				 --quilt=gbp \
				 >$tmp/t.output
t-grep-mpat E:'^\[dgit distro=test-dummy split --quilt=gbp\]$' \
	    $tmp/t.output

v=1.0-2
t-dch-commit -v $v -m bump
t-dch-commit-r

t-dgit -wgf --quilt=gbp --dgit-view-save=split.b quilt-fixup

: 'Test that git-debpush can now figure out the quilt mode for itself.'

t-expect-fail "unneeded --gbp on command line, would have autodetected it" \
t-t2u-test --gbp

t-t2u-test
t-t2u-succeeded

git branch split.p dgit/dgit/sid # we didn't generate this here

t-gbp-pushed-good sid

t-t2u-gittarxz-unpack
t-dgit --quilt=gbp --dgit-view-save=split.t quilt-fixup
t-t2u-gittarxz-reproduced

: 'Test changing suite while in NEW'

v=1.0-3
t-dch-commit -v $v -m bump
t-dch-commit-r experimental
t-t2u-test-core irrecoverable
grep 'Package is in NEW and has not been accepted or rejected yet' \
    ../t2u/worker-cwd/w0/dgit-tmp/t2u.log

: 'Test orig handling when we change suites (#1105766)'

v=1.0-2
t-archive-process-incoming unstable

t-refs-same-start
export XZ_OPT=-1 # change the generated orig

v=1.0-4
t-dch-commit -v $v -m bump
t-dch-commit-r experimental
t-t2u-test
t-t2u-succeeded

origs_from_dsc () { grep -P "^ [0-9a-f].* ${p}.*orig.tar"; }

origs_from_dsc >../old-orig <../mirror/pool/main/${p}_1.0-2.dsc
origs_from_dsc >../new-orig <../incoming/${p}_${v}.dsc
diff -u ../{old,new}-orig

: "Test quilt discrepancy"

git checkout upstream/1.0
echo discrepancy >discrepancy
git add discrepancy
git commit -m discrepancy discrepancy
git tag -m discrepant mutant-upstream/1.0

v=1.0-5
git checkout master~0
git merge mutant-upstream/1.0
t-refs-same-start
t-dch-commit -v $v -m bump
t-dch-commit-r experimental
t-t2u-test-core irrecoverable --remote=salsa --force=detached \
		--upstream=mutant-upstream/1.0
grep -P 'git tree differs from orig in upstream files' \
    ../t2u/worker-cwd/w0/dgit-tmp/t2u.log

: 'Test quilt discrepancy repro instructions'

# seddery the reproduction ommands, and the diff command, from the t2u.log
perl <../t2u/worker-cwd/w0/dgit-tmp/t2u.log -wne '
    BEGIN {
        open CMDS, ">../repro-cmds" or die $!;
        open EXP, ">../repro-exp" or die $!;
    }
    my $should_repro = qr{you should be able to repro}i;
    next unless m{^dgit \(build host\): $should_repro}..0;
    next if !s{^dgit \(build host\): }{}..0;
    next if m{^$should_repro};
    if (m{^ {,7}\d. }) {
        $section = $_;
        next;
    }
    next unless m{\S};
    die "$_ ?" unless s{^ {8,}}{};
    if ($section =~ m{run the following commands}i) {
        s{^(?:dgit|tag2upload-obtain-origs) }{t-$&};
        print CMDS $_ or die $!;
    } elsif ($section =~ m{should.*suggest you should run}o) {
        print EXP $_ or die $!;
    }
    END {
        close CMDS or die $!;
        close EXP or die $!;
    }
'

# There should be precisely one line of diff rune
exp_count=$(wc -l <../repro-exp)
test $exp_count = 1

# Do this repro in a fresh directory (to make sure it gets origs, etc.)
mkdir ../repro-cwd
git worktree add ../repro-cwd/$p
cd ../repro-cwd/$p

# Run the commands we found.  The last command is expected to fail.
set +o pipefail
( set -o pipefail; exec 2>&1; source ../../repro-cmds ) \
    | tee ../../repro-got-all
test "${PIPESTATUS[*]}" = "255 0"
set -o pipefail

# Fish out of the repro output, lines that might be the diff rune
perl -ne 'print if s{^dgit: *}{}' ../../repro-got-all >../../repro-got

# Before we leave the repro dir, run the *expected* diff rune
# to check that it works.
( source ../../repro-exp >../../repro-diff-output )

cd ../../$p

# Check that the expected rune was also printed by the repro
grep -Fxf ../repro-exp ../repro-got

# Check that the expected rune mentioned the change we made
grep -P '^diff.*discrepancy' ../repro-diff-output
grep -P '^\+discrepancy$' ../repro-diff-output


git checkout master

: "Test git-debpush's use of git-deborig"

v=1.0-6
t-dch-commit -v $v -m bump
t-dch-commit-r experimental

git tag 1.0 upstream/1.0
t-expect-fail "use --upstream=TAG to say which one to use" t-t2u-test

git tag -d 1.0
git tag -d upstream/1.0
t-expect-fail "found no upstream tag" t-t2u-test

t-ok
