Created
October 3, 2012 16:30
-
-
Save GromNaN/3828087 to your computer and use it in GitHub Desktop.
Satis Mirror repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of Satis. | |
* | |
* (c) Jordi Boggiano <j.boggiano@seld.be> | |
* Nils Adermann <naderman@naderman.de> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Composer\Satis\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Command\Command; | |
use Composer\Console\Application as ComposerApplication; | |
use Composer\Package\Dumper\ArrayDumper; | |
use Composer\Package\AliasPackage; | |
use Composer\Package\LinkConstraint\VersionConstraint; | |
use Composer\Json\JsonFile; | |
use Composer\IO\IOInterface; | |
use Composer\IO\ConsoleIO; | |
use Composer\Util\ProcessExecutor; | |
/** | |
* @author Jordi Boggiano <j.boggiano@seld.be> | |
*/ | |
class MirrorCommand extends Command | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('mirror') | |
->setDescription('Mirror Git repositories') | |
->setDefinition(array( | |
new InputArgument('file', InputArgument::REQUIRED, 'Json file to use'), | |
new InputArgument('build-dir', InputArgument::REQUIRED, 'Location where to clone repositories'), | |
)) | |
->setHelp(<<<EOT | |
The <info>mirror</info> command reads the given json file and | |
mirror the Git repositories. | |
EOT | |
) | |
; | |
} | |
/** | |
* @param InputInterface $input The input instance | |
* @param OutputInterface $output The output instance | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$dir = $input->getArgument('build-dir'); | |
$json = new JsonFile($input->getArgument('file')); | |
$data = $json->read(); | |
$this->process = new ProcessExecutor(); | |
foreach ($data['mirrors'] as $mirror) { | |
// Fetch | |
if (is_dir($repo = $dir . '/' . preg_replace('/[^\w]+/', '-', strtolower(trim($mirror['target']))))) { | |
$this->runCommand(sprintf('cd %s && git branch --no-color | grep -v \* | xargs git branch -D && git fetch origin "+refs/heads/*:refs/heads/*"', $repo), $output); | |
} else { | |
$this->runCommand(sprintf('git clone --bare %s %s && cd %s && git remote rm origin && git remote add --mirror origin %s', $mirror['source'], $repo, $repo, $mirror['source']), $output); | |
} | |
// Push | |
$this->runCommand(sprintf('cd %s && git push --mirror %s', $repo, $mirror['target']), $output); | |
} | |
} | |
/** | |
* @param string $command The command to execute | |
* @param OutputInterface $output The output instance | |
*/ | |
protected function runCommand($command, OutputInterface $output) | |
{ | |
$output->writeln('<info>run</info> ' . str_replace('&&', '<info>&&</info>', $command)); | |
try { | |
return $this->process->execute($command); | |
} catch (\RuntimeException $e) { | |
return $e->getMessage(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment