Beispiel #1
0
 public static function setUpBeforeClass()
 {
     if (getenv('TMP')) {
         self::$tmpdir = getenv('TMP');
     } elseif (getenv('TMPDIR')) {
         self::$tmpdir = getenv('TMPDIR');
     } else {
         self::$tmpdir = '/tmp';
     }
     self::$tmpdir .= '/gitlist_' . md5(time() . mt_rand()) . '/';
     $fs = new Filesystem();
     $fs->mkdir(self::$tmpdir);
     if (!is_writable(self::$tmpdir)) {
         $this->markTestSkipped('There are no write permissions in order to create test repositories.');
     }
     $options['path'] = getenv('GIT_CLIENT') ?: '/usr/bin/git';
     $options['hidden'] = array(self::$tmpdir . '/hiddenrepo');
     $git = new Client($options);
     // GitTest repository fixture
     $git->createRepository(self::$tmpdir . 'GitTest');
     $repository = $git->getRepository(self::$tmpdir . 'GitTest');
     file_put_contents(self::$tmpdir . 'GitTest/README.md', "## GitTest\nGitTest is a *test* repository!");
     file_put_contents(self::$tmpdir . 'GitTest/test.php', "<?php\necho 'Hello World'; // This is a test");
     $repository->setConfig('user.name', 'Luke Skywalker');
     $repository->setConfig('user.email', '*****@*****.**');
     $repository->addAll();
     $repository->commit("Initial commit");
     $repository->createBranch('issue12');
     $repository->createBranch('issue42');
     // foobar repository fixture
     $git->createRepository(self::$tmpdir . 'foobar');
     $repository = $git->getRepository(self::$tmpdir . '/foobar');
     file_put_contents(self::$tmpdir . 'foobar/bar.json', "{\n\"name\": \"foobar\"\n}");
     file_put_contents(self::$tmpdir . 'foobar/.git/description', 'This is a test repo!');
     $fs->mkdir(self::$tmpdir . 'foobar/myfolder');
     $fs->mkdir(self::$tmpdir . 'foobar/testfolder');
     file_put_contents(self::$tmpdir . 'foobar/myfolder/mytest.php', "<?php\necho 'Hello World'; // This is my test");
     file_put_contents(self::$tmpdir . 'foobar/testfolder/test.php', "<?php\necho 'Hello World'; // This is a test");
     $repository->setConfig('user.name', 'Luke Skywalker');
     $repository->setConfig('user.email', '*****@*****.**');
     $repository->addAll();
     $repository->commit("First commit");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $startTime = microtime(true);
     $targetDirectory = getcwd();
     $commitBefore = $input->getArgument('before');
     $commitAfter = $input->getArgument('after');
     $includeBefore = $input->getOption('include-before');
     $excludeBefore = $input->getOption('exclude-before');
     $includeAfter = $input->getOption('include-after');
     $excludeAfter = $input->getOption('exclude-after');
     $finder = new Finder();
     $sourceFilter = new SourceFilter();
     $beforeScanner = new Scanner();
     $afterScanner = new Scanner();
     $client = new Client();
     $repository = $client->getRepository($targetDirectory);
     $modifiedFiles = $repository->getModifiedFiles($commitBefore, $commitAfter);
     $modifiedFiles = array_filter($modifiedFiles, function ($modifiedFile) {
         return substr($modifiedFile, -4) === '.php';
     });
     $initialBranch = $repository->getCurrentBranch();
     $repository->checkout($commitBefore . ' --');
     $sourceBefore = $finder->findFromString($targetDirectory, $includeBefore, $excludeBefore);
     $sourceBeforeMatchedCount = count($sourceBefore);
     $sourceBefore = $sourceFilter->filter($sourceBefore, $modifiedFiles);
     $progress = new ProgressBar($output, count($sourceBefore));
     foreach ($sourceBefore as $file) {
         $beforeScanner->scan($file);
         $progress->advance();
     }
     $progress->clear();
     $repository->checkout($commitAfter . ' --');
     $sourceAfter = $finder->findFromString($targetDirectory, $includeAfter, $excludeAfter);
     $sourceAfterMatchedCount = count($sourceAfter);
     $sourceAfter = $sourceFilter->filter($sourceAfter, $modifiedFiles);
     $progress = new ProgressBar($output, count($sourceAfter));
     foreach ($sourceAfter as $file) {
         $afterScanner->scan($file);
         $progress->advance();
     }
     $progress->clear();
     if ($initialBranch) {
         $repository->checkout($initialBranch);
     }
     $progress->clear();
     $registryBefore = $beforeScanner->getRegistry();
     $registryAfter = $afterScanner->getRegistry();
     $analyzer = new Analyzer();
     $report = $analyzer->analyze($registryBefore, $registryAfter);
     $reporter = new Reporter($report);
     $reporter->setFullPath(true);
     $reporter->output($output);
     $toJson = $input->getOption('to-json');
     if ($toJson) {
         $commitBeforeHash = $repository->getCommit($commitBefore)->getHash();
         $commitAfterHash = $repository->getCommit($commitAfter)->getHash();
         $jsonReporter = new JsonReporter($report, $toJson, $commitBeforeHash, $commitAfterHash);
         $jsonReporter->output();
     }
     $duration = microtime(true) - $startTime;
     $output->writeln('');
     $output->writeln('[Scanned files] Before: ' . count($sourceBefore) . ' (' . $sourceBeforeMatchedCount . ' unfiltered), After: ' . count($sourceAfter) . ' (' . $sourceAfterMatchedCount . ' unfiltered)');
     $output->writeln('Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $startTime = microtime(true);
     $targetDirectory = getcwd();
     $tag = $this->config->get('tag');
     $against = $this->config->get('against') ?: 'HEAD';
     $includeBefore = $this->config->get('include-before');
     $excludeBefore = $this->config->get('exclude-before');
     $includeAfter = $this->config->get('include-after');
     $excludeAfter = $this->config->get('exclude-after');
     $client = new Client();
     $repository = $client->getRepository($targetDirectory);
     if ($tag === null) {
         $tag = $this->findLatestTag($repository);
     } else {
         $tag = $this->findTag($repository, $tag);
     }
     if ($tag === null) {
         $output->writeln('<error>No tags to suggest against</error>');
         return;
     }
     $output->writeln('<info>Testing ' . $against . ' against tag: ' . $tag . '</info>');
     $finder = new Finder();
     $sourceFilter = new SourceFilter();
     $beforeScanner = new Scanner();
     $afterScanner = new Scanner();
     $modifiedFiles = $repository->getModifiedFiles($tag, $against);
     $modifiedFiles = array_filter($modifiedFiles, function ($modifiedFile) {
         return substr($modifiedFile, -4) === '.php';
     });
     $initialBranch = $repository->getCurrentBranch();
     if (!$this->config->get('allow-detached') && !$initialBranch) {
         $output->writeln('<error>You are on a detached HEAD, aborting.</error>');
         $output->writeln('<info>If you still wish to run against a detached HEAD, use --allow-detached.</info>');
         return -1;
     }
     // Start with the against commit
     $repository->checkout($against . ' --');
     $sourceAfter = $finder->findFromString($targetDirectory, $includeAfter, $excludeAfter);
     $sourceAfterMatchedCount = count($sourceAfter);
     $sourceAfter = $sourceFilter->filter($sourceAfter, $modifiedFiles);
     $progress = new ProgressBar($output, count($sourceAfter));
     foreach ($sourceAfter as $file) {
         $afterScanner->scan($file);
         $progress->advance();
     }
     $progress->clear();
     // Finish with the tag commit
     $repository->checkout($tag . ' --');
     $sourceBefore = $finder->findFromString($targetDirectory, $includeBefore, $excludeBefore);
     $sourceBeforeMatchedCount = count($sourceBefore);
     $sourceBefore = $sourceFilter->filter($sourceBefore, $modifiedFiles);
     $progress = new ProgressBar($output, count($sourceBefore));
     foreach ($sourceBefore as $file) {
         $beforeScanner->scan($file);
         $progress->advance();
     }
     $progress->clear();
     // Reset repository to initial branch
     if ($initialBranch) {
         $repository->checkout($initialBranch);
     }
     $registryBefore = $beforeScanner->getRegistry();
     $registryAfter = $afterScanner->getRegistry();
     $analyzer = new Analyzer();
     $report = $analyzer->analyze($registryBefore, $registryAfter);
     $tag = new SemanticVersion($tag);
     $newTag = new SemanticVersion($tag);
     $suggestedLevel = $report->getSuggestedLevel();
     if ($suggestedLevel !== Level::NONE) {
         if ($newTag->getPrerelease()) {
             $newTag->inc('prerelease');
         } else {
             if ($newTag->getMajor() < 1 && $suggestedLevel === Level::MAJOR) {
                 $newTag->inc('minor');
             } else {
                 $newTag->inc(strtolower(Level::toString($suggestedLevel)));
             }
         }
     }
     $output->writeln('');
     $output->writeln('<info>Initial semantic version: ' . $tag . '</info>');
     $output->writeln('<info>Suggested semantic version: ' . $newTag . '</info>');
     if ($this->config->get('details')) {
         $reporter = new Reporter($report);
         $reporter->output($output);
     }
     $duration = microtime(true) - $startTime;
     $output->writeln('');
     $output->writeln('[Scanned files] Before: ' . count($sourceBefore) . ' (' . $sourceBeforeMatchedCount . ' unfiltered), After: ' . count($sourceAfter) . ' (' . $sourceAfterMatchedCount . '  unfiltered)');
     $output->writeln('Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB');
 }
Beispiel #4
0
 /**
  * Get last tag.
  *
  * @return array
  */
 protected function getLastTag()
 {
     $client = new Client();
     $repository = $client->getRepository($this->path);
     return $repository->getLastTag();
 }