Module: Overcommit::Utils

Defined in:
lib/overcommit/utils.rb

Constant Summary collapse

@@hooks =
[]

Class Method Summary collapse

Class Method Details

.absolute_path(path) ⇒ Object



29
30
31
# File 'lib/overcommit/utils.rb', line 29

def absolute_path(path)
  File.join(File.expand_path('../../..', __FILE__), path)
end

.hook_nameObject



14
15
16
# File 'lib/overcommit/utils.rb', line 14

def hook_name
  File.basename($0).tr('-', '_')
end

.load_hooksObject



18
19
20
21
22
23
# File 'lib/overcommit/utils.rb', line 18

def load_hooks
  require File.expand_path("../hooks/#{hook_name}", __FILE__)
rescue LoadError
  log.error "No hook definition found for #{hook_name}"
  exit 1
end

.modified_filesObject

Get a list of staged Added, Copied, or Modified files (ignore renames and deletions, since there should be nothing to check).



52
53
54
# File 'lib/overcommit/utils.rb', line 52

def modified_files
  `git diff --cached --name-only --diff-filter=ACM --ignore-submodules=all`.split "\n"
end

.register_hook(hook) ⇒ Object



6
7
8
# File 'lib/overcommit/utils.rb', line 6

def register_hook(hook)
  @@hooks << hook
end

.repo_path(path) ⇒ Object

File.expand_path takes one more ‘..’ than you’re used to… we want to go two directories up from the caller (which will be .git/hooks/something) to the root of the git repo.



36
37
38
# File 'lib/overcommit/utils.rb', line 36

def repo_path(path)
  File.join(File.expand_path('../../..', $0), path)
end

.run_hooks(*args) ⇒ Object



10
11
12
# File 'lib/overcommit/utils.rb', line 10

def run_hooks(*args)
  @@hooks.each { |hook| hook.new.run(*args) }
end

.script_path(script) ⇒ Object



25
26
27
# File 'lib/overcommit/utils.rb', line 25

def script_path(script)
  File.join(File.expand_path('../../hooks/scripts', $0), script)
end

.underscorize(str) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/overcommit/utils.rb', line 42

def underscorize(str)
  str.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
      gsub(/([a-z\d])([A-Z])/, '\1_\2').
      tr('-', '_').
      downcase
end