From c9f57c7815ed07a834ccf4df9cc84aa19b961235 Mon Sep 17 00:00:00 2001 From: Josh Bodah Date: Thu, 21 May 2015 21:45:03 -0400 Subject: [PATCH 1/4] add install all CLI and Installer support --- lib/git_hooks/cli.rb | 9 ++++-- lib/git_hooks/hook_installer.rb | 49 +++++++++++++++++++++++++++++++++ lib/git_hooks/installer.rb | 42 ++++++---------------------- 3 files changed, 64 insertions(+), 36 deletions(-) create mode 100644 lib/git_hooks/hook_installer.rb diff --git a/lib/git_hooks/cli.rb b/lib/git_hooks/cli.rb index 0225540..2d74c0d 100644 --- a/lib/git_hooks/cli.rb +++ b/lib/git_hooks/cli.rb @@ -11,9 +11,14 @@ class CLI < Thor Install some hook: $ git_hooks install pre-commit + + Intall all hooks: + + $ git_hooks install LONGDESC - def install(hook) - GitHooks::Installer.new(hook, options[:ruby_path]) + def install(*hooks) + hooks = GitHooks::HOOKS if hooks.empty? + GitHooks::Installer.new(*hooks, ruby_path: options[:ruby_path]) .install(options[:force]) end diff --git a/lib/git_hooks/hook_installer.rb b/lib/git_hooks/hook_installer.rb new file mode 100644 index 0000000..ba7848b --- /dev/null +++ b/lib/git_hooks/hook_installer.rb @@ -0,0 +1,49 @@ +module GitHooks + class HookInstaller + HOOK_SAMPLE_FILE = 'hook.sample' + + def initialize(hook, ruby_path) + @hook = hook + @ruby_path = ruby_path + end + + def install(force = false) + throw Exceptions::UnknownHookPresent.new(hook) if !force && installed? + + hook_script = hook_template + hook_script.gsub!('/usr/bin/env ruby', ruby_path) if ruby_path + + puts "Writing to file #{hook_path}" + File + .open(hook_path, 'w') + .write(hook_script) + + FileUtils.chmod(0775, hook_path) + end + + def installed? + File.exist?(hook_path) && hook_file_contains_hook_command? + end + + private + + attr_accessor :hook, :ruby_path + + def hook_file_contains_hook_command? + expected = /GitHooks.execute_#{Regexp.escape(@hook.gsub('-', '_'))}s/ + File.read(hook_path).match(expected) + end + + def hook_template + File.read(hook_template_path) + end + + def hook_template_path + File.join(GitHooks.base_path, HOOK_SAMPLE_FILE) + end + + def hook_path + File.join('.git', 'hooks', hook) + end + end +end diff --git a/lib/git_hooks/installer.rb b/lib/git_hooks/installer.rb index 79e0632..878ecbe 100644 --- a/lib/git_hooks/installer.rb +++ b/lib/git_hooks/installer.rb @@ -1,45 +1,19 @@ +require_relative 'hook_installer' + module GitHooks class Installer - HOOK_SAMPLE_FILE = 'hook.sample' - - def initialize(hook, ruby_path = nil) - @hook = hook - @ruby_path = ruby_path + def initialize(*hooks, ruby_path: nil) + @installers = hooks.map do |hook| + GitHooks::HookInstaller.new(hook, ruby_path) + end end def install(force = false) - throw Exceptions::UnknownHookPresent.new(hook) if !force && installed? - - hook_script = hook_template - hook_script.gsub!('/usr/bin/env ruby', ruby_path) if ruby_path - - puts "Writing to file #{hook_path}" - File - .open(hook_path, 'w') - .write(hook_script) - - FileUtils.chmod(0775, hook_path) + @installers.each { |i| i.install(force) } end def installed? - File.exist?(hook_path) && - File.read(hook_path).match(/GitHooks.execute_pre_commits/) - end - - private - - attr_accessor :hook, :ruby_path - - def hook_template - File.read(hook_template_path) - end - - def hook_template_path - File.join(GitHooks.base_path, HOOK_SAMPLE_FILE) - end - - def hook_path - File.join('.git', 'hooks', hook) + @installers.all?(&:installed?) end end end From 1805cbee731b8216d8c595524229ca6afa52aa81 Mon Sep 17 00:00:00 2001 From: Josh Bodah Date: Thu, 21 May 2015 22:03:13 -0400 Subject: [PATCH 2/4] remove unused GitHooks.real_hook_template_path --- lib/git-hooks.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/git-hooks.rb b/lib/git-hooks.rb index 889bb9f..e3b24fd 100644 --- a/lib/git-hooks.rb +++ b/lib/git-hooks.rb @@ -17,7 +17,6 @@ require_relative 'git_hooks/pre_commit' module GitHooks - HOOK_SAMPLE_FILE = 'hook.sample' HOOKS = [PRE_COMMIT = 'pre-commit'] class << self @@ -49,10 +48,6 @@ def valid_pre_commit_hook? configurations.pre_commits.empty? || Installer.new(PRE_COMMIT).installed? end - - def real_hook_template_path - File.join(base_path, HOOK_SAMPLE_FILE) - end end end From cbb9b75cb765ae87a2bf82c9a373687c8543c1a2 Mon Sep 17 00:00:00 2001 From: Josh Bodah Date: Thu, 21 May 2015 22:08:44 -0400 Subject: [PATCH 3/4] variablize hook.sample for other hooks --- hook.sample | 6 ------ hook.sample.erb | 6 ++++++ lib/git_hooks/hook_installer.rb | 12 +++++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) delete mode 100755 hook.sample create mode 100755 hook.sample.erb diff --git a/hook.sample b/hook.sample deleted file mode 100755 index 7c1e1b7..0000000 --- a/hook.sample +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby -require 'git-hooks' - -puts 'Running pre-commit hooks...' - -GitHooks.execute_pre_commits diff --git a/hook.sample.erb b/hook.sample.erb new file mode 100755 index 0000000..340d1ed --- /dev/null +++ b/hook.sample.erb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby +require 'git-hooks' + +puts 'Running <%= hook %> hooks...' + +<%= run_hook_command %> diff --git a/lib/git_hooks/hook_installer.rb b/lib/git_hooks/hook_installer.rb index ba7848b..0ea8cfe 100644 --- a/lib/git_hooks/hook_installer.rb +++ b/lib/git_hooks/hook_installer.rb @@ -1,6 +1,8 @@ +require 'erb' + module GitHooks class HookInstaller - HOOK_SAMPLE_FILE = 'hook.sample' + HOOK_SAMPLE_FILE = 'hook.sample.erb' def initialize(hook, ruby_path) @hook = hook @@ -10,7 +12,7 @@ def initialize(hook, ruby_path) def install(force = false) throw Exceptions::UnknownHookPresent.new(hook) if !force && installed? - hook_script = hook_template + hook_script = ERB.new(hook_template).result(binding) hook_script.gsub!('/usr/bin/env ruby', ruby_path) if ruby_path puts "Writing to file #{hook_path}" @@ -29,8 +31,12 @@ def installed? attr_accessor :hook, :ruby_path + def run_hook_command + "GitHooks.execute_#{@hook.gsub('-', '_')}s" + end + def hook_file_contains_hook_command? - expected = /GitHooks.execute_#{Regexp.escape(@hook.gsub('-', '_'))}s/ + expected = /#{Regexp.escape(run_hook_command)}/ File.read(hook_path).match(expected) end From 174372f078054ad1e3dbd516c2bea3a2d9031819 Mon Sep 17 00:00:00 2001 From: Josh Bodah Date: Thu, 21 May 2015 22:19:19 -0400 Subject: [PATCH 4/4] refactor ruby_path substitution into ERB template --- hook.sample.erb | 2 +- lib/git_hooks/hook_installer.rb | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/hook.sample.erb b/hook.sample.erb index 340d1ed..e61891f 100755 --- a/hook.sample.erb +++ b/hook.sample.erb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +#!<%= ruby_path || '/usr/bin/env ruby' %> require 'git-hooks' puts 'Running <%= hook %> hooks...' diff --git a/lib/git_hooks/hook_installer.rb b/lib/git_hooks/hook_installer.rb index 0ea8cfe..114f0b9 100644 --- a/lib/git_hooks/hook_installer.rb +++ b/lib/git_hooks/hook_installer.rb @@ -13,7 +13,6 @@ def install(force = false) throw Exceptions::UnknownHookPresent.new(hook) if !force && installed? hook_script = ERB.new(hook_template).result(binding) - hook_script.gsub!('/usr/bin/env ruby', ruby_path) if ruby_path puts "Writing to file #{hook_path}" File