Mercurial > hg
view hgext/largefiles/lfutil.py @ 16719:e7bf09acd410
localrepo: add branchtip() method for faster single-branch lookups
For the PyPy repo with 744 branches and 843 branch heads, this brings
hg log -r default over NFS from:
CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
3249 0 1.3222 1.3222 <open>
3244 0 0.6211 0.6211 <method 'close' of 'file' objects>
3243 0 0.0800 0.0800 <method 'read' of 'file' objects>
3241 0 0.0660 0.0660 <method 'seek' of 'file' objects>
3905 0 0.0476 0.0476 <zlib.decompress>
3281 0 2.6756 0.0472 mercurial.changelog:182(read)
+3281 0 2.5256 0.0453 +mercurial.revlog:881(revision)
+3276 0 0.0389 0.0196 +mercurial.changelog:28(decodeextra)
+6562 0 0.0123 0.0123 +<method 'split' of 'str' objects>
+6562 0 0.0408 0.0073 +mercurial.encoding:61(tolocal)
+3281 0 0.0054 0.0054 +<method 'index' of 'str' objects>
3241 0 2.2464 0.0456 mercurial.revlog:818(_loadchunk)
+3241 0 0.6205 0.6205 +<method 'close' of 'file' objects>
+3241 0 0.0765 0.0765 +<method 'read' of 'file' objects>
+3241 0 0.0660 0.0660 +<method 'seek' of 'file' objects>
+3241 0 1.4209 0.0135 +mercurial.store:374(__call__)
+3241 0 0.0122 0.0107 +mercurial.revlog:810(_addchunk)
3281 0 2.5256 0.0453 mercurial.revlog:881(revision)
+3280 0 0.0175 0.0175 +mercurial.revlog:305(rev)
+3281 0 2.2819 0.0119 +mercurial.revlog:847(_chunkraw)
+3281 0 0.0603 0.0083 +mercurial.revlog:945(_checkhash)
+3281 0 0.0051 0.0051 +mercurial.revlog:349(flags)
+3281 0 0.0040 0.0040 +<mercurial.mpatch.patches>
13682 0 0.0479 0.0248 <method 'decode' of 'str' objects>
+7418 0 0.0228 0.0076 +encodings.utf_8:15(decode)
+1 0 0.0003 0.0000 +encodings:71(search_function)
3248 0 1.3995 0.0246 mercurial.scmutil:218(__call__)
+3248 0 1.3222 1.3222 +<open>
+3248 0 0.0235 0.0184 +os.path:80(split)
+3248 0 0.0084 0.0068 +mercurial.scmutil:92(__call__)
Time: real 2.750 secs (user 0.680+0.000 sys 0.360+0.000)
down to:
CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
55 31 0.0197 0.0163 <__import__>
+1 0 0.0006 0.0002 +mercurial.context:8(<module>)
+1 0 0.0042 0.0001 +mercurial.revlog:12(<module>)
+1 0 0.0002 0.0001 +mercurial.match:8(<module>)
+1 0 0.0003 0.0001 +mercurial.dirstate:7(<module>)
+1 0 0.0057 0.0001 +mercurial.changelog:8(<module>)
1 0 0.0117 0.0032 mercurial.localrepo:525(_readbranchcache)
+844 0 0.0015 0.0015 +<binascii.unhexlify>
+845 0 0.0010 0.0010 +<method 'split' of 'str' objects>
+843 0 0.0045 0.0009 +mercurial.encoding:61(tolocal)
+843 0 0.0004 0.0004 +<method 'setdefault' of 'dict' objects>
+1 0 0.0003 0.0003 +<method 'close' of 'file' objects>
3 0 0.0029 0.0029 <method 'read' of 'file' objects>
9 0 0.0018 0.0018 <open>
990 0 0.0017 0.0017 <binascii.unhexlify>
53 0 0.0016 0.0016 mercurial.demandimport:43(__init__)
862 0 0.0015 0.0015 <_codecs.utf_8_decode>
862 0 0.0037 0.0014 <method 'decode' of 'str' objects>
+862 0 0.0023 0.0008 +encodings.utf_8:15(decode)
981 0 0.0011 0.0011 <method 'split' of 'str' objects>
861 0 0.0046 0.0009 mercurial.encoding:61(tolocal)
+861 0 0.0037 0.0014 +<method 'decode' of 'str' objects>
862 0 0.0023 0.0008 encodings.utf_8:15(decode)
+862 0 0.0015 0.0015 +<_codecs.utf_8_decode>
4 0 0.0008 0.0008 <method 'close' of 'file' objects>
179 154 0.0202 0.0004 mercurial.demandimport:83(__getattribute__)
+36 11 0.0199 0.0003 +mercurial.demandimport:55(_load)
+72 0 0.0001 0.0001 +mercurial.demandimport:83(__getattribute__)
+36 0 0.0000 0.0000 +<getattr>
1 0 0.0015 0.0004 mercurial.tags:148(_readtagcache)
Time: real 0.060 secs (user 0.030+0.000 sys 0.010+0.000)
author | Brodie Rao <brodie@sf.io> |
---|---|
date | Sun, 13 May 2012 14:04:04 +0200 |
parents | d87d9d8a8e03 |
children | 73b9286e667c |
line wrap: on
line source
# Copyright 2009-2010 Gregory P. Ward # Copyright 2009-2010 Intelerad Medical Systems Incorporated # Copyright 2010-2011 Fog Creek Software # Copyright 2010-2011 Unity Technologies # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. '''largefiles utility code: must not import other modules in this package.''' import os import errno import platform import shutil import stat from mercurial import dirstate, httpconnection, match as match_, util, scmutil from mercurial.i18n import _ shortname = '.hglf' longname = 'largefiles' # -- Portability wrappers ---------------------------------------------- def dirstatewalk(dirstate, matcher, unknown=False, ignored=False): return dirstate.walk(matcher, [], unknown, ignored) def repoadd(repo, list): add = repo[None].add return add(list) def reporemove(repo, list, unlink=False): def remove(list, unlink): wlock = repo.wlock() try: if unlink: for f in list: try: util.unlinkpath(repo.wjoin(f)) except OSError, inst: if inst.errno != errno.ENOENT: raise repo[None].forget(list) finally: wlock.release() return remove(list, unlink=unlink) def repoforget(repo, list): forget = repo[None].forget return forget(list) def findoutgoing(repo, remote, force): from mercurial import discovery common, _anyinc, _heads = discovery.findcommonincoming(repo, remote, force=force) return repo.changelog.findmissing(common) # -- Private worker functions ------------------------------------------ def getminsize(ui, assumelfiles, opt, default=10): lfsize = opt if not lfsize and assumelfiles: lfsize = ui.config(longname, 'minsize', default=default) if lfsize: try: lfsize = float(lfsize) except ValueError: raise util.Abort(_('largefiles: size must be number (not %s)\n') % lfsize) if lfsize is None: raise util.Abort(_('minimum size for largefiles must be specified')) return lfsize def link(src, dest): try: util.oslink(src, dest) except OSError: # if hardlinks fail, fallback on atomic copy dst = util.atomictempfile(dest) for chunk in util.filechunkiter(open(src, 'rb')): dst.write(chunk) dst.close() os.chmod(dest, os.stat(src).st_mode) def usercachepath(ui, hash): path = ui.configpath(longname, 'usercache', None) if path: path = os.path.join(path, hash) else: if os.name == 'nt': appdata = os.getenv('LOCALAPPDATA', os.getenv('APPDATA')) if appdata: path = os.path.join(appdata, longname, hash) elif platform.system() == 'Darwin': home = os.getenv('HOME') if home: path = os.path.join(home, 'Library', 'Caches', longname, hash) elif os.name == 'posix': path = os.getenv('XDG_CACHE_HOME') if path: path = os.path.join(path, longname, hash) else: home = os.getenv('HOME') if home: path = os.path.join(home, '.cache', longname, hash) else: raise util.Abort(_('unknown operating system: %s\n') % os.name) return path def inusercache(ui, hash): path = usercachepath(ui, hash) return path and os.path.exists(path) def findfile(repo, hash): if instore(repo, hash): repo.ui.note(_('Found %s in store\n') % hash) return storepath(repo, hash) elif inusercache(repo.ui, hash): repo.ui.note(_('Found %s in system cache\n') % hash) path = storepath(repo, hash) util.makedirs(os.path.dirname(path)) link(usercachepath(repo.ui, hash), path) return path return None class largefilesdirstate(dirstate.dirstate): def __getitem__(self, key): return super(largefilesdirstate, self).__getitem__(unixpath(key)) def normal(self, f): return super(largefilesdirstate, self).normal(unixpath(f)) def remove(self, f): return super(largefilesdirstate, self).remove(unixpath(f)) def add(self, f): return super(largefilesdirstate, self).add(unixpath(f)) def drop(self, f): return super(largefilesdirstate, self).drop(unixpath(f)) def forget(self, f): return super(largefilesdirstate, self).forget(unixpath(f)) def normallookup(self, f): return super(largefilesdirstate, self).normallookup(unixpath(f)) def openlfdirstate(ui, repo): ''' Return a dirstate object that tracks largefiles: i.e. its root is the repo root, but it is saved in .hg/largefiles/dirstate. ''' admin = repo.join(longname) opener = scmutil.opener(admin) lfdirstate = largefilesdirstate(opener, ui, repo.root, repo.dirstate._validate) # If the largefiles dirstate does not exist, populate and create # it. This ensures that we create it on the first meaningful # largefiles operation in a new clone. if not os.path.exists(os.path.join(admin, 'dirstate')): util.makedirs(admin) matcher = getstandinmatcher(repo) for standin in dirstatewalk(repo.dirstate, matcher): lfile = splitstandin(standin) hash = readstandin(repo, lfile) lfdirstate.normallookup(lfile) try: if hash == hashfile(repo.wjoin(lfile)): lfdirstate.normal(lfile) except OSError, err: if err.errno != errno.ENOENT: raise return lfdirstate def lfdirstatestatus(lfdirstate, repo, rev): match = match_.always(repo.root, repo.getcwd()) s = lfdirstate.status(match, [], False, False, False) unsure, modified, added, removed, missing, unknown, ignored, clean = s for lfile in unsure: if repo[rev][standin(lfile)].data().strip() != \ hashfile(repo.wjoin(lfile)): modified.append(lfile) else: clean.append(lfile) lfdirstate.normal(lfile) return (modified, added, removed, missing, unknown, ignored, clean) def listlfiles(repo, rev=None, matcher=None): '''return a list of largefiles in the working copy or the specified changeset''' if matcher is None: matcher = getstandinmatcher(repo) # ignore unknown files in working directory return [splitstandin(f) for f in repo[rev].walk(matcher) if rev is not None or repo.dirstate[f] != '?'] def instore(repo, hash): return os.path.exists(storepath(repo, hash)) def storepath(repo, hash): return repo.join(os.path.join(longname, hash)) def copyfromcache(repo, hash, filename): '''Copy the specified largefile from the repo or system cache to filename in the repository. Return true on success or false if the file was not found in either cache (which should not happened: this is meant to be called only after ensuring that the needed largefile exists in the cache).''' path = findfile(repo, hash) if path is None: return False util.makedirs(os.path.dirname(repo.wjoin(filename))) # The write may fail before the file is fully written, but we # don't use atomic writes in the working copy. shutil.copy(path, repo.wjoin(filename)) return True def copytostore(repo, rev, file, uploaded=False): hash = readstandin(repo, file) if instore(repo, hash): return copytostoreabsolute(repo, repo.wjoin(file), hash) def copyalltostore(repo, node): '''Copy all largefiles in a given revision to the store''' ctx = repo[node] for filename in ctx.files(): if isstandin(filename) and filename in ctx.manifest(): realfile = splitstandin(filename) copytostore(repo, ctx.node(), realfile) def copytostoreabsolute(repo, file, hash): util.makedirs(os.path.dirname(storepath(repo, hash))) if inusercache(repo.ui, hash): link(usercachepath(repo.ui, hash), storepath(repo, hash)) else: dst = util.atomictempfile(storepath(repo, hash), createmode=repo.store.createmode) for chunk in util.filechunkiter(open(file, 'rb')): dst.write(chunk) dst.close() linktousercache(repo, hash) def linktousercache(repo, hash): path = usercachepath(repo.ui, hash) if path: util.makedirs(os.path.dirname(path)) link(storepath(repo, hash), path) def getstandinmatcher(repo, pats=[], opts={}): '''Return a match object that applies pats to the standin directory''' standindir = repo.pathto(shortname) if pats: # patterns supplied: search standin directory relative to current dir cwd = repo.getcwd() if os.path.isabs(cwd): # cwd is an absolute path for hg -R <reponame> # work relative to the repository root in this case cwd = '' pats = [os.path.join(standindir, cwd, pat) for pat in pats] elif os.path.isdir(standindir): # no patterns: relative to repo root pats = [standindir] else: # no patterns and no standin dir: return matcher that matches nothing match = match_.match(repo.root, None, [], exact=True) match.matchfn = lambda f: False return match return getmatcher(repo, pats, opts, showbad=False) def getmatcher(repo, pats=[], opts={}, showbad=True): '''Wrapper around scmutil.match() that adds showbad: if false, neuter the match object's bad() method so it does not print any warnings about missing files or directories.''' match = scmutil.match(repo[None], pats, opts) if not showbad: match.bad = lambda f, msg: None return match def composestandinmatcher(repo, rmatcher): '''Return a matcher that accepts standins corresponding to the files accepted by rmatcher. Pass the list of files in the matcher as the paths specified by the user.''' smatcher = getstandinmatcher(repo, rmatcher.files()) isstandin = smatcher.matchfn def composedmatchfn(f): return isstandin(f) and rmatcher.matchfn(splitstandin(f)) smatcher.matchfn = composedmatchfn return smatcher def standin(filename): '''Return the repo-relative path to the standin for the specified big file.''' # Notes: # 1) Most callers want an absolute path, but _createstandin() needs # it repo-relative so lfadd() can pass it to repoadd(). So leave # it up to the caller to use repo.wjoin() to get an absolute path. # 2) Join with '/' because that's what dirstate always uses, even on # Windows. Change existing separator to '/' first in case we are # passed filenames from an external source (like the command line). return shortname + '/' + util.pconvert(filename) def isstandin(filename): '''Return true if filename is a big file standin. filename must be in Mercurial's internal form (slash-separated).''' return filename.startswith(shortname + '/') def splitstandin(filename): # Split on / because that's what dirstate always uses, even on Windows. # Change local separator to / first just in case we are passed filenames # from an external source (like the command line). bits = util.pconvert(filename).split('/', 1) if len(bits) == 2 and bits[0] == shortname: return bits[1] else: return None def updatestandin(repo, standin): file = repo.wjoin(splitstandin(standin)) if os.path.exists(file): hash = hashfile(file) executable = getexecutable(file) writestandin(repo, standin, hash, executable) def readstandin(repo, filename, node=None): '''read hex hash from standin for filename at given node, or working directory if no node is given''' return repo[node][standin(filename)].data().strip() def writestandin(repo, standin, hash, executable): '''write hash to <repo.root>/<standin>''' writehash(hash, repo.wjoin(standin), executable) def copyandhash(instream, outfile): '''Read bytes from instream (iterable) and write them to outfile, computing the SHA-1 hash of the data along the way. Close outfile when done and return the binary hash.''' hasher = util.sha1('') for data in instream: hasher.update(data) outfile.write(data) # Blecch: closing a file that somebody else opened is rude and # wrong. But it's so darn convenient and practical! After all, # outfile was opened just to copy and hash. outfile.close() return hasher.digest() def hashrepofile(repo, file): return hashfile(repo.wjoin(file)) def hashfile(file): if not os.path.exists(file): return '' hasher = util.sha1('') fd = open(file, 'rb') for data in blockstream(fd): hasher.update(data) fd.close() return hasher.hexdigest() class limitreader(object): def __init__(self, f, limit): self.f = f self.limit = limit def read(self, length): if self.limit == 0: return '' length = length > self.limit and self.limit or length self.limit -= length return self.f.read(length) def close(self): pass def blockstream(infile, blocksize=128 * 1024): """Generator that yields blocks of data from infile and closes infile.""" while True: data = infile.read(blocksize) if not data: break yield data # same blecch as copyandhash() above infile.close() def writehash(hash, filename, executable): util.makedirs(os.path.dirname(filename)) util.writefile(filename, hash + '\n') os.chmod(filename, getmode(executable)) def getexecutable(filename): mode = os.stat(filename).st_mode return ((mode & stat.S_IXUSR) and (mode & stat.S_IXGRP) and (mode & stat.S_IXOTH)) def getmode(executable): if executable: return 0755 else: return 0644 def urljoin(first, second, *arg): def join(left, right): if not left.endswith('/'): left += '/' if right.startswith('/'): right = right[1:] return left + right url = join(first, second) for a in arg: url = join(url, a) return url def hexsha1(data): """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like object data""" h = util.sha1() for chunk in util.filechunkiter(data): h.update(chunk) return h.hexdigest() def httpsendfile(ui, filename): return httpconnection.httpsendfile(ui, filename, 'rb') def unixpath(path): '''Return a version of path normalized for use with the lfdirstate.''' return util.pconvert(os.path.normpath(path)) def islfilesrepo(repo): return ('largefiles' in repo.requirements and util.any(shortname + '/' in f[0] for f in repo.store.datafiles())) class storeprotonotcapable(Exception): def __init__(self, storetypes): self.storetypes = storetypes def getcurrentheads(repo): branches = repo.branchmap() heads = [] for branch in branches: newheads = repo.branchheads(branch) heads = heads + newheads return heads def getstandinsstate(repo): standins = [] matcher = getstandinmatcher(repo) for standin in dirstatewalk(repo.dirstate, matcher): lfile = splitstandin(standin) standins.append((lfile, readstandin(repo, lfile))) return standins def getlfilestoupdate(oldstandins, newstandins): changedstandins = set(oldstandins).symmetric_difference(set(newstandins)) filelist = [] for f in changedstandins: if f[0] not in filelist: filelist.append(f[0]) return filelist