Skip to content

Commit 92c0795

Browse files
sdsShane da Silva
authored andcommitted
Change top-level hook types to use CamelCase
Previously, the hook type names were snake case (`pre_commit`, etc). This was a bit annoying as it was difficult to remember to switch between underscores for the group name and camel case for the hook name. Convert the naming convention to use camel case. Note that individual settings are still snake case to make them further distinct from the other camel-cased entries. Change-Id: I47f7dd83452de43684d9d01b43464ba49f468592 Reviewed-on: http://gerrit.causes.com/35596 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent 76d52bf commit 92c0795

File tree

11 files changed

+60
-54
lines changed

11 files changed

+60
-54
lines changed

.overcommit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
commit_msg:
1+
CommitMsg:
22
GerritChangeId:
33
enabled: true

config/default.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugin_directory: '.githooks'
1111
# Hooks that run after HEAD changes or a file is explicitly checked out. Useful
1212
# for updating source tags (e.g. via ctags) or warning about new migrations,
1313
# etc.
14-
post_checkout:
14+
PostCheckout:
1515
ALL:
1616
required: false
1717
quiet: true
@@ -27,7 +27,7 @@ post_checkout:
2727
# editor is displayed. These hooks are ideal for syntax checkers, linters, and
2828
# other checks that you want to run before you allow a commit object to be
2929
# created.
30-
pre_commit:
30+
PreCommit:
3131
ALL:
3232
requires_files: true
3333
required: false
@@ -97,7 +97,7 @@ pre_commit:
9797
# Hooks that are run against every commit message after a user has written it.
9898
# These hooks are useful for enforcing policies on commit messages written for a
9999
# project.
100-
commit_msg:
100+
CommitMsg:
101101
ALL:
102102
requires_files: false
103103
quiet: false

lib/overcommit/configuration.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def for_hook(hook, hook_type = nil)
3030
unless hook_type
3131
components = hook.class.name.split('::')
3232
hook = components.last
33-
hook_type = Overcommit::Utils.underscorize(components[-2])
33+
hook_type = components[-2]
3434
end
3535

3636
# Merge hook configuration with special 'ALL' config
@@ -47,8 +47,6 @@ def merge(config)
4747
# Applies additional configuration settings based on the provided
4848
# environment variables.
4949
def apply_environment!(hook_type, env)
50-
hook_type = hook_type.gsub('-', '_')
51-
5250
skipped_hooks = "#{env['SKIP']} #{env['SKIP_CHECKS']}".split(/[:, ]/)
5351

5452
if skipped_hooks.include?('all') || skipped_hooks.include?('ALL')
@@ -71,9 +69,10 @@ def apply_environment!(hook_type, env)
7169

7270
def hook_exists?(hook_type, hook_name)
7371
hook_name = Overcommit::Utils.underscorize(hook_name)
72+
underscored_hook_type = Overcommit::Utils.underscorize(hook_type)
7473

7574
File.exist?(File.join(OVERCOMMIT_HOME, 'lib', 'overcommit', 'hook',
76-
hook_type, "#{hook_name}.rb"))
75+
underscored_hook_type, "#{hook_name}.rb"))
7776
end
7877

7978
def hook_enabled?(hook_type, hook_name)
@@ -107,10 +106,7 @@ def smart_merge(parent, child)
107106
end
108107

109108
def ensure_hook_type_sections_exist(hash)
110-
hook_types = Overcommit::Utils.supported_hook_types.
111-
map { |type| type.gsub('-', '_') }
112-
113-
hook_types.each do |hook_type|
109+
Overcommit::Utils.supported_hook_type_classes.each do |hook_type|
114110
hash[hook_type] ||= {}
115111
hash[hook_type]['ALL'] ||= {}
116112
end

lib/overcommit/hook_context.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
# Utility module which manages the creation of {HookContext}s.
22
module Overcommit::HookContext
33
def self.create(hook_type, config, args, input)
4-
require "overcommit/hook_context/#{hook_type.gsub('-', '_')}"
4+
underscored_hook_type = Overcommit::Utils.underscorize(hook_type)
55

6-
Overcommit::HookContext.const_get(hook_type_to_class_name(hook_type)).
7-
new(config, args, input)
6+
require "overcommit/hook_context/#{underscored_hook_type}"
7+
8+
Overcommit::HookContext.const_get(hook_type).new(config, args, input)
89
rescue LoadError, NameError => error
910
# Could happen when a symlink was created for a hook type Overcommit does
1011
# not yet support.
1112
raise Overcommit::Exceptions::HookContextLoadError,
1213
"Unable to load '#{hook_type}' hook context: '#{error}'",
1314
error.backtrace
1415
end
15-
16-
private
17-
18-
def self.hook_type_to_class_name(hook_type)
19-
hook_type.split('-').map { |s| s.capitalize }.join
20-
end
2116
end

lib/overcommit/hook_runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def load_hooks
5050
# Load hooks that ship with Overcommit, ignoring ones that are excluded from
5151
# the repository's configuration.
5252
def load_builtin_hooks
53-
@config.enabled_builtin_hooks(underscored_hook_type).each do |hook_name|
53+
@config.enabled_builtin_hooks(@context.hook_class_name).each do |hook_name|
5454
underscored_hook_name = Overcommit::Utils.underscorize(hook_name)
5555
require "overcommit/hook/#{underscored_hook_type}/#{underscored_hook_name}"
5656
@hooks << create_hook(hook_name)

lib/overcommit/utils.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def supported_hook_types
3939
map { |file| File.basename(file, '.rb').gsub('_', '-') }
4040
end
4141

42+
# Returns a list of supported hook classes (PreCommit, CommitMsg, etc.)
43+
def supported_hook_type_classes
44+
supported_hook_types.map do |file|
45+
file.split('-').map { |part| part.capitalize }.join
46+
end
47+
end
48+
4249
# Returns whether a command can be found given the current environment path.
4350
def in_path?(cmd)
4451
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

libexec/overcommit-hook

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
begin
1818
require 'overcommit'
19-
rescue LoadError => ex
19+
rescue LoadError
2020
puts 'Overcommit is not installed. Install it to manage git hooks for ' <<
2121
'this repository? (y/n)'
2222

@@ -35,14 +35,16 @@ rescue LoadError => ex
3535
end
3636

3737
begin
38+
hook_type_class = Overcommit::Utils.camel_case(hook_type)
39+
3840
config = Overcommit::ConfigurationLoader.load_repo_config
39-
config.apply_environment!(hook_type, ENV)
41+
config.apply_environment!(hook_type_class, ENV)
4042

4143
# Ensure this script and all symlinks are always up-to-date (it's cheap to do)
4244
#Overcommit::Installer.new(Overcommit::Logger.silent).
4345
# run(Overcommit::Utils.repo_root, :action => :install)
4446

45-
context = Overcommit::HookContext.create(hook_type, config, ARGV, STDIN)
47+
context = Overcommit::HookContext.create(hook_type_class, config, ARGV, STDIN)
4648
logger = Overcommit::Logger.new(STDOUT)
4749
runner = Overcommit::HookRunner.new(config, logger)
4850

spec/integration/committing_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
subject { shell('git commit --allow-empty -m "Test"') }
55

66
let(:config) { <<-YML }
7-
commit_msg:
7+
CommitMsg:
88
ALL:
99
enabled: false
10-
pre_commit:
10+
PreCommit:
1111
ALL:
1212
enabled: false
1313
AuthorName:

spec/overcommit/configuration_spec.rb

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414

1515
context 'when no configuration exists for a hook type' do
1616
it 'creates sections for those hook types' do
17-
internal_hash.should have_key 'pre_commit'
17+
internal_hash.should have_key 'PreCommit'
1818
end
1919

2020
it 'creates the special ALL section for the hook type' do
21-
internal_hash['pre_commit'].should have_key 'ALL'
21+
internal_hash['PreCommit'].should have_key 'ALL'
2222
end
2323
end
2424

2525
context 'when keys with empty values exist' do
2626
let(:hash) do
2727
{
28-
'pre_commit' => {
28+
'PreCommit' => {
2929
'SomeHook' => nil
3030
},
3131
}
3232
end
3333

3434
it 'converts the values to empty hashes' do
35-
internal_hash['pre_commit']['SomeHook'].should == {}
35+
internal_hash['PreCommit']['SomeHook'].should == {}
3636
end
3737
end
3838
end
@@ -53,7 +53,7 @@
5353
describe '#enabled_builtin_hooks' do
5454
let(:hash) do
5555
{
56-
'pre_commit' => {
56+
'PreCommit' => {
5757
'SomeHook' => nil,
5858
'SomeOtherHook' => {
5959
'enabled' => false
@@ -62,7 +62,7 @@
6262
}
6363
end
6464

65-
subject { config.enabled_builtin_hooks('pre_commit') }
65+
subject { config.enabled_builtin_hooks('PreCommit') }
6666

6767
it 'includes hooks that are not disabled' do
6868
subject.should == ['SomeHook']
@@ -72,7 +72,7 @@
7272
describe '#for_hook' do
7373
let(:hash) do
7474
{
75-
'pre_commit' => {
75+
'PreCommit' => {
7676
'ALL' => {
7777
'required' => false,
7878
},
@@ -84,7 +84,7 @@
8484
}
8585
end
8686

87-
subject { config.for_hook('SomeHook', 'pre_commit') }
87+
subject { config.for_hook('SomeHook', 'PreCommit') }
8888

8989
it 'returns the subset of the config for the specified hook' do
9090
subject['enabled'].should be_true
@@ -132,43 +132,43 @@
132132
end
133133

134134
context 'when parent item contains a hash' do
135-
let(:parent) { { 'pre_commit' => { 'SomeHook' => { 'some-value' => 1 } } } }
135+
let(:parent) { { 'PreCommit' => { 'SomeHook' => { 'some-value' => 1 } } } }
136136

137137
context 'and child item contains a different hash under the same key' do
138-
let(:child) { { 'pre_commit' => { 'SomeOtherHook' => { 'something' => 2 } } } }
138+
let(:child) { { 'PreCommit' => { 'SomeOtherHook' => { 'something' => 2 } } } }
139139

140140
it 'merges the hashes together' do
141-
subject.for_hook('SomeHook', 'pre_commit').should == { 'some-value' => 1 }
142-
subject.for_hook('SomeOtherHook', 'pre_commit').should == { 'something' => 2 }
141+
subject.for_hook('SomeHook', 'PreCommit').should == { 'some-value' => 1 }
142+
subject.for_hook('SomeOtherHook', 'PreCommit').should == { 'something' => 2 }
143143
end
144144
end
145145

146146
context 'and child item contains a hash under a different key' do
147-
let(:child) { { 'commit_msg' => { 'SomeHook' => { 'some-value' => 2 } } } }
147+
let(:child) { { 'CommitMsg' => { 'SomeHook' => { 'some-value' => 2 } } } }
148148

149149
it 'appends the item to the parent array' do
150-
subject.for_hook('SomeHook', 'pre_commit').should == { 'some-value' => 1 }
151-
subject.for_hook('SomeHook', 'commit_msg').should == { 'some-value' => 2 }
150+
subject.for_hook('SomeHook', 'PreCommit').should == { 'some-value' => 1 }
151+
subject.for_hook('SomeHook', 'CommitMsg').should == { 'some-value' => 2 }
152152
end
153153
end
154154
end
155155

156156
context 'when parent item contains an array' do
157-
let(:parent) { { 'pre_commit' => { 'SomeHook' => { 'list' => [1, 2, 3] } } } }
157+
let(:parent) { { 'PreCommit' => { 'SomeHook' => { 'list' => [1, 2, 3] } } } }
158158

159159
context 'and child item contains an array' do
160-
let(:child) { { 'pre_commit' => { 'SomeHook' => { 'list' => [4, 5] } } } }
160+
let(:child) { { 'PreCommit' => { 'SomeHook' => { 'list' => [4, 5] } } } }
161161

162162
it 'concatenates the arrays together' do
163-
subject.for_hook('SomeHook', 'pre_commit')['list'] == [1, 2, 3, 4, 5]
163+
subject.for_hook('SomeHook', 'PreCommit')['list'] == [1, 2, 3, 4, 5]
164164
end
165165
end
166166

167167
context 'and child item contains a single item' do
168-
let(:child) { { 'pre_commit' => { 'SomeHook' => { 'list' => 4 } } } }
168+
let(:child) { { 'PreCommit' => { 'SomeHook' => { 'list' => 4 } } } }
169169

170170
it 'appends the item to the parent array' do
171-
subject.for_hook('SomeHook', 'pre_commit')['list'] == [1, 2, 3, 4]
171+
subject.for_hook('SomeHook', 'PreCommit')['list'] == [1, 2, 3, 4]
172172
end
173173
end
174174
end
@@ -181,7 +181,7 @@
181181
subject { config }
182182

183183
before do
184-
config.apply_environment!('pre-commit', env)
184+
config.apply_environment!('PreCommit', env)
185185
end
186186

187187
context 'when no hooks are requested to be skipped' do
@@ -204,22 +204,22 @@
204204
let(:env) { { 'SKIP' => 'AuthorName' } }
205205

206206
it 'sets the skip option of the hook to true' do
207-
subject.for_hook('AuthorName', 'pre_commit')['skip'].should be_true
207+
subject.for_hook('AuthorName', 'PreCommit')['skip'].should be_true
208208
end
209209

210210
context 'and the hook is spelt with underscores' do
211211
let(:env) { { 'SKIP' => 'author_name' } }
212212

213213
it 'sets the skip option of the hook to true' do
214-
subject.for_hook('AuthorName', 'pre_commit')['skip'].should be_true
214+
subject.for_hook('AuthorName', 'PreCommit')['skip'].should be_true
215215
end
216216
end
217217

218218
context 'and the hook is spelt with hyphens' do
219219
let(:env) { { 'SKIP' => 'author-name' } }
220220

221221
it 'sets the skip option of the hook to true' do
222-
subject.for_hook('AuthorName', 'pre_commit')['skip'].should be_true
222+
subject.for_hook('AuthorName', 'PreCommit')['skip'].should be_true
223223
end
224224
end
225225
end
@@ -228,14 +228,14 @@
228228
let(:env) { { 'SKIP' => 'all' } }
229229

230230
it 'sets the skip option of the ALL section to true' do
231-
subject.for_hook('ALL', 'pre_commit')['skip'].should be_true
231+
subject.for_hook('ALL', 'PreCommit')['skip'].should be_true
232232
end
233233

234234
context 'and "all" is capitalized' do
235235
let(:env) { { 'SKIP' => 'ALL' } }
236236

237237
it 'sets the skip option of the special ALL config to true' do
238-
subject.for_hook('ALL', 'pre_commit')['skip'].should be_true
238+
subject.for_hook('ALL', 'PreCommit')['skip'].should be_true
239239
end
240240
end
241241
end

spec/overcommit/hook/pre_commit/author_email_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
context 'when a custom pattern is specified' do
3333
let(:config) do
3434
super().merge(Overcommit::Configuration.new(
35-
'pre_commit' => {
35+
'PreCommit' => {
3636
'AuthorEmail' => {
3737
'pattern' => '^[^@]+@causes\.com$'
3838
}

0 commit comments

Comments
 (0)