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.

serializationdevops
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.

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.

import gitDiffParser 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.

import type { File, Hunk, Change } from 'gitdiff-parser';

The `parse` function is a method on the default-exported object, not a direct named export from the module root.

import gitDiffParser from 'gitdiff-parser'; const files = gitDiffParser.parse(diffText);

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 footguns
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.
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.
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.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
9 hits · last 30 days
gptbot
4
ahrefsbot
3
script
1
chatgpt-user
1
Resources