-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-mirror
More file actions
executable file
·179 lines (128 loc) · 5.16 KB
/
run-mirror
File metadata and controls
executable file
·179 lines (128 loc) · 5.16 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
#!/usr/bin/env python
from optparse import OptionParser
import os
from subprocess import check_call
import sys
DEBUG = False
HERE = os.path.abspath(os.path.dirname(__file__))
def run(*popenargs, **kwargs):
cmd = popenargs[0]
if DEBUG:
if isinstance(cmd, list):
print "Running: %s" % " ".join(cmd)
else:
print "Running: %s" % cmd
return check_call(*popenargs, **kwargs)
def run_output(*popenargs, **kwargs):
cmd = popenargs[0]
if DEBUG:
if isinstance(cmd, list):
print "Running: %s" % " ".join(cmd)
else:
print "Running: %s" % cmd
return check_output(*popenargs, **kwargs)
def check_output(*popenargs, **kwargs): # From python2.6
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the
returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor.
Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it
is used internally.
To capture standard error in the
result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
from subprocess import Popen, PIPE, CalledProcessError
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd) #, output=output)
return output
def convert_to_git(src, dst):
print "Converting SVN repository %s to Git repository %s" % (
src, dst)
env = os.environ.copy()
env['GIT_DIR'] = os.path.join(dst, '.git')
git = lambda *args, **kwargs: run_output(
["git"] + args[0], *args[1:], env=env, **kwargs)
if os.path.exists(dst):
print "Fetching new revisions..."
git(["svn", "fetch"])
else:
print "Cloning from %s" % src
git(["svn", "clone", "--stdlayout", src, dst])
tags = [line.strip()
for line in check_output(["git", "tag"], env=env).split('\n')
if line.strip() != '']
branches = [line[2:].strip()
for line in check_output(["git", "branch"], env=env).split('\n')
if line.strip() != '']
print "Found %d tags and %d branches" % (len(tags), len(branches))
refs = [line[2:].strip()
for line in git(["branch", "-a"]).split('\n')
if line.strip() != '']
# Add tags
for tag_ref in (line for line in refs if line.startswith('remotes/tags')):
tag = tag_ref.split('/', 2)[2]
if tag not in tags:
# First, find the author of the commit we want to tag
# Take only the first line, since some lines seems to be broken
# sometimes !?
commiter = check_output(
["git", "show", "--pretty=format:%cN", tag_ref],
env=env).strip().split('\n')[0]
# Change the author of the next commit
git(["config", "user.name", commiter])
print "Tagging %r with %r" % (tag_ref, tag)
# Now, create the tag
git(["tag",
"-a", "-m", "Tag release: %s" % tag,
tag, tag_ref])
# And unset the configuration we changed before
git(["config", "--remove-section", "user"])
# Add branches
for branch_ref in (line for line in refs
if line.startswith('remotes/') and
# Don't create branch already pushed!
# 'origin' here is the repository where the mirror is
# mirrored to, not where it's mirrored *from*.
not line.startswith('remotes/origin/') and
not line.startswith('remotes/tags') and
not line.startswith('remotes/trunk')):
branch = branch_ref.split('/', 1)[1]
if branch not in branches:
print "Creating branch %r from %r" % (branch, branch_ref)
git(["branch", branch, branch_ref])
# Compress the Git data
print "Clean the repository"
git(["repack", "-Adfq"])
def run(argv=None):
global DEBUG # beuargh
if argv is None:
argv = sys.argv
parser = OptionParser(usage="%prog SVN_URL [DEST]")
parser.add_option('-d', '--debug', action='store_true', dest="debug",
help="Display commands run", default=False)
(options, args) = parser.parse_args(argv)
DEBUG = options.debug
if len(args) == 3:
(svn_url, dest) = args[1:]
else:
parser.error("Not enought argument")
convert_to_git(svn_url, dest)
if __name__ == '__main__':
run()