I use semantic versioning to track version changes in projects and to discreetly organize releases.

When I’m using Capistrano for deployments, the latest semantic version release as defined in the tags should be the version which is deployed in production.

I’m setting branch to the latest tag by putting the tags into an array and using Gem to provide comparison for the sort method. The Gem trick is necessary because v1.0.10 is later than v1.0.9, but default sorting methods won’t treat semantic versions as the spec defines them.

set :branch, Proc.new {
  tags = `git tag`.split("\n")
  vers = {}

  tags.each do |t|
    v = t.gsub('v','')
    vers[v] = t

  sorted_vers = vers.keys.sort { |x,y| Gem::Version.new(x) <=> Gem::Version.new(y) }