-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvn2git.py
More file actions
executable file
·325 lines (228 loc) · 9.77 KB
/
svn2git.py
File metadata and controls
executable file
·325 lines (228 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /usr/bin/env python3
import argparse
import os
import re
import shutil
import subprocess
import tempfile
import unittest
def parse_arguments():
parser = argparse.ArgumentParser(
description='An interactive tool to convert SVN repositories to GIT'
)
parser.add_argument("repo_dump_gz", help='gzipped SVN repo dump file')
return parser.parse_args()
################################################################################
def come_up_with_repo_name(gzipped_dump_fname):
if not gzipped_dump_fname:
raise ValueError('Empty input')
return os.path.basename(gzipped_dump_fname).split('.')[0]
class TestComeUpWithRepoName(unittest.TestCase):
def test_empty_string(self):
with self.assertRaises(ValueError):
come_up_with_repo_name('')
def test_nonempty_strings(self):
testcases = [
('name-without-dots', 'name-without-dots'),
('name.with.dots', 'name'),
('/tmp/some.dump.gz', 'some'),
('../my-dump.gz', 'my-dump')
]
for path, repo_name in testcases:
result = come_up_with_repo_name(path)
self.assertEqual(repo_name, result)
################################################################################
class Repository:
def __init__(self, dump_fname, tmp_dir):
self.name = come_up_with_repo_name(dump_fname)
self.abs_path = os.path.join(tmp_dir, self.name)
self.url = 'file://{}'.format(self.abs_path)
subprocess.check_call(['svnadmin', 'create', self.abs_path])
def is_standard_layout(self):
return sorted(self.list('/')) == 'branches/ tags/ trunk/'.split()
def list(self, path):
return subprocess.check_output(
['svn', 'ls', self.url + '/' + path],
encoding='UTF-8'
).splitlines()
def restore_from_dump(self, dump_fname):
gunzip = subprocess.Popen(['gunzip', '-c', dump_fname], stdout=subprocess.PIPE)
svnadmin = subprocess.Popen(['svnadmin', 'load', '-q', self.abs_path], stdin=gunzip.stdout)
svnadmin.communicate()
################################################################################
def parse_author(author_xml):
author_tag = '<author>'
if not author_xml.startswith(author_tag):
raise ValueError('Not an {} element'.format(author_tag))
v = author_xml[len(author_tag):]
bracket_pos = v.index('<')
return v[:bracket_pos]
class TestParseAuthor(unittest.TestCase):
def test_empty_input(self):
with self.assertRaises(ValueError):
parse_author('')
def test_regular_input(self):
self.assertEqual('me', parse_author('<author>me</author>'))
################################################################################
def parse_svn_log_authors(log_output):
log_lines = log_output.split('\n')
return sorted(list(set([parse_author(s) for s in log_lines if s.startswith('<author>')])))
class TestGetAuthors(unittest.TestCase):
def test_empty_input(self):
self.assertEqual([], parse_svn_log_authors(''))
def test_regular_input(self):
log_output = """
<author>gli</author>
<date>2006-10-19T19:42:33.061832Z</date>
<author>xyz</author>
<date>2006-10-16T20:09:08.871296Z</date>
"""
self.assertEqual(['gli', 'xyz'], parse_svn_log_authors(log_output))
def test_repeating_authors(self):
log_output = """
<author>gli</author>
<date>2006-10-19T19:42:33.061832Z</date>
<author>gli</author>
<date>2006-10-16T20:09:08.871296Z</date>
"""
self.assertEqual(['gli'], parse_svn_log_authors(log_output))
def test_output_is_sorted(self):
log_output = """
<author>q</author>
<date>2006-10-19T19:42:33.061832Z</date>
<author>a</author>
<date>2006-10-16T20:09:08.871296Z</date>
<author>z</author>
<date>2006-10-16T20:09:08.871296Z</date>
"""
self.assertEqual(['a', 'q', 'z'], parse_svn_log_authors(log_output))
# TODO: test the order
################################################################################
class WorkingCopy:
def __init__(self, tmp_dir, repo, branch):
self.abs_path = os.path.join(tmp_dir, 'wc')
subprocess.check_call(
['svn', 'co', '-q', 'file://{}/{}'.format(repo.abs_path, branch), self.abs_path]
)
def get_authors(self):
log_output = subprocess.check_output(
['svn', 'log', '--xml', '--quiet', self.abs_path],
encoding='UTF-8'
)
return parse_svn_log_authors(log_output)
################################################################################
def extract_keys_from_prompt(prompt):
p = re.compile('\[.\]')
matches = re.findall(p, prompt)
if not matches:
raise ValueError('No menu items defined')
return ''.join([m[1] for m in matches])
class TestExtractKeysFromPrompt(unittest.TestCase):
def test_no_keys(self):
with self.assertRaises(ValueError):
extract_keys_from_prompt('no keys')
def test_with_keys(self):
self.assertEqual('ac', extract_keys_from_prompt('Type [a] to abort, [c] to continue'))
################################################################################
def prompt_menu(prompt):
choices = extract_keys_from_prompt(prompt).lower()
while True:
choice = input(prompt + ' > ').lower()
if len(choice) == 1 and (choice in choices):
return choice
################################################################################
def ensure_standard_repo_layout(tmp_dir, repo):
full_wc = None
while not repo.is_standard_layout():
print('The top level directories in the repository are not in standard layout:')
print(repo.list('/'))
if not full_wc:
print('Checking out the root of the repository to fix it...')
full_wc = WorkingCopy(tmp_dir, repo, '/')
print('The working copy is checked out at {}'.format(full_wc.abs_path))
print('Fix the layout in another terminal window and make sure to commit')
choice = prompt_menu('Type [c] to continue when done...')
if full_wc:
shutil.rmtree(full_wc.abs_path)
################################################################################
class AuthorsFile:
def __init__(self, tmp_dir, repo, domain):
wc = WorkingCopy(tmp_dir, repo, 'trunk')
authors = wc.get_authors()
shutil.rmtree(wc.abs_path)
self.abs_path = os.path.join(tmp_dir, 'authors.txt')
with open(self.abs_path, 'w') as f:
f.write(''.join([
'{author} = {author} <{author}@{domain}>\n'.format(author=a, domain=domain)
for a in authors
]))
def read(self):
return open(self.abs_path).read()
################################################################################
class GitRepo:
def __init__(self, tmp_dir):
self.abs_path = os.path.join(tmp_dir, 'git')
def branch(self, name, what):
subprocess.check_call(['git', 'branch', name, what], cwd=self.abs_path)
def delete_branch(self, branch, remote=False):
remote_arg = ['-r'] if remote else []
subprocess.check_call(['git', 'branch', '-D'] + remote_arg + [branch], cwd=self.abs_path)
def get_short_refs(self, refs=None):
cmdline = ['git', 'for-each-ref', '--format=%(refname:short)']
if refs:
cmdline.append(refs)
return subprocess.check_output(cmdline, cwd=self.abs_path, encoding='UTF-8').splitlines()
def tag(self, tag_name, what):
subprocess.check_call(['git', 'tag', tag_name, what], cwd=self.abs_path)
################################################################################
def generate_authors_file(tmp_dir, repo):
domain = input('Which domain to assign users to? > ')
authors_file = AuthorsFile(tmp_dir, repo, domain)
while True:
choice = prompt_menu('Generated authors list, [e]dit, [p]roceeed or [v]iew?')
if choice == 'e':
subprocess.check_call([os.environ['EDITOR'], authors_file.abs_path])
elif choice == 'p':
break
elif choice == 'v':
print(authors_file.read())
return authors_file
def convert_svn_to_git(authors_file, git, repo):
subprocess.check_call([
'git', 'svn', 'clone', repo.url, '--authors-file={}'.format(authors_file.abs_path),
'--no-metadata', '--prefix', '', '-s', git
])
def fix_tag_name(tag):
tag_prefix = 'tags/'
if tag.startswith(tag_prefix):
return tag[len(tag_prefix):]
else:
return tag
def main():
args = parse_arguments()
print('Using SVN repository dump {}'.format(args.repo_dump_gz))
tmp_dir = tempfile.mkdtemp()
print("Intermediate results will be stored in {}".format(tmp_dir))
repo = Repository(args.repo_dump_gz, tmp_dir)
print('Suggested repository name: {}'.format(repo.name))
print('Restoring the SVN repository from the dump...')
repo.restore_from_dump(args.repo_dump_gz)
ensure_standard_repo_layout(tmp_dir, repo)
authors_file = generate_authors_file(tmp_dir, repo)
git_repo = GitRepo(tmp_dir)
print('Converting to GIT repository at {}'.format(git_repo.abs_path))
convert_svn_to_git(authors_file, git_repo.abs_path, repo)
for tag in git_repo.get_short_refs('refs/remotes/tags'):
git_repo.tag(fix_tag_name(tag), tag)
git_repo.delete_branch(tag, remote=True)
for branch in git_repo.get_short_refs('refs/remotes'):
git_repo.branch(branch, 'refs/remotes/' + branch)
git_repo.delete_branch(branch, remote=True)
for branch in git_repo.get_short_refs():
if '@' in branch:
git_repo.delete_branch(branch, remote=True)
git_repo.delete_branch('trunk', remote=False)
print(tmp_dir)
# TODO: remove temporary directory
if __name__ == '__main__':
main()