diff mercurial/commands.py @ 9662:f3d60543924f

walkchangerevs: move 'add' to callback Now walkchangerevs is a simple iterator over contexts
author Matt Mackall <mpm@selenic.com>
date Thu, 29 Oct 2009 17:07:51 -0500
parents 2ae3758526d8
children 4164a17e7126
line wrap: on
line diff
--- a/mercurial/commands.py	Wed Oct 28 23:59:18 2009 +0900
+++ b/mercurial/commands.py	Thu Oct 29 17:07:51 2009 -0500
@@ -1302,61 +1302,62 @@
     matchfn = cmdutil.match(repo, pats, opts)
     found = False
     follow = opts.get('follow')
-    for st, ctx, fns in cmdutil.walkchangerevs(ui, repo, matchfn, opts):
-        if st == 'add':
-            rev = ctx.rev()
-            pctx = ctx.parents()[0]
-            parent = pctx.rev()
-            matches.setdefault(rev, {})
-            matches.setdefault(parent, {})
-            files = revfiles.setdefault(rev, [])
-            for fn in fns:
-                flog = getfile(fn)
+
+    def prep(ctx, fns):
+        rev = ctx.rev()
+        pctx = ctx.parents()[0]
+        parent = pctx.rev()
+        matches.setdefault(rev, {})
+        matches.setdefault(parent, {})
+        files = revfiles.setdefault(rev, [])
+        for fn in fns:
+            flog = getfile(fn)
+            try:
+                fnode = ctx.filenode(fn)
+            except error.LookupError:
+                continue
+
+            copied = flog.renamed(fnode)
+            copy = follow and copied and copied[0]
+            if copy:
+                copies.setdefault(rev, {})[fn] = copy
+            if fn in skip:
+                if copy:
+                    skip[copy] = True
+                continue
+            files.append(fn)
+
+            if fn not in matches[rev]:
+                grepbody(fn, rev, flog.read(fnode))
+
+            pfn = copy or fn
+            if pfn not in matches[parent]:
                 try:
-                    fnode = ctx.filenode(fn)
+                    fnode = pctx.filenode(pfn)
+                    grepbody(pfn, parent, flog.read(fnode))
                 except error.LookupError:
-                    continue
-
-                copied = flog.renamed(fnode)
-                copy = follow and copied and copied[0]
+                    pass
+
+    for ctx in cmdutil.walkchangerevs(ui, repo, matchfn, opts, prep):
+        rev = ctx.rev()
+        parent = ctx.parents()[0].rev()
+        for fn in sorted(revfiles.get(rev, [])):
+            states = matches[rev][fn]
+            copy = copies.get(rev, {}).get(fn)
+            if fn in skip:
                 if copy:
-                    copies.setdefault(rev, {})[fn] = copy
-                if fn in skip:
+                    skip[copy] = True
+                continue
+            pstates = matches.get(parent, {}).get(copy or fn, [])
+            if pstates or states:
+                r = display(fn, ctx, pstates, states)
+                found = found or r
+                if r and not opts.get('all'):
+                    skip[fn] = True
                     if copy:
                         skip[copy] = True
-                    continue
-                files.append(fn)
-
-                if fn not in matches[rev]:
-                    grepbody(fn, rev, flog.read(fnode))
-
-                pfn = copy or fn
-                if pfn not in matches[parent]:
-                    try:
-                        fnode = pctx.filenode(pfn)
-                        grepbody(pfn, parent, flog.read(fnode))
-                    except error.LookupError:
-                        pass
-        elif st == 'iter':
-            rev = ctx.rev()
-            parent = ctx.parents()[0].rev()
-            for fn in sorted(revfiles.get(rev, [])):
-                states = matches[rev][fn]
-                copy = copies.get(rev, {}).get(fn)
-                if fn in skip:
-                    if copy:
-                        skip[copy] = True
-                    continue
-                pstates = matches.get(parent, {}).get(copy or fn, [])
-                if pstates or states:
-                    r = display(fn, ctx, pstates, states)
-                    found = found or r
-                    if r and not opts.get('all'):
-                        skip[fn] = True
-                        if copy:
-                            skip[copy] = True
-            del matches[rev]
-            del revfiles[rev]
+        del matches[rev]
+        del revfiles[rev]
 
 def heads(ui, repo, *branchrevs, **opts):
     """show current repository heads or show branch heads
@@ -2037,50 +2038,46 @@
     only_branches = opts.get('only_branch')
 
     displayer = cmdutil.show_changeset(ui, repo, opts, True, matchfn)
-    for st, ctx, fns in cmdutil.walkchangerevs(ui, repo, matchfn, opts):
+    def prep(ctx, fns):
         rev = ctx.rev()
-        if st == 'add':
-            parents = [p for p in repo.changelog.parentrevs(rev)
-                       if p != nullrev]
-            if opts.get('no_merges') and len(parents) == 2:
-                continue
-            if opts.get('only_merges') and len(parents) != 2:
-                continue
-
-            if only_branches and ctx.branch() not in only_branches:
-                continue
-
-            if df and not df(ctx.date()[0]):
-                continue
-
-            if opts.get('keyword'):
-                miss = 0
-                for k in [kw.lower() for kw in opts['keyword']]:
-                    if not (k in ctx.user().lower() or
-                            k in ctx.description().lower() or
-                            k in " ".join(ctx.files()).lower()):
-                        miss = 1
-                        break
-                if miss:
-                    continue
-
-            if opts['user']:
-                if not [k for k in opts['user'] if k in ctx.user()]:
-                    continue
-
-            copies = []
-            if opts.get('copies') and rev:
-                for fn in ctx.files():
-                    rename = getrenamed(fn, rev)
-                    if rename:
-                        copies.append((fn, rename[0]))
-
-            displayer.show(ctx, copies=copies)
-
-        elif st == 'iter':
-            if count == limit: break
-
-            if displayer.flush(rev):
+        parents = [p for p in repo.changelog.parentrevs(rev)
+                   if p != nullrev]
+        if opts.get('no_merges') and len(parents) == 2:
+            return
+        if opts.get('only_merges') and len(parents) != 2:
+            return
+        if only_branches and ctx.branch() not in only_branches:
+            return
+        if df and not df(ctx.date()[0]):
+            return
+
+        if opts.get('keyword'):
+            miss = 0
+            for k in [kw.lower() for kw in opts['keyword']]:
+                if not (k in ctx.user().lower() or
+                        k in ctx.description().lower() or
+                        k in " ".join(ctx.files()).lower()):
+                    miss = 1
+                    break
+            if miss:
+                return
+
+        if opts['user']:
+            if not [k for k in opts['user'] if k in ctx.user()]:
+                return
+
+        copies = []
+        if opts.get('copies') and rev:
+            for fn in ctx.files():
+                rename = getrenamed(fn, rev)
+                if rename:
+                    copies.append((fn, rename[0]))
+
+        displayer.show(ctx, copies=copies)
+
+    for ctx in cmdutil.walkchangerevs(ui, repo, matchfn, opts, prep):
+        if count != limit:
+            if displayer.flush(ctx.rev()):
                 count += 1
 
 def manifest(ui, repo, node=None, rev=None):