21

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.

StackOverflowed
  • 5,854
  • 9
  • 55
  • 119

5 Answers5

29

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

Drealmer
  • 5,578
  • 3
  • 30
  • 37
  • 1
    Bonus info for solution 2: `prefix=*` doesn't work for me when the `[auth]` section in my actual config file already contains such an entry for the same hoster. Example: my config file contains `bb.prefix = https://bitbucket.org/` and `bb.username = christianspecht`. Pulling from Bitbucket with solution 2 only works for me with `--config auth.x.prefix=https://bitbucket.org/` instead of `--config auth.x.prefix=*`. – Christian Specht Jun 26 '18 at 19:06
7

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.

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23
5

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.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
2

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
Vladimir Konkov
  • 161
  • 2
  • 9
-1

You might be able to use the --config switch to do that like this:

hg --config auth.username=USERNAME --config auth.password=PASSWORD pull -u

I personally have never done this, but hopefully this helps.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • 6
    No doesn't work, ignoring invalid [auth] key 'username' ignoring invalid [auth] key 'password' – radtek Mar 06 '14 at 17:14