Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
path: fix win32 relative() when "to" is a prefix
when the basename of "to" was a prefix of the basename of "from" win32
relative() would miss including it in the result

Fixes #5447
  • Loading branch information
omsmith committed Feb 27, 2016
commit c18d24e8f49132cfeeed1c280ba0ee4f06e60b26
28 changes: 21 additions & 7 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,28 @@ const win32 = {
var i = 0;
for (; i <= length; ++i) {
if (i === length) {
if (lastCommonSep > 2 && // ignore separator match following device root
toLen > length &&
to.charCodeAt(i) === 92/*\*/) {
// We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(i + 1);
if (toLen > length) {
if (to.charCodeAt(toStart + i) === 92/*\*/) {
// We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(toStart + i + 1);
} else if (lastCommonSep === 2) {
// We get here if `from` is the device root.
// For example: from='C:\\'; to='C:\\foo'
return toOrig.slice(toStart + i);
}
}
if (fromLen > length) {
if (from.charCodeAt(fromStart + i) === 92/*\*/) {
// We get here if `to` is the exact base path for `from`.
// For example: from='C:\\foo\\bar'; to='C:\\foo'
lastCommonSep = i;
} else if (lastCommonSep === 2) {
// We get here if `to` is the device root.
// For example: from='C:\\foo\\bar'; to='C:\\'
lastCommonSep = 3;
}
}
lastCommonSep = i;
break;
}
var fromCode = from.charCodeAt(fromStart + i);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ const relativeTests = [
['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json']
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz']
]
],
[ path.posix.relative,
Expand Down