#!/bin/sh

set -e

. ./helpers.sh

# The UPSTREAM_GIT variable of debian/rules is the source of truth for
# the debian/watch URL: trailing .git must be stripped, and any URL
# (eg: opendev.org or a foreign upstream) must be used verbatim. A
# debian/watch which is already in the version=4 mode=git form must
# never be touched, whatever its URL is.

# .git stripped, opendev URL used verbatim.
set_stub_rmadison "" 0
make_fixture "1.0.0-1" unstable "supply" \
	"UPSTREAM_GIT ?= https://opendev.org/openstack/supply.git"
run_modernize
assert_eq "exit code" "${MODERNIZE_RC}" "0"
assert_contains "watch url" "$(cat debian/watch)" \
	"https://opendev.org/openstack/supply refs/tags/(\d[brc\d\.]+)"
assert_not_contains "no .git in url" "$(cat debian/watch)" "supply.git"

# Foreign upstream, from a UPSTREAM_GIT of another form (= instead of :=).
make_fixture "1.0.0-1" unstable "python-k5test" \
	"UPSTREAM_GIT = https://github.com/pythongssapi/k5test.git"
run_modernize
assert_contains "foreign url" "$(cat debian/watch)" \
	"https://github.com/pythongssapi/k5test refs/tags/(\d[brc\d\.]+)"

# Already modern watch, with a foreign URL: untouched.
make_fixture "1.0.0-1" unstable "python-k5test" \
	"UPSTREAM_GIT = https://github.com/pythongssapi/k5test.git"
printf 'version=4\nopts="mode=git,uversionmangle=s/\\.0rc/~rc/;s/\\.0b1/~b1/;s/\\.0b2/~b2/;s/\\.0b3/~b3/" \\\nhttps://example.com/foo refs/tags/(\\d[brc\\d\\.]+)\n' > debian/watch
git add debian/watch
git commit -qm "Add a modern watch file"
WATCH1=$(md5sum debian/watch)
run_modernize
assert_eq "exit code" "${MODERNIZE_RC}" "0"
assert_eq "watch untouched" "$(md5sum debian/watch)" "${WATCH1}"
assert_not_contains "no watch commit" "$(git log --format=%s | head -5)" "Update debian/watch"
assert_contains "watch url kept" "$(cat debian/watch)" "https://example.com/foo"

exit 0
