npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-git-diff

v0.0.16

Published

A parser for git diff

Downloads

15,416

Readme

action status npm version license codecov

parse-git-diff

A parser for git diff.

Installation

npm install parse-git-diff

Demo

See online demo

Usage

import parseGitDiff from 'parse-git-diff'; // import
// or
const parseGitDiff = require('parse-git-diff').default; // require
import parseGitDiff from 'parse-git-diff';

const result = parseGitDiff('... git diff ...');

console.log(result);
// {
//   "type": "GitDiff",
//   "files": [
//     {
//       "type": "AddedFile",
//       "chunks": [
//         {
//           "type": "Chunk",
//           "toFileRange": {
//             "start": 1,
// ...
// }

Options

noPrefix (boolean)

Specifies whether the git diff command is used with the --no-prefix option. (default: false)

// git diff HEAD~3 --no-prefix

const result = parseGitDiff(DIFF, {
  noPrefix: true,
});

Examples

Input

diff --git a/newfile.md b/newfile.md
new file mode 100644
index 0000000..aa39060
--- /dev/null
+++ b/newfile.md
@@ -0,0 +1 @@
+newfile

Output

{
  "type": "GitDiff",
  "files": [
    {
      "type": "AddedFile",
      "chunks": [
        {
          "type": "Chunk",
          "toFileRange": {
            "start": 1,
            "lines": 1
          },
          "fromFileRange": {
            "start": 0,
            "lines": 0
          },
          "changes": [
            {
              "type": "AddedLine",
              "lineAfter": 1,
              "content": "newfile"
            }
          ]
        }
      ],
      "path": "newfile.md"
    }
  ]
}

Input

diff --git a/newfile.md b/newfile.md
deleted file mode 100644
index aa39060..0000000
--- a/newfile.md
+++ /dev/null
@@ -1 +0,0 @@
-newfile

Output

{
  "type": "GitDiff",
  "files": [
    {
      "type": "DeletedFile",
      "chunks": [
        {
          "type": "Chunk",
          "toFileRange": {
            "start": 0,
            "lines": 0
          },
          "fromFileRange": {
            "start": 1,
            "lines": 1
          },
          "changes": [
            {
              "type": "DeletedLine",
              "lineBefore": 1,
              "content": "newfile"
            }
          ]
        }
      ],
      "path": "newfile.md"
    }
  ]
}

Input

diff --git a/newfile.md b/rename.md
similarity index 100%
rename from newfile.md
rename to rename.md

Output

{
  "type": "GitDiff",
  "files": [
    {
      "type": "RenamedFile",
      "pathAfter": "rename.md",
      "pathBefore": "newfile.md",
      "chunks": []
    }
  ]
}

Input

diff --cc README.md
index 2445f65,f4b8569..0000000
--- a/README.md
+++ b/README.md
@@@ -8,7 -8,7 +8,11 @@@
  npm install parse-git-diff


++<<<<<<< HEAD
 +## a
++=======
+ ## b
++>>>>>>> branch-b

  - [demo](https://yeonjuan.github.io/parse-git-diff/)

Output

{
  "type": "GitDiff",
  "files": [
    {
      "type": "ChangedFile",
      "chunks": [
        {
          "type": "CombinedChunk",
          "fromFileRangeA": {
            "start": 8,
            "lines": 7
          },
          "fromFileRangeB": {
            "start": 8,
            "lines": 7
          },
          "toFileRange": {
            "start": 8,
            "lines": 11
          },
          "changes": [
            {
              "type": "UnchangedLine",
              "lineBefore": 8,
              "lineAfter": 8,
              "content": " npm install parse-git-diff"
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 9,
              "lineAfter": 9,
              "content": " "
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 10,
              "lineAfter": 10,
              "content": " "
            },
            {
              "type": "AddedLine",
              "lineAfter": 11,
              "content": "+<<<<<<< HEAD"
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 11,
              "lineAfter": 12,
              "content": "+## a"
            },
            {
              "type": "AddedLine",
              "lineAfter": 13,
              "content": "+======="
            },
            {
              "type": "AddedLine",
              "lineAfter": 14,
              "content": " ## b"
            },
            {
              "type": "AddedLine",
              "lineAfter": 15,
              "content": "+>>>>>>> branch-b"
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 12,
              "lineAfter": 16,
              "content": " "
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 13,
              "lineAfter": 17,
              "content": " - [demo](https://yeonjuan.github.io/parse-git-diff/)"
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 14,
              "lineAfter": 18,
              "content": " "
            },
            {
              "type": "UnchangedLine",
              "lineBefore": 15,
              "lineAfter": 19,
              "content": " "
            }
          ]
        }
      ],
      "path": "README.md"
    }
  ]
}

Input

diff --git a/rename.md b/rename.md
index aa39060..0e05564 100644
--- a/rename.md
+++ b/rename.md
@@ -1 +1,2 @@
 newfile
+newline

Output

{
  "type": "GitDiff",
  "files": [
    {
      "type": "ChangedFile",
      "chunks": [
        {
          "type": "Chunk",
          "toFileRange": {
            "start": 1,
            "lines": 2
          },
          "fromFileRange": {
            "start": 1,
            "lines": 1
          },
          "changes": [
            {
              "type": "UnchangedLine",
              "lineBefore": 1,
              "lineAfter": 1,
              "content": "newfile"
            },
            {
              "type": "AddedLine",
              "lineAfter": 2,
              "content": "newline"
            }
          ]
        }
      ],
      "path": "rename.md"
    }
  ]
}

Input

diff --git a/rename.md b/rename.md
index 0e05564..aa39060 100644
--- a/rename.md
+++ b/rename.md
@@ -1,2 +1 @@
 newfile
-newline

Output

{
  "type": "GitDiff",
  "files": [
    {
      "type": "ChangedFile",
      "chunks": [
        {
          "type": "Chunk",
          "toFileRange": {
            "start": 1,
            "lines": 1
          },
          "fromFileRange": {
            "start": 1,
            "lines": 2
          },
          "changes": [
            {
              "type": "UnchangedLine",
              "lineBefore": 1,
              "lineAfter": 1,
              "content": "newfile"
            },
            {
              "type": "DeletedLine",
              "lineBefore": 2,
              "content": "newline"
            }
          ]
        }
      ],
      "path": "rename.md"
    }
  ]
}
diff --git a/rename.md b/rename.md
index 0e05564..aa39060 100644
--- a/rename.md
+++ b/rename.md
@@ -1,2 +1 @@
 newfile
-newline
+newline
\ No newline at end of file
diff --git a/rename2.md b/rename2.md
index 0e05564..aa39060 100644
--- a/rename2.md
+++ b/rename2.md
@@ -1,2 +1 @@
 newfile2
-newline2
+newline2
\ No newline at end of file
{
  "type": "GitDiff",
  "files": [
    {
      "type": "ChangedFile",
      "chunks": [
        {
          "type": "Chunk",
          "toFileRange": {
            "start": 1,
            "lines": 1
          },
          "fromFileRange": {
            "start": 1,
            "lines": 2
          },
          "changes": [
            {
              "type": "UnchangedLine",
              "lineBefore": 1,
              "lineAfter": 1,
              "content": "newfile"
            },
            {
              "type": "DeletedLine",
              "lineBefore": 2,
              "content": "newline"
            },
            {
              "type": "AddedLine",
              "lineAfter": 2,
              "content": "newline"
            },
            {
              "type": "MessageLine",
              "content": "No newline at end of file"
            }
          ]
        }
      ],
      "path": "rename.md"
    },
    {
      "type": "ChangedFile",
      "chunks": [
        {
          "type": "Chunk",
          "toFileRange": {
            "start": 1,
            "lines": 1
          },
          "fromFileRange": {
            "start": 1,
            "lines": 2
          },
          "changes": [
            {
              "type": "UnchangedLine",
              "lineBefore": 1,
              "lineAfter": 1,
              "content": "newfile2"
            },
            {
              "type": "DeletedLine",
              "lineBefore": 2,
              "content": "newline2"
            },
            {
              "type": "AddedLine",
              "lineAfter": 2,
              "content": "newline2"
            },
            {
              "type": "MessageLine",
              "content": "No newline at end of file"
            }
          ]
        }
      ],
      "path": "rename2.md"
    }
  ]
}

AST Format

See the types.ts file for all AST formats.

License

MIT