Pregunta

As noted in the documentation, the hg backout command can cause problems when used with merge changesets. We have had a couple of cases recently of newer developers backing out merge changesets and causing code that we wanted to keep to be reverted when everything gets merged back together.

To avoid this, I'm trying to think of a good way to prevent this from happening at all. Is there a good general way I could write a hook or just disable the backout command entirely?

(Part of our standard developer setup is to install a custom set of extensions, so I already have a good way to install these types of rules locally for our entire development team -- I just haven't thought of a good way to implement the ruleset.)

¿Fue útil?

Solución 2

Here's a simple Python-based hook that prevents backout of merges. Credit goes to @Oben for pointing me in the right direction.

def prebackout_prevent_backout_merge( ui, repo, **kwargs ):
    '''Don't allow backouts to of merge changesets.'''

    # Figure out if a --parent version was given or not.
    backout_to_parent = kwargs['opts'].get( 'parent', None )

    # If no parent version was given, proceed.
    if backout_to_parent is '':
        return False

    # Otherwise abort the operation.
    from mercurial import util
    raise util.Abort( 'Backout of a merge changeset is not allowed.' )

You can configure this hook in .hgrc or Mercurial.ini with:

[hooks]
pre-backout.ttd_prevent_backout_merge = python:PATH_TO_HOOK_SCRIPT:prebackout_prevent_backout_merge

Otros consejos

What about this:

$ hg --version
Mercurial Distributed SCM (version 2.6.3)
...
$ hg log --graph --template='{rev} {desc}'
@  5 c5
|
o    4 merge
|\
| o  3 c4
| |
o |  2 c3
|/
o  1 c2
|
o  0 c1

$ hg backout 4
abort: cannot backout a merge changeset

So it looks like Mercurial does what you want by default. Maybe you need a more recent Mercurial version.

If you are stuck to an old Mercurial version, here is a hacky hook (for *nix/Bash) which aborts backouts of merges:

[hooks]
pre-backout=REV=`echo $HG_PATS | sed -e "s/[^0-9]//g"`; test `hg log -r "parents($REV)" --template='{node}\n' | wc -l` -eq 1 || { echo 'do not do that'; exit 1; }

It extract the revision number from $HG_PATS and then uses hg log and wc to count the number of parents of the revision in question. If there is more than one parent, it is a merge.

Still, I highly recommend to use a recent Mercurial: check the release notes for exciting features you are missing.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top