From 014d98bc9699d5667b15603001c8d1ed56611a29 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Wed, 16 Jul 2014 16:29:44 +0200 Subject: [PATCH] Implement releases in git-rebase Note: the parameter repository needs to be set to make this work. --- Mage/Task/AbstractTask.php | 18 +++++ .../Deployment/Strategy/GitRebaseTask.php | 67 +++++++++++++------ 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/Mage/Task/AbstractTask.php b/Mage/Task/AbstractTask.php index 69ba25f..bf01c83 100644 --- a/Mage/Task/AbstractTask.php +++ b/Mage/Task/AbstractTask.php @@ -214,4 +214,22 @@ abstract class AbstractTask return $this->runCommandLocal($command, $output); } } + + /** + * adds a cd to the needed release if we work with releases. + * + * @param string $command + * @return string + */ + protected function getReleasesAwareCommand($command) + { + if ($this->getConfig()->release('enabled', false) == true) { + $releasesDirectory = $this->getConfig()->release('directory', 'releases'); + + $deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId(); + return 'cd ' . $deployToDirectory . ' && ' . $command; + } + + return $command; + } } diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php index 5bcbd8e..f3ffbde 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php @@ -18,12 +18,12 @@ use Mage\Task\Releases\IsReleaseAware; * * @author Oscar Reales */ -class GitRebaseTask extends AbstractTask implements IsReleaseAware +class GitRebaseTask extends BaseStrategyTaskAbstract implements IsReleaseAware { - /** - * (non-PHPdoc) - * @see \Mage\Task\AbstractTask::getName() - */ + /** + * (non-PHPdoc) + * @see \Mage\Task\AbstractTask::getName() + */ public function getName() { return 'Deploy via Git Rebase [built-in]'; @@ -34,44 +34,69 @@ class GitRebaseTask extends AbstractTask implements IsReleaseAware * @see \Mage\Task\AbstractTask::run() */ public function run() - { - $branch = $this->getParameter('branch', 'master'); - $remote = $this->getParameter('remote', 'origin'); - - // Fetch Remote - $command = 'git fetch ' . $remote; + { + $this->checkOverrideRelease(); + $excludes = $this->getExcludes(); + + // If we are working with releases + $deployToDirectory = $this->getConfig()->deployment('to'); + if ($this->getConfig()->release('enabled', false) == true) { + $releasesDirectory = $this->getConfig()->release('directory', 'releases'); + + $deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/') + . '/' . $releasesDirectory + . '/' . $this->getConfig()->getReleaseId(); + $this->runCommandRemote('mkdir -p ' . $releasesDirectory . '/' . $this->getConfig()->getReleaseId()); + } + + $branch = $this->getParameter('branch', 'master'); + $remote = $this->getParameter('remote', 'origin'); + + // Fetch Remote + $command = $this->getReleasesAwareCommand('git fetch ' . $remote); $result = $this->runCommandRemote($command); + if ($result === false) { + $repository = $this->getConfig()->deployment('repository'); + if ($repository) { + $command = $this->getReleasesAwareCommand('git clone ' . $repository . ' .'); + $result = $this->runCommandRemote($command); + + $command = $this->getReleasesAwareCommand('git fetch ' . $remote); + $result = $this->runCommandRemote($command); + } + } + // Checkout - $command = 'git checkout ' . $branch; + $command = $this->getReleasesAwareCommand('git checkout ' . $branch); $result = $this->runCommandRemote($command) && $result; // Check Working Copy status $stashed = false; $status = ''; - $command = 'git checkout ' . $branch; + $command = $this->getReleasesAwareCommand('git checkout ' . $branch); $result = $this->runCommandRemote($command) && $result; // Stash if Working Copy is not clean if(!$status) { - $stashResult = ''; - $command = 'git stash'; - $result = $this->runCommandRemote($command, $stashResult) && $result; - if($stashResult != "No local changes to save") { + $stashResult = ''; + $command = $this->getReleasesAwareCommand('git stash'); + $result = $this->runCommandRemote($command, $stashResult) && $result; + if($stashResult != "No local changes to save") { $stashed = true; } } // Rebase - $command = 'git rebase ' . $remote . '/' . $branch; + $command = $this->getReleasesAwareCommand('git rebase ' . $remote . '/' . $branch); $result = $this->runCommandRemote($command) && $result; // If Stashed, restore. if ($stashed) { - $command = 'git stash pop'; - $result = $this->runCommandRemote($command) && $result; + $command = $this->getReleasesAwareCommand('git stash pop'); + $result = $this->runCommandRemote($command) && $result; } return $result; } -} \ No newline at end of file +}