Skip to content

Instantly share code, notes, and snippets.

@egobude
Created April 19, 2017 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egobude/e8b5d07fde0f8f1dc99caac634d9e61f to your computer and use it in GitHub Desktop.
Save egobude/e8b5d07fde0f8f1dc99caac634d9e61f to your computer and use it in GitHub Desktop.
[WIP] Git Release Script
#!/usr/bin/env php
<?php
/**
* updateComposerManifest
*/
#function updateComposerManifest($version = null) {
# $composerManifest = print_r(json_decode(file_get_contents('composer.json'), true));
# if (isset($composerManifest['version']) {
# $composerManifest['version'] = $version;
# $output = json_encode($composerManifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
# file_put_contents('composer.json', $output);
# }
#}
/**
* Gets the commit message from a git hash
*
* @param string $hash
* @return string
*/
function getCommitMessage($hash)
{
return shell_exec('git log --format=%B -n 1 ' . $hash);
}
/**
* Gets the commits
*
* @param string $latestVersion
* @param string $version
* @return array
*/
function getCommits($latestVersion, $version)
{
$command = sprintf(
'git log --oneline --pretty=format:"%%h:::" %s..%s',
$latestVersion,
$version
);
return explode(':::', shell_exec($command));
}
/**
* @param string $branch
*/
function createBranch($branch)
{
$command = sprintf('git checkout -b %s', $branch);
shell_exec($command);
}
/**
* @param string $tag
*/
function createTag($tag)
{
$command = sprintf('git tag %s', $tag);
shell_exec($command);
}
/**
* @param string $branch
*/
function checkout($branch)
{
$command = sprintf('git checkout %s', $branch);
shell_exec($command);
}
/**
* @param string $version
*/
function pushBranch($version)
{
$command = sprintf('git push origin %s', $version);
shell_exec($command);
}
/**
* @param string $tag
*/
function pushTag($tag)
{
$command = sprintf('git push origin %s', $tag);
shell_exec($command);
}
function pull($version)
{
$command = sprintf('git pull origin %s', $version);
shell_exec($command);
}
/**
* @param string $file
*/
function addFile($file)
{
$command = sprintf('git add %s', $file);
shell_exec($command);
}
/**
* @param string $message
*/
function commit($message)
{
$command = sprintf('git commit -m %s', $message);
shell_exec($command);
}
/**
* createReleaseNotes
*
* @param string $latestVersion
* @param string $version
*/
function createReleaseNotes($latestVersion, $version)
{
if (!$version) {
throw new InvalidArgumentException('The parameter $value is missing.', 1492600644);
}
$date = new DateTime('now');
$releaseNotes = 'ReleaseNotes.md';
$content = sprintf('# Version %s (%s)', $version, $date->format('d.m.Y')) . PHP_EOL;
$commits = array_filter(getCommits($latestVersion, $version), function ($value) {
return $value !== '';
});
foreach ($commits as $commit) {
$content .= '###' . getCommitMessage(trim($commit));
}
file_put_contents($releaseNotes, $content);
}
checkout($argv[1]);
pull($argv[1]);
createBranch($argv[2]);
pushBranch($argv[2]);
createTag('v' . $argv[2]);
pushTag('v' . $argv[2]);
checkout('master');
#createReleaseNotes($argv[1], $argv[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment