Skip to content

Instantly share code, notes, and snippets.

@drm317
Created November 22, 2025 19:25
Show Gist options
  • Select an option

  • Save drm317/7a37ede1298df4d82cadaa700a26d6df to your computer and use it in GitHub Desktop.

Select an option

Save drm317/7a37ede1298df4d82cadaa700a26d6df to your computer and use it in GitHub Desktop.
TCR (Test && Commit || Revert) script
#!/bin/bash
# Test && Commit || Revert (TCR) script for macOS
# Usage: ./tcr.sh [test_command]
# If no test command is provided, it will try common test runners
set -e
TEST_CMD="${1:-auto}"
COMMIT_MSG="${2:-TCR: automated commit}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${2}${1}${NC}"
}
# Auto-detect test command
detect_test_cmd() {
if [ -f "package.json" ]; then
if grep -q '"test"' package.json; then
echo "npm test"
else
echo "npm run test"
fi
elif [ -f "pubspec.yaml" ]; then
echo "flutter test"
elif [ -f "build.gradle" ] || [ -f "build.gradle.kts" ] || [ -f "gradlew" ]; then
if [ -f "gradlew" ]; then
echo "./gradlew test"
else
echo "gradle test"
fi
elif [ -f "pom.xml" ]; then
echo "mvn test"
elif find . -maxdepth 2 -name "*.csproj" -o -name "*.sln" | grep -q .; then
echo "dotnet test"
elif [ -f "go.mod" ]; then
echo "go test ./..."
elif [ -f "Cargo.toml" ]; then
echo "cargo test"
elif [ -f "Makefile" ] && grep -q "test:" Makefile; then
echo "make test"
elif [ -f "pytest.ini" ] || [ -f "setup.py" ] || [ -f "pyproject.toml" ]; then
echo "pytest"
else
print_status "No test runner detected. Please specify test command as first argument." "$RED"
exit 1
fi
}
# Determine test command
if [ "$TEST_CMD" = "auto" ]; then
TEST_CMD=$(detect_test_cmd)
print_status "Auto-detected test command: $TEST_CMD" "$YELLOW"
fi
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
print_status "Not in a git repository!" "$RED"
exit 1
fi
# Check for uncommitted changes
if ! git diff --quiet || ! git diff --cached --quiet; then
print_status "Starting TCR cycle with uncommitted changes..." "$YELLOW"
else
print_status "No changes detected. Make some changes first!" "$YELLOW"
exit 0
fi
print_status "Running tests: $TEST_CMD" "$YELLOW"
# Run tests
if eval "$TEST_CMD"; then
# Tests passed - commit
print_status "✅ Tests passed! Committing changes..." "$GREEN"
# Stage all changes
git add -A
# Commit with message
git commit -m "$COMMIT_MSG"
print_status "✅ Changes committed successfully!" "$GREEN"
else
# Tests failed - revert
print_status "❌ Tests failed! Reverting changes..." "$RED"
# Revert all unstaged changes
git checkout -- .
# Remove untracked files
git clean -fd
# Reset staged changes
git reset HEAD .
print_status "✅ Changes reverted!" "$YELLOW"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment