Skip to content

Instantly share code, notes, and snippets.

@chris-moreton
Created May 9, 2018 09:33
Show Gist options
  • Save chris-moreton/06f2f9fa9879ab1ab01210def236a1fa to your computer and use it in GitHub Desktop.
Save chris-moreton/06f2f9fa9879ab1ab01210def236a1fa to your computer and use it in GitHub Desktop.
Increase the version of the latest tag on the current git branch and push the tag.
<?php
/***********************************************************
Hacky script to increase the version of the latest tag
on the current branch and push the tag. Assumes a simple
SemVer tag of the format vx.x.x
************************************************************/
$usage = 'Usage: php tag.php [major|minor|patch]' . PHP_EOL;
if (count($argv) != 2) {
die($usage);
}
$latestTag = exec('git describe --tags --abbrev=0');
$parts = explode('.', $latestTag);
$valid = substr($parts[0],0,1) == 'v' && count($parts) == 3 && is_numeric(substr($parts[0], 1)) && is_numeric($parts[1]) && is_numeric($parts[2]);
if (!$valid) {
die('Expecting latest tag to be of format: vx.x.x' . PHP_EOL);
}
switch ($argv[1]) {
case 'major' :
$part0 = (substr($parts[0], 1)+1);
$newTag = 'v' . $part0 . '.0.0';
break;
case 'minor' :
$newTag = $parts[0] . '.' . ($parts[1]+1) . '.0';
break;
case 'patch' :
$newTag = $parts[0] . '.' . $parts[1] . '.' . ($parts[2]+1);
break;
case 'default' :
die($usage);
}
echo 'Creating new tag ' . $newTag . PHP_EOL;
exec('git tag ' . $newTag);
echo 'Pushing new tag...' . PHP_EOL;
exec('git push --tags');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment