Skip to content

Instantly share code, notes, and snippets.

@mgax
Created May 12, 2010 12:56
Show Gist options
  • Save mgax/398556 to your computer and use it in GitHub Desktop.
Save mgax/398556 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
""" Make a Git checkout from an SVN repository """
import os
from subprocess import check_call, check_output
def option_parser():
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog svn_url [-a]")
parser.add_option('-a', '--all', help='fetch all history',
action="store_true", dest='all_history', default=False)
parser.add_option('-r', '--revision', help='fetch specific revision',
type="int", dest='revision', default=None)
parser.add_option('-b', '--branch-name', type='str')
return parser
def patch_git_branch_name(branch_name):
with open('.git/config', 'rb') as f:
git_config = f.read()
git_config = git_config.replace('refs/remotes/git-svn', 'refs/remotes/svn')
with open('.git/config', 'wb') as f:
f.write(git_config)
os.rmdir('.git/svn/refs/remotes/git-svn')
def oldest_svn_revision(url):
log = check_output(['svn', 'log', url, '-q'])
log_line = log.splitlines()[-2]
rev = log_line.split()[0]
assert rev[0] == 'r'
return rev[1:] # rev_number
def main():
(options, args) = option_parser().parse_args()
if len(args) != 1:
parser.error('You must specify exactly one SVN URL')
url = args[0]
dirname = url.split('/')[-2 if url.endswith('/') else -1]
if options.revision is not None:
rev = str(options.revision)
elif options.all_history:
rev = oldest_svn_revision(url)
else:
rev = None
check_call(['mkdir', dirname])
os.chdir(dirname)
check_call(['git', 'svn', 'init', url])
if options.branch_name is not None:
branch_name = options.branch_name
patch_git_branch_name(branch_name)
else:
branch_name = 'git-svn'
if rev is not None:
check_call(['git', 'svn', 'fetch', '-r', rev])
check_call(['git', 'svn', 'fetch'])
check_call(['git', 'merge', branch_name])
check_call(['git', 'checkout'])
if __name__ == '__main__':
main()
@xarg
Copy link

xarg commented May 12, 2010

svn2git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment