[PATCH 5 of 5] grep: exit loop immediately, if matching is found in the file for "hg grep -l"

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Sat Jan 18 09:24:02 CST 2014


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1390058568 -32400
#      Sun Jan 19 00:22:48 2014 +0900
# Node ID 6d45a04d72d399f72d925633fcb9c408c93479d7
# Parent  3415a8717a17a5161df851510dbbfdacc3247d3a
grep: exit loop immediately, if matching is found in the file for "hg grep -l"

Before this patch, internal function "display()" of "hg grep" is not
efficient for "-l"/"--files-with-matches", because loop is continued,
even after the first matching is found in the specified file.

This patch exits loop immediately, if matching is found for
"--files-with-matches".

In this case, "before is None" is equal to "opts.get('files_with_matches')".

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3251,10 +3251,7 @@
                 cols.append((ui.shortuser(ctx.user()), 'grep.user'))
             if opts.get('date'):
                 cols.append((datefunc(ctx.date()), 'grep.date'))
-            if opts.get('files_with_matches'):
-                if found:
-                    continue
-            else:
+            if not opts.get('files_with_matches'):
                 before = l.line[:l.colstart]
                 match = l.line[l.colstart:l.colend]
                 after = l.line[l.colend:]
@@ -3272,6 +3269,8 @@
                     ui.write(after)
             ui.write(eol)
             found = True
+            if before is None:
+                break
         return found
 
     skip = {}


More information about the Mercurial-devel mailing list