Sha256: 73e96cb2f687593f0c68c857b06a48aea31dfc7ed9a0ff7437c9dbf42b213d6d

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

class Tabs

  attr_accessor :staged_files, :error_message

  # Maintaining the functionality of `call` for backwards compatibility
  # Currently, the call method is expected to:
  # * run a check
  # * print any corresponding error messages if the check fails
  def self.call
    check = new
    check.staged_files = Utils.staged_files('*')
    result = check.run

    if !result
      $stderr.puts check.error_message
      $stderr.puts
      $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
      $stderr.puts
    end

    result
  end

  def run
    # There is nothing to check
    if staged_files.empty?
      return true
    end

    if detected_bad_code?
      @error_message = "pre-commit: detected tab before initial space:\n"
      @error_message += violations

      @passed = false
    else
      @passed = true
    end
  end

  LEADING_TAB_PATTERN = '^ *\t'

  def detected_bad_code?
    system("#{grep} -q '#{LEADING_TAB_PATTERN}' #{staged_files}")
  end

  def violations
    `#{grep} '#{LEADING_TAB_PATTERN}' #{staged_files}`
  end

  def grep
    grep_version = `grep --version | head -n 1 | sed -e 's/^[^0-9.]*\([0-9.]*\)$/\1/'`
    if grep_version =~ /FreeBSD/
      "grep -EnIH"
    else
      "grep -PnIH"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pre-commit-0.5.0 lib/pre-commit/checks/tabs.rb
pre-commit-0.4.0 lib/pre-commit/checks/tabs.rb
pre-commit-0.3.1 lib/pre-commit/checks/tabs.rb