#!/bin/bash set -e # Setup: Add src directory to PATH and create isolated test directory SRCDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../src" && pwd)" export PATH="$SRCDIR:$PATH" TESTDIR=$(mktemp -d) trap "rm -rf '$TESTDIR'" EXIT cd "$TESTDIR" echo "=== Test: subgit add and init ===" # Step 1: Create an upstream repository to track mkdir -p upstream/repo1 cd upstream/repo1 git init echo "test content" > file1.txt git add file1.txt git commit -m "Initial commit" REPO1_COMMIT=$(git rev-parse HEAD) cd "$TESTDIR" # Step 2: Create a parent repository and add the upstream repo as a subrepo mkdir parent cd parent git init subgit add ../upstream/repo1 subrepo1 # Step 3: Verify that subgit add created the necessary files and directories # Check that .subgitrc configuration file was created if [ ! -f .subgitrc ]; then echo "FAIL: .subgitrc not created" exit 1 fi # Check that subrepo is tracked in .subgitrc source .subgitrc if [ -z "${subgit[subrepo1]}" ]; then echo "FAIL: subrepo1 not in subgit array" exit 1 fi # Check that bare repository was created in .subgit/ if [ ! -d .subgit/subrepo1 ]; then echo "FAIL: bare repo not created" exit 1 fi # Check that working directory was created if [ ! -d subrepo1 ]; then echo "FAIL: working directory not created" exit 1 fi # Check that .git is a symlink to the bare repo if [ ! -L subrepo1/.git ]; then echo "FAIL: .git symlink not created" exit 1 fi # Check that files from upstream were checked out if [ ! -f subrepo1/file1.txt ]; then echo "FAIL: file1.txt not present in working directory" exit 1 fi # Check that the correct commit was cloned ACTUAL_COMMIT=$(git -C .subgit/subrepo1 rev-parse HEAD) if [ "$ACTUAL_COMMIT" != "$REPO1_COMMIT" ]; then echo "FAIL: commit mismatch (expected $REPO1_COMMIT, got $ACTUAL_COMMIT)" exit 1 fi # Step 4: Test that init can restore a deleted working directory echo "=== Test: reinit should work ===" rm -rf subrepo1 subgit init -f # Verify that files were restored if [ ! -f subrepo1/file1.txt ]; then echo "FAIL: reinit did not restore working directory" exit 1 fi echo "PASS: add and init test"