Patchwork [5,of,6] grep: move readfile() to grepsearcher class

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 7, 2020, 12:56 p.m.
Message ID <3bc6728f3ab93a4b2831.1602075364@mimosa>
Download mbox | patch
Permalink /patch/47403/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 7, 2020, 12:56 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1599635079 -32400
#      Wed Sep 09 16:04:39 2020 +0900
# Node ID 3bc6728f3ab93a4b2831c706052bbd6fe7090b23
# Parent  a19bef408f16ba119ae395379df62aba2586c42a
grep: move readfile() to grepsearcher class

Patch

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)}
+                )