mirror of https://github.com/hauke68/Magallanes
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.0 KiB
111 lines
3.0 KiB
12 years ago
|
<?php
|
||
11 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
11 years ago
|
namespace Mage\Task\BuiltIn\Scm;
|
||
|
|
||
|
use Mage\Task\AbstractTask;
|
||
|
use Mage\Task\SkipException;
|
||
|
use Mage\Task\ErrorWithMessageException;
|
||
|
|
||
|
use Exception;
|
||
|
|
||
|
/**
|
||
|
* Task for Changing the Branch of the SCM
|
||
|
*
|
||
|
* @author Andrés Montañez <andres@andresmontanez.com>
|
||
|
*/
|
||
|
class ChangeBranchTask extends AbstractTask
|
||
12 years ago
|
{
|
||
11 years ago
|
/**
|
||
|
* Branch the executiong began with
|
||
|
* @var string
|
||
|
*/
|
||
11 years ago
|
protected static $startingBranch = 'master';
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Name of the Task
|
||
|
* @var string
|
||
|
*/
|
||
|
private $name = 'SCM Changing branch [built-in]';
|
||
|
|
||
|
/**
|
||
|
* (non-PHPdoc)
|
||
|
* @see \Mage\Task\AbstractTask::getName()
|
||
|
*/
|
||
12 years ago
|
public function getName()
|
||
|
{
|
||
11 years ago
|
return $this->name;
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* (non-PHPdoc)
|
||
|
* @see \Mage\Task\AbstractTask::init()
|
||
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
11 years ago
|
$scmType = $this->getConfig()->general('scm');
|
||
12 years ago
|
|
||
11 years ago
|
switch ($scmType) {
|
||
|
case 'git':
|
||
|
$this->name = 'SCM Changing branch (GIT) [built-in]';
|
||
12 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Changes the Branch of the SCM
|
||
|
* @see \Mage\Task\AbstractTask::run()
|
||
|
*/
|
||
12 years ago
|
public function run()
|
||
|
{
|
||
11 years ago
|
$scmConfig = $this->getConfig()->general('scm', array());
|
||
|
switch ((isset($scmConfig['type']) ? $scmConfig['type'] : false)) {
|
||
12 years ago
|
case 'git':
|
||
12 years ago
|
if ($this->getParameter('_changeBranchRevert', false)) {
|
||
11 years ago
|
$command = 'git checkout ' . self::$startingBranch;
|
||
11 years ago
|
$result = $this->runCommandLocal($command);
|
||
12 years ago
|
|
||
12 years ago
|
} else {
|
||
|
$command = 'git branch | grep \'*\' | cut -d\' \' -f 2';
|
||
|
$currentBranch = 'master';
|
||
11 years ago
|
$result = $this->runCommandLocal($command, $currentBranch);
|
||
12 years ago
|
|
||
12 years ago
|
$scmData = $this->getConfig()->deployment('scm', false);
|
||
|
|
||
11 years ago
|
if ($result && is_array($scmData) && isset($scmData['branch']) && $scmData['branch'] != $currentBranch) {
|
||
|
$command = 'git branch | grep \'' . $scmData['branch'] . '\' | tr -s \' \' | sed \'s/^[ ]//g\'';
|
||
|
$isBranchTracked = '';
|
||
11 years ago
|
$result = $this->runCommandLocal($command, $isBranchTracked);
|
||
11 years ago
|
|
||
|
if ($isBranchTracked == '') {
|
||
11 years ago
|
throw new ErrorWithMessageException('The branch <purple>' . $scmData['branch'] . '</purple> must be tracked.');
|
||
11 years ago
|
}
|
||
|
|
||
|
$branch = $this->getParameter('branch', $scmData['branch']);
|
||
|
$command = 'git checkout ' . $branch;
|
||
11 years ago
|
$result = $this->runCommandLocal($command);
|
||
12 years ago
|
|
||
11 years ago
|
self::$startingBranch = $currentBranch;
|
||
12 years ago
|
} else {
|
||
11 years ago
|
throw new SkipException;
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
break;
|
||
|
|
||
|
default:
|
||
11 years ago
|
throw new SkipException;
|
||
12 years ago
|
break;
|
||
|
}
|
||
|
|
||
|
$this->getConfig()->reload();
|
||
|
|
||
|
return $result;
|
||
|
}
|
||
|
}
|