ingydotnet / git-subrepo

MIT License
3.18k stars 263 forks source link

Add failing subsubrepo test case #604

Open doak opened 7 months ago

doak commented 7 months ago

If the outer subrepo bar is pushed before the inner subrepo doak, the latter will fail with fatal: this operation must be run in a work tree.

This PR add a test case respectively a show case.

Repository layout of test case

- foo                ("super" repo)
    L bar            (subrepo pushed first)
        L doak       (subrepo pushed second)
doak commented 7 months ago

This is a show case in plain Bash:

#!/usr/bin/env bash

set -eu
trap '[ $? -eq 0 ] || echo FAILED' EXIT
set -o pipefail

BASEDIR=$(dirname -- "$0")

setupColors() {
    CRED="`tput setaf 1 2>/dev/null`" ||
    CRED=
    CGREEN="`tput setaf 2 2>/dev/null`" ||
    CGREEN=
    CYELLOW="`tput setaf 3 2>/dev/null`" ||
    CYELLOW=
    CBLUE="`tput setaf 4 2>/dev/null`" ||
    CBLUE=
    CRESET="`tput op 2>/dev/null`" ||
    CRESET=
}

prefixOutput() {
    local prefix=$1
    sed -uE "s/^/$prefix/"
}

check() {
    local line=${BASH_LINENO[0]}

    "$@" &&
    echo "${CGREEN}SUCCESS [$line]: ${@@Q}$CRESET" ||
    echo "${CRED}FAILURE [$line]: ${@@Q}$CRESET"
}

not() {
    ! "$@"
}

git() {
    case "$1" in
        #subrepo)
        #    command git subrepo --debug "${@:2}"
        #    ;;
        *)
            command git "$@"
            ;;
    esac
}

setupRepo() {
    cd "$BASEDIR"
    rm -rf "super" "project1" "project2"

    git init --bare "project1"
    git init --bare "project2"

    git init "super"
    cd "super"

    echo "super" >"file"
    git add "file"
    git commit -m "[1] super"

    mkdir "project1"
    echo "someContent" >"project1/p1"
    git add "project1"
    git commit -m "[1] project1"
    git subrepo init -r "../project1" "project1"

    mkdir -p "project1/ext/project2"
    echo "someContent" >"project1/ext/project2/p2"
    git add "project1"
    git commit -m "[1] project2"
    git subrepo init -r "../project2" "project1/ext/project2"
}

tc-pushSubRepos-OuterFirst() (
    setupRepo

    check git subrepo push "project1"
    check git subrepo push "project1/ext/project2"
)

tc-pushSubRepos-InnerFirst() (
    setupRepo

    check git subrepo push "project1/ext/project2"
    check git subrepo push "project1"
)

FILTER=${1:-.}

setupColors

TEST_CASES=($(
    grep -oP '^tc-\S*(?=\(\))' "$0" |
    grep -P "$FILTER" ||
    echo "No test cases found." >&2
))
for TC in "${TEST_CASES[@]}"; do
    {
        (
            set +e
            (
                set -e
                "$TC"
            )
            [ $? -eq 0 ] ||
            echo "${CRED}Test case aborted!$CRESET"
        ) |
        prefixOutput "$CBLUE[$TC]$CRESET "
        echo
    } 2>&1 1>&3 |
    prefixOutput "$CYELLOW[$TC]$CRESET "
done 3>&1 1>&2