Created
May 17, 2017 13:07
-
-
Save carloswherbet/ee059e1eaa15d826d58ec5f8c9f52173 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# This pre-commit hook will prevent any commits to forbidden branches | |
# It will also help prevent you leaving debuggers and merge conflig flags | |
# Put this file in your local repo, in the .git/hooks folder | |
# and make sure it is executable by running: chmod u+x pre-commit | |
# The name of the file *must* be "pre-commit" for Git to pick it up. | |
class String | |
def black; "\e[30m#{self}\e[0m" end | |
def red; "\e[31m#{self}\e[0m" end | |
def green; "\e[32m#{self}\e[0m" end | |
def brown; "\e[33m#{self}\e[0m" end | |
def blue; "\e[34m#{self}\e[0m" end | |
def magenta; "\e[35m#{self}\e[0m" end | |
def cyan; "\e[36m#{self}\e[0m" end | |
def gray; "\e[37m#{self}\e[0m" end | |
def bg_black; "\e[40m#{self}\e[0m" end | |
def bg_red; "\e[41m#{self}\e[0m" end | |
def bg_green; "\e[42m#{self}\e[0m" end | |
def bg_brown; "\e[43m#{self}\e[0m" end | |
def bg_blue; "\e[44m#{self}\e[0m" end | |
def bg_magenta; "\e[45m#{self}\e[0m" end | |
def bg_cyan; "\e[46m#{self}\e[0m" end | |
def bg_gray; "\e[47m#{self}\e[0m" end | |
def bold; "\e[1m#{self}\e[22m" end | |
def italic; "\e[3m#{self}\e[23m" end | |
def underline; "\e[4m#{self}\e[24m" end | |
def blink; "\e[5m#{self}\e[25m" end | |
def reverse_color; "\e[7m#{self}\e[27m" end | |
end | |
error_found = false | |
FORBIDDEN_BRANCHES = ["master", "staging", "production"] | |
FORBIDDEN_CODE = [ | |
/TMP_DEBUG/, # My TextExpander macros for embedding debug code always include this for easy scanning. | |
/>>>>>>/, # Git conflict markers | |
/<<<<<</, # '' | |
/======/, # '' | |
/console\.debug/, # JavaScript debug code that would break IE. | |
/console\.log/, # '' | |
/binding\.pry/, # pry debugging code | |
/binding\.remote_pry/, # '' | |
/save_and_open_page/, # Launchy debugging code | |
/debugger/, # Ruby debugging code | |
/focus:\s*true/ | |
] | |
branch = `git rev-parse --abbrev-ref=strict HEAD`.chomp | |
#if FORBIDDEN_BRANCHES.include?(branch) | |
# puts | |
# puts " HOLD UP!" | |
# puts " You are trying to commit directly to *#{branch}* branch." | |
# puts " (>_<)" | |
# error_found = true | |
#end | |
full_diff = `git diff --cached --` | |
full_diff.scan(%r{^\+\+\+ b/(.+)\n@@.*\n([\s\S]*?)(?:^diff|\z)}).each do |file, diff| | |
added = diff.split("\n").select { |x| x.start_with?("+") }.join("\n") | |
# Scan for "forbidden" calls | |
FORBIDDEN_CODE.each do |re| | |
if added.match(re) | |
puts "" | |
puts "" | |
puts %{Error: git pre-commit hook forbids committing lines with "#{($1 || $&).bg_red.gray.blink} to #{file.to_ | |
s.bold}\n--------------}.brown | |
error_found = true | |
end | |
end | |
end | |
if error_found | |
puts | |
# puts " You can force the commit by adding --no-verify to the command." | |
puts " " | |
puts " " | |
puts | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment