##// END OF EJS Templates
clonebundles: add missing newline to legacy response...
Julien Cristau -
r52521:33bcb1dd default
parent child Browse files
Show More
@@ -1,803 +1,804 b''
1 # wireprotov1server.py - Wire protocol version 1 server functionality
1 # wireprotov1server.py - Wire protocol version 1 server functionality
2 #
2 #
3 # Copyright 2005-2010 Olivia Mackall <olivia@selenic.com>
3 # Copyright 2005-2010 Olivia Mackall <olivia@selenic.com>
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
8
9 import binascii
9 import binascii
10 import os
10 import os
11
11
12 from .i18n import _
12 from .i18n import _
13 from .node import hex
13 from .node import hex
14
14
15 from . import (
15 from . import (
16 bundle2,
16 bundle2,
17 bundlecaches,
17 bundlecaches,
18 changegroup as changegroupmod,
18 changegroup as changegroupmod,
19 discovery,
19 discovery,
20 encoding,
20 encoding,
21 error,
21 error,
22 exchange,
22 exchange,
23 hook,
23 hook,
24 pushkey as pushkeymod,
24 pushkey as pushkeymod,
25 pycompat,
25 pycompat,
26 repoview,
26 repoview,
27 requirements as requirementsmod,
27 requirements as requirementsmod,
28 streamclone,
28 streamclone,
29 util,
29 util,
30 wireprototypes,
30 wireprototypes,
31 )
31 )
32
32
33 from .utils import (
33 from .utils import (
34 procutil,
34 procutil,
35 stringutil,
35 stringutil,
36 )
36 )
37
37
38 urlerr = util.urlerr
38 urlerr = util.urlerr
39 urlreq = util.urlreq
39 urlreq = util.urlreq
40
40
41 bundle2requiredmain = _(b'incompatible Mercurial client; bundle2 required')
41 bundle2requiredmain = _(b'incompatible Mercurial client; bundle2 required')
42 bundle2requiredhint = _(
42 bundle2requiredhint = _(
43 b'see https://www.mercurial-scm.org/wiki/IncompatibleClient'
43 b'see https://www.mercurial-scm.org/wiki/IncompatibleClient'
44 )
44 )
45 bundle2required = b'%s\n(%s)\n' % (bundle2requiredmain, bundle2requiredhint)
45 bundle2required = b'%s\n(%s)\n' % (bundle2requiredmain, bundle2requiredhint)
46
46
47
47
48 def clientcompressionsupport(proto):
48 def clientcompressionsupport(proto):
49 """Returns a list of compression methods supported by the client.
49 """Returns a list of compression methods supported by the client.
50
50
51 Returns a list of the compression methods supported by the client
51 Returns a list of the compression methods supported by the client
52 according to the protocol capabilities. If no such capability has
52 according to the protocol capabilities. If no such capability has
53 been announced, fallback to the default of zlib and uncompressed.
53 been announced, fallback to the default of zlib and uncompressed.
54 """
54 """
55 for cap in proto.getprotocaps():
55 for cap in proto.getprotocaps():
56 if cap.startswith(b'comp='):
56 if cap.startswith(b'comp='):
57 return cap[5:].split(b',')
57 return cap[5:].split(b',')
58 return [b'zlib', b'none']
58 return [b'zlib', b'none']
59
59
60
60
61 # wire protocol command can either return a string or one of these classes.
61 # wire protocol command can either return a string or one of these classes.
62
62
63
63
64 def getdispatchrepo(repo, proto, command, accesshidden=False):
64 def getdispatchrepo(repo, proto, command, accesshidden=False):
65 """Obtain the repo used for processing wire protocol commands.
65 """Obtain the repo used for processing wire protocol commands.
66
66
67 The intent of this function is to serve as a monkeypatch point for
67 The intent of this function is to serve as a monkeypatch point for
68 extensions that need commands to operate on different repo views under
68 extensions that need commands to operate on different repo views under
69 specialized circumstances.
69 specialized circumstances.
70 """
70 """
71 viewconfig = repo.ui.config(b'server', b'view')
71 viewconfig = repo.ui.config(b'server', b'view')
72
72
73 # Only works if the filter actually supports being upgraded to show hidden
73 # Only works if the filter actually supports being upgraded to show hidden
74 # changesets.
74 # changesets.
75 if (
75 if (
76 accesshidden
76 accesshidden
77 and viewconfig is not None
77 and viewconfig is not None
78 and viewconfig + b'.hidden' in repoview.filtertable
78 and viewconfig + b'.hidden' in repoview.filtertable
79 ):
79 ):
80 viewconfig += b'.hidden'
80 viewconfig += b'.hidden'
81
81
82 return repo.filtered(viewconfig)
82 return repo.filtered(viewconfig)
83
83
84
84
85 def dispatch(repo, proto, command, accesshidden=False):
85 def dispatch(repo, proto, command, accesshidden=False):
86 repo = getdispatchrepo(repo, proto, command, accesshidden=accesshidden)
86 repo = getdispatchrepo(repo, proto, command, accesshidden=accesshidden)
87
87
88 func, spec = commands[command]
88 func, spec = commands[command]
89 args = proto.getargs(spec)
89 args = proto.getargs(spec)
90
90
91 return func(repo, proto, *args)
91 return func(repo, proto, *args)
92
92
93
93
94 def options(cmd, keys, others):
94 def options(cmd, keys, others):
95 opts = {}
95 opts = {}
96 for k in keys:
96 for k in keys:
97 if k in others:
97 if k in others:
98 opts[k] = others[k]
98 opts[k] = others[k]
99 del others[k]
99 del others[k]
100 if others:
100 if others:
101 procutil.stderr.write(
101 procutil.stderr.write(
102 b"warning: %s ignored unexpected arguments %s\n"
102 b"warning: %s ignored unexpected arguments %s\n"
103 % (cmd, b",".join(others))
103 % (cmd, b",".join(others))
104 )
104 )
105 return opts
105 return opts
106
106
107
107
108 def bundle1allowed(repo, action):
108 def bundle1allowed(repo, action):
109 """Whether a bundle1 operation is allowed from the server.
109 """Whether a bundle1 operation is allowed from the server.
110
110
111 Priority is:
111 Priority is:
112
112
113 1. server.bundle1gd.<action> (if generaldelta active)
113 1. server.bundle1gd.<action> (if generaldelta active)
114 2. server.bundle1.<action>
114 2. server.bundle1.<action>
115 3. server.bundle1gd (if generaldelta active)
115 3. server.bundle1gd (if generaldelta active)
116 4. server.bundle1
116 4. server.bundle1
117 """
117 """
118 ui = repo.ui
118 ui = repo.ui
119 gd = requirementsmod.GENERALDELTA_REQUIREMENT in repo.requirements
119 gd = requirementsmod.GENERALDELTA_REQUIREMENT in repo.requirements
120
120
121 if gd:
121 if gd:
122 v = ui.configbool(b'server', b'bundle1gd.%s' % action)
122 v = ui.configbool(b'server', b'bundle1gd.%s' % action)
123 if v is not None:
123 if v is not None:
124 return v
124 return v
125
125
126 v = ui.configbool(b'server', b'bundle1.%s' % action)
126 v = ui.configbool(b'server', b'bundle1.%s' % action)
127 if v is not None:
127 if v is not None:
128 return v
128 return v
129
129
130 if gd:
130 if gd:
131 v = ui.configbool(b'server', b'bundle1gd')
131 v = ui.configbool(b'server', b'bundle1gd')
132 if v is not None:
132 if v is not None:
133 return v
133 return v
134
134
135 return ui.configbool(b'server', b'bundle1')
135 return ui.configbool(b'server', b'bundle1')
136
136
137
137
138 commands = wireprototypes.commanddict()
138 commands = wireprototypes.commanddict()
139
139
140
140
141 def wireprotocommand(name, args=None, permission=b'push'):
141 def wireprotocommand(name, args=None, permission=b'push'):
142 """Decorator to declare a wire protocol command.
142 """Decorator to declare a wire protocol command.
143
143
144 ``name`` is the name of the wire protocol command being provided.
144 ``name`` is the name of the wire protocol command being provided.
145
145
146 ``args`` defines the named arguments accepted by the command. It is
146 ``args`` defines the named arguments accepted by the command. It is
147 a space-delimited list of argument names. ``*`` denotes a special value
147 a space-delimited list of argument names. ``*`` denotes a special value
148 that says to accept all named arguments.
148 that says to accept all named arguments.
149
149
150 ``permission`` defines the permission type needed to run this command.
150 ``permission`` defines the permission type needed to run this command.
151 Can be ``push`` or ``pull``. These roughly map to read-write and read-only,
151 Can be ``push`` or ``pull``. These roughly map to read-write and read-only,
152 respectively. Default is to assume command requires ``push`` permissions
152 respectively. Default is to assume command requires ``push`` permissions
153 because otherwise commands not declaring their permissions could modify
153 because otherwise commands not declaring their permissions could modify
154 a repository that is supposed to be read-only.
154 a repository that is supposed to be read-only.
155 """
155 """
156 transports = {
156 transports = {
157 k for k, v in wireprototypes.TRANSPORTS.items() if v[b'version'] == 1
157 k for k, v in wireprototypes.TRANSPORTS.items() if v[b'version'] == 1
158 }
158 }
159
159
160 if permission not in (b'push', b'pull'):
160 if permission not in (b'push', b'pull'):
161 raise error.ProgrammingError(
161 raise error.ProgrammingError(
162 b'invalid wire protocol permission; '
162 b'invalid wire protocol permission; '
163 b'got %s; expected "push" or "pull"' % permission
163 b'got %s; expected "push" or "pull"' % permission
164 )
164 )
165
165
166 if args is None:
166 if args is None:
167 args = b''
167 args = b''
168
168
169 if not isinstance(args, bytes):
169 if not isinstance(args, bytes):
170 raise error.ProgrammingError(
170 raise error.ProgrammingError(
171 b'arguments for version 1 commands must be declared as bytes'
171 b'arguments for version 1 commands must be declared as bytes'
172 )
172 )
173
173
174 def register(func):
174 def register(func):
175 if name in commands:
175 if name in commands:
176 raise error.ProgrammingError(
176 raise error.ProgrammingError(
177 b'%s command already registered for version 1' % name
177 b'%s command already registered for version 1' % name
178 )
178 )
179 commands[name] = wireprototypes.commandentry(
179 commands[name] = wireprototypes.commandentry(
180 func, args=args, transports=transports, permission=permission
180 func, args=args, transports=transports, permission=permission
181 )
181 )
182
182
183 return func
183 return func
184
184
185 return register
185 return register
186
186
187
187
188 # TODO define a more appropriate permissions type to use for this.
188 # TODO define a more appropriate permissions type to use for this.
189 @wireprotocommand(b'batch', b'cmds *', permission=b'pull')
189 @wireprotocommand(b'batch', b'cmds *', permission=b'pull')
190 def batch(repo, proto, cmds, others):
190 def batch(repo, proto, cmds, others):
191 unescapearg = wireprototypes.unescapebatcharg
191 unescapearg = wireprototypes.unescapebatcharg
192 res = []
192 res = []
193 for pair in cmds.split(b';'):
193 for pair in cmds.split(b';'):
194 op, args = pair.split(b' ', 1)
194 op, args = pair.split(b' ', 1)
195 vals = {}
195 vals = {}
196 for a in args.split(b','):
196 for a in args.split(b','):
197 if a:
197 if a:
198 n, v = a.split(b'=')
198 n, v = a.split(b'=')
199 vals[unescapearg(n)] = unescapearg(v)
199 vals[unescapearg(n)] = unescapearg(v)
200 func, spec = commands[op]
200 func, spec = commands[op]
201
201
202 # Validate that client has permissions to perform this command.
202 # Validate that client has permissions to perform this command.
203 perm = commands[op].permission
203 perm = commands[op].permission
204 assert perm in (b'push', b'pull')
204 assert perm in (b'push', b'pull')
205 proto.checkperm(perm)
205 proto.checkperm(perm)
206
206
207 if spec:
207 if spec:
208 keys = spec.split()
208 keys = spec.split()
209 data = {}
209 data = {}
210 for k in keys:
210 for k in keys:
211 if k == b'*':
211 if k == b'*':
212 star = {}
212 star = {}
213 for key in vals.keys():
213 for key in vals.keys():
214 if key not in keys:
214 if key not in keys:
215 star[key] = vals[key]
215 star[key] = vals[key]
216 data[b'*'] = star
216 data[b'*'] = star
217 else:
217 else:
218 data[k] = vals[k]
218 data[k] = vals[k]
219 result = func(repo, proto, *[data[k] for k in keys])
219 result = func(repo, proto, *[data[k] for k in keys])
220 else:
220 else:
221 result = func(repo, proto)
221 result = func(repo, proto)
222 if isinstance(result, wireprototypes.ooberror):
222 if isinstance(result, wireprototypes.ooberror):
223 return result
223 return result
224
224
225 # For now, all batchable commands must return bytesresponse or
225 # For now, all batchable commands must return bytesresponse or
226 # raw bytes (for backwards compatibility).
226 # raw bytes (for backwards compatibility).
227 assert isinstance(result, (wireprototypes.bytesresponse, bytes))
227 assert isinstance(result, (wireprototypes.bytesresponse, bytes))
228 if isinstance(result, wireprototypes.bytesresponse):
228 if isinstance(result, wireprototypes.bytesresponse):
229 result = result.data
229 result = result.data
230 res.append(wireprototypes.escapebatcharg(result))
230 res.append(wireprototypes.escapebatcharg(result))
231
231
232 return wireprototypes.bytesresponse(b';'.join(res))
232 return wireprototypes.bytesresponse(b';'.join(res))
233
233
234
234
235 @wireprotocommand(b'between', b'pairs', permission=b'pull')
235 @wireprotocommand(b'between', b'pairs', permission=b'pull')
236 def between(repo, proto, pairs):
236 def between(repo, proto, pairs):
237 pairs = [wireprototypes.decodelist(p, b'-') for p in pairs.split(b" ")]
237 pairs = [wireprototypes.decodelist(p, b'-') for p in pairs.split(b" ")]
238 r = []
238 r = []
239 for b in repo.between(pairs):
239 for b in repo.between(pairs):
240 r.append(wireprototypes.encodelist(b) + b"\n")
240 r.append(wireprototypes.encodelist(b) + b"\n")
241
241
242 return wireprototypes.bytesresponse(b''.join(r))
242 return wireprototypes.bytesresponse(b''.join(r))
243
243
244
244
245 @wireprotocommand(b'branchmap', permission=b'pull')
245 @wireprotocommand(b'branchmap', permission=b'pull')
246 def branchmap(repo, proto):
246 def branchmap(repo, proto):
247 branchmap = repo.branchmap()
247 branchmap = repo.branchmap()
248 heads = []
248 heads = []
249 for branch, nodes in branchmap.items():
249 for branch, nodes in branchmap.items():
250 branchname = urlreq.quote(encoding.fromlocal(branch))
250 branchname = urlreq.quote(encoding.fromlocal(branch))
251 branchnodes = wireprototypes.encodelist(nodes)
251 branchnodes = wireprototypes.encodelist(nodes)
252 heads.append(b'%s %s' % (branchname, branchnodes))
252 heads.append(b'%s %s' % (branchname, branchnodes))
253
253
254 return wireprototypes.bytesresponse(b'\n'.join(heads))
254 return wireprototypes.bytesresponse(b'\n'.join(heads))
255
255
256
256
257 @wireprotocommand(b'branches', b'nodes', permission=b'pull')
257 @wireprotocommand(b'branches', b'nodes', permission=b'pull')
258 def branches(repo, proto, nodes):
258 def branches(repo, proto, nodes):
259 nodes = wireprototypes.decodelist(nodes)
259 nodes = wireprototypes.decodelist(nodes)
260 r = []
260 r = []
261 for b in repo.branches(nodes):
261 for b in repo.branches(nodes):
262 r.append(wireprototypes.encodelist(b) + b"\n")
262 r.append(wireprototypes.encodelist(b) + b"\n")
263
263
264 return wireprototypes.bytesresponse(b''.join(r))
264 return wireprototypes.bytesresponse(b''.join(r))
265
265
266
266
267 @wireprotocommand(b'get_cached_bundle_inline', b'path', permission=b'pull')
267 @wireprotocommand(b'get_cached_bundle_inline', b'path', permission=b'pull')
268 def get_cached_bundle_inline(repo, proto, path):
268 def get_cached_bundle_inline(repo, proto, path):
269 """
269 """
270 Server command to send a clonebundle to the client
270 Server command to send a clonebundle to the client
271 """
271 """
272 if hook.hashook(repo.ui, b'pretransmit-inline-clone-bundle'):
272 if hook.hashook(repo.ui, b'pretransmit-inline-clone-bundle'):
273 hook.hook(
273 hook.hook(
274 repo.ui,
274 repo.ui,
275 repo,
275 repo,
276 b'pretransmit-inline-clone-bundle',
276 b'pretransmit-inline-clone-bundle',
277 throw=True,
277 throw=True,
278 clonebundlepath=path,
278 clonebundlepath=path,
279 )
279 )
280
280
281 bundle_dir = repo.vfs.join(bundlecaches.BUNDLE_CACHE_DIR)
281 bundle_dir = repo.vfs.join(bundlecaches.BUNDLE_CACHE_DIR)
282 clonebundlepath = repo.vfs.join(bundle_dir, path)
282 clonebundlepath = repo.vfs.join(bundle_dir, path)
283 if not repo.vfs.exists(clonebundlepath):
283 if not repo.vfs.exists(clonebundlepath):
284 raise error.Abort(b'clonebundle %s does not exist' % path)
284 raise error.Abort(b'clonebundle %s does not exist' % path)
285
285
286 clonebundles_dir = os.path.realpath(bundle_dir)
286 clonebundles_dir = os.path.realpath(bundle_dir)
287 if not os.path.realpath(clonebundlepath).startswith(clonebundles_dir):
287 if not os.path.realpath(clonebundlepath).startswith(clonebundles_dir):
288 raise error.Abort(b'clonebundle %s is using an illegal path' % path)
288 raise error.Abort(b'clonebundle %s is using an illegal path' % path)
289
289
290 def generator(vfs, bundle_path):
290 def generator(vfs, bundle_path):
291 with vfs(bundle_path) as f:
291 with vfs(bundle_path) as f:
292 length = os.fstat(f.fileno())[6]
292 length = os.fstat(f.fileno())[6]
293 yield util.uvarintencode(length)
293 yield util.uvarintencode(length)
294 for chunk in util.filechunkiter(f):
294 for chunk in util.filechunkiter(f):
295 yield chunk
295 yield chunk
296
296
297 stream = generator(repo.vfs, clonebundlepath)
297 stream = generator(repo.vfs, clonebundlepath)
298 return wireprototypes.streamres(gen=stream, prefer_uncompressed=True)
298 return wireprototypes.streamres(gen=stream, prefer_uncompressed=True)
299
299
300
300
301 @wireprotocommand(b'clonebundles', b'', permission=b'pull')
301 @wireprotocommand(b'clonebundles', b'', permission=b'pull')
302 def clonebundles(repo, proto):
302 def clonebundles(repo, proto):
303 """A legacy version of clonebundles_manifest
303 """A legacy version of clonebundles_manifest
304
304
305 This version filtered out new url scheme (like peer-bundle-cache://) to
305 This version filtered out new url scheme (like peer-bundle-cache://) to
306 avoid confusion in older clients.
306 avoid confusion in older clients.
307 """
307 """
308 manifest_contents = bundlecaches.get_manifest(repo)
308 manifest_contents = bundlecaches.get_manifest(repo)
309 # Filter out peer-bundle-cache:// entries
309 # Filter out peer-bundle-cache:// entries
310 modified_manifest = []
310 modified_manifest = []
311 for line in manifest_contents.splitlines():
311 for line in manifest_contents.splitlines():
312 if line.startswith(bundlecaches.CLONEBUNDLESCHEME):
312 if line.startswith(bundlecaches.CLONEBUNDLESCHEME):
313 continue
313 continue
314 modified_manifest.append(line)
314 modified_manifest.append(line)
315 modified_manifest.append(b'')
315 return wireprototypes.bytesresponse(b'\n'.join(modified_manifest))
316 return wireprototypes.bytesresponse(b'\n'.join(modified_manifest))
316
317
317
318
318 @wireprotocommand(b'clonebundles_manifest', b'*', permission=b'pull')
319 @wireprotocommand(b'clonebundles_manifest', b'*', permission=b'pull')
319 def clonebundles_2(repo, proto, args):
320 def clonebundles_2(repo, proto, args):
320 """Server command for returning info for available bundles to seed clones.
321 """Server command for returning info for available bundles to seed clones.
321
322
322 Clients will parse this response and determine what bundle to fetch.
323 Clients will parse this response and determine what bundle to fetch.
323
324
324 Extensions may wrap this command to filter or dynamically emit data
325 Extensions may wrap this command to filter or dynamically emit data
325 depending on the request. e.g. you could advertise URLs for the closest
326 depending on the request. e.g. you could advertise URLs for the closest
326 data center given the client's IP address.
327 data center given the client's IP address.
327
328
328 The only filter on the server side is filtering out inline clonebundles
329 The only filter on the server side is filtering out inline clonebundles
329 in case a client does not support them.
330 in case a client does not support them.
330 Otherwise, older clients would retrieve and error out on those.
331 Otherwise, older clients would retrieve and error out on those.
331 """
332 """
332 manifest_contents = bundlecaches.get_manifest(repo)
333 manifest_contents = bundlecaches.get_manifest(repo)
333 return wireprototypes.bytesresponse(manifest_contents)
334 return wireprototypes.bytesresponse(manifest_contents)
334
335
335
336
336 wireprotocaps = [
337 wireprotocaps = [
337 b'lookup',
338 b'lookup',
338 b'branchmap',
339 b'branchmap',
339 b'pushkey',
340 b'pushkey',
340 b'known',
341 b'known',
341 b'getbundle',
342 b'getbundle',
342 b'unbundlehash',
343 b'unbundlehash',
343 ]
344 ]
344
345
345
346
346 def _capabilities(repo, proto):
347 def _capabilities(repo, proto):
347 """return a list of capabilities for a repo
348 """return a list of capabilities for a repo
348
349
349 This function exists to allow extensions to easily wrap capabilities
350 This function exists to allow extensions to easily wrap capabilities
350 computation
351 computation
351
352
352 - returns a lists: easy to alter
353 - returns a lists: easy to alter
353 - change done here will be propagated to both `capabilities` and `hello`
354 - change done here will be propagated to both `capabilities` and `hello`
354 command without any other action needed.
355 command without any other action needed.
355 """
356 """
356 # copy to prevent modification of the global list
357 # copy to prevent modification of the global list
357 caps = list(wireprotocaps)
358 caps = list(wireprotocaps)
358
359
359 # Command of same name as capability isn't exposed to version 1 of
360 # Command of same name as capability isn't exposed to version 1 of
360 # transports. So conditionally add it.
361 # transports. So conditionally add it.
361 if commands.commandavailable(b'changegroupsubset', proto):
362 if commands.commandavailable(b'changegroupsubset', proto):
362 caps.append(b'changegroupsubset')
363 caps.append(b'changegroupsubset')
363
364
364 if streamclone.allowservergeneration(repo):
365 if streamclone.allowservergeneration(repo):
365 if repo.ui.configbool(b'server', b'preferuncompressed'):
366 if repo.ui.configbool(b'server', b'preferuncompressed'):
366 caps.append(b'stream-preferred')
367 caps.append(b'stream-preferred')
367 requiredformats = streamclone.streamed_requirements(repo)
368 requiredformats = streamclone.streamed_requirements(repo)
368 # if our local revlogs are just revlogv1, add 'stream' cap
369 # if our local revlogs are just revlogv1, add 'stream' cap
369 if not requiredformats - {requirementsmod.REVLOGV1_REQUIREMENT}:
370 if not requiredformats - {requirementsmod.REVLOGV1_REQUIREMENT}:
370 caps.append(b'stream')
371 caps.append(b'stream')
371 # otherwise, add 'streamreqs' detailing our local revlog format
372 # otherwise, add 'streamreqs' detailing our local revlog format
372 else:
373 else:
373 caps.append(b'streamreqs=%s' % b','.join(sorted(requiredformats)))
374 caps.append(b'streamreqs=%s' % b','.join(sorted(requiredformats)))
374 if repo.ui.configbool(b'experimental', b'bundle2-advertise'):
375 if repo.ui.configbool(b'experimental', b'bundle2-advertise'):
375 capsblob = bundle2.encodecaps(bundle2.getrepocaps(repo, role=b'server'))
376 capsblob = bundle2.encodecaps(bundle2.getrepocaps(repo, role=b'server'))
376 caps.append(b'bundle2=' + urlreq.quote(capsblob))
377 caps.append(b'bundle2=' + urlreq.quote(capsblob))
377 caps.append(b'unbundle=%s' % b','.join(bundle2.bundlepriority))
378 caps.append(b'unbundle=%s' % b','.join(bundle2.bundlepriority))
378
379
379 if repo.ui.configbool(b'experimental', b'narrow'):
380 if repo.ui.configbool(b'experimental', b'narrow'):
380 caps.append(wireprototypes.NARROWCAP)
381 caps.append(wireprototypes.NARROWCAP)
381 if repo.ui.configbool(b'experimental', b'narrowservebrokenellipses'):
382 if repo.ui.configbool(b'experimental', b'narrowservebrokenellipses'):
382 caps.append(wireprototypes.ELLIPSESCAP)
383 caps.append(wireprototypes.ELLIPSESCAP)
383
384
384 return proto.addcapabilities(repo, caps)
385 return proto.addcapabilities(repo, caps)
385
386
386
387
387 # If you are writing an extension and consider wrapping this function. Wrap
388 # If you are writing an extension and consider wrapping this function. Wrap
388 # `_capabilities` instead.
389 # `_capabilities` instead.
389 @wireprotocommand(b'capabilities', permission=b'pull')
390 @wireprotocommand(b'capabilities', permission=b'pull')
390 def capabilities(repo, proto):
391 def capabilities(repo, proto):
391 caps = _capabilities(repo, proto)
392 caps = _capabilities(repo, proto)
392 return wireprototypes.bytesresponse(b' '.join(sorted(caps)))
393 return wireprototypes.bytesresponse(b' '.join(sorted(caps)))
393
394
394
395
395 @wireprotocommand(b'changegroup', b'roots', permission=b'pull')
396 @wireprotocommand(b'changegroup', b'roots', permission=b'pull')
396 def changegroup(repo, proto, roots):
397 def changegroup(repo, proto, roots):
397 nodes = wireprototypes.decodelist(roots)
398 nodes = wireprototypes.decodelist(roots)
398 outgoing = discovery.outgoing(
399 outgoing = discovery.outgoing(
399 repo, missingroots=nodes, ancestorsof=repo.heads()
400 repo, missingroots=nodes, ancestorsof=repo.heads()
400 )
401 )
401 cg = changegroupmod.makechangegroup(repo, outgoing, b'01', b'serve')
402 cg = changegroupmod.makechangegroup(repo, outgoing, b'01', b'serve')
402 gen = iter(lambda: cg.read(32768), b'')
403 gen = iter(lambda: cg.read(32768), b'')
403 return wireprototypes.streamres(gen=gen)
404 return wireprototypes.streamres(gen=gen)
404
405
405
406
406 @wireprotocommand(b'changegroupsubset', b'bases heads', permission=b'pull')
407 @wireprotocommand(b'changegroupsubset', b'bases heads', permission=b'pull')
407 def changegroupsubset(repo, proto, bases, heads):
408 def changegroupsubset(repo, proto, bases, heads):
408 bases = wireprototypes.decodelist(bases)
409 bases = wireprototypes.decodelist(bases)
409 heads = wireprototypes.decodelist(heads)
410 heads = wireprototypes.decodelist(heads)
410 outgoing = discovery.outgoing(repo, missingroots=bases, ancestorsof=heads)
411 outgoing = discovery.outgoing(repo, missingroots=bases, ancestorsof=heads)
411 cg = changegroupmod.makechangegroup(repo, outgoing, b'01', b'serve')
412 cg = changegroupmod.makechangegroup(repo, outgoing, b'01', b'serve')
412 gen = iter(lambda: cg.read(32768), b'')
413 gen = iter(lambda: cg.read(32768), b'')
413 return wireprototypes.streamres(gen=gen)
414 return wireprototypes.streamres(gen=gen)
414
415
415
416
416 @wireprotocommand(b'debugwireargs', b'one two *', permission=b'pull')
417 @wireprotocommand(b'debugwireargs', b'one two *', permission=b'pull')
417 def debugwireargs(repo, proto, one, two, others):
418 def debugwireargs(repo, proto, one, two, others):
418 # only accept optional args from the known set
419 # only accept optional args from the known set
419 opts = options(b'debugwireargs', [b'three', b'four'], others)
420 opts = options(b'debugwireargs', [b'three', b'four'], others)
420 return wireprototypes.bytesresponse(
421 return wireprototypes.bytesresponse(
421 repo.debugwireargs(one, two, **pycompat.strkwargs(opts))
422 repo.debugwireargs(one, two, **pycompat.strkwargs(opts))
422 )
423 )
423
424
424
425
425 def find_pullbundle(repo, proto, opts, clheads, heads, common):
426 def find_pullbundle(repo, proto, opts, clheads, heads, common):
426 """Return a file object for the first matching pullbundle.
427 """Return a file object for the first matching pullbundle.
427
428
428 Pullbundles are specified in .hg/pullbundles.manifest similar to
429 Pullbundles are specified in .hg/pullbundles.manifest similar to
429 clonebundles.
430 clonebundles.
430 For each entry, the bundle specification is checked for compatibility:
431 For each entry, the bundle specification is checked for compatibility:
431 - Client features vs the BUNDLESPEC.
432 - Client features vs the BUNDLESPEC.
432 - Revisions shared with the clients vs base revisions of the bundle.
433 - Revisions shared with the clients vs base revisions of the bundle.
433 A bundle can be applied only if all its base revisions are known by
434 A bundle can be applied only if all its base revisions are known by
434 the client.
435 the client.
435 - At least one leaf of the bundle's DAG is missing on the client.
436 - At least one leaf of the bundle's DAG is missing on the client.
436 - Every leaf of the bundle's DAG is part of node set the client wants.
437 - Every leaf of the bundle's DAG is part of node set the client wants.
437 E.g. do not send a bundle of all changes if the client wants only
438 E.g. do not send a bundle of all changes if the client wants only
438 one specific branch of many.
439 one specific branch of many.
439 """
440 """
440
441
441 def decodehexstring(s):
442 def decodehexstring(s):
442 return {binascii.unhexlify(h) for h in s.split(b';')}
443 return {binascii.unhexlify(h) for h in s.split(b';')}
443
444
444 manifest = repo.vfs.tryread(b'pullbundles.manifest')
445 manifest = repo.vfs.tryread(b'pullbundles.manifest')
445 if not manifest:
446 if not manifest:
446 return None
447 return None
447 res = bundlecaches.parseclonebundlesmanifest(repo, manifest)
448 res = bundlecaches.parseclonebundlesmanifest(repo, manifest)
448 res = bundlecaches.filterclonebundleentries(repo, res, pullbundles=True)
449 res = bundlecaches.filterclonebundleentries(repo, res, pullbundles=True)
449 if not res:
450 if not res:
450 return None
451 return None
451 cl = repo.unfiltered().changelog
452 cl = repo.unfiltered().changelog
452 heads_anc = cl.ancestors([cl.rev(rev) for rev in heads], inclusive=True)
453 heads_anc = cl.ancestors([cl.rev(rev) for rev in heads], inclusive=True)
453 common_anc = cl.ancestors([cl.rev(rev) for rev in common], inclusive=True)
454 common_anc = cl.ancestors([cl.rev(rev) for rev in common], inclusive=True)
454 compformats = clientcompressionsupport(proto)
455 compformats = clientcompressionsupport(proto)
455 for entry in res:
456 for entry in res:
456 comp = entry.get(b'COMPRESSION')
457 comp = entry.get(b'COMPRESSION')
457 altcomp = util.compengines._bundlenames.get(comp)
458 altcomp = util.compengines._bundlenames.get(comp)
458 if comp and comp not in compformats and altcomp not in compformats:
459 if comp and comp not in compformats and altcomp not in compformats:
459 continue
460 continue
460 # No test yet for VERSION, since V2 is supported by any client
461 # No test yet for VERSION, since V2 is supported by any client
461 # that advertises partial pulls
462 # that advertises partial pulls
462 if b'heads' in entry:
463 if b'heads' in entry:
463 try:
464 try:
464 bundle_heads = decodehexstring(entry[b'heads'])
465 bundle_heads = decodehexstring(entry[b'heads'])
465 except TypeError:
466 except TypeError:
466 # Bad heads entry
467 # Bad heads entry
467 continue
468 continue
468 if bundle_heads.issubset(common):
469 if bundle_heads.issubset(common):
469 continue # Nothing new
470 continue # Nothing new
470 if all(cl.rev(rev) in common_anc for rev in bundle_heads):
471 if all(cl.rev(rev) in common_anc for rev in bundle_heads):
471 continue # Still nothing new
472 continue # Still nothing new
472 if any(
473 if any(
473 cl.rev(rev) not in heads_anc and cl.rev(rev) not in common_anc
474 cl.rev(rev) not in heads_anc and cl.rev(rev) not in common_anc
474 for rev in bundle_heads
475 for rev in bundle_heads
475 ):
476 ):
476 continue
477 continue
477 if b'bases' in entry:
478 if b'bases' in entry:
478 try:
479 try:
479 bundle_bases = decodehexstring(entry[b'bases'])
480 bundle_bases = decodehexstring(entry[b'bases'])
480 except TypeError:
481 except TypeError:
481 # Bad bases entry
482 # Bad bases entry
482 continue
483 continue
483 if not all(cl.rev(rev) in common_anc for rev in bundle_bases):
484 if not all(cl.rev(rev) in common_anc for rev in bundle_bases):
484 continue
485 continue
485 path = entry[b'URL']
486 path = entry[b'URL']
486 repo.ui.debug(b'sending pullbundle "%s"\n' % path)
487 repo.ui.debug(b'sending pullbundle "%s"\n' % path)
487 try:
488 try:
488 return repo.vfs.open(path)
489 return repo.vfs.open(path)
489 except IOError:
490 except IOError:
490 repo.ui.debug(b'pullbundle "%s" not accessible\n' % path)
491 repo.ui.debug(b'pullbundle "%s" not accessible\n' % path)
491 continue
492 continue
492 return None
493 return None
493
494
494
495
495 @wireprotocommand(b'getbundle', b'*', permission=b'pull')
496 @wireprotocommand(b'getbundle', b'*', permission=b'pull')
496 def getbundle(repo, proto, others):
497 def getbundle(repo, proto, others):
497 opts = options(
498 opts = options(
498 b'getbundle', wireprototypes.GETBUNDLE_ARGUMENTS.keys(), others
499 b'getbundle', wireprototypes.GETBUNDLE_ARGUMENTS.keys(), others
499 )
500 )
500 for k, v in opts.items():
501 for k, v in opts.items():
501 keytype = wireprototypes.GETBUNDLE_ARGUMENTS[k]
502 keytype = wireprototypes.GETBUNDLE_ARGUMENTS[k]
502 if keytype == b'nodes':
503 if keytype == b'nodes':
503 opts[k] = wireprototypes.decodelist(v)
504 opts[k] = wireprototypes.decodelist(v)
504 elif keytype == b'csv':
505 elif keytype == b'csv':
505 opts[k] = list(v.split(b','))
506 opts[k] = list(v.split(b','))
506 elif keytype == b'scsv':
507 elif keytype == b'scsv':
507 opts[k] = set(v.split(b','))
508 opts[k] = set(v.split(b','))
508 elif keytype == b'boolean':
509 elif keytype == b'boolean':
509 # Client should serialize False as '0', which is a non-empty string
510 # Client should serialize False as '0', which is a non-empty string
510 # so it evaluates as a True bool.
511 # so it evaluates as a True bool.
511 if v == b'0':
512 if v == b'0':
512 opts[k] = False
513 opts[k] = False
513 else:
514 else:
514 opts[k] = bool(v)
515 opts[k] = bool(v)
515 elif keytype != b'plain':
516 elif keytype != b'plain':
516 raise KeyError(b'unknown getbundle option type %s' % keytype)
517 raise KeyError(b'unknown getbundle option type %s' % keytype)
517
518
518 if not bundle1allowed(repo, b'pull'):
519 if not bundle1allowed(repo, b'pull'):
519 if not exchange.bundle2requested(opts.get(b'bundlecaps')):
520 if not exchange.bundle2requested(opts.get(b'bundlecaps')):
520 if proto.name == b'http-v1':
521 if proto.name == b'http-v1':
521 return wireprototypes.ooberror(bundle2required)
522 return wireprototypes.ooberror(bundle2required)
522 raise error.Abort(bundle2requiredmain, hint=bundle2requiredhint)
523 raise error.Abort(bundle2requiredmain, hint=bundle2requiredhint)
523
524
524 try:
525 try:
525 clheads = set(repo.changelog.heads())
526 clheads = set(repo.changelog.heads())
526 heads = set(opts.get(b'heads', set()))
527 heads = set(opts.get(b'heads', set()))
527 common = set(opts.get(b'common', set()))
528 common = set(opts.get(b'common', set()))
528 common.discard(repo.nullid)
529 common.discard(repo.nullid)
529 if (
530 if (
530 repo.ui.configbool(b'server', b'pullbundle')
531 repo.ui.configbool(b'server', b'pullbundle')
531 and b'partial-pull' in proto.getprotocaps()
532 and b'partial-pull' in proto.getprotocaps()
532 ):
533 ):
533 # Check if a pre-built bundle covers this request.
534 # Check if a pre-built bundle covers this request.
534 bundle = find_pullbundle(repo, proto, opts, clheads, heads, common)
535 bundle = find_pullbundle(repo, proto, opts, clheads, heads, common)
535 if bundle:
536 if bundle:
536 return wireprototypes.streamres(
537 return wireprototypes.streamres(
537 gen=util.filechunkiter(bundle), prefer_uncompressed=True
538 gen=util.filechunkiter(bundle), prefer_uncompressed=True
538 )
539 )
539
540
540 if repo.ui.configbool(b'server', b'disablefullbundle'):
541 if repo.ui.configbool(b'server', b'disablefullbundle'):
541 # Check to see if this is a full clone.
542 # Check to see if this is a full clone.
542 changegroup = opts.get(b'cg', True)
543 changegroup = opts.get(b'cg', True)
543 if changegroup and not common and clheads == heads:
544 if changegroup and not common and clheads == heads:
544 raise error.Abort(
545 raise error.Abort(
545 _(b'server has pull-based clones disabled'),
546 _(b'server has pull-based clones disabled'),
546 hint=_(b'remove --pull if specified or upgrade Mercurial'),
547 hint=_(b'remove --pull if specified or upgrade Mercurial'),
547 )
548 )
548
549
549 info, chunks = exchange.getbundlechunks(
550 info, chunks = exchange.getbundlechunks(
550 repo, b'serve', **pycompat.strkwargs(opts)
551 repo, b'serve', **pycompat.strkwargs(opts)
551 )
552 )
552 prefercompressed = info.get(b'prefercompressed', True)
553 prefercompressed = info.get(b'prefercompressed', True)
553 except error.Abort as exc:
554 except error.Abort as exc:
554 # cleanly forward Abort error to the client
555 # cleanly forward Abort error to the client
555 if not exchange.bundle2requested(opts.get(b'bundlecaps')):
556 if not exchange.bundle2requested(opts.get(b'bundlecaps')):
556 if proto.name == b'http-v1':
557 if proto.name == b'http-v1':
557 return wireprototypes.ooberror(exc.message + b'\n')
558 return wireprototypes.ooberror(exc.message + b'\n')
558 raise # cannot do better for bundle1 + ssh
559 raise # cannot do better for bundle1 + ssh
559 # bundle2 request expect a bundle2 reply
560 # bundle2 request expect a bundle2 reply
560 bundler = bundle2.bundle20(repo.ui)
561 bundler = bundle2.bundle20(repo.ui)
561 manargs = [(b'message', exc.message)]
562 manargs = [(b'message', exc.message)]
562 advargs = []
563 advargs = []
563 if exc.hint is not None:
564 if exc.hint is not None:
564 advargs.append((b'hint', exc.hint))
565 advargs.append((b'hint', exc.hint))
565 bundler.addpart(bundle2.bundlepart(b'error:abort', manargs, advargs))
566 bundler.addpart(bundle2.bundlepart(b'error:abort', manargs, advargs))
566 chunks = bundler.getchunks()
567 chunks = bundler.getchunks()
567 prefercompressed = False
568 prefercompressed = False
568
569
569 return wireprototypes.streamres(
570 return wireprototypes.streamres(
570 gen=chunks, prefer_uncompressed=not prefercompressed
571 gen=chunks, prefer_uncompressed=not prefercompressed
571 )
572 )
572
573
573
574
574 @wireprotocommand(b'heads', permission=b'pull')
575 @wireprotocommand(b'heads', permission=b'pull')
575 def heads(repo, proto):
576 def heads(repo, proto):
576 h = repo.heads()
577 h = repo.heads()
577 return wireprototypes.bytesresponse(wireprototypes.encodelist(h) + b'\n')
578 return wireprototypes.bytesresponse(wireprototypes.encodelist(h) + b'\n')
578
579
579
580
580 @wireprotocommand(b'hello', permission=b'pull')
581 @wireprotocommand(b'hello', permission=b'pull')
581 def hello(repo, proto):
582 def hello(repo, proto):
582 """Called as part of SSH handshake to obtain server info.
583 """Called as part of SSH handshake to obtain server info.
583
584
584 Returns a list of lines describing interesting things about the
585 Returns a list of lines describing interesting things about the
585 server, in an RFC822-like format.
586 server, in an RFC822-like format.
586
587
587 Currently, the only one defined is ``capabilities``, which consists of a
588 Currently, the only one defined is ``capabilities``, which consists of a
588 line of space separated tokens describing server abilities:
589 line of space separated tokens describing server abilities:
589
590
590 capabilities: <token0> <token1> <token2>
591 capabilities: <token0> <token1> <token2>
591 """
592 """
592 caps = capabilities(repo, proto).data
593 caps = capabilities(repo, proto).data
593 return wireprototypes.bytesresponse(b'capabilities: %s\n' % caps)
594 return wireprototypes.bytesresponse(b'capabilities: %s\n' % caps)
594
595
595
596
596 @wireprotocommand(b'listkeys', b'namespace', permission=b'pull')
597 @wireprotocommand(b'listkeys', b'namespace', permission=b'pull')
597 def listkeys(repo, proto, namespace):
598 def listkeys(repo, proto, namespace):
598 d = sorted(repo.listkeys(encoding.tolocal(namespace)).items())
599 d = sorted(repo.listkeys(encoding.tolocal(namespace)).items())
599 return wireprototypes.bytesresponse(pushkeymod.encodekeys(d))
600 return wireprototypes.bytesresponse(pushkeymod.encodekeys(d))
600
601
601
602
602 @wireprotocommand(b'lookup', b'key', permission=b'pull')
603 @wireprotocommand(b'lookup', b'key', permission=b'pull')
603 def lookup(repo, proto, key):
604 def lookup(repo, proto, key):
604 try:
605 try:
605 k = encoding.tolocal(key)
606 k = encoding.tolocal(key)
606 n = repo.lookup(k)
607 n = repo.lookup(k)
607 r = hex(n)
608 r = hex(n)
608 success = 1
609 success = 1
609 except Exception as inst:
610 except Exception as inst:
610 r = stringutil.forcebytestr(inst)
611 r = stringutil.forcebytestr(inst)
611 success = 0
612 success = 0
612 return wireprototypes.bytesresponse(b'%d %s\n' % (success, r))
613 return wireprototypes.bytesresponse(b'%d %s\n' % (success, r))
613
614
614
615
615 @wireprotocommand(b'known', b'nodes *', permission=b'pull')
616 @wireprotocommand(b'known', b'nodes *', permission=b'pull')
616 def known(repo, proto, nodes, others):
617 def known(repo, proto, nodes, others):
617 v = b''.join(
618 v = b''.join(
618 b and b'1' or b'0' for b in repo.known(wireprototypes.decodelist(nodes))
619 b and b'1' or b'0' for b in repo.known(wireprototypes.decodelist(nodes))
619 )
620 )
620 return wireprototypes.bytesresponse(v)
621 return wireprototypes.bytesresponse(v)
621
622
622
623
623 @wireprotocommand(b'protocaps', b'caps', permission=b'pull')
624 @wireprotocommand(b'protocaps', b'caps', permission=b'pull')
624 def protocaps(repo, proto, caps):
625 def protocaps(repo, proto, caps):
625 if proto.name == wireprototypes.SSHV1:
626 if proto.name == wireprototypes.SSHV1:
626 proto._protocaps = set(caps.split(b' '))
627 proto._protocaps = set(caps.split(b' '))
627 return wireprototypes.bytesresponse(b'OK')
628 return wireprototypes.bytesresponse(b'OK')
628
629
629
630
630 @wireprotocommand(b'pushkey', b'namespace key old new', permission=b'push')
631 @wireprotocommand(b'pushkey', b'namespace key old new', permission=b'push')
631 def pushkey(repo, proto, namespace, key, old, new):
632 def pushkey(repo, proto, namespace, key, old, new):
632 # compatibility with pre-1.8 clients which were accidentally
633 # compatibility with pre-1.8 clients which were accidentally
633 # sending raw binary nodes rather than utf-8-encoded hex
634 # sending raw binary nodes rather than utf-8-encoded hex
634 if len(new) == 20 and stringutil.escapestr(new) != new:
635 if len(new) == 20 and stringutil.escapestr(new) != new:
635 # looks like it could be a binary node
636 # looks like it could be a binary node
636 try:
637 try:
637 new.decode('utf-8')
638 new.decode('utf-8')
638 new = encoding.tolocal(new) # but cleanly decodes as UTF-8
639 new = encoding.tolocal(new) # but cleanly decodes as UTF-8
639 except UnicodeDecodeError:
640 except UnicodeDecodeError:
640 pass # binary, leave unmodified
641 pass # binary, leave unmodified
641 else:
642 else:
642 new = encoding.tolocal(new) # normal path
643 new = encoding.tolocal(new) # normal path
643
644
644 with proto.mayberedirectstdio() as output:
645 with proto.mayberedirectstdio() as output:
645 r = (
646 r = (
646 repo.pushkey(
647 repo.pushkey(
647 encoding.tolocal(namespace),
648 encoding.tolocal(namespace),
648 encoding.tolocal(key),
649 encoding.tolocal(key),
649 encoding.tolocal(old),
650 encoding.tolocal(old),
650 new,
651 new,
651 )
652 )
652 or False
653 or False
653 )
654 )
654
655
655 output = output.getvalue() if output else b''
656 output = output.getvalue() if output else b''
656 return wireprototypes.bytesresponse(b'%d\n%s' % (int(r), output))
657 return wireprototypes.bytesresponse(b'%d\n%s' % (int(r), output))
657
658
658
659
659 @wireprotocommand(b'stream_out', permission=b'pull')
660 @wireprotocommand(b'stream_out', permission=b'pull')
660 def stream(repo, proto):
661 def stream(repo, proto):
661 """If the server supports streaming clone, it advertises the "stream"
662 """If the server supports streaming clone, it advertises the "stream"
662 capability with a value representing the version and flags of the repo
663 capability with a value representing the version and flags of the repo
663 it is serving. Client checks to see if it understands the format.
664 it is serving. Client checks to see if it understands the format.
664 """
665 """
665 return wireprototypes.streamreslegacy(streamclone.generatev1wireproto(repo))
666 return wireprototypes.streamreslegacy(streamclone.generatev1wireproto(repo))
666
667
667
668
668 @wireprotocommand(b'unbundle', b'heads', permission=b'push')
669 @wireprotocommand(b'unbundle', b'heads', permission=b'push')
669 def unbundle(repo, proto, heads):
670 def unbundle(repo, proto, heads):
670 their_heads = wireprototypes.decodelist(heads)
671 their_heads = wireprototypes.decodelist(heads)
671
672
672 with proto.mayberedirectstdio() as output:
673 with proto.mayberedirectstdio() as output:
673 try:
674 try:
674 exchange.check_heads(repo, their_heads, b'preparing changes')
675 exchange.check_heads(repo, their_heads, b'preparing changes')
675 cleanup = lambda: None
676 cleanup = lambda: None
676 try:
677 try:
677 payload = proto.getpayload()
678 payload = proto.getpayload()
678 if repo.ui.configbool(b'server', b'streamunbundle'):
679 if repo.ui.configbool(b'server', b'streamunbundle'):
679
680
680 def cleanup():
681 def cleanup():
681 # Ensure that the full payload is consumed, so
682 # Ensure that the full payload is consumed, so
682 # that the connection doesn't contain trailing garbage.
683 # that the connection doesn't contain trailing garbage.
683 for p in payload:
684 for p in payload:
684 pass
685 pass
685
686
686 fp = util.chunkbuffer(payload)
687 fp = util.chunkbuffer(payload)
687 else:
688 else:
688 # write bundle data to temporary file as it can be big
689 # write bundle data to temporary file as it can be big
689 fp, tempname = None, None
690 fp, tempname = None, None
690
691
691 def cleanup():
692 def cleanup():
692 if fp:
693 if fp:
693 fp.close()
694 fp.close()
694 if tempname:
695 if tempname:
695 os.unlink(tempname)
696 os.unlink(tempname)
696
697
697 fd, tempname = pycompat.mkstemp(prefix=b'hg-unbundle-')
698 fd, tempname = pycompat.mkstemp(prefix=b'hg-unbundle-')
698 repo.ui.debug(
699 repo.ui.debug(
699 b'redirecting incoming bundle to %s\n' % tempname
700 b'redirecting incoming bundle to %s\n' % tempname
700 )
701 )
701 fp = os.fdopen(fd, pycompat.sysstr(b'wb+'))
702 fp = os.fdopen(fd, pycompat.sysstr(b'wb+'))
702 for p in payload:
703 for p in payload:
703 fp.write(p)
704 fp.write(p)
704 fp.seek(0)
705 fp.seek(0)
705
706
706 gen = exchange.readbundle(repo.ui, fp, None)
707 gen = exchange.readbundle(repo.ui, fp, None)
707 if isinstance(
708 if isinstance(
708 gen, changegroupmod.cg1unpacker
709 gen, changegroupmod.cg1unpacker
709 ) and not bundle1allowed(repo, b'push'):
710 ) and not bundle1allowed(repo, b'push'):
710 if proto.name == b'http-v1':
711 if proto.name == b'http-v1':
711 # need to special case http because stderr do not get to
712 # need to special case http because stderr do not get to
712 # the http client on failed push so we need to abuse
713 # the http client on failed push so we need to abuse
713 # some other error type to make sure the message get to
714 # some other error type to make sure the message get to
714 # the user.
715 # the user.
715 return wireprototypes.ooberror(bundle2required)
716 return wireprototypes.ooberror(bundle2required)
716 raise error.Abort(
717 raise error.Abort(
717 bundle2requiredmain, hint=bundle2requiredhint
718 bundle2requiredmain, hint=bundle2requiredhint
718 )
719 )
719
720
720 r = exchange.unbundle(
721 r = exchange.unbundle(
721 repo, gen, their_heads, b'serve', proto.client()
722 repo, gen, their_heads, b'serve', proto.client()
722 )
723 )
723 if hasattr(r, 'addpart'):
724 if hasattr(r, 'addpart'):
724 # The return looks streamable, we are in the bundle2 case
725 # The return looks streamable, we are in the bundle2 case
725 # and should return a stream.
726 # and should return a stream.
726 return wireprototypes.streamreslegacy(gen=r.getchunks())
727 return wireprototypes.streamreslegacy(gen=r.getchunks())
727 return wireprototypes.pushres(
728 return wireprototypes.pushres(
728 r, output.getvalue() if output else b''
729 r, output.getvalue() if output else b''
729 )
730 )
730
731
731 finally:
732 finally:
732 cleanup()
733 cleanup()
733
734
734 except (error.BundleValueError, error.Abort, error.PushRaced) as exc:
735 except (error.BundleValueError, error.Abort, error.PushRaced) as exc:
735 # handle non-bundle2 case first
736 # handle non-bundle2 case first
736 if not getattr(exc, 'duringunbundle2', False):
737 if not getattr(exc, 'duringunbundle2', False):
737 try:
738 try:
738 raise
739 raise
739 except error.Abort as exc:
740 except error.Abort as exc:
740 # The old code we moved used procutil.stderr directly.
741 # The old code we moved used procutil.stderr directly.
741 # We did not change it to minimise code change.
742 # We did not change it to minimise code change.
742 # This need to be moved to something proper.
743 # This need to be moved to something proper.
743 # Feel free to do it.
744 # Feel free to do it.
744 procutil.stderr.write(exc.format())
745 procutil.stderr.write(exc.format())
745 procutil.stderr.flush()
746 procutil.stderr.flush()
746 return wireprototypes.pushres(
747 return wireprototypes.pushres(
747 0, output.getvalue() if output else b''
748 0, output.getvalue() if output else b''
748 )
749 )
749 except error.PushRaced:
750 except error.PushRaced:
750 return wireprototypes.pusherr(
751 return wireprototypes.pusherr(
751 pycompat.bytestr(exc),
752 pycompat.bytestr(exc),
752 output.getvalue() if output else b'',
753 output.getvalue() if output else b'',
753 )
754 )
754
755
755 bundler = bundle2.bundle20(repo.ui)
756 bundler = bundle2.bundle20(repo.ui)
756 for out in getattr(exc, '_bundle2salvagedoutput', ()):
757 for out in getattr(exc, '_bundle2salvagedoutput', ()):
757 bundler.addpart(out)
758 bundler.addpart(out)
758 try:
759 try:
759 try:
760 try:
760 raise
761 raise
761 except error.PushkeyFailed as exc:
762 except error.PushkeyFailed as exc:
762 # check client caps
763 # check client caps
763 remotecaps = getattr(exc, '_replycaps', None)
764 remotecaps = getattr(exc, '_replycaps', None)
764 if (
765 if (
765 remotecaps is not None
766 remotecaps is not None
766 and b'pushkey' not in remotecaps.get(b'error', ())
767 and b'pushkey' not in remotecaps.get(b'error', ())
767 ):
768 ):
768 # no support remote side, fallback to Abort handler.
769 # no support remote side, fallback to Abort handler.
769 raise
770 raise
770 part = bundler.newpart(b'error:pushkey')
771 part = bundler.newpart(b'error:pushkey')
771 part.addparam(b'in-reply-to', exc.partid)
772 part.addparam(b'in-reply-to', exc.partid)
772 if exc.namespace is not None:
773 if exc.namespace is not None:
773 part.addparam(
774 part.addparam(
774 b'namespace', exc.namespace, mandatory=False
775 b'namespace', exc.namespace, mandatory=False
775 )
776 )
776 if exc.key is not None:
777 if exc.key is not None:
777 part.addparam(b'key', exc.key, mandatory=False)
778 part.addparam(b'key', exc.key, mandatory=False)
778 if exc.new is not None:
779 if exc.new is not None:
779 part.addparam(b'new', exc.new, mandatory=False)
780 part.addparam(b'new', exc.new, mandatory=False)
780 if exc.old is not None:
781 if exc.old is not None:
781 part.addparam(b'old', exc.old, mandatory=False)
782 part.addparam(b'old', exc.old, mandatory=False)
782 if exc.ret is not None:
783 if exc.ret is not None:
783 part.addparam(b'ret', exc.ret, mandatory=False)
784 part.addparam(b'ret', exc.ret, mandatory=False)
784 except error.BundleValueError as exc:
785 except error.BundleValueError as exc:
785 errpart = bundler.newpart(b'error:unsupportedcontent')
786 errpart = bundler.newpart(b'error:unsupportedcontent')
786 if exc.parttype is not None:
787 if exc.parttype is not None:
787 errpart.addparam(b'parttype', exc.parttype)
788 errpart.addparam(b'parttype', exc.parttype)
788 if exc.params:
789 if exc.params:
789 errpart.addparam(b'params', b'\0'.join(exc.params))
790 errpart.addparam(b'params', b'\0'.join(exc.params))
790 except error.Abort as exc:
791 except error.Abort as exc:
791 manargs = [(b'message', exc.message)]
792 manargs = [(b'message', exc.message)]
792 advargs = []
793 advargs = []
793 if exc.hint is not None:
794 if exc.hint is not None:
794 advargs.append((b'hint', exc.hint))
795 advargs.append((b'hint', exc.hint))
795 bundler.addpart(
796 bundler.addpart(
796 bundle2.bundlepart(b'error:abort', manargs, advargs)
797 bundle2.bundlepart(b'error:abort', manargs, advargs)
797 )
798 )
798 except error.PushRaced as exc:
799 except error.PushRaced as exc:
799 bundler.newpart(
800 bundler.newpart(
800 b'error:pushraced',
801 b'error:pushraced',
801 [(b'message', stringutil.forcebytestr(exc))],
802 [(b'message', stringutil.forcebytestr(exc))],
802 )
803 )
803 return wireprototypes.streamreslegacy(gen=bundler.getchunks())
804 return wireprototypes.streamreslegacy(gen=bundler.getchunks())
General Comments 0
You need to be logged in to leave comments. Login now