##// END OF EJS Templates
grep: move readfile() to grepsearcher class
Yuya Nishihara -
r46291:888e633f default
parent child Browse files
Show More
@@ -3519,28 +3519,6 b' def grep(ui, repo, pattern, *pats, **opt'
3519
3519
3520 getrenamed = searcher._getrenamed
3520 getrenamed = searcher._getrenamed
3521
3521
3522 def readfile(ctx, fn):
3523 rev = ctx.rev()
3524 if rev is None:
3525 fctx = ctx[fn]
3526 try:
3527 return fctx.data()
3528 except IOError as e:
3529 if e.errno != errno.ENOENT:
3530 raise
3531 else:
3532 flog = getfile(fn)
3533 fnode = ctx.filenode(fn)
3534 try:
3535 return flog.read(fnode)
3536 except error.CensoredNodeError:
3537 ui.warn(
3538 _(
3539 b'cannot search in censored file: %(filename)s:%(revnum)s\n'
3540 )
3541 % {b'filename': fn, b'revnum': pycompat.bytestr(rev),}
3542 )
3543
3544 def prep(ctx, fmatch):
3522 def prep(ctx, fmatch):
3545 rev = ctx.rev()
3523 rev = ctx.rev()
3546 pctx = ctx.p1()
3524 pctx = ctx.p1()
@@ -3581,12 +3559,14 b' def grep(ui, repo, pattern, *pats, **opt'
3581 files.append(fn)
3559 files.append(fn)
3582
3560
3583 if fn not in matches[rev]:
3561 if fn not in matches[rev]:
3584 searcher._grepbody(fn, rev, readfile(ctx, fn))
3562 searcher._grepbody(fn, rev, searcher._readfile(ctx, fn))
3585
3563
3586 if diff:
3564 if diff:
3587 pfn = copy or fn
3565 pfn = copy or fn
3588 if pfn not in matches[parent] and pfn in pctx:
3566 if pfn not in matches[parent] and pfn in pctx:
3589 searcher._grepbody(pfn, parent, readfile(pctx, pfn))
3567 searcher._grepbody(
3568 pfn, parent, searcher._readfile(pctx, pfn)
3569 )
3590
3570
3591 wopts = logcmdutil.walkopts(
3571 wopts = logcmdutil.walkopts(
3592 pats=pats,
3572 pats=pats,
@@ -8,8 +8,12 b''
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import difflib
10 import difflib
11 import errno
12
13 from .i18n import _
11
14
12 from . import (
15 from . import (
16 error,
13 pycompat,
17 pycompat,
14 scmutil,
18 scmutil,
15 util,
19 util,
@@ -100,3 +104,26 b' class grepsearcher(object):'
100 for lnum, cstart, cend, line in matchlines(body, self._regexp):
104 for lnum, cstart, cend, line in matchlines(body, self._regexp):
101 s = linestate(line, lnum, cstart, cend)
105 s = linestate(line, lnum, cstart, cend)
102 m.append(s)
106 m.append(s)
107
108 def _readfile(self, ctx, fn):
109 rev = ctx.rev()
110 if rev is None:
111 fctx = ctx[fn]
112 try:
113 return fctx.data()
114 except IOError as e:
115 if e.errno != errno.ENOENT:
116 raise
117 else:
118 flog = self._getfile(fn)
119 fnode = ctx.filenode(fn)
120 try:
121 return flog.read(fnode)
122 except error.CensoredNodeError:
123 self._ui.warn(
124 _(
125 b'cannot search in censored file: '
126 b'%(filename)s:%(revnum)s\n'
127 )
128 % {b'filename': fn, b'revnum': pycompat.bytestr(rev)}
129 )
General Comments 0
You need to be logged in to leave comments. Login now