Coder Social home page Coder Social logo

gitdiff-parser's Introduction

gitdiff-parser

A fast and reliable git diff parser.

Install

npm install gitdiff-parser

Usage

import gitDiffParser from 'gitdiff-parser';

gitDiffParser.parse(gitDiffText);

gitDiffText should be a diff output by git diff command.

API

export interface Change {
    content: string;
    type: 'insert' | 'delete' | 'normal';
    isInsert?: boolean;
    isDelete?: boolean;
    isNormal?: boolean;
    lineNumber?: number;
    oldLineNumber?: number;
    newLineNumber?: number;
}

export interface Hunk {
    content: string;
    oldStart: number;
    newStart: number;
    oldLines: number;
    newLines: number;
    changes: Change[];
}

export interface File {
    hunks: Hunk[];
    oldEndingNewLine: boolean;
    newEndingNewLine: boolean;
    oldMode: string;
    newMode: string;
    similarity?: number;
    oldRevision: string;
    newRevision: string;
    oldPath: string;
    newPath: string;
    isBinary?: boolean;
    type: 'add' | 'delete' | 'modify'  'rename';
}

export default {
    parse(source: string): File[];
};

gitdiff-parser's People

Contributors

erik168 avatar errorrik avatar otakustay avatar sdorra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gitdiff-parser's Issues

换行符变动丢失

原始 diff 信息:

n-\n const onRequest: PurchaseContractWithGoodsExtendTableCombineProps<any>['onRequest'] =\n   value => {\n     return {\n       contractIds: value.map(item => item.id).toString()\n     }\n   }\n-\n
-
 const onRequest: PurchaseContractWithGoodsExtendTableCombineProps<any>['onRequest'] =
   value => {
     return {
       contractIds: value.map(item => item.id).toString()
     }
   }
-

解析出来的 change 中 content 值应该为\n但是现在是""

var lines = source.split('\n');

This package has type errors

If I install this package and run the type checker, a couple errors are detected in the package itself:

node_modules/.pnpm/[email protected]/node_modules/gitdiff-parser/index.d.ts:53:16 - error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.

53 export default {
                  ~
54     parse(source: string): File[];
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 };
   ~

node_modules/.pnpm/[email protected]/node_modules/gitdiff-parser/index.d.ts:54:34 - error TS1005:
 '{' expected.

54     parse(source: string): File[];
                                    ~


Found 2 errors in the same file, starting at: node_modules/.pnpm/[email protected]/node_modules/gitdiff-parser/index.d.ts:53

There's a PR to fix this (#27) but it's been stale for 9 months for some reason. Can we get this fixed?

`parseDiff` throws exception when these's no diff.

I am getting exception when the gitDiffText is non-empty but without any changes. For example:

When the gitDiffText is:

Diff between local code and base commit 657e12c36bfbf6029d8067f4d77bd19aee29c4ea (on branch xxx/xxx):
--

I get exception:
image

Paths undefined for empty added/removed files

This library does not correctly parse git diffs for empty files that are added or removed. Below are sample diffs and the resulting parse() output:

Adding an empty file

diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..7898192
{
  "hunks": [],
  "oldEndingNewLine": true,
  "newEndingNewLine": true,
  "newMode": "100644",
  "oldRevision": "0000000",
  "newRevision": "7898192",
  "type": "modify"
}

Removing an empty file

diff --git a/a.txt b/a.txt
deleted file mode 100644
index 7898192..0000000
{
  "hunks": [],
  "oldEndingNewLine": true,
  "newEndingNewLine": true,
  "oldMode": "100644",
  "oldRevision": "7898192",
  "newRevision": "0000000",
  "type": "modify"
}

bug: Accessing `String.slice` on an undefined value

Expected Behavior

Should parse the diff without any errors or handle the errors gracefully.

Current Behavior

Throws an error when the diff is incorrect

Possible Solution

Check if the string is defined before accessing the .slice method.

Steps to Reproduce

  1. Pass the following diff to gitDiffParser.parse method
'diff --git \nindex 9c96b34..853bddf 100644\n---@@ -1,8 +1,19 @@\n-// write your code here!\n const solution = () => {\n-  // global clear all timeout:\n+  const allT = [];\n+  const old = setTimeout;\n+  window.setTimeout = (func, delay) => {\n+    const realTimeout = old(func, delay);\n+    allT.push(realTimeout);\n+    return realTimeout;\n+  };\n+  window.clearAllTimouts = () => {\n+    while (allT.length) {\n+      clearTimeout(allT.pop());\n+    }\n+  };\n   cat = () => {\n-  }\n+    window.clearAllTimouts();\n+  };\n };\n \n module.exports = solution;'

It will throw:

TypeError: Cannot read properties of undefined (reading 'slice')

at parsePathFromFirstLine (node_modules/gitdiff-parser/index.js:21:35)

Context

It started happening after upgrading our app package to version 0.3.1 and I believe the issue started happening after this commit has been merged. The lines causing the error are highlighted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.