##// END OF EJS Templates
unduplicate bundle writing code from httprepo
Matt Mackall -
r3662:f4dc02d7 default
parent child Browse files
Show More
@@ -8,7 +8,7 b' of the GNU General Public License, incor'
8 """
8 """
9 from i18n import gettext as _
9 from i18n import gettext as _
10 from demandload import *
10 from demandload import *
11 demandload(globals(), "struct os bz2 util tempfile")
11 demandload(globals(), "struct os bz2 zlib util tempfile")
12
12
13 def getchunk(source):
13 def getchunk(source):
14 """get a chunk from a changegroup"""
14 """get a chunk from a changegroup"""
@@ -47,7 +47,14 b' class nocompress(object):'
47 def flush(self):
47 def flush(self):
48 return ""
48 return ""
49
49
50 def writebundle(cg, filename, compress):
50 bundletypes = {
51 "": nocompress,
52 "HG10UN": nocompress,
53 "HG10": lambda: bz2.BZ2Compressor(9),
54 "HG10GZ": zlib.compressobj,
55 }
56
57 def writebundle(cg, filename, type):
51 """Write a bundle file and return its filename.
58 """Write a bundle file and return its filename.
52
59
53 Existing files will not be overwritten.
60 Existing files will not be overwritten.
@@ -68,12 +75,9 b' def writebundle(cg, filename, compress):'
68 fh = os.fdopen(fd, "wb")
75 fh = os.fdopen(fd, "wb")
69 cleanup = filename
76 cleanup = filename
70
77
71 if compress:
78 fh.write(type)
72 fh.write("HG10")
79 z = bundletypes[type]()
73 z = bz2.BZ2Compressor(9)
80
74 else:
75 fh.write("HG10UN")
76 z = nocompress()
77 # parse the changegroup data, otherwise we will block
81 # parse the changegroup data, otherwise we will block
78 # in case of sshrepo because we don't know the end of the stream
82 # in case of sshrepo because we don't know the end of the stream
79
83
@@ -332,7 +332,7 b' def bundle(ui, repo, fname, dest=None, *'
332 cg = repo.changegroupsubset(o, revs, 'bundle')
332 cg = repo.changegroupsubset(o, revs, 'bundle')
333 else:
333 else:
334 cg = repo.changegroup(o, 'bundle')
334 cg = repo.changegroup(o, 'bundle')
335 changegroup.writebundle(cg, fname, False)
335 changegroup.writebundle(cg, fname, "HG10")
336
336
337 def cat(ui, repo, file1, *pats, **opts):
337 def cat(ui, repo, file1, *pats, **opts):
338 """output the latest or given revisions of files
338 """output the latest or given revisions of files
@@ -1292,7 +1292,8 b' def incoming(ui, repo, source="default",'
1292 if fname or not other.local():
1292 if fname or not other.local():
1293 # create a bundle (uncompressed if other repo is not local)
1293 # create a bundle (uncompressed if other repo is not local)
1294 cg = other.changegroup(incoming, "incoming")
1294 cg = other.changegroup(incoming, "incoming")
1295 fname = cleanup = changegroup.writebundle(cg, fname, other.local())
1295 type = other.local() and "HG10" or "HG10UN"
1296 fname = cleanup = changegroup.writebundle(cg, fname, type)
1296 # keep written bundle?
1297 # keep written bundle?
1297 if opts["bundle"]:
1298 if opts["bundle"]:
1298 cleanup = None
1299 cleanup = None
@@ -11,7 +11,7 b' from remoterepo import *'
11 from i18n import gettext as _
11 from i18n import gettext as _
12 from demandload import *
12 from demandload import *
13 demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib")
13 demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib")
14 demandload(globals(), "errno keepalive tempfile socket")
14 demandload(globals(), "errno keepalive tempfile socket changegroup")
15
15
16 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
16 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
17 def __init__(self, ui):
17 def __init__(self, ui):
@@ -326,47 +326,18 b' class httprepository(remoterepository):'
326 # have to stream bundle to a temp file because we do not have
326 # have to stream bundle to a temp file because we do not have
327 # http 1.1 chunked transfer.
327 # http 1.1 chunked transfer.
328
328
329 # XXX duplication from commands.py
329 type = ""
330 class nocompress(object):
330 types = self.capable('unbundle')
331 def compress(self, x):
331 if types:
332 return x
332 for x in types.split(','):
333 def flush(self):
333 if x in changegroup.bundletypes:
334 return ""
334 type = x
335
335 break
336 unbundleversions = self.capable('unbundle')
337 try:
338 unbundleversions = unbundleversions.split(',')
339 except AttributeError:
340 unbundleversions = [""]
341
336
342 while unbundleversions:
337 tempname = changegroup.writebundle(cg, None, type)
343 header = unbundleversions[0]
338 fp = file(tempname, "rb")
344 if header == "HG10GZ":
345 self.ui.note(_("using zlib compression\n"))
346 z = zlib.compressobj()
347 break
348 elif header == "HG10UN":
349 self.ui.note(_("using no compression\n"))
350 z = nocompress()
351 break
352 elif header == "":
353 self.ui.note(_("old server without compression support,"
354 " sending uncompressed\n"))
355 z = nocompress()
356 break
357 unbundleversions.pop(0)
358 if not unbundleversions:
359 raise util.Abort(_("The server doesn't accept any bundle format"
360 " method we know."))
361
362 fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
363 fp = os.fdopen(fd, 'wb+')
364 try:
339 try:
365 fp.write(header)
340 length = os.stat(tempname).st_size
366 for chunk in util.filechunkiter(cg):
367 fp.write(z.compress(chunk))
368 fp.write(z.flush())
369 length = fp.tell()
370 try:
341 try:
371 rfp = self.do_cmd(
342 rfp = self.do_cmd(
372 'unbundle', data=fp,
343 'unbundle', data=fp,
General Comments 0
You need to be logged in to leave comments. Login now