cloudenv / git-hooks /pre-push
Vishvam10
initial space commit
b404de3
Raw History Blame Contribute Delete
1.35 kB
#!/bin/bash
printf "\nRunning pre-push hook\n" >&2
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Prevent push if the branch is 'main' or 'master'
if [[ "$current_branch" == "main" || "$current_branch" == "master" ]]; then
echo "🔴 Pushing to '$current_branch' is not allowed!"
echo "Please use a feature branch and open a pull request."
exit 1
fi
echo "Running Ruff linter and applying fixes ..."
echo "Checking code quality ..."
ruff check --config pyproject.toml
if [[ $? -ne 0 ]]; then
printf "\n🔴 Pre-commit hook failed due to Ruff linter errors.\n"
exit 1
fi
# Apply safe fixes
echo "Applying safe fixes ..."
ruff check --fix --config pyproject.toml
if [[ $? -ne 0 ]]; then
printf "\n🔴 Pre-commit hook failed while applying fixes.\n"
exit 1
fi
echo "Applying Ruff import sort ..."
ruff_output=$(ruff check --select I --fix . --config pyproject.toml 2>&1)
ruff_exit=$?
echo "$ruff_output"
if [[ $ruff_exit -ne 0 ]] || echo "$ruff_output" | grep -Ei "fixed|error" > /dev/null; then
echo "\n🔴 Pre-commit hook failed while applying import sort\n"
exit 1
fi
echo "Applying Ruff formatting ..."
ruff format --config pyproject.toml
if [[ $? -ne 0 ]]; then
printf "\n🔴 Pre-commit hook failed while applying formatting.\n"
exit 1
fi
printf "\n🟢 Pre-push checks passed !\n"
exit 0