forked from astaric/legit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_to_git
executable file
·87 lines (76 loc) · 2.63 KB
/
import_to_git
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
#!/usr/bin/env python
import re, subprocess
def get_svn_log():
return subprocess.check_output(['svn', 'log']).split('\n')
def parse_svn_log_data(svn_log):
revisions = []
rev, author, timestamp, message = None, None, None, ''
for line in svn_log:
if line.startswith('----------------'):
if rev:
revisions.append((rev, author, timestamp, message.strip()))
rev, author, timestamp, message = None, None, None, ''
elif line.startswith('r') and rev == None:
tokens = [token.strip() for token in line.split('|')]
rev, author, timestamp, nlines = tokens
else:
message += line + '\n'
revisions.reverse()
return revisions
def svn_checkout(revision):
subprocess.call(['svn', 'update', '-r', revision])
def git_commit(message, author=None, date=None):
if message == "":
message = "No message."
git_call = ['git', 'commit', '-a', '-m', message]
if author:
if '<' not in author:
author = '%s <%s>' % (author, author)
git_call.extend(['--author', author])
if date:
git_call.extend(['--date', date])
subprocess.call(['git', 'add', '.'])
subprocess.call(git_call)
def git_tag(revision):
subprocess.call(['git', 'tag', revision])
def git_revision_tags():
tags = []
for tag in subprocess.check_output(['git', 'tag']).split('\n'):
tag = tag.strip()
if re.match(r'r\d+', tag):
tags.append(tag)
return tags
def get_latest_local_revision():
try:
i = 0
while True:
tags = subprocess.check_output(['git', 'tag', '--points-at', 'HEAD~%i' % i]).strip().split()
revisions = [int(tag[1:]) for tag in tags if tag.startswith('r') and tag[1:].isdigit()]
if revisions:
return 'r%d' % max(revisions)
i += 1
except:
pass
def add_changes_to_git(revisions):
for revision, author, date, message in revisions:
svn_checkout(revision)
git_commit(message, author, date)
git_tag(revision)
def git_fetch(revisions):
max_local_revision = get_latest_local_revision()
if max_local_revision:
rev_iter = iter(revisions)
for rev in rev_iter:
if rev[0] == max_local_revision:
break
remote_revisions = list(rev_iter)
else:
remote_revisions = revisions
if remote_revisions:
add_changes_to_git(remote_revisions)
else:
print "Already up to date."
if __name__ == '__main__':
svn_log = get_svn_log()
revision_data = parse_svn_log_data(svn_log)
git_fetch(revision_data)