Skip to content

Instantly share code, notes, and snippets.

@sdhull
Created June 6, 2012 21:30
Show Gist options
  • Save sdhull/2884926 to your computer and use it in GitHub Desktop.
Save sdhull/2884926 to your computer and use it in GitHub Desktop.
place in .git/hooks to get ctags workin
#!/usr/bin/env 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.
require 'rubygems'
require 'ruby-debug'
CTAGS = `which ctags`.chomp
HOOKS = %w{ post-merge post-commit post-rebase post-checkout }
HOOKS_DIR = '.git/hooks'
def install(force)
if !File.writable?(HOOKS_DIR)
$stderr.print "The install option [-i] can only be used within a git repo; exiting.\n"
exit 1
end
HOOKS.each { |hook| install_hook("#{HOOKS_DIR}/#{hook}", force) }
end
def run_tags(dir, run_in_background = false)
if File.executable?(CTAGS) and File.writable?(dir)
cmd = "find #{dir} -name \\\*.rb | #{CTAGS} -f #{dir}/tags -L - 2>>/dev/null "
cmd << '&' if run_in_background
$stderr.puts "Regenerating tags..."
system cmd
else
$stderr.print "FAILED to write TAGS file to #{dir}\n"
end
end
def install_hook(hook, force)
if File.exists?(hook) and not force
$stderr.print "A file already exists at #{hook}, and will NOT be replaced.\n"
return
end
print "Linking #{File.expand_path __FILE__} to #{hook}\n"
%x{ln -sfF #{File.expand_path __FILE__} #{hook}}
end
if ARGV.include? '-i'
force = ARGV.include? "-f"
install(force)
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