Skip to content

Instantly share code, notes, and snippets.

@Orangenhain
Created August 21, 2012 09:37
Show Gist options
  • Save Orangenhain/3413952 to your computer and use it in GitHub Desktop.
Save Orangenhain/3413952 to your computer and use it in GitHub Desktop.
svn revision checkout -- checkout specific revision and (most importantly) checkout matching externals revision; useful if your repository has HEAD-tracking externals, but you want to compile an earlier version
#!/usr/bin/env ruby
require 'uri'
def params_valid?
lengthOkay = [2,3].include? ARGV.length
revOkay = ARGV[0].to_i.to_s == ARGV[0]
lengthOkay && revOkay
end
def date_for_svn_revision(url, revision)
output = `svn log --limit 1 -r #{revision} '#{url}' --xml | grep '<date>.*</date>'`
date = output.strip[/^<date>(.*)<\/date>$/, 1].strip rescue nil
end
def parse_externals(line)
# http://svnbook.red-bean.com/en/1.7/svn.advanced.externals.html
# good enough(tm) for my purposes
name, url = line.split("\s")
end
def get_svn(url, path, date)
output = `svn checkout --ignore-externals --revision '{#{date}}' "#{url}" "#{path}"`
puts " -- START --"
puts "URL: #{url}", "path: #{path}", "date: #{date}"
puts output
puts " -- END --"
Dir.chdir(path) do
output = `svn propget 'svn:externals'`.strip
return if output.length == 0
externals = output.lines.collect { |line| parse_externals(line) }.compact
externals.each do |name, url|
get_svn(url, name, date)
end
end
end
unless params_valid?
puts "Usage: #{File.basename __FILE__} #revision URL [path]"
exit 1
end
revision = ARGV[0].to_i
url = URI.parse(ARGV[1])
path = ARGV[2] || File.basename(url.path) || "checkout_#{url.host}_#{revision}"
date = date_for_svn_revision(url, revision)
if date.nil? or date.length == 0
puts "ERROR: could not find date for r#{revision} @ #{url}"
puts "\tmost likely the corresponding commit did not touch the provided path"
exit 2
end
get_svn url, path, date
@niklas
Copy link

niklas commented Aug 21, 2012

you.. poor.. gitless.. aaaww

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