Patchwork [2,of,9,sparse] sparse: move config file writing into core

login
register
mail settings
Submitter Gregory Szorc
Date July 6, 2017, 9:54 p.m.
Message ID <868fe26df6ac39fddf1d.1499378058@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/22054/
State Accepted
Headers show

Comments

Gregory Szorc - July 6, 2017, 9:54 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1499369095 25200
#      Thu Jul 06 12:24:55 2017 -0700
# Node ID 868fe26df6ac39fddf1decc3ceae5dc0db9248be
# Parent  56de1555b77f5cd553032c54bddb79645316c86d
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.

Patch

diff --git a/hgext/sparse.py b/hgext/sparse.py
--- a/hgext/sparse.py
+++ b/hgext/sparse.py
@@ -510,14 +510,6 @@  def _wraprepo(ui, repo):
 
             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 @@  def _config(ui, repo, pats, opts, includ
                 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 @@  def _config(ui, repo, pats, opts, includ
             _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 @@  def _import(ui, repo, files, opts, force
 
             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 @@  def _clear(ui, repo, files, force=False)
         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):
diff --git a/mercurial/sparse.py b/mercurial/sparse.py
--- a/mercurial/sparse.py
+++ b/mercurial/sparse.py
@@ -129,3 +129,23 @@  def activeprofiles(repo):
 
 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)
diff --git a/tests/test-sparse.t b/tests/test-sparse.t
--- a/tests/test-sparse.t
+++ b/tests/test-sparse.t
@@ -63,8 +63,6 @@  Verify 'hg debugsparse' default output
   $ hg debugsparse
   [include]
   show*
-  [exclude]
-  
   
 Verify update only writes included files
 
@@ -150,8 +148,6 @@  Verify rebase temporarily includes exclu
   [1]
 
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -187,8 +183,6 @@  Verify merge fails if merging excluded f
   use 'hg resolve' to retry unresolved file merges or 'hg update -C .' to abandon
   [1]
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -199,8 +193,6 @@  Verify merge fails if merging excluded f
   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 @@  Verify strip -k resets dirstate correctl
 
   $ hg status
   $ hg debugsparse
-  [include]
-  
   [exclude]
   hide*
   
@@ -257,8 +247,6 @@  Test that add -s adds dirs to sparse pro
   $ hg debugsparse
   [include]
   empty
-  [exclude]
-  
   
 
   $ mkdir add
@@ -276,8 +264,6 @@  Test that add -s adds dirs to sparse pro
   [include]
   add
   empty
-  [exclude]
-  
   
   $ hg add -s add/*
   add/foo already tracked!
@@ -288,8 +274,6 @@  Test that add -s adds dirs to sparse pro
   [include]
   add
   empty
-  [exclude]
-  
   
 
   $ cd ..