Patchwork [3,of,4] revset: extract a parsefollowlinespattern helper function

login
register
mail settings
Submitter Denis Laxalde
Date Oct. 4, 2017, 3:04 p.m.
Message ID <9274bdbba0f913672305.1507129441@sh77.tls.logilab.fr>
Download mbox | patch
Permalink /patch/24497/
State Changes Requested
Headers show

Comments

Denis Laxalde - Oct. 4, 2017, 3:04 p.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1507123663 -7200
#      Wed Oct 04 15:27:43 2017 +0200
# Node ID 9274bdbba0f913672305ed3d91c7e05f38d1eab7
# Parent  f7b31d85dc0e7c7e60b85d0a7fd728a32ba210ea
# Available At http://hg.logilab.org/users/dlaxalde/hg
#              hg pull http://hg.logilab.org/users/dlaxalde/hg -r 9274bdbba0f9
# EXP-Topic followlines-cli
revset: extract a parsefollowlinespattern helper function

We'll need the same logic in forthcoming changeset to handle --line-range
option in 'hg log' command.
The function lives in scmutil.py (rather than util.py) as it uses match and
pathutil modules.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -970,16 +970,9 @@  def followlines(repo, subset, x):
         rev = revs.last()
 
     pat = getstring(args['file'], _("followlines requires a pattern"))
-    if not matchmod.patkind(pat):
-        fname = pathutil.canonpath(repo.root, repo.getcwd(), pat)
-    else:
-        m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[rev])
-        files = [f for f in repo[rev] if m(f)]
-        if len(files) != 1:
-            # i18n: "followlines" is a keyword
-            raise error.ParseError(_("followlines expects exactly one file"))
-        fname = files[0]
-
+    # i18n: "followlines" is a keyword
+    msg = _("followlines expects exactly one file")
+    fname = scmutil.parsefollowlinespattern(repo, rev, pat, msg)
     # i18n: "followlines" is a keyword
     lr = getrange(args['lines'][0], _("followlines expects a line range"))
     fromline, toline = [getinteger(a, _("line range bounds must be integers"))
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -564,6 +564,20 @@  def matchfiles(repo, files, badfn=None):
     '''Return a matcher that will efficiently match exactly these files.'''
     return matchmod.exact(repo.root, repo.getcwd(), files, badfn=badfn)
 
+def parsefollowlinespattern(repo, rev, pat, msg):
+    """Return a file name from `pat` pattern suitable for usage in followlines
+    logic.
+    """
+    if not matchmod.patkind(pat):
+        return pathutil.canonpath(repo.root, repo.getcwd(), pat)
+    else:
+        ctx = repo[rev]
+        m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=ctx)
+        files = [f for f in ctx if m(f)]
+        if len(files) != 1:
+            raise error.ParseError(msg)
+        return files[0]
+
 def origpath(ui, repo, filepath):
     '''customize where .orig files are created