Created
May 15, 2013 13:40
-
-
Save radmiraal/5584078 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL ^ E_STRICT); | |
define('FLOW_PATH_ROOT', __DIR__ . DIRECTORY_SEPARATOR); | |
define('FLOW_PATH_PACKAGES', FLOW_PATH_ROOT . 'Packages' . DIRECTORY_SEPARATOR); | |
class Gerrit { | |
/** | |
* @var array | |
*/ | |
static protected $colors = array( | |
'green' => '0;32', | |
'red' => '0;31', | |
'yellow' => '0;33' | |
); | |
static public function checkoutGerritCommitDependenciesCommand($commitMessage) { | |
if (stripos($commitMessage, 'Depends:') !== FALSE) { | |
preg_match_all('/ | |
Depends:[ ]? | |
((?P<packageKey>[a-z0-9]*\.[a-z0-9\.]*)\/)? | |
(?P<changeId>I[a-z0-9]*) | |
(\/(?P<branch>[^\n]*))? | |
/xi', $commitMessage, $matches | |
); | |
foreach ($matches['changeId'] as $index => $changeId) { | |
self::checkoutChange($changeId, $matches['branch'][$index], $matches['packageKey'][$index]); | |
} | |
} | |
} | |
static protected function checkoutChange($changeId, $branch = 'master', $packageKey = NULL) { | |
$typeDirs = scandir(FLOW_PATH_PACKAGES); | |
$packagePaths = array('buildessentials' => 'Build/BuildEssentials'); | |
foreach ($typeDirs as $typeDir) { | |
if (is_dir(FLOW_PATH_PACKAGES . $typeDir) && substr($typeDir, 0, 1) !== '.') { | |
$typeDir = FLOW_PATH_PACKAGES . $typeDir . '/'; | |
$packageDirs = scandir($typeDir); | |
foreach ($packageDirs as $packageDir) { | |
if (is_dir($typeDir . $packageDir) && substr($packageDir, 0, 1) !== '.') { | |
$packagePaths[strtolower($packageDir)] = $typeDir . $packageDir; | |
} | |
} | |
} | |
} | |
$changeInformation = self::fetchChangeInformation($changeId); | |
if (empty($changeInformation)) { | |
// TODO: display error | |
return; | |
} | |
if (empty($packageKey)) { | |
try { | |
$composerConfig = json_decode(file_get_contents('https://git.typo3.org/' . $changeInformation->project . '.git/blob_plain/HEAD:/composer.json')); | |
$packageKey = str_replace(array('/', '-'), '.', $composerConfig->name); | |
} catch(\Exception $exception) { | |
// Some error | |
} | |
} | |
$packageKey = strtolower($packageKey); | |
if (!isset($packagePaths[$packageKey])) { | |
echo self::colorize('The Package ' . $packageKey . ' is not installed', 'red') . PHP_EOL; | |
return; | |
} | |
chdir($packagePaths[$packageKey]); | |
echo self::colorize($packageKey . ': ' . $changeInformation->subject, 'green') . PHP_EOL; | |
if ($changeInformation->status == 'MERGED') { | |
echo self::colorize('This change has been merged!', 'yellow') . PHP_EOL; | |
} elseif ($changeInformation->status == 'ABANDONED') { | |
echo self::colorize('This change has been abandoned!', 'red') . PHP_EOL; | |
} else { | |
$commits = self::executeShellCommand('git log -n30'); | |
$command = 'git fetch --quiet git://review.typo3.org/' . $changeInformation->project . ' ' . $changeInformation->revisions->{$changeInformation->current_revision}->fetch->git->ref . ''; | |
$output = self::executeShellCommand($command); | |
$commit = self::executeShellCommand('git log --format="%H" -n1 FETCH_HEAD'); | |
if (self::isAlreadyPicked($commit, $commits)) { | |
echo self::colorize('Already picked', 'yellow') . PHP_EOL; | |
} else { | |
echo $output; | |
system('git cherry-pick -x --strategy=recursive -X theirs FETCH_HEAD'); | |
} | |
} | |
echo PHP_EOL; | |
chdir(FLOW_PATH_ROOT); | |
} | |
/** | |
* @param string $commit | |
* @param string $commits | |
* @return boolean | |
*/ | |
static protected function isAlreadyPicked($commit, $commits) { | |
return stristr($commits, '(cherry picked from commit ' . $commit . ')') !== FALSE; | |
} | |
/** | |
* @param string $command | |
* @return string | |
*/ | |
static protected function executeShellCommand($command) { | |
$output = ''; | |
$fp = popen($command, 'r'); | |
while (($line = fgets($fp)) !== FALSE) { | |
$output .= $line; | |
} | |
pclose($fp); | |
return trim($output); | |
} | |
/** | |
* @param string $text | |
* @param string $color Allowed values: green, red, yellow | |
* @return string | |
*/ | |
static protected function colorize($text, $color) { | |
return sprintf("\033[%sm%s\033[0m", self::$colors[$color], $text); | |
} | |
/** | |
* @param integer $changeId The numeric change id, not the hash | |
* @return mixed | |
*/ | |
static protected function fetchChangeInformation($changeId) { | |
$output = file_get_contents('https://review.typo3.org/changes/?format=JSON_COMPACT&q=' . $changeId . '&o=CURRENT_REVISION'); | |
// Remove first line | |
$output = substr($output, strpos($output, "\n") + 1); | |
// trim [] | |
$output = ltrim($output, '['); | |
$output = rtrim(rtrim($output), ']'); | |
$data = json_decode($output); | |
return $data; | |
} | |
} | |
/** | |
* Feed it with some commit message like $commitMessage = shell_exec('git log -1 --pretty=%B') | |
*/ | |
Gerrit::checkoutGerritCommitDependenciesCommand(getenv('COMMIT_MESSAGE')); | |
?> |
[TASK] Commit message
Resolves: #123
Depends: I23a1ef064fc542d6fd20a101e43c2400ce40ab86
Depends: I8e5ea1aa0fdb80057b075d29ff84259dd23f4b60
Depends: I8700ce155cf929eddb5a046d6362209f796e6b81
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[TASK] Commit message
Resolves: #123
Depends: TYPO3.Flow/I23a1ef064fc542d6fd20a101e43c2400ce40ab86/master
Depends: TYPO3.Neos/I8e5ea1aa0fdb80057b075d29ff84259dd23f4b60
Depends: TYPO3.TYPO3CR/I8700ce155cf929eddb5a046d6362209f796e6b81