Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Satis Mirror repositories
<?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
You can’t perform that action at this time.