Commits

Anonymous committed c005de2

[states] make disabling state saftier

It is not allowed to disable a state that is not empty
because "Explicit is better than implicit".

A hint is displayed to help the user.

Comments (0)

Files changed (2)

                 return 1
             if off:
                 if st in repo._enabledstates:
-                    repo._enabledstates.remove(st)
+                    repo.disablestate(st)
                 else:
                     ui.write_err(_('state %s already deactivated\n') %
                                  state_name)
                         break
             return state
 
-
+        def disablestate(self, state):
+            """disable empty state"""
+            # the lowest is mandatory
+            if state == ST0:
+                raise error.Abort(_('could not disable %s' % state.name))
+            enabled =  self._enabledstates
+            # look up for lower state that is enabled
+            prev = max(st for st in STATES if st < state and st in enabled)
+            if repo.stateheads(state) != repo.stateheads(prev):
+                raise error.Abort(
+                    _('%s could not be disabled: not empty' % state.name),
+                    hint=_("You may want to use `hg %s '%sheads()'`"
+                            % (prev.name, state.name))
+                    )
+            else:
+                enabled.remove(state)
 
         def stateheads(self, state):
             """Return the set of head that define the state"""

tests/test-draft.t

 
 turn draft off again (repo side)
   $ hg states --off draft
+  abort: draft could not be disabled: not empty
+  (You may want to use `hg published 'draftheads()'`)
+  [255]
+  $ hg published tip
   $ hg log --template='{rev}:{node|short}: {state}\n'
   3:73585b17392a: published
   2:3c8695235a32: published
 
 turn draft on again (repo side)
   $ hg states draft
+  $ hg draft --exact 2
 
 test incoming and pull
 
   $ cd ..
   $ "$TESTDIR/killdaemons.py"
   $ hg -R local states --off draft
+  abort: draft could not be disabled: not empty
+  (You may want to use `hg published 'draftheads()'`)
+  [255]
+  $ hg -R local published tip
+  $ hg -R local states --off draft
   $ hg -R local serve -p $HGPORT -d --pid-file=local.pid
   $ cat local.pid >> "$DAEMON_PIDS"
   $ cd fromhttp2
   searching for changes
   0:5caa672bac26
   1:710fe444b3b0
+  2:3c8695235a32
+  3:73585b17392a
   $ hg push http://localhost:$HGPORT/
   pushing to http://localhost:$HGPORT/
   searching for changes
   remote: adding changesets
   remote: adding manifests
   remote: adding file changes
-  remote: added 2 changesets with 2 changes to 1 files
+  remote: added 4 changesets with 4 changes to 2 files
   $ "$TESTDIR/killdaemons.py"