#!/bin/bash

# Copyright (C) 2018, Xavier <yadd@debian.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# On Debian systems, the complete text of the GNU General Public License
# version 3 can be found in the /usr/share/common-licenses/GPL-3 file.

set -u
#set -x

TESTTYPE=Git
. ./lib_test_uscan

COMMAND="chronic_sh uscan --no-conf --compression=xz --dehs"

# prevent the local from messing with this test
export GIT_CONFIG_NOGLOBAL=1
export HOME=""
export XDG_CONFIG_HOME=""

# comment out for debug
#COMMAND="$COMMAND --verbose"
#COMMAND="$COMMAND --debug"

PKG=foo
MODULE1=subrepo1
MODULE2=subrepo2

# shellcheck source=shunit2-helper-functions.sh
. "${0%/*}/shunit2-helper-functions.sh"

cleanup() {
    rm -rf "${TEMP_PKG_DIR}"
}

trap cleanup EXIT

spawnGitDaemon() {
    [ -n "${TEMP_SRV_DIR}" ] || fail "unexpected testsuite error"
    if pidof -q git-daemon; then
        kill `pidof git-daemon`
    fi

    chronic_sh git daemon --base-path="${TEMP_SRV_DIR}" \
        --reuseaddr --detach \
        --pid-file="${TEMP_SRV_DIR}/pid"
    local t=0
    while [ ! -s "${TEMP_SRV_DIR}/pid" ]; do
        sleep 1s
        if [ $t -gt 5 ]; then
            echo "git daemon failed to start"
            exit 1
        fi
        t=$((t+1))
    done
}

killGitDaemon() {
    if [ -n "${TEMP_SRV_DIR}" ] && [ -s "${TEMP_SRV_DIR}/pid" ]; then
        local pid=$(< "${TEMP_SRV_DIR}/pid")
        kill -9 "$pid"
    fi
}

spawnGitRepo() {
    mkdir -p "${TEMP_SRV_DIR}/${1}"
    (
        cd "$TEMP_SRV_DIR/${1}" || exit 1
        chronic_sh git init
        git config user.name "Joe Developer"
        git config user.email "none@debian.org"
        touch changelog file.c extra.c
        echo 'extra.c export-ignore' >.gitattributes
        chronic_sh git add changelog file.c extra.c .gitattributes
        chronic_sh git commit -a -m 'Init'
        for version in 1.0-rc-1 1.0; do
            echo "# Version $version" >> file.c
            echo -e "Version $version\n" >> changelog
            chronic_sh git commit -a -m "Releasing $version"
            chronic_sh git tag -s -u 72543FAF -m "Version $version" "v$version"
        done
    )
}

spawnGitSubmodule() {
    spawnGitRepo "${1}"
    touch "${TEMP_SRV_DIR}/${1}/.git/git-daemon-export-ok"
    cd "${TEMP_SRV_DIR}/${PKG}" || exit 1
    chronic_sh git submodule add "git://localhost/${1}"
    chronic_sh git commit -a -m "Add submodule: '${1}'"
    cd - > /dev/null || exit 1
}

spawnGitUpstreamRemote() {
    cd "$TEMP_PKG_DIR/$PKG" || exit 1
    chronic_sh git init
    chronic_sh git remote add upstream "file:///${TEMP_SRV_DIR}/${PKG}"
    chronic_sh git fetch upstream
    cd - > /dev/null || exit 1
}

writeDebianWatch() {
    local watchargs=$(echo "${1:-}" | tr -d ' ' | tr ',' '\n')
    local gitref=${2:-"refs/tags/v([\\d\\.rc-]+)"}
    declare -A array=(
        [Git-Mode]="shallow"
        [Matching-Pattern]="${gitref}"
        [Mode]="git"
        [Pgp-Mode]="none"
        [Source]="file:///${TEMP_SRV_DIR}/${PKG}"
    )

    for pair in $watchargs; do
        IFS=":" read -r key value <<< "$pair"
        array["$key"]="$value"
    done

    echo -e "Version: 5\n" > "$TEMP_PKG_DIR/$PKG/debian/watch"

    local sorted_keys=$(printf "%s\n" "${!array[@]}" | sort)
    for key in $sorted_keys; do
        echo "$key: ${array[$key]}" >> "$TEMP_PKG_DIR/$PKG/debian/watch"
    done
}

makeDebianDir() {
    TEMP_PKG_DIR=$(mktemp -d --tmpdir="$SHUNIT_TMPDIR" uscan_git_${PKG}.XXXXXX)
    if [ -z "$TEMP_PKG_DIR" ]; then
        echo "Failed to create TEMP_PKG_DIR" >&2
        exit 1
    fi
    mkdir -p "$TEMP_PKG_DIR"/$PKG/debian/upstream
    mkdir -p "$TEMP_PKG_DIR"/$PKG/debian/source

    cat <<END > "$TEMP_PKG_DIR/$PKG/debian/changelog"
$PKG (0-1) unstable; urgency=low

  * Initial release

 -- Joe Developer <jd@debian.org>  Mon, 02 Nov 2013 22:21:31 -0100
END
    echo '3.0 (quilt)' > "$TEMP_PKG_DIR/$PKG/debian/source/format"
    cp -f "$test_dir/uscan/PUBLIC_KEY.asc" "$TEMP_PKG_DIR/$PKG/debian/upstream/signing-key.asc"
}

runTest() {
    local cmd_arg="${1:-}"
    ( cd "$TEMP_PKG_DIR/$PKG" || exit 1 ; $COMMAND $cmd_arg --watchfile=debian/watch )
    assertEquals "uscan: exit_code!=0 but exit_code=0" "$?" "0"
}

testGitSetup() {
    TEMP_SRV_DIR=$(mktemp -d --tmpdir="$SHUNIT_TMPDIR" uscan_git_srv.XXXXXX)
    if [ -z "${TEMP_SRV_DIR}" ]; then
        echo "Failed to create TEMP_SRV_DIR" >&2
        exit 1
    fi

    spawnGitRepo ${PKG}
    spawnGitDaemon
    spawnGitSubmodule ${MODULE1}
    spawnGitSubmodule ${MODULE2}
    cd "${TEMP_SRV_DIR}/${PKG}" || exit 1
    local version="2.0"
    echo -e "Version $version\n" >> changelog
    chronic_sh git commit -a -m "Releasing $version"
    chronic_sh git tag -s -u 72543FAF -m "Version $version" "v$version"
    cd - > /dev/null || exit 1

    makeDebianDir
}

testGitTag() {
    writeDebianWatch
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue 'tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitTagIgnoreExclusions() {
    writeDebianWatch "Git-Export: all"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitTagWithSubmodules() {
    writeDebianWatch "Git-Modules: all"
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    runTest
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 1: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$pristine' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Modules: ${MODULE1}"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Modules: ${MODULE2}"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitTagWithSubmodulesIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Modules: all"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Export: all, Git-Modules: ${MODULE1}"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/${PKG}-${version}.tar.xz"

    writeDebianWatch "Git-Export: all, Git-Modules: ${MODULE2}"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitSignedTag() {
    writeDebianWatch "Pgp-Mode: gittag"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitSignedTagWithDestDir() {
    writeDebianWatch "Pgp-Mode: gittag"
    local destdir=$TEMP_PKG_DIR/destdir
    mkdir -p $destdir
    runTest "--destdir $destdir"
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$destdir/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$destdir/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$destdir/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$destdir/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$destdir/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -rf "$destdir"
}

testGitUncompressed() {
    writeDebianWatch
    runTest "--vcs-export-uncompressed"
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should not be a symlink' \
        "[ ! -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitVersionMangle() {
    writeDebianWatch "Version-Mangle: s/-rc-/~rc/"
    runTest "--download-version 1.0-rc-1"
    local version="1.0~rc1"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitVersionMangleDeb() {
    writeDebianWatch "Version-Mangle: s/-rc-/~rc/"
    runTest "--download-debversion 1.0~rc1-1"
    local version="1.0~rc1"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHead() {
    writeDebianWatch "Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    local pristine=$(find "$TEMP_PKG_DIR" -type l -name "$PKG*" -exec basename {} \;)
    local upstream=$(find "$TEMP_PKG_DIR" -type f -name "$PKG*" -exec basename {} \;)
    local version=$(echo "${upstream#*-}" | cut -d '.' -f -3)
    assertTrue 'tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    local pristine=$(find "$TEMP_PKG_DIR" -type l -name "$PKG*" -exec basename {} \;)
    local upstream=$(find "$TEMP_PKG_DIR" -type f -name "$PKG*" -exec basename {} \;)
    local version=$(echo "${upstream#*-}" | cut -d '.' -f -3)
    assertTrue 'tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadWithSubmodules() {
    writeDebianWatch "Git-Modules: all, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    local pristine=$(find "$TEMP_PKG_DIR" -type l -name "$PKG*" -exec basename {} \;)
    local upstream=$(find "$TEMP_PKG_DIR" -type f -name "$PKG*" -exec basename {} \;)
    local version=$(echo "${upstream#*-}" | cut -d '.' -f -3)
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1 : pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 1: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Modules: ${MODULE1}, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Modules: ${MODULE2}, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadWithSubmodulesIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Modules: all, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    local pristine=$(find "$TEMP_PKG_DIR" -type l -name "$PKG*" -exec basename {} \;)
    local upstream=$(find "$TEMP_PKG_DIR" -type f -name "$PKG*" -exec basename {} \;)
    local version=$(echo "${upstream#*-}" | cut -d '.' -f -3)
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Export: all, Git-Modules: ${MODULE1}, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Export: all, Git-Modules: ${MODULE2}, Git-Pretty: 0.0+git%cd.%h" "HEAD"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadPrettyDescribe() {
    writeDebianWatch "Git-Mode: full, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadPrettyDescribeIgnoreExclusions() {
    writeDebianWatch "Git-Mode: full, Git-Export: all, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadPrettyDescribeWithSubmodules() {
    writeDebianWatch "Git-Modules: all, Git-Mode: full, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 1: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Mode: full, Git-Modules: ${MODULE1}, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Mode: full, Git-Modules: ${MODULE2}, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadPrettyDescribeWithSubmodulesIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Mode: full, Git-Modules: all, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Export: all, Git-Mode: full, Git-Modules: ${MODULE1}, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Export: all, Git-Mode: full, Git-Modules: ${MODULE2}, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitUpstreamSetup() {
    spawnGitUpstreamRemote
    cd "$TEMP_PKG_DIR/$PKG" || exit 1
    chronic_sh git merge upstream
    chronic_sh git submodule update --init
    cd - > /dev/null || exit 1
}

testGitUpstreamTag() {
    writeDebianWatch
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitUpstreamTagIgnoreExclusions() {
    writeDebianWatch "Git-Export: all"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

no_testGitUpstreamTagWithSubmodules() {
    writeDebianWatch "Git-Modules: all"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 1: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Modules: ${MODULE1}"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE1}/file.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    writeDebianWatch "Git-Modules: ${MODULE2}"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/${MODULE2}/file.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

no_testGitUpstreamTagWithSubmodulesIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Modules: all"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "Test 1: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/${PKG}-${version}.tar.xz' ]"
    assertTrue "Test 1: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 1: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 1: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$pristine' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    cd "$TEMP_PKG_DIR/$PKG" || exit 1
    chronic_sh git submodule deinit -- ${MODULE2}
    cd - > /dev/null || exit 1

    writeDebianWatch "Git-Export: all, Git-Modules: ${MODULE1}"
    runTest
    assertTrue "Test 2: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 2: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 2: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"

    cd "$TEMP_PKG_DIR/$PKG" || exit 1
    chronic_sh git submodule deinit -- ${MODULE1}
    chronic_sh git submodule update --init -- ${MODULE2}
    cd - > /dev/null || exit 1

    writeDebianWatch "Git-Export: all, Git-Modules: ${MODULE2}"
    runTest
    assertTrue "Test 3: tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "Test 3: pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "Test 3: tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c' \
        '${PKG}-${version}/extra.c' \
        '${PKG}-${version}/${MODULE2}/file.c' \
        '${PKG}-${version}/${MODULE2}/extra.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/${MODULE1}/file.c' \
        '${PKG}-${version}/${MODULE1}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitUpstreamSignedTag() {
    writeDebianWatch "Pgp-Mode: gittag"
    runTest
    local version="2.0"
    local pristine="${PKG}_${version}.orig.tar.xz"
    local upstream="${PKG}-${version}.tar.xz"
    assertTrue "tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/$upstream' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "tarball is incomplete" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/file.c'"
    assertFalse "unexpected file in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${PKG}-${version}/extra.c'"
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitCleanup() {
    killGitDaemon
    rm -rf "${TEMP_PKG_DIR}" "${TEMP_SRV_DIR}"
}

# shellcheck disable=SC1091
. shunit2
