Skip to content

Instantly share code, notes, and snippets.

Created November 1, 2010 15:23
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save predominant/658338 to your computer and use it in GitHub Desktop.
Automatically generate changelogs for CakePHP
$options = array(
'repo' => '/Users/predominant/Projects/cakephp/2.0/.git',
'titleOrder' => array(
'dev', 'alpha', 'beta', 'rc',
'regex' => '/(?<version>[\d\.]+)(?:-(?<title>[a-zA-Z]+)(?:(?<iteration>\d)?))?/',
'formats' => array(
'plaintext' => '| awk \'{print "Commit: ["$1"]\n"; $1=""; print "-"$0"\n"}\'',
'lighthouse' => '| awk \'{print "#### Commit: ["$1"]("$1")\n"; $1=""; print $0"\n"}\'',
'compact-md' => '| awk \'{print "* [Commit "$1"]("$1"): "; $1=""; print $0}\'',
$options['git-dir'] = '--git-dir=' . $options['repo'];
$options['format'] = !empty($_GET['format']) && array_key_exists($_GET['format'], $options['formats']) ? $_GET['format'] : 'plaintext';
$tags = explode("\n", trim(`git ${options['git-dir']} tag`));
usort($tags, function($from, $to) use ($options) {
preg_match($options['regex'], $from, $fromMatches);
preg_match($options['regex'], $to, $toMatches);
$version = version_compare($fromMatches['version'], $toMatches['version']);
if ($version !== 0) {
return $version;
if (!isset($fromMatches['title'])) {
return 1;
if (!isset($toMatches['title'])) {
return -1;
$title = array_search(strtolower($fromMatches['title']), $options['titleOrder']) - array_search(strtolower($toMatches['title']), $options['titleOrder']);
if ($title !== 0) {
return $title;
return version_compare($fromMatches['iteration'], $toMatches['iteration']);
$tags = array_reverse($tags);
$previous = null;
echo '<h2>Format: ' . $options['format'] . '</h2>';
$format = $options['formats'][$options['format']];
foreach ($tags as $tag) {
if (!$previous) {
$previous = $tag;
echo '<h3>Version ' . $tag . '</h3>';
echo '<pre>' . htmlentities(`git ${options['git-dir']} rev-list --no-merges --oneline ${previous} ^${tag} ${format}`) . '</pre>';
$previous = $tag;
Copy link

The only thing wrong with this, it requires you are doing branches. What if your just doing a lot of bug updates, your not going to be versioning a production application that much though. So if you never version it causes issue

Copy link

This is designed to generate changelogs for the CakePHP project. It was never designed or intended to suit any other project.

It relies on tags for listing the versions, and doesn't care what branches you are using.

Copy link

make sense i'm going to test this on a current app i have with a bunch of a tags and various branches. thanks for the feedback though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment