changeset 45628:7f033a587414

log: extract function that builds (revs, makefilematcher) from walkopts "hg grep" and "hg churn" will use this interface.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 10 Sep 2020 17:44:34 +0900
parents 224c786f4fce
children 292962570857
files mercurial/logcmdutil.py
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/logcmdutil.py	Sat Sep 12 22:42:58 2020 +0900
+++ b/mercurial/logcmdutil.py	Thu Sep 10 17:44:34 2020 +0900
@@ -47,13 +47,14 @@
 if pycompat.TYPE_CHECKING:
     from typing import (
         Any,
+        Callable,
         Dict,
         List,
         Optional,
         Tuple,
     )
 
-    for t in (Any, Dict, List, Optional, Tuple):
+    for t in (Any, Callable, Dict, List, Optional, Tuple):
         assert t
 
 
@@ -721,7 +722,7 @@
     # type: (Any, List[bytes], Dict[bytes, Any]) -> walkopts
     """Parse log command options into walkopts
 
-    The returned walkopts will be passed in to getrevs().
+    The returned walkopts will be passed in to getrevs() or makewalker().
     """
     if opts.get(b'follow_first'):
         follow = 1
@@ -956,11 +957,12 @@
     return revs
 
 
-def getrevs(repo, wopts):
-    # type: (Any, walkopts) -> Tuple[smartset.abstractsmartset, Optional[changesetdiffer]]
-    """Return (revs, differ) where revs is a smartset
+def makewalker(repo, wopts):
+    # type: (Any, walkopts) -> Tuple[smartset.abstractsmartset, Optional[Callable[[Any], matchmod.basematcher]]]
+    """Build (revs, makefilematcher) to scan revision/file history
 
-    differ is a changesetdiffer with pre-configured file matcher.
+    - revs is the smartset to be traversed.
+    - makefilematcher is a function to map ctx to a matcher for that revision
     """
     revs = _initialrevs(repo, wopts)
     if not revs:
@@ -1003,6 +1005,18 @@
     if wopts.limit is not None:
         revs = revs.slice(0, wopts.limit)
 
+    return revs, filematcher
+
+
+def getrevs(repo, wopts):
+    # type: (Any, walkopts) -> Tuple[smartset.abstractsmartset, Optional[changesetdiffer]]
+    """Return (revs, differ) where revs is a smartset
+
+    differ is a changesetdiffer with pre-configured file matcher.
+    """
+    revs, filematcher = makewalker(repo, wopts)
+    if not revs:
+        return revs, None
     differ = changesetdiffer()
     differ._makefilematcher = filematcher
     return revs, differ