Mercurial > hg > gitserve
annotate mercurial/revlog.py @ 1402:9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
---|---|
date | Tue, 18 Oct 2005 18:38:39 -0700 |
parents | cf9a1233738a |
children | bc3e66edb04c |
rev | line source |
---|---|
1083 | 1 """ |
2 revlog.py - storage back-end for mercurial | |
3 | |
4 This provides efficient delta storage with O(1) retrieve and append | |
5 and O(changes) merge between branches | |
6 | |
7 Copyright 2005 Matt Mackall <mpm@selenic.com> | |
8 | |
9 This software may be used and distributed according to the terms | |
10 of the GNU General Public License, incorporated herein by reference. | |
11 """ | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
12 |
1089 | 13 from node import * |
1400
cf9a1233738a
i18n first part: make '_' available for files who need it
Benoit Boissinot <benoit.boissinot@ens-lyon.org
parents:
1393
diff
changeset
|
14 from i18n import gettext as _ |
1322
b3d44e9b3092
Make revlog constructor more discerning in its treatment of errors.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1232
diff
changeset
|
15 from demandload import demandload |
1325
57220daf40e9
Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1322
diff
changeset
|
16 demandload(globals(), "binascii errno heapq mdiff sha struct zlib") |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
17 |
1091
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
18 def hash(text, p1, p2): |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
19 """generate a hash from the given text and its parent hashes |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
20 |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
21 This hash combines both the current file contents and its history |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
22 in a manner that makes it easy to distinguish nodes with the same |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
23 content in the revision graph. |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
24 """ |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
25 l = [p1, p2] |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
26 l.sort() |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
27 s = sha.new(l[0]) |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
28 s.update(l[1]) |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
29 s.update(text) |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
30 return s.digest() |
d62130f99a73
Move hash function back to revlog from node
mpm@selenic.com
parents:
1089
diff
changeset
|
31 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
32 def compress(text): |
1083 | 33 """ generate a possibly-compressed representation of text """ |
112 | 34 if not text: return text |
35 if len(text) < 44: | |
36 if text[0] == '\0': return text | |
37 return 'u' + text | |
38 bin = zlib.compress(text) | |
39 if len(bin) > len(text): | |
40 if text[0] == '\0': return text | |
41 return 'u' + text | |
42 return bin | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
43 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
44 def decompress(bin): |
1083 | 45 """ decompress the given input """ |
112 | 46 if not bin: return bin |
47 t = bin[0] | |
48 if t == '\0': return bin | |
49 if t == 'x': return zlib.decompress(bin) | |
50 if t == 'u': return bin[1:] | |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
51 raise RevlogError(_("unknown compression type %s") % t) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
52 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
53 indexformat = ">4l20s20s20s" |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
54 |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
55 class lazyparser: |
1083 | 56 """ |
57 this class avoids the need to parse the entirety of large indices | |
58 | |
59 By default we parse and load 1000 entries at a time. | |
60 | |
61 If no position is specified, we load the whole index, and replace | |
62 the lazy objects in revlog with the underlying objects for | |
63 efficiency in cases where we look at most of the nodes. | |
64 """ | |
323 | 65 def __init__(self, data, revlog): |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
66 self.data = data |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
67 self.s = struct.calcsize(indexformat) |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
68 self.l = len(data)/self.s |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
69 self.index = [None] * self.l |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
70 self.map = {nullid: -1} |
323 | 71 self.all = 0 |
72 self.revlog = revlog | |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
73 |
323 | 74 def load(self, pos=None): |
75 if self.all: return | |
76 if pos is not None: | |
77 block = pos / 1000 | |
78 i = block * 1000 | |
79 end = min(self.l, i + 1000) | |
80 else: | |
81 self.all = 1 | |
82 i = 0 | |
83 end = self.l | |
84 self.revlog.index = self.index | |
85 self.revlog.nodemap = self.map | |
515 | 86 |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
87 while i < end: |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
88 d = self.data[i * self.s: (i + 1) * self.s] |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
89 e = struct.unpack(indexformat, d) |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
90 self.index[i] = e |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
91 self.map[e[6]] = i |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
92 i += 1 |
515 | 93 |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
94 class lazyindex: |
1083 | 95 """a lazy version of the index array""" |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
96 def __init__(self, parser): |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
97 self.p = parser |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
98 def __len__(self): |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
99 return len(self.p.index) |
115 | 100 def load(self, pos): |
101 self.p.load(pos) | |
102 return self.p.index[pos] | |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
103 def __getitem__(self, pos): |
115 | 104 return self.p.index[pos] or self.load(pos) |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
105 def append(self, e): |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
106 self.p.index.append(e) |
515 | 107 |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
108 class lazymap: |
1083 | 109 """a lazy version of the node map""" |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
110 def __init__(self, parser): |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
111 self.p = parser |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
112 def load(self, key): |
323 | 113 if self.p.all: return |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
114 n = self.p.data.find(key) |
1214 | 115 if n < 0: |
116 raise KeyError(key) | |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
117 pos = n / self.p.s |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
118 self.p.load(pos) |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
119 def __contains__(self, key): |
323 | 120 self.p.load() |
121 return key in self.p.map | |
97 | 122 def __iter__(self): |
469 | 123 yield nullid |
97 | 124 for i in xrange(self.p.l): |
125 try: | |
126 yield self.p.index[i][6] | |
127 except: | |
128 self.p.load(i) | |
129 yield self.p.index[i][6] | |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
130 def __getitem__(self, key): |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
131 try: |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
132 return self.p.map[key] |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
133 except KeyError: |
86
1b945e8ba67b
Friendlier exceptions for unknown node errors
mpm@selenic.com
parents:
84
diff
changeset
|
134 try: |
1b945e8ba67b
Friendlier exceptions for unknown node errors
mpm@selenic.com
parents:
84
diff
changeset
|
135 self.load(key) |
1b945e8ba67b
Friendlier exceptions for unknown node errors
mpm@selenic.com
parents:
84
diff
changeset
|
136 return self.p.map[key] |
1b945e8ba67b
Friendlier exceptions for unknown node errors
mpm@selenic.com
parents:
84
diff
changeset
|
137 except KeyError: |
1b945e8ba67b
Friendlier exceptions for unknown node errors
mpm@selenic.com
parents:
84
diff
changeset
|
138 raise KeyError("node " + hex(key)) |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
139 def __setitem__(self, key, val): |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
140 self.p.map[key] = val |
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
141 |
1073
7b35a980b982
[PATCH] raise exceptions with Exception subclasses
Bart Trojanowski <bart@jukie.net>
parents:
1062
diff
changeset
|
142 class RevlogError(Exception): pass |
7b35a980b982
[PATCH] raise exceptions with Exception subclasses
Bart Trojanowski <bart@jukie.net>
parents:
1062
diff
changeset
|
143 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
144 class revlog: |
1083 | 145 """ |
146 the underlying revision storage object | |
147 | |
148 A revlog consists of two parts, an index and the revision data. | |
149 | |
150 The index is a file with a fixed record size containing | |
151 information on each revision, includings its nodeid (hash), the | |
152 nodeids of its parents, the position and offset of its data within | |
153 the data file, and the revision it's based on. Finally, each entry | |
154 contains a linkrev entry that can serve as a pointer to external | |
155 data. | |
156 | |
157 The revision data itself is a linear collection of data chunks. | |
158 Each chunk represents a revision and is usually represented as a | |
159 delta against the previous chunk. To bound lookup time, runs of | |
160 deltas are limited to about 2 times the length of the original | |
161 version data. This makes retrieval of a version proportional to | |
162 its size, or O(1) relative to the number of revisions. | |
163 | |
164 Both pieces of the revlog are written to in an append-only | |
165 fashion, which means we never need to rewrite a file to insert or | |
166 remove data, and can use some simple techniques to avoid the need | |
167 for locking while reading. | |
168 """ | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
169 def __init__(self, opener, indexfile, datafile): |
1083 | 170 """ |
171 create a revlog object | |
172 | |
173 opener is a function that abstracts the file opening operation | |
174 and can be used to implement COW semantics or the like. | |
175 """ | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
176 self.indexfile = indexfile |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
177 self.datafile = datafile |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
178 self.opener = opener |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
179 self.cache = None |
116
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
180 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
181 try: |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
182 i = self.opener(self.indexfile).read() |
1322
b3d44e9b3092
Make revlog constructor more discerning in its treatment of errors.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1232
diff
changeset
|
183 except IOError, inst: |
b3d44e9b3092
Make revlog constructor more discerning in its treatment of errors.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1232
diff
changeset
|
184 if inst.errno != errno.ENOENT: |
b3d44e9b3092
Make revlog constructor more discerning in its treatment of errors.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1232
diff
changeset
|
185 raise |
76
d993ebd69d28
Add lazy{parser,index,map} to speed up processing of index files
mpm@selenic.com
parents:
73
diff
changeset
|
186 i = "" |
116
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
187 |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
188 if len(i) > 10000: |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
189 # big index, let's parse it on demand |
323 | 190 parser = lazyparser(i, self) |
116
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
191 self.index = lazyindex(parser) |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
192 self.nodemap = lazymap(parser) |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
193 else: |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
194 s = struct.calcsize(indexformat) |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
195 l = len(i) / s |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
196 self.index = [None] * l |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
197 m = [None] * l |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
198 |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
199 n = 0 |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
200 for f in xrange(0, len(i), s): |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
201 # offset, size, base, linkrev, p1, p2, nodeid |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
202 e = struct.unpack(indexformat, i[f:f + s]) |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
203 m[n] = (e[6], n) |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
204 self.index[n] = e |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
205 n += 1 |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
206 |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
207 self.nodemap = dict(m) |
e484cd5ec282
Only use lazy indexing for big indices and avoid the overhead of the
mpm@selenic.com
parents:
115
diff
changeset
|
208 self.nodemap[nullid] = -1 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
209 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
210 def tip(self): return self.node(len(self.index) - 1) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
211 def count(self): return len(self.index) |
26 | 212 def node(self, rev): return (rev < 0) and nullid or self.index[rev][6] |
1201
59bfbdbc38f6
revlog: raise informative exception if file is missing.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1099
diff
changeset
|
213 def rev(self, node): |
59bfbdbc38f6
revlog: raise informative exception if file is missing.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1099
diff
changeset
|
214 try: |
59bfbdbc38f6
revlog: raise informative exception if file is missing.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1099
diff
changeset
|
215 return self.nodemap[node] |
59bfbdbc38f6
revlog: raise informative exception if file is missing.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1099
diff
changeset
|
216 except KeyError: |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
217 raise RevlogError(_('%s: no node %s') % (self.indexfile, hex(node))) |
1201
59bfbdbc38f6
revlog: raise informative exception if file is missing.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1099
diff
changeset
|
218 def linkrev(self, node): return self.index[self.rev(node)][3] |
2 | 219 def parents(self, node): |
220 if node == nullid: return (nullid, nullid) | |
1201
59bfbdbc38f6
revlog: raise informative exception if file is missing.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1099
diff
changeset
|
221 return self.index[self.rev(node)][4:6] |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
222 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
223 def start(self, rev): return self.index[rev][0] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
224 def length(self, rev): return self.index[rev][1] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
225 def end(self, rev): return self.start(rev) + self.length(rev) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
226 def base(self, rev): return self.index[rev][2] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
227 |
1074
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
228 def reachable(self, rev, stop=None): |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
229 reachable = {} |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
230 visit = [rev] |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
231 reachable[rev] = 1 |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
232 if stop: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
233 stopn = self.rev(stop) |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
234 else: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
235 stopn = 0 |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
236 while visit: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
237 n = visit.pop(0) |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
238 if n == stop: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
239 continue |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
240 if n == nullid: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
241 continue |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
242 for p in self.parents(n): |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
243 if self.rev(p) < stopn: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
244 continue |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
245 if p not in reachable: |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
246 reachable[p] = 1 |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
247 visit.append(p) |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
248 return reachable |
55bf5cfde69e
Add revlog.reachable to find a graph of ancestors for a given rev
mason@suse.com
parents:
1073
diff
changeset
|
249 |
902 | 250 def heads(self, stop=None): |
1083 | 251 """return the list of all nodes that have no children""" |
221 | 252 p = {} |
253 h = [] | |
902 | 254 stoprev = 0 |
255 if stop and stop in self.nodemap: | |
256 stoprev = self.rev(stop) | |
1083 | 257 |
243 | 258 for r in range(self.count() - 1, -1, -1): |
221 | 259 n = self.node(r) |
260 if n not in p: | |
261 h.append(n) | |
902 | 262 if n == stop: |
263 break | |
264 if r < stoprev: | |
265 break | |
221 | 266 for pn in self.parents(n): |
267 p[pn] = 1 | |
268 return h | |
370 | 269 |
270 def children(self, node): | |
1083 | 271 """find the children of a given node""" |
370 | 272 c = [] |
273 p = self.rev(node) | |
274 for r in range(p + 1, self.count()): | |
275 n = self.node(r) | |
276 for pn in self.parents(n): | |
854
473c030d34a6
Fixed revlog.children.
Tristan Wibberley <tristan@wibberley.org>
parents:
655
diff
changeset
|
277 if pn == node: |
473c030d34a6
Fixed revlog.children.
Tristan Wibberley <tristan@wibberley.org>
parents:
655
diff
changeset
|
278 c.append(n) |
370 | 279 continue |
280 elif pn == nullid: | |
281 continue | |
282 return c | |
515 | 283 |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
284 def lookup(self, id): |
1083 | 285 """locate a node based on revision number or subset of hex nodeid""" |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
286 try: |
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
287 rev = int(id) |
469 | 288 if str(rev) != id: raise ValueError |
289 if rev < 0: rev = self.count() + rev | |
476
0a338d506268
Really _call_ method revlog.count in revlog.lookup()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
469
diff
changeset
|
290 if rev < 0 or rev >= self.count(): raise ValueError |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
291 return self.node(rev) |
469 | 292 except (ValueError, OverflowError): |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
293 c = [] |
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
294 for n in self.nodemap: |
469 | 295 if hex(n).startswith(id): |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
296 c.append(n) |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
297 if len(c) > 1: raise RevlogError(_("Ambiguous identifier")) |
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
298 if len(c) < 1: raise RevlogError(_("No match found")) |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
299 return c[0] |
515 | 300 |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
301 return None |
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
302 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
303 def diff(self, a, b): |
1083 | 304 """return a delta between two revisions""" |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
305 return mdiff.textdiff(a, b) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
306 |
73 | 307 def patches(self, t, pl): |
1083 | 308 """apply a list of patches to a string""" |
73 | 309 return mdiff.patches(t, pl) |
310 | |
119
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
311 def delta(self, node): |
1083 | 312 """return or calculate a delta between a node and its predecessor""" |
119
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
313 r = self.rev(node) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
314 b = self.base(r) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
315 if r == b: |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
316 return self.diff(self.revision(self.node(r - 1)), |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
317 self.revision(node)) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
318 else: |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
319 f = self.opener(self.datafile) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
320 f.seek(self.start(r)) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
321 data = f.read(self.length(r)) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
322 return decompress(data) |
c7a66f9752a4
Add code to retrieve or construct a revlog delta
mpm@selenic.com
parents:
117
diff
changeset
|
323 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
324 def revision(self, node): |
1083 | 325 """return an uncompressed revision of a given""" |
36
da28286bf6b7
Add smart node lookup by substring or by rev number
mpm@selenic.com
parents:
26
diff
changeset
|
326 if node == nullid: return "" |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
327 if self.cache and self.cache[0] == node: return self.cache[2] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
328 |
1083 | 329 # look up what we need to read |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
330 text = None |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
331 rev = self.rev(node) |
117 | 332 start, length, base, link, p1, p2, node = self.index[rev] |
333 end = start + length | |
334 if base != rev: start = self.start(base) | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
335 |
1083 | 336 # do we have useful data cached? |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
337 if self.cache and self.cache[1] >= base and self.cache[1] < rev: |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
338 base = self.cache[1] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
339 start = self.start(base + 1) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
340 text = self.cache[2] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
341 last = 0 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
342 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
343 f = self.opener(self.datafile) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
344 f.seek(start) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
345 data = f.read(end - start) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
346 |
651 | 347 if text is None: |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
348 last = self.length(base) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
349 text = decompress(data[:last]) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
350 |
71
47c9a869adee
Add mdiff.patches to speed up applying thousands of patches to the manifest
mpm@selenic.com
parents:
67
diff
changeset
|
351 bins = [] |
64 | 352 for r in xrange(base + 1, rev + 1): |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
353 s = self.length(r) |
71
47c9a869adee
Add mdiff.patches to speed up applying thousands of patches to the manifest
mpm@selenic.com
parents:
67
diff
changeset
|
354 bins.append(decompress(data[last:last + s])) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
355 last = last + s |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
356 |
71
47c9a869adee
Add mdiff.patches to speed up applying thousands of patches to the manifest
mpm@selenic.com
parents:
67
diff
changeset
|
357 text = mdiff.patches(text, bins) |
47c9a869adee
Add mdiff.patches to speed up applying thousands of patches to the manifest
mpm@selenic.com
parents:
67
diff
changeset
|
358 |
26 | 359 if node != hash(text, p1, p2): |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
360 raise RevlogError(_("integrity check failed on %s:%d") |
98 | 361 % (self.datafile, rev)) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
362 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
363 self.cache = (node, rev, text) |
515 | 364 return text |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
365 |
644 | 366 def addrevision(self, text, transaction, link, p1=None, p2=None, d=None): |
1083 | 367 """add a revision to the log |
368 | |
369 text - the revision data to add | |
370 transaction - the transaction object used for rollback | |
371 link - the linkrev data to add | |
372 p1, p2 - the parent nodeids of the revision | |
373 d - an optional precomputed delta | |
374 """ | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
375 if text is None: text = "" |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
376 if p1 is None: p1 = self.tip() |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
377 if p2 is None: p2 = nullid |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
378 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
379 node = hash(text, p1, p2) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
380 |
301 | 381 if node in self.nodemap: |
382 return node | |
383 | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
384 n = self.count() |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
385 t = n - 1 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
386 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
387 if n: |
64 | 388 base = self.base(t) |
389 start = self.start(base) | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
390 end = self.end(t) |
644 | 391 if not d: |
392 prev = self.revision(self.tip()) | |
393 d = self.diff(prev, text) | |
98 | 394 data = compress(d) |
64 | 395 dist = end - start + len(data) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
396 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
397 # full versions are inserted when the needed deltas |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
398 # become comparable to the uncompressed text |
64 | 399 if not n or dist > len(text) * 2: |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
400 data = compress(text) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
401 base = n |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
402 else: |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
403 base = self.base(t) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
404 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
405 offset = 0 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
406 if t >= 0: |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
407 offset = self.end(t) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
408 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
409 e = (offset, len(data), base, link, p1, p2, node) |
515 | 410 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
411 self.index.append(e) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
412 self.nodemap[node] = n |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
413 entry = struct.pack(indexformat, *e) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
414 |
26 | 415 transaction.add(self.datafile, e[0]) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
416 self.opener(self.datafile, "a").write(data) |
41 | 417 transaction.add(self.indexfile, n * len(entry)) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
418 self.opener(self.indexfile, "a").write(entry) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
419 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
420 self.cache = (node, n, text) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
421 return node |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
422 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
423 def ancestor(self, a, b): |
1083 | 424 """calculate the least common ancestor of nodes a and b""" |
147 | 425 # calculate the distance of every node from root |
426 dist = {nullid: 0} | |
427 for i in xrange(self.count()): | |
428 n = self.node(i) | |
429 p1, p2 = self.parents(n) | |
430 dist[n] = max(dist[p1], dist[p2]) + 1 | |
515 | 431 |
147 | 432 # traverse ancestors in order of decreasing distance from root |
433 def ancestors(node): | |
434 # we store negative distances because heap returns smallest member | |
435 h = [(-dist[node], node)] | |
436 seen = {} | |
437 earliest = self.count() | |
438 while h: | |
439 d, n = heapq.heappop(h) | |
440 if n not in seen: | |
441 seen[n] = 1 | |
381 | 442 r = self.rev(n) |
1351
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
443 yield (-d, n) |
147 | 444 for p in self.parents(n): |
445 heapq.heappush(h, (-dist[p], p)) | |
45
f2b2d5daec30
Fix recursion depth trouble with ancestor algorithm
mpm@selenic.com
parents:
41
diff
changeset
|
446 |
1351
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
447 def generations(node): |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
448 sg, s = None, {} |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
449 for g,n in ancestors(node): |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
450 if g != sg: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
451 if sg: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
452 yield sg, s |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
453 sg, s = g, {n:1} |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
454 else: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
455 s[n] = 1 |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
456 yield sg, s |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
457 |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
458 x = generations(a) |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
459 y = generations(b) |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
460 gx = x.next() |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
461 gy = y.next() |
45
f2b2d5daec30
Fix recursion depth trouble with ancestor algorithm
mpm@selenic.com
parents:
41
diff
changeset
|
462 |
147 | 463 # increment each ancestor list until it is closer to root than |
464 # the other, or they match | |
465 while 1: | |
1351
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
466 #print "ancestor gen %s %s" % (gx[0], gy[0]) |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
467 if gx[0] == gy[0]: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
468 # find the intersection |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
469 i = [ n for n in gx[1] if n in gy[1] ] |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
470 if i: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
471 return i[0] |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
472 else: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
473 #print "next" |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
474 gy = y.next() |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
475 gx = x.next() |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
476 elif gx[0] < gy[0]: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
477 #print "next y" |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
478 gy = y.next() |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
479 else: |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
480 #print "next x" |
0e2be889ccd7
Repair ancestor logic, fix up test cases
Matt Mackall <mpm@selenic.com>
parents:
1325
diff
changeset
|
481 gx = x.next() |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
482 |
46 | 483 def group(self, linkmap): |
1083 | 484 """calculate a delta group |
46 | 485 |
1083 | 486 Given a list of changeset revs, return a set of deltas and |
487 metadata corresponding to nodes. the first delta is | |
488 parent(nodes[0]) -> nodes[0] the receiver is guaranteed to | |
489 have this parent as it has all history before these | |
490 changesets. parent is parent[0] | |
491 """ | |
46 | 492 revs = [] |
493 needed = {} | |
494 | |
495 # find file nodes/revs that match changeset revs | |
496 for i in xrange(0, self.count()): | |
497 if self.index[i][3] in linkmap: | |
498 revs.append(i) | |
499 needed[i] = 1 | |
500 | |
501 # if we don't have any revisions touched by these changesets, bail | |
192 | 502 if not revs: |
503 yield struct.pack(">l", 0) | |
504 return | |
46 | 505 |
506 # add the parent of the first rev | |
507 p = self.parents(self.node(revs[0]))[0] | |
508 revs.insert(0, self.rev(p)) | |
509 | |
510 # for each delta that isn't contiguous in the log, we need to | |
511 # reconstruct the base, reconstruct the result, and then | |
512 # calculate the delta. We also need to do this where we've | |
513 # stored a full version and not a delta | |
514 for i in xrange(0, len(revs) - 1): | |
515 a, b = revs[i], revs[i + 1] | |
516 if a + 1 != b or self.base(b) == b: | |
517 for j in xrange(self.base(a), a + 1): | |
518 needed[j] = 1 | |
519 for j in xrange(self.base(b), b + 1): | |
520 needed[j] = 1 | |
521 | |
522 # calculate spans to retrieve from datafile | |
523 needed = needed.keys() | |
524 needed.sort() | |
525 spans = [] | |
192 | 526 oo = -1 |
527 ol = 0 | |
46 | 528 for n in needed: |
529 if n < 0: continue | |
530 o = self.start(n) | |
531 l = self.length(n) | |
192 | 532 if oo + ol == o: # can we merge with the previous? |
533 nl = spans[-1][2] | |
534 nl.append((n, l)) | |
535 ol += l | |
536 spans[-1] = (oo, ol, nl) | |
46 | 537 else: |
192 | 538 oo = o |
539 ol = l | |
540 spans.append((oo, ol, [(n, l)])) | |
46 | 541 |
542 # read spans in, divide up chunks | |
543 chunks = {} | |
192 | 544 for span in spans: |
46 | 545 # we reopen the file for each span to make http happy for now |
546 f = self.opener(self.datafile) | |
547 f.seek(span[0]) | |
548 data = f.read(span[1]) | |
549 | |
550 # divide up the span | |
551 pos = 0 | |
552 for r, l in span[2]: | |
192 | 553 chunks[r] = decompress(data[pos: pos + l]) |
46 | 554 pos += l |
555 | |
556 # helper to reconstruct intermediate versions | |
557 def construct(text, base, rev): | |
192 | 558 bins = [chunks[r] for r in xrange(base + 1, rev + 1)] |
71
47c9a869adee
Add mdiff.patches to speed up applying thousands of patches to the manifest
mpm@selenic.com
parents:
67
diff
changeset
|
559 return mdiff.patches(text, bins) |
46 | 560 |
561 # build deltas | |
562 deltas = [] | |
71
47c9a869adee
Add mdiff.patches to speed up applying thousands of patches to the manifest
mpm@selenic.com
parents:
67
diff
changeset
|
563 for d in xrange(0, len(revs) - 1): |
46 | 564 a, b = revs[d], revs[d + 1] |
565 n = self.node(b) | |
192 | 566 |
567 # do we need to construct a new delta? | |
46 | 568 if a + 1 != b or self.base(b) == b: |
569 if a >= 0: | |
570 base = self.base(a) | |
192 | 571 ta = chunks[self.base(a)] |
46 | 572 ta = construct(ta, base, a) |
573 else: | |
574 ta = "" | |
515 | 575 |
46 | 576 base = self.base(b) |
577 if a > base: | |
578 base = a | |
579 tb = ta | |
580 else: | |
192 | 581 tb = chunks[self.base(b)] |
46 | 582 tb = construct(tb, base, b) |
583 d = self.diff(ta, tb) | |
584 else: | |
192 | 585 d = chunks[b] |
46 | 586 |
587 p = self.parents(n) | |
588 meta = n + p[0] + p[1] + linkmap[self.linkrev(n)] | |
589 l = struct.pack(">l", len(meta) + len(d) + 4) | |
192 | 590 yield l |
591 yield meta | |
592 yield d | |
46 | 593 |
192 | 594 yield struct.pack(">l", 0) |
595 | |
1062 | 596 def addgroup(self, revs, linkmapper, transaction, unique=0): |
1083 | 597 """ |
598 add a delta group | |
46 | 599 |
1083 | 600 given a set of deltas, add them to the revision log. the |
601 first delta is against its parent, which should be in our | |
602 log, the rest are against the previous delta. | |
603 """ | |
604 | |
605 #track the base of the current delta log | |
46 | 606 r = self.count() |
607 t = r - 1 | |
192 | 608 node = nullid |
515 | 609 |
655 | 610 base = prev = -1 |
653
94cdd02792b5
Fix corruption resulting from skipping parts of a revision group
Matt Mackall <mpm@selenic.com>
parents:
651
diff
changeset
|
611 start = end = measure = 0 |
46 | 612 if r: |
613 start = self.start(self.base(t)) | |
614 end = self.end(t) | |
615 measure = self.length(self.base(t)) | |
616 base = self.base(t) | |
617 prev = self.tip() | |
618 | |
619 transaction.add(self.datafile, end) | |
620 transaction.add(self.indexfile, r * struct.calcsize(indexformat)) | |
621 dfh = self.opener(self.datafile, "a") | |
622 ifh = self.opener(self.indexfile, "a") | |
623 | |
624 # loop through our set of deltas | |
192 | 625 chain = None |
626 for chunk in revs: | |
627 node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80]) | |
94 | 628 link = linkmapper(cs) |
77 | 629 if node in self.nodemap: |
224
ccbcc4d76f81
fix bad assumption about uniqueness of file versions
mpm@selenic.com
parents:
221
diff
changeset
|
630 # this can happen if two branches make the same change |
1218
cde6818e082a
Add preliminary support for the bundle and unbundle commands
mpm@selenic.com
parents:
1214
diff
changeset
|
631 # if unique: |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
632 # raise RevlogError(_("already have %s") % hex(node[:4])) |
653
94cdd02792b5
Fix corruption resulting from skipping parts of a revision group
Matt Mackall <mpm@selenic.com>
parents:
651
diff
changeset
|
633 chain = node |
224
ccbcc4d76f81
fix bad assumption about uniqueness of file versions
mpm@selenic.com
parents:
221
diff
changeset
|
634 continue |
192 | 635 delta = chunk[80:] |
636 | |
637 if not chain: | |
638 # retrieve the parent revision of the delta chain | |
639 chain = p1 | |
640 if not chain in self.nodemap: | |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
641 raise RevlogError(_("unknown base %s") % short(chain[:4])) |
46 | 642 |
643 # full versions are inserted when the needed deltas become | |
644 # comparable to the uncompressed text or when the previous | |
645 # version is not the one we have a delta against. We use | |
646 # the size of the previous full rev as a proxy for the | |
647 # current size. | |
648 | |
649 if chain == prev: | |
650 cdelta = compress(delta) | |
651 | |
652 if chain != prev or (end - start + len(cdelta)) > measure * 2: | |
653 # flush our writes here so we can read it in revision | |
654 dfh.flush() | |
655 ifh.flush() | |
65
d40cc5aacc31
Fix up a bunch of bugs in the new merge code
mpm@selenic.com
parents:
64
diff
changeset
|
656 text = self.revision(chain) |
73 | 657 text = self.patches(text, [delta]) |
46 | 658 chk = self.addrevision(text, transaction, link, p1, p2) |
659 if chk != node: | |
1402
9d2c2e6b32b5
i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1400
diff
changeset
|
660 raise RevlogError(_("consistency error adding group")) |
46 | 661 measure = len(text) |
662 else: | |
663 e = (end, len(cdelta), self.base(t), link, p1, p2, node) | |
664 self.index.append(e) | |
665 self.nodemap[node] = r | |
666 dfh.write(cdelta) | |
667 ifh.write(struct.pack(indexformat, *e)) | |
668 | |
65
d40cc5aacc31
Fix up a bunch of bugs in the new merge code
mpm@selenic.com
parents:
64
diff
changeset
|
669 t, r, chain, prev = r, r + 1, node, node |
46 | 670 start = self.start(self.base(t)) |
671 end = self.end(t) | |
672 | |
673 dfh.close() | |
674 ifh.close() | |
675 return node |