#!/usr/bin/env bash # -------------------------------------------------------------# # Run many checks before commit # # -------------------------------------------------------------# # Sergio Vaccaro # # Preferences # -------------------------------------------------------------- PREVENT_COMMIT_IN_MASTER_BRANCH=1 QA=1 # -------------------------------------------------------------- # Redirect output to stderr. exec 1>&2 # Prevent commits in master branch # ================================ if [[ 0 -ne "$PREVENT_COMMIT_IN_MASTER_BRANCH" ]]; then # See https://stackoverflow.com/questions/40462111/git-prevent-commits-in-master-branch branch="$(git rev-parse --abbrev-ref HEAD)" if [ "$branch" = "master" ]; then echo echo "[BEST PRATICE] You can't commit directly to master branch." echo echo " You can quickly change to a new branch with the command" echo " git stash && git stash branch newBranch" echo " or to an existing branch with command" echo " git stash && git checkout existingBranch && git stash pop." echo exit 1 fi fi # Quality assurance # ================= if [[ 0 -ne "$QA" ]]; then source .git/hooks/qa #run checks echo "Running QA checks" qa $(git diff HEAD --name-only --diff-filter=dr | grep \.php$) EXIT=$? if [ "$EXIT" -ne 0 ]; then echo 'Commit unsuccessful because quality errors. Please fix them.' exit $EXIT fi fi exit 0