I have to write a script that automates pulling from a mercurial repo. Is there anyway I can perform an hg pull -u
that includes passing the requested username and password in the same command? I know there's an interactive method which is the default behaviour, and I don't want to save the username and password in hgrc or elsewhere because it will be used by multiple users, so is there a way to pass the username and password via the command line?
I tried using proc_open
in PHP but that wasn't working as well as echoing out to STDIN.
Solution 2 is specific to a repository. Do
[auth]
x.prefix = *
x.username = USERNAME
x.password = PASSWORD
to make it apply to all repositories. Also, if you put this in "~/.hgrc" and give that permission of 600 you don't have to do the HGRCPATH trick. Just leave it there.
I found two solutions:
1. Explicitly provide the URL, including full credentials:
hg pull -u https://user:pass@host/path
2. Provide the credentials with --config, using * as a prefix (from Victor's answer):
hg pull -u --config auth.x.prefix=* --config auth.x.username=user --config auth.x.password=pass
Passing the password on the command line is insecure since other users can see it using ps
and similar. What you should do is either:
Communicate with hg
on the pipes you get back from proc_open
. You'll need to carefully parse the output from Mercurial and feed it the username and password on stdin. Run Mercurial as:
hg --config ui.interactive=yes
to make sure that it will talk to you — otherwise I'm afraid that it detects that there is no TTY and wont prompt you at all.
Write a temporary configuration file with an [auth]
section. It will look like this:
[auth]
x.prefix = output of "hg paths default"
x.username = USERNAME
x.password = PASSWORD
(The x
is not important, it can be anything.)
Make sure that the file is only readable by the user you're running the script as. Then set the HGRCPATH
environment variable to point to the file and execute Mercurial. Delete the file when you're done!
There is a libhg PHP library for the command server, but I don't see anything about usernames or passwords for pull or clone in that library. The library is work in progress, but when it's more mature I suggest using it. But for now the above approaches should help you along.
Thats worked:
hg --config auth.rc.prefix=http://host/ --config auth.rc.username=user --config auth.rc.password=password pull -u http://host/full/path/to/repo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With