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..e61891f --- /dev/null +++ b/hook.sample.erb @@ -0,0 +1,6 @@ +#!<%= ruby_path || '/usr/bin/env ruby' %> +require 'git-hooks' + +puts 'Running <%= hook %> hooks...' + +<%= run_hook_command %> 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 diff --git a/lib/git_hooks/cli.rb b/lib/git_hooks/cli.rb index 2e97005..6d05787 100644 --- a/lib/git_hooks/cli.rb +++ b/lib/git_hooks/cli.rb @@ -12,10 +12,15 @@ class CLI < Thor Install some hook: $ git_hooks install pre-commit + + Intall all hooks: + + $ git_hooks install LONGDESC - def install(hook) + def install(*hooks) + hooks = GitHooks::HOOKS if hooks.empty? installer = GitHooks::Installer.new( - hook, + *hooks, ruby_path: options[:ruby_path], logger: logger ) diff --git a/lib/git_hooks/hook_installer.rb b/lib/git_hooks/hook_installer.rb new file mode 100644 index 0000000..8626a01 --- /dev/null +++ b/lib/git_hooks/hook_installer.rb @@ -0,0 +1,56 @@ +require 'erb' +require 'logger' + +module GitHooks + class HookInstaller + HOOK_SAMPLE_FILE = 'hook.sample.erb' + + def initialize(hook, ruby_path: nil, logger: nil) + @hook = hook + @ruby_path = ruby_path + @logger = logger || Logger.new('/dev/null') + end + + def install(force = false) + throw Exceptions::UnknownHookPresent.new(hook) if !force && installed? + + hook_script = ERB.new(hook_template).result(binding) + + logger.info("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, :logger + + def run_hook_command + "GitHooks.execute_#{@hook.gsub('-', '_')}s" + end + + def hook_file_contains_hook_command? + expected = /#{Regexp.escape(run_hook_command)}/ + 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 b683795..f8e8c05 100644 --- a/lib/git_hooks/installer.rb +++ b/lib/git_hooks/installer.rb @@ -1,48 +1,19 @@ -require 'logger' +require_relative 'hook_installer' module GitHooks class Installer - HOOK_SAMPLE_FILE = 'hook.sample' - - def initialize(hook, ruby_path: nil, logger: Logger.new('/dev/null')) - @hook = hook - @ruby_path = ruby_path - @logger = logger + def initialize(*hooks, ruby_path: nil, logger: nil) + @installers = hooks.map do |hook| + GitHooks::HookInstaller.new(hook, ruby_path: ruby_path, logger: logger) + 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 - - logger.info "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, :logger - - 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