diff --git a/lib/git_diff_parser/patch.rb b/lib/git_diff_parser/patch.rb index 26011b6..6bcf9f8 100644 --- a/lib/git_diff_parser/patch.rb +++ b/lib/git_diff_parser/patch.rb @@ -3,6 +3,7 @@ module GitDiffParser class Patch RANGE_INFORMATION_LINE = /^@@ .+\+(?\d+),/ MODIFIED_LINE = /^\+(?!\+|\+)/ + REMOVED_LINE = /^[-]/ NOT_REMOVED_LINE = /^[^-]/ attr_accessor :file, :body, :secure_hash @@ -76,6 +77,27 @@ def changed_lines end end + def removed_lines + line_number = 0 + + lines.each_with_index.inject([]) do |lines, (content, patch_position)| + case content + when RANGE_INFORMATION_LINE + line_number = Regexp.last_match[:line_number].to_i + when REMOVED_LINE + line = Line.new( + content: content, + number: line_number, + patch_position: patch_position + ) + lines << line + line_number += 1 + end + + lines + end + end + # @return [Array] changed line numbers def changed_line_numbers changed_lines.map(&:number) diff --git a/spec/git_diff_parser/patch_spec.rb b/spec/git_diff_parser/patch_spec.rb index 1a11508..eb5de2c 100644 --- a/spec/git_diff_parser/patch_spec.rb +++ b/spec/git_diff_parser/patch_spec.rb @@ -21,6 +21,25 @@ module GitDiffParser end end + describe '#removed_lines' do + it 'returns lines that were modified' do + patch_body = File.read('spec/support/fixtures/patch.diff') + patch = Patch.new(patch_body) + + expect(patch.removed_lines.size).to eq(7) + expect(patch.removed_lines.map(&:number)).to eq [11, 36, 37, 38, 39, 40, 48] + expect(patch.removed_lines.map(&:patch_position)).to eq [4, 21, 22, 23, 24, 25, 36] + end + + context 'when body is nil' do + it 'returns no lines' do + patch = Patch.new(nil) + + expect(patch.removed_lines.size).to eq(0) + end + end + end + describe '#changed_line_numbers' do it 'returns line numbers that were modified' do patch_body = File.read('spec/support/fixtures/patch.diff')