[PATCH 5 of 9] grep: correctly handle multiline matches with several expressions

Idan Kamara idankk86 at gmail.com
Sun Oct 14 15:54:22 CDT 2012


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1349693025 -7200
# Node ID a83911a11e922e7b941e1c1dd6c66a6654a6b069
# Parent  06edbeb509881597122b46de831da4332076c4a1
grep: correctly handle multiline matches with several expressions

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2953,6 +2953,17 @@
             lstart = body.rfind('\n', begin, mstart) + 1 or begin
             begin = body.find('\n', mend) + 1 or len(body) + 1
             lend = begin - 1
+            # We need to be careful with multiline regexps. Since we pick
+            # the match that is seen earlier in the file, it's possible
+            # that a multiline regexp spans the same line as the match we
+            # picked but includes the following lines. In that case we'd like
+            # to pick the longer regexp.
+            for m in matches:
+                if m != match:
+                    s, e = m.span()
+                    if lstart <= s <= lend and e > lend:
+                        begin = body.find('\n', e) + 1 or len(body) + 1
+                        lend = begin - 1
             yield linenum, mstart - lstart, mend - lstart, body[lstart:lend]
 
     class linestate(object):
diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -31,6 +31,9 @@
   port:4:export
   port:4:vaportight
   port:4:import/export
+  $ hg grep -e vap -e 'ght\nimp' port
+  port:4:vaportight
+  import/export
   $ hg grep --no-filename port port
   4:export
   4:vaportight


More information about the Mercurial-devel mailing list