Patchwork subrepo: check phase of state in each subrepositories before committing

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Nov. 13, 2013, 7:01 a.m.
Message ID <d810705b781032d37517.1384326063@juju>
Download mbox | patch
Permalink /patch/2935/
State Accepted
Commit 4c96c50ef9372ca8ee496ddf3556ce2c3f7f4062
Headers show

Comments

Katsunori FUJIWARA - Nov. 13, 2013, 7:01 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1384325730 -32400
#      Wed Nov 13 15:55:30 2013 +0900
# Node ID d810705b781032d3751700f0c9893911b8868101
# Parent  c38c3fdc8b9317ba09e03ab09364c3800da7c50c
subrepo: check phase of state in each subrepositories before committing

Before this patch, phase of newly created commit is determined by
"phases.new-commit" configuration regardless of phase of state in each
subrepositories.

For example, this may cause the "public" revision in the parent
repository referring the "secret" one in subrepository.

This patch checks phase of state in each subrepositories before
committing in the parent, and aborts or changes phase of newly created
commit if subrepositories have more restricted phase than the parent.

This patch uses "follow" as default value of "phases.checksubrepos"
configuration, because it can keep consistency between phases of the
parent and subrepositories without breaking existing tool chains.
Matt Mackall - Dec. 16, 2013, 8:27 p.m.
On Wed, 2013-11-13 at 16:01 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1384325730 -32400
> #      Wed Nov 13 15:55:30 2013 +0900
> # Node ID d810705b781032d3751700f0c9893911b8868101
> # Parent  c38c3fdc8b9317ba09e03ab09364c3800da7c50c
> subrepo: check phase of state in each subrepositories before committing

Queued for default after tidying up the help text a bit, thanks.

Patch

diff --git a/mercurial/help/config.txt b/mercurial/help/config.txt
--- a/mercurial/help/config.txt
+++ b/mercurial/help/config.txt
@@ -945,6 +945,16 @@ 
     Phase of newly-created commits.
     Default: draft
 
+``checksubrepos``
+
+    Check phase of state in each subrepositories. "ignore", "follow"
+    or "abort". For other than "ignore", phase of state in each
+    subrepositories are checked, before committing in the parent
+    repository. If there is any grater phase than one of the parent
+    ("secret" against "draft", for example), commit is aborted for
+    "abort", and changes are committed in such grater phase in the
+    parent for "follow". Default: "follow".
+
 ``profiling``
 -------------
 
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1379,7 +1379,7 @@ 
                       parent2=xp2, pending=p)
             self.changelog.finalize(trp)
             # set the new commit is proper phase
-            targetphase = phases.newcommitphase(self.ui)
+            targetphase = subrepo.newcommitphase(self.ui, ctx)
             if targetphase:
                 # retract boundary do not alter parent changeset.
                 # if a parent have higher the resulting phase will
diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -10,6 +10,7 @@ 
 import stat, subprocess, tarfile
 from i18n import _
 import config, scmutil, util, node, error, cmdutil, bookmarks, match as matchmod
+import phases
 hg = None
 propertycache = util.propertycache
 
@@ -338,6 +339,37 @@ 
         raise util.Abort(_('unknown subrepo type %s') % state[2])
     return types[state[2]](ctx, path, state[:2])
 
+def newcommitphase(ui, ctx):
+    commitphase = phases.newcommitphase(ui)
+    substate = getattr(ctx, "substate", None)
+    if not substate:
+        return commitphase
+    check = ui.config('phases', 'checksubrepos', 'follow')
+    if check not in ('ignore', 'follow', 'abort'):
+        raise util.Abort(_('invalid phases.checksubrepos configuration: %s')
+                         % (check))
+    if check == 'ignore':
+        return commitphase
+    maxphase = phases.public
+    maxsub = None
+    for s in sorted(substate):
+        sub = ctx.sub(s)
+        subphase = sub.phase(substate[s][1])
+        if maxphase < subphase:
+            maxphase = subphase
+            maxsub = s
+    if commitphase < maxphase:
+        if check == 'abort':
+            raise util.Abort(_("can't commit in %s phase"
+                               " conflicting %s from subrepository %s") %
+                             (phases.phasenames[commitphase],
+                              phases.phasenames[maxphase], maxsub))
+        ui.warn(_("warning: changes are committed in"
+                  " %s phase from subrepository %s\n") %
+                (phases.phasenames[maxphase], maxsub))
+        return maxphase
+    return commitphase
+
 # subrepo classes need to implement the following abstract class:
 
 class abstractsubrepo(object):
@@ -372,6 +404,11 @@ 
         """
         raise NotImplementedError
 
+    def phase(self, state):
+        """returns phase of specified state in the subrepository.
+        """
+        return phases.public
+
     def remove(self):
         """remove the subrepo
 
@@ -639,6 +676,10 @@ 
         return node.hex(n)
 
     @annotatesubrepoerror
+    def phase(self, state):
+        return self._repo[state].phase()
+
+    @annotatesubrepoerror
     def remove(self):
         # we can't fully delete the repository as it may contain
         # local-only history
diff --git a/tests/test-subrepo.t b/tests/test-subrepo.t
--- a/tests/test-subrepo.t
+++ b/tests/test-subrepo.t
@@ -1232,4 +1232,69 @@ 
   searching for changes
   no changes found
   [1]
+  $ cd ..
 
+Test phase choice for newly created commit with "phases.subrepochecks"
+configuration
+
+  $ cd t
+  $ hg update -q -r 12
+
+  $ cat >> s/ss/.hg/hgrc <<EOF
+  > [phases]
+  > new-commit = secret
+  > EOF
+  $ cat >> s/.hg/hgrc <<EOF
+  > [phases]
+  > new-commit = draft
+  > EOF
+  $ echo phasecheck1 >> s/ss/a
+  $ hg -R s commit -S --config phases.checksubrepos=abort -m phasecheck1
+  committing subrepository ss
+  transaction abort!
+  rollback completed
+  abort: can't commit in draft phase conflicting secret from subrepository ss
+  [255]
+  $ echo phasecheck2 >> s/ss/a
+  $ hg -R s commit -S --config phases.checksubrepos=ignore -m phasecheck2
+  committing subrepository ss
+  $ hg -R s/ss phase tip
+  3: secret
+  $ hg -R s phase tip
+  6: draft
+  $ echo phasecheck3 >> s/ss/a
+  $ hg -R s commit -S -m phasecheck3
+  committing subrepository ss
+  warning: changes are committed in secret phase from subrepository ss
+  $ hg -R s/ss phase tip
+  4: secret
+  $ hg -R s phase tip
+  7: secret
+
+  $ cat >> t/.hg/hgrc <<EOF
+  > [phases]
+  > new-commit = draft
+  > EOF
+  $ cat >> .hg/hgrc <<EOF
+  > [phases]
+  > new-commit = public
+  > EOF
+  $ echo phasecheck4 >>   s/ss/a
+  $ echo phasecheck4 >>   t/t
+  $ hg commit -S -m phasecheck4
+  committing subrepository s
+  committing subrepository s/ss
+  warning: changes are committed in secret phase from subrepository ss
+  committing subrepository t
+  warning: changes are committed in secret phase from subrepository s
+  created new head
+  $ hg -R s/ss phase tip
+  5: secret
+  $ hg -R s phase tip
+  8: secret
+  $ hg -R t phase tip
+  6: draft
+  $ hg phase tip
+  15: secret
+
+  $ cd ..