Patchwork [3,of,6] grep: filter target files by matcher

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 3, 2020, 4:10 a.m.
Message ID <8eec1c384c992f9ce343.1601698207@mimosa>
Download mbox | patch
Permalink /patch/47370/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 3, 2020, 4:10 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1599725643 -32400
#      Thu Sep 10 17:14:03 2020 +0900
# Node ID 8eec1c384c992f9ce343a4245c2abe5f371b9bcd
# Parent  c0d5362379c3b13d6d644260d395bad169888924
grep: filter target files by matcher

Prepares for the migration to logcmdutil's logic, where cmdutil.walkchangerevs()
won't always build an exact set of paths to be scanned.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -45,6 +45,7 @@  from . import (
     help,
     hg,
     logcmdutil,
+    match as matchmod,
     merge as mergemod,
     mergestate as mergestatemod,
     narrowspec,
@@ -3621,8 +3622,13 @@  def grep(ui, repo, pattern, *pats, **opt
         else:
             contextmanager = util.nullcontextmanager
         with contextmanager():
-            assert fmatch.isexact()
-            for fn in fmatch.files():
+            # TODO: maybe better to warn missing files?
+            if all_files:
+                fmatch = matchmod.badmatch(fmatch, lambda f, msg: None)
+                filenames = ctx.matches(fmatch)
+            else:
+                filenames = (f for f in ctx.files() if fmatch(f))
+            for fn in filenames:
                 # fn might not exist in the revision (could be a file removed by
                 # the revision). We could check `fn not in ctx` even when rev is
                 # None, but it's less racy to protect againt that in readfile.