##// END OF EJS Templates
hgweb: fix handling of arguments in the between command...
Sune Foldager -
r10530:ed87b6c6 stable
parent child Browse files
Show More
@@ -1,208 +1,207
1 1 #
2 2 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 import cStringIO, zlib, tempfile, errno, os, sys, urllib, copy
9 9 from mercurial import util, streamclone
10 10 from mercurial.node import bin, hex
11 11 from mercurial import changegroup as changegroupmod
12 12 from common import ErrorResponse, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
13 13
14 14 # __all__ is populated with the allowed commands. Be sure to add to it if
15 15 # you're adding a new command, or the new command won't work.
16 16
17 17 __all__ = [
18 18 'lookup', 'heads', 'branches', 'between', 'changegroup',
19 19 'changegroupsubset', 'capabilities', 'unbundle', 'stream_out',
20 20 'branchmap',
21 21 ]
22 22
23 23 HGTYPE = 'application/mercurial-0.1'
24 24 basecaps = 'lookup changegroupsubset branchmap'.split()
25 25
26 26 def lookup(repo, req):
27 27 try:
28 28 r = hex(repo.lookup(req.form['key'][0]))
29 29 success = 1
30 30 except Exception, inst:
31 31 r = str(inst)
32 32 success = 0
33 33 resp = "%s %s\n" % (success, r)
34 34 req.respond(HTTP_OK, HGTYPE, length=len(resp))
35 35 yield resp
36 36
37 37 def heads(repo, req):
38 38 resp = " ".join(map(hex, repo.heads())) + "\n"
39 39 req.respond(HTTP_OK, HGTYPE, length=len(resp))
40 40 yield resp
41 41
42 42 def branchmap(repo, req):
43 43 branches = repo.branchmap()
44 44 heads = []
45 45 for branch, nodes in branches.iteritems():
46 46 branchname = urllib.quote(branch)
47 47 branchnodes = [hex(node) for node in nodes]
48 48 heads.append('%s %s' % (branchname, ' '.join(branchnodes)))
49 49 resp = '\n'.join(heads)
50 50 req.respond(HTTP_OK, HGTYPE, length=len(resp))
51 51 yield resp
52 52
53 53 def branches(repo, req):
54 54 nodes = []
55 55 if 'nodes' in req.form:
56 56 nodes = map(bin, req.form['nodes'][0].split(" "))
57 57 resp = cStringIO.StringIO()
58 58 for b in repo.branches(nodes):
59 59 resp.write(" ".join(map(hex, b)) + "\n")
60 60 resp = resp.getvalue()
61 61 req.respond(HTTP_OK, HGTYPE, length=len(resp))
62 62 yield resp
63 63
64 64 def between(repo, req):
65 if 'pairs' in req.form:
66 pairs = [map(bin, p.split("-"))
67 for p in req.form['pairs'][0].split(" ")]
65 pairs = [map(bin, p.split("-"))
66 for p in req.form['pairs'][0].split(" ")]
68 67 resp = cStringIO.StringIO()
69 68 for b in repo.between(pairs):
70 69 resp.write(" ".join(map(hex, b)) + "\n")
71 70 resp = resp.getvalue()
72 71 req.respond(HTTP_OK, HGTYPE, length=len(resp))
73 72 yield resp
74 73
75 74 def changegroup(repo, req):
76 75 req.respond(HTTP_OK, HGTYPE)
77 76 nodes = []
78 77
79 78 if 'roots' in req.form:
80 79 nodes = map(bin, req.form['roots'][0].split(" "))
81 80
82 81 z = zlib.compressobj()
83 82 f = repo.changegroup(nodes, 'serve')
84 83 while 1:
85 84 chunk = f.read(4096)
86 85 if not chunk:
87 86 break
88 87 yield z.compress(chunk)
89 88
90 89 yield z.flush()
91 90
92 91 def changegroupsubset(repo, req):
93 92 req.respond(HTTP_OK, HGTYPE)
94 93 bases = []
95 94 heads = []
96 95
97 96 if 'bases' in req.form:
98 97 bases = [bin(x) for x in req.form['bases'][0].split(' ')]
99 98 if 'heads' in req.form:
100 99 heads = [bin(x) for x in req.form['heads'][0].split(' ')]
101 100
102 101 z = zlib.compressobj()
103 102 f = repo.changegroupsubset(bases, heads, 'serve')
104 103 while 1:
105 104 chunk = f.read(4096)
106 105 if not chunk:
107 106 break
108 107 yield z.compress(chunk)
109 108
110 109 yield z.flush()
111 110
112 111 def capabilities(repo, req):
113 112 caps = copy.copy(basecaps)
114 113 if streamclone.allowed(repo.ui):
115 114 caps.append('stream=%d' % repo.changelog.version)
116 115 if changegroupmod.bundlepriority:
117 116 caps.append('unbundle=%s' % ','.join(changegroupmod.bundlepriority))
118 117 rsp = ' '.join(caps)
119 118 req.respond(HTTP_OK, HGTYPE, length=len(rsp))
120 119 yield rsp
121 120
122 121 def unbundle(repo, req):
123 122
124 123 proto = req.env.get('wsgi.url_scheme') or 'http'
125 124 their_heads = req.form['heads'][0].split(' ')
126 125
127 126 def check_heads():
128 127 heads = map(hex, repo.heads())
129 128 return their_heads == [hex('force')] or their_heads == heads
130 129
131 130 # fail early if possible
132 131 if not check_heads():
133 132 req.drain()
134 133 raise ErrorResponse(HTTP_OK, 'unsynced changes')
135 134
136 135 # do not lock repo until all changegroup data is
137 136 # streamed. save to temporary file.
138 137
139 138 fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
140 139 fp = os.fdopen(fd, 'wb+')
141 140 try:
142 141 length = int(req.env['CONTENT_LENGTH'])
143 142 for s in util.filechunkiter(req, limit=length):
144 143 fp.write(s)
145 144
146 145 try:
147 146 lock = repo.lock()
148 147 try:
149 148 if not check_heads():
150 149 raise ErrorResponse(HTTP_OK, 'unsynced changes')
151 150
152 151 fp.seek(0)
153 152 header = fp.read(6)
154 153 if header.startswith('HG') and not header.startswith('HG10'):
155 154 raise ValueError('unknown bundle version')
156 155 elif header not in changegroupmod.bundletypes:
157 156 raise ValueError('unknown bundle compression type')
158 157 gen = changegroupmod.unbundle(header, fp)
159 158
160 159 # send addchangegroup output to client
161 160
162 161 oldio = sys.stdout, sys.stderr
163 162 sys.stderr = sys.stdout = cStringIO.StringIO()
164 163
165 164 try:
166 165 url = 'remote:%s:%s:%s' % (
167 166 proto,
168 167 urllib.quote(req.env.get('REMOTE_HOST', '')),
169 168 urllib.quote(req.env.get('REMOTE_USER', '')))
170 169 try:
171 170 ret = repo.addchangegroup(gen, 'serve', url)
172 171 except util.Abort, inst:
173 172 sys.stdout.write("abort: %s\n" % inst)
174 173 ret = 0
175 174 finally:
176 175 val = sys.stdout.getvalue()
177 176 sys.stdout, sys.stderr = oldio
178 177 req.respond(HTTP_OK, HGTYPE)
179 178 return '%d\n%s' % (ret, val),
180 179 finally:
181 180 lock.release()
182 181 except ValueError, inst:
183 182 raise ErrorResponse(HTTP_OK, inst)
184 183 except (OSError, IOError), inst:
185 184 error = getattr(inst, 'strerror', 'Unknown error')
186 185 if inst.errno == errno.ENOENT:
187 186 code = HTTP_NOT_FOUND
188 187 else:
189 188 code = HTTP_SERVER_ERROR
190 189 filename = getattr(inst, 'filename', '')
191 190 # Don't send our filesystem layout to the client
192 191 if filename and filename.startswith(repo.root):
193 192 filename = filename[len(repo.root)+1:]
194 193 text = '%s: %s' % (error, filename)
195 194 else:
196 195 text = error.replace(repo.root + os.path.sep, '')
197 196 raise ErrorResponse(code, text)
198 197 finally:
199 198 fp.close()
200 199 os.unlink(tempname)
201 200
202 201 def stream_out(repo, req):
203 202 req.respond(HTTP_OK, HGTYPE)
204 203 try:
205 204 for chunk in streamclone.stream_out(repo):
206 205 yield chunk
207 206 except streamclone.StreamException, inst:
208 207 yield str(inst)
General Comments 0
You need to be logged in to leave comments. Login now