GetSource.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $output->writeln("<error>" . $e->getMessage() . "</error>");
  49. $git->init($git_path);
  50. }
  51. $git->setRepository($git_path);
  52. try {
  53. $git->remote->rm($input->getOption("remote-name"));
  54. } catch (GitException $e) {
  55. $output->writeln("<error>" . $e->getMessage() . "</error>");
  56. }
  57. $git->remote->add($input->getOption("remote-name"), $url);
  58. try {
  59. $git->fetch($input->getOption("remote-name"));
  60. } catch (GitException $e) {
  61. $output->writeln("<error>" . $e->getMessage() . "</error>");
  62. }
  63. $branches = $git->branch(array("all" => false, "remotes" => true));
  64. $branch_from_options = $input->getOption("branch");
  65. if (!empty($branch_from_options)) {
  66. $git->checkout($branch_from_options);
  67. $conf["branch"] = $branch_from_options;
  68. } else if (strtolower($conf["branch"]) != "master"){
  69. try {
  70. $git->checkout->create($conf["branch"]);
  71. } catch (GitException $e) {
  72. $output->writeln($e->getTraceAsString());
  73. die;
  74. }
  75. $git->merge("origin/" . $conf["branch"]);
  76. }
  77. if (isset($branches["remotes/" . $input->getOption("remote-name") . "/" . $conf["branch"]])) {
  78. $git->merge($input->getOption("remote-name") . "/" . $conf["branch"]);
  79. }
  80. $status = $git->status();
  81. if (isset($status["changes"]) and !empty($status["changes"])) {
  82. foreach ($status["changes"] as $change) {
  83. $output->writeln("\t" . $change["file"] . " is not commited");
  84. }
  85. } else if ($input->getOption("push")) {
  86. $git->push($input->getOption("remote-name"), $conf["branch"]);
  87. foreach ($git->tag() as $tag) {
  88. $git->push($input->getOption("remote-name"), $tag, array('force' => true));
  89. }
  90. }
  91. }
  92. }
  93. }