##// END OF EJS Templates
wireproto: refactor list of nodeid encoding / decoding
Benoit Boissinot -
r11597:9141d2c9 default
parent child Browse files
Show More
@@ -12,6 +12,14 b' import changegroup as changegroupmod'
12 import streamclone, repo, error, encoding, util
12 import streamclone, repo, error, encoding, util
13 import pushkey as pushkey_
13 import pushkey as pushkey_
14
14
15 # list of nodes encoding / decoding
16
17 def decodelist(l, sep=' '):
18 return map(bin, l.split(sep))
19
20 def encodelist(l, sep=' '):
21 return sep.join(map(hex, l))
22
15 # client side
23 # client side
16
24
17 class wirerepository(repo.repository):
25 class wirerepository(repo.repository):
@@ -26,7 +34,7 b' class wirerepository(repo.repository):'
26 def heads(self):
34 def heads(self):
27 d = self._call("heads")
35 d = self._call("heads")
28 try:
36 try:
29 return map(bin, d[:-1].split(" "))
37 return decodelist(d[:-1])
30 except:
38 except:
31 self.abort(error.ResponseError(_("unexpected response:"), d))
39 self.abort(error.ResponseError(_("unexpected response:"), d))
32
40
@@ -35,8 +43,8 b' class wirerepository(repo.repository):'
35 try:
43 try:
36 branchmap = {}
44 branchmap = {}
37 for branchpart in d.splitlines():
45 for branchpart in d.splitlines():
38 branchheads = branchpart.split(' ')
46 branchname, branchheads = branchpart.split(' ', 1)
39 branchname = urllib.unquote(branchheads[0])
47 branchname = urllib.unquote(branchname)
40 # Earlier servers (1.3.x) send branch names in (their) local
48 # Earlier servers (1.3.x) send branch names in (their) local
41 # charset. The best we can do is assume it's identical to our
49 # charset. The best we can do is assume it's identical to our
42 # own local charset, in case it's not utf-8.
50 # own local charset, in case it's not utf-8.
@@ -44,17 +52,17 b' class wirerepository(repo.repository):'
44 branchname.decode('utf-8')
52 branchname.decode('utf-8')
45 except UnicodeDecodeError:
53 except UnicodeDecodeError:
46 branchname = encoding.fromlocal(branchname)
54 branchname = encoding.fromlocal(branchname)
47 branchheads = [bin(x) for x in branchheads[1:]]
55 branchheads = decodelist(branchheads)
48 branchmap[branchname] = branchheads
56 branchmap[branchname] = branchheads
49 return branchmap
57 return branchmap
50 except TypeError:
58 except TypeError:
51 self._abort(error.ResponseError(_("unexpected response:"), d))
59 self._abort(error.ResponseError(_("unexpected response:"), d))
52
60
53 def branches(self, nodes):
61 def branches(self, nodes):
54 n = " ".join(map(hex, nodes))
62 n = encodelist(nodes)
55 d = self._call("branches", nodes=n)
63 d = self._call("branches", nodes=n)
56 try:
64 try:
57 br = [tuple(map(bin, b.split(" "))) for b in d.splitlines()]
65 br = [tuple(decodelist(b)) for b in d.splitlines()]
58 return br
66 return br
59 except:
67 except:
60 self._abort(error.ResponseError(_("unexpected response:"), d))
68 self._abort(error.ResponseError(_("unexpected response:"), d))
@@ -63,11 +71,10 b' class wirerepository(repo.repository):'
63 batch = 8 # avoid giant requests
71 batch = 8 # avoid giant requests
64 r = []
72 r = []
65 for i in xrange(0, len(pairs), batch):
73 for i in xrange(0, len(pairs), batch):
66 n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]])
74 n = " ".join([encodelist(p, '-') for p in pairs[i:i + batch]])
67 d = self._call("between", pairs=n)
75 d = self._call("between", pairs=n)
68 try:
76 try:
69 r += [l and map(bin, l.split(" ")) or []
77 r.extend(l and decodelist(l) or [] for l in d.splitlines())
70 for l in d.splitlines()]
71 except:
78 except:
72 self._abort(error.ResponseError(_("unexpected response:"), d))
79 self._abort(error.ResponseError(_("unexpected response:"), d))
73 return r
80 return r
@@ -93,14 +100,14 b' class wirerepository(repo.repository):'
93 return self._callstream('stream_out')
100 return self._callstream('stream_out')
94
101
95 def changegroup(self, nodes, kind):
102 def changegroup(self, nodes, kind):
96 n = " ".join(map(hex, nodes))
103 n = encodelist(nodes)
97 f = self._callstream("changegroup", roots=n)
104 f = self._callstream("changegroup", roots=n)
98 return self._decompress(f)
105 return self._decompress(f)
99
106
100 def changegroupsubset(self, bases, heads, kind):
107 def changegroupsubset(self, bases, heads, kind):
101 self.requirecap('changegroupsubset', _('look up remote changes'))
108 self.requirecap('changegroupsubset', _('look up remote changes'))
102 bases = " ".join(map(hex, bases))
109 bases = encodelist(bases)
103 heads = " ".join(map(hex, heads))
110 heads = encodelist(heads)
104 return self._decompress(self._callstream("changegroupsubset",
111 return self._decompress(self._callstream("changegroupsubset",
105 bases=bases, heads=heads))
112 bases=bases, heads=heads))
106
113
@@ -110,7 +117,7 b' class wirerepository(repo.repository):'
110 remote server as a bundle. Return an integer indicating the
117 remote server as a bundle. Return an integer indicating the
111 result of the push (see localrepository.addchangegroup()).'''
118 result of the push (see localrepository.addchangegroup()).'''
112
119
113 ret, output = self._callpush("unbundle", cg, heads=' '.join(map(hex, heads)))
120 ret, output = self._callpush("unbundle", cg, heads=encodelist(heads))
114 if ret == "":
121 if ret == "":
115 raise error.ResponseError(
122 raise error.ResponseError(
116 _('push failed:'), output)
123 _('push failed:'), output)
@@ -137,10 +144,10 b' def dispatch(repo, proto, command):'
137 return True
144 return True
138
145
139 def between(repo, proto, pairs):
146 def between(repo, proto, pairs):
140 pairs = [map(bin, p.split("-")) for p in pairs.split(" ")]
147 pairs = [decodelist(p, '-') for p in pairs.split(" ")]
141 r = []
148 r = []
142 for b in repo.between(pairs):
149 for b in repo.between(pairs):
143 r.append(" ".join(map(hex, b)) + "\n")
150 r.append(encodelist(b) + "\n")
144 return "".join(r)
151 return "".join(r)
145
152
146 def branchmap(repo, proto):
153 def branchmap(repo, proto):
@@ -148,15 +155,15 b' def branchmap(repo, proto):'
148 heads = []
155 heads = []
149 for branch, nodes in branchmap.iteritems():
156 for branch, nodes in branchmap.iteritems():
150 branchname = urllib.quote(branch)
157 branchname = urllib.quote(branch)
151 branchnodes = [hex(node) for node in nodes]
158 branchnodes = encodelist(nodes)
152 heads.append('%s %s' % (branchname, ' '.join(branchnodes)))
159 heads.append('%s %s' % (branchname, branchnodes))
153 return '\n'.join(heads)
160 return '\n'.join(heads)
154
161
155 def branches(repo, proto, nodes):
162 def branches(repo, proto, nodes):
156 nodes = map(bin, nodes.split(" "))
163 nodes = decodelist(nodes)
157 r = []
164 r = []
158 for b in repo.branches(nodes):
165 for b in repo.branches(nodes):
159 r.append(" ".join(map(hex, b)) + "\n")
166 r.append(encodelist(b) + "\n")
160 return "".join(r)
167 return "".join(r)
161
168
162 def capabilities(repo, proto):
169 def capabilities(repo, proto):
@@ -167,19 +174,19 b' def capabilities(repo, proto):'
167 return ' '.join(caps)
174 return ' '.join(caps)
168
175
169 def changegroup(repo, proto, roots):
176 def changegroup(repo, proto, roots):
170 nodes = map(bin, roots.split(" "))
177 nodes = decodelist(roots)
171 cg = repo.changegroup(nodes, 'serve')
178 cg = repo.changegroup(nodes, 'serve')
172 proto.sendchangegroup(cg)
179 proto.sendchangegroup(cg)
173
180
174 def changegroupsubset(repo, proto, bases, heads):
181 def changegroupsubset(repo, proto, bases, heads):
175 bases = [bin(n) for n in bases.split(' ')]
182 bases = decodelist(bases)
176 heads = [bin(n) for n in heads.split(' ')]
183 heads = decodelist(heads)
177 cg = repo.changegroupsubset(bases, heads, 'serve')
184 cg = repo.changegroupsubset(bases, heads, 'serve')
178 proto.sendchangegroup(cg)
185 proto.sendchangegroup(cg)
179
186
180 def heads(repo, proto):
187 def heads(repo, proto):
181 h = repo.heads()
188 h = repo.heads()
182 return " ".join(map(hex, h)) + "\n"
189 return encodelist(h) + "\n"
183
190
184 def hello(repo, proto):
191 def hello(repo, proto):
185 '''the hello command returns a set of lines describing various
192 '''the hello command returns a set of lines describing various
@@ -217,11 +224,11 b' def stream(repo, proto):'
217 return str(inst)
224 return str(inst)
218
225
219 def unbundle(repo, proto, heads):
226 def unbundle(repo, proto, heads):
220 their_heads = heads.split()
227 their_heads = decodelist(heads)
221
228
222 def check_heads():
229 def check_heads():
223 heads = map(hex, repo.heads())
230 heads = repo.heads()
224 return their_heads == [hex('force')] or their_heads == heads
231 return their_heads == ['force'] or their_heads == heads
225
232
226 # fail early if possible
233 # fail early if possible
227 if not check_heads():
234 if not check_heads():
General Comments 0
You need to be logged in to leave comments. Login now