##// END OF EJS Templates
exchange: refactor bundle specification parsing...
Gregory Szorc -
r26640:b13fdcc4 default
parent child Browse files
Show More
@@ -1242,7 +1242,13 b' def bundle(ui, repo, fname, dest=None, *'
1242 revs = scmutil.revrange(repo, opts['rev'])
1242 revs = scmutil.revrange(repo, opts['rev'])
1243
1243
1244 bundletype = opts.get('type', 'bzip2').lower()
1244 bundletype = opts.get('type', 'bzip2').lower()
1245 cgversion, bcompression = exchange.parsebundlespec(repo, bundletype)
1245 try:
1246 bcompression, cgversion = exchange.parsebundlespec(
1247 repo, bundletype, strict=False)
1248 except error.UnsupportedBundleSpecification as e:
1249 raise error.Abort(str(e),
1250 hint=_('see "hg help bundle" for supported '
1251 'values for --type'))
1246
1252
1247 if opts.get('all'):
1253 if opts.get('all'):
1248 base = ['null']
1254 base = ['null']
@@ -201,3 +201,12 b' class CensoredBaseError(RevlogError):'
201 operation which replaces the entire base with new content. This ensures
201 operation which replaces the entire base with new content. This ensures
202 the delta may be applied by clones which have not censored the base.
202 the delta may be applied by clones which have not censored the base.
203 """
203 """
204
205 class InvalidBundleSpecification(Exception):
206 """error raised when a bundle specification is invalid.
207
208 This is used for syntax errors as opposed to support errors.
209 """
210
211 class UnsupportedBundleSpecification(Exception):
212 """error raised when a bundle specification is not supported."""
@@ -15,58 +15,83 b' import streamclone'
15 import tags
15 import tags
16 import url as urlmod
16 import url as urlmod
17
17
18 _bundlecompspecs = {'none': None,
18 # Maps bundle compression human names to internal representation.
19 'bzip2': 'BZ',
19 _bundlespeccompressions = {'none': None,
20 'gzip': 'GZ',
20 'bzip2': 'BZ',
21 }
21 'gzip': 'GZ',
22 }
22
23
23 _bundleversionspecs = {'v1': '01',
24 # Maps bundle version human names to changegroup versions.
24 'v2': '02',
25 _bundlespeccgversions = {'v1': '01',
25 'bundle2': '02', #legacy
26 'v2': '02',
26 }
27 'bundle2': '02', #legacy
28 }
29
30 def parsebundlespec(repo, spec, strict=True):
31 """Parse a bundle string specification into parts.
32
33 Bundle specifications denote a well-defined bundle/exchange format.
34 The content of a given specification should not change over time in
35 order to ensure that bundles produced by a newer version of Mercurial are
36 readable from an older version.
37
38 The string currently has the form:
27
39
28 def parsebundlespec(repo, spec):
40 <compression>-<type>
29 """return the internal bundle type to use from a user input
41
42 Where <compression> is one of the supported compression formats
43 and <type> is (currently) a version string.
30
44
31 This is parsing user specified bundle type as accepted in:
45 If ``strict`` is True (the default) <compression> is required. Otherwise,
46 it is optional.
32
47
33 'hg bundle --type TYPE'.
48 Returns a 2-tuple of (compression, version). Compression will be ``None``
49 if not in strict mode and a compression isn't defined.
34
50
35 It accept format in the form [compression][-version]|[version]
51 An ``InvalidBundleSpecification`` is raised when the specification is
52 not syntactically well formed.
53
54 An ``UnsupportedBundleSpecification`` is raised when the compression or
55 bundle type/version is not recognized.
36
56
37 Consensus about extensions of the format for various bundle2 feature
57 Note: this function will likely eventually return a more complex data
38 is to prefix any feature with "+". eg "+treemanifest" or "gzip+phases"
58 structure, including bundle2 part information.
39 """
59 """
40 comp, version = None, None
60 if strict and '-' not in spec:
61 raise error.InvalidBundleSpecification(
62 _('invalid bundle specification; '
63 'must be prefixed with compression: %s') % spec)
41
64
42 if '-' in spec:
65 if '-' in spec:
43 comp, version = spec.split('-', 1)
66 compression, version = spec.split('-', 1)
44 elif spec in _bundlecompspecs:
45 comp = spec
46 elif spec in _bundleversionspecs:
47 version = spec
48 else:
49 raise error.Abort(_('unknown bundle type specified with --type'))
50
67
51 if comp is None:
68 if compression not in _bundlespeccompressions:
52 comp = 'BZ'
69 raise error.UnsupportedBundleSpecification(
70 _('%s compression is not supported') % compression)
71
72 if version not in _bundlespeccgversions:
73 raise error.UnsupportedBundleSpecification(
74 _('%s is not a recognized bundle version') % version)
53 else:
75 else:
54 try:
76 # Value could be just the compression or just the version, in which
55 comp = _bundlecompspecs[comp]
77 # case some defaults are assumed (but only when not in strict mode).
56 except KeyError:
78 assert not strict
57 raise error.Abort(_('unknown bundle type specified with --type'))
58
79
59 if version is None:
80 if spec in _bundlespeccompressions:
60 version = '01'
81 compression = spec
61 if 'generaldelta' in repo.requirements:
82 version = 'v1'
62 version = '02'
83 if 'generaldelta' in repo.requirements:
63 else:
84 version = 'v2'
64 try:
85 elif spec in _bundlespeccgversions:
65 version = _bundleversionspecs[version]
86 compression = 'bzip2'
66 except KeyError:
87 version = spec
67 raise error.Abort(_('unknown bundle type specified with --type'))
88 else:
89 raise error.UnsupportedBundleSpecification(
90 _('%s is not a recognized bundle specification') % spec)
68
91
69 return version, comp
92 compression = _bundlespeccompressions[compression]
93 version = _bundlespeccgversions[version]
94 return compression, version
70
95
71 def readbundle(ui, fh, fname, vfs=None):
96 def readbundle(ui, fh, fname, vfs=None):
72 header = changegroup.readexactly(fh, 4)
97 header = changegroup.readexactly(fh, 4)
@@ -102,6 +102,7 b' test invalid bundle type'
102
102
103 $ cd t1
103 $ cd t1
104 $ hg bundle -a -t garbage ../bgarbage
104 $ hg bundle -a -t garbage ../bgarbage
105 abort: unknown bundle type specified with --type
105 abort: garbage is not a recognized bundle specification
106 (see "hg help bundle" for supported values for --type)
106 [255]
107 [255]
107 $ cd ..
108 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now