Skip to content

Instantly share code, notes, and snippets.

@chesterbr
Created February 28, 2013 20:29
Show Gist options
  • Save chesterbr/5059850 to your computer and use it in GitHub Desktop.
Save chesterbr/5059850 to your computer and use it in GitHub Desktop.
Quick, hack-y script to add lolcommits hooks to all repos in a dir.
#!/usr/bin/env ruby
#
# lolcommitify.rb - adds the post-commit hook of https://github.com/mroth/lolcommits
# to every single git repository on a directory, recursively
#
# Copyright 2013 Chester ([email protected]), BSD-style copyright and disclaimer apply
# (http://opensource.org/licenses/bsd-license.php)
require 'fileutils'
abort "Usage: #{__FILE__} dirname" if ARGV.count != 1
HASHBANG = "#!/bin/sh\n"
LOL_HOOK = "lolcommits -c\n"
Dir.chdir ARGV[0]
puts "Scanning directories under #{Dir.pwd}..."
Dir.glob('**/\.git').each do |gitdir|
next unless Dir.exists? gitdir
name = "#{gitdir}/hooks/post-commit"
print "Checking #{ARGV[0]}/#{name}... "
contents = File.exists?(name) ? File.readlines(name) : []
before = contents.clone
contents.unshift HASHBANG unless contents.any? && contents[0].start_with?('#!')
contents << LOL_HOOK unless contents.grep(/lolcommits/).any?
new_lines = contents - before
if new_lines.any?
puts "Adding lines:\n #{new_lines.join ' '}"
FileUtils.makedirs File.dirname(name), :noop => true
File.open(name, 'w') { |f| f.puts contents }
else
puts "no action needed."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment