13 |
13 |
14 from mercurial import ( |
14 from mercurial import ( |
15 error, |
15 error, |
16 pycompat, |
16 pycompat, |
17 ) |
17 ) |
18 from mercurial.utils import ( |
18 from mercurial.utils import stringutil |
19 stringutil, |
19 |
20 ) |
|
21 |
20 |
22 class InvalidPointer(error.StorageError): |
21 class InvalidPointer(error.StorageError): |
23 pass |
22 pass |
|
23 |
24 |
24 |
25 class gitlfspointer(dict): |
25 class gitlfspointer(dict): |
26 VERSION = 'https://git-lfs.github.com/spec/v1' |
26 VERSION = 'https://git-lfs.github.com/spec/v1' |
27 |
27 |
28 def __init__(self, *args, **kwargs): |
28 def __init__(self, *args, **kwargs): |
32 |
32 |
33 @classmethod |
33 @classmethod |
34 def deserialize(cls, text): |
34 def deserialize(cls, text): |
35 try: |
35 try: |
36 return cls(l.split(' ', 1) for l in text.splitlines()).validate() |
36 return cls(l.split(' ', 1) for l in text.splitlines()).validate() |
37 except ValueError: # l.split returns 1 item instead of 2 |
37 except ValueError: # l.split returns 1 item instead of 2 |
38 raise InvalidPointer(_('cannot parse git-lfs text: %s') |
38 raise InvalidPointer( |
39 % stringutil.pprint(text)) |
39 _('cannot parse git-lfs text: %s') % stringutil.pprint(text) |
|
40 ) |
40 |
41 |
41 def serialize(self): |
42 def serialize(self): |
42 sortkeyfunc = lambda x: (x[0] != 'version', x) |
43 sortkeyfunc = lambda x: (x[0] != 'version', x) |
43 items = sorted(self.validate().iteritems(), key=sortkeyfunc) |
44 items = sorted(self.validate().iteritems(), key=sortkeyfunc) |
44 return ''.join('%s %s\n' % (k, v) for k, v in items) |
45 return ''.join('%s %s\n' % (k, v) for k, v in items) |
65 for k, v in self.iteritems(): |
66 for k, v in self.iteritems(): |
66 if k in self._requiredre: |
67 if k in self._requiredre: |
67 if not self._requiredre[k].match(v): |
68 if not self._requiredre[k].match(v): |
68 raise InvalidPointer( |
69 raise InvalidPointer( |
69 _('unexpected lfs pointer value: %s=%s') |
70 _('unexpected lfs pointer value: %s=%s') |
70 % (k, stringutil.pprint(v))) |
71 % (k, stringutil.pprint(v)) |
|
72 ) |
71 requiredcount += 1 |
73 requiredcount += 1 |
72 elif not self._keyre.match(k): |
74 elif not self._keyre.match(k): |
73 raise InvalidPointer(_('unexpected lfs pointer key: %s') % k) |
75 raise InvalidPointer(_('unexpected lfs pointer key: %s') % k) |
74 if not self._valuere.match(v): |
76 if not self._valuere.match(v): |
75 raise InvalidPointer(_('unexpected lfs pointer value: %s=%s') |
77 raise InvalidPointer( |
76 % (k, stringutil.pprint(v))) |
78 _('unexpected lfs pointer value: %s=%s') |
|
79 % (k, stringutil.pprint(v)) |
|
80 ) |
77 if len(self._requiredre) != requiredcount: |
81 if len(self._requiredre) != requiredcount: |
78 miss = sorted(set(self._requiredre.keys()).difference(self.keys())) |
82 miss = sorted(set(self._requiredre.keys()).difference(self.keys())) |
79 raise InvalidPointer(_('missing lfs pointer keys: %s') |
83 raise InvalidPointer( |
80 % ', '.join(miss)) |
84 _('missing lfs pointer keys: %s') % ', '.join(miss) |
|
85 ) |
81 return self |
86 return self |
82 |
87 |
|
88 |
83 deserialize = gitlfspointer.deserialize |
89 deserialize = gitlfspointer.deserialize |