parse-git-diff
A parser for git diff.
Installation
npm install parse-git-diff
Demo
See online demo
Usage
import parseGitDiff from 'parse-git-diff'; // import
// or
const parseGitDiff = require('parse-git-diff').default; // require
import parseGitDiff from 'parse-git-diff';
const result = parseGitDiff('... git diff ...');
console.log(result);
// {
// "type": "GitDiff",
// "files": [
// {
// "type": "AddedFile",
// "chunks": [
// {
// "type": "Chunk",
// "toFileRange": {
// "start": 1,
// ...
// }
Options
noPrefix
(boolean)
Specifies whether the git diff command is used with the --no-prefix
option. (default: false
)
// git diff HEAD~3 --no-prefix
const result = parseGitDiff(DIFF, {
noPrefix: true,
});
Examples
New file diff
### Input
```diff
diff --git a/newfile.md b/newfile.md
new file mode 100644
index 0000000..aa39060
--- /dev/null
+++ b/newfile.md
@@ -0,0 +1 @@
+newfile
```
### Output
```json
{
"type": "GitDiff",
"files": [
{
"type": "AddedFile",
"chunks": [
{
"type": "Chunk",
"toFileRange": {
"start": 1,
"lines": 1
},
"fromFileRange": {
"start": 0,
"lines": 0
},
"changes": [
{
"type": "AddedLine",
"lineAfter": 1,
"content": "newfile"
}
]
}
],
"path": "newfile.md"
}
]
}
```
Deleted file diff
### Input
```diff
diff --git a/newfile.md b/newfile.md
deleted file mode 100644
index aa39060..0000000
--- a/newfile.md
+++ /dev/null
@@ -1 +0,0 @@
-newfile
```
### Output
```json
{
"type": "GitDiff",
"files": [
{
"type": "DeletedFile",
"chunks": [
{
"type": "Chunk",
"toFileRange": {
"start": 0,
"lines": 0
},
"fromFileRange": {
"start": 1,
"lines": 1
},
"changes": [
{
"type": "DeletedLine",
"lineBefore": 1,
"content": "newfile"
}
]
}
],
"path": "newfile.md"
}
]
}
```
Renamed file diff
### Input
```diff
diff --git a/newfile.md b/rename.md
similarity index 100%
rename from newfile.md
rename to rename.md
```
### Output
```json
{
"type": "GitDiff",
"files": [
{
"type": "RenamedFile",
"pathAfter": "rename.md",
"pathBefore": "newfile.md",
"chunks": []
}
]
}
```
Conflict file diff
### Input
```diff
diff --cc README.md
index 2445f65,f4b8569..0000000
--- a/README.md
+++ b/README.md
@@@ -8,7 -8,7 +8,11 @@@
npm install parse-git-diff
++<<<<<<< HEAD
+## a
++=======
+ ## b
++>>>>>>> branch-b
- [demo](https://yeonjuan.github.io/parse-git-diff/)
```
### Output
```json
{
"type": "GitDiff",
"files": [
{
"type": "ChangedFile",
"chunks": [
{
"type": "CombinedChunk",
"fromFileRangeA": {
"start": 8,
"lines": 7
},
"fromFileRangeB": {
"start": 8,
"lines": 7
},
"toFileRange": {
"start": 8,
"lines": 11
},
"changes": [
{
"type": "UnchangedLine",
"lineBefore": 8,
"lineAfter": 8,
"content": " npm install parse-git-diff"
},
{
"type": "UnchangedLine",
"lineBefore": 9,
"lineAfter": 9,
"content": " "
},
{
"type": "UnchangedLine",
"lineBefore": 10,
"lineAfter": 10,
"content": " "
},
{
"type": "AddedLine",
"lineAfter": 11,
"content": "+<<<<<<< HEAD"
},
{
"type": "UnchangedLine",
"lineBefore": 11,
"lineAfter": 12,
"content": "+## a"
},
{
"type": "AddedLine",
"lineAfter": 13,
"content": "+======="
},
{
"type": "AddedLine",
"lineAfter": 14,
"content": " ## b"
},
{
"type": "AddedLine",
"lineAfter": 15,
"content": "+>>>>>>> branch-b"
},
{
"type": "UnchangedLine",
"lineBefore": 12,
"lineAfter": 16,
"content": " "
},
{
"type": "UnchangedLine",
"lineBefore": 13,
"lineAfter": 17,
"content": " - [demo](https://yeonjuan.github.io/parse-git-diff/)"
},
{
"type": "UnchangedLine",
"lineBefore": 14,
"lineAfter": 18,
"content": " "
},
{
"type": "UnchangedLine",
"lineBefore": 15,
"lineAfter": 19,
"content": " "
}
]
}
],
"path": "README.md"
}
]
}
```
New line file diff
### Input
```diff
diff --git a/rename.md b/rename.md
index aa39060..0e05564 100644
--- a/rename.md
+++ b/rename.md
@@ -1 +1,2 @@
newfile
+newline
```
### Output
```json
{
"type": "GitDiff",
"files": [
{
"type": "ChangedFile",
"chunks": [
{
"type": "Chunk",
"toFileRange": {
"start": 1,
"lines": 2
},
"fromFileRange": {
"start": 1,
"lines": 1
},
"changes": [
{
"type": "UnchangedLine",
"lineBefore": 1,
"lineAfter": 1,
"content": "newfile"
},
{
"type": "AddedLine",
"lineAfter": 2,
"content": "newline"
}
]
}
],
"path": "rename.md"
}
]
}
```
Deleted line file diff
### Input
```diff
diff --git a/rename.md b/rename.md
index 0e05564..aa39060 100644
--- a/rename.md
+++ b/rename.md
@@ -1,2 +1 @@
newfile
-newline
```
### Output
```json
{
"type": "GitDiff",
"files": [
{
"type": "ChangedFile",
"chunks": [
{
"type": "Chunk",
"toFileRange": {
"start": 1,
"lines": 1
},
"fromFileRange": {
"start": 1,
"lines": 2
},
"changes": [
{
"type": "UnchangedLine",
"lineBefore": 1,
"lineAfter": 1,
"content": "newfile"
},
{
"type": "DeletedLine",
"lineBefore": 2,
"content": "newline"
}
]
}
],
"path": "rename.md"
}
]
}
```
```diff
diff --git a/rename.md b/rename.md
index 0e05564..aa39060 100644
--- a/rename.md
+++ b/rename.md
@@ -1,2 +1 @@
newfile
-newline
+newline
\ No newline at end of file
diff --git a/rename2.md b/rename2.md
index 0e05564..aa39060 100644
--- a/rename2.md
+++ b/rename2.md
@@ -1,2 +1 @@
newfile2
-newline2
+newline2
\ No newline at end of file
```
```json
{
"type": "GitDiff",
"files": [
{
"type": "ChangedFile",
"chunks": [
{
"type": "Chunk",
"toFileRange": {
"start": 1,
"lines": 1
},
"fromFileRange": {
"start": 1,
"lines": 2
},
"changes": [
{
"type": "UnchangedLine",
"lineBefore": 1,
"lineAfter": 1,
"content": "newfile"
},
{
"type": "DeletedLine",
"lineBefore": 2,
"content": "newline"
},
{
"type": "AddedLine",
"lineAfter": 2,
"content": "newline"
},
{
"type": "MessageLine",
"content": "No newline at end of file"
}
]
}
],
"path": "rename.md"
},
{
"type": "ChangedFile",
"chunks": [
{
"type": "Chunk",
"toFileRange": {
"start": 1,
"lines": 1
},
"fromFileRange": {
"start": 1,
"lines": 2
},
"changes": [
{
"type": "UnchangedLine",
"lineBefore": 1,
"lineAfter": 1,
"content": "newfile2"
},
{
"type": "DeletedLine",
"lineBefore": 2,
"content": "newline2"
},
{
"type": "AddedLine",
"lineAfter": 2,
"content": "newline2"
},
{
"type": "MessageLine",
"content": "No newline at end of file"
}
]
}
],
"path": "rename2.md"
}
]
}
```
AST Format
See the types.ts file for all AST formats.
License
MIT