comparison svncommands.py @ 246:074f27c68818

Move rebuildmeta into svncommands.
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Wed, 08 Apr 2009 18:49:44 +0200
parents 28d0ee605308
children 1272e87546ed
comparison
equal deleted inserted replaced
245:f8e9b74df403 246:074f27c68818
1 import os 1 import os
2 import cPickle as pickle
2 3
3 from mercurial import hg 4 from mercurial import hg
4 from mercurial import node 5 from mercurial import node
5 from mercurial import patch 6 from mercurial import patch
6 from mercurial import util as hgutil 7 from mercurial import util as hgutil
209 'unified': True, 210 'unified': True,
210 'text': False, 211 'text': False,
211 })) 212 }))
212 ui.write(cmdutil.filterdiff(''.join(it), base_rev)) 213 ui.write(cmdutil.filterdiff(''.join(it), base_rev))
213 diff = util.register_subcommand('diff')(diff) 214 diff = util.register_subcommand('diff')(diff)
215
216
217 def rebuildmeta(ui, repo, hg_repo_path, args, **opts):
218 """rebuild hgsubversion metadata using values stored in revisions
219 """
220 if len(args) != 1:
221 raise hgutil.Abort('You must pass the svn URI used to create this repo.')
222 uuid = None
223 url = args[0].rstrip('/')
224 user = opts.get('username', hgutil.getuser())
225 passwd = opts.get('password', '')
226 svn = svnwrap.SubversionRepo(url, user, passwd)
227 subdir = svn.subdir
228 svnmetadir = os.path.join(repo.path, 'svn')
229 if not os.path.exists(svnmetadir):
230 os.makedirs(svnmetadir)
231
232 revmap = open(os.path.join(svnmetadir, 'rev_map'), 'w')
233 revmap.write('1\n')
234 last_rev = -1
235 branchinfo = {}
236 noderevnums = {}
237 for rev in repo:
238 ctx = repo[rev]
239 convinfo = ctx.extra().get('convert_revision', None)
240 if convinfo:
241 assert convinfo.startswith('svn:')
242 revpath, revision = convinfo[40:].split('@')
243 if subdir and subdir[0] != '/':
244 subdir = '/' + subdir
245 if subdir and subdir[-1] == '/':
246 subdir = subdir[:-1]
247 assert revpath.startswith(subdir), ('That does not look like the '
248 'right location in the repo.')
249 if uuid is None:
250 uuid = convinfo[4:40]
251 assert uuid == svn.uuid, 'UUIDs did not match!'
252 urlfile = open(os.path.join(svnmetadir, 'url'), 'w')
253 urlfile.write(url)
254 urlfile.close()
255 uuidfile = open(os.path.join(svnmetadir, 'uuid'), 'w')
256 uuidfile.write(uuid)
257 uuidfile.close()
258 commitpath = revpath[len(subdir)+1:]
259 if commitpath.startswith('branches'):
260 commitpath = commitpath[len('branches/'):]
261 elif commitpath == 'trunk':
262 commitpath = ''
263 else:
264 assert False, 'Unhandled case in rebuildmeta'
265 revmap.write('%s %s %s\n' % (revision,
266 node.hex(ctx.node()),
267 commitpath))
268 revision = int(revision)
269 noderevnums[ctx.node()] = revision
270 if revision > last_rev:
271 last_rev = revision
272 branch = ctx.branch()
273 if branch == 'default':
274 branch = None
275 if branch not in branchinfo:
276 parent = ctx.parents()[0]
277 if (parent.node() in noderevnums
278 and parent.branch() != ctx.branch()):
279 parentbranch = parent.branch()
280 if parentbranch == 'default':
281 parentbranch = None
282 else:
283 parentbranch = None
284 branchinfo[branch] = (parentbranch,
285 noderevnums.get(parent.node(), 0),
286 revision)
287 for c in ctx.children():
288 if c.branch() == 'closed-branches':
289 if branch in branchinfo:
290 del branchinfo[branch]
291 branchinfofile = open(os.path.join(svnmetadir, 'branch_info'), 'w')
292 pickle.dump(branchinfo, branchinfofile)
293 branchinfofile.close()
294
295 # now handle tags
296 tagsinfo = {}
297 realtags = svn.tags
298 tagsleft = realtags.items()
299 while tagsleft:
300 tag, tagparent = tagsleft.pop(0)
301 source, rev = tagparent
302 if source.startswith('tags/'):
303 src = source[len('tags/'):]
304 if src in tagsinfo:
305 tagsinfo[tag] = tagsinfo[src]
306 elif src in realtags:
307 if (realtags[src][1] <= last_rev
308 or realtags[src][0].startswith('tags/')):
309 tagsleft.append(src)
310 else:
311 older_tags = svn.tags_at_rev(rev)
312 newsrc, newrev = older_tags[src]
313 tagsleft.append((tag, (newsrc, newrev)))
314 continue
315 else:
316 # determine the branch
317 assert not source.startswith('tags/'), "Tags can't be tags of other tags."
318 if source.startswith('branches/'):
319 source = source[len('branches/'):]
320 elif source == 'trunk':
321 source = None
322 else:
323 source = '../' + source
324 if rev <= last_rev and (source or 'default') in repo.branchtags():
325 tagsinfo[tag] = source, rev
326
327 tagsinfofile = open(os.path.join(svnmetadir, 'tag_info'), 'w')
328 pickle.dump(tagsinfo, tagsinfofile)
329 tagsinfofile.close()
330 rebuildmeta = util.register_subcommand('rebuildmeta')(rebuildmeta)
331 rebuildmeta = util.command_needs_no_url(rebuildmeta)