[PATCH 08 of 13 sparse] sparse: use vfs.tryread()

Gregory Szorc gregory.szorc at gmail.com
Sat Jul 1 21:55:25 EDT 2017


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1498935753 25200
#      Sat Jul 01 12:02:33 2017 -0700
# Node ID 6636a2dfa31cb84684373bf3a9fdb03c26e42f99
# Parent  fc1292b23044d76d25f8a88ff819abf5ac0ed6cf
sparse: use vfs.tryread()

vfs.exists() followed by a file read is an anti-pattern because it
incurs an extra stat() to test for file presence. vfs.tryread()
returns empty string on missing file and avoids the stat().

diff --git a/hgext/sparse.py b/hgext/sparse.py
--- a/hgext/sparse.py
+++ b/hgext/sparse.py
@@ -445,13 +445,13 @@ def _wraprepo(ui, repo):
             """Returns the include/exclude patterns specified by the
             given rev.
             """
-            if not self.vfs.exists('sparse'):
+            raw = self.vfs.tryread('sparse')
+            if not raw:
                 return set(), set(), []
             if rev is None:
                 raise error.Abort(_("cannot parse sparse patterns from " +
                     "working copy"))
 
-            raw = self.vfs.read('sparse')
             includes, excludes, profiles = self.readsparseconfig(raw)
 
             ctx = self[rev]
@@ -621,8 +621,8 @@ def _wraprepo(ui, repo):
 
         def gettemporaryincludes(self):
             existingtemp = set()
-            if self.vfs.exists('tempsparse'):
-                raw = self.vfs.read('tempsparse')
+            raw = self.vfs.tryread('tempsparse')
+            if raw:
                 existingtemp.update(raw.split('\n'))
             return existingtemp
 
@@ -782,8 +782,8 @@ def _config(ui, repo, pats, opts, includ
     try:
         oldsparsematch = repo.sparsematch()
 
-        if repo.vfs.exists('sparse'):
-            raw = repo.vfs.read('sparse')
+        raw = repo.vfs.tryread('sparse')
+        if raw:
             oldinclude, oldexclude, oldprofiles = map(
                 set, repo.readsparseconfig(raw))
         else:
@@ -843,9 +843,7 @@ def _import(ui, repo, files, opts, force
                 repo.dirstate.parents() if node != nullid]
 
         # read current configuration
-        raw = ''
-        if repo.vfs.exists('sparse'):
-            raw = repo.vfs.read('sparse')
+        raw = repo.vfs.tryread('sparse')
         oincludes, oexcludes, oprofiles = repo.readsparseconfig(raw)
         includes, excludes, profiles = map(
                 set, (oincludes, oexcludes, oprofiles))
@@ -896,9 +894,7 @@ def _import(ui, repo, files, opts, force
 
 def _clear(ui, repo, files, force=False):
     with repo.wlock():
-        raw = ''
-        if repo.vfs.exists('sparse'):
-            raw = repo.vfs.read('sparse')
+        raw = repo.vfs.tryread('sparse')
         includes, excludes, profiles = repo.readsparseconfig(raw)
 
         if includes or excludes:


More information about the Mercurial-devel mailing list