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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Jan. 18, 2014, 3:24 p.m.
Message ID <6d45a04d72d399f72d92.1390058642@vm-debian.dzone.foozy.private>
Download mbox | patch
Permalink /patch/3384/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - Jan. 18, 2014, 3:24 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@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')".

Patch

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 = {}