sparse: move config file writing into core
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 06 Jul 2017 12:24:55 -0700
changeset 33303 8b571495d811
parent 33302 36a415b5a4b2
child 33304 3e1accab7447
sparse: move config file writing into core The code was refactored during the move to be more procedural instead of using string formatting. This has the benefit of not writing empty sections, which changed tests.
hgext/sparse.py
mercurial/sparse.py
tests/test-sparse.t
--- a/hgext/sparse.py	Thu Jul 06 12:20:53 2017 -0700
+++ b/hgext/sparse.py	Thu Jul 06 12:24:55 2017 -0700
@@ -510,14 +510,6 @@
 
             return result
 
-        def writesparseconfig(self, include, exclude, profiles):
-            raw = '%s[include]\n%s\n[exclude]\n%s\n' % (
-                ''.join(['%%include %s\n' % p for p in sorted(profiles)]),
-                '\n'.join(sorted(include)),
-                '\n'.join(sorted(exclude)))
-            self.vfs.write("sparse", raw)
-            sparse.invalidatesignaturecache(self)
-
         def addtemporaryincludes(self, files):
             includes = self.gettemporaryincludes()
             for file in files:
@@ -722,7 +714,8 @@
                 newinclude.difference_update(pats)
                 newexclude.difference_update(pats)
 
-            repo.writesparseconfig(newinclude, newexclude, newprofiles)
+            sparse.writeconfig(repo, newinclude, newexclude, newprofiles)
+
             fcounts = map(
                 len, _refresh(ui, repo, oldstatus, oldsparsematch, force))
 
@@ -735,7 +728,7 @@
             _verbose_output(
                 ui, opts, profilecount, includecount, excludecount, *fcounts)
         except Exception:
-            repo.writesparseconfig(oldinclude, oldexclude, oldprofiles)
+            sparse.writeconfig(repo, oldinclude, oldexclude, oldprofiles)
             raise
     finally:
         wlock.release()
@@ -784,13 +777,13 @@
 
             oldstatus = repo.status()
             oldsparsematch = repo.sparsematch()
-            repo.writesparseconfig(includes, excludes, profiles)
+            sparse.writeconfig(repo, includes, excludes, profiles)
 
             try:
                 fcounts = map(
                     len, _refresh(ui, repo, oldstatus, oldsparsematch, force))
             except Exception:
-                repo.writesparseconfig(oincludes, oexcludes, oprofiles)
+                sparse.writeconfig(repo, oincludes, oexcludes, oprofiles)
                 raise
 
         _verbose_output(ui, opts, profilecount, includecount, excludecount,
@@ -804,7 +797,7 @@
         if includes or excludes:
             oldstatus = repo.status()
             oldsparsematch = repo.sparsematch()
-            repo.writesparseconfig(set(), set(), profiles)
+            sparse.writeconfig(repo, set(), set(), profiles)
             _refresh(ui, repo, oldstatus, oldsparsematch, force)
 
 def _refresh(ui, repo, origstatus, origsparsematch, force):
--- a/mercurial/sparse.py	Thu Jul 06 12:20:53 2017 -0700
+++ b/mercurial/sparse.py	Thu Jul 06 12:24:55 2017 -0700
@@ -129,3 +129,23 @@
 
 def invalidatesignaturecache(repo):
     repo._sparsesignaturecache.clear()
+
+def writeconfig(repo, includes, excludes, profiles):
+    """Write the sparse config file given a sparse configuration."""
+    with repo.vfs('sparse', 'wb') as fh:
+        for p in sorted(profiles):
+            fh.write('%%include %s\n' % p)
+
+        if includes:
+            fh.write('[include]\n')
+            for i in sorted(includes):
+                fh.write(i)
+                fh.write('\n')
+
+        if excludes:
+            fh.write('[exclude]\n')
+            for e in sorted(excludes):
+                fh.write(e)
+                fh.write('\n')
+
+    invalidatesignaturecache(repo)
--- a/tests/test-sparse.t	Thu Jul 06 12:20:53 2017 -0700
+++ b/tests/test-sparse.t	Thu Jul 06 12:24:55 2017 -0700
@@ -63,8 +63,6 @@
   $ hg debugsparse
   [include]
   show*
-  [exclude]
-  
   
 Verify update only writes included files
 
@@ -150,8 +148,6 @@
   [1]
 
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -187,8 +183,6 @@
   use 'hg resolve' to retry unresolved file merges or 'hg update -C .' to abandon
   [1]
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -199,8 +193,6 @@
   cleaned up 1 temporarily added file(s) from the sparse checkout
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -209,8 +201,6 @@
 
   $ hg status
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -257,8 +247,6 @@
   $ hg debugsparse
   [include]
   empty
-  [exclude]
-  
   
 
   $ mkdir add
@@ -276,8 +264,6 @@
   [include]
   add
   empty
-  [exclude]
-  
   
   $ hg add -s add/*
   add/foo already tracked!
@@ -288,8 +274,6 @@
   [include]
   add
   empty
-  [exclude]
-  
   
 
   $ cd ..