Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created July 17, 2010 22:52
Show Gist options
  • Save jamesu/479917 to your computer and use it in GitHub Desktop.
Save jamesu/479917 to your computer and use it in GitHub Desktop.
# reformat_log - Reformats log messages
# USAGE:
# reformat_log [file]
#
# Will read the SVN-style commit log from file and generate commands in [file].sh
# which will recreate the commits in a git repository.
# Assumes the following:
# * You have a .patch file generated using extract_patches
# * Your git repository is in "trunk"
# * A copy of your repository in its final state in "all-files" (if you are adding binary files)
#
MAIN_AUTHOR = '' # main author (i.e. username you are commiting as)
BINARY_FILES = ['voc', 'bmp'] # e.g.
AUTHOR_MAP = {} # e.g. 'billg' => '[email protected]'
File.open(ARGV[0], "r") do |in_file|
File.open(ARGV[0] + '.sh', 'w') do |out_file|
parse_mode = :scan
puts "IN: #{ARGV[0]}"
puts "OUT: #{ARGV[0] + '.sh'}"
files = {:add => [], :remove => []}
the_date = nil
the_log = []
the_revision = nil
the_author = nil
while !in_file.eof do
line = in_file.readline
split = line.split
next if split.empty?
case split[0]
when 'Author:'
if split[1] != MAIN_AUTHOR
the_author = "#{split[1]} <#{AUTHOR_MAP[split[1]]}>"
end
when 'Date:'
# Date: 2009-08-03 20:03:07 +0200 (Mon, 03 Aug 2009)
the_date = line.match(/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) .([0-9]{4})/)
the_date = the_date[0] if the_date
when 'Added:'
parse_mode = :add
when 'Removed:'
parse_mode = :remove
when 'Modified:'
parse_mode = :add
when 'New'
if line[1] == 'Revision:'
the_revision = lines[2]
end
when 'Log:'
parse_mode = :log
else
if parse_mode == :log
the_log << line
elsif parse_mode != :scan
sub_line = line.strip.gsub(/ /, '\ ')
files[parse_mode] << sub_line.split('/')[1..-1].join('/')
end
end
end
the_revision ||= ARGV[0].split('.')[0][1..-1] # fallback: r123
puts "REVISION: #{the_revision}"
puts "DATE: #{the_date}"
puts "LOG: #{the_log}"
puts "FILES TO ADD: #{files[:add].join(',')}"
puts "FILES TO REMOVE: #{files[:remove].join(',')}"
out_file.puts "#!/bin/sh"
out_file.puts "patch -p0 < r#{the_revision}.patch"
out_file.puts "cd trunk"
files[:add].each do |file|
if BINARY_FILES.include?(file[-3..-1])
out_file.puts "cp ../all-files/#{file} #{file}"
end
end
out_file.puts "git add #{files[:add].join(' ')}" unless files[:add].empty?
out_file.puts "git rm #{files[:remove].join(' ')}" unless files[:remove].empty?
log = the_log.join
author = the_author ? "--author \"#{the_author}\"" : ''
out_file.puts "git commit #{author} --date \"#{the_date}\" -m \"New Revision: #{the_revision}\n\n#{log}\""
out_file.puts "cd .."
puts "DONE"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment