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