annotate mercurial/grep.py @ 45702:deeb215be337

tests: update test-copies-chain-merge.t to not use empty files Merging empty files is not very interesting or realistic. Differential Revision: https://phab.mercurial-scm.org/D9172
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 08 Oct 2020 15:09:28 -0700
parents c694b1841a5e
children c10c87c8fe79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
110 def _grepbody(self, fn, rev, body):
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
111 self._matches[rev].setdefault(fn, [])
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
112 m = self._matches[rev][fn]
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
113 if body is None:
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
114 return
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
115
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
116 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
117 s = linestate(line, lnum, cstart, cend)
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
118 m.append(s)
45699
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
119
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
120 def _readfile(self, ctx, fn):
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
121 rev = ctx.rev()
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
122 if rev is None:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
123 fctx = ctx[fn]
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
124 try:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
125 return fctx.data()
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
126 except IOError as e:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
127 if e.errno != errno.ENOENT:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
128 raise
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
129 else:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
130 flog = self._getfile(fn)
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
131 fnode = ctx.filenode(fn)
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
132 try:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
133 return flog.read(fnode)
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
134 except error.CensoredNodeError:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
135 self._ui.warn(
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
136 _(
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
137 b'cannot search in censored file: '
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
138 b'%(filename)s:%(revnum)s\n'
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
139 )
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
140 % {b'filename': fn, b'revnum': pycompat.bytestr(rev)}
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
141 )
45700
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
142
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
143 def _prep(self, ctx, fmatch):
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
144 rev = ctx.rev()
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
145 pctx = ctx.p1()
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
146 self._matches.setdefault(rev, {})
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
147 if self._diff:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
148 parent = pctx.rev()
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
149 self._matches.setdefault(parent, {})
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
150 files = self._revfiles.setdefault(rev, [])
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
151 if rev is None:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
152 # 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
153 # pathauditor checks without this in mozilla-central
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
154 contextmanager = self._repo.wvfs.audit.cached
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
155 else:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
156 contextmanager = util.nullcontextmanager
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
157 with contextmanager():
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
158 # TODO: maybe better to warn missing files?
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
159 if self._all_files:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
160 fmatch = matchmod.badmatch(fmatch, lambda f, msg: None)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
161 filenames = ctx.matches(fmatch)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
162 else:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
163 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
164 for fn in filenames:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
165 # 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
166 # 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
167 # 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
168 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
169 continue
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
170
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
171 copy = None
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
172 if self._follow:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
173 copy = self._getrenamed(fn, rev)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
174 if copy:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
175 self._copies.setdefault(rev, {})[fn] = copy
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
176 if fn in self._skip:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
177 self._skip.add(copy)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
178 if fn in self._skip:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
179 continue
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
180 files.append(fn)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
181
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
182 if fn not in self._matches[rev]:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
183 self._grepbody(fn, rev, self._readfile(ctx, fn))
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
184
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
185 if self._diff:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
186 pfn = copy or fn
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
187 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
188 self._grepbody(pfn, parent, self._readfile(pctx, pfn))