Find the Latest SVN Repository Tag with Ruby
Here’s a quick and dirty (and I mean DIRTY – use at your own risk) way to extract the latest tag from a SVN repository. This is a helper method I wrote for a Capistrano deploy script:
# Quick and dirty means to pull the latest tag name from an SVN repository
# TODO: Error checking - it should have some
# TODO: Check to see if the repository exists before running code
# TODO: Return nothing if the tag doesn't look right (eg, the tag name is 'tags')
# Given an SVN repository URL, return the name of the latest tag in the repo/tags directory.
# * Assumes the standard SVN setup: trunk/, tags/, branches. It will append the /tags/ directory to the end of the URL
# * Does ZERO error checking...returns whatever it finds.
def get_last_svn_tag(repo)
txt = `svn log #{repo}/tags/ --limit=1 -v`
# thanks to http://www.txt2re.com/ for this
re1='.*?' # Non-greedy match on filler
re2='((?:\\/[\\w\\.\\-]+)+)' # Unix Path 1
re=(re1+re2)
m=Regexp.new(re,Regexp::IGNORECASE);
if m.match(txt)
unixpath1=m.match(txt)[1];
puts "("<<unixpath1<<")"<< "\n"
latesttag = File.basename(unixpath1)
puts "Tag name: "<< latesttag << "\n"
return latesttag
end
end
tag = "get_last_svn_tag("http://some/repo")
puts "Tagname: #{tag}"