hgext3rd/evolve/safeguard.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Tue, 25 Apr 2017 10:56:55 +0200
changeset 2286 a4c5744a7b93
child 3080 461c9d940519
permissions -rw-r--r--
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.

# 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