Skip to content

Instantly share code, notes, and snippets.

@colinrymer
Last active December 27, 2015 14:59
Show Gist options
  • Save colinrymer/7343964 to your computer and use it in GitHub Desktop.
Save colinrymer/7343964 to your computer and use it in GitHub Desktop.
def tag_to_deploy
Capistrano::CLI.ui.choose do |menu|
menu.choices *last_ten_tags
menu.header = "Available tags"
menu.prompt = "Tag to deploy?"
menu.select_by = :index_or_name
end
end
def last_ten_tags
tags = `git tag`.split("\n")
sorted_tags = tags.sort_by{ |version| version.split('.').map{ |version_level| version_level[/\d+/].to_i } }
sorted_tags.last(10)
end
def tag_to_deploy
Capistrano::CLI.ui.choose do |menu|
menu.choices *last_ten_tags
menu.header = "Available tags"
menu.prompt = "Tag to deploy?"
menu.select_by = :index_or_name
end
end
def last_ten_tags
tags = `git tag`.split("\n")
sorted_tags = tags.sort_by{ |version| version_as_array(version) }
sorted_tags.last(10)
end
def version_as_array(version)
version.split('.').map{ |version_level| version_level[/\d+/].to_i }
end
def tag_to_deploy
require_annotated = /refs\/tags\/(v.+\^\{\})$/
all_version_tags = `git ls-remote --tags #{repository}`.scan(require_annotated).flatten
sorted_version_tags = all_version_tags.sort_by{|v| v.split('.').map{|nbr| nbr[/\d+/].to_i}}
stripped_version_tags = sorted_version_tags.map{|tag| tag.strip}
puts "stripped_version_tags: #{stripped_version_tags.class}"
last_x_tags = []
if stripped_version_tags.size > 10
last_x_tags = stripped_version_tags[-10..-1]
puts "last_ten_tags: #{last_x_tags}"
else
max = stripped_version_tags.size
last_x_tags = stripped_version_tags[-max..-1]
puts "last_ten_tags: #{last_x_tags}"
end
tag = Capistrano::CLI.ui.choose { |menu|
menu.choices *last_x_tags
menu.header = "Available tags"
menu.prompt = "Tag to deploy?"
menu.select_by = :index_or_name
}
end
@hoitomt
Copy link

hoitomt commented Nov 6, 2013

I like it, nice refactor. You could break out sorted_tags as well and name it so as to clarify what it is providing:

...
def last_ten_tags
git_tags_sorted_by_version.last(10)
end

def git_tags_sorted_by_version
git tag.split("\n").sort_by{ |version| version_as_array(version) }
end
...

@hoitomt
Copy link

hoitomt commented Nov 6, 2013

Actually... you might not need version as array - could you do this?

def git_tags_sorted_by_version
  tags.sort_by{ |version| version.gsub(/[^\d]/, '').to_i }
end

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