2017-03-25 05:00:30 +03:00
|
|
|
# coding=UTF-8
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from mercurial import (
|
|
|
|
node,
|
|
|
|
revlog,
|
2017-03-25 05:01:42 +03:00
|
|
|
util as hgutil,
|
2017-03-25 05:00:30 +03:00
|
|
|
)
|
2017-03-25 05:01:42 +03:00
|
|
|
from mercurial.i18n import _
|
2017-03-25 05:00:30 +03:00
|
|
|
|
|
|
|
from . import (
|
|
|
|
blobstore,
|
|
|
|
pointer,
|
|
|
|
util,
|
|
|
|
)
|
|
|
|
|
|
|
|
def supportedoutgoingversions(orig, repo):
|
|
|
|
versions = orig(repo)
|
|
|
|
versions.discard('01')
|
|
|
|
versions.discard('02')
|
|
|
|
versions.add('03')
|
|
|
|
return versions
|
|
|
|
|
|
|
|
def allsupportedversions(orig, ui):
|
|
|
|
versions = orig(ui)
|
|
|
|
versions.add('03')
|
|
|
|
return versions
|
|
|
|
|
|
|
|
def bypasscheckhash(self, text):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def readfromstore(self, text):
|
|
|
|
"""Read filelog content from local blobstore transform for flagprocessor.
|
|
|
|
|
|
|
|
Default tranform for flagprocessor, returning contents from blobstore.
|
|
|
|
Returns a 2-typle (text, validatehash) where validatehash is True as the
|
|
|
|
contents of the blobstore should be checked using checkhash.
|
|
|
|
"""
|
2017-04-05 02:08:36 +03:00
|
|
|
if self.opener.options['lfsbypass']:
|
|
|
|
return (text, False)
|
|
|
|
|
2017-03-31 02:41:46 +03:00
|
|
|
metadata = pointer.deserialize(text)
|
|
|
|
storeids = metadata.tostoreids()
|
2017-04-06 01:48:10 +03:00
|
|
|
store = self.opener.lfslocalblobstore
|
2017-03-31 02:41:46 +03:00
|
|
|
if not isinstance(storeids, list):
|
|
|
|
storeids = [storeids]
|
|
|
|
missing = filter(lambda id: not store.has(id), storeids)
|
|
|
|
if missing:
|
2017-04-06 01:48:10 +03:00
|
|
|
self.opener.lfsremoteblobstore.readbatch(missing, store)
|
2017-03-31 02:41:46 +03:00
|
|
|
text = ''.join([store.read(id) for id in storeids])
|
|
|
|
return (text, True)
|
2017-03-25 05:00:30 +03:00
|
|
|
|
|
|
|
def writetostore(self, text):
|
2017-04-05 02:08:36 +03:00
|
|
|
if self.opener.options['lfsbypass']:
|
|
|
|
return (text, False)
|
|
|
|
|
2017-03-25 05:00:30 +03:00
|
|
|
offset = 0
|
|
|
|
chunkoids = []
|
2017-04-05 02:10:29 +03:00
|
|
|
chunksize = self.opener.options['lfschunksize']
|
2017-03-25 05:00:30 +03:00
|
|
|
|
|
|
|
if not chunksize:
|
|
|
|
chunksize = len(text)
|
|
|
|
|
|
|
|
while offset < len(text):
|
2017-03-25 05:01:42 +03:00
|
|
|
chunk = text[offset:offset + chunksize] # Python handles overflows
|
2017-03-25 05:00:30 +03:00
|
|
|
chunklen = len(chunk)
|
|
|
|
# compute sha256 for git-lfs
|
|
|
|
sha = util.sha256(chunk)
|
|
|
|
# Store actual contents to local blobstore
|
|
|
|
storeid = blobstore.StoreID(sha, chunklen)
|
2017-04-06 01:48:10 +03:00
|
|
|
self.opener.lfslocalblobstore.write(storeid, chunk)
|
2017-03-25 05:00:30 +03:00
|
|
|
chunkoids.append(storeid)
|
|
|
|
offset += chunklen
|
|
|
|
|
|
|
|
# replace contents with metadata
|
|
|
|
metadata = pointer.ChunkingPointer(
|
|
|
|
chunks=[{'oid': v.oid, 'size': v.size} for v in chunkoids],
|
|
|
|
hashalgo='sha256',
|
|
|
|
size=len(text))
|
|
|
|
text = str(metadata)
|
|
|
|
|
|
|
|
return (text, False)
|
|
|
|
|
|
|
|
def addrevision(orig, self, text, transaction, link, p1, p2, cachedelta=None,
|
|
|
|
node=None, flags=revlog.REVIDX_DEFAULT_FLAGS):
|
2017-04-05 02:08:36 +03:00
|
|
|
# filelog.addrevision wrapper
|
|
|
|
if not self.opener.options['lfsbypass']:
|
2017-04-05 02:22:31 +03:00
|
|
|
threshold = self.opener.options['lfsthreshold']
|
2017-03-25 05:00:30 +03:00
|
|
|
|
2017-04-05 02:08:36 +03:00
|
|
|
if threshold and len(text) > threshold:
|
|
|
|
flags |= revlog.REVIDX_EXTSTORED
|
2017-03-25 05:00:30 +03:00
|
|
|
|
|
|
|
return orig(self, text, transaction, link, p1, p2, cachedelta=cachedelta,
|
|
|
|
node=node, flags=flags)
|
|
|
|
|
|
|
|
def prepush(pushop):
|
|
|
|
"""Prepush hook.
|
|
|
|
|
|
|
|
Read through the revisions to push, looking for filelog entries that can be
|
|
|
|
deserialized into metadata so that we can block the push on their upload to
|
|
|
|
the remote blobstore.
|
|
|
|
"""
|
|
|
|
repo = pushop.repo
|
|
|
|
ui = pushop.ui
|
|
|
|
remoterepo = pushop.remote.local()
|
|
|
|
|
|
|
|
# We beed to pass on the information to the remote about the threshold so
|
|
|
|
# that _peek_islargefile can mark the file as large file.
|
|
|
|
threshold = repo.svfs.options.get('lfsthreshold')
|
|
|
|
if threshold is not None:
|
|
|
|
remoterepo.svfs.options['lfsthreshold'] = threshold
|
|
|
|
|
2017-04-06 01:58:56 +03:00
|
|
|
if ui.verbose:
|
|
|
|
ui.write(_('lfs: computing set of blobs to upload\n'))
|
2017-03-25 05:00:30 +03:00
|
|
|
toupload = []
|
|
|
|
totalsize = 0
|
|
|
|
for i, n in enumerate(pushop.outgoing.missing):
|
|
|
|
ctx = repo[n]
|
|
|
|
files = set(ctx.files())
|
|
|
|
parents = [p for p in ctx.parents() if p != node.nullid]
|
|
|
|
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 f in files:
|
2017-03-31 02:41:46 +03:00
|
|
|
if f not in ctx:
|
|
|
|
continue
|
2017-03-25 05:00:30 +03:00
|
|
|
filectx = ctx[f]
|
|
|
|
flags = filectx.filelog().flags(filectx.filerev())
|
|
|
|
if flags & revlog.REVIDX_EXTSTORED != revlog.REVIDX_EXTSTORED:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
metadata = pointer.deserialize(ctx[f].rawdata())
|
|
|
|
totalsize += long(metadata['size'])
|
|
|
|
storeids = metadata.tostoreids()
|
|
|
|
if isinstance(storeids, list):
|
|
|
|
toupload.extend(storeids)
|
|
|
|
else:
|
|
|
|
toupload.append(storeids)
|
|
|
|
except pointer.PointerDeserializationError:
|
2017-03-25 05:01:42 +03:00
|
|
|
msg = _('lfs: could not deserialize pointer for file %s, '
|
|
|
|
'revision %s\n')
|
|
|
|
ui.write(msg % (f, filectx.filerev()))
|
2017-03-25 05:00:30 +03:00
|
|
|
raise
|
|
|
|
|
2017-04-06 01:58:56 +03:00
|
|
|
if not toupload:
|
|
|
|
return
|
|
|
|
|
|
|
|
if ui.verbose:
|
|
|
|
msg = _('lfs: uploading blobs to the remote (%s chunk(s), %s)\n')
|
|
|
|
ui.write(msg % (len(toupload), hgutil.bytecount(totalsize)))
|
|
|
|
|
2017-04-06 01:48:10 +03:00
|
|
|
remoteblob = repo.svfs.lfsremoteblobstore
|
|
|
|
remoteblob.writebatch(toupload, repo.svfs.lfslocalblobstore,
|
2017-03-25 05:01:42 +03:00
|
|
|
total=totalsize)
|