Skip to content

Instantly share code, notes, and snippets.

@s-mage
Last active December 21, 2015 17:09
Show Gist options
  • Save s-mage/6338987 to your computer and use it in GitHub Desktop.
Save s-mage/6338987 to your computer and use it in GitHub Desktop.
Set git hook that runs ctags automatically after each pool, commit and checkout. Got from https://gist.github.com/tobias/42308/raw/2e7b7072278a2ba65e9d95597e148498d9ba7654/run_tags.rb and refactore. Need to set it more flexible.
#!/usr/bin/env 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/bin/env ctags'
HOOKS = %w{ post-merge post-commit post-checkout }
HOOKS_DIR = '.git/hooks'
def install
unless 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}") }
end
def run_tags(dir, run_in_background = false)
return unless all_good?(dir)
find_files = "find #{dir} -name \\\*.rb"
generate_tags = "#{CTAGS} -f #{dir}/tags -L - 2>>/dev/null"
cmd = "#{find_files} | #{generate_tags}"
cmd << ' &' if run_in_background
$stderr.print "calling #{cmd}\n"
system cmd
end
def all_good?(dir)
unless File.writable?(dir)
$stderr.print "#{dir} file is not writable"
return false
end
return true
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
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