Patchwork walkchangerevs: make followfilter a top-level class

login
register
mail settings
Submitter Martin von Zweigbergk
Date March 19, 2015, 4:18 p.m.
Message ID <48c9721bf4c7c54c752c.1426781929@martinvonz.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/8163/
State Accepted
Commit 6c3a93e690c782c0b5658c7bce85d0f585231e18
Headers show

Comments

Martin von Zweigbergk - March 19, 2015, 4:18 p.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1426781541 25200
#      Thu Mar 19 09:12:21 2015 -0700
# Node ID 48c9721bf4c7c54c752c186668865f2d7195969f
# Parent  5cb459dc32d209653a3e5d77749cf989ab9a51e4
walkchangerevs: make followfilter a top-level class

The class only depends on the 'repo' variable in the closure, so let's
move the class out of the function and make it explicit that that (the
repo) is all it needs.
Matt Mackall - March 19, 2015, 7:26 p.m.
On Thu, 2015-03-19 at 09:18 -0700, Martin von Zweigbergk wrote:
> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz@google.com>
> # Date 1426781541 25200
> #      Thu Mar 19 09:12:21 2015 -0700
> # Node ID 48c9721bf4c7c54c752c186668865f2d7195969f
> # Parent  5cb459dc32d209653a3e5d77749cf989ab9a51e4
> walkchangerevs: make followfilter a top-level class

Sure, queued for default, thanks.

Patch

diff -r 5cb459dc32d2 -r 48c9721bf4c7 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Tue Oct 28 22:32:18 2014 -0700
+++ b/mercurial/cmdutil.py	Thu Mar 19 09:12:21 2015 -0700
@@ -1669,6 +1669,44 @@ 
 
     return wanted
 
+class _followfilter(object):
+    def __init__(self, repo, onlyfirst=False):
+        self.repo = repo
+        self.startrev = nullrev
+        self.roots = set()
+        self.onlyfirst = onlyfirst
+
+    def match(self, rev):
+        def realparents(rev):
+            if self.onlyfirst:
+                return self.repo.changelog.parentrevs(rev)[0:1]
+            else:
+                return filter(lambda x: x != nullrev,
+                              self.repo.changelog.parentrevs(rev))
+
+        if self.startrev == nullrev:
+            self.startrev = rev
+            return True
+
+        if rev > self.startrev:
+            # forward: all descendants
+            if not self.roots:
+                self.roots.add(self.startrev)
+            for parent in realparents(rev):
+                if parent in self.roots:
+                    self.roots.add(rev)
+                    return True
+        else:
+            # backwards: all parents
+            if not self.roots:
+                self.roots.update(realparents(self.startrev))
+            if rev in self.roots:
+                self.roots.remove(rev)
+                self.roots.update(realparents(rev))
+                return True
+
+        return False
+
 def walkchangerevs(repo, match, opts, prepare):
     '''Iterate over files and the revs in which they changed.
 
@@ -1757,48 +1795,11 @@ 
 
         wanted = lazywantedset()
 
-    class followfilter(object):
-        def __init__(self, onlyfirst=False):
-            self.startrev = nullrev
-            self.roots = set()
-            self.onlyfirst = onlyfirst
-
-        def match(self, rev):
-            def realparents(rev):
-                if self.onlyfirst:
-                    return repo.changelog.parentrevs(rev)[0:1]
-                else:
-                    return filter(lambda x: x != nullrev,
-                                  repo.changelog.parentrevs(rev))
-
-            if self.startrev == nullrev:
-                self.startrev = rev
-                return True
-
-            if rev > self.startrev:
-                # forward: all descendants
-                if not self.roots:
-                    self.roots.add(self.startrev)
-                for parent in realparents(rev):
-                    if parent in self.roots:
-                        self.roots.add(rev)
-                        return True
-            else:
-                # backwards: all parents
-                if not self.roots:
-                    self.roots.update(realparents(self.startrev))
-                if rev in self.roots:
-                    self.roots.remove(rev)
-                    self.roots.update(realparents(rev))
-                    return True
-
-            return False
-
     # it might be worthwhile to do this in the iterator if the rev range
     # is descending and the prune args are all within that range
     for rev in opts.get('prune', ()):
         rev = repo[rev].rev()
-        ff = followfilter()
+        ff = _followfilter(repo)
         stop = min(revs[0], revs[-1])
         for x in xrange(rev, stop - 1, -1):
             if ff.match(x):
@@ -1808,7 +1809,7 @@ 
     # revision range, yielding only revisions in wanted.
     def iterate():
         if follow and not match.files():
-            ff = followfilter(onlyfirst=opts.get('follow_first'))
+            ff = _followfilter(repo, onlyfirst=opts.get('follow_first'))
             def want(rev):
                 return ff.match(rev) and rev in wanted
         else: