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
|
|
|
|
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)
|
|
|
|
last, changed = Dir.chdir(@srcdir) {yield path}
|
|
|
|
last or raise "last revision not found"
|
|
|
|
changed or raise "changed revision not found"
|
2009-01-12 22:37:24 -05:00
|
|
|
return last, changed
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
def relative_to(path)
|
|
|
|
path ? Pathname(path).relative_path_from(@srcdir) : '.'
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
2010-03-13 03:48:49 -05:00
|
|
|
|
|
|
|
class SVN < self
|
|
|
|
register(".svn")
|
|
|
|
|
2010-03-14 15:18:49 -04:00
|
|
|
def get_revisions(*)
|
|
|
|
super do |path|
|
2010-03-13 03:48:49 -05:00
|
|
|
info_xml = `svn info --xml "#{path}"`
|
|
|
|
_, last, _, changed, _ = info_xml.split(/revision="(\d+)"/)
|
|
|
|
[last, changed]
|
|
|
|
end
|
|
|
|
end
|
2008-12-22 08:19:08 -05:00
|
|
|
end
|
|
|
|
|
2010-03-13 03:48:49 -05:00
|
|
|
class GIT_SVN < self
|
|
|
|
register(".git/svn")
|
|
|
|
|
2010-03-14 15:18:49 -04:00
|
|
|
def get_revisions(*)
|
2010-03-14 11:17:05 -04:00
|
|
|
super do |path|
|
2010-03-13 03:48:49 -05:00
|
|
|
info = `git svn info "#{path}"`
|
|
|
|
[info[/^Revision: (\d+)/, 1], info[/^Last Changed Rev: (\d+)/, 1]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GIT < self
|
|
|
|
register(".git")
|
|
|
|
|
2010-03-14 15:18:49 -04:00
|
|
|
def get_revisions(*)
|
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-14 15:18:49 -04:00
|
|
|
super do |path|
|
2010-03-13 03:48:49 -05:00
|
|
|
last = `#{logcmd}`[idpat, 1]
|
|
|
|
changed = path ? `#{logcmd} "#{path}"`[idpat, 1] : last
|
|
|
|
[last, changed]
|
|
|
|
end
|
|
|
|
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
|
|
|
|
last, changed = vcs.get_revisions(ARGV.shift)
|
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
|