views:

304

answers:

2

For those of you that use Mercurial with the MQ extension:

This is the second time I accidentally submit changes to the central repository (hg push) instead of applying a patch to my working directory (hg qpush).

I think this is very unfortunate, because it is a very simple error to make and has very severe consequences (the least having to do a hg backout and an extra hg push for each submitted change in order to generate a new commit that "undoes" the las one to the central repository, but the history becomes convoluted and unpleasant.

My goal is to configure some alias or something in my environment in orden to make hg push harder to do by accident.

Do you have any suggestions? I was thinking something like:

[alias]
push=      <-- how to NOP the push command??
pushtoserver=push

As this is a completely subjective question, this goes as community wiki.

thanks!

+5  A: 

some vague ideas:

  • you could remove the default push location from your repo
  • you could write a "did you mean qpush? yes, no" pre-push hook

This hook (bash command line) asks for confirmation before pushing changes to the remote (tested with mercurial 1.4):

[hooks]
preoutgoing.confirm = read -p 'Are you sure you want to push to remote? (y/n): '; echo $REPLY | grep -q 'y'
  • you could alias push to qpush and alias pushtoserver to push (i think this works but can't try it now)
jk
Very helpful. The two first suggestions are precisely the kind of answers I was looking for: different alternatives to solve my problem. I like the hook idea, but I don't know how to write them yet. That way I can keep using the native commands but with a safety net. Thanks.
Sergio Acosta
the book has a good chapter on hooks - added link in answer
jk
Thanks jk, I read the chapter and edited your question with the hook I came up with after reading it.
Sergio Acosta
and so the student becomes the master
jk
+2  A: 

You do what you suggested. Put the following in your .hgrc and you're done:

[alias]
pushtoserver = push
push = 

Works just fine:

$ hg push
no definition for alias 'push'

$ hg pushtoserver
abort: repository default-push not found!

See also: http://www.selenic.com/mercurial/hgrc.5.html#alias

Sofahamster
Nice. Thanks. Now everyone knows I didn't try to do it before posting =)
Sergio Acosta