{"id":13234,"library":"gitdiff-parser","title":"Git Diff Parser","description":"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.","status":"active","version":"0.3.1","language":"javascript","source_language":"en","source_url":"https://github.com/ecomfe/gitdiff-parser","tags":["javascript","typescript"],"install":[{"cmd":"npm install gitdiff-parser","lang":"bash","label":"npm"},{"cmd":"yarn add gitdiff-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add gitdiff-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { gitDiffParser } from 'gitdiff-parser';\nconst gitDiffParser = require('gitdiff-parser');","symbol":"gitDiffParser","correct":"import gitDiffParser from 'gitdiff-parser';"},{"note":"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.","wrong":"import { File, Hunk, Change } from 'gitdiff-parser';","symbol":"File, Hunk, Change","correct":"import type { File, Hunk, Change } from 'gitdiff-parser';"},{"note":"The `parse` function is a method on the default-exported object, not a direct named export from the module root.","wrong":"import { parse } from 'gitdiff-parser';","symbol":"parse","correct":"import gitDiffParser from 'gitdiff-parser';\nconst files = gitDiffParser.parse(diffText);"}],"quickstart":{"code":"import gitDiffParser from 'gitdiff-parser';\nimport type { File, Hunk, Change } from 'gitdiff-parser';\n\nconst sampleDiff = `diff --git a/oldfile.txt b/newfile.txt\nindex 80b9101..f356ed5 100644\n--- a/oldfile.txt\n+++ b/newfile.txt\n@@ -1,3 +1,4 @@\n Line 1\n-Line 2 old\n+Line 2 new\n Line 3\n+New line at end\n`;\n\nconsole.log('Parsing a sample git diff...');\n\nconst parsedFiles: File[] = gitDiffParser.parse(sampleDiff);\n\nconsole.log('Parsed Files:', JSON.stringify(parsedFiles, null, 2));\n\nif (parsedFiles.length > 0) {\n  const firstFile = parsedFiles[0];\n  console.log(`\\nFirst file path: ${firstFile.newPath}`);\n  console.log(`Type of change: ${firstFile.type}`);\n  if (firstFile.hunks.length > 0) {\n    const firstHunk = firstFile.hunks[0];\n    console.log(`Hunk content preview: \"${firstHunk.content.trim().substring(0, 50)}...\"`);\n    console.log(`Number of changes in first hunk: ${firstHunk.changes.length}`);\n    firstHunk.changes.forEach((change: Change, index: number) => {\n      console.log(`  Change ${index + 1}: Type='${change.type}', Content='${change.content.trim()}'`);\n    });\n  }\n}","lang":"typescript","description":"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."},"warnings":[{"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.","message":"As 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.","severity":"breaking","affected_versions":">=0.0.1"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=0.0.1"},{"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.","message":"Parsing 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.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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`.","cause":"Attempting to use `require()` in an ES Module context or incorrect module resolution setup for CommonJS.","error":"Error: Cannot find module 'gitdiff-parser' from 'your-project-path'"},{"fix":"The `parse` function is a method of the default export object. Use `import gitDiffParser from 'gitdiff-parser';` then call it via `gitDiffParser.parse(...)`.","cause":"Incorrect import statement, typically trying to destructure the default export (e.g., `import { parse } from 'gitdiff-parser';`).","error":"TypeError: gitDiffParser.parse is not a function"},{"fix":"When importing types, use the `import type` syntax: `import type { File, Hunk, Change } from 'gitdiff-parser';`.","cause":"Attempting to import TypeScript type definitions as if they were named value exports.","error":"SyntaxError: Named export 'File' not found. The requested module 'gitdiff-parser' does not provide an export named 'File'"},{"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.","cause":"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.","error":"Parser returns an empty array or incorrect results for a seemingly valid diff."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}