douyi9447
douyi9447
2012-08-29 19:20
浏览 190
已采纳

Mercurial(HG)拉参数:用户名和密码

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.

图片转代码服务由CSDN问答提供 功能建议

我必须编写一个自动从mercurial repo中提取的脚本。 无论如何我可以执行 hg pull -u ,包括在同一命令中传递请求的用户名和密码? 我知道有一个交互方法是默认行为,我不想在hgrc或其他地方保存用户名和密码,因为它将被多个用户使用,所以有没有办法通过用户名和密码传递 命令行? 我尝试在PHP中使用 proc_open ,但这并不像回应STDIN那样有效。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duan051347
    duan051347 2014-03-02 10:22
    已采纳

    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

    点赞 评论
  • drfqfuhej48511519
    drfqfuhej48511519 2012-08-29 19:35

    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.

    点赞 评论
  • drfls28608
    drfls28608 2012-08-29 21:47

    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.

    点赞 评论
  • dsqdpn31467
    dsqdpn31467 2013-02-05 19:46

    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.

    点赞 评论
  • dousaoxiancy199896
    dousaoxiancy199896 2013-02-14 14:39

    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
    
    点赞 评论

相关推荐