subrepo: add config option to reject any subrepo operations (SEC) stable
authorYuya Nishihara <yuya@tcha.org>
Sun, 05 Nov 2017 21:22:07 +0900
branchstable
changeset 34985 5e27afeddaee
parent 34984 071cbeba4212
child 34986 828cf35f1de6
subrepo: add config option to reject any subrepo operations (SEC) This is an alternative workaround for the issue5730. Perhaps this is the simplest way of disabling subrepo operations. It does nothing clever, but just aborts if Mercurial starts accessing to a subrepo. I think Greg's patch is more useful since it allows us to at least check out the parent repository. However, that would be confusing if the default is flipped to checkout=False and subrepos are silently ignored. I don't like the config name 'allowed', but I couldn't get any better name.
mercurial/configitems.py
mercurial/help/config.txt
mercurial/subrepo.py
tests/test-subrepo-git.t
tests/test-subrepo.t
--- a/mercurial/configitems.py	Fri Nov 03 20:12:50 2017 +0900
+++ b/mercurial/configitems.py	Sun Nov 05 21:22:07 2017 +0900
@@ -811,6 +811,9 @@
 coreconfigitem('sparse', 'missingwarning',
     default=True,
 )
+coreconfigitem('subrepos', 'allowed',
+    default=dynamicdefault,  # to make backporting simpler
+)
 coreconfigitem('templates', '.*',
     default=None,
     generic=True,
--- a/mercurial/help/config.txt	Fri Nov 03 20:12:50 2017 +0900
+++ b/mercurial/help/config.txt	Sun Nov 05 21:22:07 2017 +0900
@@ -1893,6 +1893,19 @@
 doesn't match the full path, an attempt is made to apply it on the
 relative path alone. The rules are applied in definition order.
 
+``subrepos``
+------------
+
+This section contains options that control the behavior of the
+subrepositories feature. See also :hg:`help subrepos`.
+
+``allowed``
+    Whether subrepository operation in the working directory is allowed.
+
+    When disabled, any commands including :hg:`update` will fail if
+    subrepositories are involved.
+    (default: True)
+
 ``templatealias``
 -----------------
 
--- a/mercurial/subrepo.py	Fri Nov 03 20:12:50 2017 +0900
+++ b/mercurial/subrepo.py	Sun Nov 05 21:22:07 2017 +0900
@@ -365,6 +365,13 @@
     if repo.wvfs.islink(path):
         raise error.Abort(_("subrepo '%s' traverses symbolic link") % path)
 
+def _checktype(ui, kind):
+    if not ui.configbool('subrepos', 'allowed', True):
+        raise error.Abort(_("subrepo not allowed"),
+                          hint=_("see 'hg help config.subrepos' for details"))
+    if kind not in types:
+        raise error.Abort(_('unknown subrepo type %s') % kind)
+
 def subrepo(ctx, path, allowwdir=False, allowcreate=True):
     """return instance of the right subrepo class for subrepo in path"""
     # subrepo inherently violates our import layering rules
@@ -375,10 +382,10 @@
     from . import hg as h
     hg = h
 
-    _auditsubrepopath(ctx.repo(), path)
+    repo = ctx.repo()
+    _auditsubrepopath(repo, path)
     state = ctx.substate[path]
-    if state[2] not in types:
-        raise error.Abort(_('unknown subrepo type %s') % state[2])
+    _checktype(repo.ui, state[2])
     if allowwdir:
         state = (state[0], ctx.subrev(path), state[2])
     return types[state[2]](ctx, path, state[:2], allowcreate)
@@ -393,10 +400,10 @@
     from . import hg as h
     hg = h
 
-    _auditsubrepopath(ctx.repo(), path)
+    repo = ctx.repo()
+    _auditsubrepopath(repo, path)
     state = ctx.substate[path]
-    if state[2] not in types:
-        raise error.Abort(_('unknown subrepo type %s') % state[2])
+    _checktype(repo.ui, state[2])
     subrev = ''
     if state[2] == 'hg':
         subrev = "0" * 40
--- a/tests/test-subrepo-git.t	Fri Nov 03 20:12:50 2017 +0900
+++ b/tests/test-subrepo-git.t	Sun Nov 05 21:22:07 2017 +0900
@@ -86,9 +86,29 @@
   path s
    source   ../gitroot
    revision 126f2a14290cd5ce061fdedc430170e8d39e1c5a
+  $ cd ..
+
+clone with subrepo disabled (update should fail)
+
+  $ hg clone t -U tc2 --config subrepos.allowed=false
+  $ hg update -R tc2 --config subrepos.allowed=false
+  abort: subrepo not allowed
+  (see 'hg help config.subrepos' for details)
+  [255]
+  $ ls tc2
+  a
+
+  $ hg clone t tc3 --config subrepos.allowed=false
+  updating to branch default
+  abort: subrepo not allowed
+  (see 'hg help config.subrepos' for details)
+  [255]
+  $ ls tc3
+  a
 
 update to previous substate
 
+  $ cd tc
   $ hg update 1 -q
   $ cat s/g
   g
--- a/tests/test-subrepo.t	Fri Nov 03 20:12:50 2017 +0900
+++ b/tests/test-subrepo.t	Sun Nov 05 21:22:07 2017 +0900
@@ -484,9 +484,29 @@
   path t
    source   t
    revision 20a0db6fbf6c3d2836e6519a642ae929bfc67c0e
+  $ cd ..
+
+clone with subrepo disabled (update should fail)
+
+  $ hg clone t -U tc2 --config subrepos.allowed=false
+  $ hg update -R tc2 --config subrepos.allowed=false
+  abort: subrepo not allowed
+  (see 'hg help config.subrepos' for details)
+  [255]
+  $ ls tc2
+  a
+
+  $ hg clone t tc3 --config subrepos.allowed=false
+  updating to branch default
+  abort: subrepo not allowed
+  (see 'hg help config.subrepos' for details)
+  [255]
+  $ ls tc3
+  a
 
 push
 
+  $ cd tc
   $ echo bah > t/t
   $ hg ci -m11
   committing subrepository t