diff --git a/src/__tests__/diff-parser-tests.ts b/src/__tests__/diff-parser-tests.ts index dde2778..5d07ee1 100644 --- a/src/__tests__/diff-parser-tests.ts +++ b/src/__tests__/diff-parser-tests.ts @@ -255,6 +255,22 @@ describe('DiffParser', () => { `); }); + it('should parse diff with a space in the file name', () => { + // git ends --- and +++ names that contain a space with a TAB + const diff = + 'diff --git a/My Docs/app.js b/My Docs/app.js\n' + + 'index 4c679d7..e9bd385 100644\n' + + '--- a/My Docs/app.js\t\n' + + '+++ b/My Docs/app.js\t\n' + + '@@ -1 +1,2 @@\n' + + ' const x = 1;\n' + + '+const y = 2;'; + const [file] = parse(diff); + expect(file.oldName).toBe('My Docs/app.js'); + expect(file.newName).toBe('My Docs/app.js'); + expect(file.language).toBe('js'); + }); + it('should parse diff with prefix', () => { const diff = 'diff --git "\tbla with \ttab.scala" "\tbla with \ttab.scala"\n' + diff --git a/src/diff-parser.ts b/src/diff-parser.ts index 83d9c63..0f9c3de 100644 --- a/src/diff-parser.ts +++ b/src/diff-parser.ts @@ -28,7 +28,9 @@ function getFilename(line: string, linePrefix?: string, extraPrefix?: string): s const [, filename = ''] = FilenameRegExp.exec(line) || []; const matchingPrefix = prefixes.find(p => filename.indexOf(p) === 0); - const fnameWithoutPrefix = matchingPrefix ? filename.slice(matchingPrefix.length) : filename; + const fnameWithoutPrefix = (matchingPrefix ? filename.slice(matchingPrefix.length) : filename) + // git ends ---/+++ names that contain a space with a TAB + .replace(/\t$/, ''); // Cleanup timestamps generated by the unified diff (diff command) as specified in // https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html