Module: GitTools::Hooks
- Defined in:
- lib/git_tools/hooks.rb
Constant Summary collapse
- GIT_HOOK_INSTALL_LINE_BEGIN =
"# BEGIN Ruby git-hooks\n"
- GIT_HOOK_INSTALL_LINE_END =
"# END Ruby git-hooks\n"
- GIT_HOOK_DIR =
File.join('.git', 'hooks')
- GIT_TOOLS_CUSTOM_HOOKS_DIR =
File.join(CUSTOM_DIR, 'hooks')
- GIT_TOOLS_INCLUDED_HOOKS_DIR =
File.join(File.dirname(__FILE__), 'hooks')
Class Method Summary collapse
- .clear_git_hooks ⇒ Object
- .custom_ruby_hooks ⇒ Object
- .default_ruby_hooks ⇒ Object
- .install_git_hooks ⇒ Object
- .with_git_hook_files ⇒ Object
Class Method Details
.clear_git_hooks ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/git_tools/hooks.rb', line 34 def self.clear_git_hooks with_git_hook_files do |dir, ruby_hook, git_hook| if File.exists?(git_hook) hook_content = File.read(git_hook) if hook_content.match(/#{GIT_HOOK_INSTALL_LINE_BEGIN}/) puts "Clearing Ruby #{ruby_hook} git-hooks." hook_content.gsub!(/#{GIT_HOOK_INSTALL_LINE_BEGIN}.*#{GIT_HOOK_INSTALL_LINE_END}/m, '') File.open(git_hook, 'w+') do |file| file.write(hook_content) end end end end end |
.custom_ruby_hooks ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/git_tools/hooks.rb', line 26 def self.custom_ruby_hooks if Dir.exists?(GIT_TOOLS_CUSTOM_HOOKS_DIR) {GIT_TOOLS_CUSTOM_HOOKS_DIR => (Dir.entries(GIT_TOOLS_CUSTOM_HOOKS_DIR) - ['.', '..'])} else {} end end |
.default_ruby_hooks ⇒ Object
22 23 24 |
# File 'lib/git_tools/hooks.rb', line 22 def self.default_ruby_hooks {GIT_TOOLS_INCLUDED_HOOKS_DIR => (Dir.entries(GIT_TOOLS_INCLUDED_HOOKS_DIR) - ['.', '..'])} end |
.install_git_hooks ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/git_tools/hooks.rb', line 49 def self.install_git_hooks with_git_hook_files do |ruby_hook_dir, ruby_hook_file, git_hook| if File.exists?(git_hook) hook_content = File.read(git_hook) else hook_content = "#!/bin/sh\n\n" end if hook_content.match(/#{GIT_HOOK_INSTALL_LINE_BEGIN}/) next else puts "Installing Ruby #{ruby_hook_file} git-hooks." hook_commands = '' hook_files = File.join(ruby_hook_dir, ruby_hook_file) puts "Hook file: #{hook_files}" if $VERBOSE Dir.foreach(hook_files) do |file_path| if file_path.match(/\.rb$/) hook_commands += "if [ $? -eq 0 ]; then ruby #{File.join(ruby_hook_dir, ruby_hook_file, file_path)} \"$@\"; else exit 1; fi\n" end end hook_content += "#{GIT_HOOK_INSTALL_LINE_BEGIN}\n#{hook_commands}\n#{GIT_HOOK_INSTALL_LINE_END}" File.open(git_hook, 'w+') do |file| file.write(hook_content) end FileUtils.chmod(0744, git_hook) end end end |
.with_git_hook_files ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/git_tools/hooks.rb', line 9 def self.with_git_hook_files if File.exist?(GIT_HOOK_DIR) default_ruby_hooks.merge(custom_ruby_hooks).each do |dir, files| files.each do |file| git_hook = File.join(GIT_HOOK_DIR, file) yield(dir, file, git_hook) end end else puts "Git hook directory not found." end end |