Skip to content

Instantly share code, notes, and snippets.

@srushti
Last active December 31, 2015 17:28
Show Gist options
  • Save srushti/8019977 to your computer and use it in GitHub Desktop.
Save srushti/8019977 to your computer and use it in GitHub Desktop.
Git hooks
#!/usr/bin/ruby
#-*-ruby-*-
# A script to run takes git shots on every commit. Can be run on
# the current dir, called from a git callback, or install itself as a
# git post-commit callback.
HOOKS = %w{post-commit}
HOOKS_DIR = '.git/hooks'
def ensure_git_repo
if !File.writable?(HOOKS_DIR)
$stderr.print "The install option [-i] can only be used within a git repo; exiting.\n"
exit 1
end
end
def install
ensure_git_repo
HOOKS.each { |hook| install_hook("#{HOOKS_DIR}/#{hook}") }
end
def reinstall
ensure_git_repo
HOOKS.each { |hook| delete_hook("#{HOOKS_DIR}/#{hook}") }
install
end
def take_shot(dir, run_in_background = false)
file="~/.gitshots/#{Time.now.strftime('%Y%m%d%H%M%S')}.jpg"
unless File.directory?(File.expand_path("../../rebase-merge", __FILE__))
puts "Taking capture into #{file}!"
system "imagesnap -q -w 3 #{file} &"
end
end
def delete_hook(hook)
File.delete(hook) if File.exists?(hook)
end
def install_hook(hook)
if File.exists?(hook)
$stderr.print "A file already exists at #{hook}, and will NOT be replaced.\n"
return
end
print "Linking #{__FILE__} to #{hook}\n"
%x{ln -s #{__FILE__} #{hook}}
end
if ARGV.first == '-i'
install
elsif ARGV.first == '-r'
reinstall
else
take_shot Dir.pwd, HOOKS.include?(File.basename(__FILE__))
end
tag_hook.rb -i
git_shots_hook.rb -i
#!/usr/bin/ruby
#-*-ruby-*-
# A script to run ctags on all .rb files in a project. Can be run on
# the current dir, called from a git callback, or install itself as a
# git post-merge and post-commit callback.
CTAGS = '/usr/local/Cellar/ctags/5.8/bin/ctags'
HOOKS = %w{post-merge post-checkout}
HOOKS_DIR = '.git/hooks'
def ensure_git_repo
if !File.writable?(HOOKS_DIR)
$stderr.print "The install option [-i] can only be used within a git repo; exiting.\n"
exit 1
end
end
def install
ensure_git_repo
HOOKS.each { |hook| install_hook("#{HOOKS_DIR}/#{hook}") }
end
def reinstall
ensure_git_repo
HOOKS.each { |hook| delete_hook("#{HOOKS_DIR}/#{hook}") }
install
end
def run_tags(dir, run_in_background = false)
if File.executable?(CTAGS) and File.writable?(dir)
extensions = ['rb', 'java', 'cs', 'js', 'haml', 'erb', 'm', 'h', 'coffee']
if File.exists?('Gemfile')
ctags_command = "bundle list --paths=true | xargs #{CTAGS} -f #{dir}/tags --recurse=yes #{dir}--extra=+f --exclude=.git --exclude=public --exclude=tmp --exclude=*.js --exclude=log -R * 2>> /dev/null"
else
ctags_command = "#{CTAGS} -f #{dir}/tags --recurse=yes #{dir} 2>> /dev/null"
end
cmd = "#{ctags_command} && find #{dir} #{extensions.map{|ext| " -name '*.#{ext}'"}.join(' -o ')} >| cscope.files && cscope -b -q"
cmd << ' &' if run_in_background
$stderr.print "refreshed tags\n"
#$stderr.print "calling #{cmd}\n"
system cmd
else
$stderr.print "FAILED to write TAGS file to #{dir}\n"
end
end
def delete_hook(hook)
File.delete(hook) if File.exists?(hook)
end
def install_hook(hook)
if File.exists?(hook)
$stderr.print "A file already exists at #{hook}, and will NOT be replaced.\n"
return
end
print "Linking #{__FILE__} to #{hook}\n"
%x{ln -s #{__FILE__} #{hook}}
end
if ARGV.first == '-i'
install
elsif ARGV.first == '-r'
reinstall
else
run_tags Dir.pwd, HOOKS.include?(File.basename(__FILE__))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment