Adding recursive repository scanning. Fixes #2

This commit is contained in:
Klaus Silveira
2012-05-23 03:31:21 -03:00
parent 595bd91d2c
commit de57d7d489

View File

@@ -49,14 +49,31 @@ class Client
* @return array Found repositories, containing their name, path and description * @return array Found repositories, containing their name, path and description
*/ */
public function getRepositories($path) public function getRepositories($path)
{
$repositories = $this->recurseDirectory($path);
if (!isset($repositories)) {
throw new \RuntimeException('There are no GIT repositories in ' . $path);
}
sort($repositories);
return $repositories;
}
private function recurseDirectory($path)
{ {
$dir = new \DirectoryIterator($path); $dir = new \DirectoryIterator($path);
foreach ($dir as $file) { foreach ($dir as $file) {
if ($file->isDot()) {
continue;
}
$isBare = file_exists($file->getPathname() . '/HEAD'); $isBare = file_exists($file->getPathname() . '/HEAD');
$isRepository = file_exists($file->getPathname() . '/.git/HEAD'); $isRepository = file_exists($file->getPathname() . '/.git/HEAD');
if ($file->isDir() && !$file->isDot() && $isRepository || $isBare) { if ($file->isDir() && $isRepository || $isBare) {
if ($isBare) { if ($isBare) {
$description = file_get_contents($file->getPathname() . '/description'); $description = file_get_contents($file->getPathname() . '/description');
} else { } else {
@@ -64,15 +81,14 @@ class Client
} }
$repositories[] = array('name' => $file->getFilename(), 'path' => $file->getPathname(), 'description' => $description); $repositories[] = array('name' => $file->getFilename(), 'path' => $file->getPathname(), 'description' => $description);
continue;
}
if ($file->isDir()) {
$repositories = $this->recurseDirectory($file->getPathname());
} }
} }
if (!isset($repositories)) {
throw new \RuntimeException('There are no GIT repositories in ' . $path);
}
sort($repositories);
return $repositories; return $repositories;
} }