Show More
@@ -1,414 +1,421 b'' | |||
|
1 | 1 | # linelog - efficient cache for annotate data |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2018 Google LLC. |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | """linelog is an efficient cache for annotate data inspired by SCCS Weaves. |
|
8 | 8 | |
|
9 | 9 | SCCS Weaves are an implementation of |
|
10 | 10 | https://en.wikipedia.org/wiki/Interleaved_deltas. See |
|
11 | 11 | mercurial/help/internals/linelog.txt for an exploration of SCCS weaves |
|
12 | 12 | and how linelog works in detail. |
|
13 | 13 | |
|
14 | 14 | Here's a hacker's summary: a linelog is a program which is executed in |
|
15 | 15 | the context of a revision. Executing the program emits information |
|
16 | 16 | about lines, including the revision that introduced them and the line |
|
17 | 17 | number in the file at the introducing revision. When an insertion or |
|
18 | 18 | deletion is performed on the file, a jump instruction is used to patch |
|
19 | 19 | in a new body of annotate information. |
|
20 | 20 | """ |
|
21 | 21 | from __future__ import absolute_import, print_function |
|
22 | 22 | |
|
23 | 23 | import abc |
|
24 | 24 | import struct |
|
25 | 25 | |
|
26 | 26 | from .thirdparty import ( |
|
27 | 27 | attr, |
|
28 | 28 | ) |
|
29 | 29 | from . import ( |
|
30 | 30 | pycompat, |
|
31 | 31 | ) |
|
32 | 32 | |
|
33 | 33 | _llentry = struct.Struct('>II') |
|
34 | 34 | |
|
35 | 35 | class LineLogError(Exception): |
|
36 | 36 | """Error raised when something bad happens internally in linelog.""" |
|
37 | 37 | |
|
38 | 38 | @attr.s |
|
39 | 39 | class lineinfo(object): |
|
40 | 40 | # Introducing revision of this line. |
|
41 | 41 | rev = attr.ib() |
|
42 | 42 | # Line number for this line in its introducing revision. |
|
43 | 43 | linenum = attr.ib() |
|
44 | 44 | # Private. Offset in the linelog program of this line. Used internally. |
|
45 | 45 | _offset = attr.ib() |
|
46 | 46 | |
|
47 | 47 | @attr.s |
|
48 | 48 | class annotateresult(object): |
|
49 | 49 | rev = attr.ib() |
|
50 | 50 | lines = attr.ib() |
|
51 | 51 | _eof = attr.ib() |
|
52 | 52 | |
|
53 | 53 | def __iter__(self): |
|
54 | 54 | return iter(self.lines) |
|
55 | 55 | |
|
56 | 56 | class _llinstruction(object): |
|
57 | 57 | |
|
58 | 58 | __metaclass__ = abc.ABCMeta |
|
59 | 59 | |
|
60 | 60 | @abc.abstractmethod |
|
61 | 61 | def __init__(self, op1, op2): |
|
62 | 62 | pass |
|
63 | 63 | |
|
64 | 64 | @abc.abstractmethod |
|
65 | 65 | def __str__(self): |
|
66 | 66 | pass |
|
67 | 67 | |
|
68 | 68 | def __repr__(self): |
|
69 | 69 | return str(self) |
|
70 | 70 | |
|
71 | 71 | @abc.abstractmethod |
|
72 | 72 | def __eq__(self, other): |
|
73 | 73 | pass |
|
74 | 74 | |
|
75 | 75 | @abc.abstractmethod |
|
76 | 76 | def encode(self): |
|
77 | 77 | """Encode this instruction to the binary linelog format.""" |
|
78 | 78 | |
|
79 | 79 | @abc.abstractmethod |
|
80 | 80 | def execute(self, rev, pc, emit): |
|
81 | 81 | """Execute this instruction. |
|
82 | 82 | |
|
83 | 83 | Args: |
|
84 | 84 | rev: The revision we're annotating. |
|
85 | 85 | pc: The current offset in the linelog program. |
|
86 | 86 | emit: A function that accepts a single lineinfo object. |
|
87 | 87 | |
|
88 | 88 | Returns: |
|
89 | 89 | The new value of pc. Returns None if exeuction should stop |
|
90 | 90 | (that is, we've found the end of the file.) |
|
91 | 91 | """ |
|
92 | 92 | |
|
93 | 93 | class _jge(_llinstruction): |
|
94 | 94 | """If the current rev is greater than or equal to op1, jump to op2.""" |
|
95 | 95 | |
|
96 | 96 | def __init__(self, op1, op2): |
|
97 | 97 | self._cmprev = op1 |
|
98 | 98 | self._target = op2 |
|
99 | 99 | |
|
100 | 100 | def __str__(self): |
|
101 | 101 | return r'JGE %d %d' % (self._cmprev, self._target) |
|
102 | 102 | |
|
103 | 103 | def __eq__(self, other): |
|
104 | 104 | return (type(self) == type(other) |
|
105 | 105 | and self._cmprev == other._cmprev |
|
106 | 106 | and self._target == other._target) |
|
107 | 107 | |
|
108 | 108 | def encode(self): |
|
109 | 109 | return _llentry.pack(self._cmprev << 2, self._target) |
|
110 | 110 | |
|
111 | 111 | def execute(self, rev, pc, emit): |
|
112 | 112 | if rev >= self._cmprev: |
|
113 | 113 | return self._target |
|
114 | 114 | return pc + 1 |
|
115 | 115 | |
|
116 | 116 | class _jump(_llinstruction): |
|
117 | 117 | """Unconditional jumps are expressed as a JGE with op1 set to 0.""" |
|
118 | 118 | |
|
119 | 119 | def __init__(self, op1, op2): |
|
120 | 120 | if op1 != 0: |
|
121 | 121 | raise LineLogError("malformed JUMP, op1 must be 0, got %d" % op1) |
|
122 | 122 | self._target = op2 |
|
123 | 123 | |
|
124 | 124 | def __str__(self): |
|
125 | 125 | return r'JUMP %d' % (self._target) |
|
126 | 126 | |
|
127 | 127 | def __eq__(self, other): |
|
128 | 128 | return (type(self) == type(other) |
|
129 | 129 | and self._target == other._target) |
|
130 | 130 | |
|
131 | 131 | def encode(self): |
|
132 | 132 | return _llentry.pack(0, self._target) |
|
133 | 133 | |
|
134 | 134 | def execute(self, rev, pc, emit): |
|
135 | 135 | return self._target |
|
136 | 136 | |
|
137 | 137 | class _eof(_llinstruction): |
|
138 | 138 | """EOF is expressed as a JGE that always jumps to 0.""" |
|
139 | 139 | |
|
140 | 140 | def __init__(self, op1, op2): |
|
141 | 141 | if op1 != 0: |
|
142 | 142 | raise LineLogError("malformed EOF, op1 must be 0, got %d" % op1) |
|
143 | 143 | if op2 != 0: |
|
144 | 144 | raise LineLogError("malformed EOF, op2 must be 0, got %d" % op2) |
|
145 | 145 | |
|
146 | 146 | def __str__(self): |
|
147 | 147 | return r'EOF' |
|
148 | 148 | |
|
149 | 149 | def __eq__(self, other): |
|
150 | 150 | return type(self) == type(other) |
|
151 | 151 | |
|
152 | 152 | def encode(self): |
|
153 | 153 | return _llentry.pack(0, 0) |
|
154 | 154 | |
|
155 | 155 | def execute(self, rev, pc, emit): |
|
156 | 156 | return None |
|
157 | 157 | |
|
158 | 158 | class _jl(_llinstruction): |
|
159 | 159 | """If the current rev is less than op1, jump to op2.""" |
|
160 | 160 | |
|
161 | 161 | def __init__(self, op1, op2): |
|
162 | 162 | self._cmprev = op1 |
|
163 | 163 | self._target = op2 |
|
164 | 164 | |
|
165 | 165 | def __str__(self): |
|
166 | 166 | return r'JL %d %d' % (self._cmprev, self._target) |
|
167 | 167 | |
|
168 | 168 | def __eq__(self, other): |
|
169 | 169 | return (type(self) == type(other) |
|
170 | 170 | and self._cmprev == other._cmprev |
|
171 | 171 | and self._target == other._target) |
|
172 | 172 | |
|
173 | 173 | def encode(self): |
|
174 | 174 | return _llentry.pack(1 | (self._cmprev << 2), self._target) |
|
175 | 175 | |
|
176 | 176 | def execute(self, rev, pc, emit): |
|
177 | 177 | if rev < self._cmprev: |
|
178 | 178 | return self._target |
|
179 | 179 | return pc + 1 |
|
180 | 180 | |
|
181 | 181 | class _line(_llinstruction): |
|
182 | 182 | """Emit a line.""" |
|
183 | 183 | |
|
184 | 184 | def __init__(self, op1, op2): |
|
185 | 185 | # This line was introduced by this revision number. |
|
186 | 186 | self._rev = op1 |
|
187 | 187 | # This line had the specified line number in the introducing revision. |
|
188 | 188 | self._origlineno = op2 |
|
189 | 189 | |
|
190 | 190 | def __str__(self): |
|
191 | 191 | return r'LINE %d %d' % (self._rev, self._origlineno) |
|
192 | 192 | |
|
193 | 193 | def __eq__(self, other): |
|
194 | 194 | return (type(self) == type(other) |
|
195 | 195 | and self._rev == other._rev |
|
196 | 196 | and self._origlineno == other._origlineno) |
|
197 | 197 | |
|
198 | 198 | def encode(self): |
|
199 | 199 | return _llentry.pack(2 | (self._rev << 2), self._origlineno) |
|
200 | 200 | |
|
201 | 201 | def execute(self, rev, pc, emit): |
|
202 | 202 | emit(lineinfo(self._rev, self._origlineno, pc)) |
|
203 | 203 | return pc + 1 |
|
204 | 204 | |
|
205 | 205 | def _decodeone(data, offset): |
|
206 | 206 | """Decode a single linelog instruction from an offset in a buffer.""" |
|
207 | 207 | try: |
|
208 | 208 | op1, op2 = _llentry.unpack_from(data, offset) |
|
209 | 209 | except struct.error as e: |
|
210 | 210 | raise LineLogError('reading an instruction failed: %r' % e) |
|
211 | 211 | opcode = op1 & 0b11 |
|
212 | 212 | op1 = op1 >> 2 |
|
213 | 213 | if opcode == 0: |
|
214 | 214 | if op1 == 0: |
|
215 | 215 | if op2 == 0: |
|
216 | 216 | return _eof(op1, op2) |
|
217 | 217 | return _jump(op1, op2) |
|
218 | 218 | return _jge(op1, op2) |
|
219 | 219 | elif opcode == 1: |
|
220 | 220 | return _jl(op1, op2) |
|
221 | 221 | elif opcode == 2: |
|
222 | 222 | return _line(op1, op2) |
|
223 | 223 | raise NotImplementedError('Unimplemented opcode %r' % opcode) |
|
224 | 224 | |
|
225 | 225 | class linelog(object): |
|
226 | 226 | """Efficient cache for per-line history information.""" |
|
227 | 227 | |
|
228 | 228 | def __init__(self, program=None, maxrev=0): |
|
229 | 229 | if program is None: |
|
230 | 230 | # We pad the program with an extra leading EOF so that our |
|
231 | 231 | # offsets will match the C code exactly. This means we can |
|
232 | 232 | # interoperate with the C code. |
|
233 | 233 | program = [_eof(0, 0), _eof(0, 0)] |
|
234 | 234 | self._program = program |
|
235 | 235 | self._lastannotate = None |
|
236 | 236 | self._maxrev = maxrev |
|
237 | 237 | |
|
238 | 238 | def __eq__(self, other): |
|
239 | 239 | return (type(self) == type(other) |
|
240 | 240 | and self._program == other._program |
|
241 | 241 | and self._maxrev == other._maxrev) |
|
242 | 242 | |
|
243 | 243 | def __repr__(self): |
|
244 | 244 | return '<linelog at %s: maxrev=%d size=%d>' % ( |
|
245 | 245 | hex(id(self)), self._maxrev, len(self._program)) |
|
246 | 246 | |
|
247 | 247 | def debugstr(self): |
|
248 | 248 | fmt = r'%%%dd %%s' % len(str(len(self._program))) |
|
249 | 249 | return pycompat.sysstr('\n').join( |
|
250 | 250 | fmt % (idx, i) for idx, i in enumerate(self._program[1:], 1)) |
|
251 | 251 | |
|
252 | 252 | @classmethod |
|
253 | 253 | def fromdata(cls, buf): |
|
254 | 254 | if len(buf) % _llentry.size != 0: |
|
255 | 255 | raise LineLogError( |
|
256 | 256 | "invalid linelog buffer size %d (must be a multiple of %d)" % ( |
|
257 | 257 | len(buf), _llentry.size)) |
|
258 | 258 | expected = len(buf) / _llentry.size |
|
259 | 259 | fakejge = _decodeone(buf, 0) |
|
260 | 260 | if isinstance(fakejge, _jump): |
|
261 | 261 | maxrev = 0 |
|
262 | 262 | else: |
|
263 | 263 | maxrev = fakejge._cmprev |
|
264 | 264 | numentries = fakejge._target |
|
265 | 265 | if expected != numentries: |
|
266 | 266 | raise LineLogError("corrupt linelog data: claimed" |
|
267 | 267 | " %d entries but given data for %d entries" % ( |
|
268 | 268 | expected, numentries)) |
|
269 | 269 | instructions = [_eof(0, 0)] |
|
270 | 270 | for offset in pycompat.xrange(1, numentries): |
|
271 | 271 | instructions.append(_decodeone(buf, offset * _llentry.size)) |
|
272 | 272 | return cls(instructions, maxrev=maxrev) |
|
273 | 273 | |
|
274 | 274 | def encode(self): |
|
275 | 275 | hdr = _jge(self._maxrev, len(self._program)).encode() |
|
276 | 276 | return hdr + ''.join(i.encode() for i in self._program[1:]) |
|
277 | 277 | |
|
278 | 278 | def clear(self): |
|
279 | 279 | self._program = [] |
|
280 | 280 | self._maxrev = 0 |
|
281 | 281 | self._lastannotate = None |
|
282 | 282 | |
|
283 |
def replacelines(self, rev, a1, a2, b |
|
|
283 | def replacelines_vec(self, rev, a1, a2, blines): | |
|
284 | return self.replacelines(rev, a1, a2, 0, len(blines), | |
|
285 | _internal_blines=blines) | |
|
286 | ||
|
287 | def replacelines(self, rev, a1, a2, b1, b2, _internal_blines=None): | |
|
284 | 288 | """Replace lines [a1, a2) with lines [b1, b2).""" |
|
285 | 289 | if self._lastannotate: |
|
286 | 290 | # TODO(augie): make replacelines() accept a revision at |
|
287 | 291 | # which we're editing as well as a revision to mark |
|
288 | 292 | # responsible for the edits. In hg-experimental it's |
|
289 | 293 | # stateful like this, so we're doing the same thing to |
|
290 | 294 | # retain compatibility with absorb until that's imported. |
|
291 | 295 | ar = self._lastannotate |
|
292 | 296 | else: |
|
293 | 297 | ar = self.annotate(rev) |
|
294 | 298 | # ar = self.annotate(self._maxrev) |
|
295 | 299 | if a1 > len(ar.lines): |
|
296 | 300 | raise LineLogError( |
|
297 | 301 | '%d contains %d lines, tried to access line %d' % ( |
|
298 | 302 | rev, len(ar.lines), a1)) |
|
299 | 303 | elif a1 == len(ar.lines): |
|
300 | 304 | # Simulated EOF instruction since we're at EOF, which |
|
301 | 305 | # doesn't have a "real" line. |
|
302 | 306 | a1inst = _eof(0, 0) |
|
303 | 307 | a1info = lineinfo(0, 0, ar._eof) |
|
304 | 308 | else: |
|
305 | 309 | a1info = ar.lines[a1] |
|
306 | 310 | a1inst = self._program[a1info._offset] |
|
307 | 311 | oldproglen = len(self._program) |
|
308 | 312 | appendinst = self._program.append |
|
309 | 313 | |
|
310 | 314 | # insert |
|
311 | 315 | if b1 < b2: |
|
312 | 316 | # Determine the jump target for the JGE at the start of |
|
313 | 317 | # the new block. |
|
314 | 318 | tgt = oldproglen + (b2 - b1 + 1) |
|
315 | 319 | # Jump to skip the insert if we're at an older revision. |
|
316 | 320 | appendinst(_jl(rev, tgt)) |
|
317 | 321 | for linenum in pycompat.xrange(b1, b2): |
|
318 |
|
|
|
322 | if _internal_blines is None: | |
|
323 | appendinst(_line(rev, linenum)) | |
|
324 | else: | |
|
325 | appendinst(_line(*_internal_blines[linenum])) | |
|
319 | 326 | # delete |
|
320 | 327 | if a1 < a2: |
|
321 | 328 | if a2 > len(ar.lines): |
|
322 | 329 | raise LineLogError( |
|
323 | 330 | '%d contains %d lines, tried to access line %d' % ( |
|
324 | 331 | rev, len(ar.lines), a2)) |
|
325 | 332 | elif a2 == len(ar.lines): |
|
326 | 333 | endaddr = ar._eof |
|
327 | 334 | else: |
|
328 | 335 | endaddr = ar.lines[a2]._offset |
|
329 | 336 | if a2 > 0 and rev < self._maxrev: |
|
330 | 337 | # If we're here, we're deleting a chunk of an old |
|
331 | 338 | # commit, so we need to be careful and not touch |
|
332 | 339 | # invisible lines between a2-1 and a2 (IOW, lines that |
|
333 | 340 | # are added later). |
|
334 | 341 | endaddr = ar.lines[a2 - 1]._offset + 1 |
|
335 | 342 | appendinst(_jge(rev, endaddr)) |
|
336 | 343 | # copy instruction from a1 |
|
337 | 344 | appendinst(a1inst) |
|
338 | 345 | # if a1inst isn't a jump or EOF, then we need to add an unconditional |
|
339 | 346 | # jump back into the program here. |
|
340 | 347 | if not isinstance(a1inst, (_jump, _eof)): |
|
341 | 348 | appendinst(_jump(0, a1info._offset + 1)) |
|
342 | 349 | # Patch instruction at a1, which makes our patch live. |
|
343 | 350 | self._program[a1info._offset] = _jump(0, oldproglen) |
|
344 | 351 | # For compat with the C version, re-annotate rev so that |
|
345 | 352 | # self.annotateresult is cromulent.. We could fix up the |
|
346 | 353 | # annotateresult in place (which is how the C version works), |
|
347 | 354 | # but for now we'll pass on that and see if it matters in |
|
348 | 355 | # practice. |
|
349 | 356 | self.annotate(max(self._lastannotate.rev, rev)) |
|
350 | 357 | if rev > self._maxrev: |
|
351 | 358 | self._maxrev = rev |
|
352 | 359 | |
|
353 | 360 | def annotate(self, rev): |
|
354 | 361 | pc = 1 |
|
355 | 362 | lines = [] |
|
356 | 363 | # Sanity check: if len(lines) is longer than len(program), we |
|
357 | 364 | # hit an infinite loop in the linelog program somehow and we |
|
358 | 365 | # should stop. |
|
359 | 366 | while pc is not None and len(lines) < len(self._program): |
|
360 | 367 | inst = self._program[pc] |
|
361 | 368 | lastpc = pc |
|
362 | 369 | pc = inst.execute(rev, pc, lines.append) |
|
363 | 370 | if pc is not None: |
|
364 | 371 | raise LineLogError( |
|
365 | 372 | 'Probably hit an infinite loop in linelog. Program:\n' + |
|
366 | 373 | self.debugstr()) |
|
367 | 374 | ar = annotateresult(rev, lines, lastpc) |
|
368 | 375 | self._lastannotate = ar |
|
369 | 376 | return ar |
|
370 | 377 | |
|
371 | 378 | @property |
|
372 | 379 | def maxrev(self): |
|
373 | 380 | return self._maxrev |
|
374 | 381 | |
|
375 | 382 | # Stateful methods which depend on the value of the last |
|
376 | 383 | # annotation run. This API is for compatiblity with the original |
|
377 | 384 | # linelog, and we should probably consider refactoring it. |
|
378 | 385 | @property |
|
379 | 386 | def annotateresult(self): |
|
380 | 387 | """Return the last annotation result. C linelog code exposed this.""" |
|
381 | 388 | return [(l.rev, l.linenum) for l in self._lastannotate.lines] |
|
382 | 389 | |
|
383 | 390 | def getoffset(self, line): |
|
384 | 391 | return self._lastannotate.lines[line]._offset |
|
385 | 392 | |
|
386 | 393 | def getalllines(self, start=0, end=0): |
|
387 | 394 | """Get all lines that ever occurred in [start, end). |
|
388 | 395 | |
|
389 | 396 | Passing start == end == 0 means "all lines ever". |
|
390 | 397 | |
|
391 | 398 | This works in terms of *internal* program offsets, not line numbers. |
|
392 | 399 | """ |
|
393 | 400 | pc = start or 1 |
|
394 | 401 | lines = [] |
|
395 | 402 | # only take as many steps as there are instructions in the |
|
396 | 403 | # program - if we don't find an EOF or our stop-line before |
|
397 | 404 | # then, something is badly broken. |
|
398 | 405 | for step in pycompat.xrange(len(self._program)): |
|
399 | 406 | inst = self._program[pc] |
|
400 | 407 | nextpc = pc + 1 |
|
401 | 408 | if isinstance(inst, _jump): |
|
402 | 409 | nextpc = inst._target |
|
403 | 410 | elif isinstance(inst, _eof): |
|
404 | 411 | return lines |
|
405 | 412 | elif isinstance(inst, (_jl, _jge)): |
|
406 | 413 | pass |
|
407 | 414 | elif isinstance(inst, _line): |
|
408 | 415 | lines.append((inst._rev, inst._origlineno)) |
|
409 | 416 | else: |
|
410 | 417 | raise LineLogError("Illegal instruction %r" % inst) |
|
411 | 418 | if nextpc == end: |
|
412 | 419 | return lines |
|
413 | 420 | pc = nextpc |
|
414 | 421 | raise LineLogError("Failed to perform getalllines") |
@@ -1,173 +1,184 b'' | |||
|
1 | 1 | from __future__ import absolute_import, print_function |
|
2 | 2 | |
|
3 | 3 | import difflib |
|
4 | 4 | import random |
|
5 | 5 | import unittest |
|
6 | 6 | |
|
7 | 7 | from mercurial import linelog |
|
8 | 8 | |
|
9 | vecratio = 3 # number of replacelines / number of replacelines_vec | |
|
9 | 10 | maxlinenum = 0xffffff |
|
10 | 11 | maxb1 = 0xffffff |
|
11 | 12 | maxdeltaa = 10 |
|
12 | 13 | maxdeltab = 10 |
|
13 | 14 | |
|
14 | 15 | def _genedits(seed, endrev): |
|
15 | 16 | lines = [] |
|
16 | 17 | random.seed(seed) |
|
17 | 18 | rev = 0 |
|
18 | 19 | for rev in range(0, endrev): |
|
19 | 20 | n = len(lines) |
|
20 | 21 | a1 = random.randint(0, n) |
|
21 | 22 | a2 = random.randint(a1, min(n, a1 + maxdeltaa)) |
|
22 | 23 | b1 = random.randint(0, maxb1) |
|
23 | 24 | b2 = random.randint(b1, b1 + maxdeltab) |
|
24 | blines = [(rev, idx) for idx in range(b1, b2)] | |
|
25 | usevec = not bool(random.randint(0, vecratio)) | |
|
26 | if usevec: | |
|
27 | blines = [(random.randint(0, rev), random.randint(0, maxlinenum)) | |
|
28 | for _ in range(b1, b2)] | |
|
29 | else: | |
|
30 | blines = [(rev, bidx) for bidx in range(b1, b2)] | |
|
25 | 31 | lines[a1:a2] = blines |
|
26 | yield lines, rev, a1, a2, b1, b2 | |
|
32 | yield lines, rev, a1, a2, b1, b2, blines, usevec | |
|
27 | 33 | |
|
28 | 34 | class linelogtests(unittest.TestCase): |
|
29 | 35 | def testlinelogencodedecode(self): |
|
30 | 36 | program = [linelog._eof(0, 0), |
|
31 | 37 | linelog._jge(41, 42), |
|
32 | 38 | linelog._jump(0, 43), |
|
33 | 39 | linelog._eof(0, 0), |
|
34 | 40 | linelog._jl(44, 45), |
|
35 | 41 | linelog._line(46, 47), |
|
36 | 42 | ] |
|
37 | 43 | ll = linelog.linelog(program, maxrev=100) |
|
38 | 44 | enc = ll.encode() |
|
39 | 45 | # round-trips okay |
|
40 | 46 | self.assertEqual(linelog.linelog.fromdata(enc)._program, ll._program) |
|
41 | 47 | self.assertEqual(linelog.linelog.fromdata(enc), ll) |
|
42 | 48 | # This encoding matches the encoding used by hg-experimental's |
|
43 | 49 | # linelog file, or is supposed to if it doesn't. |
|
44 | 50 | self.assertEqual(enc, (b'\x00\x00\x01\x90\x00\x00\x00\x06' |
|
45 | 51 | b'\x00\x00\x00\xa4\x00\x00\x00*' |
|
46 | 52 | b'\x00\x00\x00\x00\x00\x00\x00+' |
|
47 | 53 | b'\x00\x00\x00\x00\x00\x00\x00\x00' |
|
48 | 54 | b'\x00\x00\x00\xb1\x00\x00\x00-' |
|
49 | 55 | b'\x00\x00\x00\xba\x00\x00\x00/')) |
|
50 | 56 | |
|
51 | 57 | def testsimpleedits(self): |
|
52 | 58 | ll = linelog.linelog() |
|
53 | 59 | # Initial revision: add lines 0, 1, and 2 |
|
54 | 60 | ll.replacelines(1, 0, 0, 0, 3) |
|
55 | 61 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(1)], |
|
56 | 62 | [(1, 0), |
|
57 | 63 | (1, 1), |
|
58 | 64 | (1, 2), |
|
59 | 65 | ]) |
|
60 | 66 | # Replace line 1 with a new line |
|
61 | 67 | ll.replacelines(2, 1, 2, 1, 2) |
|
62 | 68 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(2)], |
|
63 | 69 | [(1, 0), |
|
64 | 70 | (2, 1), |
|
65 | 71 | (1, 2), |
|
66 | 72 | ]) |
|
67 | 73 | # delete a line out of 2 |
|
68 | 74 | ll.replacelines(3, 1, 2, 0, 0) |
|
69 | 75 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(3)], |
|
70 | 76 | [(1, 0), |
|
71 | 77 | (1, 2), |
|
72 | 78 | ]) |
|
73 | 79 | # annotation of 1 is unchanged |
|
74 | 80 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(1)], |
|
75 | 81 | [(1, 0), |
|
76 | 82 | (1, 1), |
|
77 | 83 | (1, 2), |
|
78 | 84 | ]) |
|
79 | 85 | ll.annotate(3) # set internal state to revision 3 |
|
80 | 86 | start = ll.getoffset(0) |
|
81 | 87 | end = ll.getoffset(1) |
|
82 | 88 | self.assertEqual(ll.getalllines(start, end), [ |
|
83 | 89 | (1, 0), |
|
84 | 90 | (2, 1), |
|
85 | 91 | (1, 1), |
|
86 | 92 | ]) |
|
87 | 93 | self.assertEqual(ll.getalllines(), [ |
|
88 | 94 | (1, 0), |
|
89 | 95 | (2, 1), |
|
90 | 96 | (1, 1), |
|
91 | 97 | (1, 2), |
|
92 | 98 | ]) |
|
93 | 99 | |
|
94 | 100 | def testparseclinelogfile(self): |
|
95 | 101 | # This data is what the replacements in testsimpleedits |
|
96 | 102 | # produce when fed to the original linelog.c implementation. |
|
97 | 103 | data = (b'\x00\x00\x00\x0c\x00\x00\x00\x0f' |
|
98 | 104 | b'\x00\x00\x00\x00\x00\x00\x00\x02' |
|
99 | 105 | b'\x00\x00\x00\x05\x00\x00\x00\x06' |
|
100 | 106 | b'\x00\x00\x00\x06\x00\x00\x00\x00' |
|
101 | 107 | b'\x00\x00\x00\x00\x00\x00\x00\x07' |
|
102 | 108 | b'\x00\x00\x00\x06\x00\x00\x00\x02' |
|
103 | 109 | b'\x00\x00\x00\x00\x00\x00\x00\x00' |
|
104 | 110 | b'\x00\x00\x00\t\x00\x00\x00\t' |
|
105 | 111 | b'\x00\x00\x00\x00\x00\x00\x00\x0c' |
|
106 | 112 | b'\x00\x00\x00\x08\x00\x00\x00\x05' |
|
107 | 113 | b'\x00\x00\x00\x06\x00\x00\x00\x01' |
|
108 | 114 | b'\x00\x00\x00\x00\x00\x00\x00\x05' |
|
109 | 115 | b'\x00\x00\x00\x0c\x00\x00\x00\x05' |
|
110 | 116 | b'\x00\x00\x00\n\x00\x00\x00\x01' |
|
111 | 117 | b'\x00\x00\x00\x00\x00\x00\x00\t') |
|
112 | 118 | llc = linelog.linelog.fromdata(data) |
|
113 | 119 | self.assertEqual([(l.rev, l.linenum) for l in llc.annotate(1)], |
|
114 | 120 | [(1, 0), |
|
115 | 121 | (1, 1), |
|
116 | 122 | (1, 2), |
|
117 | 123 | ]) |
|
118 | 124 | self.assertEqual([(l.rev, l.linenum) for l in llc.annotate(2)], |
|
119 | 125 | [(1, 0), |
|
120 | 126 | (2, 1), |
|
121 | 127 | (1, 2), |
|
122 | 128 | ]) |
|
123 | 129 | self.assertEqual([(l.rev, l.linenum) for l in llc.annotate(3)], |
|
124 | 130 | [(1, 0), |
|
125 | 131 | (1, 2), |
|
126 | 132 | ]) |
|
127 | 133 | # Check we emit the same bytecode. |
|
128 | 134 | ll = linelog.linelog() |
|
129 | 135 | # Initial revision: add lines 0, 1, and 2 |
|
130 | 136 | ll.replacelines(1, 0, 0, 0, 3) |
|
131 | 137 | # Replace line 1 with a new line |
|
132 | 138 | ll.replacelines(2, 1, 2, 1, 2) |
|
133 | 139 | # delete a line out of 2 |
|
134 | 140 | ll.replacelines(3, 1, 2, 0, 0) |
|
135 | 141 | diff = '\n ' + '\n '.join(difflib.unified_diff( |
|
136 | 142 | ll.debugstr().splitlines(), llc.debugstr().splitlines(), |
|
137 | 143 | 'python', 'c', lineterm='')) |
|
138 | 144 | self.assertEqual(ll._program, llc._program, 'Program mismatch: ' + diff) |
|
139 | 145 | # Done as a secondary step so we get a better result if the |
|
140 | 146 | # program is where the mismatch is. |
|
141 | 147 | self.assertEqual(ll, llc) |
|
142 | 148 | self.assertEqual(ll.encode(), data) |
|
143 | 149 | |
|
144 | 150 | def testanothersimplecase(self): |
|
145 | 151 | ll = linelog.linelog() |
|
146 | 152 | ll.replacelines(3, 0, 0, 0, 2) |
|
147 | 153 | ll.replacelines(4, 0, 2, 0, 0) |
|
148 | 154 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(4)], |
|
149 | 155 | []) |
|
150 | 156 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(3)], |
|
151 | 157 | [(3, 0), (3, 1)]) |
|
152 | 158 | # rev 2 is empty because contents were only ever introduced in rev 3 |
|
153 | 159 | self.assertEqual([(l.rev, l.linenum) for l in ll.annotate(2)], |
|
154 | 160 | []) |
|
155 | 161 | |
|
156 | 162 | def testrandomedits(self): |
|
157 | 163 | # Inspired by original linelog tests. |
|
158 | 164 | seed = random.random() |
|
159 | 165 | numrevs = 2000 |
|
160 | 166 | ll = linelog.linelog() |
|
161 | 167 | # Populate linelog |
|
162 |
for lines, rev, a1, a2, b1, b2 in _genedits( |
|
|
163 | ll.replacelines(rev, a1, a2, b1, b2) | |
|
168 | for lines, rev, a1, a2, b1, b2, blines, usevec in _genedits( | |
|
169 | seed, numrevs): | |
|
170 | if usevec: | |
|
171 | ll.replacelines_vec(rev, a1, a2, blines) | |
|
172 | else: | |
|
173 | ll.replacelines(rev, a1, a2, b1, b2) | |
|
164 | 174 | ar = ll.annotate(rev) |
|
165 | 175 | self.assertEqual(ll.annotateresult, lines) |
|
166 | 176 | # Verify we can get back these states by annotating each rev |
|
167 |
for lines, rev, a1, a2, b1, b2 in _genedits( |
|
|
177 | for lines, rev, a1, a2, b1, b2, blines, usevec in _genedits( | |
|
178 | seed, numrevs): | |
|
168 | 179 | ar = ll.annotate(rev) |
|
169 | 180 | self.assertEqual([(l.rev, l.linenum) for l in ar], lines) |
|
170 | 181 | |
|
171 | 182 | if __name__ == '__main__': |
|
172 | 183 | import silenttestrunner |
|
173 | 184 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now