5

I want to know if there's a way to turn off the default push, but keep the default pull when using Mercurial. I don't want to accidentally pollute the master repository by inadvertently pushing from an experimental repository.

moswald
  • 11,491
  • 7
  • 52
  • 78

4 Answers4

7

I was able to solve this by putting the following in my .hg/hgrc file, but I was wondering if there's a better/official way.

[paths]
default = http://server/hg/repo
default-push = .
Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
moswald
  • 11,491
  • 7
  • 52
  • 78
  • This answer almost works, or, rather, mostly works, but can break if "." contains a repo - e.g. if you hg your home directory. I fleshed it out a bit below to avoid such a problem. – Krazy Glew Jun 18 '12 at 19:19
  • @KrazyGlew I don't think that's necessary. He said he's doing this in `.hg/hrc` so it's in repo and thus relative to the repo root, so that would always be `pushing to itself` which should never do anything. – Ry4an Brase Jun 19 '12 at 01:46
3

Your solution probably is the quickest and is certainly effective. If there's any official way it would be using a preoutgoing hook:

[hooks]
preoutgoing = bash -c 'read -p "Really push to $HG_URL? " -n 1 RESP ; [ "$RESP" == "y" ]'

which will ask you if you want to push and provide the URL to which it would go as a reminder.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • Hey, I really like this one, it's similar to Martin's, but gives me the option to continue when I manually type the url on the command line. If you can tell me how to do that in powershell, we're in business. – moswald Jul 22 '10 at 15:08
  • I think the command to do complex scripting in powershell is wubi.exe. By which I mean, sorry, I dont know powershell. ;) – Ry4an Brase Jul 22 '10 at 20:22
  • I did quite a bit of experimenting, but couldn't quite get it to work. I'll keep plugging away and update here if I solve it. – moswald Jul 23 '10 at 14:26
2

I like your own answer of setting paths.default-push = . -- it is simple and it is clear that it will work.

Another option would be a pre-push hook:

[hooks]
pre-push = if [ $HG_PATS == "[]" -o $HG_PATS == "['default']" ]; then
               read -p "Really push to default? " -n 1; echo
               [ "$REPLY" == "y" ]
           fi

(Here I'm taking advantage of how you can split a long value over several lines by indenting them in a Mercurial config file.)

A push to default looks this

% hg push
Really push to default? n
warning: pre-push hook exited with status 1

where I typed the n. The hooks checks for both no arguments ($HG_PATS == "[]") and a default as the argument ($HG_PATS == "['default']") and will only prompt you in those cases. The $HG_PATS variable was introduced in Mercurial 1.6.

PS: I saw you updated the question and asked for a solution in PowerShell, but I'm afraid I know nothing about that language. However, you should be able to lift the important concepts from this answer yourself.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • This is pretty good, but doesn't it completely remove the push ability? I want to be able to push; I just want to make sure that I don't accidentally push to the default. – moswald Jul 22 '10 at 15:06
  • mos: You're right... I've updated the answer to let you push again and I've incorporated the best of Ry4an's answer as well. – Martin Geisler Jul 22 '10 at 19:17
  • Thanks for the update. I think it's time for me to ask a new question: how to get a powershell command to work in an hg hook. – moswald Jul 23 '10 at 14:29
  • Hmm, when I add this to my hgrc I end up getting the warning/prompt regardless of what branch I'm on. – Adam Parkin Mar 20 '15 at 21:10
0

The answer previously posted, in hgrc setting

   default-push = .

is ALMOST but not quite correct. It can break, e.g. if you hg your home directory.

Here is the my current BKM to disable default-push:

I've embellished the idea of setting paths.default-push in ~/.hgrc, making it a little bit more self documenting and less error-prone - since, as I point out below, setting default-push = . does not always disable pushing.

in ~/.hgrc

[paths]
# my main project master repo
project-master = ...

#DISABLING IMPLICIT PUSH
#     to prevent embarassment from accidentally pushing to the project master repo
#     instead of, in my case, a repo that has fine grain commits
#     that the rest of the team does not want to see in the project master repo
#default-push = .
#     this works mostly, but NOT if you use hg on your home directory
#     since '.' in ~/.hgrc seems to be interpreted as -R ~
#default-push = /NONEXISTENT_default-push_--_must_specify_push_target_explicity
#     this works ok, but I can clean up the error message using blanks
#     keeping this around because blanks in pathnames confuses many UNIX tools
default-push = /'NONEXISTENT default-push -- must specify push target explicitly'
#     this amounts to disabling implicit push targets.
Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
  • You probably are better off doing that in `.hg/hgrc` (the per-repo hgrc) for your experimental repos rather than in `~.hgrc` (the per-user hgrc). That's certainly what MG was suggesting above. When that's the case `.` is perfectly safe. – Ry4an Brase Jun 19 '12 at 01:58
  • ~/.hrc is exactly what *I* want. I clone repos a lot, and it is in these clones, with fresh-made default, that I want to prevent accidentally pushing back to the wrong place. The problem with doing it in /.hg/hgrc is that /.hg/hgrc is NOT propagated by clone. If you want to use /.hg/hgrc, you have to install it after every clone. Now, I *have* written makefiles to do that - hg clone project foo; make -C foo install-hgrc - but (a) I don't think there is a default action after clone, so you have to wrapperize. and (b) other users of the may not want this. – Krazy Glew Jun 23 '12 at 20:10
  • By the way, I am very happy with setting default-push in ~/.hgrc. It has prevented some embarrassment. However, I still would like to override default completely - because I have fallen into the habit of typing "hg push default". Which is NOT what you want to do when your workflow is to push to a staging area. Another question, perhaps... – Krazy Glew Jun 23 '12 at 20:12