use strip() iso. slicing for removing \n
[fast-export/rorcz.git] / svn-fast-export.py
bloba2304c76b64f6ba2002af941f5e006fbbf724e03
1 #!/usr/bin/python
3 # svn-fast-export.py
4 # ----------
5 # Walk through each revision of a local Subversion repository and export it
6 # in a stream that git-fast-import can consume.
8 # Author: Chris Lee <clee@kde.org>
9 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
11 trunk_path = '/trunk/'
12 branches_path = '/branches/'
13 tags_path = '/tags/'
15 first_rev = 1
16 final_rev = 0
18 import gc, sys, os.path
19 from optparse import OptionParser
20 from time import sleep, mktime, localtime, strftime, strptime
21 from svn.fs import svn_fs_dir_entries, 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_revision_prop, svn_fs_paths_changed
22 from svn.core import svn_pool_create, svn_pool_clear, svn_pool_destroy, svn_stream_read, svn_stream_for_stdout, svn_stream_copy, svn_stream_close, run_app
23 from svn.repos import svn_repos_open, svn_repos_fs
25 ct_short = ['M', 'A', 'D', 'R', 'X']
27 def dump_file_blob(root, full_path, pool):
28 stream_length = svn_fs_file_length(root, full_path, pool)
29 stream = svn_fs_file_contents(root, full_path, pool)
30 sys.stdout.write("data %s\n" % stream_length)
31 ostream = svn_stream_for_stdout(pool)
32 svn_stream_copy(stream, ostream, pool)
33 sys.stdout.write("\n")
36 def export_revision(rev, repo, fs, pool):
37 sys.stderr.write("Exporting revision %s... " % rev)
39 revpool = svn_pool_create(pool)
40 svn_pool_clear(revpool)
42 # Open a root object representing the youngest (HEAD) revision.
43 root = svn_fs_revision_root(fs, rev, revpool)
45 # And the list of what changed in this revision.
46 changes = svn_fs_paths_changed(root, revpool)
48 i = 1
49 marks = {}
50 file_changes = []
52 for path, change_type in changes.iteritems():
53 c_t = ct_short[change_type.change_kind]
54 if svn_fs_is_dir(root, path, revpool):
55 continue
57 if not path.startswith(trunk_path):
58 # We don't handle branches. Or tags. Yet.
59 pass
60 else:
61 if c_t == 'D':
62 file_changes.append("D %s" % path.replace(trunk_path, ''))
63 else:
64 marks[i] = path.replace(trunk_path, '')
65 file_changes.append("M 644 :%s %s" % (i, marks[i]))
66 sys.stdout.write("blob\nmark :%s\n" % i)
67 dump_file_blob(root, path, revpool)
68 i += 1
70 # Get the commit author and message
71 props = svn_fs_revision_proplist(fs, rev, revpool)
73 # Do the recursive crawl.
74 if props.has_key('svn:author'):
75 author = "%s <%s@localhost>" % (props['svn:author'], props['svn:author'])
76 else:
77 author = 'nobody <nobody@localhost>'
79 if len(file_changes) == 0:
80 svn_pool_destroy(revpool)
81 sys.stderr.write("skipping.\n")
82 return
84 svndate = props['svn:date'][0:-8]
85 commit_time = mktime(strptime(svndate, '%Y-%m-%dT%H:%M:%S'))
86 sys.stdout.write("commit refs/heads/master\n")
87 sys.stdout.write("committer %s %s -0000\n" % (author, int(commit_time)))
88 sys.stdout.write("data %s\n" % len(props['svn:log']))
89 sys.stdout.write(props['svn:log'])
90 sys.stdout.write("\n")
91 sys.stdout.write('\n'.join(file_changes))
92 sys.stdout.write("\n\n")
94 svn_pool_destroy(revpool)
96 sys.stderr.write("done!\n")
98 #if rev % 1000 == 0:
99 # sys.stderr.write("gc: %s objects\n" % len(gc.get_objects()))
100 # sleep(5)
103 def crawl_revisions(pool, repos_path):
104 """Open the repository at REPOS_PATH, and recursively crawl all its
105 revisions."""
106 global final_rev
108 # Open the repository at REPOS_PATH, and get a reference to its
109 # versioning filesystem.
110 repos_obj = svn_repos_open(repos_path, pool)
111 fs_obj = svn_repos_fs(repos_obj)
113 # Query the current youngest revision.
114 youngest_rev = svn_fs_youngest_rev(fs_obj, pool)
117 first_rev = 1
118 if final_rev == 0:
119 final_rev = youngest_rev
120 for rev in xrange(first_rev, final_rev + 1):
121 export_revision(rev, repos_obj, fs_obj, pool)
124 if __name__ == '__main__':
125 usage = '%prog [options] REPOS_PATH'
126 parser = OptionParser()
127 parser.set_usage(usage)
128 parser.add_option('-f', '--final-rev', help='Final revision to import',
129 dest='final_rev', metavar='FINAL_REV', type='int')
130 parser.add_option('-t', '--trunk-path', help='Path in repo to /trunk',
131 dest='trunk_path', metavar='TRUNK_PATH')
132 parser.add_option('-b', '--branches-path', help='Path in repo to /branches',
133 dest='branches_path', metavar='BRANCHES_PATH')
134 parser.add_option('-T', '--tags-path', help='Path in repo to /tags',
135 dest='tags_path', metavar='TAGS_PATH')
136 (options, args) = parser.parse_args()
138 if options.trunk_path != None:
139 trunk_path = options.trunk_path
140 if options.branches_path != None:
141 branches_path = options.branches_path
142 if options.tags_path != None:
143 tags_path = options.tags_path
144 if options.final_rev != None:
145 final_rev = options.final_rev
147 if len(args) != 1:
148 parser.print_help()
149 sys.exit(2)
151 # Canonicalize (enough for Subversion, at least) the repository path.
152 repos_path = os.path.normpath(args[0])
153 if repos_path == '.':
154 repos_path = ''
156 # Call the app-wrapper, which takes care of APR initialization/shutdown
157 # and the creation and cleanup of our top-level memory pool.
158 run_app(crawl_revisions, repos_path)