Skip to content

Commit 9eb5b9a

Browse files
committed
Make vcsDetect() a little safer
1 parent 1e12f5d commit 9eb5b9a

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

composer-lock-diff

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,37 @@ function parseOpts() {
332332
);
333333
}
334334

335-
function vcsDetect($base_path){
335+
function dirnameSafe($path) {
336+
$parent = dirname($path);
337+
return ($parent != $path && !empty($parent)) ? $parent : false;
338+
}
339+
340+
function joinPath(/* path parts */) {
341+
return implode(DIRECTORY_SEPARATOR, array_map(function($part) {
342+
return trim($part, DIRECTORY_SEPARATOR);
343+
}, func_get_args()));
344+
}
345+
346+
function vcsDetect($base_path) {
336347
if (empty($base_path)) {
337-
$base_path = '.' . DIRECTORY_SEPARATOR;
338-
} else {
339-
$base_path = rtrim($base_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
348+
$base_path = '.';
340349
}
341350

351+
// > Trailing delimiters, such as \ and /, are also removed
352+
// > returns false on failure, e.g. if the file does not exist.
342353
$base_path = realpath($base_path);
343-
while(!empty($base_path) && $base_path !== '/' ){
344-
if(is_dir($base_path.'/.svn')){
345-
return 'svn';
346-
}elseif(is_dir($base_path.'/.git')){
354+
if ($base_path === false) return 'unknown';
355+
356+
$tries = 10;
357+
do {
358+
if(is_dir(joinPath($base_path, '.git'))) {
347359
return 'git';
360+
} elseif(is_dir(joinPath($base_path, '.svn'))) {
361+
return 'svn';
348362
}
349-
$base_path = dirname($base_path);
350-
}
363+
364+
$base_path = dirnameSafe($base_path);
365+
} while($base_path !== false && --$tries > 0);
351366

352367
return 'unknown';
353368
}

0 commit comments

Comments
 (0)