setName('get:source')
->setDescription('Get the source using a ini file.')
->setHelp('This command allows you to fetch code based on the git.ini file configuration...')
->addArgument('ini_file', InputArgument::REQUIRED, 'The ini file from where to get the source code config.')
->addOption('remote-name', null, InputOption::VALUE_REQUIRED, 'Rename the remote to this name.', "upstream")
->addOption('push', null, InputOption::VALUE_NONE, 'Push to the remote.')
->addOption('timeout', null, InputOption::VALUE_OPTIONAL, 'Git process timeout in seconds', 10)
->addOption('branch', null, InputOption::VALUE_OPTIONAL, 'get a diferente branch from the specified on the ini-file', "");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = $input->getArgument("ini_file");
$realpath = realpath($file);
$dirname = dirname($realpath);
if (!chdir($dirname)) {
throw new \Exception("Can't change working directory to " . $dirname);
}
$content = parse_ini_file($realpath, true);
foreach ($content as $sec => $conf) {
$git = new Git();
$git->setTimeout($input->getOption("timeout"));
$git_path = $dirname . '/' . $sec;
$url = $conf["url"];
// Verifico si tiene ssh como protocolo y si la url es de gogs
$url_info = parse_url($url);
if (isset($url_info["host"]) and (!isset($url_info["scheme"]) or empty($url_info["scheme"]))) {
$protocol = 'ssh://';
$url = $protocol . $url;
}
try {
$output->writeln($url . " -> " . $git_path);
$git->clone($url, $git_path);
} catch (GitException $e) {
$output->writeln("" . $e->getMessage() . "");
$git->init($git_path);
}
$git->setRepository($git_path);
try {
$git->remote->rm($input->getOption("remote-name"));
} catch (GitException $e) {
$output->writeln("" . $e->getMessage() . "");
}
$git->remote->add($input->getOption("remote-name"), $url);
try {
$git->fetch($input->getOption("remote-name"));
} catch (GitException $e) {
$output->writeln("" . $e->getMessage() . "");
}
$branches = $git->branch(array("all" => false, "remotes" => true));
$branch_from_options = $input->getOption("branch");
if (!empty($branch_from_options)) {
$git->checkout($branch_from_options);
$conf["branch"] = $branch_from_options;
} else if (strtolower($conf["branch"]) != "master"){
try {
$git->checkout->create($conf["branch"]);
} catch (GitException $e) {
$output->writeln($e->getTraceAsString());
die;
}
$git->merge("origin/" . $conf["branch"]);
}
if (isset($branches["remotes/" . $input->getOption("remote-name") . "/" . $conf["branch"]])) {
$git->merge($input->getOption("remote-name") . "/" . $conf["branch"]);
}
$status = $git->status();
if (isset($status["changes"]) and !empty($status["changes"])) {
foreach ($status["changes"] as $change) {
$output->writeln("\t" . $change["file"] . " is not commited");
}
} else if ($input->getOption("push")) {
$git->push($input->getOption("remote-name"), $conf["branch"]);
foreach ($git->tag() as $tag) {
$git->push($input->getOption("remote-name"), $tag, array('force' => true));
}
}
}
}
}