From 30bfcf58981d34672915eb6cad5a2d721701c2a8 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Thu, 15 Dec 2016 18:55:17 +0900 Subject: [PATCH] Skip Git message `\ No newline at end of file` is inserted when the file does not contain LF at EOF. Ignore the message to count line numbers correctly. --- lib/git_diff_parser/patch.rb | 3 +++ spec/git_diff_parser/patch_spec.rb | 11 +++++++++++ spec/support/fixtures/file4.diff | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 spec/support/fixtures/file4.diff diff --git a/lib/git_diff_parser/patch.rb b/lib/git_diff_parser/patch.rb index 26011b6..d3aa690 100644 --- a/lib/git_diff_parser/patch.rb +++ b/lib/git_diff_parser/patch.rb @@ -4,6 +4,7 @@ class Patch RANGE_INFORMATION_LINE = /^@@ .+\+(?\d+),/ MODIFIED_LINE = /^\+(?!\+|\+)/ NOT_REMOVED_LINE = /^[^-]/ + NO_NEWLINE_MESSAGE = /^\\ No newline at end of file$/ attr_accessor :file, :body, :secure_hash # @!attribute [rw] file @@ -60,6 +61,8 @@ def changed_lines case content when RANGE_INFORMATION_LINE line_number = Regexp.last_match[:line_number].to_i + when NO_NEWLINE_MESSAGE + # nop when MODIFIED_LINE line = Line.new( content: content, diff --git a/spec/git_diff_parser/patch_spec.rb b/spec/git_diff_parser/patch_spec.rb index 1a11508..3a058e0 100644 --- a/spec/git_diff_parser/patch_spec.rb +++ b/spec/git_diff_parser/patch_spec.rb @@ -39,6 +39,17 @@ module GitDiffParser end end + context "Newline inserted at EOF" do + describe "changed_line_numbers" do + it "returns changed line number" do + patch_body = File.read('spec/support/fixtures/file4.diff') + patch = Patch.new(patch_body) + + expect(patch.changed_line_numbers).to eq [4] + end + end + end + describe '#find_patch_position_by_line_number' do it 'returns patch position that were included' do patch_body = File.read('spec/support/fixtures/patch.diff') diff --git a/spec/support/fixtures/file4.diff b/spec/support/fixtures/file4.diff new file mode 100644 index 0000000..36c5659 --- /dev/null +++ b/spec/support/fixtures/file4.diff @@ -0,0 +1,7 @@ +@@ -1,4 +1,4 @@ + 1 + 2 + 3 +-File1 +\ No newline at end of file ++File1 v2