Skip to content

Instantly share code, notes, and snippets.

@dchest
Created November 2, 2009 12:48
Show Gist options
  • Save dchest/224141 to your computer and use it in GitHub Desktop.
Save dchest/224141 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Convert Git repository to Fossil
#
# Use this script inside git working copy:
# (make a backup copy before using it!)
#
# $ cd git-repo
# $ ruby ../git2fossil.rb
#
# fossil repository will be created at ../repo.fossil
# The script doesn't convert tags, branches, or users.
# It works by checking out every change from git and
# committing them to fossil.
require 'time'
log = []
ci = nil
IO.popen("git log master --date=iso8601","r").each do |line|
if line =~ /^commit/
log.push(ci) unless ci.nil?
ci = {}
ci['commit'] = line.scan(/commit (.*)/)[0][0]
ci['date'] = ""
ci['message'] = ""
elsif line =~ /^Date:/
date_str = line.scan(/Date:(.*)/)[0][0].strip
date = Time.parse(date_str)
ci['date'] = date.xmlschema[0..-7]
elsif line =~ /[ ]{4}/ # commit message after 4 spaces
ci['message'] += line.strip
end
end
log.push(ci)
`fossil new ../repo.fossil`
`fossil open ../repo.fossil`
log.reverse_each do |ci|
if ci['commit'].length > 0
IO.popen("fossil changes","r").each do |line|
if line =~ /^MISSING/
file = line.scan(/MISSING (.*)/)[0][0].strip
system("fossil rm \"#{file}\"")
end
end
system("git checkout #{ci['commit']} --force")
`fossil add .`
system("fossil commit --nosign -m \"#{ci['message']}\" " +
"--date-override #{ci['date']}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment