Some really good hook scripts for git

(written by Lawrence Krubner, however indented passages are often quotes)

For the last few years we have been using Subversion (via Springloops) for version control. I’ve been thinking of switching to git. I wasn’t sure if the advantages were great enough to be worth the hassle of learning a new technology. However, this collection of hook scripts has convinced me.

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!”
exit(1)
end

If you try this though you’ll probably discover that it doesn’t quite work as expected, because there are some binary files that happen to include these characters. More shell scripting to the rescue then, we will pipe the results into a couple of other commands to filter it out. First it goes via xargs to allow us to take the input from STDIN and pass each line recursively into file to find out what type of file we are dealing with. We then pipe that into egrep again to select only the script and text files:

#!/usr/bin/env ruby

if `egrep -rls “^<<<<<<< |^>>>>>>> |^=======$” * | xargs file | egrep ‘script|text’` != “”
puts “Dang, looks like you screwed the merge!”
exit(1)
end

I saw this via Hacker News

Source