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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Feb. 15, 2014, 10:59 a.m.
Message ID <7ba0c5670385b7b0d391.1392461977@juju>
Download mbox | patch
Permalink /patch/3672/
State Accepted
Commit fe849868fc5a6978fc69192f5e8c6fb35ff355eb
Headers show

Comments

Katsunori FUJIWARA - Feb. 15, 2014, 10:59 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1392461654 -32400
#      Sat Feb 15 19:54:14 2014 +0900
# Node ID 7ba0c5670385b7b0d391e94cc45ccbc027071021
# Parent  07301d80e333276ad9b1c5da007e0084e745749e
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')".

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3260,10 +3260,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:]
@@ -3281,6 +3278,8 @@ 
                     ui.write(after)
             ui.write(eol)
             found = True
+            if before is None:
+                break
         return found
 
     skip = {}