##// END OF EJS Templates
shelve: stop passing list of files to revert...
shelve: stop passing list of files to revert It seems to work just fine to not specify any files here. I suspect it looked the way it did for historical reasons. It apparently used to use merge instead of rebase until 1d7a36ff2615 (shelve: use rebase instead of merge (issue4068), 2013-10-23) and it makes sense to want to restrict the set of files then. I noticed this because of the files.extend(shelvectx.p1().files()). If the working copy was clean before, then shelvectx.p1() will be the working copy parent and that ended up adding all the files in that set. In our Google-internal Mercurial setup (including a FUSE) that was very noticeably slow when the working copy parent happened to have many files in large directories. This patch doesn't yet remove the call to shelvectx.p1().files(). We also use that set for deciding what to back up. I'm pretty sure it's safe to back up only the set of files we already back even if we no longer restrict the set of files to revert, so this patch should be safe on its own. Regardless, the next patch will delegate the backing-up to cmdutil.revert(). Incidentally, this also gets rid of a repo.pathto() that I had earlier wanted to get rid of. Differential Revision: https://phab.mercurial-scm.org/D6173

File last commit:

r40288:090e5f39 default
r42204:46065855 default
Show More
store.py
167 lines | 5.6 KiB | text/x-python | PythonLexer
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
# based on bundleheads extension by Gregory Szorc <gps@mozilla.com>
from __future__ import absolute_import
import abc
import hashlib
import os
import subprocess
import tempfile
from mercurial import (
node,
pycompat,
)
from mercurial.utils import (
procutil,
)
NamedTemporaryFile = tempfile.NamedTemporaryFile
class BundleWriteException(Exception):
pass
class BundleReadException(Exception):
pass
class abstractbundlestore(object):
"""Defines the interface for bundle stores.
A bundle store is an entity that stores raw bundle data. It is a simple
key-value store. However, the keys are chosen by the store. The keys can
be any Python object understood by the corresponding bundle index (see
``abstractbundleindex`` below).
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def write(self, data):
"""Write bundle data to the store.
This function receives the raw data to be written as a str.
Throws BundleWriteException
The key of the written data MUST be returned.
"""
@abc.abstractmethod
def read(self, key):
"""Obtain bundle data for a key.
Returns None if the bundle isn't known.
Throws BundleReadException
The returned object should be a file object supporting read()
and close().
"""
class filebundlestore(object):
"""bundle store in filesystem
meant for storing bundles somewhere on disk and on network filesystems
"""
def __init__(self, ui, repo):
self.ui = ui
self.repo = repo
self.storepath = ui.configpath('scratchbranch', 'storepath')
if not self.storepath:
self.storepath = self.repo.vfs.join("scratchbranches",
"filebundlestore")
if not os.path.exists(self.storepath):
os.makedirs(self.storepath)
def _dirpath(self, hashvalue):
"""First two bytes of the hash are the name of the upper
level directory, next two bytes are the name of the
next level directory"""
return os.path.join(self.storepath, hashvalue[0:2], hashvalue[2:4])
def _filepath(self, filename):
return os.path.join(self._dirpath(filename), filename)
def write(self, data):
filename = node.hex(hashlib.sha1(data).digest())
dirpath = self._dirpath(filename)
if not os.path.exists(dirpath):
os.makedirs(dirpath)
with open(self._filepath(filename), 'wb') as f:
f.write(data)
return filename
def read(self, key):
try:
with open(self._filepath(key), 'rb') as f:
return f.read()
except IOError:
return None
class externalbundlestore(abstractbundlestore):
def __init__(self, put_binary, put_args, get_binary, get_args):
"""
`put_binary` - path to binary file which uploads bundle to external
storage and prints key to stdout
`put_args` - format string with additional args to `put_binary`
{filename} replacement field can be used.
`get_binary` - path to binary file which accepts filename and key
(in that order), downloads bundle from store and saves it to file
`get_args` - format string with additional args to `get_binary`.
{filename} and {handle} replacement field can be used.
"""
self.put_args = put_args
self.get_args = get_args
self.put_binary = put_binary
self.get_binary = get_binary
def _call_binary(self, args):
p = subprocess.Popen(
pycompat.rapply(procutil.tonativestr, args),
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True)
stdout, stderr = p.communicate()
returncode = p.returncode
return returncode, stdout, stderr
def write(self, data):
# Won't work on windows because you can't open file second time without
# closing it
# TODO: rewrite without str.format() and replace NamedTemporaryFile()
# with pycompat.namedtempfile()
with NamedTemporaryFile() as temp:
temp.write(data)
temp.flush()
temp.seek(0)
formatted_args = [arg.format(filename=temp.name)
for arg in self.put_args]
returncode, stdout, stderr = self._call_binary(
[self.put_binary] + formatted_args)
if returncode != 0:
raise BundleWriteException(
'Failed to upload to external store: %s' % stderr)
stdout_lines = stdout.splitlines()
if len(stdout_lines) == 1:
return stdout_lines[0]
else:
raise BundleWriteException(
'Bad output from %s: %s' % (self.put_binary, stdout))
def read(self, handle):
# Won't work on windows because you can't open file second time without
# closing it
# TODO: rewrite without str.format() and replace NamedTemporaryFile()
# with pycompat.namedtempfile()
with NamedTemporaryFile() as temp:
formatted_args = [arg.format(filename=temp.name, handle=handle)
for arg in self.get_args]
returncode, stdout, stderr = self._call_binary(
[self.get_binary] + formatted_args)
if returncode != 0:
raise BundleReadException(
'Failed to download from external store: %s' % stderr)
return temp.read()