Module: Overcommit::Utils

Defined in:
lib/overcommit/utils.rb

Overview

Utility functions for general use.

Class Method Summary collapse

Class Method Details

.camel_case(str) ⇒ Object

Converts a string containing underscores/hyphens/spaces into CamelCase.



31
32
33
# File 'lib/overcommit/utils.rb', line 31

def camel_case(str)
  str.split(/_|-| /).map { |part| part.sub(/^\w/) { |c| c.upcase } }.join
end

.execute(args) ⇒ Object

Wrap external subshell calls. This is necessary in order to allow Overcommit to call other Ruby executables without requiring that they be specified in Overcommit’s Gemfile–a nasty consequence of using ‘bundle exec overcommit` while developing locally.



65
66
67
68
69
# File 'lib/overcommit/utils.rb', line 65

def execute(args)
  with_environment 'RUBYOPT' => nil do
    Subprocess.spawn(args)
  end
end

.in_path?(cmd) ⇒ Boolean

Returns whether a command can be found given the current environment path.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
# File 'lib/overcommit/utils.rb', line 50

def in_path?(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return true if File.executable?(exe)
    end
  end
  false
end

.repo_rootObject

Returns an absolute path to the root of the repository.



12
13
14
15
16
17
18
# File 'lib/overcommit/utils.rb', line 12

def repo_root
  @repo_root ||=
    begin
      result = `git rev-parse --show-toplevel`.chomp
      result if $?.success?
    end
end

.script_path(script) ⇒ Object



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

def script_path(script)
  File.join(OVERCOMMIT_HOME, 'libexec', script)
end

.snake_case(str) ⇒ Object

Shamelessly stolen from: stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby



22
23
24
25
26
27
28
# File 'lib/overcommit/utils.rb', line 22

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

.supported_hook_type_classesObject

Returns a list of supported hook classes (PreCommit, CommitMsg, etc.)



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

def supported_hook_type_classes
  supported_hook_types.map do |file|
    file.split('-').map { |part| part.capitalize }.join
  end
end

.supported_hook_typesObject

Returns a list of supported hook types (pre-commit, commit-msg, etc.)



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

def supported_hook_types
  Dir[File.join(OVERCOMMIT_HOME, 'lib', 'overcommit', 'hook', '*')].
    select { |file| File.directory?(file) }.
    map { |file| File.basename(file, '.rb').gsub('_', '-') }
end