/** * @param RepositoryInterface $repository * @param $destination * @param array|null $options */ public function cloneRepository(RepositoryInterface $repository, $destination, array $options = null) { $options = array_merge($this->defaultOptions, $options ?: []); $configFile = DIRECTORY_SEPARATOR === $options['clone_config'][0] ? $options['clone_config'] : $destination . DIRECTORY_SEPARATOR . $options['clone_config']; $fs = new Filesystem(); if (!$fs->exists($configFile)) { $fs->touch($configFile); } /** @var RepositoryInterface $repository */ $path = $destination . DIRECTORY_SEPARATOR . $repository->getName(); if (!$fs->exists($path)) { $fs->mkdir($path); } if (!Git::isInitialized($path)) { $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.pre_clone_repository', $repository, 'clone', null, ['path' => $path])); $git = Git::cloneRemote($repository->getUri(), $path); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.post_clone_repository', $repository, 'clone', $git)); } if (!isset($git)) { $git = Git::create($path); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.create_git', $repository, 'create', $git)); } $skip = false; $log = json_decode(file_get_contents($configFile) ?: '[]', true); if (isset($log[$repository->getName()]) && new \DateTime($log[$repository->getName()]['fetched_at']) > $repository->getUpdatedAt()) { $git->remoteSetUrl('origin', $repository->getAnonymousUri()); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.config_skip_repository', $repository, 'config_skip', $git)); $skip = true; } if (!$skip) { $git->remoteSetUrl('origin', $repository->getUri()); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.pre_fetch_repository', $repository, 'fetch', $git)); $git->fetch(['--all']); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.post_fetch_repository', $repository, 'fetch', $git)); $git->remoteSetUrl('origin', $repository->getAnonymousUri()); } $commitsCount = null; try { $commitsCount = (int) $git->run(['rev-list', '--all', '--count']); } catch (GitProcessException $e) { // do nothing about it } $finder = new Finder(); $files = $finder->in($path)->count(); $log[$repository->getName()] = ['name' => $repository->getName(), 'description' => $repository->getDescription(), 'uri' => $repository->getAnonymousUri(), 'path' => $path, 'commits' => $git->getLogs(10), 'commits_count' => $commitsCount, 'size' => $repository->getSize(), 'private' => $repository->isPrivate(), 'branches' => $git->getBranches(), 'fetched_at' => date(DATE_ISO8601), 'files' => $files, 'programming_language' => $this->guessLanguages($path, $files), 'updated_at' => $repository->getUpdatedAt() ? $repository->getUpdatedAt()->format(DATE_ISO8601) : null]; $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.pre_config_log_repository', $repository, 'config_log', $git, ['log' => $log])); file_put_contents($configFile, json_encode($log, JSON_PRETTY_PRINT)); }
/** * Clone provided remote repository in a local Git project using the specified executable. * * @param $remote * @param $path * @param string $gitBin * @return Git */ public static function cloneRemote($remote, $path, $gitBin = self::GIT_BIN) { $git = new Git($path, $gitBin); $git->run(['clone', $remote, '.']); return $git; }
public static function tearDownAfterClass() { self::getFilesystem()->remove(self::$git->getPath()); }