#!/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"

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

trap cleanup EXIT

spawnGitDaemon() {
    [ -n "$TEMP_PKG_DIR" ] || fail "unexpected testsuite error"
    if pidof -q git-daemon; then
        kill `pidof git-daemon`
    fi
    (
        local srv=${1:-srv}
        mkdir -p "$TEMP_PKG_DIR/$srv" || exit 1
        chronic_sh git daemon --base-path="$TEMP_PKG_DIR/$srv" \
            --reuseaddr --detach \
            --pid-file="$TEMP_PKG_DIR/$srv/pid"
        local t=0
        while [ ! -s "$TEMP_PKG_DIR/$srv/pid" ]; do
            sleep 1s
            if [ $t -gt 5 ]; then
                echo "git daemon failed to start"
                exit 1
            fi
            t=$((t+1))
        done
    )
}

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

spawnGitRepo() {
    local repo=${1:-repo}
    mkdir -p "$TEMP_PKG_DIR/$repo"
    (cd "$TEMP_PKG_DIR/$repo" || 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 2.0; do
        echo "# Version $version" >> file.c
        cat >> changelog <<END
Version $version

END
        chronic_sh git commit -a -m "Releasing $version"
        chronic_sh git tag -s -u 72543FAF -m "Version $version" "v$version"
    done)
}

spawnGitSubmodule() {
    spawnGitRepo "srv/${1}.git"
    touch "$TEMP_PKG_DIR/srv/${1}.git/.git/git-daemon-export-ok"
    cd "$TEMP_PKG_DIR/repo" || exit 1
    chronic_sh git submodule add "git://localhost/${1}.git"
    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_PKG_DIR/repo"
    chronic_sh git fetch upstream
    cd - > /dev/null || exit 1
}

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

PKG=foo

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_PKG_DIR}/repo"
    )

    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.XXXXXX)
    if [ -z "$TEMP_PKG_DIR" ]; then
        echo "Failed to create temporary directory" >&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"
}

testGit() {
    makeDebianDir
    writeDebianWatch
    spawnGitRepo
    runTest
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitHead() {
    makeDebianDir
    writeDebianWatch "Git-Pretty: 0.0+git%cd.%h" "HEAD"
    spawnGitRepo
    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 {} \;)
    assertTrue 'downloaded 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' ]"
    cleanup
}

testGitHeadPrettyDescribe() {
    makeDebianDir
    spawnGitRepo
    writeDebianWatch "Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue "downloaded tarfile not present" \
        "[ -f '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' ]"
    assertTrue "pristine tarball not created" \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue "pristine tarball should be a symlink" \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitIgnoreExclusions() {
    makeDebianDir
    writeDebianWatch "Git-Export: all"
    spawnGitRepo
    runTest
    assertTrue 'downloaded tarball is incomplete' \
        "tar tf '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' '${PKG}-2.0/extra.c'"
    cleanup
}

testGitSignedTag() {
    makeDebianDir
    writeDebianWatch "Pgp-Mode: gittag"
    spawnGitRepo
    runTest
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitSignedTagWithDestDir() {
    makeDebianDir
    writeDebianWatch "Pgp-Mode: gittag"
    spawnGitRepo
    local destdir=$TEMP_PKG_DIR/destdir
    mkdir -p $destdir
    runTest "--destdir $destdir"
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$destdir/${PKG}-2.0.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$destdir/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$destdir/$pristine' ]"
    cleanup
}

testGitUncompressed() {
    makeDebianDir
    writeDebianWatch
    spawnGitRepo
    runTest "--vcs-export-uncompressed"
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-2.0.tar' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should not be a symlink' \
        "[ ! -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitVersionMangle() {
    makeDebianDir
    writeDebianWatch "Version-Mangle: s/-rc-/~rc/"
    spawnGitRepo
    runTest "--download-version 1.0-rc-1"
    local pristine=${PKG}_1.0\~rc1.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-1.0~rc1.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitVersionMangleDeb() {
    makeDebianDir
    writeDebianWatch "Version-Mangle: s/-rc-/~rc/"
    spawnGitRepo
    runTest "--download-debversion 1.0~rc1-1"
    local pristine=${PKG}_1.0\~rc1.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-1.0~rc1.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitUpstream() {
    makeDebianDir
    writeDebianWatch
    spawnGitRepo
    spawnGitUpstreamRemote
    runTest
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitUpstreamSignedTag() {
    makeDebianDir
    writeDebianWatch "Pgp-Mode: gittag"
    spawnGitRepo
    spawnGitUpstreamRemote
    runTest
    local pristine=${PKG}_2.0.orig.tar.xz
    assertTrue 'downloaded tarfile not present' \
        "[ -f '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' ]"
    assertTrue 'pristine tarball not created' \
        "[ -f '$TEMP_PKG_DIR/$pristine' ]"
    assertTrue 'pristine tarball should be a symlink' \
        "[ -L '$TEMP_PKG_DIR/$pristine' ]"
    cleanup
}

testGitUpstreamIgnoreExclusions() {
    makeDebianDir
    writeDebianWatch "Git-Export: all"
    spawnGitRepo
    spawnGitUpstreamRemote
    runTest
    assertTrue 'downloaded tarball is incomplete' \
        "tar tf '$TEMP_PKG_DIR/${PKG}-2.0.tar.xz' '${PKG}-2.0/extra.c'"
    cleanup
}

MODULE1=subrepo1
MODULE2=subrepo2

testGitWithSubmodulesInit() {
    makeDebianDir
    spawnGitRepo
    spawnGitDaemon
    spawnGitSubmodule ${MODULE1}
    spawnGitSubmodule ${MODULE2}
    cd "$TEMP_PKG_DIR/repo" || exit 1
    local version="3.0"
    cat >> changelog <<END
Version $version

END
    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
}

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_dir=$(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' \
        '${version_dir}/file.c' \
        '${version_dir}/${MODULE1}/file.c' \
        '${version_dir}/${MODULE2}/file.c'"
    assertFalse "Test 1: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${version_dir}/extra.c' \
        '${version_dir}/${MODULE1}/extra.c' \
        '${version_dir}/${MODULE2}/extra.c'"
    # cleanup() omitted intentionally
    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' \
        '${version_dir}/file.c' \
        '${version_dir}/${MODULE1}/file.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${version_dir}/extra.c' \
        '${version_dir}/${MODULE1}/extra.c' \
        '${version_dir}/${MODULE2}/file.c' \
        '${version_dir}/${MODULE2}/extra.c'"
    # cleanup() omitted intentionally
    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' \
        '${version_dir}/file.c' \
        '${version_dir}/${MODULE2}/file.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${version_dir}/extra.c' \
        '${version_dir}/${MODULE1}/file.c' \
        '${version_dir}/${MODULE1}/extra.c' \
        '${version_dir}/${MODULE2}/extra.c'"
    # cleanup() omitted intentionally
    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_dir=$(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' \
        '${version_dir}/file.c' \
        '${version_dir}/extra.c' \
        '${version_dir}/${MODULE1}/file.c' \
        '${version_dir}/${MODULE1}/extra.c' \
        '${version_dir}/${MODULE2}/file.c' \
        '${version_dir}/${MODULE2}/extra.c'"
    # cleanup() omitted intentionally
    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' \
        '${version_dir}/file.c' \
        '${version_dir}/extra.c' \
        '${version_dir}/${MODULE1}/file.c' \
        '${version_dir}/${MODULE1}/extra.c'"
    assertFalse "Test 2: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${version_dir}/${MODULE2}/file.c' \
        '${version_dir}/${MODULE2}/extra.c'"
    # cleanup() omitted intentionally
    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' \
        '${version_dir}/file.c' \
        '${version_dir}/extra.c' \
        '${version_dir}/${MODULE2}/file.c' \
        '${version_dir}/${MODULE2}/extra.c'"
    assertFalse "Test 3: unexpected files in tarball" \
        "tar -tf '$TEMP_PKG_DIR/$upstream' \
        '${version_dir}/${MODULE1}/file.c' \
        '${version_dir}/${MODULE1}/extra.c'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadWithSubmodulesPrettyDescribe() {
    writeDebianWatch "Git-Modules: all, Git-Mode: full, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local version="3.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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitHeadWithSubmodulesPrettyDescribeIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Mode: full, Git-Modules: all, Git-Pretty: describe, Filename-Mangle: s/^(@PACKAGE@-)v/\$1/" "HEAD"
    runTest
    local version="3.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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitTagWithSubmodules() {
    writeDebianWatch "Git-Modules: all"
    local version="3.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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitTagWithSubmodulesIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Modules: all"
    runTest
    local version="3.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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

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

testGitUpstreamWithSubmodules() {
    writeDebianWatch "Git-Modules: all"
    runTest
    local version="3.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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitUpstreamWithSubmodulesIgnoreExclusions() {
    writeDebianWatch "Git-Export: all, Git-Modules: all"
    runTest
    local version="3.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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    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'"
    # cleanup() omitted intentionally
    rm -f "$TEMP_PKG_DIR/$pristine" "$TEMP_PKG_DIR/$upstream"
}

testGitWithSubmodulesCleanup() {
    killGitDaemon
    cleanup
}

# shellcheck disable=SC1091
. shunit2
