author | Matt Mackall <mpm@selenic.com> |
Wed, 30 Mar 2011 17:50:34 -0500 | |
changeset 13810 | 0252abaafb8a |
parent 13786 | 4ed718f909e5 |
child 13831 | d69c9510d648 |
permissions | -rw-r--r-- |
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
1 |
# changegroup.py - Mercurial changegroup manipulation functions |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
2 |
# |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
3 |
# Copyright 2006 Matt Mackall <mpm@selenic.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 |
# |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
10263 | 6 |
# GNU General Public License version 2 or any later version. |
3877
abaee83ce0a6
Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
3859
diff
changeset
|
7 |
|
3891 | 8 |
from i18n import _ |
8312
b87a50b7125c
separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents:
8226
diff
changeset
|
9 |
import util |
b87a50b7125c
separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents:
8226
diff
changeset
|
10 |
import struct, os, bz2, zlib, tempfile |
1981
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
11 |
|
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
12 |
def readexactly(stream, n): |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
13 |
'''read n bytes from stream.read and abort if less was available''' |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
14 |
s = stream.read(n) |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
15 |
if len(s) < n: |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
16 |
raise util.Abort(_("stream ended unexpectedly" |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
17 |
" (got %d bytes, expected %d)") |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
18 |
% (len(s), n)) |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
19 |
return s |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
20 |
|
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
21 |
def getchunk(stream): |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
22 |
"""return the next chunk from stream as a string""" |
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
23 |
d = readexactly(stream, 4) |
1981
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
24 |
l = struct.unpack(">l", d)[0] |
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
25 |
if l <= 4: |
13458
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
26 |
if l: |
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
27 |
raise util.Abort(_("invalid chunk length %d") % l) |
1981
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
28 |
return "" |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
29 |
return readexactly(stream, l - 4) |
1981
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
30 |
|
5368
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
31 |
def chunkheader(length): |
9437
1c4e4004f3a6
Improve some docstrings relating to changegroups and prepush().
Greg Ward <greg-hg@gerg.ca>
parents:
9087
diff
changeset
|
32 |
"""return a changegroup chunk header (string)""" |
5368
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
33 |
return struct.pack(">l", length + 4) |
1981
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
34 |
|
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
35 |
def closechunk(): |
9437
1c4e4004f3a6
Improve some docstrings relating to changegroups and prepush().
Greg Ward <greg-hg@gerg.ca>
parents:
9087
diff
changeset
|
36 |
"""return a changegroup chunk header (string) for a zero-length chunk""" |
1981
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
37 |
return struct.pack(">l", 0) |
736b6c96bbbc
make incoming work via ssh (issue139); move chunk code into separate module.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff
changeset
|
38 |
|
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
39 |
class nocompress(object): |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
40 |
def compress(self, x): |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
41 |
return x |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
42 |
def flush(self): |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
43 |
return "" |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
44 |
|
3662
f4dc02d7fb71
unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents:
3660
diff
changeset
|
45 |
bundletypes = { |
3704
9c1737a3e254
fix writebundle for bz2 bundles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3662
diff
changeset
|
46 |
"": ("", nocompress), |
9c1737a3e254
fix writebundle for bz2 bundles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3662
diff
changeset
|
47 |
"HG10UN": ("HG10UN", nocompress), |
3762
b9d3e12da485
changegroup.py: delay the loading of the bz2 and zlib modules
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3706
diff
changeset
|
48 |
"HG10BZ": ("HG10", lambda: bz2.BZ2Compressor()), |
b9d3e12da485
changegroup.py: delay the loading of the bz2 and zlib modules
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3706
diff
changeset
|
49 |
"HG10GZ": ("HG10GZ", lambda: zlib.compressobj()), |
3662
f4dc02d7fb71
unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents:
3660
diff
changeset
|
50 |
} |
f4dc02d7fb71
unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents:
3660
diff
changeset
|
51 |
|
9087 | 52 |
# hgweb uses this list to communicate its preferred type |
6152
c050548307a4
hgweb: use bundletypes from mercurial.changegroup
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5906
diff
changeset
|
53 |
bundlepriority = ['HG10GZ', 'HG10BZ', 'HG10UN'] |
c050548307a4
hgweb: use bundletypes from mercurial.changegroup
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5906
diff
changeset
|
54 |
|
3706
0d810798acb1
Use 'bundletype' instead of 'type' to not shadow built-in function.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3705
diff
changeset
|
55 |
def writebundle(cg, filename, bundletype): |
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
56 |
"""Write a bundle file and return its filename. |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
57 |
|
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
58 |
Existing files will not be overwritten. |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
59 |
If no filename is specified, a temporary file is created. |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
60 |
bz2 compression can be turned off. |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
61 |
The bundle file will be deleted in case of errors. |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
62 |
""" |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
63 |
|
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
64 |
fh = None |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
65 |
cleanup = None |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
66 |
try: |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
67 |
if filename: |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
68 |
fh = open(filename, "wb") |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
69 |
else: |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
70 |
fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg") |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
71 |
fh = os.fdopen(fd, "wb") |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
72 |
cleanup = filename |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
73 |
|
3706
0d810798acb1
Use 'bundletype' instead of 'type' to not shadow built-in function.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3705
diff
changeset
|
74 |
header, compressor = bundletypes[bundletype] |
3704
9c1737a3e254
fix writebundle for bz2 bundles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3662
diff
changeset
|
75 |
fh.write(header) |
9c1737a3e254
fix writebundle for bz2 bundles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3662
diff
changeset
|
76 |
z = compressor() |
3662
f4dc02d7fb71
unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents:
3660
diff
changeset
|
77 |
|
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
78 |
# parse the changegroup data, otherwise we will block |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
79 |
# in case of sshrepo because we don't know the end of the stream |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
80 |
|
12335
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
81 |
# an empty chunkgroup is the end of the changegroup |
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
82 |
# a changegroup has at least 2 chunkgroups (changelog and manifest). |
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
83 |
# after that, an empty chunkgroup is the end of the changegroup |
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
84 |
empty = False |
5906
0136d7f58982
allow the creation of bundles with empty changelog/manifest chunks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5368
diff
changeset
|
85 |
count = 0 |
0136d7f58982
allow the creation of bundles with empty changelog/manifest chunks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5368
diff
changeset
|
86 |
while not empty or count <= 2: |
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
87 |
empty = True |
5906
0136d7f58982
allow the creation of bundles with empty changelog/manifest chunks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5368
diff
changeset
|
88 |
count += 1 |
12335
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
89 |
while 1: |
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
90 |
chunk = getchunk(cg) |
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
91 |
if not chunk: |
e21fe9c5fb25
bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents:
12334
diff
changeset
|
92 |
break |
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
93 |
empty = False |
5368
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
94 |
fh.write(z.compress(chunkheader(len(chunk)))) |
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
95 |
pos = 0 |
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
96 |
while pos < len(chunk): |
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
97 |
next = pos + 2**20 |
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
98 |
fh.write(z.compress(chunk[pos:next])) |
61462e7d62ed
changegroup: avoid large copies
Matt Mackall <mpm@selenic.com>
parents:
3932
diff
changeset
|
99 |
pos = next |
3659
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
100 |
fh.write(z.compress(closechunk())) |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
101 |
fh.write(z.flush()) |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
102 |
cleanup = None |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
103 |
return filename |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
104 |
finally: |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
105 |
if fh is not None: |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
106 |
fh.close() |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
107 |
if cleanup is not None: |
025f68f22ae2
move write_bundle to changegroup.py
Matt Mackall <mpm@selenic.com>
parents:
2470
diff
changeset
|
108 |
os.unlink(cleanup) |
3660
8500a13ec44b
create a readbundle function
Matt Mackall <mpm@selenic.com>
parents:
3659
diff
changeset
|
109 |
|
12041
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
110 |
def decompressor(fh, alg): |
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
111 |
if alg == 'UN': |
6154
ef1c5a3b653d
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6152
diff
changeset
|
112 |
return fh |
12041
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
113 |
elif alg == 'GZ': |
6154
ef1c5a3b653d
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6152
diff
changeset
|
114 |
def generator(f): |
ef1c5a3b653d
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6152
diff
changeset
|
115 |
zd = zlib.decompressobj() |
ef1c5a3b653d
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6152
diff
changeset
|
116 |
for chunk in f: |
ef1c5a3b653d
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6152
diff
changeset
|
117 |
yield zd.decompress(chunk) |
12041
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
118 |
elif alg == 'BZ': |
3660
8500a13ec44b
create a readbundle function
Matt Mackall <mpm@selenic.com>
parents:
3659
diff
changeset
|
119 |
def generator(f): |
8500a13ec44b
create a readbundle function
Matt Mackall <mpm@selenic.com>
parents:
3659
diff
changeset
|
120 |
zd = bz2.BZ2Decompressor() |
8500a13ec44b
create a readbundle function
Matt Mackall <mpm@selenic.com>
parents:
3659
diff
changeset
|
121 |
zd.decompress("BZ") |
8500a13ec44b
create a readbundle function
Matt Mackall <mpm@selenic.com>
parents:
3659
diff
changeset
|
122 |
for chunk in util.filechunkiter(f, 4096): |
8500a13ec44b
create a readbundle function
Matt Mackall <mpm@selenic.com>
parents:
3659
diff
changeset
|
123 |
yield zd.decompress(chunk) |
12041
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
124 |
else: |
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
125 |
raise util.Abort("unknown bundle compression '%s'" % alg) |
12329
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
126 |
return util.chunkbuffer(generator(fh)) |
12041
270fb4d39153
bundle: factor out decompressor
Matt Mackall <mpm@selenic.com>
parents:
11648
diff
changeset
|
127 |
|
12043
bef5effb3db0
bundle: introduce bundle class
Matt Mackall <mpm@selenic.com>
parents:
12042
diff
changeset
|
128 |
class unbundle10(object): |
bef5effb3db0
bundle: introduce bundle class
Matt Mackall <mpm@selenic.com>
parents:
12042
diff
changeset
|
129 |
def __init__(self, fh, alg): |
12329
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
130 |
self._stream = decompressor(fh, alg) |
12044
bcc7139521b7
bundlerepo: remove duplication of bundle decompressors
Matt Mackall <mpm@selenic.com>
parents:
12043
diff
changeset
|
131 |
self._type = alg |
12334
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
132 |
self.callback = None |
12044
bcc7139521b7
bundlerepo: remove duplication of bundle decompressors
Matt Mackall <mpm@selenic.com>
parents:
12043
diff
changeset
|
133 |
def compressed(self): |
bcc7139521b7
bundlerepo: remove duplication of bundle decompressors
Matt Mackall <mpm@selenic.com>
parents:
12043
diff
changeset
|
134 |
return self._type != 'UN' |
12043
bef5effb3db0
bundle: introduce bundle class
Matt Mackall <mpm@selenic.com>
parents:
12042
diff
changeset
|
135 |
def read(self, l): |
bef5effb3db0
bundle: introduce bundle class
Matt Mackall <mpm@selenic.com>
parents:
12042
diff
changeset
|
136 |
return self._stream.read(l) |
12330
e527b8635881
bundle: make unbundle object seekable
Matt Mackall <mpm@selenic.com>
parents:
12329
diff
changeset
|
137 |
def seek(self, pos): |
e527b8635881
bundle: make unbundle object seekable
Matt Mackall <mpm@selenic.com>
parents:
12329
diff
changeset
|
138 |
return self._stream.seek(pos) |
e527b8635881
bundle: make unbundle object seekable
Matt Mackall <mpm@selenic.com>
parents:
12329
diff
changeset
|
139 |
def tell(self): |
12332
680fe77ab5b8
bundlerepo: use bundle objects everywhere
Matt Mackall <mpm@selenic.com>
parents:
12330
diff
changeset
|
140 |
return self._stream.tell() |
12347
6277a9469dff
bundlerepo: restore close() method
Matt Mackall <mpm@selenic.com>
parents:
12336
diff
changeset
|
141 |
def close(self): |
6277a9469dff
bundlerepo: restore close() method
Matt Mackall <mpm@selenic.com>
parents:
12336
diff
changeset
|
142 |
return self._stream.close() |
12334
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
143 |
|
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
144 |
def chunklength(self): |
13459
acbe171c8fbe
changegroup: fix typo introduced in 9f2c407caf34
Jim Hague <jim.hague@acm.org>
parents:
13458
diff
changeset
|
145 |
d = readexactly(self._stream, 4) |
13458
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
146 |
l = struct.unpack(">l", d)[0] |
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
147 |
if l <= 4: |
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
148 |
if l: |
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
149 |
raise util.Abort(_("invalid chunk length %d") % l) |
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
150 |
return 0 |
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
151 |
if self.callback: |
12334
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
152 |
self.callback() |
13458
9f2c407caf34
changegroup: don't accept odd chunk headers
Mads Kiilerich <mads@kiilerich.com>
parents:
13457
diff
changeset
|
153 |
return l - 4 |
12334
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
154 |
|
12333
44c7dfc2f6a3
bundle: make getchunk() a method
Matt Mackall <mpm@selenic.com>
parents:
12332
diff
changeset
|
155 |
def chunk(self): |
12334
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
156 |
"""return the next chunk from changegroup 'source' as a string""" |
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
157 |
l = self.chunklength() |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
158 |
return readexactly(self._stream, l) |
12334
50946802593d
bundle: refactor progress callback
Matt Mackall <mpm@selenic.com>
parents:
12333
diff
changeset
|
159 |
|
12336
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
160 |
def parsechunk(self): |
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
161 |
l = self.chunklength() |
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
162 |
if not l: |
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
163 |
return {} |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
164 |
h = readexactly(self._stream, 80) |
12336
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
165 |
node, p1, p2, cs = struct.unpack("20s20s20s20s", h) |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
166 |
data = readexactly(self._stream, l - 80) |
12336
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
167 |
return dict(node=node, p1=p1, p2=p2, cs=cs, data=data) |
9d234f7d8a77
bundle: move chunk parsing into unbundle class
Matt Mackall <mpm@selenic.com>
parents:
12335
diff
changeset
|
168 |
|
12329
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
169 |
class headerlessfixup(object): |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
170 |
def __init__(self, fh, h): |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
171 |
self._h = h |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
172 |
self._fh = fh |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
173 |
def read(self, n): |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
174 |
if self._h: |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
175 |
d, self._h = self._h[:n], self._h[n:] |
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
176 |
if len(d) < n: |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
177 |
d += readexactly(self._fh, n - len(d)) |
12329
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
178 |
return d |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
179 |
return readexactly(self._fh, n) |
12329
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
180 |
|
6154
ef1c5a3b653d
improve changegroup.readbundle(), use it in hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6152
diff
changeset
|
181 |
def readbundle(fh, fname): |
13457
e74fe15dc7fd
changegroup: verify all stream reads
Mads Kiilerich <mads@kiilerich.com>
parents:
13456
diff
changeset
|
182 |
header = readexactly(fh, 6) |
12042
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
183 |
|
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
184 |
if not fname: |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
185 |
fname = "stream" |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
186 |
if not header.startswith('HG') and header.startswith('\0'): |
12329
7458de933f26
bundle: push chunkbuffer down into decompress
Matt Mackall <mpm@selenic.com>
parents:
12044
diff
changeset
|
187 |
fh = headerlessfixup(fh, header) |
12042
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
188 |
header = "HG10UN" |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
189 |
|
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
190 |
magic, version, alg = header[0:2], header[2:4], header[4:6] |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
191 |
|
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
192 |
if magic != 'HG': |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
193 |
raise util.Abort(_('%s: not a Mercurial bundle') % fname) |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
194 |
if version != '10': |
210049a8d16e
bundle: unify/refactor unbundle/readbundle
Matt Mackall <mpm@selenic.com>
parents:
12041
diff
changeset
|
195 |
raise util.Abort(_('%s: unknown bundle version %s') % (fname, version)) |
12043
bef5effb3db0
bundle: introduce bundle class
Matt Mackall <mpm@selenic.com>
parents:
12042
diff
changeset
|
196 |
return unbundle10(fh, alg) |