Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install all command #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions hook.sample

This file was deleted.

6 changes: 6 additions & 0 deletions hook.sample.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!<%= ruby_path || '/usr/bin/env ruby' %>
require 'git-hooks'

puts 'Running <%= hook %> hooks...'

<%= run_hook_command %>
5 changes: 0 additions & 5 deletions lib/git-hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
require_relative 'git_hooks/pre_commit'

module GitHooks
HOOK_SAMPLE_FILE = 'hook.sample'
HOOKS = [PRE_COMMIT = 'pre-commit']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will we do about it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this was used? I might be incorrect


class << self
Expand Down Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lib/git_hooks/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
56 changes: 56 additions & 0 deletions lib/git_hooks/hook_installer.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is to make the hook an executable? @rranelli may be able to speak to this better

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
43 changes: 7 additions & 36 deletions lib/git_hooks/installer.rb
Original file line number Diff line number Diff line change
@@ -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