Skip to content

Commit 2e59e39

Browse files
committed
Merge pull request #120 from adius/file-name-with-spaces
Correctly handle file names containing spaces
2 parents 3b35b73 + d92e223 commit 2e59e39

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

Diff for: package.json

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"babel-preset-es2015-mod": "^6.3.13",
3434
"chai": "^3.3.0",
3535
"colors": "^1.1.2",
36+
"deindent": "^0.1.0",
3637
"eslint": "^1.6.0",
3738
"grunt": "^0.4.5",
3839
"grunt-babel": "^6.0.0",

Diff for: src/patch/parse.js

100644100755
+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function parsePatch(uniDiff, options = {}) {
5252
// Parses the --- and +++ headers, if none are found, no lines
5353
// are consumed.
5454
function parseFileHeader(index) {
55-
let fileHeader = (/^(\-\-\-|\+\+\+)\s+(\S*)\s?(.*?)\s*$/).exec(diffstr[i]);
55+
const headerPattern = /^(---|\+\+\+)\s+([\S ]*)(?:\t(.*?)\s*)?$/;
56+
const fileHeader = headerPattern.exec(diffstr[i]);
5657
if (fileHeader) {
5758
let keyPrefix = fileHeader[1] === '---' ? 'old' : 'new';
5859
index[keyPrefix + 'FileName'] = fileHeader[2];

Diff for: test/patch/apply.js

100644100755
+56
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {parsePatch} from '../../lib/patch/parse';
33
import {createPatch} from '../../lib/patch/create';
44

55
import {expect} from 'chai';
6+
import deindent from 'deindent';
67

78
describe('patch/apply', function() {
89
describe('#applyPatch', function() {
@@ -602,5 +603,60 @@ describe('patch/apply', function() {
602603
complete: done
603604
});
604605
});
606+
607+
it('should handle file names containing spaces', done => {
608+
const patch = deindent
609+
`===================================================================
610+
--- test file\theader1
611+
+++ test file\theader2
612+
@@ -1,2 +1,3 @@
613+
line1
614+
+line2
615+
line3
616+
===================================================================
617+
--- test file 2\theader1
618+
+++ test file 2\theader2
619+
@@ -1,2 +1,3 @@
620+
foo1
621+
+foo2
622+
foo3
623+
`;
624+
625+
const contents = {
626+
'test file': deindent
627+
`line1
628+
line3
629+
`,
630+
'test file 2': deindent
631+
`foo1
632+
foo3
633+
`
634+
};
635+
636+
const expected = {
637+
'test file': deindent
638+
`line1
639+
line2
640+
line3
641+
`,
642+
'test file 2': deindent
643+
`foo1
644+
foo2
645+
foo3
646+
`
647+
};
648+
649+
applyPatches(patch, {
650+
loadFile(index, callback) {
651+
callback(undefined, contents[index.oldFileName]);
652+
},
653+
patched(index, content) {
654+
expect(content)
655+
.to.equal(expected[index.newFileName])
656+
.to.not.be.undefined;
657+
},
658+
complete: done
659+
});
660+
});
605661
});
606662
});

0 commit comments

Comments
 (0)