Skip to content

Correctly handle file names containing spaces #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
"babel-preset-es2015-mod": "^6.3.13",
"chai": "^3.3.0",
"colors": "^1.1.2",
"deindent": "^0.1.0",
"eslint": "^1.6.0",
"grunt": "^0.4.5",
"grunt-babel": "^6.0.0",
3 changes: 2 additions & 1 deletion src/patch/parse.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -52,7 +52,8 @@ export function parsePatch(uniDiff, options = {}) {
// Parses the --- and +++ headers, if none are found, no lines
// are consumed.
function parseFileHeader(index) {
let fileHeader = (/^(\-\-\-|\+\+\+)\s+(\S*)\s?(.*?)\s*$/).exec(diffstr[i]);
const headerPattern = /^(---|\+\+\+)\s+([\S ]*)(?:\t(.*?)\s*)?$/;
const fileHeader = headerPattern.exec(diffstr[i]);
if (fileHeader) {
let keyPrefix = fileHeader[1] === '---' ? 'old' : 'new';
index[keyPrefix + 'FileName'] = fileHeader[2];
56 changes: 56 additions & 0 deletions test/patch/apply.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import {parsePatch} from '../../lib/patch/parse';
import {createPatch} from '../../lib/patch/create';

import {expect} from 'chai';
import deindent from 'deindent';

describe('patch/apply', function() {
describe('#applyPatch', function() {
@@ -602,5 +603,60 @@ describe('patch/apply', function() {
complete: done
});
});

it('should handle file names containing spaces', done => {
const patch = deindent
`===================================================================
--- test file\theader1
+++ test file\theader2
@@ -1,2 +1,3 @@
line1
+line2
line3
===================================================================
--- test file 2\theader1
+++ test file 2\theader2
@@ -1,2 +1,3 @@
foo1
+foo2
foo3
`;

const contents = {
'test file': deindent
`line1
line3
`,
'test file 2': deindent
`foo1
foo3
`
};

const expected = {
'test file': deindent
`line1
line2
line3
`,
'test file 2': deindent
`foo1
foo2
foo3
`
};

applyPatches(patch, {
loadFile(index, callback) {
callback(undefined, contents[index.oldFileName]);
},
patched(index, content) {
expect(content)
.to.equal(expected[index.newFileName])
.to.not.be.undefined;
},
complete: done
});
});
});
});