Git Ruby Combo - hooks and preventing checkins

Here are two example from the article below Slaying dragons with git, bash, and ruby a on pre-commit hook for git.

First setup your hook that will run before you commit.

chmod +x .git/hooks/pre-commit

Second test out a test for ruby debugger code left, when it should be removed

#!/usr/bin/env ruby
if `grep -rls "require 'ruby-debug'; debugger" *` != ""
  puts "You twit, you've left a debugger in!"
Now whenever I try to commit code, it will first run a recursive grep over the codebase to ensure I’ve not left my debug statement in (I can be sure it always looks like “require ‘ruby-debug’; debugger” as I have it bound to a shortcut).
Another example included

Stopping an incomplete merge

There’s been occasions where a particularly large rebase or merge creates a lot of conflicts in a file, and one of those has snuck through and rather than being fixed the inline diff has actually been committed. Time to add another check to pre-commit, using egrep to scan recursively for the 3 different line markers that git uses to indicate a merge conflict:
#!/usr/bin/env ruby

if `egrep -rls "^<<<<<<< |^>>>>>>> |^=======$" *`
  puts "Dang, looks like you screwed the merge!"


Popular posts from this blog

Vim vi how to reload a file your editing