##// END OF EJS Templates
ssh: gather initial output so we can do capability detection
Matt Mackall -
r2420:144280f1 default
parent child Browse files
Show More
@@ -1,155 +1,154 b''
1 # sshrepo.py - ssh repository proxy class for mercurial
1 # sshrepo.py - ssh repository proxy class for mercurial
2 #
2 #
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 from node import *
8 from node import *
9 from remoterepo import *
9 from remoterepo import *
10 from i18n import gettext as _
10 from i18n import gettext as _
11 from demandload import *
11 from demandload import *
12 demandload(globals(), "hg os re stat util")
12 demandload(globals(), "hg os re stat util")
13
13
14 class sshrepository(remoterepository):
14 class sshrepository(remoterepository):
15 def __init__(self, ui, path):
15 def __init__(self, ui, path):
16 self.url = path
16 self.url = path
17 self.ui = ui
17 self.ui = ui
18
18
19 m = re.match(r'ssh://(([^@]+)@)?([^:/]+)(:(\d+))?(/(.*))?', path)
19 m = re.match(r'ssh://(([^@]+)@)?([^:/]+)(:(\d+))?(/(.*))?', path)
20 if not m:
20 if not m:
21 raise hg.RepoError(_("couldn't parse destination %s") % path)
21 raise hg.RepoError(_("couldn't parse destination %s") % path)
22
22
23 self.user = m.group(2)
23 self.user = m.group(2)
24 self.host = m.group(3)
24 self.host = m.group(3)
25 self.port = m.group(5)
25 self.port = m.group(5)
26 self.path = m.group(7) or "."
26 self.path = m.group(7) or "."
27
27
28 args = self.user and ("%s@%s" % (self.user, self.host)) or self.host
28 args = self.user and ("%s@%s" % (self.user, self.host)) or self.host
29 args = self.port and ("%s -p %s") % (args, self.port) or args
29 args = self.port and ("%s -p %s") % (args, self.port) or args
30
30
31 sshcmd = self.ui.config("ui", "ssh", "ssh")
31 sshcmd = self.ui.config("ui", "ssh", "ssh")
32 remotecmd = self.ui.config("ui", "remotecmd", "hg")
32 remotecmd = self.ui.config("ui", "remotecmd", "hg")
33 cmd = '%s %s "%s -R %s serve --stdio"'
33 cmd = '%s %s "%s -R %s serve --stdio"'
34 cmd = cmd % (sshcmd, args, remotecmd, self.path)
34 cmd = cmd % (sshcmd, args, remotecmd, self.path)
35
35
36 ui.note('running %s\n' % cmd)
36 ui.note('running %s\n' % cmd)
37 self.pipeo, self.pipei, self.pipee = os.popen3(cmd, 'b')
37 self.pipeo, self.pipei, self.pipee = os.popen3(cmd, 'b')
38
38
39 # skip any noise generated by remote shell
39 # skip any noise generated by remote shell
40 r = self.do_cmd("between", pairs=("%s-%s" % ("0"*40, "0"*40)))
40 r = self.do_cmd("between", pairs=("%s-%s" % ("0"*40, "0"*40)))
41 l1 = ""
41 lines = ["", "dummy"]
42 l2 = "dummy"
43 max_noise = 500
42 max_noise = 500
44 while l2 and max_noise:
43 while lines[-1] and max_noise:
45 l2 = r.readline()
44 l = r.readline()
46 self.readerr()
45 self.readerr()
47 if l1 == "1\n" and l2 == "\n":
46 if lines[-1] == "1\n" and l == "\n":
48 break
47 break
49 if l1:
48 if l:
50 ui.debug(_("remote: "), l1)
49 ui.debug(_("remote: "), l)
51 l1 = l2
50 lines.append(l)
52 max_noise -= 1
51 max_noise -= 1
53 else:
52 else:
54 if l1:
53 if l1:
55 ui.debug(_("remote: "), l1)
54 ui.debug(_("remote: "), l1)
56 raise hg.RepoError(_("no response from remote hg"))
55 raise hg.RepoError(_("no response from remote hg"))
57
56
58 def readerr(self):
57 def readerr(self):
59 while 1:
58 while 1:
60 size = util.fstat(self.pipee).st_size
59 size = util.fstat(self.pipee).st_size
61 if size == 0: break
60 if size == 0: break
62 l = self.pipee.readline()
61 l = self.pipee.readline()
63 if not l: break
62 if not l: break
64 self.ui.status(_("remote: "), l)
63 self.ui.status(_("remote: "), l)
65
64
66 def __del__(self):
65 def __del__(self):
67 try:
66 try:
68 self.pipeo.close()
67 self.pipeo.close()
69 self.pipei.close()
68 self.pipei.close()
70 # read the error descriptor until EOF
69 # read the error descriptor until EOF
71 for l in self.pipee:
70 for l in self.pipee:
72 self.ui.status(_("remote: "), l)
71 self.ui.status(_("remote: "), l)
73 self.pipee.close()
72 self.pipee.close()
74 except:
73 except:
75 pass
74 pass
76
75
77 def dev(self):
76 def dev(self):
78 return -1
77 return -1
79
78
80 def do_cmd(self, cmd, **args):
79 def do_cmd(self, cmd, **args):
81 self.ui.debug(_("sending %s command\n") % cmd)
80 self.ui.debug(_("sending %s command\n") % cmd)
82 self.pipeo.write("%s\n" % cmd)
81 self.pipeo.write("%s\n" % cmd)
83 for k, v in args.items():
82 for k, v in args.items():
84 self.pipeo.write("%s %d\n" % (k, len(v)))
83 self.pipeo.write("%s %d\n" % (k, len(v)))
85 self.pipeo.write(v)
84 self.pipeo.write(v)
86 self.pipeo.flush()
85 self.pipeo.flush()
87
86
88 return self.pipei
87 return self.pipei
89
88
90 def call(self, cmd, **args):
89 def call(self, cmd, **args):
91 r = self.do_cmd(cmd, **args)
90 r = self.do_cmd(cmd, **args)
92 l = r.readline()
91 l = r.readline()
93 self.readerr()
92 self.readerr()
94 try:
93 try:
95 l = int(l)
94 l = int(l)
96 except:
95 except:
97 raise hg.RepoError(_("unexpected response '%s'") % l)
96 raise hg.RepoError(_("unexpected response '%s'") % l)
98 return r.read(l)
97 return r.read(l)
99
98
100 def lock(self):
99 def lock(self):
101 self.call("lock")
100 self.call("lock")
102 return remotelock(self)
101 return remotelock(self)
103
102
104 def unlock(self):
103 def unlock(self):
105 self.call("unlock")
104 self.call("unlock")
106
105
107 def heads(self):
106 def heads(self):
108 d = self.call("heads")
107 d = self.call("heads")
109 try:
108 try:
110 return map(bin, d[:-1].split(" "))
109 return map(bin, d[:-1].split(" "))
111 except:
110 except:
112 raise hg.RepoError(_("unexpected response '%s'") % (d[:400] + "..."))
111 raise hg.RepoError(_("unexpected response '%s'") % (d[:400] + "..."))
113
112
114 def branches(self, nodes):
113 def branches(self, nodes):
115 n = " ".join(map(hex, nodes))
114 n = " ".join(map(hex, nodes))
116 d = self.call("branches", nodes=n)
115 d = self.call("branches", nodes=n)
117 try:
116 try:
118 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
117 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
119 return br
118 return br
120 except:
119 except:
121 raise hg.RepoError(_("unexpected response '%s'") % (d[:400] + "..."))
120 raise hg.RepoError(_("unexpected response '%s'") % (d[:400] + "..."))
122
121
123 def between(self, pairs):
122 def between(self, pairs):
124 n = "\n".join(["-".join(map(hex, p)) for p in pairs])
123 n = "\n".join(["-".join(map(hex, p)) for p in pairs])
125 d = self.call("between", pairs=n)
124 d = self.call("between", pairs=n)
126 try:
125 try:
127 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
126 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
128 return p
127 return p
129 except:
128 except:
130 raise hg.RepoError(_("unexpected response '%s'") % (d[:400] + "..."))
129 raise hg.RepoError(_("unexpected response '%s'") % (d[:400] + "..."))
131
130
132 def changegroup(self, nodes, kind):
131 def changegroup(self, nodes, kind):
133 n = " ".join(map(hex, nodes))
132 n = " ".join(map(hex, nodes))
134 f = self.do_cmd("changegroup", roots=n)
133 f = self.do_cmd("changegroup", roots=n)
135 return self.pipei
134 return self.pipei
136
135
137 def addchangegroup(self, cg, source):
136 def addchangegroup(self, cg, source):
138 d = self.call("addchangegroup")
137 d = self.call("addchangegroup")
139 if d:
138 if d:
140 raise hg.RepoError(_("push refused: %s"), d)
139 raise hg.RepoError(_("push refused: %s"), d)
141
140
142 while 1:
141 while 1:
143 d = cg.read(4096)
142 d = cg.read(4096)
144 if not d: break
143 if not d: break
145 self.pipeo.write(d)
144 self.pipeo.write(d)
146 self.readerr()
145 self.readerr()
147
146
148 self.pipeo.flush()
147 self.pipeo.flush()
149
148
150 self.readerr()
149 self.readerr()
151 l = int(self.pipei.readline())
150 l = int(self.pipei.readline())
152 r = self.pipei.read(l)
151 r = self.pipei.read(l)
153 if not r:
152 if not r:
154 return 1
153 return 1
155 return int(r)
154 return int(r)
General Comments 0
You need to be logged in to leave comments. Login now