# HG changeset patch # User Yuya Nishihara # Date 2020-09-09 07:04:39 # Node ID 888e633f0c1cd494a5dbd5b1517c3cba8373ee52 # Parent 41e0cbccb260b295dae6540fa6c48be70901184a grep: move readfile() to grepsearcher class diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -3519,28 +3519,6 @@ def grep(ui, repo, pattern, *pats, **opt getrenamed = searcher._getrenamed - def readfile(ctx, fn): - rev = ctx.rev() - if rev is None: - fctx = ctx[fn] - try: - return fctx.data() - except IOError as e: - if e.errno != errno.ENOENT: - raise - else: - flog = getfile(fn) - fnode = ctx.filenode(fn) - try: - return flog.read(fnode) - except error.CensoredNodeError: - ui.warn( - _( - b'cannot search in censored file: %(filename)s:%(revnum)s\n' - ) - % {b'filename': fn, b'revnum': pycompat.bytestr(rev),} - ) - def prep(ctx, fmatch): rev = ctx.rev() pctx = ctx.p1() @@ -3581,12 +3559,14 @@ def grep(ui, repo, pattern, *pats, **opt files.append(fn) if fn not in matches[rev]: - searcher._grepbody(fn, rev, readfile(ctx, fn)) + searcher._grepbody(fn, rev, searcher._readfile(ctx, fn)) if diff: pfn = copy or fn if pfn not in matches[parent] and pfn in pctx: - searcher._grepbody(pfn, parent, readfile(pctx, pfn)) + searcher._grepbody( + pfn, parent, searcher._readfile(pctx, pfn) + ) wopts = logcmdutil.walkopts( pats=pats, diff --git a/mercurial/grep.py b/mercurial/grep.py --- a/mercurial/grep.py +++ b/mercurial/grep.py @@ -8,8 +8,12 @@ from __future__ import absolute_import import difflib +import errno + +from .i18n import _ from . import ( + error, pycompat, scmutil, util, @@ -100,3 +104,26 @@ class grepsearcher(object): for lnum, cstart, cend, line in matchlines(body, self._regexp): s = linestate(line, lnum, cstart, cend) m.append(s) + + def _readfile(self, ctx, fn): + rev = ctx.rev() + if rev is None: + fctx = ctx[fn] + try: + return fctx.data() + except IOError as e: + if e.errno != errno.ENOENT: + raise + else: + flog = self._getfile(fn) + fnode = ctx.filenode(fn) + try: + return flog.read(fnode) + except error.CensoredNodeError: + self._ui.warn( + _( + b'cannot search in censored file: ' + b'%(filename)s:%(revnum)s\n' + ) + % {b'filename': fn, b'revnum': pycompat.bytestr(rev)} + )