1
0
Fork 0
forked from deblan/gist

Adding of field that contains the number of commits

This commit is contained in:
Simon Vieille 2016-12-23 11:17:56 +01:00
parent df3035f2f1
commit 0e02add6e6
6 changed files with 92 additions and 19 deletions

View file

@ -5,6 +5,7 @@ use Gist\Command\CreateCommand;
use Gist\Command\UpdateCommand; use Gist\Command\UpdateCommand;
use Gist\Command\StatsCommand; use Gist\Command\StatsCommand;
use Gist\Command\UserCreateCommand; use Gist\Command\UserCreateCommand;
use Gist\Command\Migration\UpgradeTo1p4p1Command;
$app = require __DIR__.'/bootstrap.php'; $app = require __DIR__.'/bootstrap.php';
@ -12,5 +13,6 @@ $app['console']->add(new CreateCommand());
$app['console']->add(new UpdateCommand()); $app['console']->add(new UpdateCommand());
$app['console']->add(new StatsCommand()); $app['console']->add(new StatsCommand());
$app['console']->add(new UserCreateCommand()); $app['console']->add(new UserCreateCommand());
$app['console']->add(new UpgradeTo1p4p1Command());
$app['console']->run(); $app['console']->run();

View file

@ -0,0 +1,44 @@
<?php
namespace Gist\Command\Migration;
use Knp\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Gist\Model\GistQuery;
/**
* class UpgradeTo1p4p1Command.
*
* @author Simon Vieille <simon@deblan.fr>
*/
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 <info>%command.name%</info> 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();
}
}
}

View file

@ -12,8 +12,8 @@ use GitWrapper\GitException;
/** /**
* class StatsCommand; * class StatsCommand;
* *
* @author Simon Vieille <simon@deblan.fr> * @author Simon Vieille <simon@deblan.fr>
*/ */
class StatsCommand extends Command class StatsCommand extends Command
{ {
@ -30,7 +30,7 @@ Show stats about GIST
EOF EOF
); );
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -52,18 +52,13 @@ EOF
$withEncryption[$gist->getType()] = 0; $withEncryption[$gist->getType()] = 0;
$commits[$gist->getType()] = 0; $commits[$gist->getType()] = 0;
} }
if ($gist->getCipher()) { if ($gist->getCipher()) {
$withEncryption[$gist->getType()]++; $withEncryption[$gist->getType()]++;
} }
$languages[$gist->getType()]++; $languages[$gist->getType()]++;
try { $commits[$gist->getType()] += $gist->getCommits();
$count = count($gistService->getHistory($gist));
$commits[$gist->getType()] += $count;
} catch(GitException $e) {
}
} }
$output->writeln(['<comment>Gists statistics</comment>', '']); $output->writeln(['<comment>Gists statistics</comment>', '']);
@ -73,9 +68,9 @@ EOF
->setHeaders(array('Without encryption', 'With encryption', 'Commits', 'Total')) ->setHeaders(array('Without encryption', 'With encryption', 'Commits', 'Total'))
->setRows(array( ->setRows(array(
array( array(
$total - $v = array_sum($withEncryption), $total - $v = array_sum($withEncryption),
$v, $v,
array_sum($commits), array_sum($commits),
$total $total
), ),
)) ))
@ -90,9 +85,9 @@ EOF
$table->setHeaders(array( $table->setHeaders(array(
'Type', 'Type',
'Without encryption', 'Without encryption',
'With encryption', 'With encryption',
'Commits', 'Commits',
'Total', 'Total',
)); ));

View file

@ -76,4 +76,14 @@ class Gist extends BaseGist
return str_replace(array_keys($data), array_values($data), $this->getType()); 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;
}
} }

View file

@ -7,7 +7,8 @@
<column name="type" type="VARCHAR" size="30" required="true" /> <column name="type" type="VARCHAR" size="30" required="true" />
<column name="file" type="VARCHAR" size="30" required="true" /> <column name="file" type="VARCHAR" size="30" required="true" />
<column name="user_id" type="INTEGER" required="false" /> <column name="user_id" type="INTEGER" required="false" />
<column name="commits" type="INTEGER" required="true" defaultValue="0" />
<foreign-key foreignTable="user" onDelete="setnull" onUpdate="cascade"> <foreign-key foreignTable="user" onDelete="setnull" onUpdate="cascade">
<reference local="user_id" foreign="id"/> <reference local="user_id" foreign="id"/>
</foreign-key> </foreign-key>

View file

@ -149,7 +149,7 @@ class Gist
$gist->setUser($user); $gist->setUser($user);
} }
$gist->save(); $gist->commit()->save();
return $gist; return $gist;
} }
@ -170,9 +170,30 @@ class Gist
->add($gist->getFile()) ->add($gist->getFile())
->commit('Update'); ->commit('Update');
$gist->commit()->save();
return $gist; 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. * Highlight the content.
* *