safeguard: add an option to disable automatic publishing
authorPierre-Yves David <pierre-yves.david@ens-lyon.org>
Tue, 25 Apr 2017 10:56:55 +0200
changeset 2286 a4c5744a7b93
parent 2285 080b9384d741
child 2288 b6ea9049693d
safeguard: add an option to disable automatic publishing Pushing to publishing server by mistake is a bit too common in the current state of evolve. Especially when the lack of good feature branch story make the use of -f a bit too common. So we add a very simple experimental option to allow warning (or abort) when changeset are pushed to a publishing server. This is unlikely to survive as is, but this is useful now.
README
hgext3rd/evolve/__init__.py
hgext3rd/evolve/safeguard.py
tests/test-obsolete-push.t
--- a/README	Thu Apr 20 13:05:45 2017 +0200
+++ b/README	Tue Apr 25 10:56:55 2017 +0200
@@ -112,6 +112,12 @@
 Changelog
 =========
 
+6.1.0 - in progress
+-------------------
+
+ * add a 'experimental.auto-publish' config. Set it so 'warn' to get a warning
+   when a push is publishing some draft changesets and 'abort' to prevent that
+   to happen at all.
 
 6.0.1 -- 2017-04-20
 -------------------
--- a/hgext3rd/evolve/__init__.py	Thu Apr 20 13:05:45 2017 +0200
+++ b/hgext3rd/evolve/__init__.py	Tue Apr 25 10:56:55 2017 +0200
@@ -116,6 +116,7 @@
     obsexchange,
     exthelper,
     metadata,
+    safeguard,
     utility,
 )
 
@@ -147,6 +148,7 @@
 eh.merge(debugcmd.eh)
 eh.merge(obsexchange.eh)
 eh.merge(checkheads.eh)
+eh.merge(safeguard.eh)
 uisetup = eh.final_uisetup
 extsetup = eh.final_extsetup
 reposetup = eh.final_reposetup
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hgext3rd/evolve/safeguard.py	Tue Apr 25 10:56:55 2017 +0200
@@ -0,0 +1,42 @@
+# Code dedicated to adding various "safeguard" around evolution
+#
+# Some of these will be pollished and upstream when mature. Some other will be
+# replaced by better alternative later.
+#
+# Copyright 2017 Pierre-Yves David <pierre-yves.david@ens-lyon.org>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from mercurial import error
+
+from mercurial.i18n import _
+
+from . import exthelper
+
+eh = exthelper.exthelper()
+
+@eh.reposetup
+def setuppublishprevention(ui, repo):
+
+    class noautopublishrepo(repo.__class__):
+
+        def checkpush(self, pushop):
+            super(noautopublishrepo, self).checkpush(pushop)
+            behavior = repo.ui.config('experimental', 'auto-publish', 'default')
+            remotephases = pushop.remote.listkeys('phases')
+            publishing = remotephases.get('publishing', False)
+            if behavior in ('warn', 'abort') and publishing:
+                if pushop.revs is None:
+                    published = repo.filtered('served').revs("not public()")
+                else:
+                    published = repo.revs("::%ln - public()", pushop.revs)
+                if published:
+                    if behavior == 'warn':
+                        repo.ui.warn(_('%i changesets about to be published\n') % len(published))
+                    elif behavior == 'abort':
+                        msg = _('push would publish 1 changesets')
+                        hint = _("behavior controlled by 'experimental.auto-publish' config")
+                        raise error.Abort(msg, hint=hint)
+
+    repo.__class__ = noautopublishrepo
--- a/tests/test-obsolete-push.t	Thu Apr 20 13:05:45 2017 +0200
+++ b/tests/test-obsolete-push.t	Tue Apr 25 10:56:55 2017 +0200
@@ -44,3 +44,50 @@
   comparing with ../clone
   searching for changes
   0:1994f17a630e@default(obsolete/draft) A
+  $ cd ..
+
+Test options to prevent implicite publishing of changesets
+----------------------------------------------------------
+
+
+  $ hg clone source strict-publish-client --pull
+  requesting all changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+  1 new obsolescence markers
+  updating to branch default
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd strict-publish-client
+  $ echo c > c
+  $ hg ci -qAm C c
+
+abort behavior
+
+  $ cat >> .hg/hgrc <<eof
+  > [experimental]
+  > auto-publish = abort
+  > eof
+  $ hg push -r .
+  pushing to $TESTTMP/source
+  abort: push would publish 1 changesets
+  (behavior controlled by 'experimental.auto-publish' config)
+  [255]
+  $ hg push
+  pushing to $TESTTMP/source
+  abort: push would publish 1 changesets
+  (behavior controlled by 'experimental.auto-publish' config)
+  [255]
+
+warning behavior
+
+  $ echo 'auto-publish = warn' >> .hg/hgrc
+  $ hg push
+  pushing to $TESTTMP/source
+  1 changesets about to be published
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 0 changesets with 0 changes to 1 files