diff options
Diffstat (limited to 'tool/file2lastrev.rb')
-rwxr-xr-x | tool/file2lastrev.rb | 91 |
1 files changed, 36 insertions, 55 deletions
diff --git a/tool/file2lastrev.rb b/tool/file2lastrev.rb index 3d8c69357d..6200e78a56 100755 --- a/tool/file2lastrev.rb +++ b/tool/file2lastrev.rb @@ -8,50 +8,47 @@ require 'optparse' # this file run with BASERUBY, which may be older than 1.9, so no # require_relative require File.expand_path('../lib/vcs', __FILE__) +require File.expand_path('../lib/output', __FILE__) Program = $0 -@output = nil -def self.output=(output) - if @output and @output != output +@format = nil +def self.format=(format) + if @format and @format != format raise "you can specify only one of --changed, --revision.h and --doxygen" end - @output = output + @format = format end @suppress_not_found = false @limit = 20 +@output = Output.new -format = '%Y-%m-%dT%H:%M:%S%z' +time_format = '%Y-%m-%dT%H:%M:%S%z' vcs = nil +create_only = false OptionParser.new {|opts| opts.banner << " paths..." vcs_options = VCS.define_options(opts) - new_vcs = proc do |path| - begin - vcs = VCS.detect(path, vcs_options, opts.new) - rescue VCS::NotFoundError => e - abort "#{File.basename(Program)}: #{e.message}" unless @suppress_not_found - opts.remove - end - nil - end + opts.new {@output.def_options(opts)} + srcdir = nil opts.new opts.on("--srcdir=PATH", "use PATH as source directory") do |path| - abort "#{File.basename(Program)}: srcdir is already set" if vcs - new_vcs[path] + abort "#{File.basename(Program)}: srcdir is already set" if srcdir + srcdir = path + @output.vpath.add(srcdir) end opts.on("--changed", "changed rev") do - self.output = :changed + self.format = :changed end opts.on("--revision.h", "RUBY_REVISION macro") do - self.output = :revision_h + self.format = :revision_h end opts.on("--doxygen", "Doxygen format") do - self.output = :doxygen + self.format = :doxygen end opts.on("--modified[=FORMAT]", "modified time") do |fmt| - self.output = :modified - format = fmt if fmt + self.format = :modified + time_format = fmt if fmt end opts.on("--limit=NUM", "limit branch name length (#@limit)", Integer) do |n| @limit = n @@ -60,44 +57,27 @@ OptionParser.new {|opts| @suppress_not_found = true end opts.order! rescue abort "#{File.basename(Program)}: #{$!}\n#{opts}" - if vcs - vcs.set_options(vcs_options) # options after --srcdir - else - new_vcs["."] + begin + vcs = VCS.detect(srcdir || ".", vcs_options, opts.new) + rescue VCS::NotFoundError => e + abort "#{File.basename(Program)}: #{e.message}" unless @suppress_not_found + opts.remove + (vcs = VCS::Null.new(nil)).set_options(vcs_options) + if @format == :revision_h + create_only = true # don't overwrite existing revision.h when .git doesn't exist + end end } -exit unless vcs -@output = - case @output +formatter = + case @format when :changed, nil Proc.new {|last, changed| - changed + changed || "" } when :revision_h Proc.new {|last, changed, modified, branch, title| - short = vcs.short_revision(last) - if /[^\x00-\x7f]/ =~ title and title.respond_to?(:force_encoding) - title = title.dup.force_encoding("US-ASCII") - end - [ - "#define RUBY_REVISION #{short.inspect}", - ("#define RUBY_FULL_REVISION #{last.inspect}" unless short == last), - if branch - e = '..' - limit = @limit - name = branch.sub(/\A(.{#{limit-e.size}}).{#{e.size+1},}/o) {$1+e} - name = name.dump.sub(/\\#/, '#') - "#define RUBY_BRANCH_NAME #{name}" - end, - if title - title = title.dump.sub(/\\#/, '#') - "#define RUBY_LAST_COMMIT_TITLE #{title}" - end, - if modified - modified.utc.strftime('#define RUBY_RELEASE_DATETIME "%FT%TZ"') - end, - ].compact + vcs.revision_header(last, modified, modified, branch, title, limit: @limit).join("\n") } when :doxygen Proc.new {|last, changed| @@ -105,18 +85,19 @@ exit unless vcs } when :modified Proc.new {|last, changed, modified| - modified.strftime(format) + modified.strftime(time_format) } else - raise "unknown output format `#{@output}'" + raise "unknown output format `#{@format}'" end ok = true (ARGV.empty? ? [nil] : ARGV).each do |arg| begin - puts @output[*vcs.get_revisions(arg)] + data = formatter[*vcs.get_revisions(arg)] + data.sub!(/(?<!\A|\n)\z/, "\n") + @output.write(data, overwrite: true, create_only: create_only) rescue => e - next if @suppress_not_found and VCS::NotFoundError === e warn "#{File.basename(Program)}: #{e.message}" ok = false end |