hg.py
388 lines
| 13.9 KiB
| text/x-python
|
PythonLexer
Martin Geisler
|
r8250 | # hg.py - hg backend for convert extension | ||
# | ||||
# Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Brendan Cully
|
r4536 | |||
Bryan O'Sullivan
|
r5556 | # Notes for hg->hg conversion: | ||
# | ||||
# * Old versions of Mercurial didn't trim the whitespace from the ends | ||||
# of commit messages, but new versions do. Changesets created by | ||||
# those older versions, then converted, may thus have different | ||||
# hashes for changesets that are otherwise identical. | ||||
# | ||||
Patrick Mezard
|
r8596 | # * Using "--config convert.hg.saverev=true" will make the source | ||
# identifier to be stored in the converted revision. This will cause | ||||
# the converted revision to have a different identity than the | ||||
# source. | ||||
Bryan O'Sullivan
|
r5013 | |||
Patrick Mezard
|
r8693 | import os, time, cStringIO | ||
Bryan O'Sullivan
|
r5014 | from mercurial.i18n import _ | ||
Joel Rosdahl
|
r6211 | from mercurial.node import bin, hex, nullid | ||
Edouard Gomez
|
r13746 | from mercurial import hg, util, context, bookmarks, error | ||
Brendan Cully
|
r4536 | |||
Bryan O'Sullivan
|
r5013 | from common import NoRepo, commit, converter_source, converter_sink | ||
Brendan Cully
|
r4536 | |||
Bryan O'Sullivan
|
r5013 | class mercurial_sink(converter_sink): | ||
Brendan Cully
|
r4763 | def __init__(self, ui, path): | ||
Bryan O'Sullivan
|
r5440 | converter_sink.__init__(self, ui, path) | ||
Brendan Cully
|
r5173 | self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True) | ||
self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False) | ||||
Brendan Cully
|
r5260 | self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default') | ||
Brendan Cully
|
r5173 | self.lastbranch = None | ||
Bryan O'Sullivan
|
r5441 | if os.path.isdir(path) and len(os.listdir(path)) > 0: | ||
try: | ||||
self.repo = hg.repository(self.ui, path) | ||||
Patrick Mezard
|
r5918 | if not self.repo.local(): | ||
Martin Geisler
|
r10938 | raise NoRepo(_('%s is not a local Mercurial repository') | ||
% path) | ||||
Matt Mackall
|
r7637 | except error.RepoError, err: | ||
Matt Mackall
|
r8206 | ui.traceback() | ||
Bryan O'Sullivan
|
r5441 | raise NoRepo(err.args[0]) | ||
else: | ||||
try: | ||||
ui.status(_('initializing destination %s repository\n') % path) | ||||
self.repo = hg.repository(self.ui, path, create=True) | ||||
Patrick Mezard
|
r5918 | if not self.repo.local(): | ||
Martin Geisler
|
r10938 | raise NoRepo(_('%s is not a local Mercurial repository') | ||
% path) | ||||
Bryan O'Sullivan
|
r5441 | self.created.append(path) | ||
Peter Arrenbrecht
|
r7875 | except error.RepoError: | ||
Matt Mackall
|
r8206 | ui.traceback() | ||
Martin Geisler
|
r10939 | raise NoRepo(_("could not create hg repository %s as sink") | ||
Martin Geisler
|
r10938 | % path) | ||
Bryan O'Sullivan
|
r5014 | self.lock = None | ||
self.wlock = None | ||||
Alexis S. L. Carvalho
|
r5378 | self.filemapmode = False | ||
Bryan O'Sullivan
|
r5014 | |||
def before(self): | ||||
Martin Geisler
|
r9467 | self.ui.debug('run hg sink pre-conversion action\n') | ||
Alexis S. L. Carvalho
|
r5052 | self.wlock = self.repo.wlock() | ||
Bryan O'Sullivan
|
r5014 | self.lock = self.repo.lock() | ||
def after(self): | ||||
Martin Geisler
|
r9467 | self.ui.debug('run hg sink post-conversion action\n') | ||
Matt Mackall
|
r10086 | if self.lock: | ||
self.lock.release() | ||||
if self.wlock: | ||||
self.wlock.release() | ||||
Brendan Cully
|
r4536 | |||
Bryan O'Sullivan
|
r5011 | def revmapfile(self): | ||
Brendan Cully
|
r4536 | return os.path.join(self.path, ".hg", "shamap") | ||
Edouard Gomez
|
r4589 | def authorfile(self): | ||
return os.path.join(self.path, ".hg", "authormap") | ||||
Brendan Cully
|
r4536 | def getheads(self): | ||
h = self.repo.changelog.heads() | ||||
Matt Mackall
|
r10282 | return [hex(x) for x in h] | ||
Brendan Cully
|
r4536 | |||
Patrick Mezard
|
r5934 | def setbranch(self, branch, pbranches): | ||
if not self.clonebranches: | ||||
Brendan Cully
|
r5173 | return | ||
Patrick Mezard
|
r5934 | setbranch = (branch != self.lastbranch) | ||
Brendan Cully
|
r5173 | self.lastbranch = branch | ||
if not branch: | ||||
branch = 'default' | ||||
Patrick Mezard
|
r5934 | pbranches = [(b[0], b[1] and b[1] or 'default') for b in pbranches] | ||
pbranch = pbranches and pbranches[0][1] or 'default' | ||||
Brendan Cully
|
r5173 | |||
branchpath = os.path.join(self.path, branch) | ||||
Patrick Mezard
|
r5934 | if setbranch: | ||
self.after() | ||||
try: | ||||
self.repo = hg.repository(self.ui, branchpath) | ||||
except: | ||||
Brendan Cully
|
r5173 | self.repo = hg.repository(self.ui, branchpath, create=True) | ||
Patrick Mezard
|
r5934 | self.before() | ||
# pbranches may bring revisions from other branches (merge parents) | ||||
# Make sure we have them, or pull them. | ||||
missings = {} | ||||
for b in pbranches: | ||||
try: | ||||
self.repo.lookup(b[0]) | ||||
except: | ||||
missings.setdefault(b[1], []).append(b[0]) | ||||
Thomas Arendsen Hein
|
r6210 | |||
Patrick Mezard
|
r5934 | if missings: | ||
self.after() | ||||
for pbranch, heads in missings.iteritems(): | ||||
pbranchpath = os.path.join(self.path, pbranch) | ||||
prepo = hg.repository(self.ui, pbranchpath) | ||||
self.ui.note(_('pulling from %s into %s\n') % (pbranch, branch)) | ||||
self.repo.pull(prepo, [prepo.lookup(h) for h in heads]) | ||||
self.before() | ||||
Brendan Cully
|
r5173 | |||
Patrick Mezard
|
r8693 | def _rewritetags(self, source, revmap, data): | ||
fp = cStringIO.StringIO() | ||||
for line in data.splitlines(): | ||||
s = line.split(' ', 1) | ||||
if len(s) != 2: | ||||
continue | ||||
revid = revmap.get(source.lookuprev(s[0])) | ||||
if not revid: | ||||
continue | ||||
fp.write('%s %s\n' % (revid, s[1])) | ||||
return fp.getvalue() | ||||
def putcommit(self, files, copies, parents, commit, source, revmap): | ||||
Patrick Mezard
|
r6716 | |||
Patrick Mezard
|
r6717 | files = dict(files) | ||
def getfilectx(repo, memctx, f): | ||||
v = files[f] | ||||
Patrick Mezard
|
r11134 | data, mode = source.getfile(f, v) | ||
Patrick Mezard
|
r8693 | if f == '.hgtags': | ||
data = self._rewritetags(source, revmap, data) | ||||
Patrick Mezard
|
r11134 | return context.memfilectx(f, data, 'l' in mode, 'x' in mode, | ||
copies.get(f)) | ||||
Patrick Mezard
|
r6717 | |||
Brendan Cully
|
r4536 | pl = [] | ||
for p in parents: | ||||
Patrick Mezard
|
r6717 | if p not in pl: | ||
Brendan Cully
|
r4536 | pl.append(p) | ||
parents = pl | ||||
Alexis S. L. Carvalho
|
r5378 | nparents = len(parents) | ||
if self.filemapmode and nparents == 1: | ||||
m1node = self.repo.changelog.read(bin(parents[0]))[0] | ||||
parent = parents[0] | ||||
Brendan Cully
|
r4536 | |||
Matt Mackall
|
r10282 | if len(parents) < 2: | ||
parents.append(nullid) | ||||
if len(parents) < 2: | ||||
parents.append(nullid) | ||||
Brendan Cully
|
r4536 | p2 = parents.pop(0) | ||
text = commit.desc | ||||
Bryan O'Sullivan
|
r5439 | extra = commit.extra.copy() | ||
Bryan O'Sullivan
|
r5038 | if self.branchnames and commit.branch: | ||
Brendan Cully
|
r4873 | extra['branch'] = commit.branch | ||
if commit.rev: | ||||
extra['convert_revision'] = commit.rev | ||||
Thomas Arendsen Hein
|
r4957 | |||
Brendan Cully
|
r4536 | while parents: | ||
p1 = p2 | ||||
p2 = parents.pop(0) | ||||
Matt Mackall
|
r10282 | ctx = context.memctx(self.repo, (p1, p2), text, files.keys(), | ||
getfilectx, commit.author, commit.date, extra) | ||||
Peter Arrenbrecht
|
r7874 | self.repo.commitctx(ctx) | ||
Brendan Cully
|
r4536 | text = "(octopus merge fixup)\n" | ||
Joel Rosdahl
|
r6217 | p2 = hex(self.repo.changelog.tip()) | ||
Brendan Cully
|
r4536 | |||
Alexis S. L. Carvalho
|
r5378 | if self.filemapmode and nparents == 1: | ||
man = self.repo.manifest | ||||
mnode = self.repo.changelog.read(bin(p2))[0] | ||||
Matt Mackall
|
r11673 | closed = 'close' in commit.extra | ||
if not closed and not man.cmp(m1node, man.revision(mnode)): | ||||
Patrick Mezard
|
r8611 | self.ui.status(_("filtering out empty revision\n")) | ||
Alexis S. L. Carvalho
|
r5378 | self.repo.rollback() | ||
return parent | ||||
Brendan Cully
|
r4536 | return p2 | ||
def puttags(self, tags): | ||||
Peter Arrenbrecht
|
r7877 | try: | ||
parentctx = self.repo[self.tagsbranch] | ||||
tagparent = parentctx.node() | ||||
except error.RepoError: | ||||
parentctx = None | ||||
tagparent = nullid | ||||
Brendan Cully
|
r4536 | |||
Peter Arrenbrecht
|
r7877 | try: | ||
Nicolas Dumazet
|
r9136 | oldlines = sorted(parentctx['.hgtags'].data().splitlines(True)) | ||
Peter Arrenbrecht
|
r7877 | except: | ||
oldlines = [] | ||||
Brendan Cully
|
r4536 | |||
Matt Mackall
|
r8209 | newlines = sorted([("%s %s\n" % (tags[tag], tag)) for tag in tags]) | ||
Peter Arrenbrecht
|
r7877 | if newlines == oldlines: | ||
Patrick Mezard
|
r9431 | return None, None | ||
Peter Arrenbrecht
|
r7877 | data = "".join(newlines) | ||
def getfilectx(repo, memctx, f): | ||||
Patrick Mezard
|
r6717 | return context.memfilectx(f, data, False, False, None) | ||
Peter Arrenbrecht
|
r7877 | self.ui.status(_("updating tags\n")) | ||
date = "%s 0" % int(time.mktime(time.gmtime())) | ||||
extra = {'branch': self.tagsbranch} | ||||
ctx = context.memctx(self.repo, (tagparent, None), "update tags", | ||||
[".hgtags"], getfilectx, "convert-repo", date, | ||||
extra) | ||||
self.repo.commitctx(ctx) | ||||
Patrick Mezard
|
r9431 | return hex(self.repo.changelog.tip()), hex(tagparent) | ||
Bryan O'Sullivan
|
r5013 | |||
Alexis S. L. Carvalho
|
r5378 | def setfilemapmode(self, active): | ||
self.filemapmode = active | ||||
Edouard Gomez
|
r13746 | def putbookmarks(self, updatedbookmark): | ||
if not len(updatedbookmark): | ||||
return | ||||
self.ui.status(_("updating bookmarks\n")) | ||||
for bookmark in updatedbookmark: | ||||
self.repo._bookmarks[bookmark] = bin(updatedbookmark[bookmark]) | ||||
bookmarks.write(self.repo) | ||||
Bryan O'Sullivan
|
r5013 | class mercurial_source(converter_source): | ||
def __init__(self, ui, path, rev=None): | ||||
converter_source.__init__(self, ui, path, rev) | ||||
Patrick Mezard
|
r7231 | self.ignoreerrors = ui.configbool('convert', 'hg.ignoreerrors', False) | ||
Benoit Boissinot
|
r8456 | self.ignored = set() | ||
Matt Mackall
|
r7815 | self.saverev = ui.configbool('convert', 'hg.saverev', False) | ||
Bryan O'Sullivan
|
r5358 | try: | ||
self.repo = hg.repository(self.ui, path) | ||||
Bryan O'Sullivan
|
r5437 | # try to provoke an exception if this isn't really a hg | ||
# repo, but some other bogus compatible-looking url | ||||
Alexis S. L. Carvalho
|
r5522 | if not self.repo.local(): | ||
Matt Mackall
|
r7637 | raise error.RepoError() | ||
except error.RepoError: | ||||
Matt Mackall
|
r8206 | ui.traceback() | ||
Martin Geisler
|
r10939 | raise NoRepo(_("%s is not a local Mercurial repository") % path) | ||
Bryan O'Sullivan
|
r5013 | self.lastrev = None | ||
self.lastctx = None | ||||
Alexis S. L. Carvalho
|
r5379 | self._changescache = None | ||
Bryan O'Sullivan
|
r5554 | self.convertfp = None | ||
Patrick Mezard
|
r6885 | # Restrict converted revisions to startrev descendants | ||
startnode = ui.config('convert', 'hg.startrev') | ||||
if startnode is not None: | ||||
try: | ||||
startnode = self.repo.lookup(startnode) | ||||
Matt Mackall
|
r7637 | except error.RepoError: | ||
Dirkjan Ochtman
|
r6923 | raise util.Abort(_('%s is not a valid start revision') | ||
Patrick Mezard
|
r6885 | % startnode) | ||
startrev = self.repo.changelog.rev(startnode) | ||||
children = {startnode: 1} | ||||
for rev in self.repo.changelog.descendants(startrev): | ||||
children[self.repo.changelog.node(rev)] = 1 | ||||
self.keep = children.__contains__ | ||||
else: | ||||
self.keep = util.always | ||||
Bryan O'Sullivan
|
r5013 | |||
def changectx(self, rev): | ||||
if self.lastrev != rev: | ||||
Matt Mackall
|
r6747 | self.lastctx = self.repo[rev] | ||
Bryan O'Sullivan
|
r5013 | self.lastrev = rev | ||
return self.lastctx | ||||
Patrick Mezard
|
r6885 | def parents(self, ctx): | ||
Patrick Mezard
|
r9531 | return [p for p in ctx.parents() if p and self.keep(p.node())] | ||
Patrick Mezard
|
r6885 | |||
Bryan O'Sullivan
|
r5013 | def getheads(self): | ||
Bryan O'Sullivan
|
r5131 | if self.rev: | ||
Patrick Mezard
|
r6885 | heads = [self.repo[self.rev].node()] | ||
Bryan O'Sullivan
|
r5131 | else: | ||
Patrick Mezard
|
r6885 | heads = self.repo.heads() | ||
return [hex(h) for h in heads if self.keep(h)] | ||||
Bryan O'Sullivan
|
r5013 | |||
def getfile(self, name, rev): | ||||
try: | ||||
Patrick Mezard
|
r11134 | fctx = self.changectx(rev)[name] | ||
return fctx.data(), fctx.flags() | ||||
Matt Mackall
|
r7633 | except error.LookupError, err: | ||
Bryan O'Sullivan
|
r5013 | raise IOError(err) | ||
def getchanges(self, rev): | ||||
ctx = self.changectx(rev) | ||||
Patrick Mezard
|
r6885 | parents = self.parents(ctx) | ||
if not parents: | ||||
Matt Mackall
|
r8209 | files = sorted(ctx.manifest()) | ||
Mads Kiilerich
|
r14151 | # getcopies() is not needed for roots, but it is a simple way to | ||
# detect missing revlogs and abort on errors or populate self.ignored | ||||
self.getcopies(ctx, parents, files) | ||||
Patrick Mezard
|
r7231 | return [(f, rev) for f in files if f not in self.ignored], {} | ||
Alexis S. L. Carvalho
|
r5379 | if self._changescache and self._changescache[0] == rev: | ||
m, a, r = self._changescache[1] | ||||
else: | ||||
Patrick Mezard
|
r9531 | m, a, r = self.repo.status(parents[0].node(), ctx.node())[:3] | ||
Patrick Mezard
|
r7231 | # getcopies() detects missing revlogs early, run it before | ||
# filtering the changes. | ||||
Patrick Mezard
|
r9532 | copies = self.getcopies(ctx, parents, m + a) | ||
Thomas Arendsen Hein
|
r7236 | changes = [(name, rev) for name in m + a + r | ||
Patrick Mezard
|
r7231 | if name not in self.ignored] | ||
Matt Mackall
|
r8209 | return sorted(changes), copies | ||
Bryan O'Sullivan
|
r5013 | |||
Patrick Mezard
|
r9532 | def getcopies(self, ctx, parents, files): | ||
Bryan O'Sullivan
|
r5013 | copies = {} | ||
Alexis S. L. Carvalho
|
r5280 | for name in files: | ||
Patrick Mezard
|
r7231 | if name in self.ignored: | ||
continue | ||||
Bryan O'Sullivan
|
r5013 | try: | ||
Patrick Mezard
|
r7231 | copysource, copynode = ctx.filectx(name).renamed() | ||
if copysource in self.ignored or not self.keep(copynode): | ||||
continue | ||||
Patrick Mezard
|
r9532 | # Ignore copy sources not in parent revisions | ||
found = False | ||||
for p in parents: | ||||
if copysource in p: | ||||
found = True | ||||
break | ||||
if not found: | ||||
continue | ||||
Patrick Mezard
|
r7231 | copies[name] = copysource | ||
Bryan O'Sullivan
|
r5013 | except TypeError: | ||
pass | ||||
Matt Mackall
|
r7633 | except error.LookupError, e: | ||
Patrick Mezard
|
r7231 | if not self.ignoreerrors: | ||
raise | ||||
Benoit Boissinot
|
r8456 | self.ignored.add(name) | ||
Patrick Mezard
|
r7231 | self.ui.warn(_('ignoring: %s\n') % e) | ||
Bryan O'Sullivan
|
r5013 | return copies | ||
Thomas Arendsen Hein
|
r5143 | |||
Bryan O'Sullivan
|
r5013 | def getcommit(self, rev): | ||
ctx = self.changectx(rev) | ||||
Patrick Mezard
|
r9531 | parents = [p.hex() for p in self.parents(ctx)] | ||
Bryan O'Sullivan
|
r5556 | if self.saverev: | ||
crev = rev | ||||
else: | ||||
crev = None | ||||
Bryan O'Sullivan
|
r5013 | return commit(author=ctx.user(), date=util.datestr(ctx.date()), | ||
Bryan O'Sullivan
|
r5556 | desc=ctx.description(), rev=crev, parents=parents, | ||
Patrick Mezard
|
r8690 | branch=ctx.branch(), extra=ctx.extra(), | ||
sortkey=ctx.rev()) | ||||
Bryan O'Sullivan
|
r5013 | |||
def gettags(self): | ||||
tags = [t for t in self.repo.tagslist() if t[0] != 'tip'] | ||||
Patrick Mezard
|
r6885 | return dict([(name, hex(node)) for name, node in tags | ||
if self.keep(node)]) | ||||
Alexis S. L. Carvalho
|
r5379 | |||
def getchangedfiles(self, rev, i): | ||||
ctx = self.changectx(rev) | ||||
Patrick Mezard
|
r6885 | parents = self.parents(ctx) | ||
if not parents and i is None: | ||||
i = 0 | ||||
changes = [], ctx.manifest().keys(), [] | ||||
else: | ||||
i = i or 0 | ||||
Patrick Mezard
|
r9531 | changes = self.repo.status(parents[i].node(), ctx.node())[:3] | ||
Patrick Mezard
|
r7231 | changes = [[f for f in l if f not in self.ignored] for l in changes] | ||
Alexis S. L. Carvalho
|
r5379 | |||
if i == 0: | ||||
self._changescache = (rev, changes) | ||||
return changes[0] + changes[1] + changes[2] | ||||
Bryan O'Sullivan
|
r5554 | def converted(self, rev, destrev): | ||
if self.convertfp is None: | ||||
self.convertfp = open(os.path.join(self.path, '.hg', 'shamap'), | ||||
'a') | ||||
self.convertfp.write('%s %s\n' % (destrev, rev)) | ||||
self.convertfp.flush() | ||||
Patrick Mezard
|
r5805 | |||
def before(self): | ||||
Martin Geisler
|
r9467 | self.ui.debug('run hg source pre-conversion action\n') | ||
Patrick Mezard
|
r5805 | |||
def after(self): | ||||
Martin Geisler
|
r9467 | self.ui.debug('run hg source post-conversion action\n') | ||
Patrick Mezard
|
r8691 | |||
def hasnativeorder(self): | ||||
return True | ||||
Patrick Mezard
|
r8693 | |||
def lookuprev(self, rev): | ||||
try: | ||||
return hex(self.repo.lookup(rev)) | ||||
except error.RepoError: | ||||
return None | ||||
Edouard Gomez
|
r13757 | |||
def getbookmarks(self): | ||||
return bookmarks.listbookmarks(self.repo) | ||||