Skip to content

Instantly share code, notes, and snippets.

@woods
Last active September 27, 2016 10:08
Show Gist options
  • Save woods/7424599 to your computer and use it in GitHub Desktop.
Save woods/7424599 to your computer and use it in GitHub Desktop.
A script to bump a project's version number in git.
#!/usr/bin/env ruby
# Bump the version of the current git project
#
# Installation instructions: copy to /usr/local/bin/git-bump-version and set
# permissions to 0755.
#
# Author: Scott Woods <scott@westarete.com>
# Ref: https://gist.github.com/woods/7424599
require 'rubygems'
gem 'colored'
gem 'versionomy'
require 'colored'
require 'versionomy'
begin
ARGV.length == 1 or raise ArgumentError
level = ARGV.first.to_sym
[:major, :minor, :tiny].include?(level) or raise ArgumentError
rescue ArgumentError => e
puts "Usage: git bump-version {major|minor|tiny}"
exit 1
end
unless `git branch` =~ /^\* master$/
puts "You must be on the master branch to bump versions"
exit 1
end
def run(cmd)
puts cmd.red
system cmd or exit $?.exitstatus
puts
end
run "git fetch"
version_strings = `git tag`.split.select { |s| s =~ /\d\.\d/ }
current_version = version_strings.map { |s| Versionomy.parse(s) }.sort.last
new_version = current_version.bump(level)
current_revision = `git show`.split(/\n/).first.chomp.split(/ /).last
last_tagged_revision = `git show #{current_version}`.split(/\n/).first.chomp.split(/ /).last
if current_revision == last_tagged_revision
puts "No action taken -- the current revision is already tagged (as #{current_version})"
exit 1
end
puts "Bumping #{current_version} -> #{new_version}\n\n"
run "git tag #{new_version}"
run "git push"
run "git push --tags"
# Install the git-bump-version script
curl -so /usr/local/bin/git-bump-version https://gist.github.com/woods/7424599/raw/git-bump-version
chmod 755 /usr/local/bin/git-bump-version
if ! grep -q gbv ~/.bashrc ; then
echo "alias gbv='git-bump-version'" >> ~/.bashrc
source ~/.bashrc
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment