コード例 #1
0
ファイル: GitProcess.php プロジェクト: shirone/git-wrapper
 /**
  * Constructs a GitProcess object.
  *
  * @param \GitWrapper\GitWrapper $git
  * @param \GitWrapper\GitCommand $command
  * @param string|null $cwd
  */
 public function __construct(GitWrapper $git, GitCommand $command, $cwd = null)
 {
     $this->git = $git;
     $this->command = $command;
     // Build the command line options, flags, and arguments.
     $binary = ProcessUtils::escapeArgument($git->getGitBinary());
     $commandLine = rtrim($binary . ' ' . $command->getCommandLine());
     // Resolve the working directory of the Git process. Use the directory
     // in the command object if it exists.
     if (null === $cwd) {
         if (null !== ($directory = $command->getDirectory())) {
             if (!($cwd = realpath($directory))) {
                 throw new GitException('Path to working directory could not be resolved: ' . $directory);
             }
         }
     }
     // Finalize the environment variables, an empty array is converted
     // to null which enherits the environment of the PHP process.
     $env = $git->getEnvVars();
     if (!$env) {
         $env = null;
     }
     parent::__construct($commandLine, $cwd, $env, null, $git->getTimeout(), $git->getProcOptions());
 }
コード例 #2
0
ファイル: GitWrapper.php プロジェクト: bazo/git-wrapper
 /**
  * Runs a Git command.
  *
  * @param GitCommand $command
  *   The Git command being executed.
  * @param string|null $cwd
  *   Explicitly specify the working directory of the Git process. Defaults
  *   to null which automatically sets the working directory based on the
  *   command being executed relative to the working copy.
  *
  * @return string
  *   The STDOUT returned by the Git command.
  *
  * @throws GitException
  *
  * @see Process
  */
 public function run(GitCommand $command, $cwd = null)
 {
     $event = null;
     try {
         // Build the command line options, flags, and arguments.
         $command_line = rtrim($this->gitBinary . ' ' . $command->getCommandLine());
         // Resolve the working directory of the Git process. Use the
         // directory in the command object if it exists.
         if (null === $cwd) {
             if (null !== ($directory = $command->getDirectory())) {
                 if (!($cwd = realpath($directory))) {
                     throw new GitException('Path to working directory could not be resolved: ' . $directory);
                 }
             }
         }
         // Finalize the environment variables, an empty array is converted
         // to null which enherits the environment of the PHP process.
         $env = $this->env ? $this->env : null;
         $process = new Process($command_line, $cwd, $env, null, $this->timeout, $this->procOptions);
         $event = new GitEvent($this, $process, $command);
         // Throw the "git.command.prepare" event prior to executing.
         $this->dispatcher->dispatch(GitEvents::GIT_PREPARE, $event);
         // Execute command if it is not flagged to be bypassed and throw the
         // "git.command.success" event, otherwise do not execute the comamnd
         // and throw the "git.command.bypass" event.
         if ($command->notBypassed()) {
             $process->run();
             if ($process->isSuccessful()) {
                 $this->dispatcher->dispatch(GitEvents::GIT_SUCCESS, $event);
             } else {
                 throw new \RuntimeException($process->getErrorOutput());
             }
         } else {
             $this->dispatcher->dispatch(GitEvents::GIT_BYPASS, $event);
         }
     } catch (\RuntimeException $e) {
         if ($event !== null) {
             // Throw the "git.command.error" event.
             $this->dispatcher->dispatch(GitEvents::GIT_ERROR, $event);
         }
         throw new GitException($e->getMessage());
     }
     return $process->getOutput();
 }