comparison mercurial/commands.py @ 45721:f9d3ff23bfc0

grep: extract main search loop as searcher method Still displayer part is in commands.grep(), the core grep logic is now reusable. I'll revisit the displayer stuff later since it will be another long series.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 09 Sep 2020 17:17:38 +0900
parents 508dfd1c18df
children 03690079d7dd
comparison
equal deleted inserted replaced
45720:508dfd1c18df 45721:f9d3ff23bfc0
3401 searcher = grepmod.grepsearcher( 3401 searcher = grepmod.grepsearcher(
3402 ui, repo, regexp, all_files=all_files, diff=diff, follow=follow 3402 ui, repo, regexp, all_files=all_files, diff=diff, follow=follow
3403 ) 3403 )
3404 3404
3405 getfile = searcher._getfile 3405 getfile = searcher._getfile
3406 matches = searcher._matches
3407 copies = searcher._copies
3408 3406
3409 uipathfn = scmutil.getuipathfn(repo) 3407 uipathfn = scmutil.getuipathfn(repo)
3410 3408
3411 def display(fm, fn, ctx, pstates, states): 3409 def display(fm, fn, ctx, pstates, states):
3412 rev = scmutil.intrev(ctx) 3410 rev = scmutil.intrev(ctx)
3512 fm.startitem() 3510 fm.startitem()
3513 fm.write(b'text', b'%s', l.line[p:]) 3511 fm.write(b'text', b'%s', l.line[p:])
3514 fm.data(matched=False) 3512 fm.data(matched=False)
3515 fm.end() 3513 fm.end()
3516 3514
3517 skip = searcher._skip
3518 revfiles = searcher._revfiles
3519 found = False 3515 found = False
3520 3516
3521 wopts = logcmdutil.walkopts( 3517 wopts = logcmdutil.walkopts(
3522 pats=pats, 3518 pats=pats,
3523 opts=opts, 3519 opts=opts,
3530 ) 3526 )
3531 revs, makefilematcher = logcmdutil.makewalker(repo, wopts) 3527 revs, makefilematcher = logcmdutil.makewalker(repo, wopts)
3532 3528
3533 ui.pager(b'grep') 3529 ui.pager(b'grep')
3534 fm = ui.formatter(b'grep', opts) 3530 fm = ui.formatter(b'grep', opts)
3535 for ctx in scmutil.walkchangerevs( 3531 for fn, ctx, pstates, states in searcher.searchfiles(revs, makefilematcher):
3536 repo, revs, makefilematcher, searcher._prep 3532 r = display(fm, fn, ctx, pstates, states)
3537 ): 3533 found = found or r
3538 rev = ctx.rev() 3534 if r and not diff and not all_files:
3539 parent = ctx.p1().rev() 3535 searcher.skipfile(fn, ctx.rev())
3540 for fn in sorted(revfiles.get(rev, [])):
3541 states = matches[rev][fn]
3542 copy = copies.get(rev, {}).get(fn)
3543 if fn in skip:
3544 if copy:
3545 skip.add(copy)
3546 continue
3547 pstates = matches.get(parent, {}).get(copy or fn, [])
3548 if pstates or states:
3549 r = display(fm, fn, ctx, pstates, states)
3550 found = found or r
3551 if r and not diff and not all_files:
3552 searcher.skipfile(fn, rev)
3553 del revfiles[rev]
3554 # We will keep the matches dict for the duration of the window
3555 # clear the matches dict once the window is over
3556 if not revfiles:
3557 matches.clear()
3558 fm.end() 3536 fm.end()
3559 3537
3560 return not found 3538 return not found
3561 3539
3562 3540