Skip to content

Commit 2d69c08

Browse files
author
Aiden Scandella
committedMay 23, 2013
Add Installer to perform installation
This writes a repo-specific 'overcommit.yml' based on a combination of the specified template (or the 'default' template) and the command-line specified 'exclude's. The last piece of the puzzle is to make the hook runner respect this YAML file. Change-Id: Id7d78bbf7141d75bb75fde8fe796623ab15d6530 Reviewed-on: https://gerrit.causes.com/22695 Reviewed-by: Aiden Scandella <aiden@causes.com> Tested-by: Aiden Scandella <aiden@causes.com>
1 parent 07323ef commit 2d69c08

File tree

6 files changed

+118
-4
lines changed

6 files changed

+118
-4
lines changed
 

‎config/templates.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
default:
23
excludes:
34
pre_commit:
@@ -6,3 +7,5 @@ default:
67
- restricted_paths
78
commit_msg:
89
- change_id
10+
all:
11+
excludes:

‎lib/overcommit.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'overcommit/console_methods'
33
require 'overcommit/git_hook'
44
require 'overcommit/hook_specific_check'
5+
require 'overcommit/installer'
56
require 'overcommit/reporter'
67
require 'overcommit/utils'
78
require 'overcommit/version'

‎lib/overcommit/cli.rb

+50-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module Overcommit
44
class CLI
5-
65
def initialize(arguments = [])
76
@arguments = arguments
87
@options = {}
@@ -20,6 +19,38 @@ def parse_arguments
2019
puts VERSION
2120
exit 0
2221
end
22+
23+
opts.on('-a', '--all', 'Include all git hooks') do
24+
@options[:template] = 'all'
25+
end
26+
27+
opts.on('-t', '--template template',
28+
'Specify a template of hooks') do |template|
29+
@options[:template] = template
30+
end
31+
32+
opts.on('-e', '--exclude hook_name,...', Array,
33+
'Exclude hooks from installation') do |excludes|
34+
# Transform from:
35+
#
36+
# pre_commit/test_history,commit_msg/change_id
37+
#
38+
# Into:
39+
#
40+
# {
41+
# 'commit_msg' => ['change_id'],
42+
# 'pre_commit' => ['test_history']
43+
# }
44+
@options[:excludes] = excludes.inject({}) do |memo, exclude|
45+
parts = exclude.split(%r{[:/.]})
46+
next memo unless parts.size == 2
47+
48+
memo[parts.first] ||= []
49+
memo[parts.first] << parts.last
50+
51+
memo
52+
end
53+
end
2354
end
2455

2556
begin
@@ -33,7 +64,24 @@ def parse_arguments
3364
end
3465

3566
def run
36-
raise NotImplementedError, 'Nothing to see here yet'
67+
if @options[:targets].nil? || @options[:targets].empty?
68+
puts 'You must supply at least one directory to install into.'
69+
puts 'For example:', ''
70+
puts " #{File.basename($0)} <target directory>"
71+
exit 2
72+
end
73+
74+
installer = Installer.new(@options)
75+
76+
@options[:targets].each do |target|
77+
installer.install(target)
78+
end
79+
80+
puts 'Installation complete.'
81+
82+
rescue ArgumentError => ex
83+
puts "Installation failed: #{ex}"
84+
exit 3
3785
end
3886

3987
private

‎lib/overcommit/configuration.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class Configuration
88
attr_reader :templates
99

1010
def initialize
11-
@templates = YAML::load_file(File.join(File.dirname(
12-
File.expand_path(__FILE__)), '..', '..', 'config', 'templates.yml'))
11+
@templates = YAML::load_file(Utils.absolute_path('config/templates.yml'))
1312
end
1413
end
1514

‎lib/overcommit/installer.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'fileutils'
2+
require 'yaml'
3+
4+
module Overcommit
5+
class Installer
6+
def initialize(options = {})
7+
@options = options
8+
end
9+
10+
def install(target)
11+
absolute_target = File.expand_path(target)
12+
unless File.directory?(File.join(absolute_target, '.git'))
13+
14+
raise ArgumentError, "#{target} does not appear to be a git repository"
15+
end
16+
17+
puts "Installing hooks into #{target}"
18+
hook_path = File.join(absolute_target, '.git/hooks')
19+
20+
install_scripts(hook_path)
21+
install_hooks(hook_path)
22+
write_configuration(hook_path)
23+
end
24+
25+
private
26+
27+
# Make helper scripts available locally inside the repo
28+
def install_scripts(target)
29+
FileUtils.cp_r Utils.absolute_path('bin/scripts'), target
30+
end
31+
32+
# Install all available git hooks into the repo
33+
def install_hooks(target)
34+
Dir[Utils.absolute_path('bin/hooks/*')].each do |hook|
35+
FileUtils.cp hook, File.join(target, File.basename(hook))
36+
end
37+
end
38+
39+
# Dump a YAML document containing requested configuration
40+
def write_configuration(target)
41+
template = @options.fetch(:template, 'default')
42+
base_config = Overcommit.config.templates[template]
43+
if base_config.nil?
44+
raise ArgumentError, "No such template '#{template}'"
45+
end
46+
47+
base_config = base_config.dup
48+
(base_config['excludes'] ||= {}).
49+
merge!(@options[:excludes] || {}) do |_, a, b|
50+
# Concat the arrays together
51+
a + b
52+
end
53+
54+
File.open(File.join(target, 'overcommit.yml'), 'w') do |config|
55+
YAML.dump(base_config, config)
56+
end
57+
end
58+
end
59+
end

‎lib/overcommit/utils.rb

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def script_path(script)
2828
File.join(File.expand_path('../../hooks/scripts', $0), script)
2929
end
3030

31+
def absolute_path(path)
32+
File.join(File.expand_path('../../..', __FILE__), path)
33+
end
34+
3135
# Shamelessly stolen from:
3236
# http://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
3337
def underscorize(str)

0 commit comments

Comments
 (0)
Please sign in to comment.