Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial (HG) pull parameters: username and password

Tags:

php

mercurial

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.

like image 436
StackOverflowed Avatar asked Aug 29 '12 19:08

StackOverflowed


4 Answers

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.

like image 39
Victor Eijkhout Avatar answered Sep 30 '22 07:09

Victor Eijkhout


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

like image 151
Drealmer Avatar answered Sep 30 '22 07:09

Drealmer


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:

  1. 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.

  2. 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.

like image 28
Martin Geisler Avatar answered Sep 30 '22 05:09

Martin Geisler


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
like image 41
Vladimir Konkov Avatar answered Sep 30 '22 05:09

Vladimir Konkov