Class: CheckstyleFilter::Git::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/checkstyle_filter/git/filter.rb

Class Method Summary collapse

Class Method Details

.file_relative_path_string(file_name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/checkstyle_filter/git/filter.rb', line 32

def self.file_relative_path_string(file_name)
  if Pathname.new(file_name).absolute?
    Pathname.new(file_name).relative_path_from(Pathname.new(Dir.pwd)).to_s
  else
    Pathname.new(file_name).relative_path_from(Pathname.new('.')).to_s
  end
end

.filter(data, git_diff) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/checkstyle_filter/git/filter.rb', line 7

def self.filter(data, git_diff)
  patches = ::GitDiffParser.parse(git_diff)

  document = REXML::Document.new data
  document.elements.each('/checkstyle/file') do |file_element|
    file_name = file_element.attribute('name') && file_element.attribute('name').value
    file = file_relative_path_string(file_name)
    patch = patches.find_patch_by_file(file)
    if patch
      file_element.elements.each('error') do |error_element|
        line = error_element.attribute('line') && error_element.attribute('line').value.to_i
        unless patch.changed_line_numbers.include?(line)
          error_element.remove
        end
      end
    else
      file_element.elements.each('error') do |error_element|
        error_element.remove
      end
    end
  end

  document.to_s
end