Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/grep.py @ 45697:494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Prepares for extracting stateful functions from commands.grep().
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 09 Sep 2020 15:56:40 +0900 |
parents | de6f2afc0247 |
children | 41e0cbccb260 |
rev | line source |
---|---|
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 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
12 from . import ( |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
13 pycompat, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
14 scmutil, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
15 util, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
16 ) |
45696
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
17 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
18 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
19 def matchlines(body, regexp): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
20 begin = 0 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
21 linenum = 0 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 while begin < len(body): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 match = regexp.search(body, begin) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 if not match: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
25 break |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
26 mstart, mend = match.span() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 lend = begin - 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 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
|
32 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
33 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
34 class linestate(object): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
35 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
|
36 self.line = line |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
37 self.linenum = linenum |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 self.colstart = colstart |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 self.colend = colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
40 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
41 def __hash__(self): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
42 return hash(self.line) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
43 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
44 def __eq__(self, other): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
45 return self.line == other.line |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
46 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
47 def findpos(self, regexp): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
48 """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
|
49 yield self.colstart, self.colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
50 p = self.colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
51 while p < len(self.line): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
52 m = regexp.search(self.line, p) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
53 if not m: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
54 break |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
55 if m.end() == p: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
56 p += 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
57 else: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
58 yield m.span() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
59 p = m.end() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
60 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
61 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
62 def difflinestates(a, b): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
63 sm = difflib.SequenceMatcher(None, a, b) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
64 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
|
65 if tag == 'insert': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
66 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
|
67 yield (b'+', b[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
68 elif tag == 'delete': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
69 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
|
70 yield (b'-', a[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
71 elif tag == 'replace': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
72 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
|
73 yield (b'-', a[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
74 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
|
75 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
|
76 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
77 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
78 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
|
79 """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
|
80 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
86 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
|
87 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
|
88 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
89 self._matches = {} |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
90 self._copies = {} |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
91 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
|
92 self._revfiles = {} |