1
0
Fork 0

Avoid normalizing to absolute paths if base dir is /toplevel to improve docker portability, fixes #11165, fixes #4404 (#11169)

pull/11182/head
Jordi Boggiano 2022-11-10 16:32:18 +01:00 committed by GitHub
parent 6c85b875f2
commit 5b28754602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -440,7 +440,8 @@ class Filesystem
$commonPath = strtr(\dirname($commonPath), '\\', '/'); $commonPath = strtr(\dirname($commonPath), '\\', '/');
} }
if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { // no commonality at all
if (0 !== strpos($from, $commonPath)) {
return $to; return $to;
} }
@ -448,6 +449,11 @@ class Filesystem
$sourcePathDepth = substr_count((string) substr($from, \strlen($commonPath)), '/'); $sourcePathDepth = substr_count((string) substr($from, \strlen($commonPath)), '/');
$commonPathCode = str_repeat('../', $sourcePathDepth); $commonPathCode = str_repeat('../', $sourcePathDepth);
// allow top level /foo & /bar dirs to be addressed relatively as this is common in Docker setups
if ('/' === $commonPath && $sourcePathDepth > 1) {
return $to;
}
$result = $commonPathCode . substr($to, \strlen($commonPath)); $result = $commonPathCode . substr($to, \strlen($commonPath));
if (\strlen($result) === 0) { if (\strlen($result) === 0) {
return './'; return './';
@ -481,15 +487,22 @@ class Filesystem
$commonPath = strtr(\dirname($commonPath), '\\', '/'); $commonPath = strtr(\dirname($commonPath), '\\', '/');
} }
if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) { // no commonality at all
if (0 !== strpos($from, $commonPath) || '.' === $commonPath) {
return var_export($to, true); return var_export($to, true);
} }
$commonPath = rtrim($commonPath, '/') . '/'; $commonPath = rtrim($commonPath, '/') . '/';
if (strpos($to, $from.'/') === 0) { if (str_starts_with($to, $from.'/')) {
return '__DIR__ . '.var_export((string) substr($to, \strlen($from)), true); return '__DIR__ . '.var_export((string) substr($to, \strlen($from)), true);
} }
$sourcePathDepth = substr_count((string) substr($from, \strlen($commonPath)), '/') + (int) $directories; $sourcePathDepth = substr_count((string) substr($from, \strlen($commonPath)), '/') + (int) $directories;
// allow top level /foo & /bar dirs to be addressed relatively as this is common in Docker setups
if ('/' === $commonPath && $sourcePathDepth > 1) {
return var_export($to, true);
}
if ($staticCode) { if ($staticCode) {
$commonPathCode = "__DIR__ . '".str_repeat('/..', $sourcePathDepth)."'"; $commonPathCode = "__DIR__ . '".str_repeat('/..', $sourcePathDepth)."'";
} else { } else {

View File

@ -72,6 +72,7 @@ class FilesystemTest extends TestCase
['c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'D:/vendor/acme/bin/run'"], ['c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'D:/vendor/acme/bin/run'"],
['/foo/bar', '/foo/bar', true, "__DIR__"], ['/foo/bar', '/foo/bar', true, "__DIR__"],
['/foo/bar/', '/foo/bar', true, "__DIR__"], ['/foo/bar/', '/foo/bar', true, "__DIR__"],
['/foo', '/baz', true, "dirname(__DIR__).'/baz'"],
['/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"], ['/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"],
['/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"], ['/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"],
['/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"], ['/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"],
@ -150,7 +151,8 @@ class FilesystemTest extends TestCase
['C:/Temp', 'c:\Temp\..\..\test', "../test", true], ['C:/Temp', 'c:\Temp\..\..\test', "../test", true],
['C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true], ['C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true],
['C:/Temp/../..', 'D:\Temp\..\..\test', "D:/test", true], ['C:/Temp/../..', 'D:\Temp\..\..\test', "D:/test", true],
['/tmp', '/tmp/../../test', '/test', true], ['/tmp', '/tmp/../../test', '../test', true],
['/tmp', '/test', '../test', true],
['/foo/bar', '/foo/bar_vendor', '../bar_vendor', true], ['/foo/bar', '/foo/bar_vendor', '../bar_vendor', true],
['/foo/bar_vendor', '/foo/bar', '../bar', true], ['/foo/bar_vendor', '/foo/bar', '../bar', true],
['/foo/bar_vendor', '/foo/bar/src', '../bar/src', true], ['/foo/bar_vendor', '/foo/bar/src', '../bar/src', true],