Can you prevent default-push, but allow pull?

2020-02-11 06:10发布

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.

4条回答
仙女界的扛把子
2楼-- · 2020-02-11 06:37

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.

查看更多
SAY GOODBYE
3楼-- · 2020-02-11 06:38

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.
查看更多
在下西门庆
4楼-- · 2020-02-11 06:45

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 = .
查看更多
SAY GOODBYE
5楼-- · 2020-02-11 06:54

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.

查看更多
登录 后发表回答