def unbundle(self, cg, heads, source): '''Send cg (a readable file-like object representing the changegroup to push, typically a chunkbuffer object) to the remote server as a bundle. When pushing a bundle10 stream, return an integer indicating the result of the push (see localrepository.addchangegroup()). When pushing a bundle20 stream, return a bundle20 stream.''' if heads != ['force'] and self.capable('unbundlehash'): heads = encodelist(['hashed', util.sha1(''.join(sorted(heads))).digest()]) else: heads = encodelist(heads) if util.safehasattr(cg, 'deltaheader'): # this a bundle10, do the old style call sequence ret, output = self._callpush("unbundle", cg, heads=heads) if ret == "": raise error.ResponseError( _('push failed:'), output) try: ret = int(ret) except ValueError: raise error.ResponseError( _('push failed (unexpected response):'), ret) for l in output.splitlines(True): self.ui.status(_('remote: '), l) else: # bundle2 push. Send a stream, fetch a stream. stream = self._calltwowaystream('unbundle', cg, heads=heads) ret = bundle2.unbundle20(self.ui, stream) return ret
def unbundle(self, cg, heads, source): '''Send cg (a readable file-like object representing the changegroup to push, typically a chunkbuffer object) to the remote server as a bundle. Return an integer indicating the result of the push (see localrepository.addchangegroup()).''' if heads != ['force'] and self.capable('unbundlehash'): heads = encodelist( ['hashed', util.sha1(''.join(sorted(heads))).digest()]) else: heads = encodelist(heads) ret, output = self._callpush("unbundle", cg, heads=heads) if ret == "": raise error.ResponseError(_('push failed:'), output) try: ret = int(ret) except ValueError: raise error.ResponseError(_('push failed (unexpected response):'), ret) for l in output.splitlines(True): self.ui.status(_('remote: '), l) return ret
def _callpush(self, cmd, cg, **args): # have to stream bundle to a temp file because we do not have # http 1.1 chunked transfer. types = self.capable('unbundle') try: types = types.split(',') except AttributeError: # servers older than d1b16a746db6 will send 'unbundle' as a # boolean capability. They only support headerless/uncompressed # bundles. types = [""] for x in types: if x in changegroup.bundletypes: type = x break tempname = changegroup.writebundle(self.ui, cg, None, type) fp = httpconnection.httpsendfile(self.ui, tempname, "rb") headers = {'Content-Type': 'application/mercurial-0.1'} try: try: r = self._call(cmd, data=fp, headers=headers, **args) vals = r.split('\n', 1) if len(vals) < 2: raise error.ResponseError(_("unexpected response:"), r) return vals except socket.error, err: if err.args[0] in (errno.ECONNRESET, errno.EPIPE): raise util.Abort(_('push failed: %s') % err.args[1]) raise util.Abort(err.args[1]) finally: fp.close() os.unlink(tempname)
def _recv(self): l = self.pipei.readline() self.readerr() try: l = int(l) except: self._abort(error.ResponseError(_("unexpected response:"), l)) return self.pipei.read(l)
def heads(self): f = future() yield {}, f d = f.value try: yield decodelist(d[:-1]) except ValueError: self._abort(error.ResponseError(_("unexpected response:"), d))
def between(self, pairs): n = " ".join(["-".join(map(hex, p)) for p in pairs]) d = self.call("between", pairs=n) try: p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] return p except: self.abort(error.ResponseError(_("unexpected response:"), d))
def known(self, nodes): f = future() yield {'nodes': encodelist(nodes)}, f d = f.value try: yield [bool(int(b)) for b in d] except ValueError: self._abort(error.ResponseError(_("unexpected response:"), d))
def branches(self, nodes): n = encodelist(nodes) d = self._call("branches", nodes=n) try: br = [tuple(decodelist(b)) for b in d.splitlines()] return br except: self._abort(error.ResponseError(_("unexpected response:"), d))
def branches(self, nodes): n = " ".join(map(hex, nodes)) d = self.do_read("branches", nodes=n) try: br = [tuple(map(bin, b.split(" "))) for b in d.splitlines()] return br except: raise error.ResponseError(_("unexpected response:"), d)
def known(self, nodes): f = future() yield todict(nodes=encodelist(nodes)), f d = f.value try: yield [bool(int(f)) for f in d] except ValueError: self._abort(error.ResponseError(_("unexpected response:"), d))
def between(self, pairs): batch = 8 # avoid giant requests r = [] for i in xrange(0, len(pairs), batch): n = " ".join([encodelist(p, '-') for p in pairs[i:i + batch]]) d = self._call("between", pairs=n) try: r.extend(l and decodelist(l) or [] for l in d.splitlines()) except: self._abort(error.ResponseError(_("unexpected response:"), d)) return r
def between(self, pairs): batch = 8 # avoid giant requests r = [] for i in xrange(0, len(pairs), batch): n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]]) d = self.do_read("between", pairs=n) try: r += [l and map(bin, l.split(" ")) or [] for l in d.splitlines()] except: raise error.ResponseError(_("unexpected response:"), d) return r
def _recv(self): l = self.pipei.readline() if l == '\n': self.readerr() msg = _('check previous remote output') self._abort(error.OutOfBandError(hint=msg)) self.readerr() try: l = int(l) except ValueError: self._abort(error.ResponseError(_("unexpected response:"), l)) return self.pipei.read(l)
def branchmap(self): d = self.call("branchmap") try: branchmap = {} for branchpart in d.splitlines(): branchheads = branchpart.split(' ') branchname = urllib.unquote(branchheads[0]) branchheads = [bin(x) for x in branchheads[1:]] branchmap[branchname] = branchheads return branchmap except: raise error.ResponseError(_("unexpected response:"), d)
def branchmap(self): d = self._call("branchmap") try: branchmap = {} for branchpart in d.splitlines(): branchname, branchheads = branchpart.split(' ', 1) branchname = encoding.tolocal(urllib.unquote(branchname)) branchheads = decodelist(branchheads) branchmap[branchname] = branchheads return branchmap except TypeError: self._abort(error.ResponseError(_("unexpected response:"), d))
def pushkey(self, namespace, key, old, new): if not self.capable('pushkey'): return False d = self._call("pushkey", namespace=encoding.fromlocal(namespace), key=encoding.fromlocal(key), old=encoding.fromlocal(old), new=encoding.fromlocal(new)) try: d = bool(int(d)) except ValueError: raise error.ResponseError( _('push failed (unexpected response):'), d) return d
def branchmap(self): f = future() yield {}, f d = f.value try: branchmap = {} for branchpart in d.splitlines(): branchname, branchheads = branchpart.split(' ', 1) branchname = encoding.tolocal(urllib.unquote(branchname)) branchheads = decodelist(branchheads) branchmap[branchname] = branchheads yield branchmap except TypeError: self._abort(error.ResponseError(_("unexpected response:"), d))
def pushkey(self, namespace, key, old, new): if not self.capable('pushkey'): yield False, None f = future() yield todict(namespace=encoding.fromlocal(namespace), key=encoding.fromlocal(key), old=encoding.fromlocal(old), new=encoding.fromlocal(new)), f d = f.value try: d = bool(int(d)) except ValueError: raise error.ResponseError(_('push failed (unexpected response):'), d) yield d
def wrap_socket(sock, target_address, cmd = 1, *args, **kwargs): """wrap a socket with SOCKS5""" reply = packet.Reply() request = packet.Request(3, cmd, *target_address[:2]) sock = authentication.wrap_socket(sock, *args, **kwargs) try: sock.sendall(str(request)) reply.fload(sock.makefile()) except IOError as e: raise error.SOCKS5Error(e) if reply.rep: raise error.ResponseError(reply.rep) return sock
def pushkey(self, namespace, key, old, new): if not self.capable('pushkey'): yield False, None f = future() yield todict(namespace=encoding.fromlocal(namespace), key=encoding.fromlocal(key), old=encoding.fromlocal(old), new=encoding.fromlocal(new)), f d = f.value d, output = d.split('\n', 1) try: d = bool(int(d)) except ValueError: raise error.ResponseError(_('push failed (unexpected response):'), d) for l in output.splitlines(True): self.ui.status(_('remote: '), l) yield d
def branchmap(self): d = self.do_read("branchmap") try: branchmap = {} for branchpart in d.splitlines(): branchheads = branchpart.split(' ') branchname = urllib.unquote(branchheads[0]) # Earlier servers (1.3.x) send branch names in (their) local # charset. The best we can do is assume it's identical to our # own local charset, in case it's not utf-8. try: branchname.decode('utf-8') except UnicodeDecodeError: branchname = encoding.fromlocal(branchname) branchheads = [bin(x) for x in branchheads[1:]] branchmap[branchname] = branchheads return branchmap except: raise error.ResponseError(_("unexpected response:"), d)
def _recv(self): l = self.pipei.readline() if l == '\n': err = [] while True: line = self.pipee.readline() if line == '-\n': break err.extend([line]) if len(err) > 0: # strip the trailing newline added to the last line server-side err[-1] = err[-1][:-1] self._abort(error.OutOfBandError(*err)) self.readerr() try: l = int(l) except ValueError: self._abort(error.ResponseError(_("unexpected response:"), l)) return self.pipei.read(l)
def branchmap(self): d = self._call("branchmap") try: branchmap = {} for branchpart in d.splitlines(): branchname, branchheads = branchpart.split(' ', 1) branchname = urllib.unquote(branchname) # Earlier servers (1.3.x) send branch names in (their) local # charset. The best we can do is assume it's identical to our # own local charset, in case it's not utf-8. try: branchname.decode('utf-8') except UnicodeDecodeError: branchname = encoding.fromlocal(branchname) branchheads = decodelist(branchheads) branchmap[branchname] = branchheads return branchmap except TypeError: self._abort(error.ResponseError(_("unexpected response:"), d))
def pushkey(self, namespace, key, old, new): if not self.capable('pushkey'): yield False, None f = future() self.ui.debug('preparing pushkey for "%s:%s"\n' % (namespace, key)) yield {'namespace': encoding.fromlocal(namespace), 'key': encoding.fromlocal(key), 'old': encoding.fromlocal(old), 'new': encoding.fromlocal(new)}, f d = f.value d, output = d.split('\n', 1) try: d = bool(int(d)) except ValueError: raise error.ResponseError( _('push failed (unexpected response):'), d) for l in output.splitlines(True): self.ui.status(_('remote: '), l) yield d
def addchangegroup(self, cg, source, url): d = self.call("addchangegroup") if d: self.abort(error.RepoError(_("push refused: %s") % d)) while 1: d = cg.read(4096) if not d: break self.pipeo.write(d) self.readerr() self.pipeo.flush() self.readerr() r = self._recv() if not r: return 1 try: return int(r) except: self.abort(error.ResponseError(_("unexpected response:"), r))
def unbundle(self, cg, heads, source): # have to stream bundle to a temp file because we do not have # http 1.1 chunked transfer. type = "" types = self.capable('unbundle') # servers older than d1b16a746db6 will send 'unbundle' as a # boolean capability try: types = types.split(',') except AttributeError: types = [""] if types: for x in types: if x in changegroup.bundletypes: type = x break tempname = changegroup.writebundle(cg, None, type) fp = url.httpsendfile(tempname, "rb") try: try: resp = self.do_read( 'unbundle', data=fp, headers={'Content-Type': 'application/octet-stream'}, heads=' '.join(map(hex, heads))) resp_code, output = resp.split('\n', 1) try: ret = int(resp_code) except ValueError, err: raise error.ResponseError( _('push failed (unexpected response):'), resp) self.ui.write(output) return ret except socket.error, err: if err[0] in (errno.ECONNRESET, errno.EPIPE): raise util.Abort(_('push failed: %s') % err[1]) raise util.Abort(err[1])
def unbundle(self, cg, heads, source): d = self.call("unbundle", heads=' '.join(map(hex, heads))) if d: # remote may send "unsynced changes" self.abort(error.RepoError(_("push refused: %s") % d)) while 1: d = cg.read(4096) if not d: break self._send(d) self._send("", flush=True) r = self._recv() if r: # remote may send "unsynced changes" self.abort(error.RepoError(_("push failed: %s") % r)) r = self._recv() try: return int(r) except: self.abort(error.ResponseError(_("unexpected response:"), r))
def addchangegroup(self, cg, source, url, lock=None): '''Send a changegroup to the remote server. Return an integer similar to unbundle(). DEPRECATED, since it requires locking the remote.''' d = self._call("addchangegroup") if d: self._abort(error.RepoError(_("push refused: %s") % d)) while True: d = cg.read(4096) if not d: break self.pipeo.write(d) self.readerr() self.pipeo.flush() self.readerr() r = self._recv() if not r: return 1 try: return int(r) except ValueError: self._abort(error.ResponseError(_("unexpected response:"), r))
def heads(self): d = self.do_read("heads") try: return map(bin, d[:-1].split(" ")) except: raise error.ResponseError(_("unexpected response:"), d)
def handlereplycaps(op, inpart): """Used to transmit push race error over the wire""" raise error.ResponseError(_('push failed:'), inpart.params['message'])