osx - Stop git commit in pre-commit hook with exit -
i'm trying stop git commit continuing pre-commit hook. reading manual, should stop when return exit code other 0. i'm testing see if csscomb command returns error, , if break loop , exit, git commit still continues on entering message via git commit (-a)
.
i've forked , modified script below https://github.com/filtercake/git-pre-commit-csscomb
#!/bin/bash # pre-commit hook comb staged .sass , .scss files # save .git/hooks/pre-commit # make executable: chmod +x .git/hooks/pre-commit # sources: # http://www.hagenburger.net/blog/using-git-commit-hooks-to-autocompile-sass-to-css.html # http://stackoverflow.com/questions/8470755/git-pre-commit-hook-add-file-into-index/8471009#8471009 # http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script/677212#677212 # https://gist.github.com/openam/8406343 # check if sass/scss files staged commit # diff-lines http://stackoverflow.com/a/12179492/3019532 # takes line function diff-lines() { local path= local line= while read; esc=$'\033' if [[ $reply =~ ---\ (a/)?.* ]]; continue elif [[ $reply =~ \+\+\+\ (b/)?([^[:blank:]$esc]+).* ]]; path=${bash_rematch[2]} elif [[ $reply =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; line=${bash_rematch[2]} elif [[ $reply =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; echo "line $line:$reply" if [[ ${bash_rematch[2]} != - ]]; ((line++)) fi fi done } if ! git diff --quiet --cached -- **/*.{sass,scss}; echo "" echo "--> checked in sass/scss files. lets comb them..." echo "" # check if csscomb installed if hash csscomb 2>/dev/null; echo -e "\033[1;32m--> found csscomb\033[0m" # todo: check csscomb update once week # check if configfile exists if [ ! -f ./.csscomb.json ]; echo "--> no config file, using defaults" else echo -e "\033[1;32m--> found .csscomb.json\033[0m" fi echo "" # necessary check initial commit against="4b825dc642cb6eb9a060e54bf8d69288fbee4904" git rev-parse --verify head >/dev/null 2>&1 && against="head" exitcode=0 # (a)dded (c)opied or (m)odified # encapsulate loop in {} http://stackoverflow.com/a/13665483/3019532 git diff-index --cached --full-index --diff-filter=acm $against | \ { while read -r line; file_path="$(echo ${line} |cut -d' ' -f6-)" extension="${file_path##*.}" # extension=${extension,,} # convert lowercase regex="" # select discouraged words based on extension if [ "${extension}" = "sass" ] || [ "${extension}" = "scss" ]; echo "--> staged sass/scss file: " $file_path echo "--> combing..." if csscomb $file_path; echo "--> adding combed file" git add $file_path echo "--> done" else echo "check css file combing errors or alternatively add '-n' git commit bypass hook" break exit 1 # should stop git commit continuing fi fi done } else echo -e "\033[0;31m--> oh noes, css comb not installed. do:" echo "--> npm install csscomb -g" echo -e "\033[0m" echo "--> (you need change , stage sass/scss file again see work on next commit...)" echo "" fi fi # necessary check initial commit against="4b825dc642cb6eb9a060e54bf8d69288fbee4904" git rev-parse --verify head >/dev/null 2>&1 && against="head" exitcode=0 exit 0
git diff-index --cached --full-index --diff-filter=acm $against | \ { while read -r line;
here's trouble. multi-command pipe stages run in subshells, , exit loop exits subshell. subshells have own environments, too, can't pass variable settings along them either.
while read -r; if ! $reply; exit 1; fi done <<eod $(git diff-index --cached --full-index --diff-filter=acm $against) eod
Comments
Post a Comment