/** * @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)); }
/** * @param $url * @param $name * * @covers ::remoteAdd * @covers ::remoteRename * @covers ::remoteRemove * @covers ::remoteSetUrl * @covers ::remoteGetUrl * @covers ::remoteShow * @depends testCloning * @expectedException \Gioffreda\Component\Git\Exception\GitProcessException * @dataProvider remotesProvider */ public function testRemote($url, $name) { $path = self::buildPath(); self::getFilesystem()->mkdir($path); $git = Git::cloneRemote($url, $path); $this->assertStringStartsWith('origin', $git->remoteShow()); $this->assertContains($name, $git->remoteShow('origin')); $this->assertContains('origin', $git->remoteShow('origin')); $this->assertStringStartsWith($url, $git->remoteGetUrl('origin')); $brokenUrl = "{$url}.test"; $git->remoteAdd('broken', $brokenUrl); $this->assertStringStartsWith($brokenUrl, $git->remoteGetUrl('broken')); $git->remoteSetUrl('broken', $url); $this->assertStringStartsWith($url, $git->remoteGetUrl('broken')); $git->remoteRename('broken', 'fixed'); $this->assertContains($name, $git->remoteShow('fixed')); $this->assertContains('fixed', $git->remoteShow('fixed')); $git->remoteRemove('fixed'); // the following must throw an exception because the 'fixed' origin is gone $git->remoteShow('fixed'); }