Skip to content

Commit

Permalink
Don't crash while trying to parse a diff for binary files, fix BetaHu…
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Aug 23, 2022
1 parent e84bc1e commit df0d160
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21656,9 +21656,15 @@ class Git {
// split diff into separate entries for separate files. \ndiff --git should be a reliable way to detect the separation, as content of files is always indented
return `\n${ string }`.split('\ndiff --git').slice(1).reduce((resultDict, fileDiff) => {
const lines = fileDiff.split('\n')
const lastHeaderLineIndex = lines.findIndex((line) => line.startsWith('+++'))
if (lines.length === 0) return resultDict; // ignore binary files

const lastHeaderLineIndex = lines.findIndex((line) => line && line.startsWith('+++'))
const plainDiff = lines.slice(lastHeaderLineIndex + 1).join('\n').trim()
let filePath = ''

console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
console.log(lastHeaderLineIndex)

if (lines[lastHeaderLineIndex].startsWith('+++ b/')) { // every file except removed files
filePath = lines[lastHeaderLineIndex].slice(6) // remove '+++ b/'
} else { // for removed file need to use header line with filename before deletion
Expand Down
8 changes: 7 additions & 1 deletion src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,15 @@ class Git {
// split diff into separate entries for separate files. \ndiff --git should be a reliable way to detect the separation, as content of files is always indented
return `\n${ string }`.split('\ndiff --git').slice(1).reduce((resultDict, fileDiff) => {
const lines = fileDiff.split('\n')
const lastHeaderLineIndex = lines.findIndex((line) => line.startsWith('+++'))
if (lines.length === 0) return resultDict; // ignore binary files

const lastHeaderLineIndex = lines.findIndex((line) => line && line.startsWith('+++'))
const plainDiff = lines.slice(lastHeaderLineIndex + 1).join('\n').trim()
let filePath = ''

console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
console.log(lastHeaderLineIndex)

if (lines[lastHeaderLineIndex].startsWith('+++ b/')) { // every file except removed files
filePath = lines[lastHeaderLineIndex].slice(6) // remove '+++ b/'
} else { // for removed file need to use header line with filename before deletion
Expand Down

0 comments on commit df0d160

Please sign in to comment.