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'
|
|
|
|
|
2010-07-17 05:30:54 -04:00
|
|
|
unless File.respond_to? :realpath
|
|
|
|
require 'pathname'
|
|
|
|
def File.realpath(arg)
|
|
|
|
Pathname(arg).realpath.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Program = $0
|
2010-03-13 03:48:49 -05:00
|
|
|
|
|
|
|
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)
|
2012-01-02 08:14:24 -05:00
|
|
|
@@dirs.each do |dir, klass|
|
|
|
|
return klass.new(path) if File.directory?(File.join(path, dir))
|
|
|
|
prev = path
|
|
|
|
loop {
|
|
|
|
curr = File.realpath(File.join(prev, '..'))
|
|
|
|
break if curr == prev # stop at the root directory
|
|
|
|
return klass.new(path) if File.directory?(File.join(curr, dir))
|
|
|
|
prev = curr
|
|
|
|
}
|
2010-03-13 03:48:49 -05:00
|
|
|
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
|
2010-07-17 05:30:54 -04:00
|
|
|
srcdir = File.realpath(@srcdir)
|
|
|
|
path = File.realpath(path)
|
|
|
|
list1 = srcdir.split(%r{/})
|
|
|
|
list2 = path.split(%r{/})
|
|
|
|
while !list1.empty? && !list2.empty? && list1.first == list2.first
|
|
|
|
list1.shift
|
|
|
|
list2.shift
|
|
|
|
end
|
|
|
|
if list1.empty? && list2.empty?
|
|
|
|
"."
|
2010-04-02 15:48:04 -04:00
|
|
|
else
|
2010-07-17 05:30:54 -04:00
|
|
|
([".."] * list1.length + list2).join("/")
|
2010-04-02 15:48:04 -04:00
|
|
|
end
|
|
|
|
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-07-17 05:30:54 -04:00
|
|
|
parser.parse! rescue abort "#{File.basename(Program)}: #{$!}\n#{parser}"
|
2008-12-22 08:19:08 -05:00
|
|
|
|
2012-01-02 08:14:24 -05:00
|
|
|
srcdir ||= File.dirname(File.dirname(Program))
|
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
|
2010-07-17 05:30:54 -04:00
|
|
|
abort "#{File.basename(Program)}: #{e.message}" unless @suppress_not_found
|
2010-03-13 03:48:49 -05:00
|
|
|
else
|
2010-04-15 07:43:09 -04:00
|
|
|
begin
|
|
|
|
last, changed = vcs.get_revisions(ARGV.shift)
|
|
|
|
rescue => e
|
2010-07-17 05:30:54 -04:00
|
|
|
abort "#{File.basename(Program)}: #{e.message}" unless @suppress_not_found
|
2010-04-15 07:43:09 -04:00
|
|
|
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
|