Skip to content

Commit

Permalink
The "reparse" command support for revision ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Oct 30, 2024
1 parent 1ba5720 commit 22cc0de
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
45 changes: 39 additions & 6 deletions src/SVNBuddy/Command/ReparseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


use ConsoleHelpers\ConsoleKit\Exception\CommandException;
use ConsoleHelpers\SVNBuddy\Repository\Parser\RevisionListParser;
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLog;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -28,6 +29,13 @@ class ReparseCommand extends AbstractCommand
*/
private $_revisionLog;

/**
* Revision list parser.
*
* @var RevisionListParser
*/
private $_revisionListParser;

/**
* {@inheritdoc}
*/
Expand All @@ -43,15 +51,29 @@ protected function configure()
'.'
)
->addOption(
'revision',
'revisions',
'r',
InputOption::VALUE_REQUIRED,
'Reparse specified revision'
'List of revision(-s) and/or revision range(-s) to reparse, e.g. <comment>53324</comment>, <comment>1224-4433</comment> or <comment>all</comment>'
);

parent::configure();
}

/**
* Prepare dependencies.
*
* @return void
*/
protected function prepareDependencies()
{
parent::prepareDependencies();

$container = $this->getContainer();

$this->_revisionListParser = $container['revision_list_parser'];
}

/**
* {@inheritdoc}
*/
Expand All @@ -69,13 +91,24 @@ public function initialize(InputInterface $input, OutputInterface $output)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$revision = $this->io->getOption('revision');
$revisions = $this->io->getOption('revisions');

if ( !$revision ) {
throw new CommandException('The "revision" option is mandatory.');
if ( !$revisions ) {
throw new CommandException('The "revisions" option is mandatory.');
}

$this->_revisionLog->reparse($revision);
// FIXME: Not checking, that given revisions belong to a working copy.
$revisions = $this->_revisionListParser->expandRanges($this->getList($revisions));

foreach ( $this->_revisionListParser->collapseRanges($revisions) as $revision_range ) {
if ( strpos($revision_range, '-') === false ) {
$this->_revisionLog->reparse($revision_range, $revision_range);
}
else {
list($from_revision, $to_revision) = explode('-', $revision_range, 2);
$this->_revisionLog->reparse($from_revision, $to_revision);
}
}
}

}
40 changes: 40 additions & 0 deletions src/SVNBuddy/Repository/Parser/RevisionListParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,44 @@ public function expandRanges(array $revisions, $range_separator = '-')
return array_keys($ret);
}

/**
* Collapses ranges.
*
* @param array $revisions Revisions.
* @param string $range_separator Range separator.
*
* @return array
*/
public function collapseRanges(array $revisions, $range_separator = '-')
{
sort($revisions, SORT_NUMERIC);
$revisions = array_map('intval', $revisions);

$ret = array();
$range_start = $range_end = null;

foreach ( $revisions as $revision ) {
// New range detected.
if ( $range_start === null ) {
$range_start = $range_end = $revision;
continue;
}

// Expanding existing range.
if ( $range_end + 1 === $revision ) {
$range_end = $revision;
continue;
}

$ret[] = $range_start === $range_end ? $range_start : $range_start . $range_separator . $range_end;
$range_start = $range_end = $revision;
}

if ( $range_start !== null && $range_end !== null ) {
$ret[] = $range_start === $range_end ? $range_start : $range_start . $range_separator . $range_end;
}

return $ret;
}

}
7 changes: 4 additions & 3 deletions src/SVNBuddy/Repository/RevisionLog/RevisionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,20 @@ protected function getForceRefreshFlag()
/**
* Reparses a revision.
*
* @param integer $revision Revision.
* @param integer $from_revision From revision.
* @param integer $to_revision To revision.
*
* @return void
* @throws \LogicException When no plugins are registered.
*/
public function reparse($revision)
public function reparse($from_revision, $to_revision)
{
if ( !$this->_plugins ) {
throw new \LogicException('Please register at least one revision log plugin.');
}

$this->_databaseReady();
$this->_queryRevisionData($revision, $revision, true);
$this->_queryRevisionData($from_revision, $to_revision, true);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/SVNBuddy/Repository/Parser/RevisionListParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ public function testMultiDigitRevisions()
$this->assertEquals($expected, $actual);
}

public function testCollapsing()
{
$expected = array('10-12', 15, '17-19', 25);
$actual = $this->_revisionListParser->collapseRanges(array(18, 10, 11, 15, 17, 12, 19, 25));

$this->assertEquals($expected, $actual);
}

}

0 comments on commit 22cc0de

Please sign in to comment.