Bläddra i källkod

Merged in FD3-311 (pull request #16)

FD3-311 se agrega protocolo ssh a la url del repositorio. Se agrega timeout al proceso de git
Guillermo Espinoza 7 år sedan
förälder
incheckning
ad7c9c540f
2 ändrade filer med 112 tillägg och 59 borttagningar
  1. 64 59
      tools/src/GetSource.php
  2. 48 0
      tools/src/Git.php

+ 64 - 59
tools/src/GetSource.php

@@ -8,80 +8,85 @@ use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 
-use PHPGit\Git;
+use FD3\Git;
 use PHPGit\Exception\GitException;
 
 class GetSource extends Command
 {
     protected function configure()
     {
-        $this 
+        $this
         ->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.')
-    ;
+        ->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', 60)
+        ;
     }
 
     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_path = $dirname.'/'.$sec;
-
-		try{
-			$output->writeln($conf["url"]. " -> " . $git_path);
-			$git->clone($conf["url"], $git_path);
-		}catch (GitException $e){
-			//throw $e;
-			//$output->write("repo $sec exists \n");
-		}
-		$git->setRepository($git_path);
-
-		try{
-			$git->remote->rm($input->getOption("remote-name"));
-		}catch (GitException $e){
-			
-		}
-		$git->remote->add($input->getOption("remote-name"), $conf["url"]);
-		$git->fetch($input->getOption("remote-name"));
-		
-
-		$git->checkout($conf["branch"]);
-		$branches = $git->branch(array("all" => false,"remotes" => true));
-
-		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));
-			}
-		}
-	}
-	
+        $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
+            $protocol = 'ssh://';
+            if (substr($url, 0, strlen($protocol)) !== $protocol && strpos($url, 'gogs.infra.flowdat.com') !== false) {
+                $url = $protocol . $url;
+            }
+            try {
+                $output->writeln($url . " -> " . $git_path);
+                $git->clone($url, $git_path);
+            } catch (GitException $e) {
+                throw $e;
+                $output->write("repo $sec exists \n");
+            }
+            $git->setRepository($git_path);
+
+            try {
+                $git->remote->rm($input->getOption("remote-name"));
+            } catch (GitException $e) {
+            }
+            $git->remote->add($input->getOption("remote-name"), $url);
+            $git->fetch($input->getOption("remote-name"));
+
+            $git->checkout($conf["branch"]);
+            $branches = $git->branch(array("all" => false,"remotes" => true));
+
+    		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));
+    			}
+    		}
+        }
+
     }
 }

+ 48 - 0
tools/src/Git.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace FD3;
+
+use PHPGit\Git as PHPGit;
+use Symfony\Component\Process\ProcessBuilder;
+
+class Git extends PHPGit
+{
+
+    /**
+    * @var int
+    */
+    private $timeout = 60;
+
+    /**
+    * @var int $timeout
+    *
+    * @return Git
+    */
+    public function setTimeout($timeout)
+    {
+        $this->timeout = $timeout;
+
+        return $this;
+    }
+
+    /**
+    * @return int
+    */
+    public function getTimeout()
+    {
+        return $this->timeout;
+    }
+
+    /**
+     * Returns an instance of ProcessBuilder
+     *
+     * @return ProcessBuilder
+     */
+    public function getProcessBuilder()
+    {
+        $pb = parent::getProcessBuilder();
+        $pb->setTimeout($this->timeout);
+
+        return $pb;
+    }
+}