Class: GitDiffMap::Patch

Inherits:
GitDiffParser::Patch
  • Object
show all
Defined in:
lib/git_diff_map/patch.rb

Constant Summary collapse

RANGE_INFORMATION_LINE =
/^@@ -(?<original_base_line>\d+),\d+ \+(?<new_base_line>\d+),/
ADDED_LINE =
-> (line) { line.start_with?('+') && line !~ /^\+\+\+/ }
REMOVED_LINE =
-> (line) { line.start_with?('-') && line !~ /^\-\-\-/ }

Instance Method Summary collapse

Constructor Details

#initialize(original_patch) ⇒ Patch



15
16
17
18
19
# File 'lib/git_diff_map/patch.rb', line 15

def initialize(original_patch)
  @body = original_patch.body
  @file = original_patch.file
  @secure_hash = original_patch.secure_hash
end

Instance Method Details

#changed_linesArray<Line>



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git_diff_map/patch.rb', line 22

def changed_lines
  original_line = 0
  new_line = 0

  lines.each_with_index.inject([]) do |lines, (content, patch_position)|
    case content
    when RANGE_INFORMATION_LINE
      original_line = Regexp.last_match[:original_base_line].to_i
      new_line = Regexp.last_match[:new_base_line].to_i
    when ADDED_LINE
      line = Line.new(
        content: content,
        number: new_line,
        patch_position: patch_position
      )
      lines << line
      new_line += 1
    when REMOVED_LINE
      line = Line.new(
        content: content,
        number: original_line,
        patch_position: patch_position
      )
      lines << line
      original_line += 1
    when NOT_REMOVED_LINE
      original_line += 1
      new_line += 1
    end

    lines
  end
end

#changed_methodsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git_diff_map/patch.rb', line 56

def changed_methods
  lines = changed_lines
  lines
    .group_by{|l| l.number}
    .select{|_, v| v.size == 2}
    .select{|_, v| t = v.map(&:type); t.include?('-') && t.include?('+')}
    .select{|_, v| v.all?{|x| x.content =~ /def\s+\w+/}}
    .map{|line, v|
    sorted = v.sort_by(&:type)
    begin
      ChangedMethod.new(sorted.first, sorted.last, line, @file)
    rescue Method::InvalidAST => ex
      warn "Warning: #{ex}\n#{ex.backtrace.join("\n")}"if RuboCop::ConfigLoader.debug
    end
  }
    .compact
end