2008-12-22 08:19:08 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2008-12-31 16:56:38 -05:00
|
|
|
ENV.delete('PWD')
|
|
|
|
|
2008-12-22 08:19:08 -05:00
|
|
|
require 'optparse'
|
|
|
|
require 'pathname'
|
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
Program = Pathname($0)
|
|
|
|
|
|
|
|
class VCS
|
|
|
|
class NotFoundError < RuntimeError; end
|
2008-12-22 08:19:08 -05:00
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
@@dirs = []
|
|
|
|
def self.register(dir)
|
|
|
|
@@dirs << [dir, self]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.detect(path)
|
|
|
|
@@dirs.sort.reverse_each do |dir, klass|
|
|
|
|
return klass.new(path) if File.directory?("#{path}/#{dir}")
|
|
|
|
end
|
|
|
|
raise VCS::NotFoundError, "does not seem to be under a vcs: #{path}"
|
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
def initialize(path)
|
|
|
|
@srcdir = path
|
2010-03-30 00:01:17 -04:00
|
|
|
super()
|
2010-03-13 03:48:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# return a pair of strings, the last revision and the last revision in which
|
|
|
|
# +path+ was modified.
|
|
|
|
def get_revisions(path)
|
|
|
|
path = relative_to(path)
|
2010-03-30 00:01:17 -04:00
|
|
|
last, changed, *rest = Dir.chdir(@srcdir) {self.class.get_revisions(path)}
|
2010-03-13 03:48:49 -05:00
|
|
|
last or raise "last revision not found"
|
|
|
|
changed or raise "changed revision not found"
|
2010-03-30 00:01:17 -04:00
|
|
|
return last, changed, *rest
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
def relative_to(path)
|
2010-04-02 15:48:04 -04:00
|
|
|
if path
|
|
|
|
path = Pathname(path)
|
|
|
|
srcdir = @srcdir
|
|
|
|
if path.absolute? ^ srcdir.absolute?
|
|
|
|
if path.absolute?
|
|
|
|
srcdir = srcdir.expand_path
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if srcdir.absolute?
|
|
|
|
path = path.expand_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
path.relative_path_from(srcdir)
|
|
|
|
else
|
|
|
|
'.'
|
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
2010-03-13 03:48:49 -05:00
|
|
|
|
|
|
|
class SVN < self
|
|
|
|
register(".svn")
|
|
|
|
|
2010-03-30 00:01:17 -04:00
|
|
|
def self.get_revisions(path)
|
2010-04-15 08:55:11 -04:00
|
|
|
begin
|
2010-04-16 03:51:18 -04:00
|
|
|
nulldevice = %w[/dev/null NUL NIL: NL:].find {|dev| File.exist?(dev)}
|
|
|
|
if nulldevice
|
2010-04-15 08:55:11 -04:00
|
|
|
save_stderr = STDERR.dup
|
|
|
|
STDERR.reopen nulldevice, 'w'
|
|
|
|
end
|
|
|
|
info_xml = `svn info --xml "#{path}"`
|
|
|
|
ensure
|
|
|
|
if save_stderr
|
|
|
|
STDERR.reopen save_stderr
|
|
|
|
save_stderr.close
|
|
|
|
end
|
|
|
|
end
|
2010-03-30 00:01:17 -04:00
|
|
|
_, last, _, changed, _ = info_xml.split(/revision="(\d+)"/)
|
|
|
|
[last, changed]
|
2010-03-13 03:48:49 -05:00
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
class GIT < self
|
|
|
|
register(".git")
|
|
|
|
|
2010-03-30 00:01:17 -04:00
|
|
|
def self.get_revisions(path)
|
2010-03-14 11:17:05 -04:00
|
|
|
logcmd = %Q[git log -n1 --grep="^ *git-svn-id: .*@[0-9][0-9]* "]
|
2010-03-13 03:48:49 -05:00
|
|
|
idpat = /git-svn-id: .*?@(\d+) \S+\Z/
|
2010-03-30 00:01:17 -04:00
|
|
|
last = `#{logcmd}`[idpat, 1]
|
|
|
|
changed = path ? `#{logcmd} "#{path}"`[idpat, 1] : last
|
|
|
|
[last, changed]
|
2010-03-13 03:48:49 -05:00
|
|
|
end
|
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
|
2009-06-30 03:53:22 -04:00
|
|
|
@output = nil
|
|
|
|
def self.output=(output)
|
|
|
|
if @output and @output != output
|
|
|
|
raise "you can specify only one of --changed, --revision.h and --doxygen"
|
|
|
|
end
|
|
|
|
@output = output
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
2009-06-30 03:53:22 -04:00
|
|
|
@suppress_not_found = false
|
2008-12-22 08:19:08 -05:00
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
srcdir = nil
|
2008-12-22 08:19:08 -05:00
|
|
|
parser = OptionParser.new {|opts|
|
2010-03-13 03:48:49 -05:00
|
|
|
opts.on("--srcdir=PATH", "use PATH as source directory") do |path|
|
|
|
|
srcdir = path
|
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
opts.on("--changed", "changed rev") do
|
2009-06-30 03:53:22 -04:00
|
|
|
self.output = :changed
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
2010-03-13 03:48:49 -05:00
|
|
|
opts.on("--revision.h", "RUBY_REVISION macro") do
|
2009-06-30 03:53:22 -04:00
|
|
|
self.output = :revision_h
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
2010-03-13 03:48:49 -05:00
|
|
|
opts.on("--doxygen", "Doxygen format") do
|
2009-06-30 03:53:22 -04:00
|
|
|
self.output = :doxygen
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
opts.on("-q", "--suppress_not_found") do
|
2009-06-30 03:53:22 -04:00
|
|
|
@suppress_not_found = true
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
}
|
2010-03-13 03:48:49 -05:00
|
|
|
parser.parse! rescue abort "#{Program.basename}: #{$!}\n#{parser}"
|
2008-12-22 08:19:08 -05:00
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
srcdir = (srcdir ? Pathname(srcdir) : Program.parent.parent).freeze
|
2008-12-22 08:19:08 -05:00
|
|
|
begin
|
2010-03-13 03:48:49 -05:00
|
|
|
vcs = VCS.detect(srcdir)
|
|
|
|
rescue VCS::NotFoundError => e
|
|
|
|
abort "#{Program.basename}: #{e.message}" unless @suppress_not_found
|
|
|
|
else
|
2010-04-15 07:43:09 -04:00
|
|
|
begin
|
|
|
|
last, changed = vcs.get_revisions(ARGV.shift)
|
|
|
|
rescue => e
|
|
|
|
abort "#{Program.basename}: #{e.message}" unless @suppress_not_found
|
|
|
|
exit false
|
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
|
2009-06-30 03:53:22 -04:00
|
|
|
case @output
|
2008-12-22 08:19:08 -05:00
|
|
|
when :changed, nil
|
|
|
|
puts changed
|
|
|
|
when :revision_h
|
2009-01-15 06:45:01 -05:00
|
|
|
puts "#define RUBY_REVISION #{changed.to_i}"
|
2008-12-22 08:19:08 -05:00
|
|
|
when :doxygen
|
|
|
|
puts "r#{changed}/r#{last}"
|
|
|
|
else
|
2009-06-30 03:53:22 -04:00
|
|
|
raise "unknown output format `#{@output}'"
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|