7 #{$0} [options] file [file ...]
9 Fast-export the RCS history of one or more file.
12 --help, -h, -? display this help text
13 --authors-file, -A specify a file containing username = Full Name <email> mappings
14 --[no-]tag-each-rev [do not] create a lightweight tag for each RCS revision
17 rcs.authorsFile for --authors-file
18 rcs.tagEachRev for --tag-each-rev
24 STDERR.puts "Could not find #{arg}"
27 # returns a hash that maps usernames to author names & emails
28 def load_authors_file(fn)
31 File.open(File.expand_path fn) do |io|
32 io.each_line do |line|
33 uname, author = line.split('=', 2)
36 STDERR.puts "Username #{uname} redefined to #{author}" if hash.has_key? uname
48 fields = string.split('.')
49 raise ArgumentError, "wrong number of fields for RCS date #{string}" unless fields.length == 6
55 # strip an optional final ;
60 # strip the first and last @, and de-double @@s
65 raise 'malformed first line' unless ret.first[0,1] == '@'
66 raise 'malformed last line' unless ret.last[-1,1] == '@'
67 ret.first.sub!(/^@/,'')
68 ret.last.sub!(/@$/,'')
69 ret.map { |l| l.gsub('@@','@') }
71 arg.chomp('@').sub(/^@/,'').gsub('@@','@')
79 RCS.sanitize RCS.clean(arg)
83 arg.gsub('.', '0') + ('90'*5)
87 arg.gsub('.', '0') + ('09'*5)
91 attr_accessor :head, :comment, :desc, :revision
97 @revision = Hash.new { |h, r| h[r] = Revision.new(r) }
100 def has_revision?(rev)
101 @revision.has_key?(rev) and not @revision[rev].author.nil?
104 def export_commits(opts={})
107 until @revision.empty?
110 # a string sort is a very good candidate for
111 # export order, getting a miss only for
112 # multi-digit revision components
113 keys = @revision.keys.sort
115 STDERR.puts "commit export loop ##{counter}"
116 STDERR.puts "\t#{exported.length} commits exported so far: #{exported.join(', ')}" unless exported.empty?
117 STDERR.puts "\t#{keys.size} to export: #{keys.join(', ')}"
121 # the parent commit is rev.next if we're on the
122 # master branch (rev.branch is nil) or
123 # rev.diff_base otherwise
124 from = rev.branch.nil? ? rev.next : rev.diff_base
125 # A commit can only be exported if it has no
126 # parent, or if the parent has been exported
127 # already. Skip this commit otherwise
128 if from and not exported.include? from
132 branch = rev.branch || 'master'
133 author = opts[:authors][rev.author] || "#{rev.author} <empty>"
134 date = "#{rev.date.tv_sec} +0000"
137 puts "commit refs/heads/#{branch}"
138 puts "mark :#{RCS.commit key}"
139 puts "committer #{author} #{date}"
140 puts "data #{log.length}"
141 puts log unless log.empty?
142 puts "from :#{RCS.commit from}" if rev.branch_point
143 puts "M 644 :#{RCS.blob key} #{@fname}"
145 # TODO FIXME this *should* be safe, in
146 # that it should not unduly move
147 # branches back in time, but I'm not
149 rev.branches.each do |sym|
150 puts "reset refs/heads/#{sym}"
151 puts "from :#{RCS.commit key}"
153 rev.symbols.each do |sym|
154 puts "reset refs/tags/#{sym}"
155 puts "from :#{RCS.commit key}"
157 if opts[:tag_each_rev]
158 puts "reset refs/tags/#{key}"
159 puts "from :#{RCS.commit key}"
164 exported.each { |k| @revision.delete(k) }
170 attr_accessor :rev, :author, :date, :state, :next
171 attr_accessor :branches, :log, :text, :symbols
172 attr_accessor :branch, :diff_base, :branch_point
189 @date = Time.rcs(str)
194 ret = "blob\nmark :#{RCS.blob @rev}\ndata #{str.length}\n#{str}\n"
199 def RCS.parse(fname, rcsfile, opts={})
200 rcs = RCS::File.new(fname)
202 ::File.open(rcsfile, 'r') do |file|
207 file.each_line do |line|
210 command, args = line.split($;,2)
211 next if command.empty?
215 rcs.head = RCS.clean(args.chomp)
219 rcs.comment = RCS.at_clean(args.chomp)
222 if rcs.has_revision?(rev)
223 status.push :revision_data
225 status.push :new_revision
230 status.push :read_lines
232 STDERR.puts "Skipping unhandled command #{command.inspect}"
235 sym, rev = line.strip.split(':',2);
236 status.pop if rev.chomp!(';')
237 rcs.revision[rev].symbols << sym
239 rcs.desc.replace lines.dup
242 # we sanitize lines as we read them
244 actual_line = line.dup
246 # the first line must begin with a @, which we strip
248 ats = line.match(/^@+/)
249 raise 'malformed line' unless ats
250 actual_line.replace line.sub(/^@/,'')
253 # if the line ends with an ODD number of @, it's the
254 # last line -- we work on actual_line so that content
255 # such as @\n or @ work correctly (they would be
256 # encoded respectively as ['@@@\n','@\n'] and
258 ats = actual_line.chomp.match(/@+$/)
259 if nomore = (ats && Regexp.last_match(0).length.odd?)
260 actual_line.replace actual_line.chomp.sub(/@$/,'')
262 lines << actual_line.gsub('@@','@')
269 when /^date\s+(\S+);\s+author\s+(\S+);\sstate\s(\S+);$/
270 rcs.revision[rev].date = $1
271 rcs.revision[rev].author = $2
272 rcs.revision[rev].state = $3
274 status.push :branches
277 when /^next\s+(\S+)?;$/
278 nxt = rcs.revision[rev].next = $1
280 raise "multiple diff_bases for #{nxt}" unless rcs.revision[nxt].diff_base.nil?
281 rcs.revision[nxt].diff_base = rev
282 rcs.revision[nxt].branch = rcs.revision[rev].branch
287 candidate = line.split(';',2)
288 branch = candidate.first.strip
289 rcs.revision[rev].branches.push branch
290 raise "multiple diff_bases for #{branch}" unless rcs.revision[branch].diff_base.nil?
291 rcs.revision[branch].diff_base = rev
292 # we drop the last number from the branch name
293 rcs.revision[branch].branch = branch.sub(/\.\d+$/,'.x')
294 rcs.revision[branch].branch_point = rev
295 status.pop if candidate.length > 1
301 status.push :read_lines
309 status.push :read_lines
314 rcs.revision[rev].log.replace lines.dup
317 rcs.revision[rev].text.replace lines.dup
318 puts rcs.revision[rev].blob
321 difflines.replace lines.dup
322 difflines.pop if difflines.last.empty?
323 base = rcs.revision[rev].diff_base
324 unless rcs.revision[base].text
327 raise 'no diff base!'
331 rcs.revision[base].text.each { |l| buffer << [l.dup] }
337 while l = difflines.shift
339 raise 'negative index during insertion' if index < 0
340 raise 'negative count during insertion' if count < 0
343 # collected all the lines, put the before
345 buffer[index].unshift *adding
352 raise 'malformed diff' unless l =~ /^([ad])(\d+) (\d+)$/
358 # for deletion, index 1 is the first index, so the Ruby
359 # index is one less than the diff one
361 # we replace them with empty string so that 'a' commands
362 # referring to the same line work properly
369 # addition will prepend the appropriate lines
370 # to the given index, and in this case Ruby
371 # and diff indices are the same
376 # turn the buffer into an array of lines, deleting the empty ones
377 buffer.delete_if { |l| l.empty? }
380 rcs.revision[rev].text = buffer
381 puts rcs.revision[rev].blob
384 STDERR.puts "Unknown status #{status.last}"
390 # clean up the symbols/branches: look for revisions that have
391 # one or more symbols but no dates, and make them into
392 # branches, pointing to the highest commit with that key
394 keys = rcs.revision.keys
395 rcs.revision.each do |key, rev|
396 if rev.date.nil? and not rev.symbols.empty?
397 top = keys.select { |k| k.match(/^#{key}\./) }.sort.last
398 tr = rcs.revision[top]
399 raise "unhandled complex branch structure met: #{rev.inspect} refers #{tr.inspect}" if tr.date.nil?
400 tr.branches |= rev.symbols
404 branches.each { |k| rcs.revision.delete k }
407 rcs.export_commits(opts)
413 opts = GetoptLong.new(
414 # Authors file, like git-svn and git-cvsimport, more than one can be
416 ['--authors-file', '-A', GetoptLong::REQUIRED_ARGUMENT],
417 # RCS file suffix, like RCS
418 ['--rcs-suffixes', '-x', GetoptLong::REQUIRED_ARGUMENT],
420 ['--tag-each-rev', GetoptLong::NO_ARGUMENT],
421 ['--no-tag-each-rev', GetoptLong::NO_ARGUMENT],
422 ['--help', '-h', '-?', GetoptLong::NO_ARGUMENT]
425 # We read options in order, but they apply to all passed parameters.
426 # TODO maybe they should only apply to the following, unless there's only one
428 opts.ordering = GetoptLong::RETURN_IN_ORDER
432 :authors => Hash.new,
435 # Read config options
436 `git config --get-all rcs.authorsfile`.each_line do |fn|
437 parse_options[:authors].merge! load_authors_file(fn.chomp)
440 parse_options[:tag_each_rev] = (
441 `git config --bool rcs.tageachrev`.chomp == 'true'
444 opts.each do |opt, arg|
446 when '--authors-file'
447 authors = load_authors_file(arg)
448 redef = parse_options[:authors].keys & authors.keys
449 STDERR.puts "Authors file #{arg} redefines #{redef.join(', ')}" unless redef.empty?
450 parse_options[:authors].merge!(authors)
451 when '--rcs-suffixes'
453 when '--tag-each-rev'
454 parse_options[:tag_each_rev] = true
455 when '--no-tag-each-rev'
456 # this is the default, which is fine since the missing key
457 # (default) returns nil which is false in Ruby
458 parse_options[:tag_each_rev] = false
469 user = Etc.getlogin || ENV['USER']
471 # steal username/email data from other init files that may contain the
475 # the user's .hgrc file for a username field
476 ['~/.hgrc', /^\s*username\s*=\s*(["'])?(.*)\1$/, 2],
477 # the user's .(g)vimrc for a changelog_username setting
478 ['~/.vimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
479 ['~/.gvimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
481 ].each do |fn, rx, idx|
482 file = File.expand_path fn
483 if File.readable?(file) and File.read(file) =~ rx
484 parse_options[:authors][user] = Regexp.last_match(idx).strip
490 if user and not user.empty? and not parse_options[:authors].has_key?(user)
491 name = ENV['GIT_AUTHOR_NAME'] || ''
492 name.replace(`git config user.name`.chomp) if name.empty?
493 name.replace(Etc.getpwnam(user).gecos) if name.empty?
496 # couldn't find a name, try to steal data from other sources
499 # if we found a name, try to find an email too
500 email = ENV['GIT_AUTHOR_EMAIL'] || ''
501 email.replace(`git config user.email`.chomp) if email.empty?
504 # couldn't find an email, try to steal data too
507 # we got both a name and email, fill the info
508 parse_options[:authors][user] = "#{name} <#{email}>"
522 file_list.each do |arg|
527 not_found "RCS file #{arg}"
530 filename = File.basename(arg, SFX)
532 filename = File.basename(arg)
533 path = File.dirname(arg)
534 rcsfile = File.join(path, 'RCS', filename) + SFX
535 unless File.exists? rcsfile
536 rcsfile.replace File.join(path, filename) + SFX
537 unless File.exists? rcsfile
538 not_found "RCS file for #{filename} in #{path}"
543 RCS.parse(filename, rcsfile, parse_options)