Simple parser for Diff files (unified diff format)
- Parse a diff file from its filepath
- Parse a diff directly from a string
- Error reporting: If there are parsing errors, the partial result is returned and the list of detected errors including line numbers
Given a filepath it returns a Promise
that resolves to a JS object with the diff content parsed and easily accessible programmatically.
const parser = require('@tandil/diffparse');
parser.parseDiffFile('./examples/example1.diff').then(diff => {
console.log(diff);
});
const parser = require('@tandil/diffparse');
const diff = parser.parseDiffFileSync('./examples/example1.diff');
console.log(diff);
Given a diff string it returns a JS object with the diff content parsed and easily accessible programmatically.
const parser = require('@tandil/diffparse');
const diffStr = `diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2ccbe46
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/node_modules/`;
const diff = parser.parseDiffString(diffStr);
console.log(diff);
Outputs
{
header: [],
files: [
{
header: 'diff --git a/.gitignore b/.gitignore',
fileMode: 'new file mode 100644',
oldMode: null,
newMode: null,
index: 'index 0000000..2ccbe46',
oldFile: '--- /dev/null',
newFile: '+++ b/.gitignore',
chunks: [
{
header: '@@ -0,0 +1 @@',
content: [
'+/node_modules/'
]
}
]
}
],
errors: []
}
Feel free to report any issues or feature request in Github repo.