|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GitList\Git; |
| 4 | + |
| 5 | +use Gitter\Client as BaseClient; |
| 6 | + |
| 7 | +class Client extends BaseClient |
| 8 | +{ |
| 9 | + protected $defaultBranch; |
| 10 | + protected $hidden; |
| 11 | + protected $projects; |
| 12 | + |
| 13 | + public function __construct($options = null) |
| 14 | + { |
| 15 | + parent::__construct($options['path']); |
| 16 | + $this->setDefaultBranch($options['default_branch']); |
| 17 | + $this->setHidden($options['hidden']); |
| 18 | + $this->setProjects($options['projects']); |
| 19 | + } |
| 20 | + |
| 21 | + public function getRepositoryFromName($paths, $repo) |
| 22 | + { |
| 23 | + $repositories = $this->getRepositories($paths); |
| 24 | + $path = $repositories[$repo]['path']; |
| 25 | + |
| 26 | + return $this->getRepository($path); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Searches for valid repositories on the specified path. |
| 31 | + * |
| 32 | + * @param array $paths Array of paths where repositories will be searched |
| 33 | + * |
| 34 | + * @return array Found repositories, containing their name, path and description sorted |
| 35 | + * by repository name |
| 36 | + */ |
| 37 | + public function getRepositories($paths) |
| 38 | + { |
| 39 | + $allRepositories = []; |
| 40 | + |
| 41 | + foreach ($paths as $path) { |
| 42 | + /*$repositories = $this->recurseDirectory($path); |
| 43 | +
|
| 44 | + if (empty($repositories)) { |
| 45 | + throw new \RuntimeException('There are no GIT repositories in ' . $path); |
| 46 | + } |
| 47 | +
|
| 48 | + // Use "+" to preserve keys, only a problem with numeric repos. |
| 49 | + $allRepositories = $allRepositories + $repositories;*/ |
| 50 | + |
| 51 | + // START Neard edit |
| 52 | + $name = str_replace(':', '', str_replace('/', '#', $path)); |
| 53 | + $repositories[$name] = array( |
| 54 | + 'name' => $name, |
| 55 | + 'path' => $path, |
| 56 | + 'description' => file_exists($path . '/.git/description') ? file_get_contents($path . '/.git/description') : '' |
| 57 | + ); |
| 58 | + |
| 59 | + $allRepositories = array_merge($allRepositories, $repositories); |
| 60 | + // END Neard edit |
| 61 | + } |
| 62 | + |
| 63 | + $allRepositories = array_unique($allRepositories, SORT_REGULAR); |
| 64 | + uksort($allRepositories, function ($k1, $k2) { |
| 65 | + return strtolower($k2) < strtolower($k1); |
| 66 | + }); |
| 67 | + |
| 68 | + return $allRepositories; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Return name of default branch as a string. |
| 73 | + */ |
| 74 | + public function getDefaultBranch() |
| 75 | + { |
| 76 | + return $this->defaultBranch; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Overloads the parent::createRepository method for the correct Repository class instance. |
| 81 | + * |
| 82 | + * {@inheritdoc} |
| 83 | + */ |
| 84 | + public function createRepository($path, $bare = null) |
| 85 | + { |
| 86 | + if (file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) { |
| 87 | + throw new \RuntimeException('A GIT repository already exists at ' . $path); |
| 88 | + } |
| 89 | + |
| 90 | + $repository = new Repository($path, $this); |
| 91 | + |
| 92 | + return $repository->create($bare); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Overloads the parent::getRepository method for the correct Repository class instance. |
| 97 | + * |
| 98 | + * {@inheritdoc} |
| 99 | + */ |
| 100 | + public function getRepository($path) |
| 101 | + { |
| 102 | + if (!file_exists($path) || !file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) { |
| 103 | + throw new \RuntimeException('There is no GIT repository at ' . $path); |
| 104 | + } |
| 105 | + |
| 106 | + return new Repository($path, $this); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Set default branch as a string. |
| 111 | + * |
| 112 | + * @param string $branch name of branch to use when repo's HEAD is detached |
| 113 | + * |
| 114 | + * @return object |
| 115 | + */ |
| 116 | + protected function setDefaultBranch($branch) |
| 117 | + { |
| 118 | + $this->defaultBranch = $branch; |
| 119 | + |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get hidden repository list. |
| 125 | + * |
| 126 | + * @return array List of repositories to hide |
| 127 | + */ |
| 128 | + protected function getHidden() |
| 129 | + { |
| 130 | + return $this->hidden; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Set the hidden repository list. |
| 135 | + * |
| 136 | + * @param array $hidden List of repositories to hide |
| 137 | + * |
| 138 | + * @return object |
| 139 | + */ |
| 140 | + protected function setHidden($hidden) |
| 141 | + { |
| 142 | + $this->hidden = $hidden; |
| 143 | + |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get project list. |
| 149 | + * |
| 150 | + * @return array List of repositories to show |
| 151 | + */ |
| 152 | + protected function getProjects() |
| 153 | + { |
| 154 | + return $this->projects; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Set the shown repository list. |
| 159 | + * |
| 160 | + * @param array $projects List of repositories to show |
| 161 | + */ |
| 162 | + protected function setProjects($projects) |
| 163 | + { |
| 164 | + $this->projects = $projects; |
| 165 | + |
| 166 | + return $this; |
| 167 | + } |
| 168 | + |
| 169 | + private function recurseDirectory($path, $topLevel = true) |
| 170 | + { |
| 171 | + $dir = new \DirectoryIterator($path); |
| 172 | + |
| 173 | + $repositories = []; |
| 174 | + |
| 175 | + foreach ($dir as $file) { |
| 176 | + if ($file->isDot()) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + if (strrpos($file->getFilename(), '.') === 0) { |
| 181 | + continue; |
| 182 | + } |
| 183 | + |
| 184 | + if (!$file->isReadable()) { |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + if ($file->isDir()) { |
| 189 | + $isBare = file_exists($file->getPathname() . '/HEAD'); |
| 190 | + $isRepository = file_exists($file->getPathname() . '/.git/HEAD'); |
| 191 | + |
| 192 | + if ($isRepository || $isBare) { |
| 193 | + $hidden = function ($path, $hide) { |
| 194 | + $return = false; |
| 195 | + |
| 196 | + array_walk($hide, function ($value, $key) use ($path, &$return) { |
| 197 | + if (($path === $value) || (1 === preg_match($value, $path))) { |
| 198 | + $return = true; |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + return $return; |
| 203 | + }; |
| 204 | + |
| 205 | + if ($hidden($file->getPathname(), $this->getHidden())) { |
| 206 | + continue; |
| 207 | + } |
| 208 | + |
| 209 | + if ($isBare) { |
| 210 | + $description = $file->getPathname() . '/description'; |
| 211 | + } else { |
| 212 | + $description = $file->getPathname() . '/.git/description'; |
| 213 | + } |
| 214 | + |
| 215 | + if (file_exists($description)) { |
| 216 | + $description = file_get_contents($description); |
| 217 | + } else { |
| 218 | + $description = null; |
| 219 | + } |
| 220 | + |
| 221 | + if (!$topLevel) { |
| 222 | + $repoName = $file->getPathInfo()->getFilename() . '/' . $file->getFilename(); |
| 223 | + } else { |
| 224 | + $repoName = $file->getFilename(); |
| 225 | + } |
| 226 | + |
| 227 | + if (is_array($this->getProjects()) && !in_array($repoName, $this->getProjects())) { |
| 228 | + continue; |
| 229 | + } |
| 230 | + |
| 231 | + $repositories[$repoName] = [ |
| 232 | + 'name' => $repoName, |
| 233 | + 'path' => $file->getPathname(), |
| 234 | + 'description' => $description, |
| 235 | + ]; |
| 236 | + |
| 237 | + continue; |
| 238 | + } |
| 239 | + $repositories = array_merge($repositories, $this->recurseDirectory($file->getPathname(), false)); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return $repositories; |
| 244 | + } |
| 245 | +} |
0 commit comments