#!/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 save ===" # Step 1: Create an upstream repository with an initial commit mkdir -p upstream/repo1 cd upstream/repo1 git init git config user.email "test@test.com" git config user.name "Test User" echo "test content" > file1.txt git add file1.txt git commit -m "Initial commit" COMMIT1=$(git rev-parse HEAD) cd "$TESTDIR" # Step 2: Create parent repo and add upstream, pinned to specific commit mkdir parent cd parent git init subgit add -c "$COMMIT1" "$TESTDIR/upstream/repo1" subrepo1 # Verify the initial commit was recorded in .subgitrc source .subgitrc if [ "${subgitinfo[subrepo1/commit]}" != "$COMMIT1" ]; then echo "FAIL: initial commit not recorded correctly" exit 1 fi # Step 3: Add a new commit to upstream repository cd "$TESTDIR/upstream/repo1" echo "new content" > file2.txt git add file2.txt git commit -m "Second commit" COMMIT2=$(git rev-parse HEAD) # Step 4: Fetch the new commit and update bare repo to point to it cd "$TESTDIR/parent" git -C .subgit/subrepo1 fetch git -C .subgit/subrepo1 update-ref refs/heads/master refs/remotes/origin/master # Step 5: Use 'subgit save' to update .subgitrc with the new commit subgit save subrepo1 # Step 6: Verify that .subgitrc was updated with the new commit # Check in-memory after sourcing source .subgitrc if [ "${subgitinfo[subrepo1/commit]}" != "$COMMIT2" ]; then echo "FAIL: save did not update commit (expected $COMMIT2, got ${subgitinfo[subrepo1/commit]})" exit 1 fi # Check that file was actually written with new commit if ! grep -q "$COMMIT2" .subgitrc; then echo "FAIL: .subgitrc does not contain new commit" exit 1 fi echo "PASS: save test"