diff --git a/src/Vcs/Driver/GitDriver.php b/src/Vcs/Driver/GitDriver.php index 2aad75e..e7fbcc2 100644 --- a/src/Vcs/Driver/GitDriver.php +++ b/src/Vcs/Driver/GitDriver.php @@ -112,7 +112,7 @@ public function hasChangesSinceTag($tag, $branch, $path, OutputInterface $output { try { $this->getGit()->getAdapter()->execute('fetch', ['origin'], $path); - $this->checkoutBranch($branch, $path, $output); + $this->getGit()->getAdapter()->execute('branch', [$branch ,'origin/' . $branch, '-fq'], $path); $diff = $this->getGit()->getAdapter()->execute('diff', array('--ignore-all-space', $tag), $path); } catch (\RuntimeException $e) { if (false !== strpos($e->getMessage(), 'unknown revision or path')) { @@ -128,34 +128,6 @@ public function hasChangesSinceTag($tag, $branch, $path, OutputInterface $output return true; } - /** - * @param $branch - * @param $path - * @param OutputInterface $output - * @throws \Exception - */ - private function checkoutBranch($branch, $path, OutputInterface $output) - { - try { - $this->getGit()->getAdapter()->execute('checkout', ['-b', $branch ,'origin/' . $branch], $path); - } catch (\Exception $e) { - if (preg_match("/branch .+ already exists/", $e->getMessage()) === 1) { - $output->writeln( - sprintf( - 'checkout -b %s %s failed, because local branch "%s" already exists', - $branch, - 'origin/' . $branch, - $branch - ) - ); - $this->getGit()->getAdapter()->execute('checkout', [$branch], $path); - $output->writeln('checkout local branch: "'. $branch .'"'); - } else { - throw $e; - } - } - } - /** * @return Git */ diff --git a/test/Vcs/Driver/GitDriverTest.php b/test/Vcs/Driver/GitDriverTest.php index 6127d35..c9f7d44 100644 --- a/test/Vcs/Driver/GitDriverTest.php +++ b/test/Vcs/Driver/GitDriverTest.php @@ -187,8 +187,8 @@ public function shouldNotHaveChangesSinceTagWithNullValueFromGitAdapter() ); $adapter->expects($this->at(1))->method('execute')->with( - 'checkout', - array('-b', 'master', 'origin/master'), + 'branch', + array('master', 'origin/master', '-fq'), '/home/my/vcs/repo' ); @@ -214,95 +214,6 @@ public function shouldNotHaveChangesSinceTagWithNullValueFromGitAdapter() $this->assertFalse($driver->hasChangesSinceTag('0.2.5', 'master', '/home/my/vcs/repo', $output)); } - /** - * @test - */ - public function shouldCheckoutLocalBranchIfItAlreadyExists() - { - $adapter = $this->givenAnAdapter(); - $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') - ->disableOriginalConstructor() - ->getMock(); - - $adapter->expects($this->at(0))->method('execute')->with( - 'fetch', - array('origin'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(1))->method('execute')->with( - 'checkout', - array('-b', 'master', 'origin/master'), - '/home/my/vcs/repo' - )->willThrowException(new \Exception('fatal: branch master already exists')); - - $adapter->expects($this->at(2))->method('execute')->with( - 'checkout', - array('master'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(3))->method('execute')->with( - 'diff', - array('--ignore-all-space', '0.2.5'), - '/home/my/vcs/repo' - )->will($this->returnValue(null)); - - $git = $this->getMockBuilder('Webcreate\\Vcs\\Git') - ->disableOriginalConstructor() - ->setMethods(array('getAdapter')) - ->getMock(); - $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); - - $driver = $this->givenADriver(); - - $driver->expects($this->any())->method('getGit')->will( - $this->returnValue($git) - ); - - /** @var GitDriver $driver */ - $driver->hasChangesSinceTag('0.2.5', 'master', '/home/my/vcs/repo', $output); - } - - /** - * @test - * @expectedException \Exception - */ - public function shouldThrowExceptionWhileTryingToCheckoutBranch() - { - $adapter = $this->givenAnAdapter(); - $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') - ->disableOriginalConstructor() - ->getMock(); - - $adapter->expects($this->at(0))->method('execute')->with( - 'fetch', - array('origin'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(1))->method('execute')->with( - 'checkout', - array('-b', 'master', 'origin/master'), - '/home/my/vcs/repo' - )->willThrowException(new \Exception('fatal: something is wrong')); - - $git = $this->getMockBuilder('Webcreate\\Vcs\\Git') - ->disableOriginalConstructor() - ->setMethods(array('getAdapter')) - ->getMock(); - $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); - - $driver = $this->givenADriver(); - - $driver->expects($this->any())->method('getGit')->will( - $this->returnValue($git) - ); - - /** @var GitDriver $driver */ - $driver->hasChangesSinceTag('0.2.5', 'master', '/home/my/vcs/repo', $output); - } - /** * @test */ @@ -320,8 +231,8 @@ public function shouldNotHaveChangesSinceTagWithEmptyStringValueFromGitAdapter() ); $adapter->expects($this->at(1))->method('execute')->with( - 'checkout', - array('-b', 'master', 'origin/master'), + 'branch', + array('master', 'origin/master', '-fq'), '/home/my/vcs/repo' ); @@ -365,8 +276,8 @@ public function shouldHaveChangesSinceTag() ); $adapter->expects($this->at(1))->method('execute')->with( - 'checkout', - array('-b', 'master', 'origin/master'), + 'branch', + array('master', 'origin/master', '-fq'), '/home/my/vcs/repo' ); @@ -417,8 +328,8 @@ public function shouldHaveChangesSinceTagOnUnknownTag() ); $adapter->expects($this->at(1))->method('execute')->with( - 'checkout', - array('-b', 'master', 'origin/master'), + 'branch', + array('master', 'origin/master', '-fq'), '/home/my/vcs/repo' );