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

t-setup-import examplegit

cd example

suite=stable

t-commit 'No changes, just send to stable' '' stable

t-dgit -wgf build

t-expect-fail 'Apparently-unintentional overwrite' \
t-dgit push-built --trust-changelog stable

t-dgit setup-mergechangelogs

: "---------- changelog totally missing archive's version ----------"

t-expect-fail 'fix conflicts and then commit the result' \
git merge dgit/dgit/stable

git checkout master which
EDITOR=: git commit

: "---------- changelog with UNRELEASED ----------"

perl -i~ -pe 's/^(\w+ \(\S+\)) stable/$1 UNRELEASED/ if $.>1' debian/changelog
git add debian/changelog
git commit -m 'UNRELEASED changelog'

t-dgit -wgf build

t-expect-fail E:'Distribution.*is UNRELEASED' \
t-dgit push-built --trust-changelog stable

git revert --no-edit 'HEAD^{/UNRELEASED changelog}'

t-dgit -wgf build

: "---------- relevant changelog entry has had its version changed ----------"

perl -i~ -pe 's/^(\w+ \(\S+)(\) stable)/$1+X$2/ if $.>1' debian/changelog
git commit -m 'Nobble changelog' debian/changelog

t-expect-fail 'Apparently-unintentional overwrite' \
t-dgit push-built --trust-changelog stable

git revert --no-edit 'HEAD^{/Nobble changelog}'

: "---------- changelog which is mostly syntax errors ----------"

perl -i~ -pe 's/^/GARBAGE / if $.>1 && m/^\w+ \(.*\)/' debian/changelog
git commit -m 'Break changelog' debian/changelog

t-expect-fail 'Perhaps debian/changelog does not mention' \
t-dgit push-built --trust-changelog stable

git revert --no-edit 'HEAD^{/Break changelog}'

: "---------- and that it works if it's not nobbled ----------"

t-dgit push-built --overwrite stable

t-ok
