/** * {@inheritdoc} */ public function run($callback = null) { $event = new Event\GitEvent($this->git, $this, $this->command); $dispatcher = $this->git->getDispatcher(); try { // Throw the "git.command.prepare" event prior to executing. $dispatcher->dispatch(Event\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 ($this->command->notBypassed()) { parent::run($callback); if ($this->isSuccessful()) { $dispatcher->dispatch(Event\GitEvents::GIT_SUCCESS, $event); } else { $output = $this->getErrorOutput(); if (trim($output) == '') { $output = $this->getOutput(); } throw new \RuntimeException($output); } } else { $dispatcher->dispatch(Event\GitEvents::GIT_BYPASS, $event); } } catch (\RuntimeException $e) { $dispatcher->dispatch(Event\GitEvents::GIT_ERROR, $event); throw new GitException($e->getMessage()); } }
/** * Runs a Git command. * * @param \GitWrapper\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 \GitWrapper\GitException * * @see Process */ public function run(GitCommand $command, $cwd = null) { $wrapper = $this; $process = new GitProcess($this, $command, $cwd); $process->run(function ($type, $buffer) use($wrapper, $process, $command) { $event = new Event\GitOutputEvent($wrapper, $process, $command, $type, $buffer); $wrapper->getDispatcher()->dispatch(Event\GitEvents::GIT_OUTPUT, $event); }); return $command->notBypassed() ? $process->getOutput() : ''; }
/** * 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(); }