2011-09-24 19:35:45 +04:00
|
|
|
# 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
|
2011-10-21 01:05:13 +04:00
|
|
|
import platform
|
2011-09-24 19:35:45 +04:00
|
|
|
import shutil
|
|
|
|
import stat
|
2014-11-05 17:24:47 +03:00
|
|
|
import copy
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2011-10-11 16:01:24 +04:00
|
|
|
from mercurial import dirstate, httpconnection, match as match_, util, scmutil
|
2011-09-24 19:35:45 +04:00
|
|
|
from mercurial.i18n import _
|
2014-04-15 19:37:24 +04:00
|
|
|
from mercurial import node
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
shortname = '.hglf'
|
2012-12-13 22:19:06 +04:00
|
|
|
shortnameslash = shortname + '/'
|
2011-09-24 19:35:45 +04:00
|
|
|
longname = 'largefiles'
|
|
|
|
|
|
|
|
|
|
|
|
# -- Private worker functions ------------------------------------------
|
|
|
|
|
2011-10-12 05:11:01 +04:00
|
|
|
def getminsize(ui, assumelfiles, opt, default=10):
|
|
|
|
lfsize = opt
|
|
|
|
if not lfsize and assumelfiles:
|
2011-10-19 04:06:23 +04:00
|
|
|
lfsize = ui.config(longname, 'minsize', default=default)
|
2011-10-12 05:11:01 +04:00
|
|
|
if lfsize:
|
|
|
|
try:
|
2011-10-12 05:07:08 +04:00
|
|
|
lfsize = float(lfsize)
|
2011-10-12 05:11:01 +04:00
|
|
|
except ValueError:
|
2011-10-12 05:07:08 +04:00
|
|
|
raise util.Abort(_('largefiles: size must be number (not %s)\n')
|
2011-10-12 05:11:01 +04:00
|
|
|
% lfsize)
|
|
|
|
if lfsize is None:
|
|
|
|
raise util.Abort(_('minimum size for largefiles must be specified'))
|
|
|
|
return lfsize
|
|
|
|
|
2011-09-24 19:35:45 +04:00
|
|
|
def link(src, dest):
|
2013-04-16 01:32:33 +04:00
|
|
|
util.makedirs(os.path.dirname(dest))
|
2011-09-24 19:35:45 +04:00
|
|
|
try:
|
2011-10-06 13:10:06 +04:00
|
|
|
util.oslink(src, dest)
|
2011-09-24 19:35:45 +04:00
|
|
|
except OSError:
|
2011-11-24 21:13:18 +04:00
|
|
|
# if hardlinks fail, fallback on atomic copy
|
|
|
|
dst = util.atomictempfile(dest)
|
2011-12-20 21:43:38 +04:00
|
|
|
for chunk in util.filechunkiter(open(src, 'rb')):
|
2011-11-24 21:13:18 +04:00
|
|
|
dst.write(chunk)
|
|
|
|
dst.close()
|
2011-09-24 19:35:45 +04:00
|
|
|
os.chmod(dest, os.stat(src).st_mode)
|
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def usercachepath(ui, hash):
|
2011-10-23 22:25:48 +04:00
|
|
|
path = ui.configpath(longname, 'usercache', None)
|
2011-09-24 19:35:45 +04:00
|
|
|
if path:
|
|
|
|
path = os.path.join(path, hash)
|
|
|
|
else:
|
|
|
|
if os.name == 'nt':
|
2011-10-14 05:42:54 +04:00
|
|
|
appdata = os.getenv('LOCALAPPDATA', os.getenv('APPDATA'))
|
2011-12-15 22:19:43 +04:00
|
|
|
if appdata:
|
|
|
|
path = os.path.join(appdata, longname, hash)
|
2011-10-21 01:05:13 +04:00
|
|
|
elif platform.system() == 'Darwin':
|
2011-12-15 22:19:43 +04:00
|
|
|
home = os.getenv('HOME')
|
|
|
|
if home:
|
|
|
|
path = os.path.join(home, 'Library', 'Caches',
|
|
|
|
longname, hash)
|
2011-09-24 19:35:45 +04:00
|
|
|
elif os.name == 'posix':
|
2011-10-21 01:05:13 +04:00
|
|
|
path = os.getenv('XDG_CACHE_HOME')
|
|
|
|
if path:
|
|
|
|
path = os.path.join(path, longname, hash)
|
|
|
|
else:
|
2011-12-15 22:19:43 +04:00
|
|
|
home = os.getenv('HOME')
|
|
|
|
if home:
|
|
|
|
path = os.path.join(home, '.cache', longname, hash)
|
2011-09-24 19:35:45 +04:00
|
|
|
else:
|
2011-10-14 04:24:29 +04:00
|
|
|
raise util.Abort(_('unknown operating system: %s\n') % os.name)
|
2011-09-24 19:35:45 +04:00
|
|
|
return path
|
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def inusercache(ui, hash):
|
2011-12-15 22:19:43 +04:00
|
|
|
path = usercachepath(ui, hash)
|
|
|
|
return path and os.path.exists(path)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def findfile(repo, hash):
|
2011-10-20 21:24:09 +04:00
|
|
|
if instore(repo, hash):
|
2012-06-12 16:18:18 +04:00
|
|
|
repo.ui.note(_('found %s in store\n') % hash)
|
2012-01-18 16:41:03 +04:00
|
|
|
return storepath(repo, hash)
|
2011-10-20 21:24:11 +04:00
|
|
|
elif inusercache(repo.ui, hash):
|
2012-06-12 16:18:18 +04:00
|
|
|
repo.ui.note(_('found %s in system cache\n') % hash)
|
2011-11-03 18:59:32 +04:00
|
|
|
path = storepath(repo, hash)
|
|
|
|
link(usercachepath(repo.ui, hash), path)
|
2012-01-18 16:41:03 +04:00
|
|
|
return path
|
|
|
|
return None
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2012-03-09 19:11:52 +04:00
|
|
|
class largefilesdirstate(dirstate.dirstate):
|
2011-09-24 19:35:45 +04:00
|
|
|
def __getitem__(self, key):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).__getitem__(unixpath(key))
|
2011-09-24 19:35:45 +04:00
|
|
|
def normal(self, f):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).normal(unixpath(f))
|
2011-09-24 19:35:45 +04:00
|
|
|
def remove(self, f):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).remove(unixpath(f))
|
2011-09-24 19:35:45 +04:00
|
|
|
def add(self, f):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).add(unixpath(f))
|
2011-09-24 19:35:45 +04:00
|
|
|
def drop(self, f):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).drop(unixpath(f))
|
2011-09-24 19:35:45 +04:00
|
|
|
def forget(self, f):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).forget(unixpath(f))
|
2012-01-07 21:43:34 +04:00
|
|
|
def normallookup(self, f):
|
2012-03-09 19:11:52 +04:00
|
|
|
return super(largefilesdirstate, self).normallookup(unixpath(f))
|
2013-10-03 20:01:21 +04:00
|
|
|
def _ignore(self, f):
|
2012-12-13 22:19:06 +04:00
|
|
|
return False
|
2011-09-24 19:35:45 +04:00
|
|
|
|
largefiles: enable islfilesrepo() prior to a commit (issue3541)
Previously, even if a file was added with --large, 'hg addremove' or 'hg ci -A'
would add all files (including the previously added large files) as normal
files. Only after a commit where a file was added with --large would subsequent
adds or 'ci -A' take into account the minsize or the pattern configuration.
This change more closely follows the help for largefiles, which mentions that
'add --large' is required to enable the configuration, but doesn't mention the
previously required commit.
Also, if 'hg add --large' was performed and then 'hg forget <file>' (both before
a largefile enabling commit), the forget command would error out saying
'.hglf/<file> not tracked'. This is also fixed.
This reports that a repo is largefiles enabled as soon as a file is added with
--large, which enables 'add', 'addremove' and 'ci -A' to honor the config
settings before the first commit. Note that prior to the next commit, if all
largefiles are forgotten, the repository goes back to reporting the repo as not
largefiles enabled.
It makes no sense to handle this by adding a --large option to 'addremove',
because then it would also be needed for 'commit', but only when '-A' is
specified. While this gets around the awkwardness of having to add a largefile,
then commit it, and then addremove the other files when importing an existing
codebase (and preserving that extra commit in permanent history), it does still
require finding and manually adding one of the files as --large. Therefore it
is probably desirable to have a --large option for init as well.
2012-07-31 04:56:41 +04:00
|
|
|
def openlfdirstate(ui, repo, create=True):
|
2011-09-24 19:35:45 +04:00
|
|
|
'''
|
2011-10-13 04:59:27 +04:00
|
|
|
Return a dirstate object that tracks largefiles: i.e. its root is
|
|
|
|
the repo root, but it is saved in .hg/largefiles/dirstate.
|
2011-09-24 19:35:45 +04:00
|
|
|
'''
|
2012-12-13 22:19:06 +04:00
|
|
|
lfstoredir = repo.join(longname)
|
|
|
|
opener = scmutil.opener(lfstoredir)
|
2012-03-09 19:11:52 +04:00
|
|
|
lfdirstate = largefilesdirstate(opener, ui, repo.root,
|
2011-10-23 18:23:31 +04:00
|
|
|
repo.dirstate._validate)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2011-10-13 04:59:27 +04:00
|
|
|
# If the largefiles dirstate does not exist, populate and create
|
|
|
|
# it. This ensures that we create it on the first meaningful
|
2012-01-07 22:05:59 +04:00
|
|
|
# largefiles operation in a new clone.
|
2012-12-13 22:19:06 +04:00
|
|
|
if create and not os.path.exists(os.path.join(lfstoredir, 'dirstate')):
|
2011-09-24 19:35:45 +04:00
|
|
|
matcher = getstandinmatcher(repo)
|
2014-07-18 04:17:17 +04:00
|
|
|
standins = repo.dirstate.walk(matcher, [], False, False)
|
|
|
|
|
|
|
|
if len(standins) > 0:
|
|
|
|
util.makedirs(lfstoredir)
|
|
|
|
|
|
|
|
for standin in standins:
|
2011-09-24 19:35:45 +04:00
|
|
|
lfile = splitstandin(standin)
|
|
|
|
lfdirstate.normallookup(lfile)
|
|
|
|
return lfdirstate
|
|
|
|
|
2014-10-03 02:42:40 +04:00
|
|
|
def lfdirstatestatus(lfdirstate, repo):
|
|
|
|
wctx = repo['.']
|
2012-01-07 22:05:59 +04:00
|
|
|
match = match_.always(repo.root, repo.getcwd())
|
2014-10-04 08:44:10 +04:00
|
|
|
unsure, s = lfdirstate.status(match, [], False, False, False)
|
2014-10-04 09:10:08 +04:00
|
|
|
modified, clean = s.modified, s.clean
|
2012-01-07 22:05:59 +04:00
|
|
|
for lfile in unsure:
|
2013-01-11 19:30:29 +04:00
|
|
|
try:
|
2014-10-03 02:42:40 +04:00
|
|
|
fctx = wctx[standin(lfile)]
|
2013-01-11 19:30:29 +04:00
|
|
|
except LookupError:
|
|
|
|
fctx = None
|
|
|
|
if not fctx or fctx.data().strip() != hashfile(repo.wjoin(lfile)):
|
2012-01-07 22:05:59 +04:00
|
|
|
modified.append(lfile)
|
|
|
|
else:
|
|
|
|
clean.append(lfile)
|
|
|
|
lfdirstate.normal(lfile)
|
2014-10-04 08:21:20 +04:00
|
|
|
return s
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def listlfiles(repo, rev=None, matcher=None):
|
2011-10-13 04:59:27 +04:00
|
|
|
'''return a list of largefiles in the working copy or the
|
|
|
|
specified changeset'''
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
if matcher is None:
|
|
|
|
matcher = getstandinmatcher(repo)
|
|
|
|
|
|
|
|
# ignore unknown files in working directory
|
2011-10-14 05:42:54 +04:00
|
|
|
return [splitstandin(f)
|
|
|
|
for f in repo[rev].walk(matcher)
|
2011-09-24 19:35:45 +04:00
|
|
|
if rev is not None or repo.dirstate[f] != '?']
|
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def instore(repo, hash):
|
|
|
|
return os.path.exists(storepath(repo, hash))
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def storepath(repo, hash):
|
2011-09-24 19:35:45 +04:00
|
|
|
return repo.join(os.path.join(longname, hash))
|
|
|
|
|
|
|
|
def copyfromcache(repo, hash, filename):
|
2011-10-13 04:59:27 +04:00
|
|
|
'''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).'''
|
2011-09-24 19:35:45 +04:00
|
|
|
path = findfile(repo, hash)
|
|
|
|
if path is None:
|
|
|
|
return False
|
|
|
|
util.makedirs(os.path.dirname(repo.wjoin(filename)))
|
2011-11-24 21:11:43 +04:00
|
|
|
# The write may fail before the file is fully written, but we
|
|
|
|
# don't use atomic writes in the working copy.
|
2011-09-24 19:35:45 +04:00
|
|
|
shutil.copy(path, repo.wjoin(filename))
|
|
|
|
return True
|
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def copytostore(repo, rev, file, uploaded=False):
|
2012-10-24 05:07:14 +04:00
|
|
|
hash = readstandin(repo, file, rev)
|
2011-10-20 21:24:09 +04:00
|
|
|
if instore(repo, hash):
|
2011-09-24 19:35:45 +04:00
|
|
|
return
|
2011-10-20 21:24:09 +04:00
|
|
|
copytostoreabsolute(repo, repo.wjoin(file), hash)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2012-01-08 17:33:10 +04:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def copytostoreabsolute(repo, file, hash):
|
|
|
|
if inusercache(repo.ui, hash):
|
|
|
|
link(usercachepath(repo.ui, hash), storepath(repo, hash))
|
2014-11-07 18:48:41 +03:00
|
|
|
else:
|
2013-04-16 01:32:33 +04:00
|
|
|
util.makedirs(os.path.dirname(storepath(repo, hash)))
|
2012-02-23 16:22:55 +04:00
|
|
|
dst = util.atomictempfile(storepath(repo, hash),
|
|
|
|
createmode=repo.store.createmode)
|
2011-12-20 21:43:38 +04:00
|
|
|
for chunk in util.filechunkiter(open(file, 'rb')):
|
2011-11-24 21:12:13 +04:00
|
|
|
dst.write(chunk)
|
|
|
|
dst.close()
|
2011-10-20 21:24:09 +04:00
|
|
|
linktousercache(repo, hash)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2011-10-20 21:24:09 +04:00
|
|
|
def linktousercache(repo, hash):
|
2011-12-15 22:19:43 +04:00
|
|
|
path = usercachepath(repo.ui, hash)
|
|
|
|
if path:
|
|
|
|
link(storepath(repo, hash), path)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def getstandinmatcher(repo, pats=[], opts={}):
|
|
|
|
'''Return a match object that applies pats to the standin directory'''
|
2012-12-13 22:19:06 +04:00
|
|
|
standindir = repo.wjoin(shortname)
|
2011-09-24 19:35:45 +04:00
|
|
|
if pats:
|
2013-01-25 19:59:34 +04:00
|
|
|
pats = [os.path.join(standindir, pat) for pat in pats]
|
2013-02-28 16:45:18 +04:00
|
|
|
else:
|
2011-09-24 19:35:45 +04:00
|
|
|
# no patterns: relative to repo root
|
|
|
|
pats = [standindir]
|
2012-12-13 22:19:06 +04:00
|
|
|
# no warnings about missing files or directories
|
|
|
|
match = scmutil.match(repo[None], pats, opts)
|
|
|
|
match.bad = lambda f, msg: None
|
2011-09-24 19:35:45 +04:00
|
|
|
return match
|
|
|
|
|
|
|
|
def composestandinmatcher(repo, rmatcher):
|
2011-10-13 04:59:27 +04:00
|
|
|
'''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.'''
|
2011-09-24 19:35:45 +04:00
|
|
|
smatcher = getstandinmatcher(repo, rmatcher.files())
|
|
|
|
isstandin = smatcher.matchfn
|
2012-03-09 19:11:52 +04:00
|
|
|
def composedmatchfn(f):
|
2011-09-24 19:35:45 +04:00
|
|
|
return isstandin(f) and rmatcher.matchfn(splitstandin(f))
|
2012-03-09 19:11:52 +04:00
|
|
|
smatcher.matchfn = composedmatchfn
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
return smatcher
|
|
|
|
|
|
|
|
def standin(filename):
|
|
|
|
'''Return the repo-relative path to the standin for the specified big
|
|
|
|
file.'''
|
|
|
|
# Notes:
|
2012-08-16 00:39:18 +04:00
|
|
|
# 1) Some callers want an absolute path, but for instance addlargefiles
|
2012-12-13 22:19:06 +04:00
|
|
|
# needs it repo-relative so it can be passed to repo[None].add(). So
|
|
|
|
# leave it up to the caller to use repo.wjoin() to get an absolute path.
|
2011-09-24 19:35:45 +04:00
|
|
|
# 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).
|
2012-12-13 22:19:06 +04:00
|
|
|
return shortnameslash + util.pconvert(filename)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def isstandin(filename):
|
2011-10-13 04:59:27 +04:00
|
|
|
'''Return true if filename is a big file standin. filename must be
|
|
|
|
in Mercurial's internal form (slash-separated).'''
|
2012-12-13 22:19:06 +04:00
|
|
|
return filename.startswith(shortnameslash)
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
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).
|
2012-02-05 17:58:31 +04:00
|
|
|
bits = util.pconvert(filename).split('/', 1)
|
2011-09-24 19:35:45 +04:00
|
|
|
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):
|
2011-10-13 04:59:27 +04:00
|
|
|
'''write hash to <repo.root>/<standin>'''
|
2013-04-27 02:41:42 +04:00
|
|
|
repo.wwrite(standin, hash + '\n', executable and 'x' or '')
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def copyandhash(instream, outfile):
|
|
|
|
'''Read bytes from instream (iterable) and write them to outfile,
|
2013-04-16 01:43:50 +04:00
|
|
|
computing the SHA-1 hash of the data along the way. Return the hash.'''
|
2011-09-24 19:35:45 +04:00
|
|
|
hasher = util.sha1('')
|
|
|
|
for data in instream:
|
|
|
|
hasher.update(data)
|
|
|
|
outfile.write(data)
|
2013-04-16 01:35:18 +04:00
|
|
|
return hasher.hexdigest()
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
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')
|
2013-04-16 01:43:44 +04:00
|
|
|
for data in util.filechunkiter(fd, 128 * 1024):
|
2011-09-24 19:35:45 +04:00
|
|
|
hasher.update(data)
|
|
|
|
fd.close()
|
|
|
|
return hasher.hexdigest()
|
|
|
|
|
|
|
|
def getexecutable(filename):
|
|
|
|
mode = os.stat(filename).st_mode
|
2011-10-14 05:42:54 +04:00
|
|
|
return ((mode & stat.S_IXUSR) and
|
|
|
|
(mode & stat.S_IXGRP) and
|
|
|
|
(mode & stat.S_IXOTH))
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
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"""
|
2011-10-23 23:59:15 +04:00
|
|
|
h = util.sha1()
|
2011-09-24 19:35:45 +04:00
|
|
|
for chunk in util.filechunkiter(data):
|
|
|
|
h.update(chunk)
|
|
|
|
return h.hexdigest()
|
|
|
|
|
|
|
|
def httpsendfile(ui, filename):
|
2011-10-11 12:42:56 +04:00
|
|
|
return httpconnection.httpsendfile(ui, filename, 'rb')
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def unixpath(path):
|
2011-10-13 04:59:27 +04:00
|
|
|
'''Return a version of path normalized for use with the lfdirstate.'''
|
2012-02-05 17:58:31 +04:00
|
|
|
return util.pconvert(os.path.normpath(path))
|
2011-09-24 19:35:45 +04:00
|
|
|
|
|
|
|
def islfilesrepo(repo):
|
largefiles: enable islfilesrepo() prior to a commit (issue3541)
Previously, even if a file was added with --large, 'hg addremove' or 'hg ci -A'
would add all files (including the previously added large files) as normal
files. Only after a commit where a file was added with --large would subsequent
adds or 'ci -A' take into account the minsize or the pattern configuration.
This change more closely follows the help for largefiles, which mentions that
'add --large' is required to enable the configuration, but doesn't mention the
previously required commit.
Also, if 'hg add --large' was performed and then 'hg forget <file>' (both before
a largefile enabling commit), the forget command would error out saying
'.hglf/<file> not tracked'. This is also fixed.
This reports that a repo is largefiles enabled as soon as a file is added with
--large, which enables 'add', 'addremove' and 'ci -A' to honor the config
settings before the first commit. Note that prior to the next commit, if all
largefiles are forgotten, the repository goes back to reporting the repo as not
largefiles enabled.
It makes no sense to handle this by adding a --large option to 'addremove',
because then it would also be needed for 'commit', but only when '-A' is
specified. While this gets around the awkwardness of having to add a largefile,
then commit it, and then addremove the other files when importing an existing
codebase (and preserving that extra commit in permanent history), it does still
require finding and manually adding one of the files as --large. Therefore it
is probably desirable to have a --large option for init as well.
2012-07-31 04:56:41 +04:00
|
|
|
if ('largefiles' in repo.requirements and
|
2012-12-13 22:19:06 +04:00
|
|
|
util.any(shortnameslash in f[0] for f in repo.store.datafiles())):
|
largefiles: enable islfilesrepo() prior to a commit (issue3541)
Previously, even if a file was added with --large, 'hg addremove' or 'hg ci -A'
would add all files (including the previously added large files) as normal
files. Only after a commit where a file was added with --large would subsequent
adds or 'ci -A' take into account the minsize or the pattern configuration.
This change more closely follows the help for largefiles, which mentions that
'add --large' is required to enable the configuration, but doesn't mention the
previously required commit.
Also, if 'hg add --large' was performed and then 'hg forget <file>' (both before
a largefile enabling commit), the forget command would error out saying
'.hglf/<file> not tracked'. This is also fixed.
This reports that a repo is largefiles enabled as soon as a file is added with
--large, which enables 'add', 'addremove' and 'ci -A' to honor the config
settings before the first commit. Note that prior to the next commit, if all
largefiles are forgotten, the repository goes back to reporting the repo as not
largefiles enabled.
It makes no sense to handle this by adding a --large option to 'addremove',
because then it would also be needed for 'commit', but only when '-A' is
specified. While this gets around the awkwardness of having to add a largefile,
then commit it, and then addremove the other files when importing an existing
codebase (and preserving that extra commit in permanent history), it does still
require finding and manually adding one of the files as --large. Therefore it
is probably desirable to have a --large option for init as well.
2012-07-31 04:56:41 +04:00
|
|
|
return True
|
|
|
|
|
|
|
|
return util.any(openlfdirstate(repo.ui, repo, False))
|
2011-09-24 19:35:45 +04:00
|
|
|
|
2011-10-22 01:52:16 +04:00
|
|
|
class storeprotonotcapable(Exception):
|
2011-09-24 19:35:45 +04:00
|
|
|
def __init__(self, storetypes):
|
|
|
|
self.storetypes = storetypes
|
2012-02-10 17:46:09 +04:00
|
|
|
|
largefiles: optimize update speed by only updating changed largefiles
Historically, during 'hg update', every largefile in the working copy was
hashed (which is a very expensive operation on big files) and any
largefiles that did not have a hash that matched their standin were
updated.
This patch optimizes 'hg update' by keeping track of what standins have
changed between the old and new revisions, and only updating the largefiles
that have changed. This saves a lot of time by avoiding the unecessary
calculation of a list of sha1 hashes for big files.
With this patch, the time 'hg update' takes to complete is a function of
how many largefiles need to be updated and what their size is.
Performance tests on a repository with about 80 largefiles ranging from
a few MB to about 97 MB are shown below. The tests show how long it takes
to run 'hg update' with no changes actually being updated.
Mercurial 2.1 release:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
getting changed largefiles
0 largefiles updated, 0 removed
real 0m10.045s
user 0m9.367s
sys 0m0.674s
With this patch:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
real 0m0.965s
user 0m0.845s
sys 0m0.115s
The same repsoitory, without the largefiles extension enabled:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
real 0m0.799s
user 0m0.684s
sys 0m0.111s
So before the patch, 'hg update' with no changes was approximately 9.25s
slower with largefiles enabled. With this patch, it is approximately 0.165s
slower.
2012-02-13 21:37:07 +04:00
|
|
|
def getstandinsstate(repo):
|
|
|
|
standins = []
|
|
|
|
matcher = getstandinmatcher(repo)
|
2012-12-13 22:19:06 +04:00
|
|
|
for standin in repo.dirstate.walk(matcher, [], False, False):
|
largefiles: optimize update speed by only updating changed largefiles
Historically, during 'hg update', every largefile in the working copy was
hashed (which is a very expensive operation on big files) and any
largefiles that did not have a hash that matched their standin were
updated.
This patch optimizes 'hg update' by keeping track of what standins have
changed between the old and new revisions, and only updating the largefiles
that have changed. This saves a lot of time by avoiding the unecessary
calculation of a list of sha1 hashes for big files.
With this patch, the time 'hg update' takes to complete is a function of
how many largefiles need to be updated and what their size is.
Performance tests on a repository with about 80 largefiles ranging from
a few MB to about 97 MB are shown below. The tests show how long it takes
to run 'hg update' with no changes actually being updated.
Mercurial 2.1 release:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
getting changed largefiles
0 largefiles updated, 0 removed
real 0m10.045s
user 0m9.367s
sys 0m0.674s
With this patch:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
real 0m0.965s
user 0m0.845s
sys 0m0.115s
The same repsoitory, without the largefiles extension enabled:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
real 0m0.799s
user 0m0.684s
sys 0m0.111s
So before the patch, 'hg update' with no changes was approximately 9.25s
slower with largefiles enabled. With this patch, it is approximately 0.165s
slower.
2012-02-13 21:37:07 +04:00
|
|
|
lfile = splitstandin(standin)
|
2013-01-11 19:30:29 +04:00
|
|
|
try:
|
|
|
|
hash = readstandin(repo, lfile)
|
|
|
|
except IOError:
|
|
|
|
hash = None
|
|
|
|
standins.append((lfile, hash))
|
largefiles: optimize update speed by only updating changed largefiles
Historically, during 'hg update', every largefile in the working copy was
hashed (which is a very expensive operation on big files) and any
largefiles that did not have a hash that matched their standin were
updated.
This patch optimizes 'hg update' by keeping track of what standins have
changed between the old and new revisions, and only updating the largefiles
that have changed. This saves a lot of time by avoiding the unecessary
calculation of a list of sha1 hashes for big files.
With this patch, the time 'hg update' takes to complete is a function of
how many largefiles need to be updated and what their size is.
Performance tests on a repository with about 80 largefiles ranging from
a few MB to about 97 MB are shown below. The tests show how long it takes
to run 'hg update' with no changes actually being updated.
Mercurial 2.1 release:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
getting changed largefiles
0 largefiles updated, 0 removed
real 0m10.045s
user 0m9.367s
sys 0m0.674s
With this patch:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
real 0m0.965s
user 0m0.845s
sys 0m0.115s
The same repsoitory, without the largefiles extension enabled:
$ time hg update
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
real 0m0.799s
user 0m0.684s
sys 0m0.111s
So before the patch, 'hg update' with no changes was approximately 9.25s
slower with largefiles enabled. With this patch, it is approximately 0.165s
slower.
2012-02-13 21:37:07 +04:00
|
|
|
return standins
|
2012-03-09 19:45:49 +04:00
|
|
|
|
2014-08-11 17:29:43 +04:00
|
|
|
def synclfdirstate(repo, lfdirstate, lfile, normallookup):
|
|
|
|
lfstandin = standin(lfile)
|
|
|
|
if lfstandin in repo.dirstate:
|
|
|
|
stat = repo.dirstate._map[lfstandin]
|
|
|
|
state, mtime = stat[0], stat[3]
|
|
|
|
else:
|
|
|
|
state, mtime = '?', -1
|
|
|
|
if state == 'n':
|
|
|
|
if normallookup or mtime < 0:
|
|
|
|
# state 'n' doesn't ensure 'clean' in this case
|
|
|
|
lfdirstate.normallookup(lfile)
|
|
|
|
else:
|
|
|
|
lfdirstate.normal(lfile)
|
|
|
|
elif state == 'm':
|
|
|
|
lfdirstate.normallookup(lfile)
|
|
|
|
elif state == 'r':
|
|
|
|
lfdirstate.remove(lfile)
|
|
|
|
elif state == 'a':
|
|
|
|
lfdirstate.add(lfile)
|
|
|
|
elif state == '?':
|
|
|
|
lfdirstate.drop(lfile)
|
|
|
|
|
2014-11-05 17:24:47 +03:00
|
|
|
def markcommitted(orig, ctx, node):
|
2015-03-13 06:08:16 +03:00
|
|
|
repo = ctx.repo()
|
2014-11-05 17:24:47 +03:00
|
|
|
|
|
|
|
orig(node)
|
|
|
|
|
2014-11-07 18:48:38 +03:00
|
|
|
# ATTENTION: "ctx.files()" may differ from "repo[node].files()"
|
|
|
|
# because files coming from the 2nd parent are omitted in the latter.
|
|
|
|
#
|
|
|
|
# The former should be used to get targets of "synclfdirstate",
|
|
|
|
# because such files:
|
|
|
|
# - are marked as "a" by "patch.patch()" (e.g. via transplant), and
|
|
|
|
# - have to be marked as "n" after commit, but
|
|
|
|
# - aren't listed in "repo[node].files()"
|
|
|
|
|
2014-11-05 17:24:47 +03:00
|
|
|
lfdirstate = openlfdirstate(repo.ui, repo)
|
|
|
|
for f in ctx.files():
|
|
|
|
if isstandin(f):
|
|
|
|
lfile = splitstandin(f)
|
|
|
|
synclfdirstate(repo, lfdirstate, lfile, False)
|
|
|
|
lfdirstate.write()
|
|
|
|
|
2014-11-07 18:48:41 +03:00
|
|
|
# As part of committing, copy all of the largefiles into the cache.
|
|
|
|
copyalltostore(repo, node)
|
|
|
|
|
2012-03-09 19:45:49 +04:00
|
|
|
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
|
2014-04-15 19:37:24 +04:00
|
|
|
|
|
|
|
def getlfilestoupload(repo, missing, addfunc):
|
2015-01-16 21:51:25 +03:00
|
|
|
for i, n in enumerate(missing):
|
|
|
|
repo.ui.progress(_('finding outgoing largefiles'), i,
|
|
|
|
unit=_('revision'), total=len(missing))
|
2014-04-15 19:37:24 +04:00
|
|
|
parents = [p for p in repo.changelog.parents(n) if p != node.nullid]
|
2014-12-18 05:51:09 +03:00
|
|
|
|
|
|
|
oldlfstatus = repo.lfstatus
|
|
|
|
repo.lfstatus = False
|
|
|
|
try:
|
|
|
|
ctx = repo[n]
|
|
|
|
finally:
|
|
|
|
repo.lfstatus = oldlfstatus
|
|
|
|
|
2014-04-15 19:37:24 +04:00
|
|
|
files = set(ctx.files())
|
|
|
|
if len(parents) == 2:
|
|
|
|
mc = ctx.manifest()
|
|
|
|
mp1 = ctx.parents()[0].manifest()
|
|
|
|
mp2 = ctx.parents()[1].manifest()
|
|
|
|
for f in mp1:
|
|
|
|
if f not in mc:
|
|
|
|
files.add(f)
|
|
|
|
for f in mp2:
|
|
|
|
if f not in mc:
|
|
|
|
files.add(f)
|
|
|
|
for f in mc:
|
|
|
|
if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None):
|
|
|
|
files.add(f)
|
|
|
|
for fn in files:
|
|
|
|
if isstandin(fn) and fn in ctx:
|
|
|
|
addfunc(fn, ctx[fn].data().strip())
|
2015-01-16 21:51:25 +03:00
|
|
|
repo.ui.progress(_('finding outgoing largefiles'), None)
|
2014-11-05 17:24:47 +03:00
|
|
|
|
|
|
|
def updatestandinsbymatch(repo, match):
|
|
|
|
'''Update standins in the working directory according to specified match
|
|
|
|
|
|
|
|
This returns (possibly modified) ``match`` object to be used for
|
|
|
|
subsequent commit process.
|
|
|
|
'''
|
|
|
|
|
|
|
|
ui = repo.ui
|
|
|
|
|
|
|
|
# Case 1: user calls commit with no specific files or
|
|
|
|
# include/exclude patterns: refresh and commit all files that
|
|
|
|
# are "dirty".
|
|
|
|
if match is None or match.always():
|
|
|
|
# Spend a bit of time here to get a list of files we know
|
|
|
|
# are modified so we can compare only against those.
|
|
|
|
# It can cost a lot of time (several seconds)
|
|
|
|
# otherwise to update all standins if the largefiles are
|
|
|
|
# large.
|
|
|
|
lfdirstate = openlfdirstate(ui, repo)
|
|
|
|
dirtymatch = match_.always(repo.root, repo.getcwd())
|
|
|
|
unsure, s = lfdirstate.status(dirtymatch, [], False, False,
|
|
|
|
False)
|
|
|
|
modifiedfiles = unsure + s.modified + s.added + s.removed
|
|
|
|
lfiles = listlfiles(repo)
|
|
|
|
# this only loops through largefiles that exist (not
|
|
|
|
# removed/renamed)
|
|
|
|
for lfile in lfiles:
|
|
|
|
if lfile in modifiedfiles:
|
|
|
|
if os.path.exists(
|
|
|
|
repo.wjoin(standin(lfile))):
|
|
|
|
# this handles the case where a rebase is being
|
|
|
|
# performed and the working copy is not updated
|
|
|
|
# yet.
|
|
|
|
if os.path.exists(repo.wjoin(lfile)):
|
|
|
|
updatestandin(repo,
|
|
|
|
standin(lfile))
|
|
|
|
|
|
|
|
return match
|
|
|
|
|
|
|
|
lfiles = listlfiles(repo)
|
|
|
|
match._files = repo._subdirlfs(match.files(), lfiles)
|
|
|
|
|
|
|
|
# Case 2: user calls commit with specified patterns: refresh
|
|
|
|
# any matching big files.
|
|
|
|
smatcher = composestandinmatcher(repo, match)
|
|
|
|
standins = repo.dirstate.walk(smatcher, [], False, False)
|
|
|
|
|
|
|
|
# No matching big files: get out of the way and pass control to
|
|
|
|
# the usual commit() method.
|
|
|
|
if not standins:
|
|
|
|
return match
|
|
|
|
|
|
|
|
# Refresh all matching big files. It's possible that the
|
|
|
|
# commit will end up failing, in which case the big files will
|
|
|
|
# stay refreshed. No harm done: the user modified them and
|
|
|
|
# asked to commit them, so sooner or later we're going to
|
|
|
|
# refresh the standins. Might as well leave them refreshed.
|
|
|
|
lfdirstate = openlfdirstate(ui, repo)
|
|
|
|
for fstandin in standins:
|
|
|
|
lfile = splitstandin(fstandin)
|
|
|
|
if lfdirstate[lfile] != 'r':
|
|
|
|
updatestandin(repo, fstandin)
|
|
|
|
|
|
|
|
# Cook up a new matcher that only matches regular files or
|
|
|
|
# standins corresponding to the big files requested by the
|
|
|
|
# user. Have to modify _files to prevent commit() from
|
|
|
|
# complaining "not tracked" for big files.
|
|
|
|
match = copy.copy(match)
|
|
|
|
origmatchfn = match.matchfn
|
|
|
|
|
|
|
|
# Check both the list of largefiles and the list of
|
|
|
|
# standins because if a largefile was removed, it
|
|
|
|
# won't be in the list of largefiles at this point
|
|
|
|
match._files += sorted(standins)
|
|
|
|
|
|
|
|
actualfiles = []
|
|
|
|
for f in match._files:
|
|
|
|
fstandin = standin(f)
|
|
|
|
|
|
|
|
# ignore known largefiles and standins
|
|
|
|
if f in lfiles or fstandin in standins:
|
|
|
|
continue
|
|
|
|
|
|
|
|
actualfiles.append(f)
|
|
|
|
match._files = actualfiles
|
|
|
|
|
|
|
|
def matchfn(f):
|
|
|
|
if origmatchfn(f):
|
|
|
|
return f not in lfiles
|
|
|
|
else:
|
|
|
|
return f in standins
|
|
|
|
|
|
|
|
match.matchfn = matchfn
|
|
|
|
|
|
|
|
return match
|
largefiles: update standins only at the 1st commit of "hg rebase --continue"
Before this patch, "hg rebase --continue" may record incorrect
standins, because largefiles extension always avoid updating standins
while rebasing, even though largefiles in the working directory may be
modified manually at the 1st commit of "hg rebase --continue".
But, on the other hand, updating standins should be avoided at
subsequent commits for efficiency reason.
To update standins only at the 1st commit of "hg rebase --continue",
this patch introduces state-full callable object
"automatedcommithook", which updates standins by
"lfutil.updatestandinsbymatch()" only at the 1st commit of resuming.
Even after this patch, "repo._isrebasing = True" is still needed to
avoid some status report while updating largefiles in
"lfcommands.updatelfiles()".
This is reason why this patch omits not "repo._isrebasing = True" in
"overriderebase" but examination of "getattr(repo, "_isrebasing",
False)" in "updatestandinsbymatch".
2014-11-05 17:24:47 +03:00
|
|
|
|
|
|
|
class automatedcommithook(object):
|
2014-04-18 00:47:38 +04:00
|
|
|
'''Stateful hook to update standins at the 1st commit of resuming
|
largefiles: update standins only at the 1st commit of "hg rebase --continue"
Before this patch, "hg rebase --continue" may record incorrect
standins, because largefiles extension always avoid updating standins
while rebasing, even though largefiles in the working directory may be
modified manually at the 1st commit of "hg rebase --continue".
But, on the other hand, updating standins should be avoided at
subsequent commits for efficiency reason.
To update standins only at the 1st commit of "hg rebase --continue",
this patch introduces state-full callable object
"automatedcommithook", which updates standins by
"lfutil.updatestandinsbymatch()" only at the 1st commit of resuming.
Even after this patch, "repo._isrebasing = True" is still needed to
avoid some status report while updating largefiles in
"lfcommands.updatelfiles()".
This is reason why this patch omits not "repo._isrebasing = True" in
"overriderebase" but examination of "getattr(repo, "_isrebasing",
False)" in "updatestandinsbymatch".
2014-11-05 17:24:47 +03:00
|
|
|
|
|
|
|
For efficiency, updating standins in the working directory should
|
|
|
|
be avoided while automated committing (like rebase, transplant and
|
|
|
|
so on), because they should be updated before committing.
|
|
|
|
|
|
|
|
But the 1st commit of resuming automated committing (e.g. ``rebase
|
|
|
|
--continue``) should update them, because largefiles may be
|
|
|
|
modified manually.
|
|
|
|
'''
|
|
|
|
def __init__(self, resuming):
|
|
|
|
self.resuming = resuming
|
|
|
|
|
|
|
|
def __call__(self, repo, match):
|
|
|
|
if self.resuming:
|
|
|
|
self.resuming = False # avoids updating at subsequent commits
|
|
|
|
return updatestandinsbymatch(repo, match)
|
|
|
|
else:
|
|
|
|
return match
|
2014-11-05 17:24:47 +03:00
|
|
|
|
|
|
|
def getstatuswriter(ui, repo, forcibly=None):
|
|
|
|
'''Return the function to write largefiles specific status out
|
|
|
|
|
|
|
|
If ``forcibly`` is ``None``, this returns the last element of
|
2014-04-18 00:47:38 +04:00
|
|
|
``repo._lfstatuswriters`` as "default" writer function.
|
2014-11-05 17:24:47 +03:00
|
|
|
|
|
|
|
Otherwise, this returns the function to always write out (or
|
|
|
|
ignore if ``not forcibly``) status.
|
|
|
|
'''
|
largefiles: access to specific fields only if largefiles enabled (issue4547)
Even if largefiles extension is enabled in a repository, "repo"
object, which isn't "largefiles.reposetup()"-ed, is passed to
overridden functions in the cases below unexpectedly, because
extensions are enabled for each repositories strictly.
(1) clone without -U:
(2) pull with -U:
(3) pull with --rebase:
combination of "enabled@src", "disabled@dst" and
"not-required@src" cause this situation.
largefiles requirement
@src @dst @src result
-------- -------- --------------- --------------------
enabled disabled not-required aborted unexpectedly
required requirement error (intentional)
-------- -------- --------------- --------------------
enabled enabled * success
-------- -------- --------------- --------------------
disabled enabled * success (only for "pull")
-------- -------- --------------- --------------------
disabled disabled not-required success
required requirement error (intentional)
-------- -------- --------------- --------------------
(4) update/revert with a subrepo disabling largefiles
In these cases, overridden functions cause accessing to largefiles
specific fields of not "largefiles.reposetup()"-ed "repo" object, and
execution is aborted.
- (1), (2), (4) cause accessing to "_lfstatuswriters" in
"getstatuswriter()" invoked via "updatelfiles()"
- (3) causes accessing to "_lfcommithooks" in "overriderebase()"
For safe accessing to these fields, this patch examines whether passed
"repo" object is "largefiles.reposetup()"-ed or not before accessing
to them.
This patch chooses examining existence of newly introduced
"_largefilesenabled" instead of "_lfcommithooks" and
"_lfstatuswriters" directly, because the former is better name for the
generic "largefiles is enabled in this repo" mark than the latter.
In the future, all other overridden functions should avoid largefiles
specific processing for efficiency, and "_largefilesenabled" is better
also for such purpose.
BTW, "lfstatus" can't be used for such purpose, because some code
paths set it forcibly regardless of existence of it in specified
"repo" object.
2015-02-26 00:03:39 +03:00
|
|
|
if forcibly is None and util.safehasattr(repo, '_largefilesenabled'):
|
2014-11-05 17:24:47 +03:00
|
|
|
return repo._lfstatuswriters[-1]
|
|
|
|
else:
|
|
|
|
if forcibly:
|
|
|
|
return ui.status # forcibly WRITE OUT
|
|
|
|
else:
|
|
|
|
return lambda *msg, **opts: None # forcibly IGNORE
|