Class: Devloop::DiffParser

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

Defined Under Namespace

Classes: Diff

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(diff) ⇒ Object



3
4
5
# File 'lib/devloop/diff_parser.rb', line 3

def self.call(diff)
  new.call(diff)
end

Instance Method Details

#call(diff) ⇒ Object



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/devloop/diff_parser.rb', line 9

def call(diff)
  _, diffs_data = diff.split("\n").reduce(["", []]) do |(file, diffs_data), line|
    if line.start_with?("+++ b/")
      [line[6..-1], diffs_data]
    elsif line.start_with?("@@ -")
      line_number = line.match(/@@ -(\d+)/)[1]
      [file, diffs_data << Diff.new(relative_path(file), line_number)]
    else
      [file, diffs_data]
    end
  end.uniq

  results = diffs_data.group_by do |el|
    el.filename
  end.map do |key, value|
    line_numbers = value.map(&:line_number).map(&:to_i)
    if line_numbers.include?(0) || line_numbers.include?(1)
      key
    else
      lines_range = (line_numbers.min..line_numbers.max).to_a.join(":")

      "#{key}:#{lines_range}"
    end
  end

  # Remove filenames with line number if filename without line number is present
  res = results.reject { |result| results.include?(result.split(":").first) && result.include?(":") }
end