GetSource.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace FD3;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use FD3\Git;
  9. use PHPGit\Exception\GitException;
  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. }
  25. protected function execute(InputInterface $input, OutputInterface $output)
  26. {
  27. $file = $input->getArgument("ini_file");
  28. $realpath = realpath($file);
  29. $dirname = dirname($realpath);
  30. if (!chdir($dirname)) {
  31. throw new \Exception("Can't change working directory to ".$dirname);
  32. }
  33. $content = parse_ini_file($realpath, true);
  34. foreach ($content as $sec => $conf) {
  35. $git = new Git();
  36. $git->setTimeout($input->getOption("timeout"));
  37. $git_path = $dirname.'/'.$sec;
  38. $url = $conf["url"];
  39. // Verifico si tiene ssh como protocolo y si la url es de gogs
  40. $url_info = parse_url($url);
  41. if(isset($url_info["host"]) and (!isset($url_info["scheme"]) or empty($url_info["scheme"]))){
  42. $protocol = 'ssh://';
  43. $url = $protocol . $url;
  44. }
  45. try {
  46. $output->writeln($url . " -> " . $git_path);
  47. $git->clone($url, $git_path);
  48. } catch (GitException $e){
  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. }
  56. $git->remote->add($input->getOption("remote-name"), $url);
  57. try {
  58. $git->fetch($input->getOption("remote-name"));
  59. } catch( GitException $e) {
  60. $output->writeln("<error>".$e->getMessage()."</error>");
  61. }
  62. $branch_from_options = $input->getOption("branch");
  63. if(!empty($branch_from_options)){
  64. $git->checkout($branch_from_options);
  65. $conf["branch"] = $branch_from_options;
  66. }else{
  67. $git->checkout($conf["branch"]);
  68. }
  69. $branches = $git->branch(array("all" => false,"remotes" => true));
  70. if(isset($branches["remotes/" . $input->getOption("remote-name") . "/" . $conf["branch"]])){
  71. $git->merge($input->getOption("remote-name")."/".$conf["branch"]);
  72. }
  73. $status = $git->status();
  74. if(isset($status["changes"]) and !empty($status["changes"])){
  75. foreach($status["changes"] as $change){
  76. $output->writeln("\t" . $change["file"] . " is not commited");
  77. }
  78. }else if($input->getOption("push")){
  79. $git->push($input->getOption("remote-name"), $conf["branch"]);
  80. foreach($git->tag() as $tag){
  81. $git->push($input->getOption("remote-name"), $tag, array('force' => true));
  82. }
  83. }
  84. }
  85. }
  86. }