From 1c8e0ab3113cb18c9e64030d6c8129abd56a63cd Mon Sep 17 00:00:00 2001 From: Jordan Oroshiba Date: Mon, 12 Feb 2018 15:22:12 -0600 Subject: [PATCH 1/2] Line numbers within patch for removed lines should increase with each non removed line --- lib/git_diff_parser/patch.rb | 1 + spec/git_diff_parser/patch_spec.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/git_diff_parser/patch.rb b/lib/git_diff_parser/patch.rb index d0a7ed4..e30dd30 100644 --- a/lib/git_diff_parser/patch.rb +++ b/lib/git_diff_parser/patch.rb @@ -92,6 +92,7 @@ def removed_lines patch_position: patch_position ) lines << line + when NOT_REMOVED_LINE line_number += 1 end diff --git a/spec/git_diff_parser/patch_spec.rb b/spec/git_diff_parser/patch_spec.rb index eb5de2c..3918285 100644 --- a/spec/git_diff_parser/patch_spec.rb +++ b/spec/git_diff_parser/patch_spec.rb @@ -27,7 +27,7 @@ module GitDiffParser 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(&:number)).to eq [14, 39, 39, 39, 39, 39, 54] expect(patch.removed_lines.map(&:patch_position)).to eq [4, 21, 22, 23, 24, 25, 36] end From bbb441f21cefe39bc0e8f4b4a31b7b7f83d214aa Mon Sep 17 00:00:00 2001 From: Jordan Oroshiba Date: Tue, 13 Feb 2018 10:33:03 -0600 Subject: [PATCH 2/2] Use previous file line numbers for removed lines --- lib/git_diff_parser/patch.rb | 5 +++-- spec/git_diff_parser/patch_spec.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/git_diff_parser/patch.rb b/lib/git_diff_parser/patch.rb index e30dd30..8fa5d70 100644 --- a/lib/git_diff_parser/patch.rb +++ b/lib/git_diff_parser/patch.rb @@ -1,7 +1,7 @@ module GitDiffParser # Parsed patch class Patch - RANGE_INFORMATION_LINE = /^@@ .+\+(?\d+),/ + RANGE_INFORMATION_LINE = /^@@ -(?\d+),.+\+(?\d+),/ MODIFIED_LINE = /^\+(?!\+|\+)/ REMOVED_LINE = /^[-]/ NOT_REMOVED_LINE = /^[^-]/ @@ -84,7 +84,7 @@ def removed_lines 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 + line_number = Regexp.last_match[:old_line_number].to_i when REMOVED_LINE line = Line.new( content: content, @@ -92,6 +92,7 @@ def removed_lines patch_position: patch_position ) lines << line + line_number += 1 when NOT_REMOVED_LINE line_number += 1 end diff --git a/spec/git_diff_parser/patch_spec.rb b/spec/git_diff_parser/patch_spec.rb index 3918285..b3769ce 100644 --- a/spec/git_diff_parser/patch_spec.rb +++ b/spec/git_diff_parser/patch_spec.rb @@ -27,7 +27,7 @@ module GitDiffParser patch = Patch.new(patch_body) expect(patch.removed_lines.size).to eq(7) - expect(patch.removed_lines.map(&:number)).to eq [14, 39, 39, 39, 39, 39, 54] + expect(patch.removed_lines.map(&:number)).to eq [14, 38, 39, 40, 41, 42, 58] expect(patch.removed_lines.map(&:patch_position)).to eq [4, 21, 22, 23, 24, 25, 36] end