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
341 adding = false unless count > 0
346 raise 'malformed diff' unless l =~ /^([ad])(\d+) (\d+)$/
352 # we replace them with empty string so that 'a' commands
353 # referring to the same line work properly
364 # turn the buffer into an array of lines, deleting the empty ones
365 buffer.delete_if { |l| l.empty? }
368 rcs.revision[rev].text = buffer
369 puts rcs.revision[rev].blob
372 STDERR.puts "Unknown status #{status.last}"
378 # clean up the symbols/branches: look for revisions that have
379 # one or more symbols but no dates, and make them into
380 # branches, pointing to the highest commit with that key
382 keys = rcs.revision.keys
383 rcs.revision.each do |key, rev|
384 if rev.date.nil? and not rev.symbols.empty?
385 top = keys.select { |k| k.match(/^#{key}\./) }.sort.last
386 tr = rcs.revision[top]
387 raise "unhandled complex branch structure met: #{rev.inspect} refers #{tr.inspect}" if tr.date.nil?
388 tr.branches |= rev.symbols
392 branches.each { |k| rcs.revision.delete k }
395 rcs.export_commits(opts)
401 opts = GetoptLong.new(
402 # Authors file, like git-svn and git-cvsimport, more than one can be
404 ['--authors-file', '-A', GetoptLong::REQUIRED_ARGUMENT],
405 # RCS file suffix, like RCS
406 ['--rcs-suffixes', '-x', GetoptLong::REQUIRED_ARGUMENT],
408 ['--tag-each-rev', GetoptLong::NO_ARGUMENT],
409 ['--no-tag-each-rev', GetoptLong::NO_ARGUMENT],
410 ['--help', '-h', '-?', GetoptLong::NO_ARGUMENT]
413 # We read options in order, but they apply to all passed parameters.
414 # TODO maybe they should only apply to the following, unless there's only one
416 opts.ordering = GetoptLong::RETURN_IN_ORDER
420 :authors => Hash.new,
423 # Read config options
424 `git config --get-all rcs.authorsfile`.each_line do |fn|
425 parse_options[:authors].merge! load_authors_file(fn.chomp)
428 parse_options[:tag_each_rev] = (
429 `git config --bool rcs.tageachrev`.chomp == 'true'
432 opts.each do |opt, arg|
434 when '--authors-file'
435 authors = load_authors_file(arg)
436 redef = parse_options[:authors].keys & authors.keys
437 STDERR.puts "Authors file #{arg} redefines #{redef.join(', ')}" unless redef.empty?
438 parse_options[:authors].merge!(authors)
439 when '--rcs-suffixes'
441 when '--tag-each-rev'
442 parse_options[:tag_each_rev] = true
443 when '--no-tag-each-rev'
444 # this is the default, which is fine since the missing key
445 # (default) returns nil which is false in Ruby
446 parse_options[:tag_each_rev] = false
457 user = Etc.getlogin || ENV['USER']
459 # steal username/email data from other init files that may contain the
463 # the user's .hgrc file for a username field
464 ['~/.hgrc', /^\s*username\s*=\s*(["'])?(.*)\1$/, 2],
465 # the user's .(g)vimrc for a changelog_username setting
466 ['~/.vimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
467 ['~/.gvimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
469 ].each do |fn, rx, idx|
470 file = File.expand_path fn
471 if File.readable?(file) and File.read(file) =~ rx
472 parse_options[:authors][user] = Regexp.last_match(idx).strip
478 if user and not user.empty? and not parse_options[:authors].has_key?(user)
479 name = ENV['GIT_AUTHOR_NAME'] || ''
480 name.replace(`git config user.name`.chomp) if name.empty?
481 name.replace(Etc.getpwnam(user).gecos) if name.empty?
484 # couldn't find a name, try to steal data from other sources
487 # if we found a name, try to find an email too
488 email = ENV['GIT_AUTHOR_EMAIL'] || ''
489 email.replace(`git config user.email`.chomp) if email.empty?
492 # couldn't find an email, try to steal data too
495 # we got both a name and email, fill the info
496 parse_options[:authors][user] = "#{name} <#{email}>"
510 file_list.each do |arg|
515 not_found "RCS file #{arg}"
518 filename = File.basename(arg, SFX)
520 filename = File.basename(arg)
521 path = File.dirname(arg)
522 rcsfile = File.join(path, 'RCS', filename) + SFX
523 unless File.exists? rcsfile
524 rcsfile.replace File.join(path, filename) + SFX
525 unless File.exists? rcsfile
526 not_found "RCS file for #{filename} in #{path}"
531 RCS.parse(filename, rcsfile, parse_options)