##// END OF EJS Templates
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman -
r6154:ef1c5a3b default
parent child Browse files
Show More
@@ -108,22 +108,34 b' def writebundle(cg, filename, bundletype'
108 if cleanup is not None:
108 if cleanup is not None:
109 os.unlink(cleanup)
109 os.unlink(cleanup)
110
110
111 def readbundle(fh, fname):
111 def unbundle(header, fh):
112 header = fh.read(6)
112 if header == 'HG10UN':
113 if not header.startswith("HG"):
113 return fh
114 raise util.Abort(_("%s: not a Mercurial bundle file") % fname)
114 elif not header.startswith('HG'):
115 elif not header.startswith("HG10"):
115 # old client with uncompressed bundle
116 raise util.Abort(_("%s: unknown bundle version") % fname)
116 def generator(f):
117
117 yield header
118 if header == "HG10BZ":
118 for chunk in f:
119 yield chunk
120 elif header == 'HG10GZ':
121 def generator(f):
122 zd = zlib.decompressobj()
123 for chunk in f:
124 yield zd.decompress(chunk)
125 elif header == 'HG10BZ':
119 def generator(f):
126 def generator(f):
120 zd = bz2.BZ2Decompressor()
127 zd = bz2.BZ2Decompressor()
121 zd.decompress("BZ")
128 zd.decompress("BZ")
122 for chunk in util.filechunkiter(f, 4096):
129 for chunk in util.filechunkiter(f, 4096):
123 yield zd.decompress(chunk)
130 yield zd.decompress(chunk)
124 return util.chunkbuffer(generator(fh))
131 return util.chunkbuffer(generator(fh))
125 elif header == "HG10UN":
126 return fh
127
132
128 raise util.Abort(_("%s: unknown bundle compression type")
133 def readbundle(fh, fname):
129 % fname)
134 header = fh.read(6)
135 if not header.startswith('HG'):
136 raise util.Abort(_('%s: not a Mercurial bundle file') % fname)
137 if not header.startswith('HG10'):
138 raise util.Abort(_('%s: unknown bundle version') % fname)
139 elif header not in bundletypes:
140 raise util.Abort(_('%s: unknown bundle compression type') % fname)
141 return unbundle(header, fh)
@@ -9,6 +9,7 b' import cStringIO, zlib, bz2, tempfile, e'
9 from mercurial import util, streamclone
9 from mercurial import util, streamclone
10 from mercurial.i18n import gettext as _
10 from mercurial.i18n import gettext as _
11 from mercurial.node import *
11 from mercurial.node import *
12 from mercurial import changegroup as changegroupmod
12 from common import HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
13 from common import HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
13
14
14 # __all__ is populated with the allowed commands. Be sure to add to it if
15 # __all__ is populated with the allowed commands. Be sure to add to it if
@@ -167,36 +168,11 b' def unbundle(web, req):'
167
168
168 fp.seek(0)
169 fp.seek(0)
169 header = fp.read(6)
170 header = fp.read(6)
170 if not header.startswith("HG"):
171 if header.startswith('HG') and not header.startswith('HG10'):
171 # old client with uncompressed bundle
172 raise ValueError('unknown bundle version')
172 def generator(f):
173 elif header not in changegroupmod.bundletypes:
173 yield header
174 raise ValueError('unknown bundle compression type')
174 for chunk in f:
175 gen = changegroupmod.unbundle(header, fp)
175 yield chunk
176 elif not header.startswith("HG10"):
177 req.write("0\n")
178 req.write(_("unknown bundle version\n"))
179 return
180 elif header == "HG10GZ":
181 def generator(f):
182 zd = zlib.decompressobj()
183 for chunk in f:
184 yield zd.decompress(chunk)
185 elif header == "HG10BZ":
186 def generator(f):
187 zd = bz2.BZ2Decompressor()
188 zd.decompress("BZ")
189 for chunk in f:
190 yield zd.decompress(chunk)
191 elif header == "HG10UN":
192 def generator(f):
193 for chunk in f:
194 yield chunk
195 else:
196 req.write("0\n")
197 req.write(_("unknown bundle compression type\n"))
198 return
199 gen = generator(util.filechunkiter(fp, 4096))
200
176
201 # send addchangegroup output to client
177 # send addchangegroup output to client
202
178
@@ -207,8 +183,7 b' def unbundle(web, req):'
207 url = 'remote:%s:%s' % (proto,
183 url = 'remote:%s:%s' % (proto,
208 req.env.get('REMOTE_HOST', ''))
184 req.env.get('REMOTE_HOST', ''))
209 try:
185 try:
210 ret = web.repo.addchangegroup(
186 ret = web.repo.addchangegroup(gen, 'serve', url)
211 util.chunkbuffer(gen), 'serve', url)
212 except util.Abort, inst:
187 except util.Abort, inst:
213 sys.stdout.write("abort: %s\n" % inst)
188 sys.stdout.write("abort: %s\n" % inst)
214 ret = 0
189 ret = 0
@@ -219,6 +194,9 b' def unbundle(web, req):'
219 req.write(val)
194 req.write(val)
220 finally:
195 finally:
221 del lock
196 del lock
197 except ValueError, inst:
198 req.write('0\n')
199 req.write(str(inst) + '\n')
222 except (OSError, IOError), inst:
200 except (OSError, IOError), inst:
223 req.write('0\n')
201 req.write('0\n')
224 filename = getattr(inst, 'filename', '')
202 filename = getattr(inst, 'filename', '')
General Comments 0
You need to be logged in to leave comments. Login now