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