Skip to content

Instantly share code, notes, and snippets.

@ViBiOh
Last active May 2, 2018 20:18
Show Gist options
  • Save ViBiOh/c134b36596a576ba50dd4b9644a3d09b to your computer and use it in GitHub Desktop.
Save ViBiOh/c134b36596a576ba50dd4b9644a3d09b to your computer and use it in GitHub Desktop.
GitHook
#!/usr/bin/env bash
diffInitial=`git diff --name-only --diff-filter=ACMR`
BLUE='\033[0;34m'
RED='\033[0;31m'
RESET='\033[0m'
package=`git diff --cached --name-only --diff-filter=ACMR | grep -E "package.json$"`
js=`git diff --cached --name-only --diff-filter=ACMR | grep -E "jsx?$"`
pass=0
function checkInstall() {
echo -e "${BLUE}Package.json has changed, running install${RESET}"
if command -v npm > /dev/null 2>&1; then
npm --silent install
pass=$?
else
echo -e "${RED}npm not found in $PATH, check status of ${BLUE}package-lock.json{RESET}"
fi
}
function checkDependencies() {
if [ ! -d "node_modules" ]; then
checkInstall
fi
}
function checkFormat() {
echo -e "${BLUE}JS/JSX has changed, formatting files${RESET}"
checkDependencies
npm run --silent format:file ${1}
pass=$?
}
function checkEsLint() {
echo -e "${BLUE}JS/JSX has changed, linting files${RESET}"
checkDependencies
npm run --silent lint:file ${1}
pass=$?
}
if [ "${package}" != "" ]; then
checkInstall
fi
if [ "${pass}" == "0" ] && [ "${js}" != "" ]; then
checkFormat ${js}
fi
if [ "${pass}" == "0" ] && [ "${js}" != "" ]; then
checkEsLint ${js}
fi
if [ ! "${pass}" == "0" ]; then
echo -e "${RED}COMMIT FAILED${RESET}"
echo -e "${RED}Last check failed during pre-commit. Please fix errors and try committing again.${RESET}"
exit 1
fi
diffFinal=`git diff --name-only --diff-filter=ACMR`
if [ ! "$diffInitial" == "$diffFinal" ]; then
echo -e "${RED}Pre-commit has changes files${RESET}"
echo -e "${RED}Consider adding updated files with ${BLUE}git add -i \&\& git commit --amend${RESET}"
fi
exit 0
#!/usr/bin/env bash
BRANCH_NAME=`git symbolic-ref --short HEAD`
if [[ -n "${BRANCH_NAME}" && "${BRANCH_NAME}" =~ (features|fixes)/([A-Z]+[-_][0-9A-Z]+) ]]; then
PREFIX="${BASH_REMATCH[${#BASH_REMATCH[@]}-1]}"
if [[ ! `cat ${1}` =~ ^${PREFIX} ]]; then
sed -i.bak -e "1s/^/$PREFIX /" "${1}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment