Mercurial > hg
annotate mercurial/grep.py @ 48488:f8540fe4be0f
procutil: avoid an uninitialized variable usage on tempfile exception
If `pycompat.unnamedtempfile()` raises an exception, it would have called
`stdin.close()` in the `finally` block without it being initialized first.
Differential Revision: https://phab.mercurial-scm.org/D11928
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Tue, 14 Dec 2021 17:29:30 -0500 |
parents | d4ba4d51f85f |
children | 6000f5b25c9b |
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 # |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents:
45721
diff
changeset
|
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> |
45696
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, |
45700
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
17 match as matchmod, |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
18 pycompat, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
19 scmutil, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
20 util, |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
21 ) |
45696
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 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 def matchlines(body, regexp): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
25 begin = 0 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
26 linenum = 0 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
27 while begin < len(body): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
28 match = regexp.search(body, begin) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
29 if not match: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
30 break |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 mstart, mend = match.span() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 lend = begin - 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
36 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
|
37 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 class linestate(object): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
40 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
|
41 self.line = line |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
42 self.linenum = linenum |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
43 self.colstart = colstart |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
44 self.colend = colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
45 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
46 def __hash__(self): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
47 return hash(self.line) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
48 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
49 def __eq__(self, other): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
50 return self.line == other.line |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
51 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
52 def findpos(self, regexp): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
53 """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
|
54 yield self.colstart, self.colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
55 p = self.colend |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
56 while p < len(self.line): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
57 m = regexp.search(self.line, p) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
58 if not m: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
59 break |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
60 if m.end() == p: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
61 p += 1 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
62 else: |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
63 yield m.span() |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
64 p = m.end() |
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 |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
67 def difflinestates(a, b): |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
68 sm = difflib.SequenceMatcher(None, a, b) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
69 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
|
70 if tag == 'insert': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
71 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
|
72 yield (b'+', b[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
73 elif tag == 'delete': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
74 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
|
75 yield (b'-', a[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
76 elif tag == 'replace': |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
77 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
|
78 yield (b'-', a[i]) |
de6f2afc0247
grep: move match and diff logic to new module
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
79 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
|
80 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
|
81 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
82 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
83 class grepsearcher(object): |
45700
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
84 """Search files and revisions for lines matching the given pattern |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
85 |
45700
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
86 Options: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
87 - all_files to search unchanged files at that revision. |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
88 - diff to search files in the parent revision so diffs can be generated. |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
89 - follow to skip files across copies and renames. |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
90 """ |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
91 |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
92 def __init__( |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
93 self, ui, repo, regexp, all_files=False, diff=False, follow=False |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
94 ): |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
95 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
|
96 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
|
97 self._regexp = regexp |
45700
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
98 self._all_files = all_files |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
99 self._diff = diff |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
100 self._follow = follow |
45697
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
101 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
102 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
|
103 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
|
104 |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
105 self._matches = {} |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
106 self._copies = {} |
494642ed3c50
grep: add stub class that maintains cache and states of grep operation
Yuya Nishihara <yuya@tcha.org>
parents:
45696
diff
changeset
|
107 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
|
108 self._revfiles = {} |
45698
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
109 |
45719
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
110 def skipfile(self, fn, rev): |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
111 """Exclude the given file (and the copy at the specified revision) |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
112 from future search""" |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
113 copy = self._copies.get(rev, {}).get(fn) |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
114 self._skip.add(fn) |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
115 if copy: |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
116 self._skip.add(copy) |
c10c87c8fe79
grep: extract public function to register file to be skipped
Yuya Nishihara <yuya@tcha.org>
parents:
45700
diff
changeset
|
117 |
45721
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
118 def searchfiles(self, revs, makefilematcher): |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
119 """Walk files and revisions to yield (fn, ctx, pstates, states) |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
120 matches |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
121 |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
122 states is a list of linestate objects. pstates may be empty unless |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
123 diff is True. |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
124 """ |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
125 for ctx in scmutil.walkchangerevs( |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
126 self._repo, revs, makefilematcher, self._prep |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
127 ): |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
128 rev = ctx.rev() |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
129 parent = ctx.p1().rev() |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
130 for fn in sorted(self._revfiles.get(rev, [])): |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
131 states = self._matches[rev][fn] |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
132 copy = self._copies.get(rev, {}).get(fn) |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
133 if fn in self._skip: |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
134 if copy: |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
135 self._skip.add(copy) |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
136 continue |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
137 pstates = self._matches.get(parent, {}).get(copy or fn, []) |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
138 if pstates or states: |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
139 yield fn, ctx, pstates, states |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
140 del self._revfiles[rev] |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
141 # We will keep the matches dict for the duration of the window |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
142 # clear the matches dict once the window is over |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
143 if not self._revfiles: |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
144 self._matches.clear() |
f9d3ff23bfc0
grep: extract main search loop as searcher method
Yuya Nishihara <yuya@tcha.org>
parents:
45719
diff
changeset
|
145 |
45698
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
146 def _grepbody(self, fn, rev, body): |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
147 self._matches[rev].setdefault(fn, []) |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
148 m = self._matches[rev][fn] |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
149 if body is None: |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
150 return |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
151 |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
152 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
|
153 s = linestate(line, lnum, cstart, cend) |
41e0cbccb260
grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45697
diff
changeset
|
154 m.append(s) |
45699
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
155 |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
156 def _readfile(self, ctx, fn): |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
157 rev = ctx.rev() |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
158 if rev is None: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
159 fctx = ctx[fn] |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
160 try: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
161 return fctx.data() |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
162 except IOError as e: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
163 if e.errno != errno.ENOENT: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
164 raise |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
165 else: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
166 flog = self._getfile(fn) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
167 fnode = ctx.filenode(fn) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
168 try: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
169 return flog.read(fnode) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
170 except error.CensoredNodeError: |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
171 self._ui.warn( |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
172 _( |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
173 b'cannot search in censored file: ' |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
174 b'%(filename)s:%(revnum)s\n' |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
175 ) |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
176 % {b'filename': fn, b'revnum': pycompat.bytestr(rev)} |
888e633f0c1c
grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45698
diff
changeset
|
177 ) |
45700
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
178 |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
179 def _prep(self, ctx, fmatch): |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
180 rev = ctx.rev() |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
181 pctx = ctx.p1() |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
182 self._matches.setdefault(rev, {}) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
183 if self._diff: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
184 parent = pctx.rev() |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
185 self._matches.setdefault(parent, {}) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
186 files = self._revfiles.setdefault(rev, []) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
187 if rev is None: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
188 # in `hg grep pattern`, 2/3 of the time is spent is spent in |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
189 # pathauditor checks without this in mozilla-central |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
190 contextmanager = self._repo.wvfs.audit.cached |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
191 else: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
192 contextmanager = util.nullcontextmanager |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
193 with contextmanager(): |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
194 # TODO: maybe better to warn missing files? |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
195 if self._all_files: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
196 fmatch = matchmod.badmatch(fmatch, lambda f, msg: None) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
197 filenames = ctx.matches(fmatch) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
198 else: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
199 filenames = (f for f in ctx.files() if fmatch(f)) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
200 for fn in filenames: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
201 # fn might not exist in the revision (could be a file removed by |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
202 # the revision). We could check `fn not in ctx` even when rev is |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
203 # None, but it's less racy to protect againt that in readfile. |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
204 if rev is not None and fn not in ctx: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
205 continue |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
206 |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
207 copy = None |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
208 if self._follow: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
209 copy = self._getrenamed(fn, rev) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
210 if copy: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
211 self._copies.setdefault(rev, {})[fn] = copy |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
212 if fn in self._skip: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
213 self._skip.add(copy) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
214 if fn in self._skip: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
215 continue |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
216 files.append(fn) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
217 |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
218 if fn not in self._matches[rev]: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
219 self._grepbody(fn, rev, self._readfile(ctx, fn)) |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
220 |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
221 if self._diff: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
222 pfn = copy or fn |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
223 if pfn not in self._matches[parent] and pfn in pctx: |
c694b1841a5e
grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents:
45699
diff
changeset
|
224 self._grepbody(pfn, parent, self._readfile(pctx, pfn)) |