sergeyt/parse-diff

`chunk` lines are parsed as `normal` when parsing diffs w/ single line files

scottopherson opened this issue · 1 comments

chunk lines are parsed incorrectly when parsing diffs with single line files.

http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html#Detailed%20Unified

If a hunk contains just one line, only its start line number appears. Otherwise its line numbers look like ‘start,count’

Parsing this single line diff:

diff --git a/file1 b/file1
new file mode 100644
index 0000000..db81be4
--- /dev/null
+++ b/file1
@@ -0,0 +1 @@
+line1

produces the following result:

[
  {
    "lines": [
      {
        "type": "normal",
        "normal": true,
        "ln1": 0,
        "ln2": 0,
        "content": "@@ -0,0 +1 @@"
      },
      {
        "type": "add",
        "add": true,
        "ln": 1,
        "content": "+line1`"
      }
    ],
    "deletions": 0,
    "additions": 1,
    "new": true,
    "index": [
      "0000000..db81be4"
    ],
    "from": "\/dev\/null" ,
    "to": "file1"
  }
]

The first line should be a chunk line instead of a normal line:

[
  {
    "lines": [
      {
        "type": "chunk",
        "chunk": true,
        "content": "@@ -0,0 +1 @@"
      },
      {
        "type": "add",
        "add": true,
        "ln": 1,
        "content": "+line1`"
      }
    ],
    "deletions": 0,
    "additions": 1,
    "new": true,
    "index": [
      "0000000..db81be4"
    ],
    "from": "\/dev\/null" ,
    "to": "file1"
  }
]

Adjusting the chunk regex to handle single line formats should fix the issue.

@scottopherson the fix is released in v0.0.15.