mercurial/grep.py
author Yuya Nishihara <yuya@tcha.org>
Wed, 09 Sep 2020 17:04:44 +0900
changeset 45719 c10c87c8fe79
parent 45700 c694b1841a5e
child 45721 f9d3ff23bfc0
permissions -rw-r--r--
grep: extract public function to register file to be skipped The main grep loop will be extracted to a searcher method, but this skipping condition depends on the result of display() function.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
45698
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   118
    def _grepbody(self, fn, rev, body):
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   119
        self._matches[rev].setdefault(fn, [])
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   120
        m = self._matches[rev][fn]
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   121
        if body is None:
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   122
            return
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   123
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   124
        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
   125
            s = linestate(line, lnum, cstart, cend)
41e0cbccb260 grep: move getbody() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45697
diff changeset
   126
            m.append(s)
45699
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
    def _readfile(self, ctx, fn):
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   129
        rev = ctx.rev()
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   130
        if rev is None:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   131
            fctx = ctx[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 fctx.data()
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   134
            except IOError as e:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   135
                if e.errno != errno.ENOENT:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   136
                    raise
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   137
        else:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   138
            flog = self._getfile(fn)
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   139
            fnode = ctx.filenode(fn)
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   140
            try:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   141
                return flog.read(fnode)
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   142
            except error.CensoredNodeError:
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   143
                self._ui.warn(
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   144
                    _(
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   145
                        b'cannot search in censored file: '
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   146
                        b'%(filename)s:%(revnum)s\n'
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   147
                    )
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   148
                    % {b'filename': fn, b'revnum': pycompat.bytestr(rev)}
888e633f0c1c grep: move readfile() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45698
diff changeset
   149
                )
45700
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   150
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   151
    def _prep(self, ctx, fmatch):
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   152
        rev = ctx.rev()
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   153
        pctx = ctx.p1()
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   154
        self._matches.setdefault(rev, {})
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   155
        if self._diff:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   156
            parent = pctx.rev()
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   157
            self._matches.setdefault(parent, {})
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   158
        files = self._revfiles.setdefault(rev, [])
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   159
        if rev is None:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   160
            # 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
   161
            # pathauditor checks without this in mozilla-central
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   162
            contextmanager = self._repo.wvfs.audit.cached
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   163
        else:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   164
            contextmanager = util.nullcontextmanager
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   165
        with contextmanager():
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   166
            # TODO: maybe better to warn missing files?
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   167
            if self._all_files:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   168
                fmatch = matchmod.badmatch(fmatch, lambda f, msg: None)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   169
                filenames = ctx.matches(fmatch)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   170
            else:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   171
                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
   172
            for fn in filenames:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   173
                # 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
   174
                # 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
   175
                # 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
   176
                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
   177
                    continue
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
                copy = None
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   180
                if self._follow:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   181
                    copy = self._getrenamed(fn, rev)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   182
                    if copy:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   183
                        self._copies.setdefault(rev, {})[fn] = copy
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   184
                        if fn in self._skip:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   185
                            self._skip.add(copy)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   186
                if fn in self._skip:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   187
                    continue
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   188
                files.append(fn)
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   189
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   190
                if fn not in self._matches[rev]:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   191
                    self._grepbody(fn, rev, self._readfile(ctx, fn))
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   192
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   193
                if self._diff:
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   194
                    pfn = copy or fn
c694b1841a5e grep: move prep() to grepsearcher class
Yuya Nishihara <yuya@tcha.org>
parents: 45699
diff changeset
   195
                    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
   196
                        self._grepbody(pfn, parent, self._readfile(pctx, pfn))