grep: move readfile() to grepsearcher class
authorYuya Nishihara <yuya@tcha.org>
Wed, 09 Sep 2020 16:04:39 +0900
changeset 45699 888e633f0c1c
parent 45698 41e0cbccb260
child 45700 c694b1841a5e
grep: move readfile() to grepsearcher class
mercurial/commands.py
mercurial/grep.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,
--- 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)}
+                )