Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 0e013b6

Browse files
authored
Merge pull request #1855 from atom/vy/fix-wrong-prefix
Fix: wrong prefix on PR files changed
2 parents 278e871 + 0d1f597 commit 0e013b6

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/containers/pr-changed-files-container.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ export default class PullRequestChangedFilesContainer extends React.Component {
5959
}
6060

6161
buildPatch(rawDiff) {
62-
const diffs = parseDiff(rawDiff);
62+
const diffs = parseDiff(rawDiff).map(diff => {
63+
// diff coming from API will have the defaul git diff prefixes a/ and b/
64+
// e.g. a/file1.js and b/file2.js
65+
// see https://git-scm.com/docs/git-diff#_generating_patches_with_p
66+
return {
67+
...diff,
68+
newPath: diff.newPath ? diff.newPath.replace(/^[a|b]\//, '') : diff.newPath,
69+
oldPath: diff.oldPath ? diff.oldPath.replace(/^[a|b]\//, '') : diff.oldPath,
70+
};
71+
});
6372
return buildMultiFilePatch(diffs);
6473
}
6574

test/containers/pr-changed-files-container.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import {shallow} from 'enzyme';
33
import {parse as parseDiff} from 'what-the-diff';
44

5-
import rawDiff from '../fixtures/diffs/raw-diff';
5+
import {rawDiff, rawDiffWithPathPrefix} from '../fixtures/diffs/raw-diff';
66
import {buildMultiFilePatch} from '../../lib/models/patch';
77
import {getEndpoint} from '../../lib/models/endpoint';
88

@@ -72,6 +72,13 @@ describe('PullRequestChangedFilesContainer', function() {
7272
assert.strictEqual(diffURL, 'https://api.github.com/repos/smashwilson/pushbot/pulls/12');
7373
});
7474

75+
it('builds multifilepatch without the a/ and b/ prefixes in file paths', function() {
76+
const wrapper = shallow(buildApp());
77+
const {filePatches} = wrapper.instance().buildPatch(rawDiffWithPathPrefix);
78+
assert.notMatch(filePatches[0].newFile.path, /^[a|b]\//);
79+
assert.notMatch(filePatches[0].oldFile.path, /^[a|b]\//);
80+
});
81+
7582
it('passes loaded diff data through to the controller', async function() {
7683
const wrapper = shallow(buildApp({
7784
token: '4321',

test/fixtures/diffs/raw-diff.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dedent from 'dedent-js';
2+
23
const rawDiff = dedent`
34
diff --git file.txt file.txt
45
index 83db48f..bf269f4 100644
@@ -10,4 +11,15 @@ const rawDiff = dedent`
1011
+new line
1112
line3
1213
`;
13-
export default rawDiff;
14+
const rawDiffWithPathPrefix = dedent`
15+
diff --git a/badpath.txt b/badpath.txt
16+
index af607bb..cfac420 100644
17+
--- a/badpath.txt
18+
+++ b/badpath.txt
19+
@@ -1,2 +1,3 @@
20+
line0
21+
-line1
22+
+line1.5
23+
+line2
24+
`;
25+
export {rawDiff, rawDiffWithPathPrefix};

0 commit comments

Comments
 (0)