Debian Codegraph @

hgext/narrow/narrowwirepeer.py

raw
1 # narrowwirepeer.py - passes narrow spec with unbundle command
2 #
3 # Copyright 2017 Google, Inc.
4 #
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.
7
8 from __future__ import annotations
9
10 from mercurial.i18n import _
11
12 from mercurial.utils import stringutil
13
14 from mercurial import (
15 bundle2,
16 error,
17 exchange,
18 extensions,
19 hg,
20 narrowspec,
21 wireprototypes,
22 wireprotov1peer,
23 wireprotov1server,
24 )
25
26 from . import narrowbundle2
27
28
29 def uisetup():
30 wireprotov1peer.wirepeer.narrow_widen = peernarrowwiden
31
32
33 def reposetup(repo):
34 def wirereposetup(ui, peer):
35 def wrapped(orig, cmd, *args, **kwargs):
36 if cmd == b'unbundle':
37 # TODO: don't blindly add include/exclude wireproto
38 # arguments to unbundle.
39 include, exclude = repo.narrowpats
40 kwargs["includepats"] = b','.join(include)
41 kwargs["excludepats"] = b','.join(exclude)
42 return orig(cmd, *args, **kwargs)
43
44 extensions.wrapfunction(peer, '_calltwowaystream', wrapped)
45
46 hg.wirepeersetupfuncs.append(wirereposetup)
47
48
49 @wireprotov1server.wireprotocommand(
50 b'narrow_widen',
51 b'oldincludes oldexcludes'
52 b' newincludes newexcludes'
53 b' commonheads cgversion'
54 b' known ellipses',
55 permission=b'pull',
56 )
57 def narrow_widen(
58 repo,
59 proto,
60 oldincludes,
61 oldexcludes,
62 newincludes,
63 newexcludes,
64 commonheads,
65 cgversion,
66 known,
67 ellipses,
68 ):
69 """wireprotocol command to send data when a narrow clone is widen. We will
70 be sending a changegroup here.
71
72 The current set of arguments which are required:
73 oldincludes: the old includes of the narrow copy
74 oldexcludes: the old excludes of the narrow copy
75 newincludes: the new includes of the narrow copy
76 newexcludes: the new excludes of the narrow copy
77 commonheads: list of heads which are common between the server and client
78 cgversion(maybe): the changegroup version to produce
79 known: list of nodes which are known on the client (used in ellipses cases)
80 ellipses: whether to send ellipses data or not
81 """
82
83 preferuncompressed = False
84 try:
85
86 def splitpaths(data):
87 # work around ''.split(',') => ['']
88 return data.split(b',') if data else []
89
90 oldincludes = set(splitpaths(oldincludes))
91 newincludes = set(splitpaths(newincludes))
92 oldexcludes = set(splitpaths(oldexcludes))
93 newexcludes = set(splitpaths(newexcludes))
94
95 # enforce narrow acl if set
96 if repo.ui.has_section(exchange._NARROWACL_SECTION):
97 kwargs = exchange.applynarrowacl(
98 repo, {'includepats': newincludes, 'excludepats': newexcludes}
99 )
100 newincludes = kwargs['includepats']
101 requiredexcludes = kwargs['excludepats'] - newexcludes
102 if requiredexcludes:
103 # XXX: The below code to get the username was copied from exchange.py,
104 # where it is noted that this is technically a layering violation for
105 # assuming the existence of HTTP. Using it anyway to make the error
106 # message consistent with the error message for invalid includes.
107 ui = repo.ui
108 username = ui.shortuser(
109 ui.environ.get(b'REMOTE_USER') or ui.username()
110 )
111 raise error.Abort(
112 _(b"The following excludes cannot be removed for %s: %s")
113 % (username, stringutil.pprint(list(requiredexcludes)))
114 )
115 newexcludes = kwargs['excludepats']
116
117 # validate the patterns
118 narrowspec.validatepatterns(oldincludes)
119 narrowspec.validatepatterns(newincludes)
120 narrowspec.validatepatterns(oldexcludes)
121 narrowspec.validatepatterns(newexcludes)
122
123 common = wireprototypes.decodelist(commonheads)
124 known = wireprototypes.decodelist(known)
125 if ellipses == b'0':
126 ellipses = False
127 else:
128 ellipses = bool(ellipses)
129 cgversion = cgversion
130
131 bundler = bundle2.bundle20(repo.ui)
132 newmatch = narrowspec.match(
133 repo.root, include=newincludes, exclude=newexcludes
134 )
135 oldmatch = narrowspec.match(
136 repo.root, include=oldincludes, exclude=oldexcludes
137 )
138 if not ellipses:
139 bundle2.widen_bundle(
140 bundler,
141 repo,
142 oldmatch,
143 newmatch,
144 common,
145 known,
146 cgversion,
147 ellipses,
148 )
149 else:
150 narrowbundle2.generate_ellipses_bundle2_for_widening(
151 bundler,
152 repo,
153 oldmatch,
154 newmatch,
155 cgversion,
156 common,
157 known,
158 )
159 except error.Abort as exc:
160 bundler = bundle2.bundle20(repo.ui)
161 manargs = [(b'message', exc.message)]
162 advargs = []
163 if exc.hint is not None:
164 advargs.append((b'hint', exc.hint))
165 bundler.addpart(bundle2.bundlepart(b'error:abort', manargs, advargs))
166 preferuncompressed = True
167
168 chunks = bundler.getchunks()
169 return wireprototypes.streamres(
170 gen=chunks, prefer_uncompressed=preferuncompressed
171 )
172
173
174 def peernarrowwiden(remote, **kwargs):
175 for ch in ('commonheads', 'known'):
176 kwargs[ch] = wireprototypes.encodelist(kwargs[ch])
177
178 for ch in ('oldincludes', 'newincludes', 'oldexcludes', 'newexcludes'):
179 kwargs[ch] = b','.join(kwargs[ch])
180
181 kwargs['ellipses'] = b'%i' % bool(kwargs['ellipses'])
182 f = remote._callcompressable(b'narrow_widen', **kwargs)
183 return bundle2.getunbundler(remote.ui, f)