From 0e02add6e624238427686de16c51829d4d50b11c Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 23 Dec 2016 11:17:56 +0100 Subject: [PATCH] Adding of field that contains the number of commits --- app/console | 2 + .../Migration/UpgradeTo1p4p1Command.php | 44 +++++++++++++++++++ src/Gist/Command/StatsCommand.php | 29 +++++------- src/Gist/Model/Gist.php | 10 +++++ src/Gist/Resources/config/propel/schema.xml | 3 +- src/Gist/Service/Gist.php | 23 +++++++++- 6 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 src/Gist/Command/Migration/UpgradeTo1p4p1Command.php diff --git a/app/console b/app/console index 6eed2fc..eef6670 100755 --- a/app/console +++ b/app/console @@ -5,6 +5,7 @@ use Gist\Command\CreateCommand; use Gist\Command\UpdateCommand; use Gist\Command\StatsCommand; use Gist\Command\UserCreateCommand; +use Gist\Command\Migration\UpgradeTo1p4p1Command; $app = require __DIR__.'/bootstrap.php'; @@ -12,5 +13,6 @@ $app['console']->add(new CreateCommand()); $app['console']->add(new UpdateCommand()); $app['console']->add(new StatsCommand()); $app['console']->add(new UserCreateCommand()); +$app['console']->add(new UpgradeTo1p4p1Command()); $app['console']->run(); diff --git a/src/Gist/Command/Migration/UpgradeTo1p4p1Command.php b/src/Gist/Command/Migration/UpgradeTo1p4p1Command.php new file mode 100644 index 0000000..738fd8f --- /dev/null +++ b/src/Gist/Command/Migration/UpgradeTo1p4p1Command.php @@ -0,0 +1,44 @@ + + */ +class UpgradeTo1p4p1Command extends Command +{ + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('migrate:to:v1.4.1') + ->setDescription('Migrates database entries to >= v1.4.1') + ->setHelp('The %command.name% migrates database entries to >= v1.4.1'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $app = $this->getSilexApplication(); + $gists = GistQuery::create() + ->filterByCommits(0) + ->find(); + + foreach ($gists as $gist) { + $commits = $app['gist']->getNumberOfCommits($gist); + $gist->setCommits($commits); + $gist->save(); + } + } +} diff --git a/src/Gist/Command/StatsCommand.php b/src/Gist/Command/StatsCommand.php index 8e8ad3d..808aecc 100644 --- a/src/Gist/Command/StatsCommand.php +++ b/src/Gist/Command/StatsCommand.php @@ -12,8 +12,8 @@ use GitWrapper\GitException; /** * class StatsCommand; - * - * @author Simon Vieille + * + * @author Simon Vieille */ class StatsCommand extends Command { @@ -30,7 +30,7 @@ Show stats about GIST EOF ); } - + /** * {@inheritdoc} */ @@ -52,18 +52,13 @@ EOF $withEncryption[$gist->getType()] = 0; $commits[$gist->getType()] = 0; } - + if ($gist->getCipher()) { $withEncryption[$gist->getType()]++; } - + $languages[$gist->getType()]++; - try { - $count = count($gistService->getHistory($gist)); - $commits[$gist->getType()] += $count; - } catch(GitException $e) { - - } + $commits[$gist->getType()] += $gist->getCommits(); } $output->writeln(['Gists statistics', '']); @@ -73,9 +68,9 @@ EOF ->setHeaders(array('Without encryption', 'With encryption', 'Commits', 'Total')) ->setRows(array( array( - $total - $v = array_sum($withEncryption), - $v, - array_sum($commits), + $total - $v = array_sum($withEncryption), + $v, + array_sum($commits), $total ), )) @@ -90,9 +85,9 @@ EOF $table->setHeaders(array( 'Type', - 'Without encryption', - 'With encryption', - 'Commits', + 'Without encryption', + 'With encryption', + 'Commits', 'Total', )); diff --git a/src/Gist/Model/Gist.php b/src/Gist/Model/Gist.php index efb0a45..23a4809 100644 --- a/src/Gist/Model/Gist.php +++ b/src/Gist/Model/Gist.php @@ -76,4 +76,14 @@ class Gist extends BaseGist return str_replace(array_keys($data), array_values($data), $this->getType()); } + + /* + * Increments the number of commits. + */ + public function commit() + { + $this->setCommits($this->getCommits() + 1); + + return $this; + } } diff --git a/src/Gist/Resources/config/propel/schema.xml b/src/Gist/Resources/config/propel/schema.xml index 40d2f86..ce79ebb 100644 --- a/src/Gist/Resources/config/propel/schema.xml +++ b/src/Gist/Resources/config/propel/schema.xml @@ -7,7 +7,8 @@ - + + diff --git a/src/Gist/Service/Gist.php b/src/Gist/Service/Gist.php index bad53bf..7877164 100644 --- a/src/Gist/Service/Gist.php +++ b/src/Gist/Service/Gist.php @@ -149,7 +149,7 @@ class Gist $gist->setUser($user); } - $gist->save(); + $gist->commit()->save(); return $gist; } @@ -170,9 +170,30 @@ class Gist ->add($gist->getFile()) ->commit('Update'); + $gist->commit()->save(); + return $gist; } + /* + * Returns the number of commits. + * + * @param GistModel $gist + * + * @return int + */ + public function getNumberOfCommits(GistModel $gist) + { + $command = GitCommand::getInstance('log', '--oneline', '--', $gist->getFile()); + $command->setDirectory($this->gistPath); + $command->bypass(false); + + $content = trim($this->gitWrapper->run($command)); + $content = str_replace("\r\n", "\n", $content); + + return count(explode("\n", $content)); + } + /** * Highlight the content. *