Class: CheckstyleFilter::Git::DiffParser

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

Class Method Summary collapse

Class Method Details

.parse(contents) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/checkstyle_filter/git/diff_parser.rb', line 4

def self.parse(contents)
  body = false
  file_name = ''
  patch = []
  lines = contents.lines
  line_count = lines.count
  parsed = []
  lines.each_with_index do |line, count|
    case line.chomp
    when /^diff/
      unless patch.empty?
        parsed << { file_name: file_name,
                    patch: ::Git::Diff::Parser::Patch.new(patch.join("\n")) }
        patch.clear
        file_name = ''
      end
      body = false
    when /^\-\-\-/
    when %r{^\+\+\+ b/(?<file_name>.*)}
      file_name = Regexp.last_match[:file_name]
      body = true
    when /^(?<body>[\ @\+\-\\].*)/
      patch << Regexp.last_match[:body] if body
      if !patch.empty? && body && line_count == count + 1
        parsed << { file_name: file_name,
                    patch: ::Git::Diff::Parser::Patch.new(patch.join("\n")) }
        patch.clear
        file_name = ''
      end
    end
  end
  parsed
end