5 # Integer#odd? was introduced in Ruby 1.8.7, backport it to
7 unless 2.respond_to? :odd?
17 #{$0} [options] file [file ...]
19 Fast-export the RCS history of one or more file.
22 --help, -h, -? display this help text
23 --authors-file, -A specify a file containing username = Full Name <email> mappings
24 --[no-]tag-each-rev [do not] create a lightweight tag for each RCS revision
27 rcs.authorsFile for --authors-file
28 rcs.tagEachRev for --tag-each-rev
34 STDERR.puts "Could not find #{arg}"
37 # returns a hash that maps usernames to author names & emails
38 def load_authors_file(fn)
41 File.open(File.expand_path(fn)) do |io|
42 io.each_line do |line|
43 uname, author = line.split('=', 2)
46 STDERR.puts "Username #{uname} redefined to #{author}" if hash.has_key? uname
58 fields = string.split('.')
59 raise ArgumentError, "wrong number of fields for RCS date #{string}" unless fields.length == 6
65 # strip an optional final ;
70 # strip the first and last @, and de-double @@s
75 raise 'malformed first line' unless ret.first[0,1] == '@'
76 raise 'malformed last line' unless ret.last[-1,1] == '@'
77 ret.first.sub!(/^@/,'')
78 ret.last.sub!(/@$/,'')
79 ret.map { |l| l.gsub('@@','@') }
81 arg.chomp('@').sub(/^@/,'').gsub('@@','@')
89 RCS.sanitize RCS.clean(arg)
93 arg.gsub('.', '0') + ('90'*5)
97 arg.gsub('.', '0') + ('09'*5)
101 attr_accessor :head, :comment, :desc, :revision
102 def initialize(fname)
107 @revision = Hash.new { |h, r| h[r] = Revision.new(r) }
110 def has_revision?(rev)
111 @revision.has_key?(rev) and not @revision[rev].author.nil?
114 def export_commits(opts={})
117 until @revision.empty?
120 # a string sort is a very good candidate for
121 # export order, getting a miss only for
122 # multi-digit revision components
123 keys = @revision.keys.sort
125 STDERR.puts "commit export loop ##{counter}"
126 STDERR.puts "\t#{exported.length} commits exported so far: #{exported.join(', ')}" unless exported.empty?
127 STDERR.puts "\t#{keys.size} to export: #{keys.join(', ')}"
131 # the parent commit is rev.next if we're on the
132 # master branch (rev.branch is nil) or
133 # rev.diff_base otherwise
134 from = rev.branch.nil? ? rev.next : rev.diff_base
135 # A commit can only be exported if it has no
136 # parent, or if the parent has been exported
137 # already. Skip this commit otherwise
138 if from and not exported.include? from
142 branch = rev.branch || 'master'
143 author = opts[:authors][rev.author] || "#{rev.author} <empty>"
144 date = "#{rev.date.tv_sec} +0000"
147 puts "commit refs/heads/#{branch}"
148 puts "mark :#{RCS.commit key}"
149 puts "committer #{author} #{date}"
150 puts "data #{log.length}"
151 puts log unless log.empty?
152 puts "from :#{RCS.commit from}" if rev.branch_point
153 puts "M 644 :#{RCS.blob key} #{@fname}"
155 # TODO FIXME this *should* be safe, in
156 # that it should not unduly move
157 # branches back in time, but I'm not
159 rev.branches.each do |sym|
160 puts "reset refs/heads/#{sym}"
161 puts "from :#{RCS.commit key}"
163 rev.symbols.each do |sym|
164 puts "reset refs/tags/#{sym}"
165 puts "from :#{RCS.commit key}"
167 if opts[:tag_each_rev]
168 puts "reset refs/tags/#{key}"
169 puts "from :#{RCS.commit key}"
174 exported.each { |k| @revision.delete(k) }
180 attr_accessor :rev, :author, :state, :next
181 attr_accessor :branches, :log, :text, :symbols
182 attr_accessor :branch, :diff_base, :branch_point
200 @date = Time.rcs(str)
205 ret = "blob\nmark :#{RCS.blob @rev}\ndata #{str.length}\n#{str}\n"
210 def RCS.parse(fname, rcsfile, opts={})
211 rcs = RCS::File.new(fname)
213 ::File.open(rcsfile, 'r') do |file|
218 file.each_line do |line|
221 command, args = line.split($;,2)
222 next if command.empty?
226 rcs.head = RCS.clean(args.chomp)
230 rcs.comment = RCS.at_clean(args.chomp)
233 if rcs.has_revision?(rev)
234 status.push :revision_data
236 status.push :new_revision
241 status.push :read_lines
243 STDERR.puts "Skipping unhandled command #{command.inspect}"
246 sym, rev = line.strip.split(':',2);
247 status.pop if rev.chomp!(';')
248 rcs.revision[rev].symbols << sym
250 rcs.desc.replace lines.dup
253 # we sanitize lines as we read them
255 actual_line = line.dup
257 # the first line must begin with a @, which we strip
259 ats = line.match(/^@+/)
260 raise 'malformed line' unless ats
261 actual_line.replace line.sub(/^@/,'')
264 # if the line ends with an ODD number of @, it's the
265 # last line -- we work on actual_line so that content
266 # such as @\n or @ work correctly (they would be
267 # encoded respectively as ['@@@\n','@\n'] and
269 ats = actual_line.chomp.match(/@+$/)
270 if nomore = (ats && Regexp.last_match(0).length.odd?)
271 actual_line.replace actual_line.chomp.sub(/@$/,'')
273 lines << actual_line.gsub('@@','@')
280 when /^date\s+(\S+);\s+author\s+(\S+);\sstate\s(\S+);$/
281 rcs.revision[rev].date = $1
282 rcs.revision[rev].author = $2
283 rcs.revision[rev].state = $3
285 status.push :branches
288 when /^next\s+(\S+)?;$/
289 nxt = rcs.revision[rev].next = $1
291 raise "multiple diff_bases for #{nxt}" unless rcs.revision[nxt].diff_base.nil?
292 rcs.revision[nxt].diff_base = rev
293 rcs.revision[nxt].branch = rcs.revision[rev].branch
298 candidate = line.split(';',2)
299 branch = candidate.first.strip
300 rcs.revision[rev].branches.push branch
301 raise "multiple diff_bases for #{branch}" unless rcs.revision[branch].diff_base.nil?
302 rcs.revision[branch].diff_base = rev
303 # we drop the last number from the branch name
304 rcs.revision[branch].branch = branch.sub(/\.\d+$/,'.x')
305 rcs.revision[branch].branch_point = rev
306 status.pop if candidate.length > 1
312 status.push :read_lines
320 status.push :read_lines
325 rcs.revision[rev].log.replace lines.dup
328 rcs.revision[rev].text.replace lines.dup
329 puts rcs.revision[rev].blob
332 difflines.replace lines.dup
333 difflines.pop if difflines.last.empty?
334 base = rcs.revision[rev].diff_base
335 unless rcs.revision[base].text
338 raise 'no diff base!'
342 rcs.revision[base].text.each { |l| buffer << [l.dup] }
348 while l = difflines.shift
350 raise 'negative index during insertion' if index < 0
351 raise 'negative count during insertion' if count < 0
354 # collected all the lines, put the before
356 buffer[index].unshift(*adding)
363 raise 'malformed diff' unless l =~ /^([ad])(\d+) (\d+)$/
369 # for deletion, index 1 is the first index, so the Ruby
370 # index is one less than the diff one
372 # we replace them with empty string so that 'a' commands
373 # referring to the same line work properly
380 # addition will prepend the appropriate lines
381 # to the given index, and in this case Ruby
382 # and diff indices are the same
387 # turn the buffer into an array of lines, deleting the empty ones
388 buffer.delete_if { |l| l.empty? }
391 rcs.revision[rev].text = buffer
392 puts rcs.revision[rev].blob
395 STDERR.puts "Unknown status #{status.last}"
401 # clean up the symbols/branches: look for revisions that have
402 # one or more symbols but no dates, and make them into
403 # branches, pointing to the highest commit with that key
405 keys = rcs.revision.keys
406 rcs.revision.each do |key, rev|
407 if rev.date.nil? and not rev.symbols.empty?
408 top = keys.select { |k| k.match(/^#{key}\./) }.sort.last
409 tr = rcs.revision[top]
410 raise "unhandled complex branch structure met: #{rev.inspect} refers #{tr.inspect}" if tr.date.nil?
411 tr.branches |= rev.symbols
415 branches.each { |k| rcs.revision.delete k }
418 rcs.export_commits(opts)
424 opts = GetoptLong.new(
425 # Authors file, like git-svn and git-cvsimport, more than one can be
427 ['--authors-file', '-A', GetoptLong::REQUIRED_ARGUMENT],
428 # RCS file suffix, like RCS
429 ['--rcs-suffixes', '-x', GetoptLong::REQUIRED_ARGUMENT],
431 ['--tag-each-rev', GetoptLong::NO_ARGUMENT],
432 ['--no-tag-each-rev', GetoptLong::NO_ARGUMENT],
433 ['--help', '-h', '-?', GetoptLong::NO_ARGUMENT]
436 # We read options in order, but they apply to all passed parameters.
437 # TODO maybe they should only apply to the following, unless there's only one
439 opts.ordering = GetoptLong::RETURN_IN_ORDER
443 :authors => Hash.new,
446 # Read config options
447 `git config --get-all rcs.authorsfile`.each_line do |fn|
448 parse_options[:authors].merge! load_authors_file(fn.chomp)
451 parse_options[:tag_each_rev] = (
452 `git config --bool rcs.tageachrev`.chomp == 'true'
455 opts.each do |opt, arg|
457 when '--authors-file'
458 authors = load_authors_file(arg)
459 redef = parse_options[:authors].keys & authors.keys
460 STDERR.puts "Authors file #{arg} redefines #{redef.join(', ')}" unless redef.empty?
461 parse_options[:authors].merge!(authors)
462 when '--rcs-suffixes'
464 when '--tag-each-rev'
465 parse_options[:tag_each_rev] = true
466 when '--no-tag-each-rev'
467 # this is the default, which is fine since the missing key
468 # (default) returns nil which is false in Ruby
469 parse_options[:tag_each_rev] = false
480 user = Etc.getlogin || ENV['USER']
482 # steal username/email data from other init files that may contain the
486 # the user's .hgrc file for a username field
487 ['~/.hgrc', /^\s*username\s*=\s*(["'])?(.*)\1$/, 2],
488 # the user's .(g)vimrc for a changelog_username setting
489 ['~/.vimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
490 ['~/.gvimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
492 ].each do |fn, rx, idx|
493 file = File.expand_path fn
494 if File.readable?(file) and File.read(file) =~ rx
495 parse_options[:authors][user] = Regexp.last_match(idx).strip
501 if user and not user.empty? and not parse_options[:authors].has_key?(user)
502 name = ENV['GIT_AUTHOR_NAME'] || ''
503 name.replace(`git config user.name`.chomp) if name.empty?
504 name.replace(Etc.getpwnam(user).gecos) if name.empty?
507 # couldn't find a name, try to steal data from other sources
510 # if we found a name, try to find an email too
511 email = ENV['GIT_AUTHOR_EMAIL'] || ''
512 email.replace(`git config user.email`.chomp) if email.empty?
515 # couldn't find an email, try to steal data too
518 # we got both a name and email, fill the info
519 parse_options[:authors][user] = "#{name} <#{email}>"
533 file_list.each do |arg|
538 not_found "RCS file #{arg}"
541 filename = File.basename(arg, SFX)
543 filename = File.basename(arg)
544 path = File.dirname(arg)
545 rcsfile = File.join(path, 'RCS', filename) + SFX
546 unless File.exists? rcsfile
547 rcsfile.replace File.join(path, filename) + SFX
548 unless File.exists? rcsfile
549 not_found "RCS file for #{filename} in #{path}"
554 RCS.parse(filename, rcsfile, parse_options)