Created
December 2, 2011 10:34
-
-
Save timnovinger/1422741 to your computer and use it in GitHub Desktop.
Setup Happy Git Commits
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
# http://collectiveidea.com/blog/archives/2010/08/03/happy-git-commits/ | |
# | |
# This makes adding a happy post commit to a project easy, and simplifies | |
# changing the sound file periodically. Simply re-run with a different | |
# sound file reference. | |
# | |
# Defaults to the "happykids.wav" file referenced in the blog post above ^ | |
# | |
# Example usage from within ~/.dotfiles | |
# - rake add_happy_commits[/Users/tnovinger/code/my_project, vuvuzelas.mp3] | |
# | |
# Tim Novinger | @timnovinger | http://www.timnovinger.com | |
module Dotfiles | |
def self.run(cmd) | |
puts "Running: #{cmd}" | |
system cmd | |
end | |
end | |
desc "Setup Happy Git Commits" | |
task :add_happy_commits, :directory, :sound do |t, args| | |
directory = args[:directory] | |
sound = args[:sound] || 'happykids.wav' | |
command = "afplay ~/.gitsounds/the_sound > /dev/null 2>&1 &" | |
puts "-- Patching #{directory} with #{sound}" | |
unless directory | |
puts "Error: You need to include a directory argument" | |
exit | |
end | |
post_commit_hook_file = "#{directory}/.git/hooks/post-commit" | |
post_commit_hook_file_sample = "#{directory}/.git/hooks/post-commit.sample" | |
# Make sure we're dealing with a Git project | |
if !File.exists?(post_commit_hook_file_sample) && !File.exists?(post_commit_hook_file) | |
puts "Error: #{directory} is not a Git project!" | |
exit | |
end | |
# ---------------------------- | |
# Symbolically link the sounds | |
# into the user's home folder | |
# ---------------------------- | |
puts "-- Copying the sounds into your home directory" | |
Dotfiles.run "mkdir ~/.gitsounds" | |
Dotfiles.run "ln -sf ~/.dotfiles/sounds/#{sound} ~/.gitsounds/the_sound" | |
unless File.exists?("#{ENV["HOME"]}/.dotfiles/sounds/#{sound}") | |
puts "Error: Can't find a sound named \"#{ENV["HOME"]}/.dotfiles/sounds/#{sound}\"" | |
exit | |
end | |
# ---------------------------------------------- | |
# If a post-commit hook is already setup use it, | |
# if not, then copy the sample over | |
# ---------------------------------------------- | |
if File.exists?(post_commit_hook_file_sample) && !File.exists?(post_commit_hook_file) | |
Dotfiles.run "cp #{post_commit_hook_file_sample} #{post_commit_hook_file}" | |
end | |
# Prevent patching the file more than once | |
File.open(post_commit_hook_file).each do |line| | |
puts "-- Done!" && exit if line =~ /afplay/ | |
end | |
# ------------------------------------------------------- | |
# Append the sound command onto the post-commit hook file | |
# -- this should not overwrite existing file contents | |
# ------------------------------------------------------- | |
puts "-- Patching the project's post-commit hook" | |
Dotfiles.run "echo '#{command}' | cat #{post_commit_hook_file} - > /tmp/post-commit" | |
Dotfiles.run "mv /tmp/post-commit #{post_commit_hook_file} && rm -f /tmp/post-commit" | |
Dotfiles.run "chmod 755 #{post_commit_hook_file}" | |
puts "-- Done!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment