#!/usr/bin/env ruby # Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004 LRDE. All rights reserved. # License:: GNU General Public License (GPL). # $LastChangedBy: ertai $ # $Id: header 98 2004-09-29 12:07:43Z ertai $ # vcs: The version control system wrapper. # # Vcs is a wrapper over any version control system. require 'pathname' require 'logger' require 'optparse' begin REV="$Rev$".sub(/LastChangedRevision:\s+/, '').to_i rescue REV=-1 end VCS_PATH = Pathname.new(__FILE__).expand_path VCS_DIR, VCS = VCS_PATH.split SRC = VCS_DIR + '..' + 'src' $: << SRC.to_s $: << Pathname.pwd + 'vcs' LOG = Logger.new(STDOUT) LOG.level = Logger::INFO def LOG.format_message(severity, timestamp, msg, progname) progname += ': ' unless progname.nil? or progname.empty? "#{VCS}: #{progname}#{severity.downcase}: #{msg}\n" end $debug = ENV['VCS_DEBUG'].to_i Pathname.glob("{#{SRC},vcs}/*.rb") do |file| LOG.debug file.basename if $debug > 4 require file.basename end ALL_VCS_NAMES = [] ALL_VCS_BY_NAME = {} ObjectSpace.each_object(Class) do |aClass| if aClass != Vcs and aClass.ancestors.include? Vcs name = aClass.to_s.to_sym ALL_VCS_NAMES << name ALL_VCS_BY_NAME[name] = aClass end end OPTIONS = {} OPTS = OptionParser.new do |opts| opts.banner = "Usage: #{VCS} [options] *" opts.separator '' opts.on('-c', '--vcs TYPE', ALL_VCS_NAMES, 'Set your vcs') do |aVcs| OPTIONS[:vcs_type] = aVcs end opts.on('-l', '--vcs-list', 'List all vcs') do |aVcs| STDERR.puts 'Vcs list:' ALL_VCS_NAMES.each { |n| STDERR.puts " - #{n}" } exit end opts.on('--mk-alias', 'Use this command like that `vcs --mk-alias`') do ALL_VCS_NAMES.each do |n| n = n.to_s.downcase if VCS_PATH.executable? puts "alias #{n}=#{VCS_PATH}-#{n}" else puts "alias #{n}=vcs-#{n}" end end exit end opts.on('-d', '--debug LEVEL', Integer, 'Set debug level') do |anInteger| $debug = anInteger end opts.on_tail('-h', '--help', 'Show this message') do |test| STDERR.puts OPTS exit end opts.on_tail('--version', 'Show version') do STDOUT.puts "Vcs revision: #{REV}" exit end end if ENV.has_key? 'FULLNAME' FULLNAME = ENV['FULLNAME'] else require 'etc' ENV['FULLNAME'] = (Etc.getpwnam ENV['USER']).gecos LOG.warn "Need FULLNAME in the environement (default: #{ENV['FULLNAME']})" end env = %w[ EMAIL FULLNAME EDITOR PAGER ] if env.all? { |s| ENV[s] } env.each { |s| Kernel.const_set(s, ENV[s]) } else env.each { |s| LOG.error "Need #{s} in the environement" unless ENV[s] } exit end FULL_EMAIL = "#{FULLNAME} <#{EMAIL}>" def main OPTS.parse!(ARGV) unless OPTIONS.has_key? :vcs_type STDERR.puts OPTS STDERR.puts "\nSpecify at least a Vcs type, or use a wrapped command\n" + 'like svn, cvs, prcs in the vcs bin directory.' exit end vcs = ALL_VCS_BY_NAME[OPTIONS[:vcs_type]].new if ARGV.empty? meth = :help! else meth = ARGV.shift.sub(/([^!])$/, '\1!') end vcs.send(meth, *ARGV) end begin main() rescue Exception raise if $debug > 0 LOG.error $!.to_s.sub(/\.$/, '') unless $!.to_s == 'exit' end