Patchwork [5,of,8] revlog: add revlog.empty()

login
register
mail settings
Submitter Durham Goode
Date May 31, 2013, 5:19 p.m.
Message ID <37611b85f4af013a0e66.1370020787@dev350.prn1.facebook.com>
Download mbox | patch
Permalink /patch/1688/
State Rejected, archived
Headers show

Comments

Durham Goode - May 31, 2013, 5:19 p.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1369964836 25200
#      Thu May 30 18:47:16 2013 -0700
# Node ID 37611b85f4af013a0e66aad75e8578cdf3247c79
# Parent  036972b09c16295c000847ba359193858e7b3a4d
revlog: add revlog.empty()

A few places in the code use 'if not len(revlog)' to check if the revlog
exists. This adds an explicit method for that so alternative revlog
implementations can answer that question without having to implement
__len__. Updates two locations to use the new function.
Matt Mackall - June 3, 2013, 8:27 p.m.
On Fri, 2013-05-31 at 10:19 -0700, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1369964836 25200
> #      Thu May 30 18:47:16 2013 -0700
> # Node ID 37611b85f4af013a0e66aad75e8578cdf3247c79
> # Parent  036972b09c16295c000847ba359193858e7b3a4d
> revlog: add revlog.empty()

A more Pythonic way to do this would be __nonzero__().

object.__nonzero__(self)
        Called to implement truth value testing and the built-in
        operation bool(); should return False or True, or their integer
        equivalents 0 or 1. When this method is not defined, __len__()
        is called, if it is defined, and the object is considered true
        if its result is nonzero. If a class defines neither __len__()
        nor __nonzero__(), all its instances are considered true.
        
This is part of the reason we're currently using __len__, actually.

Patch

diff --git a/mercurial/changegroup.py b/mercurial/changegroup.py
--- a/mercurial/changegroup.py
+++ b/mercurial/changegroup.py
@@ -359,7 +359,7 @@ 
         msgfiles = _('files')
         for i, fname in enumerate(sorted(changedfiles)):
             filerevlog = repo.file(fname)
-            if not len(filerevlog):
+            if filerevlog.empty():
                 raise util.Abort(_("empty or missing revlog for %s") % fname)
 
             if fastpathlinkrev:
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1320,7 +1320,7 @@ 
                 raise util.Abort(_('cannot follow file not in parent '
                                    'revision: "%s"') % f)
             filelog = repo.file(f)
-            if not len(filelog):
+            if filelog.empty():
                 # A zero count may be a directory or deleted file, so
                 # try to find matching entries on the slow path.
                 if follow:
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -261,6 +261,8 @@ 
         return self.node(len(self.index) - 2)
     def __len__(self):
         return len(self.index) - 1
+    def empty(self):
+        return len(self) == 0
     def __iter__(self):
         return iter(xrange(len(self)))
     def revs(self, start=0, stop=None):