author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Wed, 16 Oct 2019 17:49:30 +0200 | |
changeset 43254 | 181d28ba05da |
parent 43077 | 687b865b95ad |
child 43472 | acc4047c2194 |
permissions | -rw-r--r-- |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 |
# linelog - efficient cache for annotate data |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 |
# |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 |
# Copyright 2018 Google LLC. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 |
# |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 |
"""linelog is an efficient cache for annotate data inspired by SCCS Weaves. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
8 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
9 |
SCCS Weaves are an implementation of |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
10 |
https://en.wikipedia.org/wiki/Interleaved_deltas. See |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
11 |
mercurial/help/internals/linelog.txt for an exploration of SCCS weaves |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
12 |
and how linelog works in detail. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
13 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
14 |
Here's a hacker's summary: a linelog is a program which is executed in |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
15 |
the context of a revision. Executing the program emits information |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
16 |
about lines, including the revision that introduced them and the line |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
17 |
number in the file at the introducing revision. When an insertion or |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
18 |
deletion is performed on the file, a jump instruction is used to patch |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
19 |
in a new body of annotate information. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
20 |
""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
21 |
from __future__ import absolute_import, print_function |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
22 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
23 |
import abc |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
24 |
import struct |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
25 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
26 |
from .thirdparty import attr |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
27 |
from . import pycompat |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
28 |
|
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
29 |
_llentry = struct.Struct(b'>II') |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
30 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
31 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
32 |
class LineLogError(Exception): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
33 |
"""Error raised when something bad happens internally in linelog.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
34 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
35 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
36 |
@attr.s |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
37 |
class lineinfo(object): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
38 |
# Introducing revision of this line. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
39 |
rev = attr.ib() |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
40 |
# Line number for this line in its introducing revision. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
41 |
linenum = attr.ib() |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
42 |
# Private. Offset in the linelog program of this line. Used internally. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
43 |
_offset = attr.ib() |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
44 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
45 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
46 |
@attr.s |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
47 |
class annotateresult(object): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
48 |
rev = attr.ib() |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
49 |
lines = attr.ib() |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
50 |
_eof = attr.ib() |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
51 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
52 |
def __iter__(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
53 |
return iter(self.lines) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
54 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
55 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
56 |
class _llinstruction(object): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
57 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
58 |
__metaclass__ = abc.ABCMeta |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
59 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
60 |
@abc.abstractmethod |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
61 |
def __init__(self, op1, op2): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
62 |
pass |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
63 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
64 |
@abc.abstractmethod |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
65 |
def __str__(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
66 |
pass |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
67 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
68 |
def __repr__(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
69 |
return str(self) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
70 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
71 |
@abc.abstractmethod |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
72 |
def __eq__(self, other): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
73 |
pass |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
74 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
75 |
@abc.abstractmethod |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
76 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
77 |
"""Encode this instruction to the binary linelog format.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
78 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
79 |
@abc.abstractmethod |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
80 |
def execute(self, rev, pc, emit): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
81 |
"""Execute this instruction. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
82 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
83 |
Args: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
84 |
rev: The revision we're annotating. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
85 |
pc: The current offset in the linelog program. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
86 |
emit: A function that accepts a single lineinfo object. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
87 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
88 |
Returns: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
89 |
The new value of pc. Returns None if exeuction should stop |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
90 |
(that is, we've found the end of the file.) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
91 |
""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
92 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
93 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
94 |
class _jge(_llinstruction): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
95 |
"""If the current rev is greater than or equal to op1, jump to op2.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
96 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
97 |
def __init__(self, op1, op2): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
98 |
self._cmprev = op1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
99 |
self._target = op2 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
100 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
101 |
def __str__(self): |
38809
57af5ee15b35
linelog: port to Python 3
Augie Fackler <augie@google.com>
parents:
38796
diff
changeset
|
102 |
return r'JGE %d %d' % (self._cmprev, self._target) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
103 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
104 |
def __eq__(self, other): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
105 |
return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
106 |
type(self) == type(other) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
107 |
and self._cmprev == other._cmprev |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
108 |
and self._target == other._target |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
109 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
110 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
111 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
112 |
return _llentry.pack(self._cmprev << 2, self._target) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
113 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
114 |
def execute(self, rev, pc, emit): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
115 |
if rev >= self._cmprev: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
116 |
return self._target |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
117 |
return pc + 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
118 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
119 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
120 |
class _jump(_llinstruction): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
121 |
"""Unconditional jumps are expressed as a JGE with op1 set to 0.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
122 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
123 |
def __init__(self, op1, op2): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
124 |
if op1 != 0: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
125 |
raise LineLogError(b"malformed JUMP, op1 must be 0, got %d" % op1) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
126 |
self._target = op2 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
127 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
128 |
def __str__(self): |
38809
57af5ee15b35
linelog: port to Python 3
Augie Fackler <augie@google.com>
parents:
38796
diff
changeset
|
129 |
return r'JUMP %d' % (self._target) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
130 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
131 |
def __eq__(self, other): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
132 |
return type(self) == type(other) and self._target == other._target |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
133 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
134 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
135 |
return _llentry.pack(0, self._target) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
136 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
137 |
def execute(self, rev, pc, emit): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
138 |
return self._target |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
139 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
140 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
141 |
class _eof(_llinstruction): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
142 |
"""EOF is expressed as a JGE that always jumps to 0.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
143 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
144 |
def __init__(self, op1, op2): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
145 |
if op1 != 0: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
146 |
raise LineLogError(b"malformed EOF, op1 must be 0, got %d" % op1) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
147 |
if op2 != 0: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
148 |
raise LineLogError(b"malformed EOF, op2 must be 0, got %d" % op2) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
149 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
150 |
def __str__(self): |
38809
57af5ee15b35
linelog: port to Python 3
Augie Fackler <augie@google.com>
parents:
38796
diff
changeset
|
151 |
return r'EOF' |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
152 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
153 |
def __eq__(self, other): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
154 |
return type(self) == type(other) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
155 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
156 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
157 |
return _llentry.pack(0, 0) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
158 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
159 |
def execute(self, rev, pc, emit): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
160 |
return None |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
161 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
162 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
163 |
class _jl(_llinstruction): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
164 |
"""If the current rev is less than op1, jump to op2.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
165 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
166 |
def __init__(self, op1, op2): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
167 |
self._cmprev = op1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
168 |
self._target = op2 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
169 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
170 |
def __str__(self): |
38809
57af5ee15b35
linelog: port to Python 3
Augie Fackler <augie@google.com>
parents:
38796
diff
changeset
|
171 |
return r'JL %d %d' % (self._cmprev, self._target) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
172 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
173 |
def __eq__(self, other): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
174 |
return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
175 |
type(self) == type(other) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
176 |
and self._cmprev == other._cmprev |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
177 |
and self._target == other._target |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
178 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
179 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
180 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
181 |
return _llentry.pack(1 | (self._cmprev << 2), self._target) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
182 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
183 |
def execute(self, rev, pc, emit): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
184 |
if rev < self._cmprev: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
185 |
return self._target |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
186 |
return pc + 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
187 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
188 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
189 |
class _line(_llinstruction): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
190 |
"""Emit a line.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
191 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
192 |
def __init__(self, op1, op2): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
193 |
# This line was introduced by this revision number. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
194 |
self._rev = op1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
195 |
# This line had the specified line number in the introducing revision. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
196 |
self._origlineno = op2 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
197 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
198 |
def __str__(self): |
38809
57af5ee15b35
linelog: port to Python 3
Augie Fackler <augie@google.com>
parents:
38796
diff
changeset
|
199 |
return r'LINE %d %d' % (self._rev, self._origlineno) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
200 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
201 |
def __eq__(self, other): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
202 |
return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
203 |
type(self) == type(other) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
204 |
and self._rev == other._rev |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
205 |
and self._origlineno == other._origlineno |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
206 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
207 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
208 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
209 |
return _llentry.pack(2 | (self._rev << 2), self._origlineno) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
210 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
211 |
def execute(self, rev, pc, emit): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
212 |
emit(lineinfo(self._rev, self._origlineno, pc)) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
213 |
return pc + 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
214 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
215 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
216 |
def _decodeone(data, offset): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
217 |
"""Decode a single linelog instruction from an offset in a buffer.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
218 |
try: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
219 |
op1, op2 = _llentry.unpack_from(data, offset) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
220 |
except struct.error as e: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
221 |
raise LineLogError(b'reading an instruction failed: %r' % e) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
222 |
opcode = op1 & 0b11 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
223 |
op1 = op1 >> 2 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
224 |
if opcode == 0: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
225 |
if op1 == 0: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
226 |
if op2 == 0: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
227 |
return _eof(op1, op2) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
228 |
return _jump(op1, op2) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
229 |
return _jge(op1, op2) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
230 |
elif opcode == 1: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
231 |
return _jl(op1, op2) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
232 |
elif opcode == 2: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
233 |
return _line(op1, op2) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
234 |
raise NotImplementedError(b'Unimplemented opcode %r' % opcode) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
235 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
236 |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
237 |
class linelog(object): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
238 |
"""Efficient cache for per-line history information.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
239 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
240 |
def __init__(self, program=None, maxrev=0): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
241 |
if program is None: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
242 |
# We pad the program with an extra leading EOF so that our |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
243 |
# offsets will match the C code exactly. This means we can |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
244 |
# interoperate with the C code. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
245 |
program = [_eof(0, 0), _eof(0, 0)] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
246 |
self._program = program |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
247 |
self._lastannotate = None |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
248 |
self._maxrev = maxrev |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
249 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
250 |
def __eq__(self, other): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
251 |
return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
252 |
type(self) == type(other) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
253 |
and self._program == other._program |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
254 |
and self._maxrev == other._maxrev |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
255 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
256 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
257 |
def __repr__(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
258 |
return b'<linelog at %s: maxrev=%d size=%d>' % ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
259 |
hex(id(self)), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
260 |
self._maxrev, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
261 |
len(self._program), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
262 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
263 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
264 |
def debugstr(self): |
38809
57af5ee15b35
linelog: port to Python 3
Augie Fackler <augie@google.com>
parents:
38796
diff
changeset
|
265 |
fmt = r'%%%dd %%s' % len(str(len(self._program))) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
266 |
return pycompat.sysstr(b'\n').join( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
267 |
fmt % (idx, i) for idx, i in enumerate(self._program[1:], 1) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
268 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
269 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
270 |
@classmethod |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
271 |
def fromdata(cls, buf): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
272 |
if len(buf) % _llentry.size != 0: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
273 |
raise LineLogError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
274 |
b"invalid linelog buffer size %d (must be a multiple of %d)" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
275 |
% (len(buf), _llentry.size) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
276 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
277 |
expected = len(buf) / _llentry.size |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
278 |
fakejge = _decodeone(buf, 0) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
279 |
if isinstance(fakejge, _jump): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
280 |
maxrev = 0 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
281 |
else: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
282 |
maxrev = fakejge._cmprev |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
283 |
numentries = fakejge._target |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
284 |
if expected != numentries: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
285 |
raise LineLogError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
286 |
b"corrupt linelog data: claimed" |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
287 |
b" %d entries but given data for %d entries" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
288 |
% (expected, numentries) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
289 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
290 |
instructions = [_eof(0, 0)] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
291 |
for offset in pycompat.xrange(1, numentries): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
292 |
instructions.append(_decodeone(buf, offset * _llentry.size)) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
293 |
return cls(instructions, maxrev=maxrev) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
294 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
295 |
def encode(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
296 |
hdr = _jge(self._maxrev, len(self._program)).encode() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
297 |
return hdr + b''.join(i.encode() for i in self._program[1:]) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
298 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
299 |
def clear(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
300 |
self._program = [] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
301 |
self._maxrev = 0 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
302 |
self._lastannotate = None |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
303 |
|
38924
6fed8b323651
linelog: add replacelines_vec for fastannotate
Augie Fackler <augie@google.com>
parents:
38809
diff
changeset
|
304 |
def replacelines_vec(self, rev, a1, a2, blines): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
305 |
return self.replacelines( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
306 |
rev, a1, a2, 0, len(blines), _internal_blines=blines |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
307 |
) |
38924
6fed8b323651
linelog: add replacelines_vec for fastannotate
Augie Fackler <augie@google.com>
parents:
38809
diff
changeset
|
308 |
|
6fed8b323651
linelog: add replacelines_vec for fastannotate
Augie Fackler <augie@google.com>
parents:
38809
diff
changeset
|
309 |
def replacelines(self, rev, a1, a2, b1, b2, _internal_blines=None): |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
310 |
"""Replace lines [a1, a2) with lines [b1, b2).""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
311 |
if self._lastannotate: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
312 |
# TODO(augie): make replacelines() accept a revision at |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
313 |
# which we're editing as well as a revision to mark |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
314 |
# responsible for the edits. In hg-experimental it's |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
315 |
# stateful like this, so we're doing the same thing to |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
316 |
# retain compatibility with absorb until that's imported. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
317 |
ar = self._lastannotate |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
318 |
else: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
319 |
ar = self.annotate(rev) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
320 |
# ar = self.annotate(self._maxrev) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
321 |
if a1 > len(ar.lines): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
322 |
raise LineLogError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
323 |
b'%d contains %d lines, tried to access line %d' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
324 |
% (rev, len(ar.lines), a1) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
325 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
326 |
elif a1 == len(ar.lines): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
327 |
# Simulated EOF instruction since we're at EOF, which |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
328 |
# doesn't have a "real" line. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
329 |
a1inst = _eof(0, 0) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
330 |
a1info = lineinfo(0, 0, ar._eof) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
331 |
else: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
332 |
a1info = ar.lines[a1] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
333 |
a1inst = self._program[a1info._offset] |
38970
32b1967b8734
linelog: extract `len(self._program)` to a local function
Jun Wu <quark@fb.com>
parents:
38962
diff
changeset
|
334 |
programlen = self._program.__len__ |
32b1967b8734
linelog: extract `len(self._program)` to a local function
Jun Wu <quark@fb.com>
parents:
38962
diff
changeset
|
335 |
oldproglen = programlen() |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
336 |
appendinst = self._program.append |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
337 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
338 |
# insert |
38971 | 339 |
blineinfos = [] |
340 |
bappend = blineinfos.append |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
341 |
if b1 < b2: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
342 |
# Determine the jump target for the JGE at the start of |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
343 |
# the new block. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
344 |
tgt = oldproglen + (b2 - b1 + 1) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
345 |
# Jump to skip the insert if we're at an older revision. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
346 |
appendinst(_jl(rev, tgt)) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
347 |
for linenum in pycompat.xrange(b1, b2): |
38924
6fed8b323651
linelog: add replacelines_vec for fastannotate
Augie Fackler <augie@google.com>
parents:
38809
diff
changeset
|
348 |
if _internal_blines is None: |
38971 | 349 |
bappend(lineinfo(rev, linenum, programlen())) |
38924
6fed8b323651
linelog: add replacelines_vec for fastannotate
Augie Fackler <augie@google.com>
parents:
38809
diff
changeset
|
350 |
appendinst(_line(rev, linenum)) |
6fed8b323651
linelog: add replacelines_vec for fastannotate
Augie Fackler <augie@google.com>
parents:
38809
diff
changeset
|
351 |
else: |
38971 | 352 |
newrev, newlinenum = _internal_blines[linenum] |
353 |
bappend(lineinfo(newrev, newlinenum, programlen())) |
|
354 |
appendinst(_line(newrev, newlinenum)) |
|
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
355 |
# delete |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
356 |
if a1 < a2: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
357 |
if a2 > len(ar.lines): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
358 |
raise LineLogError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
359 |
b'%d contains %d lines, tried to access line %d' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
360 |
% (rev, len(ar.lines), a2) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
361 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
362 |
elif a2 == len(ar.lines): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
363 |
endaddr = ar._eof |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
364 |
else: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
365 |
endaddr = ar.lines[a2]._offset |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
366 |
if a2 > 0 and rev < self._maxrev: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
367 |
# If we're here, we're deleting a chunk of an old |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
368 |
# commit, so we need to be careful and not touch |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
369 |
# invisible lines between a2-1 and a2 (IOW, lines that |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
370 |
# are added later). |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
371 |
endaddr = ar.lines[a2 - 1]._offset + 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
372 |
appendinst(_jge(rev, endaddr)) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
373 |
# copy instruction from a1 |
38971 | 374 |
a1instpc = programlen() |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
375 |
appendinst(a1inst) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
376 |
# if a1inst isn't a jump or EOF, then we need to add an unconditional |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
377 |
# jump back into the program here. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
378 |
if not isinstance(a1inst, (_jump, _eof)): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
379 |
appendinst(_jump(0, a1info._offset + 1)) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
380 |
# Patch instruction at a1, which makes our patch live. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
381 |
self._program[a1info._offset] = _jump(0, oldproglen) |
38971 | 382 |
|
383 |
# Update self._lastannotate in place. This serves as a cache to avoid |
|
384 |
# expensive "self.annotate" in this function, when "replacelines" is |
|
385 |
# used continuously. |
|
386 |
if len(self._lastannotate.lines) > a1: |
|
387 |
self._lastannotate.lines[a1]._offset = a1instpc |
|
388 |
else: |
|
389 |
assert isinstance(a1inst, _eof) |
|
390 |
self._lastannotate._eof = a1instpc |
|
391 |
self._lastannotate.lines[a1:a2] = blineinfos |
|
392 |
self._lastannotate.rev = max(self._lastannotate.rev, rev) |
|
393 |
||
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
394 |
if rev > self._maxrev: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
395 |
self._maxrev = rev |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
396 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
397 |
def annotate(self, rev): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
398 |
pc = 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
399 |
lines = [] |
38935
27a54096c92e
linelog: fix infinite loop vulnerability
Jun Wu <quark@fb.com>
parents:
38924
diff
changeset
|
400 |
executed = 0 |
27a54096c92e
linelog: fix infinite loop vulnerability
Jun Wu <quark@fb.com>
parents:
38924
diff
changeset
|
401 |
# Sanity check: if instructions executed exceeds len(program), we |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
402 |
# hit an infinite loop in the linelog program somehow and we |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
403 |
# should stop. |
38935
27a54096c92e
linelog: fix infinite loop vulnerability
Jun Wu <quark@fb.com>
parents:
38924
diff
changeset
|
404 |
while pc is not None and executed < len(self._program): |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
405 |
inst = self._program[pc] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
406 |
lastpc = pc |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
407 |
pc = inst.execute(rev, pc, lines.append) |
38935
27a54096c92e
linelog: fix infinite loop vulnerability
Jun Wu <quark@fb.com>
parents:
38924
diff
changeset
|
408 |
executed += 1 |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
409 |
if pc is not None: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
410 |
raise LineLogError( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
411 |
r'Probably hit an infinite loop in linelog. Program:\n' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
412 |
+ self.debugstr() |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38971
diff
changeset
|
413 |
) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
414 |
ar = annotateresult(rev, lines, lastpc) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
415 |
self._lastannotate = ar |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
416 |
return ar |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
417 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
418 |
@property |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
419 |
def maxrev(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
420 |
return self._maxrev |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
421 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
422 |
# Stateful methods which depend on the value of the last |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
423 |
# annotation run. This API is for compatiblity with the original |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
424 |
# linelog, and we should probably consider refactoring it. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
425 |
@property |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
426 |
def annotateresult(self): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
427 |
"""Return the last annotation result. C linelog code exposed this.""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
428 |
return [(l.rev, l.linenum) for l in self._lastannotate.lines] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
429 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
430 |
def getoffset(self, line): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
431 |
return self._lastannotate.lines[line]._offset |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
432 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
433 |
def getalllines(self, start=0, end=0): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
434 |
"""Get all lines that ever occurred in [start, end). |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
435 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
436 |
Passing start == end == 0 means "all lines ever". |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
437 |
|
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
438 |
This works in terms of *internal* program offsets, not line numbers. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
439 |
""" |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
440 |
pc = start or 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
441 |
lines = [] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
442 |
# only take as many steps as there are instructions in the |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
443 |
# program - if we don't find an EOF or our stop-line before |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
444 |
# then, something is badly broken. |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
445 |
for step in pycompat.xrange(len(self._program)): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
446 |
inst = self._program[pc] |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
447 |
nextpc = pc + 1 |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
448 |
if isinstance(inst, _jump): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
449 |
nextpc = inst._target |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
450 |
elif isinstance(inst, _eof): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
451 |
return lines |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
452 |
elif isinstance(inst, (_jl, _jge)): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
453 |
pass |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
454 |
elif isinstance(inst, _line): |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
455 |
lines.append((inst._rev, inst._origlineno)) |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
456 |
else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
457 |
raise LineLogError(b"Illegal instruction %r" % inst) |
38795
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
458 |
if nextpc == end: |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
459 |
return lines |
422d661056be
linelog: add a Python implementation of the linelog datastructure
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
460 |
pc = nextpc |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
461 |
raise LineLogError(b"Failed to perform getalllines") |