changeset 3951:cb66641cdee3

grep: remove count handling, simplify, fix issue337
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Fri, 22 Dec 2006 22:51:39 +0100
parents 3d3007064a17
children 32c1653b7dad
files mercurial/commands.py tests/test-grep tests/test-grep.out
diffstat 3 files changed, 38 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Fri Dec 22 22:16:05 2006 +0100
+++ b/mercurial/commands.py	Fri Dec 22 22:51:39 2006 +0100
@@ -1149,13 +1149,14 @@
 
     prev = {}
     def display(fn, rev, states, prevstates):
-        counts = {'-': 0, '+': 0}
+        found = False
         filerevmatches = {}
-        if not opts['all']:
-            a, b, r = prevstates, states, rev
+        r = prev.get(fn, -1)
+        if opts['all']:
+            iter = difflinestates(states, prevstates)
         else:
-            a, b, r = states, prevstates, prev.get(fn, -1)
-        for change, l in difflinestates(a, b):
+            iter = [('', l) for l in prevstates]
+        for change, l in iter:
             cols = [fn, str(r)]
             if opts['line_number']:
                 cols.append(str(l.linenum))
@@ -1171,14 +1172,14 @@
             else:
                 cols.append(l.line)
             ui.write(sep.join(cols), eol)
-            counts[change] += 1
-        return counts['+'], counts['-']
+            found = True
+        return found
 
     fstate = {}
     skip = {}
     get = util.cachefunc(lambda r: repo.changectx(r).changeset())
     changeiter, matchfn = cmdutil.walkchangerevs(ui, repo, pats, get, opts)
-    count = 0
+    found = False
     follow = opts.get('follow')
     for st, rev, fns in changeiter:
         if st == 'window':
@@ -1207,10 +1208,10 @@
                     if copy:
                         skip[copy] = True
                     continue
-                if not opts['all'] or fstate[fn]:
-                    pos, neg = display(fn, rev, m, fstate[fn])
-                    count += pos + neg
-                    if pos and not opts['all']:
+                if fn in prev or fstate[fn]:
+                    r = display(fn, rev, m, fstate[fn])
+                    found = found or r
+                    if r and not opts['all']:
                         skip[fn] = True
                         if copy:
                             skip[copy] = True
@@ -1225,8 +1226,8 @@
         if fn in skip:
             continue
         if fn not in copies.get(prev[fn], {}):
-            display(fn, rev, {}, state)
-    return (count == 0 and 1) or 0
+            found = display(fn, rev, {}, state) or found
+    return (not found and 1) or 0
 
 def heads(ui, repo, **opts):
     """show current repository heads
--- a/tests/test-grep	Fri Dec 22 22:16:05 2006 +0100
+++ b/tests/test-grep	Fri Dec 22 22:51:39 2006 +0100
@@ -31,3 +31,22 @@
 echo deport >> port2
 hg commit -m 5 -u eggs -d '6 0'
 hg grep -f --all -nu port port2
+
+cd ..
+hg init t2
+cd t2
+hg grep foobar foo
+hg grep foobar
+echo blue >> color
+echo black >> color
+hg add color
+hg ci -m 0 -d '0 0'
+echo orange >> color
+hg ci -m 1 -d '0 0'
+echo black > color
+hg ci -m 2 -d '0 0'
+echo orange >> color
+echo blue >> color
+hg ci -m 3 -d '0 0'
+hg grep orange
+hg grep --all orange
--- a/tests/test-grep.out	Fri Dec 22 22:16:05 2006 +0100
+++ b/tests/test-grep.out	Fri Dec 22 22:51:39 2006 +0100
@@ -26,3 +26,7 @@
 port:2:3:+:spam:import/export
 port:1:2:+:eggs:export
 port:0:1:+:spam:import
+color:3:orange
+color:3:+:orange
+color:2:-:orange
+color:1:+:orange