-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Changes from all commits
c9f57c7
1805cbe
cbb9b75
174372f
e9f6b41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 %> |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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