Class: PreCommit::Tabs

Inherits:
Object
  • Object
show all
Defined in:
lib/pre-commit/checks/tabs.rb

Constant Summary collapse

LEADING_TAB_PATTERN =
'^ *\t'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



6
7
8
# File 'lib/pre-commit/checks/tabs.rb', line 6

def error_message
  @error_message
end

#staged_filesObject

Returns the value of attribute staged_files.



6
7
8
# File 'lib/pre-commit/checks/tabs.rb', line 6

def staged_files
  @staged_files
end

Class Method Details

.callObject

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pre-commit/checks/tabs.rb', line 12

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

Instance Method Details

#detected_bad_code?Boolean



45
46
47
# File 'lib/pre-commit/checks/tabs.rb', line 45

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

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pre-commit/checks/tabs.rb', line 27

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

#violationsObject



49
50
51
# File 'lib/pre-commit/checks/tabs.rb', line 49

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