Skip to content

Patch.changed_lines method's numbering falsely includes lines like \ No newline at end of file #335

Description

@radomirbosak

This has a side-effect that line numbers are shifted by +1 if the patch is near end of the file which does not have a trailing newline.

How to reproduce

If we have this unified diff:

diff --git a/file.txt b/file.txt
index 2ae21dc..0ce0636 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1 @@
-First
\ No newline at end of file
+Second

And we inspect the changed_lines of this patch:

require "git_diff_parser"

output = File.open("file.diff").read

patches = GitDiffParser.parse(output)
puts patches[0].changed_lines.inspect

We will get this output

[#<GitDiffParser::Line:0x00005602fda5fd88 @number=2, @content="+Second\n", @patch_position=3>]

Expectation:

@number=1

Reality:

@number=2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions