Module: Overcommit::Utils

Defined in:
lib/overcommit/utils.rb

Overview

Utility functions for general use.

Class Method Summary collapse

Class Method Details

.broken_symlink?(file) ⇒ true, false

Returns whether a file is a broken symlink.

Returns:

  • (true, false)


93
94
95
96
97
# File 'lib/overcommit/utils.rb', line 93

def broken_symlink?(file)
  # JRuby's implementation of File.exist? returns true for broken
  # symlinks, so we need use File.size?
  File.symlink?(file) && File.size?(file).nil?
end

.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
70
71
72
73
74
# File 'lib/overcommit/utils.rb', line 65

def execute(args)
  if args.include?('|')
    raise Overcommit::Exceptions::InvalidCommandArgs,
          'Cannot pipe commands with the `execute` helper'
  end

  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(&: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

.with_environment(env) ⇒ Object

Calls a block of code with a modified set of environment variables, restoring them once the code has executed.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/overcommit/utils.rb', line 78

def with_environment(env)
  old_env = {}
  env.each do |var, value|
    old_env[var] = ENV[var.to_s]
    ENV[var.to_s] = value
  end

  yield
ensure
  old_env.each { |var, value| ENV[var.to_s] = value }
end