From 4354fcc0e81ca2e320a07dbeddeec03a2d6e44ae Mon Sep 17 00:00:00 2001 From: Nick Nisi Date: Tue, 23 Jul 2013 08:54:50 -0500 Subject: [PATCH] Add git templates with preconfigured hooks So far, add a single pre-commit hook which loops through the dotfiles for *.pre-commit files and executes them --- git/gitconfig.symlink | 2 ++ git/hooks/banned.pre-commit | 26 ++++++++++++++++++++++++++ git/hooks/jshint.pre-commit | 32 ++++++++++++++++++++++++++++++++ git/templates/hooks/pre-commit | 22 ++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100755 git/hooks/banned.pre-commit create mode 100755 git/hooks/jshint.pre-commit create mode 100755 git/templates/hooks/pre-commit diff --git a/git/gitconfig.symlink b/git/gitconfig.symlink index fd49cf6..70c4405 100644 --- a/git/gitconfig.symlink +++ b/git/gitconfig.symlink @@ -4,6 +4,8 @@ email = nick@nisi.org [github] user = nicknisi +[init] + templatedir = ~/.dotfiles/git/templates [alias] ci = commit -a co = checkout diff --git a/git/hooks/banned.pre-commit b/git/hooks/banned.pre-commit new file mode 100755 index 0000000..e91ed7f --- /dev/null +++ b/git/hooks/banned.pre-commit @@ -0,0 +1,26 @@ +#!/bin/zsh +# Banned Words +# If the file(s) being commited contain any of the below banned words +# then fail the commit + +COLOR_RED="\x1B[31m" +COLOR_GREEN="\x1B[32m" +COLOR_NONE="\x1B[0m" + +LIST="debugger\|console\|TODO\|FIXME\|BANNED" + +if `git rev-parse --verify HEAD >/dev/null 2>&1`; then + against=HEAD +else + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +for FILE in `git diff-index --name-status --cached $against -- | cut -c3-` ; do + # check if the fil contains one fo the words in the list + if grep -w $LIST $FILE; then + echo "" + echo "${COLOR_RED}✘ ${FILE} contains a banned word.${COLOR_NONE}" + exit 1 + fi +done +echo "${COLOR_GREEN}✔ No banned words detected${COLOR_NONE}" diff --git a/git/hooks/jshint.pre-commit b/git/hooks/jshint.pre-commit new file mode 100755 index 0000000..20a1faf --- /dev/null +++ b/git/hooks/jshint.pre-commit @@ -0,0 +1,32 @@ +#!/bin/sh +# JSHint Pre-Commit +# If a JavaScript file is trying to be committed and it fails linting +# then fail the commit + +EXIT_CODE=0 +COLOR_RED="\x1B[31m" +COLOR_GREEN="\x1B[32m" +COLOR_NONE="\x1B[0m" + +repo=$( git rev-parse --show-toplevel ) + +for file in $( exec git diff-index --cached --name-only HEAD ); do + if [[ $file == *".js"* ]]; then + status=$( exec git status --porcelain $file ) + + if [[ $status != D* ]]; then + # ${jshint} ${repo}/${file} >/dev/null 2>&1 + jshint ${repo}/${file} + EXIT_CODE=$((${EXIT_CODE} + $?)) + fi + fi +done + +echo "" +if [[ ${EXIT_CODE} -ne 0 ]]; then + echo "${COLOR_RED}✘ JSHINT detected syntax problems.${COLOR_NONE}" +else + echo "${COLOR_GREEN}✔ JSHINT detected no errors.${COLOR_NONE}" +fi + +exit $((${EXIT_CODE})) diff --git a/git/templates/hooks/pre-commit b/git/templates/hooks/pre-commit new file mode 100755 index 0000000..80fb830 --- /dev/null +++ b/git/templates/hooks/pre-commit @@ -0,0 +1,22 @@ +#!/bin/sh + +EXIT_CODE=0 + +repo=$( git rev-parse --show-toplevel ) +HOOKS=~/.dotfiles/git/hooks + +echo "Executing PRE-COMMIT hook(s)" + +for hook in $HOOKS/*.pre-commit; do + echo "" + echo "${COLOR_LIGHTPURPLE}Executing ${hook}${COLOR_NONE}" + ${hook} + EXIT_CODE=$((${EXIT_CODE} + $?)) +done + +if [[ ${EXIT_CODE} -ne 0 ]]; then + echo "" + echo "${COLOR_RED}Commit Failed.${COLOR_NONE}" +fi + +exit $((${EXIT_CODE}))