Created
August 8, 2008 01:00
-
-
Save subtleGradient/4529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class File | |
def self.svn? path | |
File.exists?(path + '/.svn/') | |
end | |
def self.git? path | |
File.exists?(path + '/.git/') | |
end | |
def self.hg? path | |
File.exists?(path + '/.hg/') | |
end | |
end | |
def git_init!(path) | |
puts 'Not a Subversion or Git repo already.' | |
puts 'Gitting...' | |
command = %`cd '#{path}'; git init; git add ./; git ci -m 'First Commit'` | |
puts command | |
system command unless TESTING | |
puts | |
end | |
def convert_svn_to_git!(path) | |
puts "Is a Subversion repo." | |
# abort if dirty | |
abort "Working copy is dirty. Clean up and try again :P \n or use the --force" unless `cd '#{path}'; svn st`.chomp.empty? unless FORCE | |
puts "Converting to git..." | |
# Get subversion info | |
svn_info = `cd '#{path}'; svn info` | |
# Parse out the url | |
svn_url = svn_info.scan(/URL: (.*?)\n/).first.first | |
require 'cgi' | |
svn_url = CGI.unescape svn_url | |
git_path = $path + '.gitting' | |
svn_path = $path + '.svn' | |
abort "#{git_path} exists!" if File.exists? git_path | |
abort "#{svn_path} exists!" if File.exists? svn_path | |
command = %`\ngit-svn clone '#{svn_url}' \\\n '#{git_path}'\n` | |
puts command;system command unless TESTING | |
# Swap the out with the new | |
command = %`mv '#$path' \\\n '#{svn_path}'` | |
puts command;system command unless TESTING | |
command = %`mv '#{git_path}' \\\n '#$path'` | |
puts command;system command unless TESTING | |
command = %`cd '#$path'` | |
puts command;system command unless TESTING | |
# Get rid of the original | |
puts "\n# You'll need to manually remove the original Subversion folder\n rm -rf '#{svn_path}'" | |
end | |
TESTING = true | |
# Setup | |
$path = File.expand_path(ARGV[0] || ENV['PWD']) | |
$path = ENV['PWD'] if ARGV[0] == '--force' | |
FORCE = ARGV[0] == '--force' | |
# Help | |
abort %`Bad path :(\nUsage: #{File.basename __FILE__} [PATH] [--force]` unless File.exists? $path | |
# Fail if this is already a git repo | |
abort 'Alteady a git repo :-/' if File.git? $path | |
# If an hg repo | |
abort "Doesn't support Mercruial yet :'(" if File.hg? $path | |
# If this isn't a subversion repo... | |
if File.svn? $path | |
# then just git init. | |
git_init! $path unless File.svn? $path | |
else | |
# Otherwise, convert! | |
convert_svn_to_git! $path | |
end | |
puts | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment