# HG changeset patch # User Yuya Nishihara # Date 1599635079 -32400 # Node ID 888e633f0c1cd494a5dbd5b1517c3cba8373ee52 # Parent 41e0cbccb260b295dae6540fa6c48be70901184a grep: move readfile() to grepsearcher class diff -r 41e0cbccb260 -r 888e633f0c1c mercurial/commands.py --- a/mercurial/commands.py Wed Sep 09 16:00:03 2020 +0900 +++ b/mercurial/commands.py Wed Sep 09 16:04:39 2020 +0900 @@ -3519,28 +3519,6 @@ 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 @@ 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 -r 41e0cbccb260 -r 888e633f0c1c mercurial/grep.py --- a/mercurial/grep.py Wed Sep 09 16:00:03 2020 +0900 +++ b/mercurial/grep.py Wed Sep 09 16:04:39 2020 +0900 @@ -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 @@ 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)} + )