Registry / serialization / git-diff-parser

git-diff-parser

JSON →
library1.0.0jsnpmunverified

git-diff-parser is a minimalist JavaScript library designed for parsing the output of `git diff` and `git format-patch` commands into a structured object representation. It provides a way to programmatically analyze changes, including commit details, file additions/deletions, renames, and line-by-line diff information. Released as version 1.0.0, the package appears to have seen no significant updates since its initial creation around 2014, as indicated by the license date and README notes. The author explicitly states it was created quickly with "no error handling or tests of any sorts implemented yet," suggesting it is a raw utility rather than a robust, production-ready parser. This also implies an irregular, likely non-existent, release cadence. Its primary differentiator is its simplicity and directness in parsing, suitable for quick, personal scripting where robust error handling is not critical. Alternatives often offer more comprehensive parsing, testing, and active maintenance.

npm install git-diff-parser
INSTALL
IMPORT
SIG · GIT-DIFF-PARSER
G
git-diff-parser
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parser
import parser from 'git-diff-parser';
import { parser } from 'git-diff-parser';
The package exports a default function. While primarily designed for CommonJS, modern bundlers can often resolve this to a default ESM import.
parser (CommonJS)
const parser = require('git-diff-parser');
const { parser } = require('git-diff-parser');
This is the original and intended usage pattern for the package, which exposes the parsing function as its module.exports.

This quickstart demonstrates how to parse a raw Git diff string using `git-diff-parser` and iterate through the resulting commit and file objects to display changes.

import parser from 'git-diff-parser'; import { readFileSync } from 'fs'; // Simulate a simple diff file. In a real scenario, you'd read a .diff file // const diffContent = readFileSync('some.diff', 'utf8'); const diffContent = `diff --git a/oldfile.js b/newfile.js index 1234567..abcdef0 100644 --- a/oldfile.js +++ b/newfile.js @@ -1,4 +1,5 @@ function oldFunc() { console.log('hello'); - console.log('world'); +} +function newFunc() { + console.log('new world'); }`; const diff = parser(diffContent); console.log(`Parsed ${diff.commits.length} commit(s).`); diff.commits.forEach((commit, commitIdx) => { console.log(`\n--- Commit ${commitIdx + 1} ---`); console.log(`SHA: ${commit.sha || 'N/A'}`); console.log(`Author: ${commit.author} <${commit.email}>`); console.log(`Message: ${commit.message.split('\n')[0]}`); commit.files.forEach((file, fileIdx) => { console.log(` File ${fileIdx + 1}: ${file.name}`); if (file.added) console.log(' Status: Added'); if (file.deleted) console.log(' Status: Deleted'); if (file.renamed) console.log(` Status: Renamed from ${file.oldName}`); if (file.binary) console.log(' Type: Binary'); if (!file.binary) { file.lines.forEach(line => { if (line.type === 'added') console.log(` + ${line.text}`); if (line.type === 'deleted') console.log(` - ${line.text}`); if (line.type === 'normal') console.log(` ${line.text}`); }); } }); });
Debug
Known issues
gotchaThe package explicitly states "No error handling or tests of any sorts implemented yet." This means malformed or unexpected diff formats can lead to crashes, incorrect parsing, or unhandled exceptions, making it unsuitable for production environments without significant wrapping and validation.
fix
Implement custom error handling around the parser calls, validate input diff strings, and thoroughly test with expected and unexpected diff formats relevant to your use case.
affects: >=1.0.0
gotchaThe package was last updated around 2014 (based on the license date). It is not actively maintained, which means it may not be compatible with newer Git diff formats, modern JavaScript features, or security practices. There's no support for future issues or vulnerabilities.
fix
For critical applications, consider using actively maintained alternatives or be prepared to fork and maintain the library yourself to ensure compatibility and security.
affects: >=1.0.0
gotchaThe package is primarily CommonJS (`require()`). While bundlers might allow `import` syntax, it's not native ESM. This could lead to interoperability issues or increased bundle sizes in pure ESM environments without proper configuration.
fix
Use `const parser = require('git-diff-parser');` in Node.js environments. If using with ESM, ensure your build setup (e.g., Webpack, Rollup, esbuild) correctly handles CommonJS module interoperability.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'split') OR 'text' of null
The parser encountered an unexpected line format in the diff, likely due to a malformed diff string or a Git version producing a new format it doesn't understand.
fix
Ensure the input diff string is valid and adheres to standard `git diff` output. If the issue persists with valid diffs, the parser itself may not support that specific format. Consider pre-processing the diff or switching to a more robust parser.
ReferenceError: require is not defined in ES module scope
Attempting to use `require('git-diff-parser')` in an ECMAScript Module (ESM) context (e.g., a file with `"type": "module"` in package.json, or an `.mjs` file).
fix
Use the ESM import syntax: `import parser from 'git-diff-parser';`. If the package only provides CommonJS and you must use `require` in an ESM context, you might need to use `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const parser = require('git-diff-parser');` (Node.js specific).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
git-diff-parser — npm install git-diff-parser · libregistry