From: =?UTF-8?q?Samo=20Poga=C4=8Dnik?= <samo_pogacnik@t-2.net>
Date: Sat, 19 Oct 2024 19:11:41 +0200
Forwarded: https://github.com/ingydotnet/git-subrepo/pull/639
Subject: Generate test git-repos instead of committed binaries

Instead of having test repos: foo, bar and init committed in a
binary form, generate them using the test/gen* scripts before
starting the first 'test' session. Running 'make clean' removes
the generated test repos.
The original 'binary' test repos are being handled via d/rules
while still existing upstrem.
---
 Makefile     |  2 +-
 test/genbar  | 31 +++++++++++++++++++++++++++
 test/genfoo  | 26 +++++++++++++++++++++++
 test/geninit | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/setup   | 12 +++++++++++
 5 files changed, 130 insertions(+), 1 deletion(-)
 create mode 100755 test/genbar
 create mode 100755 test/genfoo
 create mode 100755 test/geninit

diff --git a/Makefile b/Makefile
index 7521bc6..7a930e1 100644
--- a/Makefile
+++ b/Makefile
@@ -110,7 +110,7 @@ compgen: force
 	    $(SHARE)/zsh-completion/_git-subrepo
 
 clean:
-	rm -fr tmp test/tmp .gitconfig
+	rm -fr tmp test/tmp test/repo .gitconfig 
 
 define docker-make-test
 	docker run --rm \
diff --git a/test/genbar b/test/genbar
new file mode 100755
index 0000000..9d3a276
--- /dev/null
+++ b/test/genbar
@@ -0,0 +1,31 @@
+#!/bin/bash
+set -xe
+
+if [ -z "${1}" ]; then
+	echo "${BASH_SOURCE[0]}: Single argument required (common test repos path)"
+	exit 1
+fi
+
+REPO="bar"
+NAME="Bar"
+TARGET="${1}/$REPO"
+TMPREPO="${1}/tmp/$REPO"
+
+rm -rf "$TMPREPO"
+mkdir -p "$TMPREPO"
+cd "$TMPREPO"
+git init --initial-branch=master .
+git config user.name "${NAME}User"
+git config user.email "${REPO}@${REPO}"
+touch $NAME
+git add $NAME
+git commit -m"$NAME"
+git tag A -m"$NAME"
+mkdir -p bard
+touch bard/Bard
+git add bard
+git commit -m"bard/Bard"
+git config --bool core.bare true
+cd -
+mkdir -p "$1"
+mv "$TMPREPO/.git" "$TARGET"
diff --git a/test/genfoo b/test/genfoo
new file mode 100755
index 0000000..a9b9167
--- /dev/null
+++ b/test/genfoo
@@ -0,0 +1,26 @@
+#!/bin/bash
+set -xe
+
+if [ -z "${1}" ]; then
+	echo "${BASH_SOURCE[0]}: Single argument required (common test repos path)"
+	exit 1
+fi
+
+REPO="foo"
+NAME="Foo"
+TARGET="${1}/$REPO"
+TMPREPO="${1}/tmp/$REPO"
+
+rm -rf "$TMPREPO"
+mkdir -p "$TMPREPO"
+cd "$TMPREPO"
+git init --initial-branch=master .
+git config user.name "${NAME}User"
+git config user.email "${REPO}@${REPO}"
+touch $NAME
+git add $NAME
+git commit -m"$NAME"
+git config --bool core.bare true
+cd -
+mkdir -p "$1"
+mv "${TMPREPO}/.git" "$TARGET"
diff --git a/test/geninit b/test/geninit
new file mode 100755
index 0000000..b70f9f1
--- /dev/null
+++ b/test/geninit
@@ -0,0 +1,60 @@
+#!/bin/bash
+set -xe
+
+if [ -z "${1}" ]; then
+	echo "${BASH_SOURCE[0]}: Single argument required (common test repos path)"
+	exit 1
+fi
+
+REPO="init"
+NAME="Init"
+TARGET="${1}/$REPO"
+TMPREPO="${1}/tmp/$REPO"
+
+rm -rf "$TMPREPO"
+mkdir -p "$TMPREPO"
+cd "$TMPREPO"
+git init --initial-branch=master .
+git config user.name "${NAME}User"
+git config user.email "${REPO}@${REPO}"
+cat <<EOF > ReadMe
+This is a repo to test \`git subrepo init\`.
+
+We will make a short history with a subdir, then we can turn that subdir into a
+subrepo.
+EOF
+git add ReadMe
+git commit -m"Initial commit"
+mkdir -p doc
+cat <<EOF > doc/init.swim
+== Subrepo Init!
+
+This is a file to test the \`git subrepo init\` command.
+EOF
+git add doc
+git commit -m"Add a file in a subdir."
+cat <<EOF >> ReadMe
+
+This repo will go in the git-subrepo test suite.
+EOF
+git add ReadMe
+git commit -m"Add a commit to the mainline."
+cat <<EOF >> doc/init.swim
+
+It lives under the doc directory which will become a subrepo.
+EOF
+git add doc/init.swim
+git commit -m"Add a commit to the subdir."
+cat <<EOF >> ReadMe
+
+EOF
+git add ReadMe
+cat <<EOF >> doc/init.swim
+
+EOF
+git add doc/init.swim
+git commit -m"Add a commit that affects both."
+git config --bool core.bare true
+cd -
+mkdir -p "$1"
+mv "${TMPREPO}/.git" "$TARGET"
diff --git a/test/setup b/test/setup
index ab945f3..baea72d 100644
--- a/test/setup
+++ b/test/setup
@@ -14,6 +14,18 @@ export XDG_CONFIG_HOME=$PWD
 export HOME=$PWD
 export GIT_CONFIG_NOSYSTEM=1
 
+# Generate additional testing git repos, if not already present.
+mkdir -p  "${SCRIPT_DIR}/repo"
+if [ ! -d "${SCRIPT_DIR}/repo/bar" ]; then
+	"${SCRIPT_DIR}/genbar" "${SCRIPT_DIR}/repo"
+fi
+if [ ! -d "${SCRIPT_DIR}/repo/foo" ]; then
+	"${SCRIPT_DIR}/genfoo" "${SCRIPT_DIR}/repo"
+fi
+if [ ! -d "${SCRIPT_DIR}/repo/init" ]; then
+	"${SCRIPT_DIR}/geninit" "${SCRIPT_DIR}/repo"
+fi
+
 BASHLIB=$(
   find "$PWD"/ -type d -name bin -o -type d -name lib | grep -v "\/\.pc\/" | tr '\n' ':'
 )
