Skip to content

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

diffparser

Tool to convert the output of git diff to JSON for further processing.

Please note, for the most part this is an exercise of parsing simple structured text in Haskell and I do not promise support or feature-completeness.

Example

Consider a git diff output looking as such:

diff --git a/factorial.py b/factorial.py
index abcdef1..1234567 100644
--- a/factorial.py
+++ b/factorial.py
@@ -1,6 +1,7 @@
 def factorial(n):
+    # Calculate the factorial of a number recursively
     if n == 0:
         return 1
     else:
-        return n * factorial(n - 1)
+        return n * factorial(n - 1)
 
-num = 5
+num = 6
 print(f"The factorial of {num} is {factorial(num)}")

We can pipe this output to diffparser binary and jq for formatting:

git diff | diffparser | jq

Output:

[
  {
    "oldName": "factorial.py",
    "newName": "factorial.py",
    "index": "index abcdef1..1234567 100644",
    "hunks": [
      {
        "oldRange": {
          "from": 1,
          "range": 6
        },
        "newRange": {
          "from": 1,
          "range": 7
        },
        "lines": [
          {
            "type": "context",
            "line": "def factorial(n):"
          },
          {
            "type": "added",
            "line": "    # Calculate the factorial of a number recursively"
          },
          {
            "type": "context",
            "line": "    if n == 0:"
          },
          {
            "type": "context",
            "line": "        return 1"
          },
          {
            "type": "context",
            "line": "    else:"
          },
          {
            "type": "removed",
            "line": "        return n * factorial(n - 1)"
          },
          {
            "type": "added",
            "line": "        return n * factorial(n - 1)"
          },
          {
            "type": "context",
            "line": ""
          },
          {
            "type": "removed",
            "line": "num = 5"
          },
          {
            "type": "added",
            "line": "num = 6"
          },
          {
            "type": "context",
            "line": "print(f\"The factorial of {num} is {factorial(num)}\")"
          }
        ]
      }
    ]
  }
]

About

Tool to convert the output of git diff to JSON for further processing

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages