GetSource.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace FD3;
  3. use FD3\Git;
  4. use PHPGit\Exception\GitException;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. class GetSource extends Command
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('get:source')
  16. ->setDescription('Get the source using a ini file.')
  17. ->setHelp('This command allows you to fetch code based on the git.ini file configuration...')
  18. ->addArgument('ini_file', InputArgument::REQUIRED, 'The ini file from where to get the source code config.')
  19. ->addOption('remote-name', null, InputOption::VALUE_REQUIRED, 'Rename the remote to this name.', "upstream")
  20. ->addOption('push', null, InputOption::VALUE_NONE, 'Push to the remote.')
  21. ->addOption('timeout', null, InputOption::VALUE_OPTIONAL, 'Git process timeout in seconds', 10)
  22. ->addOption('branch', null, InputOption::VALUE_OPTIONAL, 'get a diferente branch from the specified on the ini-file', "");
  23. }
  24. protected function execute(InputInterface $input, OutputInterface $output)
  25. {
  26. $file = $input->getArgument("ini_file");
  27. $realpath = realpath($file);
  28. $dirname = dirname($realpath);
  29. if (!chdir($dirname)) {
  30. throw new \Exception("Can't change working directory to " . $dirname);
  31. }
  32. $content = parse_ini_file($realpath, true);
  33. foreach ($content as $sec => $conf) {
  34. $git = new Git();
  35. $git->setTimeout($input->getOption("timeout"));
  36. $git_path = $dirname . '/' . $sec;
  37. $url = $conf["url"];
  38. // Verifico si tiene ssh como protocolo y si la url es de gogs
  39. $url_info = parse_url($url);
  40. if (isset($url_info["host"]) and (!isset($url_info["scheme"]) or empty($url_info["scheme"]))) {
  41. $protocol = 'ssh://';
  42. $url = $protocol . $url;
  43. }
  44. try {
  45. $output->writeln($url . " -> " . $git_path);
  46. $git->clone($url, $git_path);
  47. } catch (GitException $e) {
  48. $git->init($git_path);
  49. }
  50. $git->setRepository($git_path);
  51. try {
  52. $git->remote->rm($input->getOption("remote-name"));
  53. } catch (GitException $e) {
  54. }
  55. $git->remote->add($input->getOption("remote-name"), $url);
  56. try {
  57. $git->fetch($input->getOption("remote-name"));
  58. } catch (GitException $e) {
  59. $output->writeln("<error>" . $e->getMessage() . "</error>");
  60. }
  61. $branch_from_options = $input->getOption("branch");
  62. if (!empty($branch_from_options)) {
  63. $git->checkout($branch_from_options);
  64. $conf["branch"] = $branch_from_options;
  65. } else {
  66. $git->checkout($conf["branch"]);
  67. }
  68. $branches = $git->branch(array("all" => false, "remotes" => true));
  69. if (isset($branches["remotes/" . $input->getOption("remote-name") . "/" . $conf["branch"]])) {
  70. $git->merge($input->getOption("remote-name") . "/" . $conf["branch"]);
  71. }
  72. $status = $git->status();
  73. if (isset($status["changes"]) and !empty($status["changes"])) {
  74. foreach ($status["changes"] as $change) {
  75. $output->writeln("\t" . $change["file"] . " is not commited");
  76. }
  77. } else if ($input->getOption("push")) {
  78. $git->push($input->getOption("remote-name"), $conf["branch"]);
  79. foreach ($git->tag() as $tag) {
  80. $git->push($input->getOption("remote-name"), $tag, array('force' => true));
  81. }
  82. }
  83. }
  84. }
  85. }