chris@1544: #!/usr/bin/python chris@1544: # chris@1544: # svn-fast-export.py chris@1544: # ---------- chris@1544: # Walk through each revision of a local Subversion repository and export it chris@1544: # in a stream that git-fast-import can consume. chris@1544: # chris@1544: # Author: Chris Lee chris@1544: # License: MIT chris@1544: chris@1544: trunk_path = '/trunk/' chris@1544: branches_path = '/branches/' chris@1544: tags_path = '/tags/' chris@1544: chris@1544: first_rev = 1 chris@1544: final_rev = 0 chris@1544: chris@1544: import sys, os.path chris@1544: from optparse import OptionParser chris@1544: from time import mktime, strptime chris@1544: from svn.fs import svn_fs_file_length, svn_fs_file_contents, svn_fs_is_dir, svn_fs_revision_root, svn_fs_youngest_rev, svn_fs_revision_proplist, svn_fs_paths_changed chris@1544: from svn.core import svn_pool_create, svn_pool_clear, svn_pool_destroy, svn_stream_for_stdout, svn_stream_copy, svn_stream_close, run_app chris@1544: from svn.repos import svn_repos_open, svn_repos_fs chris@1544: chris@1544: ct_short = ['M', 'A', 'D', 'R', 'X'] chris@1544: chris@1544: def dump_file_blob(root, full_path, pool): chris@1544: stream_length = svn_fs_file_length(root, full_path, pool) chris@1544: stream = svn_fs_file_contents(root, full_path, pool) chris@1544: sys.stdout.write("data %s\n" % stream_length) chris@1544: sys.stdout.flush() chris@1544: ostream = svn_stream_for_stdout(pool) chris@1544: svn_stream_copy(stream, ostream, pool) chris@1544: svn_stream_close(ostream) chris@1544: sys.stdout.write("\n") chris@1544: chris@1544: chris@1544: def export_revision(rev, repo, fs, pool): chris@1544: sys.stderr.write("Exporting revision %s... " % rev) chris@1544: chris@1544: revpool = svn_pool_create(pool) chris@1544: svn_pool_clear(revpool) chris@1544: chris@1544: # Open a root object representing the youngest (HEAD) revision. chris@1544: root = svn_fs_revision_root(fs, rev, revpool) chris@1544: chris@1544: # And the list of what changed in this revision. chris@1544: changes = svn_fs_paths_changed(root, revpool) chris@1544: chris@1544: i = 1 chris@1544: marks = {} chris@1544: file_changes = [] chris@1544: chris@1544: for path, change_type in changes.iteritems(): chris@1544: c_t = ct_short[change_type.change_kind] chris@1544: if svn_fs_is_dir(root, path, revpool): chris@1544: continue chris@1544: chris@1544: if not path.startswith(trunk_path): chris@1544: # We don't handle branches. Or tags. Yet. chris@1544: pass chris@1544: else: chris@1544: if c_t == 'D': chris@1544: file_changes.append("D %s" % path.replace(trunk_path, '')) chris@1544: else: chris@1544: marks[i] = path.replace(trunk_path, '') chris@1544: file_changes.append("M 644 :%s %s" % (i, marks[i])) chris@1544: sys.stdout.write("blob\nmark :%s\n" % i) chris@1544: dump_file_blob(root, path, revpool) chris@1544: i += 1 chris@1544: chris@1544: # Get the commit author and message chris@1544: props = svn_fs_revision_proplist(fs, rev, revpool) chris@1544: chris@1544: # Do the recursive crawl. chris@1544: if props.has_key('svn:author'): chris@1544: author = "%s <%s@localhost>" % (props['svn:author'], props['svn:author']) chris@1544: else: chris@1544: author = 'nobody ' chris@1544: chris@1544: if len(file_changes) == 0: chris@1544: svn_pool_destroy(revpool) chris@1544: sys.stderr.write("skipping.\n") chris@1544: return chris@1544: chris@1544: svndate = props['svn:date'][0:-8] chris@1544: commit_time = mktime(strptime(svndate, '%Y-%m-%dT%H:%M:%S')) chris@1544: sys.stdout.write("commit refs/heads/master\n") chris@1544: sys.stdout.write("committer %s %s -0000\n" % (author, int(commit_time))) chris@1544: sys.stdout.write("data %s\n" % len(props['svn:log'])) chris@1544: sys.stdout.write(props['svn:log']) chris@1544: sys.stdout.write("\n") chris@1544: sys.stdout.write('\n'.join(file_changes)) chris@1544: sys.stdout.write("\n\n") chris@1544: chris@1544: svn_pool_destroy(revpool) chris@1544: chris@1544: sys.stderr.write("done!\n") chris@1544: chris@1544: #if rev % 1000 == 0: chris@1544: # sys.stderr.write("gc: %s objects\n" % len(gc.get_objects())) chris@1544: # sleep(5) chris@1544: chris@1544: chris@1544: def crawl_revisions(pool, repos_path): chris@1544: """Open the repository at REPOS_PATH, and recursively crawl all its chris@1544: revisions.""" chris@1544: global final_rev chris@1544: chris@1544: # Open the repository at REPOS_PATH, and get a reference to its chris@1544: # versioning filesystem. chris@1544: repos_obj = svn_repos_open(repos_path, pool) chris@1544: fs_obj = svn_repos_fs(repos_obj) chris@1544: chris@1544: # Query the current youngest revision. chris@1544: youngest_rev = svn_fs_youngest_rev(fs_obj, pool) chris@1544: chris@1544: chris@1544: first_rev = 1 chris@1544: if final_rev == 0: chris@1544: final_rev = youngest_rev chris@1544: for rev in xrange(first_rev, final_rev + 1): chris@1544: export_revision(rev, repos_obj, fs_obj, pool) chris@1544: chris@1544: chris@1544: if __name__ == '__main__': chris@1544: usage = '%prog [options] REPOS_PATH' chris@1544: parser = OptionParser() chris@1544: parser.set_usage(usage) chris@1544: parser.add_option('-f', '--final-rev', help='Final revision to import', chris@1544: dest='final_rev', metavar='FINAL_REV', type='int') chris@1544: parser.add_option('-t', '--trunk-path', help='Path in repo to /trunk', chris@1544: dest='trunk_path', metavar='TRUNK_PATH') chris@1544: parser.add_option('-b', '--branches-path', help='Path in repo to /branches', chris@1544: dest='branches_path', metavar='BRANCHES_PATH') chris@1544: parser.add_option('-T', '--tags-path', help='Path in repo to /tags', chris@1544: dest='tags_path', metavar='TAGS_PATH') chris@1544: (options, args) = parser.parse_args() chris@1544: chris@1544: if options.trunk_path != None: chris@1544: trunk_path = options.trunk_path chris@1544: if options.branches_path != None: chris@1544: branches_path = options.branches_path chris@1544: if options.tags_path != None: chris@1544: tags_path = options.tags_path chris@1544: if options.final_rev != None: chris@1544: final_rev = options.final_rev chris@1544: chris@1544: if len(args) != 1: chris@1544: parser.print_help() chris@1544: sys.exit(2) chris@1544: chris@1544: # Canonicalize (enough for Subversion, at least) the repository path. chris@1544: repos_path = os.path.normpath(args[0]) chris@1544: if repos_path == '.': chris@1544: repos_path = '' chris@1544: chris@1544: # Call the app-wrapper, which takes care of APR initialization/shutdown chris@1544: # and the creation and cleanup of our top-level memory pool. chris@1544: run_app(crawl_revisions, repos_path)