##// END OF EJS Templates
branches: improve performance by removing redundant operations...
branches: improve performance by removing redundant operations This refactors the branches command so it collects all the information it needs about a branch in one pass over the branch map. In particular, it fixes an issue where the command called repo.branchtags() to get branch tips, and then used repo.branchheads() to get the closed/open status. Both repo methods read the changelog to determine if the branch is closed, resulting in extra, redundant I/O. For the PyPy repo with 744 branches and 843 branch heads, this brings hg branches over NFS from: CallCount Recursive Total(ms) Inline(ms) module:lineno(function) 2427 0 0.9057 0.9057 <open> 2424 0 0.4096 0.4096 <method 'close' of 'file' objects> 2424 0 0.0476 0.0476 <method 'read' of 'file' objects> 1 0 0.0468 0.0468 mercurial.revlog:637(headrevs) +1 0 0.0000 0.0000 +<len> 2422 0 0.0443 0.0443 <method 'seek' of 'file' objects> 2962 0 0.0337 0.0337 <zlib.decompress> 2491 0 1.8008 0.0322 mercurial.changelog:182(read) +2491 0 1.6982 0.0315 +mercurial.revlog:881(revision) +2488 0 0.0269 0.0134 +mercurial.changelog:28(decodeextra) +4982 0 0.0085 0.0085 +<method 'split' of 'str' objects> +4982 0 0.0274 0.0049 +mercurial.encoding:61(tolocal) +2491 0 0.0039 0.0039 +<method 'index' of 'str' objects> 2491 0 1.6982 0.0315 mercurial.revlog:881(revision) +2413 0 0.0112 0.0112 +mercurial.revlog:305(rev) +2491 0 1.5315 0.0068 +mercurial.revlog:847(_chunkraw) +2491 0 0.0414 0.0058 +mercurial.revlog:945(_checkhash) +2491 0 0.0028 0.0028 +mercurial.revlog:349(flags) +2491 0 0.0025 0.0025 +<mercurial.mpatch.patches> 2422 0 1.5089 0.0286 mercurial.revlog:818(_loadchunk) +2422 0 0.4093 0.4093 +<method 'close' of 'file' objects> +2422 0 0.0451 0.0451 +<method 'read' of 'file' objects> +2422 0 0.0443 0.0443 +<method 'seek' of 'file' objects> +2422 0 0.9703 0.0096 +mercurial.store:374(__call__) +2422 0 0.0079 0.0069 +mercurial.revlog:810(_addchunk) 5804 0 0.0204 0.0204 mercurial.revlog:305(rev) 2426 0 0.9552 0.0177 mercurial.scmutil:218(__call__) +2426 0 0.9057 0.9057 +<open> +2426 0 0.0120 0.0083 +os.path:80(split) +2426 0 0.0061 0.0049 +mercurial.scmutil:92(__call__) Time: real 1.950 secs (user 0.560+0.000 sys 0.220+0.000) down to: CallCount Recursive Total(ms) Inline(ms) module:lineno(function) 1545 0 0.6035 0.6035 <open> 1542 0 0.2697 0.2697 <method 'close' of 'file' objects> 1 0 0.0547 0.0547 mercurial.revlog:637(headrevs) +1 0 0.0000 0.0000 +<len> 1542 0 0.0389 0.0389 <method 'read' of 'file' objects> 1540 0 0.0316 0.0316 <method 'seek' of 'file' objects> 1853 0 0.0227 0.0227 <zlib.decompress> 1557 0 1.2131 0.0226 mercurial.changelog:182(read) +1557 0 1.1398 0.0221 +mercurial.revlog:881(revision) +1555 0 0.0199 0.0094 +mercurial.changelog:28(decodeextra) +3114 0 0.0058 0.0058 +<method 'split' of 'str' objects> +3114 0 0.0196 0.0035 +mercurial.encoding:61(tolocal) +1557 0 0.0026 0.0026 +<method 'index' of 'str' objects> 1557 0 1.1398 0.0221 mercurial.revlog:881(revision) +1557 0 1.0307 0.0047 +mercurial.revlog:847(_chunkraw) +1557 0 0.0287 0.0040 +mercurial.revlog:945(_checkhash) +1557 0 0.0018 0.0018 +<mercurial.mpatch.patches> +1557 0 0.0018 0.0018 +mercurial.revlog:326(node) +1557 0 0.0417 0.0013 +mercurial.revlog:857(_chunkbase) 1540 0 1.0147 0.0210 mercurial.revlog:818(_loadchunk) +1540 0 0.2693 0.2693 +<method 'close' of 'file' objects> +1540 0 0.0360 0.0360 +<method 'read' of 'file' objects> +1540 0 0.0316 0.0316 +<method 'seek' of 'file' objects> +1540 0 0.6487 0.0070 +mercurial.store:374(__call__) +1540 0 0.0059 0.0052 +mercurial.revlog:810(_addchunk) 3192 0 0.0173 0.0173 mercurial.revlog:305(rev) 8184 0 0.0300 0.0147 <method 'decode' of 'str' objects> +5204 0 0.0149 0.0048 +encodings.utf_8:15(decode) +1 0 0.0004 0.0000 +encodings:71(search_function) 43 26 0.0147 0.0129 <__import__> Time: real 1.390 secs (user 0.450+0.000 sys 0.170+0.000)

File last commit:

r16689:f366d4c2 default
r16721:3e6d59ae default
Show More
git.py
217 lines | 7.2 KiB | text/x-python | PythonLexer
# git.py - git support for the 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
# GNU General Public License version 2 or any later version.
import os
from mercurial import util
from mercurial.node import hex, nullid
from mercurial.i18n import _
from common import NoRepo, commit, converter_source, checktool
class convert_git(converter_source):
# Windows does not support GIT_DIR= construct while other systems
# cannot remove environment variable. Just assume none have
# both issues.
if util.safehasattr(os, 'unsetenv'):
def gitopen(self, s, noerr=False):
prevgitdir = os.environ.get('GIT_DIR')
os.environ['GIT_DIR'] = self.path
try:
if noerr:
(stdin, stdout, stderr) = util.popen3(s)
return stdout
else:
return util.popen(s, 'rb')
finally:
if prevgitdir is None:
del os.environ['GIT_DIR']
else:
os.environ['GIT_DIR'] = prevgitdir
else:
def gitopen(self, s, noerr=False):
if noerr:
(sin, so, se) = util.popen3('GIT_DIR=%s %s' % (self.path, s))
return so
else:
return util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
def gitread(self, s):
fh = self.gitopen(s)
data = fh.read()
return data, fh.close()
def __init__(self, ui, path, rev=None):
super(convert_git, self).__init__(ui, path, rev=rev)
if os.path.isdir(path + "/.git"):
path += "/.git"
if not os.path.exists(path + "/objects"):
raise NoRepo(_("%s does not look like a Git repository") % path)
checktool('git', 'git')
self.path = path
def getheads(self):
if not self.rev:
heads, ret = self.gitread('git rev-parse --branches --remotes')
heads = heads.splitlines()
else:
heads, ret = self.gitread("git rev-parse --verify %s" % self.rev)
heads = [heads[:-1]]
if ret:
raise util.Abort(_('cannot retrieve git heads'))
return heads
def catfile(self, rev, type):
if rev == hex(nullid):
raise IOError
data, ret = self.gitread("git cat-file %s %s" % (type, rev))
if ret:
raise util.Abort(_('cannot read %r object at %s') % (type, rev))
return data
def getfile(self, name, rev):
data = self.catfile(rev, "blob")
mode = self.modecache[(name, rev)]
return data, mode
def getchanges(self, version):
self.modecache = {}
fh = self.gitopen("git diff-tree -z --root -m -r %s" % version)
changes = []
seen = set()
entry = None
for l in fh.read().split('\x00'):
if not entry:
if not l.startswith(':'):
continue
entry = l
continue
f = l
if f not in seen:
seen.add(f)
entry = entry.split()
h = entry[3]
if entry[1] == '160000':
raise util.Abort('git submodules are not supported!')
p = (entry[1] == "100755")
s = (entry[1] == "120000")
self.modecache[(f, h)] = (p and "x") or (s and "l") or ""
changes.append((f, h))
entry = None
if fh.close():
raise util.Abort(_('cannot read changes in %s') % version)
return (changes, {})
def getcommit(self, version):
c = self.catfile(version, "commit") # read the commit hash
end = c.find("\n\n")
message = c[end + 2:]
message = self.recode(message)
l = c[:end].splitlines()
parents = []
author = committer = None
for e in l[1:]:
n, v = e.split(" ", 1)
if n == "author":
p = v.split()
tm, tz = p[-2:]
author = " ".join(p[:-2])
if author[0] == "<": author = author[1:-1]
author = self.recode(author)
if n == "committer":
p = v.split()
tm, tz = p[-2:]
committer = " ".join(p[:-2])
if committer[0] == "<": committer = committer[1:-1]
committer = self.recode(committer)
if n == "parent":
parents.append(v)
if committer and committer != author:
message += "\ncommitter: %s\n" % committer
tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:]
tz = -int(tzs) * (int(tzh) * 3600 + int(tzm))
date = tm + " " + str(tz)
c = commit(parents=parents, date=date, author=author, desc=message,
rev=version)
return c
def gettags(self):
tags = {}
alltags = {}
fh = self.gitopen('git ls-remote --tags "%s"' % self.path)
prefix = 'refs/tags/'
# Build complete list of tags, both annotated and bare ones
for line in fh:
line = line.strip()
node, tag = line.split(None, 1)
if not tag.startswith(prefix):
continue
alltags[tag[len(prefix):]] = node
if fh.close():
raise util.Abort(_('cannot read tags from %s') % self.path)
# Filter out tag objects for annotated tag refs
for tag in alltags:
if tag.endswith('^{}'):
tags[tag[:-3]] = alltags[tag]
else:
if tag + '^{}' in alltags:
continue
else:
tags[tag] = alltags[tag]
return tags
def getchangedfiles(self, version, i):
changes = []
if i is None:
fh = self.gitopen("git diff-tree --root -m -r %s" % version)
for l in fh:
if "\t" not in l:
continue
m, f = l[:-1].split("\t")
changes.append(f)
else:
fh = self.gitopen('git diff-tree --name-only --root -r %s '
'"%s^%s" --' % (version, version, i + 1))
changes = [f.rstrip('\n') for f in fh]
if fh.close():
raise util.Abort(_('cannot read changes in %s') % version)
return changes
def getbookmarks(self):
bookmarks = {}
# Interesting references in git are prefixed
prefix = 'refs/heads/'
prefixlen = len(prefix)
# factor two commands
gitcmd = { 'remote/': 'git ls-remote --heads origin',
'': 'git show-ref'}
# Origin heads
for reftype in gitcmd:
try:
fh = self.gitopen(gitcmd[reftype], noerr=True)
for line in fh:
line = line.strip()
rev, name = line.split(None, 1)
if not name.startswith(prefix):
continue
name = '%s%s' % (reftype, name[prefixlen:])
bookmarks[name] = rev
except Exception:
pass
return bookmarks