# HG changeset patch
# User Angel Ezquerra <angel.ezquerra@gmail.com>
# Date 2013-08-12 23:38:30
# Node ID 20096384754f8f39042dcc06874b465057c7834e
# Parent  cc338115d3b2152c8ebb42146f5b72b2cda75a1a

mq: update subrepos when applying / unapplying patches that change .hgsubstate

Up until now applying or unapplying a patch that modified .hgsubstate would not
work as expected because it would not update the subrepos according to the
.hgsubstate change. This made it very easy to lose subrepo changes when using
mq.

This revision also changes the test-mq-subrepo test so that on the qpop / qpush
tests. We no longer use the debugsub command to check the state of the subrepos
after the qpop and qpush operations. Instead we directly run the id command on
the subrepos that we want to check. The reason is that using the debugsub
command is misleading because it does not really check the state of the subrepos
on the working directory (it just returns what the change that is specified on a
given revision). Because of this the tests did not detect the problem that this
revision fixes (i.e. that applying a patch did not update the subrepos to the
corresponding revisions).


# HG changeset patch
# User Angel Ezquerra <angel.ezquerra@gmail.com>
# Date 1376350710 -7200
#      Tue Aug 13 01:38:30 2013 +0200
# Node ID 60897e264858cdcd46f89e27a702086f08adca02
# Parent  2defb5453f223c3027eb2f7788fbddd52bbb3352
mq: update subrepos when applying / unapplying patches that change .hgsubstate

Up until now applying or unapplying a patch that modified .hgsubstate would not
work as expected because it would not update the subrepos according to the
.hgsubstate change. This made it very easy to lose subrepo changes when using
mq.

This revision also changes the test-mq-subrepo test so that on the qpop / qpush
tests. We no longer use the debugsub command to check the state of the subrepos
after the qpop and qpush operations. Instead we directly run the id command on
the subrepos that we want to check. The reason is that using the debugsub
command is misleading because it does not really check the state of the subrepos
on the working directory (it just returns what the change that is specified on a
given revision). Because of this the tests did not detect the problem that this
revision fixes (i.e. that applying a patch did not update the subrepos to the
corresponding revisions).

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -66,6 +66,7 @@ from mercurial import commands, cmdutil,
 from mercurial import repair, extensions, error, phases
 from mercurial import patch as patchmod
 from mercurial import localrepo
+from mercurial import subrepo
 import os, re, errno, shutil
 
 commands.norepo += " qclone"
@@ -800,6 +801,14 @@ class queue(object):
                 p1, p2 = repo.dirstate.parents()
                 repo.setparents(p1, merge)
 
+            if all_files and '.hgsubstate' in all_files:
+                wctx = repo['.']
+                mctx = actx = repo[None]
+                overwrite = False
+                mergedsubstate = subrepo.submerge(repo, wctx, mctx, actx,
+                    overwrite)
+                files += mergedsubstate.keys()
+
             match = scmutil.matchfiles(repo, files or [])
             oldtip = repo['tip']
             n = newcommit(repo, None, message, ph.user, ph.date, match=match,
@@ -1459,6 +1468,8 @@ class queue(object):
                 self.ui.status(_("popping %s\n") % patch.name)
             del self.applied[start:end]
             self.strip(repo, [rev], update=False, backup='strip')
+            for s, state in repo['.'].substate.items():
+                repo['.'].sub(s).get(state)
             if self.applied:
                 self.ui.write(_("now at: %s\n") % self.applied[-1].name)
             else:
diff --git a/tests/test-mq-subrepo.t b/tests/test-mq-subrepo.t
--- a/tests/test-mq-subrepo.t
+++ b/tests/test-mq-subrepo.t
@@ -230,6 +230,8 @@ handle subrepos safely on qpush/qpop
   $ hg -R sub commit -m foo
   $ hg commit -m1
   $ hg qimport -r "0:tip"
+  $ hg -R sub id --id
+  aa037b301eba
 
 qpop
   $ hg -R sub update 0000
@@ -244,13 +246,11 @@ qpop
   popping 1.diff
   now at: 0.diff
   $ hg status -AS
-  M sub/a
   C .hgsub
   C .hgsubstate
-  $ hg debugsub
-  path sub
-   source   sub
-   revision b2fdb12cd82b021c3b7053d67802e77b6eeaee31
+  C sub/a
+  $ hg -R sub id --id
+  b2fdb12cd82b
 
 qpush
   $ hg -R sub update 0000
@@ -263,15 +263,14 @@ qpush
   adding sub/a
   $ hg qpush
   applying 1.diff
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   now at: 1.diff
   $ hg status -AS
-  M .hgsubstate
   C .hgsub
+  C .hgsubstate
   C sub/a
-  $ hg debugsub
-  path sub
-   source   sub
-   revision aa037b301eba54f350c75951b5486727fb98cbb5
+  $ hg -R sub id --id
+  aa037b301eba
 
   $ cd ..