Esempio n. 1
0
 /**
  * {@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());
     }
 }
Esempio n. 2
0
 /**
  * @see https://github.com/cpliakas/git-wrapper/issues/50
  */
 public function testMultiOption()
 {
     $git = GitCommand::getInstance('test-command')->setOption('test-arg', array(true, true));
     $expected = 'test-command --test-arg --test-arg';
     $commandLine = $git->getCommandLine();
     $this->assertEquals($expected, $commandLine);
 }
Esempio n. 3
0
 public function testEvent()
 {
     $process = new Process('');
     $command = GitCommand::getInstance();
     $event = new GitEvent($this->_wrapper, $process, $command);
     $this->assertEquals($this->_wrapper, $event->getWrapper());
     $this->assertEquals($process, $event->getProcess());
     $this->assertEquals($command, $event->getCommand());
 }
 public function testLogBypassedCommand()
 {
     $logger = new TestLogger();
     $this->wrapper->addLoggerListener(new GitLoggerListener($logger));
     $command = GitCommand::getInstance('status', array('s' => true));
     $command->bypass();
     $this->wrapper->run($command);
     $this->assertEquals('Git command bypassed', $logger->messages[1]);
     $this->assertArrayHasKey('command', $logger->contexts[1]);
     $this->assertEquals(LogLevel::INFO, $logger->levels[1]);
 }
Esempio n. 5
0
 /**
  * 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());
 }
Esempio n. 6
0
 /**
  * @param $data
  *
  * @dataProvider getGitLsFilesOutputData
  */
 public function testItCanFilterSomeFileNames($data)
 {
     $gitWrapper = $this->getMockBuilder('\\GitWrapper\\GitWrapper')->disableOriginalConstructor()->setMethods(array('run'))->getMock();
     $terminalOutput = implode(PHP_EOL, $data);
     $gitWrapper->expects($this->any())->method('run')->withAnyParameters()->willReturn($terminalOutput);
     $command = GitCommand::getInstance();
     $command->setFlag('version')->setDirectory('.');
     $this->assertSame($terminalOutput, $gitWrapper->run($command));
     $git = $this->getTargetMockBuilder()->disableOriginalConstructor()->setMethods(array('_getGitWrapper'))->getMock();
     $git->expects($this->any())->method('_getGitWrapper')->willReturn($gitWrapper);
     // assert empty dir
     $fs = new Filesystem();
     $fs->remove($git->getTempPath());
     // create directory
     mkdir($git->getTempPath(), 0777, true);
     // set wrapper for temporary path
     $reflectObject = new \ReflectionObject($git);
     $property = $reflectObject->getProperty('_fileWrapper');
     $property->setAccessible(true);
     $property->setValue($git, new Directory($git->getTempPath()));
     $this->assertEquals($data, array_keys($git->getAllFileNames()));
 }
Esempio n. 7
0
 /**
  * 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() : '';
 }
Esempio n. 8
0
 /**
  * @depends testGitCommit
  */
 public function testGitCommitArgs()
 {
     $commit = GitCommand::getInstance('commit', 'files', array('m' => 'log message'));
     $expected = "commit -m 'log message' 'files'";
     $this->assertEquals($expected, $commit->getCommandLine());
 }
Esempio n. 9
0
 /**
  * @expectedException \GitWrapper\GitException
  */
 public function testGitRunDirectoryError()
 {
     $command = GitCommand::getInstance();
     $command->setFlag('version');
     $command->setDirectory('/some/bad/directory');
     $this->_wrapper->run($command);
 }
Esempio n. 10
0
 /**
  * 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();
 }