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

Gregory Szorc gregory.szorc at gmail.com
Thu Jul 6 15:36:34 EDT 2017


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1499363925 25200
#      Thu Jul 06 10:58:45 2017 -0700
# Node ID 18bb106ccbe320d2be62d8c28b8ef756dad9426d
# Parent  c31af7dfe7370ade61a54c2c8191329083b7d7a2
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]
@@ -623,8 +623,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
 
@@ -784,8 +784,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:
@@ -845,9 +845,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))
@@ -898,9 +896,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