Created
September 28, 2023 13:18
-
-
Save HasanKhatib/16bba0df883d0a07f2e5e3aa706dd995 to your computer and use it in GitHub Desktop.
π Universal Git Hook: Auto-detect your project type and run tests before every commit! Keep your codebase clean and bug-free. π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Create global git hooks directory and configure git | |
mkdir -p ~/.git-hooks && git config --global core.hooksPath ~/.git-hooks | |
# Create the pre-commit hook | |
cat > ~/.git-hooks/pre-commit << 'EOF' | |
#!/bin/sh | |
if [ -f "pom.xml" ]; then | |
echo "Running tests for Java project..." | |
# Run tests/checks for Java projects | |
elif [ -f "setup.py" ] || [ -f "pyproject.toml" ]; then | |
echo "Running tests for Python project..." | |
# Run tests/checks for Python projects | |
elif [ -f "go.mod" ]; then | |
echo "Running tests for GoLang project..." | |
# Run tests/checks for Go projects | |
elif [ -f "WORKSPACE" || -f "WORKSPACE.bazel" ]; then | |
if command -v bazel &> /dev/null; then | |
echo "Running tests for Bazel workspace..." | |
bazel run //:gazelle | |
bazel build //... | |
bazel test //... | |
else | |
echo "Bazel not found. Skipping Bazel tests." | |
fi | |
else | |
echo "Unknown project type. Skipping pre-commit tests." | |
fi | |
# Check the exit status and abort the commit if tests fail | |
if [ $? -ne 0 ]; then | |
echo "\nTests failed! Commit aborted.\n" | |
exit 1 | |
fi | |
exit 0 | |
EOF | |
# Make the pre-commit hook executable | |
chmod +x ~/.git-hooks/pre-commit | |
echo "Global git hooks setup complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment