magallanes/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php

109 lines
3.3 KiB
PHP
Raw Normal View History

2012-12-26 16:15:37 +01:00
<?php
2013-11-05 17:12:09 +01:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Scm;
use Mage\Task\AbstractTask;
use Mage\Task\SkipException;
use Mage\Task\ErrorWithMessageException;
/**
* Task for Changing the Branch of the SCM
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class ChangeBranchTask extends AbstractTask
2012-12-26 16:15:37 +01:00
{
2014-08-06 19:01:39 +02:00
/**
* Branch the executiong began with
* @var string
*/
protected static $startingBranch = 'master';
/**
* Name of the Task
* @var string
*/
private $name = 'SCM Changing branch [built-in]';
/**
* (non-PHPdoc)
* @see \Mage\Task\AbstractTask::getName()
*/
2012-12-26 16:15:37 +01:00
public function getName()
{
return $this->name;
2012-12-26 16:15:37 +01:00
}
/**
* (non-PHPdoc)
* @see \Mage\Task\AbstractTask::init()
*/
2012-12-26 16:15:37 +01:00
public function init()
{
2014-08-06 19:01:39 +02:00
$scmType = $this->getConfig()->general('scm');
2012-12-26 16:15:37 +01:00
switch ($scmType) {
case 'git':
$this->name = 'SCM Changing branch (GIT) [built-in]';
2012-12-26 16:15:37 +01:00
break;
}
}
/**
* Changes the Branch of the SCM
* @see \Mage\Task\AbstractTask::run()
*/
2012-12-26 16:15:37 +01:00
public function run()
{
$preCommand = 'cd ' . $this->getConfig()->deployment('from', './') . '; ';
switch ($this->getConfig()->general('scm')) {
2012-12-26 16:15:37 +01:00
case 'git':
2014-08-06 19:01:39 +02:00
if ($this->getParameter('_changeBranchRevert', false)) {
$command = $preCommand . 'git checkout ' . self::$startingBranch;
2014-08-06 19:01:39 +02:00
$result = $this->runCommandLocal($command);
} else {
$command = $preCommand . 'git branch | grep \'*\' | cut -d\' \' -f 2';
2014-08-06 19:01:39 +02:00
$currentBranch = 'master';
$result = $this->runCommandLocal($command, $currentBranch);
self::$startingBranch = $currentBranch;
2014-08-06 19:01:39 +02:00
$scmData = $this->getConfig()->deployment('scm', false);
if ($result && is_array($scmData) && isset($scmData['branch']) && $scmData['branch'] != $currentBranch) {
$command = $preCommand . 'git branch | grep \'' . $scmData['branch'] . '\' | tr -s \' \' | sed \'s/^[ ]//g\'';
2014-08-06 19:01:39 +02:00
$isBranchTracked = '';
$result = $this->runCommandLocal($command, $isBranchTracked);
if ($isBranchTracked == '') {
throw new ErrorWithMessageException('The branch <purple>' . $scmData['branch'] . '</purple> must be tracked.');
}
$branch = $this->getParameter('branch', $scmData['branch']);
$command = $preCommand . 'git checkout ' . $branch;
2014-08-06 19:01:39 +02:00
$result = $this->runCommandLocal($command) && $result;
} else {
throw new SkipException;
}
}
2012-12-26 16:15:37 +01:00
break;
default:
throw new SkipException;
2012-12-26 16:15:37 +01:00
break;
}
$this->getConfig()->reload();
return $result;
}
}