Sha256: 7eb9c618d6758118430a6f37f936c4246985eca87b5dc2d5a23f92a9fc79fe8a

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

module Overcommit
  module Utils
    class << self
      @@hooks = []

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

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

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

      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

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

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

      # 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.
      def repo_path(path)
        File.join(File.expand_path('../../..', $0), path)
      end

      # Shamelessly stolen from:
      # http://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
      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

      # Get a list of staged Added, Copied, or Modified files (ignore renames
      # and deletions, since there should be nothing to check).
      def modified_files
        @modified_files ||=
          `git diff --cached --name-only --diff-filter=ACM`.split "\n"
      end

    private

      def log
        Logger.instance
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
overcommit-0.1.11 lib/overcommit/utils.rb
overcommit-0.1.10 lib/overcommit/utils.rb
overcommit-0.1.9 lib/overcommit/utils.rb
overcommit-0.1.8 lib/overcommit/utils.rb
overcommit-0.1.7 lib/overcommit/utils.rb
overcommit-0.1.6 lib/overcommit/utils.rb
overcommit-0.1.5 lib/overcommit/utils.rb
overcommit-0.1.4 lib/overcommit/utils.rb