Status: [Utility/Tool]
A simple haskell tool to parse the output of git diff command to JSON format.
- Read unified
git diffoutput from standard input. - Parse file-level diffs and hunks.
- Emit a JSON representation of parsed diff chunks.
stack test
git diff | stack run- Install haskell tool stack using ghcup.
- Either run directly via
stack runor install it viastack installasGitDiff-exe. - Pipe the output via this tool
git diff | GitDiff-exe.
Consider the following output from git diff:
diff --git a/file1.txt b/file1.txt
index 168ed52..e41a4f5 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,6 +1,6 @@
This is a sample file.
-This line is changed.
+These lines are part of the original content.
Add a new line here.
Another change in content.
-Additional lines in the middle.
+More lines in the original file.
New content at the end of the file.
When run this output via GitDiff-exe we see the following JSON:
[
{
"chunks": [
{
"fromLineRange": {
"from": 1,
"length": 6
},
"lineDiffs": [
{
"ContextLine": "This is a sample file."
},
{
"RemovedLine": "This line is changed."
},
{
"AddedLine": "These lines are part of the original content."
},
{
"ContextLine": "Add a new line here."
},
{
"ContextLine": "Another change in content."
},
{
"RemovedLine": "Additional lines in the middle."
},
{
"AddedLine": "More lines in the original file."
},
{
"ContextLine": "New content at the end of the file."
}
],
"toLineRange": {
"from": 1,
"length": 6
}
}
],
"fromFile": "file1.txt",
"headers": [
"index 168ed52..e41a4f5 100644"
],
"toFile": "file1.txt"
}
]
stack test- This code does not handle many
git diffoutput variations. - The header section is not parsed, rather just kept as a list of strings.
- It cannot handle diffs between more than 2 files.