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 rev.symbols.each do |sym|
146 puts "reset refs/tags/#{sym}"
147 puts "from :#{RCS.commit key}"
149 if opts[:tag_each_rev]
150 puts "reset refs/tags/#{key}"
151 puts "from :#{RCS.commit key}"
156 exported.each { |k| @revision.delete(k) }
162 attr_accessor :rev, :author, :date, :state, :next
163 attr_accessor :branches, :log, :text, :symbols
164 attr_accessor :branch, :diff_base, :branch_point
181 @date = Time.rcs(str)
186 ret = "blob\nmark :#{RCS.blob @rev}\ndata #{str.length}\n#{str}\n"
191 def RCS.parse(fname, rcsfile, opts={})
192 rcs = RCS::File.new(fname)
194 ::File.open(rcsfile, 'r') do |file|
199 file.each_line do |line|
202 command, args = line.split($;,2)
203 next if command.empty?
207 rcs.head = RCS.clean(args.chomp)
211 rcs.comment = RCS.at_clean(args.chomp)
214 if rcs.has_revision?(rev)
215 status.push :revision_data
217 status.push :new_revision
222 status.push :read_lines
224 STDERR.puts "Skipping unhandled command #{command.inspect}"
227 sym, rev = line.strip.split(':',2);
228 status.pop if rev.chomp!(';')
229 rcs.revision[rev].symbols << sym
231 rcs.desc.replace lines.dup
234 # we sanitize lines as we read them
236 actual_line = line.dup
238 # the first line must begin with a @, which we strip
240 ats = line.match(/^@+/)
241 raise 'malformed line' unless ats
242 actual_line.replace line.sub(/^@/,'')
245 # if the line ends with an ODD number of @, it's the
246 # last line -- we work on actual_line so that content
247 # such as @\n or @ work correctly (they would be
248 # encoded respectively as ['@@@\n','@\n'] and
250 ats = actual_line.chomp.match(/@+$/)
251 if nomore = (ats && Regexp.last_match(0).length.odd?)
252 actual_line.replace actual_line.chomp.sub(/@$/,'')
254 lines << actual_line.gsub('@@','@')
261 when /^date\s+(\S+);\s+author\s+(\S+);\sstate\s(\S+);$/
262 rcs.revision[rev].date = $1
263 rcs.revision[rev].author = $2
264 rcs.revision[rev].state = $3
266 status.push :branches
269 when /^next\s+(\S+)?;$/
270 nxt = rcs.revision[rev].next = $1
272 raise "multiple diff_bases for #{nxt}" unless rcs.revision[nxt].diff_base.nil?
273 rcs.revision[nxt].diff_base = rev
274 rcs.revision[nxt].branch = rcs.revision[rev].branch
279 candidate = line.split(';',2)
280 branch = candidate.first.strip
281 rcs.revision[rev].branches.push branch
282 raise "multiple diff_bases for #{branch}" unless rcs.revision[branch].diff_base.nil?
283 rcs.revision[branch].diff_base = rev
284 # we drop the last number from the branch name
285 rcs.revision[branch].branch = branch.sub(/\.\d+$/,'.x')
286 rcs.revision[branch].branch_point = rev
287 status.pop if candidate.length > 1
293 status.push :read_lines
301 status.push :read_lines
306 rcs.revision[rev].log.replace lines.dup
309 rcs.revision[rev].text.replace lines.dup
310 puts rcs.revision[rev].blob
313 difflines.replace lines.dup
314 difflines.pop if difflines.last.empty?
315 base = rcs.revision[rev].diff_base
316 unless rcs.revision[base].text
319 raise 'no diff base!'
323 rcs.revision[base].text.each { |l| buffer << l.dup }
329 while l = difflines.shift
333 adding = false unless count > 0
338 raise 'malformed diff' unless l =~ /^([ad])(\d+) (\d+)$/
343 # we replace them with empty string so that 'a' commands
344 # referring to the same line work properly
346 buffer[index].replace ''
356 buffer.delete_if { |l| l.empty? }
358 rcs.revision[rev].text = buffer
359 puts rcs.revision[rev].blob
362 STDERR.puts "Unknown status #{status.last}"
367 rcs.export_commits(opts)
373 opts = GetoptLong.new(
374 # Authors file, like git-svn and git-cvsimport, more than one can be
376 ['--authors-file', '-A', GetoptLong::REQUIRED_ARGUMENT],
377 # RCS file suffix, like RCS
378 ['--rcs-suffixes', '-x', GetoptLong::REQUIRED_ARGUMENT],
380 ['--tag-each-rev', GetoptLong::NO_ARGUMENT],
381 ['--no-tag-each-rev', GetoptLong::NO_ARGUMENT],
382 ['--help', '-h', '-?', GetoptLong::NO_ARGUMENT]
385 # We read options in order, but they apply to all passed parameters.
386 # TODO maybe they should only apply to the following, unless there's only one
388 opts.ordering = GetoptLong::RETURN_IN_ORDER
392 :authors => Hash.new,
395 # Read config options
396 `git config --get-all rcs.authorsfile`.each_line do |fn|
397 parse_options[:authors].merge! load_authors_file(fn.chomp)
400 parse_options[:tag_each_rev] = (
401 `git config --bool rcs.tageachrev`.chomp == 'true'
404 opts.each do |opt, arg|
406 when '--authors-file'
407 authors = load_authors_file(arg)
408 redef = parse_options[:authors].keys & authors.keys
409 STDERR.puts "Authors file #{arg} redefines #{redef.join(', ')}" unless redef.empty?
410 parse_options[:authors].merge!(authors)
411 when '--rcs-suffixes'
413 when '--tag-each-rev'
414 parse_options[:tag_each_rev] = true
415 when '--no-tag-each-rev'
416 # this is the default, which is fine since the missing key
417 # (default) returns nil which is false in Ruby
418 parse_options[:tag_each_rev] = false
429 user = Etc.getlogin || ENV['USER']
431 # steal username/email data from other init files that may contain the
435 # the user's .hgrc file for a username field
436 ['~/.hgrc', /^\s*username\s*=\s*(["'])?(.*)\1$/, 2],
437 # the user's .(g)vimrc for a changelog_username setting
438 ['~/.vimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
439 ['~/.gvimrc', /changelog_username\s*=\s*(["'])?(.*)\1$/, 2],
441 ].each do |fn, rx, idx|
442 file = File.expand_path fn
443 if File.readable?(file) and File.read(file) =~ rx
444 parse_options[:authors][user] = Regexp.last_match(idx).strip
450 if user and not user.empty? and not parse_options[:authors].has_key?(user)
451 name = ENV['GIT_AUTHOR_NAME'] || ''
452 name.replace(`git config user.name`.chomp) if name.empty?
453 name.replace(Etc.getpwnam(user).gecos) if name.empty?
456 # couldn't find a name, try to steal data from other sources
459 # if we found a name, try to find an email too
460 email = ENV['GIT_AUTHOR_EMAIL'] || ''
461 email.replace(`git config user.email`.chomp) if email.empty?
464 # couldn't find an email, try to steal data too
467 # we got both a name and email, fill the info
468 parse_options[:authors][user] = "#{name} <#{email}>"
482 file_list.each do |arg|
487 not_found "RCS file #{arg}"
490 filename = File.basename(arg, SFX)
492 filename = File.basename(arg)
493 path = File.dirname(arg)
494 rcsfile = File.join(path, 'RCS', filename) + SFX
495 unless File.exists? rcsfile
496 rcsfile.replace File.join(path, filename) + SFX
497 unless File.exists? rcsfile
498 not_found "RCS file for #{filename} in #{path}"
503 RCS.parse(filename, rcsfile, parse_options)