Registry / serialization / gitdiff-parser

gitdiff-parser

JSON →
library0.3.1jsnpmunverified

gitdiff-parser is a JavaScript/TypeScript library designed to programmatically parse the output of the standard `git diff` command into a structured data format. It aims for speed and reliability in processing various `git diff` outputs, including additions, deletions, modifications, and renames, extracting details like file paths, modes, hunks, and individual line changes. The current stable version is 0.3.1, indicating that it is in a pre-1.0 development phase where API changes might occur more frequently than in a major stable release. It provides a robust, typed interface for accessing file, hunk, and change-level details from raw diff strings, making it suitable for building tools that need to analyze or manipulate repository changes programmatically, such as code review tools, linters, or continuous integration systems.

npm install gitdiff-parser
INSTALL
IMPORT
SIG · GITDIFF-PARSER
G
gitdiff-parser
serializationjavascriptv0.3.1
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.

gitDiffParser
import gitDiffParser from 'gitdiff-parser';
import { gitDiffParser } from 'gitdiff-parser'; const gitDiffParser = require('gitdiff-parser');
The library primarily uses a default export pattern. While CommonJS `require` might work via transpilation or specific bundler configurations, direct usage in a pure CommonJS environment might require additional setup or lead to issues.
File, Hunk, Change
import type { File, Hunk, Change } from 'gitdiff-parser';
import { File, Hunk, Change } from 'gitdiff-parser';
These are TypeScript type definitions. Use `import type` to import them to ensure they are only used at compile-time and avoid potential runtime conflicts or erroneous value imports.
parse
import gitDiffParser from 'gitdiff-parser'; const files = gitDiffParser.parse(diffText);
import { parse } from 'gitdiff-parser';
The `parse` function is a method on the default-exported object, not a direct named export from the module root.

Demonstrates how to install the package, import the parser, feed it a sample `git diff` string, and inspect the structured output including files, hunks, and individual changes.

import gitDiffParser from 'gitdiff-parser'; import type { File, Hunk, Change } from 'gitdiff-parser'; const sampleDiff = `diff --git a/oldfile.txt b/newfile.txt index 80b9101..f356ed5 100644 --- a/oldfile.txt +++ b/newfile.txt @@ -1,3 +1,4 @@ Line 1 -Line 2 old +Line 2 new Line 3 +New line at end `; console.log('Parsing a sample git diff...'); const parsedFiles: File[] = gitDiffParser.parse(sampleDiff); console.log('Parsed Files:', JSON.stringify(parsedFiles, null, 2)); if (parsedFiles.length > 0) { const firstFile = parsedFiles[0]; console.log(`\nFirst file path: ${firstFile.newPath}`); console.log(`Type of change: ${firstFile.type}`); if (firstFile.hunks.length > 0) { const firstHunk = firstFile.hunks[0]; console.log(`Hunk content preview: "${firstHunk.content.trim().substring(0, 50)}..."`); console.log(`Number of changes in first hunk: ${firstHunk.changes.length}`); firstHunk.changes.forEach((change: Change, index: number) => { console.log(` Change ${index + 1}: Type='${change.type}', Content='${change.content.trim()}'`); }); } }
Debug
Known issues
breakingAs a pre-1.0 package (version 0.3.1), the API is not yet stable. Minor version bumps (e.g., 0.x.y to 0.a.b where a > x) may introduce breaking changes without a major version increment, requiring careful review of release notes upon upgrade.
fix
Always pin to exact versions (e.g., `"gitdiff-parser": "0.3.1"`) and review the changelog thoroughly before upgrading. Consider using tools like `npm-check-updates` with care.
affects: >=0.0.1
gotchaThe parser's reliability is dependent on the input `git diff` string strictly adhering to a standard format. Variations due to different Git versions, custom diff configurations, unusual line endings, or malformed inputs can lead to incomplete or incorrect parsing results.
fix
Ensure the `git diff` command used to generate the input produces standard, clean output. Test with simplified diffs to isolate parsing issues. If encountering consistent problems with seemingly valid diffs, report them to the library's issue tracker with the problematic diff content.
affects: >=0.0.1
gotchaParsing extremely large `git diff` outputs (e.g., thousands of lines or numerous files) can be memory intensive and potentially impact application performance, especially in environments with limited resources.
fix
For very large diffs, consider strategies such as chunking the diff input if feasible, processing diffs in a separate worker thread, or optimizing the source of the diff to only include relevant changes.
affects: >=0.0.1
Errors
Common errors & fixes
Error: Cannot find module 'gitdiff-parser' from 'your-project-path'
Attempting to use `require()` in an ES Module context or incorrect module resolution setup for CommonJS.
fix
Ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`) and use `import gitDiffParser from 'gitdiff-parser';`. If you must use CommonJS, you might need a bundler like Webpack or Rollup, or ensure your `tsconfig.json` (for TypeScript) is configured with `"module": "NodeNext"` or `"CommonJS"` and appropriate `moduleResolution`.
TypeError: gitDiffParser.parse is not a function
Incorrect import statement, typically trying to destructure the default export (e.g., `import { parse } from 'gitdiff-parser';`).
fix
The `parse` function is a method of the default export object. Use `import gitDiffParser from 'gitdiff-parser';` then call it via `gitDiffParser.parse(...)`.
SyntaxError: Named export 'File' not found. The requested module 'gitdiff-parser' does not provide an export named 'File'
Attempting to import TypeScript type definitions as if they were named value exports.
fix
When importing types, use the `import type` syntax: `import type { File, Hunk, Change } from 'gitdiff-parser';`.
Parser returns an empty array or incorrect results for a seemingly valid diff.
The input `git diff` string might contain non-standard formatting, specific Git features not fully supported by the parser yet, or be malformed in a subtle way.
fix
Double-check the exact output of your `git diff` command. Try simplifying the diff content to isolate the problematic part. Verify line endings and encoding. If the issue persists, file a bug report on the library's GitHub repository, including the exact diff string that causes the problem.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
gitdiff-parser — npm install gitdiff-parser · libregistry