diff --git a/lib/containers/pr-changed-files-container.js b/lib/containers/pr-changed-files-container.js index ece3bf4a82..beb3c5d38b 100644 --- a/lib/containers/pr-changed-files-container.js +++ b/lib/containers/pr-changed-files-container.js @@ -59,7 +59,16 @@ export default class PullRequestChangedFilesContainer extends React.Component { } buildPatch(rawDiff) { - const diffs = parseDiff(rawDiff); + const diffs = parseDiff(rawDiff).map(diff => { + // diff coming from API will have the defaul git diff prefixes a/ and b/ + // e.g. a/file1.js and b/file2.js + // see https://git-scm.com/docs/git-diff#_generating_patches_with_p + return { + ...diff, + newPath: diff.newPath ? diff.newPath.replace(/^[a|b]\//, '') : diff.newPath, + oldPath: diff.oldPath ? diff.oldPath.replace(/^[a|b]\//, '') : diff.oldPath, + }; + }); return buildMultiFilePatch(diffs); } diff --git a/test/containers/pr-changed-files-container.test.js b/test/containers/pr-changed-files-container.test.js index bcbe406bd9..af252cb56c 100644 --- a/test/containers/pr-changed-files-container.test.js +++ b/test/containers/pr-changed-files-container.test.js @@ -2,7 +2,7 @@ import React from 'react'; import {shallow} from 'enzyme'; import {parse as parseDiff} from 'what-the-diff'; -import rawDiff from '../fixtures/diffs/raw-diff'; +import {rawDiff, rawDiffWithPathPrefix} from '../fixtures/diffs/raw-diff'; import {buildMultiFilePatch} from '../../lib/models/patch'; import {getEndpoint} from '../../lib/models/endpoint'; @@ -72,6 +72,13 @@ describe('PullRequestChangedFilesContainer', function() { assert.strictEqual(diffURL, 'https://api.github.com/repos/smashwilson/pushbot/pulls/12'); }); + it('builds multifilepatch without the a/ and b/ prefixes in file paths', function() { + const wrapper = shallow(buildApp()); + const {filePatches} = wrapper.instance().buildPatch(rawDiffWithPathPrefix); + assert.notMatch(filePatches[0].newFile.path, /^[a|b]\//); + assert.notMatch(filePatches[0].oldFile.path, /^[a|b]\//); + }); + it('passes loaded diff data through to the controller', async function() { const wrapper = shallow(buildApp({ token: '4321', diff --git a/test/fixtures/diffs/raw-diff.js b/test/fixtures/diffs/raw-diff.js index 81f56d896f..0d57194fec 100644 --- a/test/fixtures/diffs/raw-diff.js +++ b/test/fixtures/diffs/raw-diff.js @@ -1,4 +1,5 @@ import dedent from 'dedent-js'; + const rawDiff = dedent` diff --git file.txt file.txt index 83db48f..bf269f4 100644 @@ -10,4 +11,15 @@ const rawDiff = dedent` +new line line3 `; -export default rawDiff; +const rawDiffWithPathPrefix = dedent` + diff --git a/badpath.txt b/badpath.txt + index af607bb..cfac420 100644 + --- a/badpath.txt + +++ b/badpath.txt + @@ -1,2 +1,3 @@ + line0 + -line1 + +line1.5 + +line2 +`; +export {rawDiff, rawDiffWithPathPrefix};