/** * This will checkout a new branch based on $source with branch name of $new * * @param Sadekbaroudi\Gitorade\Branches\Branch $source can be Sadekbaroudi\Gitorade\Branches\BranchLocal or Sadekbaroudi\Gitorade\Branches\BranchRemote * @param Sadekbaroudi\Gitorade\Branches\BranchLocal $new the new local branch based on $source * @throws OperationStateException If any of the commands fail in the process */ public function checkoutNewBranch($source, $new) { $beforeMergeBranch = $this->currentBranch(); $this->stash(); // TODO: PHP 5.4 supports "new Foo()->method()->method()" // http://docs.php.net/manual/en/migration54.new-features.php $os = new OperationState(); $os->setExecute(array($this->getGit(), 'checkout'), array($source->fullBranchString())); $os->addExecute(array($this->getGit(), 'checkoutNewBranch'), array($new->getBranch())); $os->setUndo(array($this->getGit(), 'reset'), array(array('hard' => true))); $os->addUndo(array($this->getGit(), 'checkout'), array($beforeMergeBranch)); $os->addUndo(array($this->getGit(), 'branch'), array($new->getBranch(), array('D' => true))); $this->getOsm()->add($os); try { echo "checking out {$source}" . PHP_EOL; echo "checking out new local branch {$new}" . PHP_EOL; $this->getOsm()->execute($os); } catch (OperationStateException $e) { $this->getOsm()->undoAll(); throw $e; } }