Skip to content

Commit 30bfcf5

Browse files
committed
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.
1 parent 8dd67cf commit 30bfcf5

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/git_diff_parser/patch.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Patch
44
RANGE_INFORMATION_LINE = /^@@ .+\+(?<line_number>\d+),/
55
MODIFIED_LINE = /^\+(?!\+|\+)/
66
NOT_REMOVED_LINE = /^[^-]/
7+
NO_NEWLINE_MESSAGE = /^\\ No newline at end of file$/
78

89
attr_accessor :file, :body, :secure_hash
910
# @!attribute [rw] file
@@ -60,6 +61,8 @@ def changed_lines
6061
case content
6162
when RANGE_INFORMATION_LINE
6263
line_number = Regexp.last_match[:line_number].to_i
64+
when NO_NEWLINE_MESSAGE
65+
# nop
6366
when MODIFIED_LINE
6467
line = Line.new(
6568
content: content,

spec/git_diff_parser/patch_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ module GitDiffParser
3939
end
4040
end
4141

42+
context "Newline inserted at EOF" do
43+
describe "changed_line_numbers" do
44+
it "returns changed line number" do
45+
patch_body = File.read('spec/support/fixtures/file4.diff')
46+
patch = Patch.new(patch_body)
47+
48+
expect(patch.changed_line_numbers).to eq [4]
49+
end
50+
end
51+
end
52+
4253
describe '#find_patch_position_by_line_number' do
4354
it 'returns patch position that were included' do
4455
patch_body = File.read('spec/support/fixtures/patch.diff')

spec/support/fixtures/file4.diff

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@@ -1,4 +1,4 @@
2+
1
3+
2
4+
3
5+
-File1
6+
\ No newline at end of file
7+
+File1 v2

0 commit comments

Comments
 (0)