Last active
August 29, 2015 14:05
-
-
Save proapi/d4066e1b119378a71a8f to your computer and use it in GitHub Desktop.
Git pre-push hook to check against debug functions in code #git #bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[init] | |
templatedir = ~/.git-templates | |
[color] | |
ui = always | |
[grep] | |
extendRegexp = true | |
lineNumber = true | |
fullName = true | |
[color "grep"] | |
filename = green |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Enable git templates: | |
git config --global init.templatedir '~/.git-templates' | |
This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init | |
2. Create a directory to hold the global hooks: | |
mkdir -p ~/.git-templates/hooks | |
3. Write your hooks in ~/.git-templates/hooks | |
4. Make sure the hook is executable. | |
chmod a+x ~/.git-templates/hooks/pre-push | |
5. Re-initialize git in each existing repo you'd like to use this in: | |
git init | |
6. skip the hook by adding --no-verify(short -n) to the git commit command | |
NOTE if you already have a hook defined in your local git repo, this will not overwrite it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Copy your script to /path/to/your/project/.git/hooks | |
2. Remember to keep the name of hook consistent(eg. pre-commit, pre-push, post-commit) - no file extension | |
3. Remember to chmod a+x on a hook to be executable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
cancel_on_error = 1 # defines if finding anything will cancel the commit | |
result = "" | |
%w{console.log binding.pry dupa byebug >>>>>>> <<<<<<<}.each do |regexp| | |
diff = "git diff --name-only --cached -G \"#{regexp}\"" | |
grep = "git grep -i --break --heading -e \"#{regexp}\"" | |
result += `#{diff} | xargs -I files #{grep} -- \"files\"` | |
end | |
unless result.empty? | |
puts "\e[31mThere is some debug/test code in the codebase! Please remove it before commit!\e[0m" | |
puts result | |
exit cancel_on_error | |
end | |
puts "\e[32mCheck against debug/test code OK!\e[0m" | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Check for debugging statements before pushing your code. | |
# | |
# List of function names to search for in regex format | |
FUNCTIONS="binding\.pry|console\.log|dupa" | |
# If any functions are found as executable, prevent the push. | |
EXITONFAIL=true | |
echo "Running the check against debugger functions in your code..." | |
CMD="grep -Enr --exclude=\*.{png,jpg,jpeg,gif} --exclude-dir={.git,tmp,log,public} \"$FUNCTIONS\"" | |
echo "$CMD" | |
if [[ -n "$CMD" ]]; then | |
echo "Debugging functions were found in your code!" | |
eval "$CMD" | |
if [[ $EXITONFAIL == true ]]; then | |
echo "Changes were not pushed." | |
exit 1; | |
else | |
echo "You may want to clean these up. Changes were pushed anyway." | |
exit 0; | |
fi | |
else | |
echo "No debugging functions were found. Nice job, Ace!"; | |
exit 0; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to refactor the script to ruby