Mercurial > hg
view hgext/lfs/pointer.py @ 35506:fa865878a849
lfs: show a friendly message when pushing lfs to a server without lfs enabled
Upfront disclaimer: I don't know anything about the wire protocol, and this was
pretty much cargo-culted from largefiles, and then clonebundles, since it seems
more modern. I was surprised that exchange.push() will ensure all of the proper
requirements when exchanging between two local repos, but doesn't care when one
is remote.
All this new capability marker does is inform the client that the extension is
enabled remotely. It may or may not contain commits with external blobs.
Open issues:
- largefiles uses 'largefiles=serve' for its capability. Someday I hope to
be able to push lfs blobs to an `hg serve` instance. That will probably
require a distinct capability. Should it change to '=serve' then? Or just
add an 'lfs-serve' capability then?
- The flip side of this is more complicated. It looks like largefiles adds an
'lheads' command for the client to signal to the server that the extension
is loaded. That is then converted to 'heads' and sent through the normal
wire protocol plumbing. A client using the 'heads' command directly is
kicked out with a message indicating that the largefiles extension must be
loaded. We could do similar with 'lfsheads', but then a repo with both
largefiles and lfs blobs can't be pushed over the wire. Hopefully somebody
with more wire protocol experience can think of something else. I see
'x-hgarg-1' on some commands in the tests, but not on heads, and didn't dig
any further.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 23 Dec 2017 17:49:12 -0500 |
parents | b8e5fb8d2389 |
children | 95bd9e396774 |
line wrap: on
line source
# pointer.py - Git-LFS pointer serialization # # Copyright 2017 Facebook, Inc. # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. from __future__ import absolute_import import re from mercurial.i18n import _ from mercurial import ( error, ) class InvalidPointer(error.RevlogError): pass class gitlfspointer(dict): VERSION = 'https://git-lfs.github.com/spec/v1' def __init__(self, *args, **kwargs): self['version'] = self.VERSION super(gitlfspointer, self).__init__(*args, **kwargs) @classmethod def deserialize(cls, text): try: return cls(l.split(' ', 1) for l in text.splitlines()).validate() except ValueError: # l.split returns 1 item instead of 2 raise InvalidPointer(_('cannot parse git-lfs text: %r') % text) def serialize(self): sortkeyfunc = lambda x: (x[0] != 'version', x) items = sorted(self.validate().iteritems(), key=sortkeyfunc) return ''.join('%s %s\n' % (k, v) for k, v in items) def oid(self): return self['oid'].split(':')[-1] def size(self): return int(self['size']) # regular expressions used by _validate # see https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md _keyre = re.compile(r'\A[a-z0-9.-]+\Z') _valuere = re.compile(r'\A[^\n]*\Z') _requiredre = { 'size': re.compile(r'\A[0-9]+\Z'), 'oid': re.compile(r'\Asha256:[0-9a-f]{64}\Z'), 'version': re.compile(r'\A%s\Z' % re.escape(VERSION)), } def validate(self): """raise InvalidPointer on error. return self if there is no error""" requiredcount = 0 for k, v in self.iteritems(): if k in self._requiredre: if not self._requiredre[k].match(v): raise InvalidPointer(_('unexpected value: %s=%r') % (k, v)) requiredcount += 1 elif not self._keyre.match(k): raise InvalidPointer(_('unexpected key: %s') % k) if not self._valuere.match(v): raise InvalidPointer(_('unexpected value: %s=%r') % (k, v)) if len(self._requiredre) != requiredcount: miss = sorted(set(self._requiredre.keys()).difference(self.keys())) raise InvalidPointer(_('missed keys: %s') % ', '.join(miss)) return self deserialize = gitlfspointer.deserialize