author | Yuya Nishihara <yuya@tcha.org> |
Wed, 09 Sep 2020 16:04:39 +0900 | |
changeset 45699 | 888e633f0c1c |
parent 45698 | 41e0cbccb260 |
child 45700 | c694b1841a5e |
permissions | -rw-r--r-- |
45696
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 |
# grep.py - logic for history walk and grep |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 |
# |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 |
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 |
# |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
8 |
from __future__ import absolute_import |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
10 |
import difflib |
45699
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
11 |
import errno |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
12 |
|
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
13 |
from .i18n import _ |
45696
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
14 |
|
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
15 |
from . import ( |
45699
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
16 |
error, |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
17 |
pycompat, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
18 |
scmutil, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
19 |
util, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
20 |
) |
45696
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
21 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 |
def matchlines(body, regexp): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 |
begin = 0 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
25 |
linenum = 0 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
26 |
while begin < len(body): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
27 |
match = regexp.search(body, begin) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
28 |
if not match: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
29 |
break |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
30 |
mstart, mend = match.span() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 |
linenum += body.count(b'\n', begin, mstart) + 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
32 |
lstart = body.rfind(b'\n', begin, mstart) + 1 or begin |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
33 |
begin = body.find(b'\n', mend) + 1 or len(body) + 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
34 |
lend = begin - 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
35 |
yield linenum, mstart - lstart, mend - lstart, body[lstart:lend] |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
36 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
37 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 |
class linestate(object): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 |
def __init__(self, line, linenum, colstart, colend): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
40 |
self.line = line |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
41 |
self.linenum = linenum |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
42 |
self.colstart = colstart |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
43 |
self.colend = colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
44 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
45 |
def __hash__(self): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
46 |
return hash(self.line) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
47 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
48 |
def __eq__(self, other): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
49 |
return self.line == other.line |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
50 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
51 |
def findpos(self, regexp): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
52 |
"""Iterate all (start, end) indices of matches""" |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
53 |
yield self.colstart, self.colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
54 |
p = self.colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
55 |
while p < len(self.line): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
56 |
m = regexp.search(self.line, p) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
57 |
if not m: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
58 |
break |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
59 |
if m.end() == p: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
60 |
p += 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
61 |
else: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
62 |
yield m.span() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
63 |
p = m.end() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
64 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
65 |
|
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
66 |
def difflinestates(a, b): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
67 |
sm = difflib.SequenceMatcher(None, a, b) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
68 |
for tag, alo, ahi, blo, bhi in sm.get_opcodes(): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
69 |
if tag == 'insert': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
70 |
for i in pycompat.xrange(blo, bhi): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
71 |
yield (b'+', b[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
72 |
elif tag == 'delete': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
73 |
for i in pycompat.xrange(alo, ahi): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
74 |
yield (b'-', a[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
75 |
elif tag == 'replace': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
76 |
for i in pycompat.xrange(alo, ahi): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
77 |
yield (b'-', a[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
78 |
for i in pycompat.xrange(blo, bhi): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
79 |
yield (b'+', b[i]) |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
80 |
|
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
81 |
|
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
82 |
class grepsearcher(object): |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
83 |
"""Search files and revisions for lines matching the given pattern""" |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
84 |
|
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
85 |
def __init__(self, ui, repo, regexp): |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
86 |
self._ui = ui |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
87 |
self._repo = repo |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
88 |
self._regexp = regexp |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
89 |
|
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
90 |
self._getfile = util.lrucachefunc(repo.file) |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
91 |
self._getrenamed = scmutil.getrenamedfn(repo) |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
92 |
|
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
93 |
self._matches = {} |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
94 |
self._copies = {} |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
95 |
self._skip = set() |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
96 |
self._revfiles = {} |
45698
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
97 |
|
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
98 |
def _grepbody(self, fn, rev, body): |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
99 |
self._matches[rev].setdefault(fn, []) |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
100 |
m = self._matches[rev][fn] |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
101 |
if body is None: |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
102 |
return |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
103 |
|
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
104 |
for lnum, cstart, cend, line in matchlines(body, self._regexp): |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
105 |
s = linestate(line, lnum, cstart, cend) |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
106 |
m.append(s) |
45699
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
107 |
|
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
108 |
def _readfile(self, ctx, fn): |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
109 |
rev = ctx.rev() |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
110 |
if rev is None: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
111 |
fctx = ctx[fn] |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
112 |
try: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
113 |
return fctx.data() |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
114 |
except IOError as e: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
115 |
if e.errno != errno.ENOENT: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
116 |
raise |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
117 |
else: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
118 |
flog = self._getfile(fn) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
119 |
fnode = ctx.filenode(fn) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
120 |
try: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
121 |
return flog.read(fnode) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
122 |
except error.CensoredNodeError: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
123 |
self._ui.warn( |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
124 |
_( |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
125 |
b'cannot search in censored file: ' |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
126 |
b'%(filename)s:%(revnum)s\n' |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
127 |
) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
128 |
% {b'filename': fn, b'revnum': pycompat.bytestr(rev)} |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
129 |
) |