don’t get lost in the sea of tags
¶ by Rob FrieselEver feeling lost in a sea of not-exactly-ordered tags when you enter git tag
at the command prompt? It seems that a common tagging idiom in a lot of git repos is to tag important milestones or releases. Some repos use <major>.<minor>
, others <major>.<minor>.<version>
, still others <major>.<minor>.<version>.<patch>
(and so on?). And as those repos get along, so do their version numbers. And before long, they don’t all fit on the screen; and if the updates are frequent enough, you might find that you missed that latest tag because 1.0.1.21
was “off the screen” and what you thought was the latest (1.0.1.9
) was already long out-of-date. But a little Ruby magic can make this all better:
#!/usr/bin/env ruby | |
args = $*.to_a | |
if args.index("-c") | |
ct = [args[args.index("-c") + 1].to_i, 1].max | |
end | |
tags = $stdin.to_a | |
tags.collect! {|v| v = v.split('.') | |
v.collect! {|i| i.to_i} | |
} | |
tags.sort! {|a, b| a <=> b } | |
if ct | |
ct = [ct, tags.length].min | |
tag_range = tags[-ct..-1] | |
puts "LAST #{ct} VERSIONS:" | |
tag_range.each {|i| puts "#{i.join('.')}" } | |
else | |
puts "LATEST VERSION: #{tags[-1].join('.')}" | |
end |
And then (after optionally adjusting the Ruby script as meets your needs) add the following alias to your .gitconfig
:
# add the following under `[alias]` | |
latest-tag = "!sh -c 'git tag | get_latest_version.rb' -" | |
recent-tags = "!sh -c 'git tag | get_latest_version.rb -c ${1}' -" |
…and you should be good to go. No more mysteries. (It even gracefully handles tag suffixes like -beta
.)
Leave a Reply