Mercurial > hg
view hgext/convert/bzr.py @ 20182:04036798ebed
branches: avoid unnecessary changectx.branch() calls
This requires reading from the changelog, which can be costly over NFS.
Note that this does not totally remove reading from the changelog; we
still do that when calling changectx.closesbranch(). That call will be
removed in a later patch.
Running hg branches on the PyPy repo (with 996) over a busy NFS server,
before this change:
$ time hg --profile branches > /dev/null
CallCount Recursive Total(s) Inline(s) module:lineno(function)
2042 0 2.2827 2.2827 <open>
2036 0 0.9840 0.9840 <method 'close' of 'file' objects>
2036 0 0.0464 0.0464 <method 'read' of 'file' objects>
5233 0 0.1985 0.0453 mercurial.repoview:161(changelog)
10462 0 0.0791 0.0314 mercurial.changelog:133(tip)
5233 0 0.0388 0.0176 mercurial.localrepo:26(__get__)
10462 0 0.0250 0.0126 <len>
5233 0 0.0059 0.0039 mercurial.repoview:112(filterrevs)
10462 0 0.0029 0.0029 <hash>
2034 0 0.0444 0.0444 <method 'seek' of 'file' objects>
5340 0 0.0390 0.0390 mercurial.revlog:296(rev)
2582 0 0.0371 0.0371 <zlib.decompress>
3155 0 0.1963 0.0366 mercurial.context:202(__init__)
3155 0 0.1238 0.0306 mercurial.repoview:161(changelog)
3155 0 0.0261 0.0080 mercurial.changelog:183(rev)
9465 0 0.0061 0.0061 <isinstance>
1096 0 0.0023 0.0023 <binascii.unhexlify>
4251 0 0.0014 0.0014 <len>
2059 0 3.7341 0.0332 mercurial.changelog:270(read)
2059 0 3.6304 0.0307 mercurial.revlog:907(revision)
2057 0 0.0262 0.0137 mercurial.changelog:28(decodeextra)
4118 0 0.0094 0.0094 <method 'split' of 'str' objects>
4118 0 0.0270 0.0048 mercurial.encoding:61(tolocal)
2059 0 0.0040 0.0040 <method 'index' of 'str' objects>
10462 0 0.0791 0.0314 mercurial.changelog:133(tip)
10462 0 0.0289 0.0207 mercurial.changelog:190(node)
10462 0 0.0188 0.0091 <len>
52433 20932 0.0478 0.0310 <len>
20932 0 0.0221 0.0168 mercurial.revlog:262(__len__)
2059 0 3.6304 0.0307 mercurial.revlog:907(revision)
real 0m4.361s
user 0m0.986s
sys 0m0.237s
After this change:
$ time hg --profile branches > /dev/null
CallCount Recursive Total(s) Inline(s) module:lineno(function)
1069 0 1.1098 1.1098 <open>
1063 0 0.4865 0.4865 <method 'close' of 'file' objects>
4122 0 0.1811 0.0404 mercurial.repoview:161(changelog)
8240 0 0.0712 0.0272 mercurial.changelog:133(tip)
4122 0 0.0378 0.0177 mercurial.localrepo:26(__get__)
8240 0 0.0221 0.0115 <len>
4122 0 0.0057 0.0033 mercurial.repoview:112(filterrevs)
8240 0 0.0025 0.0025 <hash>
3029 0 0.1979 0.0371 mercurial.context:202(__init__)
3029 0 0.1278 0.0310 mercurial.repoview:161(changelog)
3029 0 0.0230 0.0081 mercurial.changelog:183(rev)
9087 0 0.0061 0.0061 <isinstance>
1096 0 0.0026 0.0026 <binascii.unhexlify>
4125 0 0.0014 0.0014 <len>
4229 0 0.0337 0.0337 mercurial.revlog:296(rev)
1061 0 0.0296 0.0296 <method 'seek' of 'file' objects>
1063 0 0.0292 0.0292 <method 'read' of 'file' objects>
8240 0 0.0712 0.0272 mercurial.changelog:133(tip)
8240 0 0.0271 0.0196 mercurial.changelog:190(node)
8240 0 0.0169 0.0083 <len>
40476 16488 0.0422 0.0271 <len>
16488 0 0.0193 0.0152 mercurial.revlog:262(__len__)
1342 0 0.0241 0.0241 <zlib.decompress>
9445 0 0.0336 0.0224 mercurial.changelog:190(node)
9445 0 0.0112 0.0112 mercurial.revlog:317(node)
1074 0 1.9102 0.0224 mercurial.changelog:270(read)
1074 0 1.8397 0.0202 mercurial.revlog:907(revision)
1073 0 0.0187 0.0099 mercurial.changelog:28(decodeextra)
2148 0 0.0061 0.0061 <method 'split' of 'str' objects>
2148 0 0.0184 0.0034 mercurial.encoding:61(tolocal)
real 0m2.402s
user 0m0.735s
sys 0m0.177s
author | Brodie Rao <brodie@sf.io> |
---|---|
date | Fri, 15 Nov 2013 23:18:08 -0500 |
parents | e7cfe3587ea4 |
children | 650b5b6e75ed |
line wrap: on
line source
# bzr.py - bzr support for the convert extension # # Copyright 2008, 2009 Marek Kubica <marek@xivilization.net> and others # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. # This module is for handling 'bzr', that was formerly known as Bazaar-NG; # it cannot access 'bar' repositories, but they were never used very much import os from mercurial import demandimport # these do not work with demandimport, blacklist demandimport.ignore.extend([ 'bzrlib.transactions', 'bzrlib.urlutils', 'ElementPath', ]) from mercurial.i18n import _ from mercurial import util from common import NoRepo, commit, converter_source try: # bazaar imports from bzrlib import bzrdir, revision, errors from bzrlib.revisionspec import RevisionSpec except ImportError: pass supportedkinds = ('file', 'symlink') class bzr_source(converter_source): """Reads Bazaar repositories by using the Bazaar Python libraries""" def __init__(self, ui, path, rev=None): super(bzr_source, self).__init__(ui, path, rev=rev) if not os.path.exists(os.path.join(path, '.bzr')): raise NoRepo(_('%s does not look like a Bazaar repository') % path) try: # access bzrlib stuff bzrdir except NameError: raise NoRepo(_('Bazaar modules could not be loaded')) path = os.path.abspath(path) self._checkrepotype(path) try: self.sourcerepo = bzrdir.BzrDir.open(path).open_repository() except errors.NoRepositoryPresent: raise NoRepo(_('%s does not look like a Bazaar repository') % path) self._parentids = {} def _checkrepotype(self, path): # Lightweight checkouts detection is informational but probably # fragile at API level. It should not terminate the conversion. try: from bzrlib import bzrdir dir = bzrdir.BzrDir.open_containing(path)[0] try: tree = dir.open_workingtree(recommend_upgrade=False) branch = tree.branch except (errors.NoWorkingTree, errors.NotLocalUrl): tree = None branch = dir.open_branch() if (tree is not None and tree.bzrdir.root_transport.base != branch.bzrdir.root_transport.base): self.ui.warn(_('warning: lightweight checkouts may cause ' 'conversion failures, try with a regular ' 'branch instead.\n')) except Exception: self.ui.note(_('bzr source type could not be determined\n')) def before(self): """Before the conversion begins, acquire a read lock for all the operations that might need it. Fortunately read locks don't block other reads or writes to the repository, so this shouldn't have any impact on the usage of the source repository. The alternative would be locking on every operation that needs locks (there are currently two: getting the file and getting the parent map) and releasing immediately after, but this approach can take even 40% longer.""" self.sourcerepo.lock_read() def after(self): self.sourcerepo.unlock() def _bzrbranches(self): return self.sourcerepo.find_branches(using=True) def getheads(self): if not self.rev: # Set using=True to avoid nested repositories (see issue3254) heads = sorted([b.last_revision() for b in self._bzrbranches()]) else: revid = None for branch in self._bzrbranches(): try: r = RevisionSpec.from_string(self.rev) info = r.in_history(branch) except errors.BzrError: pass revid = info.rev_id if revid is None: raise util.Abort(_('%s is not a valid revision') % self.rev) heads = [revid] # Empty repositories return 'null:', which cannot be retrieved heads = [h for h in heads if h != 'null:'] return heads def getfile(self, name, rev): revtree = self.sourcerepo.revision_tree(rev) fileid = revtree.path2id(name.decode(self.encoding or 'utf-8')) kind = None if fileid is not None: kind = revtree.kind(fileid) if kind not in supportedkinds: # the file is not available anymore - was deleted raise IOError(_('%s is not available in %s anymore') % (name, rev)) mode = self._modecache[(name, rev)] if kind == 'symlink': target = revtree.get_symlink_target(fileid) if target is None: raise util.Abort(_('%s.%s symlink has no target') % (name, rev)) return target, mode else: sio = revtree.get_file(fileid) return sio.read(), mode def getchanges(self, version): # set up caches: modecache and revtree self._modecache = {} self._revtree = self.sourcerepo.revision_tree(version) # get the parentids from the cache parentids = self._parentids.pop(version) # only diff against first parent id prevtree = self.sourcerepo.revision_tree(parentids[0]) return self._gettreechanges(self._revtree, prevtree) def getcommit(self, version): rev = self.sourcerepo.get_revision(version) # populate parent id cache if not rev.parent_ids: parents = [] self._parentids[version] = (revision.NULL_REVISION,) else: parents = self._filterghosts(rev.parent_ids) self._parentids[version] = parents branch = self.recode(rev.properties.get('branch-nick', u'default')) if branch == 'trunk': branch = 'default' return commit(parents=parents, date='%d %d' % (rev.timestamp, -rev.timezone), author=self.recode(rev.committer), desc=self.recode(rev.message), branch=branch, rev=version) def gettags(self): bytetags = {} for branch in self._bzrbranches(): if not branch.supports_tags(): return {} tagdict = branch.tags.get_tag_dict() for name, rev in tagdict.iteritems(): bytetags[self.recode(name)] = rev return bytetags def getchangedfiles(self, rev, i): self._modecache = {} curtree = self.sourcerepo.revision_tree(rev) if i is not None: parentid = self._parentids[rev][i] else: # no parent id, get the empty revision parentid = revision.NULL_REVISION prevtree = self.sourcerepo.revision_tree(parentid) changes = [e[0] for e in self._gettreechanges(curtree, prevtree)[0]] return changes def _gettreechanges(self, current, origin): revid = current._revision_id changes = [] renames = {} seen = set() # Process the entries by reverse lexicographic name order to # handle nested renames correctly, most specific first. curchanges = sorted(current.iter_changes(origin), key=lambda c: c[1][0] or c[1][1], reverse=True) for (fileid, paths, changed_content, versioned, parent, name, kind, executable) in curchanges: if paths[0] == u'' or paths[1] == u'': # ignore changes to tree root continue # bazaar tracks directories, mercurial does not, so # we have to rename the directory contents if kind[1] == 'directory': if kind[0] not in (None, 'directory'): # Replacing 'something' with a directory, record it # so it can be removed. changes.append((self.recode(paths[0]), revid)) if kind[0] == 'directory' and None not in paths: renaming = paths[0] != paths[1] # neither an add nor an delete - a move # rename all directory contents manually subdir = origin.inventory.path2id(paths[0]) # get all child-entries of the directory for name, entry in origin.inventory.iter_entries(subdir): # hg does not track directory renames if entry.kind == 'directory': continue frompath = self.recode(paths[0] + '/' + name) if frompath in seen: # Already handled by a more specific change entry # This is important when you have: # a => b # a/c => a/c # Here a/c must not be renamed into b/c continue seen.add(frompath) if not renaming: continue topath = self.recode(paths[1] + '/' + name) # register the files as changed changes.append((frompath, revid)) changes.append((topath, revid)) # add to mode cache mode = ((entry.executable and 'x') or (entry.kind == 'symlink' and 's') or '') self._modecache[(topath, revid)] = mode # register the change as move renames[topath] = frompath # no further changes, go to the next change continue # we got unicode paths, need to convert them path, topath = paths if path is not None: path = self.recode(path) if topath is not None: topath = self.recode(topath) seen.add(path or topath) if topath is None: # file deleted changes.append((path, revid)) continue # renamed if path and path != topath: renames[topath] = path changes.append((path, revid)) # populate the mode cache kind, executable = [e[1] for e in (kind, executable)] mode = ((executable and 'x') or (kind == 'symlink' and 'l') or '') self._modecache[(topath, revid)] = mode changes.append((topath, revid)) return changes, renames def _filterghosts(self, ids): """Filters out ghost revisions which hg does not support, see <http://bazaar-vcs.org/GhostRevision> """ parentmap = self.sourcerepo.get_parent_map(ids) parents = tuple([parent for parent in ids if parent in parentmap]) return parents