verify: don't init subrepo when missing one is referenced (issue5128) (API) stable
authorMatt Harbison <matt_harbison@yahoo.com>
Wed, 27 Apr 2016 22:45:52 -0400
branchstable
changeset 29021 92d37fb3f1aa
parent 29020 ee2e4a2c3690
child 29022 d78e00e4be7c
verify: don't init subrepo when missing one is referenced (issue5128) (API) Initializing a subrepo when one doesn't exist is the right thing to do when the parent is being updated, but in few other cases. Unfortunately, there isn't enough context in the subrepo module to distinguish this case. This same issue can be caused with other subrepo aware commands, so there is a general issue here beyond the scope of this fix. A simpler attempt I tried was to add an '_updating' boolean to localrepo, and set/clear it around the call to mergemod.update() in hg.updaterepo(). That mostly worked, but doesn't handle the case where archive will clone the subrepo if it is missing. (I vaguely recall that there may be other commands that will clone if needed like this, but certainly not all do. It seems both handy, and a bit surprising for what should be a read only operation. It might be nice if all commands did this consistently, but we probably need Angel's subrepo caching first, to not make a mess of the working directory.) I originally handled 'Exception' in order to pick up the Aborts raised in subrepo.state(), but this turns out to be unnecessary because that is called once and cached by ctx.sub() when iterating the subrepos. It was suggested in the bug discussion to skip looking at the subrepo links unless -S is specified. I don't really like that idea because missing a subrepo or (less likely, but worse) a corrupt .hgsubstate is a problem of the parent repo when checking out a revision. The -S option seems like a better fit for functionality that would recurse into each subrepo and do a full verification. Ultimately, the default value for 'allowcreate' should probably be flipped, but since the default behavior was to allow creation, this is less risky for now.
mercurial/context.py
mercurial/hg.py
mercurial/subrepo.py
tests/test-subrepo-missing.t
--- a/mercurial/context.py	Thu Apr 28 08:52:13 2016 -0700
+++ b/mercurial/context.py	Wed Apr 27 22:45:52 2016 -0400
@@ -275,9 +275,9 @@
         except error.LookupError:
             return ''
 
-    def sub(self, path):
+    def sub(self, path, allowcreate=True):
         '''return a subrepo for the stored revision of path, never wdir()'''
-        return subrepo.subrepo(self, path)
+        return subrepo.subrepo(self, path, allowcreate=allowcreate)
 
     def nullsub(self, path, pctx):
         return subrepo.nullsubrepo(self, path, pctx)
--- a/mercurial/hg.py	Thu Apr 28 08:52:13 2016 -0700
+++ b/mercurial/hg.py	Wed Apr 27 22:45:52 2016 -0400
@@ -886,7 +886,11 @@
             ctx = repo[rev]
             try:
                 for subpath in ctx.substate:
-                    ret = ctx.sub(subpath).verify() or ret
+                    try:
+                        ret = (ctx.sub(subpath, allowcreate=False).verify()
+                               or ret)
+                    except error.RepoError as e:
+                        repo.ui.warn(_('%s: %s\n') % (rev, e))
             except Exception:
                 repo.ui.warn(_('.hgsubstate is corrupt in revision %s\n') %
                              node.short(ctx.node()))
--- a/mercurial/subrepo.py	Thu Apr 28 08:52:13 2016 -0700
+++ b/mercurial/subrepo.py	Wed Apr 27 22:45:52 2016 -0400
@@ -340,7 +340,7 @@
                           "in '%s'\n") % vfs.join(dirname))
                 vfs.unlink(vfs.reljoin(dirname, f))
 
-def subrepo(ctx, path, allowwdir=False):
+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
     # because it wants to make repo objects from deep inside the stack
@@ -356,7 +356,7 @@
         raise error.Abort(_('unknown subrepo type %s') % state[2])
     if allowwdir:
         state = (state[0], ctx.subrev(path), state[2])
-    return types[state[2]](ctx, path, state[:2])
+    return types[state[2]](ctx, path, state[:2], allowcreate)
 
 def nullsubrepo(ctx, path, pctx):
     """return an empty subrepo in pctx for the extant subrepo in ctx"""
@@ -375,7 +375,7 @@
     subrev = ''
     if state[2] == 'hg':
         subrev = "0" * 40
-    return types[state[2]](pctx, path, (state[0], subrev))
+    return types[state[2]](pctx, path, (state[0], subrev), True)
 
 def newcommitphase(ui, ctx):
     commitphase = phases.newcommitphase(ui)
@@ -611,12 +611,12 @@
         return self.wvfs.reljoin(reporelpath(self._ctx.repo()), self._path)
 
 class hgsubrepo(abstractsubrepo):
-    def __init__(self, ctx, path, state):
+    def __init__(self, ctx, path, state, allowcreate):
         super(hgsubrepo, self).__init__(ctx, path)
         self._state = state
         r = ctx.repo()
         root = r.wjoin(path)
-        create = not r.wvfs.exists('%s/.hg' % path)
+        create = allowcreate and not r.wvfs.exists('%s/.hg' % path)
         self._repo = hg.repository(r.baseui, root, create=create)
 
         # Propagate the parent's --hidden option
@@ -1064,7 +1064,7 @@
         return reporelpath(self._repo)
 
 class svnsubrepo(abstractsubrepo):
-    def __init__(self, ctx, path, state):
+    def __init__(self, ctx, path, state, allowcreate):
         super(svnsubrepo, self).__init__(ctx, path)
         self._state = state
         self._exe = util.findexe('svn')
@@ -1284,7 +1284,7 @@
 
 
 class gitsubrepo(abstractsubrepo):
-    def __init__(self, ctx, path, state):
+    def __init__(self, ctx, path, state, allowcreate):
         super(gitsubrepo, self).__init__(ctx, path)
         self._state = state
         self._abspath = ctx.repo().wjoin(path)
--- a/tests/test-subrepo-missing.t	Thu Apr 28 08:52:13 2016 -0700
+++ b/tests/test-subrepo-missing.t	Wed Apr 27 22:45:52 2016 -0400
@@ -121,4 +121,22 @@
   subrepo 'subrepo' is hidden in revision 674d05939c1e
   subrepo 'subrepo' not found in revision a7d05d9055a4
 
+verifying shouldn't init a new subrepo if the reference doesn't exist
+
+  $ mv subrepo b
+  $ hg verify
+  checking changesets
+  checking manifests
+  crosschecking files in changesets and manifests
+  checking files
+  2 files, 5 changesets, 5 total revisions
+  checking subrepo links
+  0: repository $TESTTMP/repo/subrepo not found (glob)
+  1: repository $TESTTMP/repo/subrepo not found (glob)
+  3: repository $TESTTMP/repo/subrepo not found (glob)
+  4: repository $TESTTMP/repo/subrepo not found (glob)
+  $ ls
+  b
+  $ mv b subrepo
+
   $ cd ..