1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/tool/file2lastrev.rb
yugui 30f3c8c70b * tool/flie2lastrev.rb: supports git repositories which are cloned
from a git-svn gateway.
  Patch by Hongli Lai. [ruby-core:21020]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21239 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-01 07:13:24 +00:00

87 lines
2 KiB
Ruby

#!/usr/bin/env ruby
ENV['LANG'] = ENV['LC_ALL'] = ENV['LC_MESSAGES'] = 'C'
ENV.delete('PWD')
require 'optparse'
require 'pathname'
SRCDIR = Pathname(File.dirname($0)).parent.freeze
class VCSNotFoundError < RuntimeError; end
def detect_vcs(path)
path = SRCDIR
return :svn, path.relative_path_from(SRCDIR) if File.directory?("#{path}/.svn")
return :git_svn, path.relative_path_from(SRCDIR) if File.directory?("#{path}/.git/svn")
return :git, path.relative_path_from(SRCDIR) if File.directory?("#{path}/.git")
raise VCSNotFoundError, "does not seem to be under a vcs"
end
def get_revisions(path)
vcs, path = detect_vcs(path)
info = case vcs
when :svn
`cd "#{SRCDIR}" && svn info "#{path}"`
when :git_svn
`cd "#{SRCDIR}" && git svn info "#{path}"`
when :git
git_log = `cd "#{SRCDIR}" && git log HEAD~1..HEAD "#{path}"`
git_log =~ /git-svn-id: .*?@(\d+)/
"Revision: #{$1}\nLast Changed Rev: #{$1}\n"
end
if /^Revision: (\d+)/ =~ info
last = $1
else
raise "last revision not found"
end
if /^Last Changed Rev: (\d+)/ =~ info
changed = $1
else
raise "changed revision not found"
end
return last, changed
end
def raise_if_conflict
raise "you can specify only one of --changed, --revision.h and --doxygen" if $output
end
parser = OptionParser.new {|opts|
opts.on("--changed", "changed rev") do
raise_if_conflict
$output = :changed
end
opts.on("--revision.h") do
raise_if_conflict
$output = :revision_h
end
opts.on("--doxygen") do
raise_if_conflict
$output = :doxygen
end
opts.on("-q", "--suppress_not_found") do
$suppress_not_found = true
end
}
parser.parse!
begin
last, changed = get_revisions(ARGV.shift)
rescue VCSNotFoundError
raise unless $suppress_not_found
end
case $output
when :changed, nil
puts changed
when :revision_h
puts "#define RUBY_REVISION #{changed}"
when :doxygen
puts "r#{changed}/r#{last}"
else
raise "unknown output format `#{$output}'"
end