Git - checking JavaScript files for debugger statements
We developers, when working on a common codebase, often step on each other's toes. Recently, while debugging code of a complex application server I have been hit with JavaScript debugger statements placed by another developer all around, that should not get to the common branch. So what is better than complaining to your colleagues to be more careful every time they commit code? Present them with a Git hook that does the checking for them and solves the problem once and for all :-) This hook is an expansion of the previous one.
The previous hook only checked the branch name against a pattern to avoid unintended commits to stable branches. This hook gets a list of files in a commit, picks only .js files and searches them for "debugger" string. If such files are found, the script lists them and asks what to do:
commit anyway
cancel the commit
commit and remove "debugger" statements
I avoid breaking someone else's workflows as far as possible so there is still an option to just commit the code. I could also implement a server-side pre-push hook that would do the checking and simply reject unwanted stuff but that would force the developers to reset their branches and do the cleaning themselves. I think that problems are best dealt with as early as possible so this hook is executed in the pre-commit phase.
The last option is especially useful, because it saves the time needed to walk through all the files and clean code manually. Replacing the "debugger" string with a semicolon is probably the safest option. As a side-effect the script will also do replacements in comments but that should not be a big problem.
#!/bin/bash# This hooks prevents unintentional committing to a branch.# Customize PATTERN to suit your needs. It also checks JavaScript# files for debugger statements and can replace them with semicolons.## Place this file in .git/hooks/pre-commit and make executable.PATTERN="stable"BRANCH=`gitrev-parse--abbrev-refHEAD`functionconfirm{#first argument - messagePS3=$1options=("Yes""No")selectoptin"${options[@]}"docase$optin${options[0]})break;;${options[1]})exit1;;*)echoinvalidoption;;esacdone</dev/tty
}functionclean_js_files{JS_FILES=`gitdiff--cached--name-only|grep.js`DEBUGGER_STATEMENT_PRESENT=0echoecho"The following lines will be modified:"forFILEin$JS_FILES;dogrep--color=auto-Hn'debugger'$FILEdoneconfirm"Are you sure? "#script will exit if not sureforFILEin$JS_FILES;dosed--in-place's/debugger/;/'$FILEdone}functionconfirm_js_files{PS3='Do you really want to commit?: 'options=("Yes""No""Remove debugger statements (substitute with semicolons)")selectoptin"${options[@]}"docase$optin${options[0]})break;;${options[1]})exit1;;${options[2]})clean_js_files
break;;*)echoinvalidoption;;esacdone</dev/tty
}functioncheck_js_files_for_debugger{JS_FILES=`gitdiff--cached--name-only|grep.js`DEBUGGER_STATEMENT_PRESENT=0forFILEin$JS_FILES;dogrep-Hn'debugger'$FILESTATUS=$?if[$STATUS-eq0];thenDEBUGGER_STATEMENT_PRESENT=1fidonereturn"$DEBUGGER_STATEMENT_PRESENT"}if[[$BRANCH=*$PATTERN*]];thenecho"----------------------------------------------------------------"echo"WARNING: You are committing to stable branch:"$BRANCHecho"----------------------------------------------------------------"confirm"Do you really want to commit? "fi
check_js_files_for_debugger$JS_FILESA=$?#exit code of previous function callif[$A-eq1];thenecho"----------------------------------------------------------------"echo"WARNING: Debugger statements detected in JavaScript files."echo"----------------------------------------------------------------"confirm_js_files
fiexit0