/** * @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)); }
/** * @covers ::init * @covers ::output * @covers ::isInitialized * @covers ::config * @covers ::getConfiguration * @depends testCreation */ public function testInitialization() { $this->assertFalse(Git::isInitialized(self::$git->getPath())); $this->assertRegExp('/^Initiali(s|z)ed empty Git repository in/', self::$git->init()->output()); $this->assertTrue(Git::isInitialized(self::$git->getPath())); $this->assertCount(1, $history = self::$git->history()); // reinitialization should have no effect $this->assertRegExp('/^Initiali(s|z)ed empty Git repository in/', self::$git->init()->output()); $this->assertCount(1, $newHistory = self::$git->history()); $this->assertEquals($history, $newHistory); // configuring the user.email and user.name self::$git->config('user.email', $email = '*****@*****.**'); self::$git->config('user.name', $name = 'Giovanni Gioffreda'); $this->assertEquals($email, trim(self::$git->config('user.email')->output())); $this->assertEquals($name, trim(self::$git->config('user.name')->output())); $configuration = self::$git->getConfiguration(); $this->assertArrayHasKey('user.email', $configuration); $this->assertArrayHasKey('user.name', $configuration); }