Sha256: 44f449727293f903ca062ed4324ec8276264814351e4fa013238d4b64a6f2297

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module PreCommit
  class DebuggerCheck

    attr_accessor :staged_files, :error_message, :grep_command

    def self.call(quiet=false)
      dirs = ['app/', 'lib/', 'script/', 'vendor/', 'test/'].reject {|d| !File.exists?(d)}
      check = new
      check.staged_files = Utils.staged_files(*dirs)

      result = check.run
      if !quiet && !result
        $stderr.puts check.error_message
        $stderr.puts
        check.print_dash_n_reminder
      end
      result
    end

    def run
      return true if staged_files.empty?

      if detected_bad_code?
        @error_message = "pre-commit: debugger statement(s) found:\n"
        @error_message += instances_of_debugger_violations
        false
      else
        true
      end
    end

    def detected_bad_code?
      !system("git diff --cached -Sdebugger --quiet --exit-code")
    end

    def instances_of_debugger_violations
      cmd = grep_command || "git grep"
      `#{cmd} -nH debugger #{staged_files}`
    end

    def print_dash_n_reminder
      $stderr.puts 'You can bypass this check using `git commit -n`'
      $stderr.puts
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pre-commit-0.8.1 lib/pre-commit/checks/debugger_check.rb
pre-commit-0.8.0 lib/pre-commit/checks/debugger_check.rb
pre-commit-0.7.0 lib/pre-commit/checks/debugger_check.rb