[PATCH stable] grep: correct handling of matching lines without line ending (issue3050)

Mads Kiilerich mads at kiilerich.com
Sat Oct 15 18:28:56 CDT 2011


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1318721166 -7200
# Node ID df72f8e7f4af85f9a83762c86ebf8a44b3bf9d52
# Parent  2889d4574726546fefd0c22a5cfd0c23bd311a6c
grep: correct handling of matching lines without line ending (issue3050)

Matching lines without trailing '\n' was missing the last character.

That seems to have been an unintended side effect of 261a9f47b44b.
The test in dac14cc9711e documents the bad behaviour.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2660,7 +2660,7 @@
             mstart, mend = match.span()
             linenum += body.count('\n', begin, mstart) + 1
             lstart = body.rfind('\n', begin, mstart) + 1 or begin
-            begin = body.find('\n', mend) + 1 or len(body)
+            begin = body.find('\n', mend) + 1 or len(body) + 1
             lend = begin - 1
             yield linenum, mstart - lstart, mend - lstart, body[lstart:lend]
 
diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -106,12 +106,8 @@
   $ python -c 'fp = open("noeol", "wb"); fp.write("no infinite loop"); fp.close();'
   $ hg ci -Amnoeol
   adding noeol
-
-last character omitted in output to avoid infinite loop
-
   $ hg grep loop
-  noeol:4:no infinite loo
-
+  noeol:4:no infinite loop
 
   $ cd ..
 


More information about the Mercurial-devel mailing list