import tempfile, shutil, os
from mercurial.i18n import _
from mercurial import wireproto, error, util
import bfutil, bfsetup
def putbfile(repo, proto, sha):
fd, tempname = tempfile.mkstemp(prefix='hg-putbfile-')
with os.fdopen(fd, 'wb+') as fp:
proto.getfile(fp)
bfutil.copytocacheabsolute(repo, tempname, sha)
return ''
def getbfile(repo, proto, sha):
filename = bfutil.findfile(repo, sha)
if not filename:
raise util.Abort(_('requested bfile %s not present in cache') % sha)
f = open(filename, 'rb')
return wireproto.streamres(f)
# '0' for OK, '1' for invalid checksum, '2' for missing
def statbfile(repo, proto, sha):
filename = bfutil.findfile(repo, sha)
if not filename:
return '2\n'
with open(filename, 'rb') as f:
return '0\n' if bfutil.hexsha1(f) == sha else '1\n'
def wirerepo_putbfile(self, sha, fd):
return self._callstream("putbfile", data=fd, sha=sha, headers={'content-type':'application/mercurial-0.1'})
def wirerepo_getbfile(self, sha):
return self._callstream("getbfile", sha=sha)
def wirerepo_statbfile(self, sha):
try:
return int(self._call("statbfile", sha=sha))
except:
return 2
# if the server returns something that's not an integer followed by a
# newline, it's not kbfiles-capable, so obviously it doesn't have the
# bfile; any other exception means _something_ went wrong, so tell the
# caller the bfile is missing
def dispatch(repo, proto, command):
func, spec = wireproto.commands[command]
args = proto.getargs(spec)
if len(args) > 0 and isinstance(args[-1], dict):
if bfutil.listbfiles(repo) and command in affectedcommands and not args[-1].pop('kbfiles'):
return '0\n'
return func(repo, proto, *args)
def capabilities(repo, proto):
return wireproto.capabilities(repo, proto) + ' bfilestore=serve'
affectedcommands = [ 'changegroup', 'changegroupsubset', 'getbundle', 'unbundle', 'stream_out', 'pushkey' ]
|
Loading...