Show More
@@ -1,75 +1,82 b'' | |||||
1 | # pointer.py - Git-LFS pointer serialization |
|
1 | # pointer.py - Git-LFS pointer serialization | |
2 | # |
|
2 | # | |
3 | # Copyright 2017 Facebook, Inc. |
|
3 | # Copyright 2017 Facebook, Inc. | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import re |
|
10 | import re | |
11 |
|
11 | |||
12 | from mercurial.i18n import _ |
|
12 | from mercurial.i18n import _ | |
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 ( | |||
|
19 | stringutil, | |||
|
20 | ) | |||
18 |
|
21 | |||
19 | class InvalidPointer(error.RevlogError): |
|
22 | class InvalidPointer(error.RevlogError): | |
20 | pass |
|
23 | pass | |
21 |
|
24 | |||
22 | class gitlfspointer(dict): |
|
25 | class gitlfspointer(dict): | |
23 | VERSION = 'https://git-lfs.github.com/spec/v1' |
|
26 | VERSION = 'https://git-lfs.github.com/spec/v1' | |
24 |
|
27 | |||
25 | def __init__(self, *args, **kwargs): |
|
28 | def __init__(self, *args, **kwargs): | |
26 | self['version'] = self.VERSION |
|
29 | self['version'] = self.VERSION | |
27 | super(gitlfspointer, self).__init__(*args) |
|
30 | super(gitlfspointer, self).__init__(*args) | |
28 | self.update(pycompat.byteskwargs(kwargs)) |
|
31 | self.update(pycompat.byteskwargs(kwargs)) | |
29 |
|
32 | |||
30 | @classmethod |
|
33 | @classmethod | |
31 | def deserialize(cls, text): |
|
34 | def deserialize(cls, text): | |
32 | try: |
|
35 | try: | |
33 | return cls(l.split(' ', 1) for l in text.splitlines()).validate() |
|
36 | return cls(l.split(' ', 1) for l in text.splitlines()).validate() | |
34 | except ValueError: # l.split returns 1 item instead of 2 |
|
37 | except ValueError: # l.split returns 1 item instead of 2 | |
35 |
raise InvalidPointer( |
|
38 | raise InvalidPointer( | |
|
39 | _('cannot parse git-lfs text: %s') % stringutil.pprint( | |||
|
40 | text, bprefix=False)) | |||
36 |
|
41 | |||
37 | def serialize(self): |
|
42 | def serialize(self): | |
38 | sortkeyfunc = lambda x: (x[0] != 'version', x) |
|
43 | sortkeyfunc = lambda x: (x[0] != 'version', x) | |
39 | items = sorted(self.validate().iteritems(), key=sortkeyfunc) |
|
44 | items = sorted(self.validate().iteritems(), key=sortkeyfunc) | |
40 | 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) | |
41 |
|
46 | |||
42 | def oid(self): |
|
47 | def oid(self): | |
43 | return self['oid'].split(':')[-1] |
|
48 | return self['oid'].split(':')[-1] | |
44 |
|
49 | |||
45 | def size(self): |
|
50 | def size(self): | |
46 | return int(self['size']) |
|
51 | return int(self['size']) | |
47 |
|
52 | |||
48 | # regular expressions used by _validate |
|
53 | # regular expressions used by _validate | |
49 | # see https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md |
|
54 | # see https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md | |
50 | _keyre = re.compile(br'\A[a-z0-9.-]+\Z') |
|
55 | _keyre = re.compile(br'\A[a-z0-9.-]+\Z') | |
51 | _valuere = re.compile(br'\A[^\n]*\Z') |
|
56 | _valuere = re.compile(br'\A[^\n]*\Z') | |
52 | _requiredre = { |
|
57 | _requiredre = { | |
53 | 'size': re.compile(br'\A[0-9]+\Z'), |
|
58 | 'size': re.compile(br'\A[0-9]+\Z'), | |
54 | 'oid': re.compile(br'\Asha256:[0-9a-f]{64}\Z'), |
|
59 | 'oid': re.compile(br'\Asha256:[0-9a-f]{64}\Z'), | |
55 | 'version': re.compile(br'\A%s\Z' % re.escape(VERSION)), |
|
60 | 'version': re.compile(br'\A%s\Z' % re.escape(VERSION)), | |
56 | } |
|
61 | } | |
57 |
|
62 | |||
58 | def validate(self): |
|
63 | def validate(self): | |
59 | """raise InvalidPointer on error. return self if there is no error""" |
|
64 | """raise InvalidPointer on error. return self if there is no error""" | |
60 | requiredcount = 0 |
|
65 | requiredcount = 0 | |
61 | for k, v in self.iteritems(): |
|
66 | for k, v in self.iteritems(): | |
62 | if k in self._requiredre: |
|
67 | if k in self._requiredre: | |
63 | if not self._requiredre[k].match(v): |
|
68 | if not self._requiredre[k].match(v): | |
64 |
raise InvalidPointer(_('unexpected value: %s=% |
|
69 | raise InvalidPointer(_('unexpected value: %s=%s') % ( | |
|
70 | k, stringutil.pprint(v, bprefix=False))) | |||
65 | requiredcount += 1 |
|
71 | requiredcount += 1 | |
66 | elif not self._keyre.match(k): |
|
72 | elif not self._keyre.match(k): | |
67 | raise InvalidPointer(_('unexpected key: %s') % k) |
|
73 | raise InvalidPointer(_('unexpected key: %s') % k) | |
68 | if not self._valuere.match(v): |
|
74 | if not self._valuere.match(v): | |
69 |
raise InvalidPointer(_('unexpected value: %s=% |
|
75 | raise InvalidPointer(_('unexpected value: %s=%s') % ( | |
|
76 | k, stringutil.pprint(v, bprefix=False))) | |||
70 | if len(self._requiredre) != requiredcount: |
|
77 | if len(self._requiredre) != requiredcount: | |
71 | miss = sorted(set(self._requiredre.keys()).difference(self.keys())) |
|
78 | miss = sorted(set(self._requiredre.keys()).difference(self.keys())) | |
72 | raise InvalidPointer(_('missed keys: %s') % ', '.join(miss)) |
|
79 | raise InvalidPointer(_('missed keys: %s') % ', '.join(miss)) | |
73 | return self |
|
80 | return self | |
74 |
|
81 | |||
75 | deserialize = gitlfspointer.deserialize |
|
82 | deserialize = gitlfspointer.deserialize |
General Comments 0
You need to be logged in to leave comments.
Login now