svn: factor out the "applicable" helper
[yap.git] / plugins / svn.py
blobee390e287fe58fea1aa6e23d209eac702cc13d4d
2 from yap.yap import YapCore, YapError
3 from yap.util import get_output, takes_options, run_command, run_safely, short_help
5 import os
6 import tempfile
7 import glob
8 import pickle
10 class RepoBlob(object):
11 def __init__(self, url, fetch):
12 self.url = url
13 self.fetch = fetch
15 self.uuid = None
16 self.branches = None
17 self.tags = None
18 self.metadata = {}
20 def add_metadata(self, branch):
21 assert branch not in self.metadata
22 gitdir = get_output("git rev-parse --git-dir")
23 assert gitdir
24 revmap = os.path.join(gitdir[0], "svn", "svn", branch, ".rev_map*")
25 revmap = glob.glob(revmap)
26 if not revmap:
27 return
28 uuid = revmap[0].split('.')[-1]
29 if self.uuid is None:
30 self.uuid = uuid
31 assert self.uuid == uuid
32 data = file(revmap[0]).read()
33 self.metadata[branch] = data
35 class SvnPlugin(YapCore):
36 "Allow yap to interoperate with Subversion repositories"
37 def _get_root(self, url):
38 root = get_output("svn info %s 2>/dev/null | gawk '/Repository Root:/{print $3}'" % url)
39 if not root:
40 raise YapError("Not an SVN repo: %s" % url)
41 return root[0]
43 def _configure_repo(self, url, fetch=None):
44 root = self._get_root(url)
45 os.system("git config svn-remote.svn.url %s" % root)
46 if fetch is None:
47 trunk = url.replace(root, '').strip('/')
48 else:
49 trunk = fetch.split(':')[0]
50 os.system("git config svn-remote.svn.fetch %s:refs/remotes/svn/trunk"
51 % trunk)
53 branches = trunk.replace('trunk', 'branches')
54 if branches != trunk:
55 os.system("git config svn-remote.svn.branches %s/*:refs/remotes/svn/*" % branches)
56 tags = trunk.replace('trunk', 'tags')
57 if tags != trunk:
58 os.system("git config svn-remote.svn.tags %s/*:refs/tags/*" % tags)
59 self.cmd_repo("svn", url)
60 os.system("git config yap.svn.enabled 1")
62 def _create_tagged_blob(self):
63 url = get_output("git config svn-remote.svn.url")[0]
64 fetch = get_output("git config --get-all svn-remote.svn.fetch")
65 blob = RepoBlob(url, fetch)
66 for b in get_output("git for-each-ref --format='%(refname)' 'refs/remotes/svn/*'"):
67 b = b.replace('refs/remotes/svn/', '')
68 blob.add_metadata(b)
70 fd_w, fd_r = os.popen2("git hash-object -w --stdin")
71 import __builtin__
72 __builtin__.RepoBlob = RepoBlob
73 pickle.dump(blob, fd_w)
74 fd_w.close()
75 hash = fd_r.readline().strip()
76 run_safely("git tag -f yap-svn %s" % hash)
78 def _clone_svn(self, url, directory=None, **flags):
79 url = url.rstrip('/')
80 if directory is None:
81 directory = url.rsplit('/')[-1]
82 directory = directory.replace('.git', '')
84 try:
85 os.mkdir(directory)
86 except OSError:
87 raise YapError("Directory exists: %s" % directory)
88 os.chdir(directory)
90 self.cmd_init()
91 run_command("git config svn-remote.svn.noMetadata 1")
92 self._configure_repo(url)
93 os.system("git svn fetch -r %s:HEAD" % flags.get('-r', '1'))
94 self._create_tagged_blob()
96 def _push_svn(self, branch, **flags):
97 if '-d' in flags:
98 raise YapError("Deleting svn branches not supported")
99 print "Verifying branch is up-to-date"
100 run_safely("git svn fetch svn")
102 branch = branch.replace('refs/heads/', '')
103 rev = get_output("git rev-parse --verify refs/remotes/svn/%s" % branch)
105 # Create the branch if requested
106 if not rev:
107 if '-c' not in flags:
108 raise YapError("No matching branch on the repo. Use -c to create a new branch there.")
109 src = get_output("git svn info | gawk '/URL:/{print $2}'")[0]
110 brev = get_output("git svn info | gawk '/Revision:/{print $2}'")[0]
111 root = get_output("git config svn-remote.svn.url")[0]
112 branch_path = get_output("git config svn-remote.svn.branches")[0].split(':')[0]
113 branch_path = branch_path.rstrip('/*')
114 dst = '/'.join((root, branch_path, branch))
116 # Create the branch in svn
117 run_safely("svn cp -r%s %s %s -m 'create branch %s'"
118 % (brev, src, dst, branch))
119 run_safely("git svn fetch svn")
120 rev = get_output("git rev-parse refs/remotes/svn/%s 2>/dev/null" % branch)
121 base = get_output("git svn find-rev r%s" % brev)
123 # Apply our commits to the new branch
124 try:
125 fd, tmpfile = tempfile.mkstemp("yap")
126 os.close(fd)
127 print base[0]
128 os.system("git format-patch -k --stdout '%s' > %s"
129 % (base[0], tmpfile))
130 start = get_output("git rev-parse HEAD")
131 self.cmd_point("refs/remotes/svn/%s"
132 % branch, **{'-f': True})
134 stat = os.stat(tmpfile)
135 size = stat[6]
136 if size > 0:
137 rc = run_command("git am -3 %s" % tmpfile)
138 if (rc):
139 self.cmd_point(start[0], **{'-f': True})
140 raise YapError("Failed to port changes to new svn branch")
141 finally:
142 os.unlink(tmpfile)
144 base = get_output("git merge-base HEAD %s" % rev[0])
145 if base[0] != rev[0]:
146 raise YapError("Branch not up-to-date. Update first.")
147 current = get_output("git symbolic-ref HEAD")
148 if not current:
149 raise YapError("Not on a branch!")
150 current = current[0].replace('refs/heads/', '')
151 self._confirm_push(current, branch, "svn")
152 if run_command("git update-index --refresh"):
153 raise YapError("Can't push with uncommitted changes")
155 master = get_output("git rev-parse --verify refs/heads/master 2>/dev/null")
156 os.system("git svn dcommit")
157 run_safely("git svn rebase")
158 if not master:
159 master = get_output("git rev-parse --verify refs/heads/master 2>/dev/null")
160 if master:
161 run_safely("git update-ref -d refs/heads/master %s" % master[0])
163 def _enabled(self):
164 enabled = get_output("git config yap.svn.enabled")
165 return bool(enabled)
167 def _applicable(self, args):
168 if not self._enabled():
169 return False
171 if args and args[0] == 'svn':
172 return True
174 if not args:
175 current = get_output("git symbolic-ref HEAD")
176 if not current:
177 raise YapError("Not on a branch!")
179 current = current[0].replace('refs/heads/', '')
180 remote, merge = self._get_tracking(current)
181 if remote == "svn":
182 return True
184 return False
186 # Ensure users don't accidentally kill our "svn" repo
187 def cmd_repo(self, *args, **flags):
188 if self._enabled():
189 if '-d' in flags and args and args[0] == "svn":
190 raise YapError("Refusing to delete special svn repository")
191 super(SvnPlugin, self).cmd_repo(*args, **flags)
193 # Configure git-svn if we just cloned a yap-svn repo
194 def cmd_clone(self, *args, **flags):
195 super(SvnPlugin, self).cmd_clone(*args, **flags)
197 if self._enabled():
198 # nothing to do
199 return
201 run_safely("git fetch origin --tags")
202 hash = get_output("git rev-parse --verify refs/tags/yap-svn 2>/dev/null")
203 if not hash:
204 return
206 fd = os.popen("git cat-file blob %s" % hash[0])
207 import __builtin__
208 __builtin__.RepoBlob = RepoBlob
209 blob = pickle.load(fd)
210 self._configure_repo(blob.url, blob.fetch[0])
211 for extra in blob.fetch[1:]:
212 run_safely("git config svn-remote.svn.fetch %s" % extra)
214 for b in blob.metadata.keys():
215 branch = os.path.join(".git", "svn", "svn", b)
216 os.makedirs(branch)
217 fd = file(os.path.join(branch, ".rev_map.%s" % blob.uuid), "w")
218 fd.write(blob.metadata[b])
219 run_safely("git fetch origin 'refs/remotes/svn/*:refs/remotes/svn/*'")
221 @takes_options("r:")
222 def cmd_clone(self, *args, **flags):
223 if args and not run_command("svn info %s" % args[0]):
224 self._clone_svn(*args, **flags)
225 else:
226 super(SvnPlugin, self).cmd_clone(*args, **flags)
228 def cmd_fetch(self, *args, **flags):
229 if self._applicable(args):
230 os.system("git svn fetch svn")
231 self._create_tagged_blob()
232 return
234 super(SvnPlugin, self).cmd_fetch(*args, **flags)
236 def cmd_push(self, *args, **flags):
237 if self._applicable(args):
238 if len (args) >= 2:
239 merge = args[1]
240 else:
241 current = get_output("git symbolic-ref HEAD")
242 if not current:
243 raise YapError("Not on a branch!")
245 current = current[0].replace('refs/heads/', '')
246 remote, merge = self._get_tracking(current)
247 if remote != "svn":
248 raise YapError("Need a branch name")
249 self._push_svn(merge, **flags)
250 return
251 super(SvnPlugin, self).cmd_push(*args, **flags)
253 @short_help("change options for the svn plugin")
254 def cmd_svn(self, subcmd):
255 "enable"
257 if subcmd not in ["enable"]:
258 raise TypeError
260 if "svn" in [x[0] for x in self._list_remotes()]:
261 raise YapError("A remote named 'svn' already exists")
264 if not run_command("git config svn-remote.svn.branches"):
265 raise YapError("Cannot currently enable in a repository with svn branches")
267 url = get_output("git config svn-remote.svn.url")
268 if not url:
269 raise YapError("Not a git-svn repository?")
270 fetch = get_output("git config svn-remote.svn.fetch")
271 assert fetch
272 lhs, rhs = fetch[0].split(':')
275 rev = get_output("git rev-parse %s" % rhs)
276 assert rev
277 run_safely("git update-ref refs/remotes/svn/trunk %s" % rev[0])
279 url = '/'.join((url[0], lhs))
280 self._configure_repo(url)
281 run_safely("git update-ref -d %s %s" % (rhs, rev[0]))