Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adf996e

Browse files
committed
Handle path separator normalization on Windows
1 parent 615df63 commit adf996e

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import {parse as parseDiff} from 'what-the-diff';
44

55
import {ItemTypePropType, EndpointPropType} from '../prop-types';
6+
import {toNativePathSep} from '../helpers';
67
import MultiFilePatchController from '../controllers/multi-file-patch-controller';
78
import LoadingView from '../views/loading-view';
89
import ErrorView from '../views/error-view';
@@ -60,13 +61,13 @@ export default class PullRequestChangedFilesContainer extends React.Component {
6061

6162
buildPatch(rawDiff) {
6263
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
64+
// diff coming from API will have the defaul git diff prefixes a/ and b/ and use *nix-style / path separators.
65+
// e.g. a/dir/file1.js and b/dir/file2.js
6566
// see https://git-scm.com/docs/git-diff#_generating_patches_with_p
6667
return {
6768
...diff,
68-
newPath: diff.newPath ? diff.newPath.replace(/^[a|b]\//, '') : diff.newPath,
69-
oldPath: diff.oldPath ? diff.oldPath.replace(/^[a|b]\//, '') : diff.oldPath,
69+
newPath: diff.newPath ? toNativePathSep(diff.newPath.replace(/^[a|b]\//, '')) : diff.newPath,
70+
oldPath: diff.oldPath ? toNativePathSep(diff.oldPath.replace(/^[a|b]\//, '')) : diff.oldPath,
7071
};
7172
});
7273
return buildMultiFilePatch(diffs);

lib/models/patch/multi-file-patch.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ export default class MultiFilePatch {
340340
}
341341

342342
getBufferRowForDiffPosition = (fileName, diffRow) => {
343-
// TODO verify that this works on Windows
344343
const {startBufferRow, index} = this.diffRowOffsetIndices.get(fileName);
345344
const {offset} = index.lowerBound({diffRow}).data();
346345
return startBufferRow + diffRow - offset;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import {shallow} from 'enzyme';
33
import {parse as parseDiff} from 'what-the-diff';
4+
import path from 'path';
45

56
import {rawDiff, rawDiffWithPathPrefix} from '../fixtures/diffs/raw-diff';
67
import {buildMultiFilePatch} from '../../lib/models/patch';
@@ -79,6 +80,13 @@ describe('PullRequestChangedFilesContainer', function() {
7980
assert.notMatch(filePatches[0].oldFile.path, /^[a|b]\//);
8081
});
8182

83+
it('converts file paths to use native path separators', function() {
84+
const wrapper = shallow(buildApp());
85+
const {filePatches} = wrapper.instance().buildPatch(rawDiffWithPathPrefix);
86+
assert.strictEqual(filePatches[0].newFile.path, path.join('bad/path.txt'));
87+
assert.strictEqual(filePatches[0].oldFile.path, path.join('bad/path.txt'));
88+
});
89+
8290
it('passes loaded diff data through to the controller', async function() {
8391
const wrapper = shallow(buildApp({
8492
token: '4321',

test/fixtures/diffs/raw-diff.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ const rawDiff = dedent`
1212
line3
1313
`;
1414
const rawDiffWithPathPrefix = dedent`
15-
diff --git a/badpath.txt b/badpath.txt
15+
diff --git a/bad/path.txt b/bad/path.txt
1616
index af607bb..cfac420 100644
17-
--- a/badpath.txt
18-
+++ b/badpath.txt
17+
--- a/bad/path.txt
18+
+++ b/bad/path.txt
1919
@@ -1,2 +1,3 @@
2020
line0
2121
-line1

0 commit comments

Comments
 (0)