php Mercurial (HG) 拉取参数:用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12185048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mercurial (HG) pull parameters: username and password
提问by StackOverflowed
I have to write a script that automates pulling from a mercurial repo. Is there anyway I can perform an hg pull -uthat 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_openin PHP but that wasn't working as well as echoing out to STDIN.
我必须编写一个脚本来自动从 mercurial 存储库中提取。无论如何,我可以执行hg pull -u包括在同一命令中传递请求的用户名和密码的操作吗?我知道有一种交互方法是默认行为,我不想将用户名和密码保存在 hgrc 或其他地方,因为它将被多个用户使用,所以有没有办法通过用户名和密码通过命令行?我尝试proc_open在 PHP 中使用,但效果不如 STDIN。
回答by Drealmer
I found two solutions:
我找到了两个解决方案:
1. Explicitly provide the URL, including full credentials:
1. 明确提供 URL,包括完整凭据:
hg pull -u https://user:pass@host/path
hg pull -u https://user:pass@host/path
2. Provide the credentials with --config, using * as a prefix (from Victor's answer):
2. 使用 --config 提供凭据,使用 * 作为前缀(来自 Victor 的回答):
hg pull -u --config auth.x.prefix=* --config auth.x.username=user --config auth.x.password=pass
hg pull -u --config auth.x.prefix=* --config auth.x.username=user --config auth.x.password=pass
回答by Martin Geisler
Passing the password on the command line is insecuresince other users can see it using psand similar. What you should do is either:
在命令行上传递密码是不安全的,因为其他用户可以使用ps类似的方法看到它。你应该做的是:
Communicate with
hgon the pipes you get back fromproc_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=yesto 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
xis 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 theHGRCPATHenvironment variable to point to the file and execute Mercurial. Delete the file when you're done!
hg在您返回的管道上进行通信proc_open。您需要仔细解析 Mercurial 的输出并将用户名和密码输入到标准输入中。将 Mercurial 运行为:hg --config ui.interactive=yes以确保它会与您交谈 - 否则我担心它会检测到没有 TTY 并且根本不会提示您。
写一个带有
[auth]节的临时配置文件。它看起来像这样:[auth] x.prefix = output of "hg paths default" x.username = USERNAME x.password = PASSWORD(这
x并不重要,它可以是任何东西。)确保该文件只能由您运行脚本的用户读取。然后设置HGRCPATH环境变量指向文件并执行Mercurial。完成后删除文件!
There is a libhg PHP libraryfor 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.
有一个libhg PHP库的命令服务器,但我没有看到有关用户名或密码在该库拉或克隆任何东西。该库正在进行中,但当它更成熟时,我建议使用它。但是现在,上述方法应该可以帮助您。
回答by Victor Eijkhout
Solution 2 is specific to a repository. Do
解决方案 2 特定于存储库。做
[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.
使其适用于所有存储库。另外,如果你把它放在“~/.hgrc”中并赋予 600 的权限,你就不必做 HGRCPATH 技巧。把它留在那里。
回答by Vladimir Konkov
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
回答by Mike Brant
You might be able to use the --configswitch to do that like this:
您也许可以使用--config开关来做到这一点:
hg --config auth.username=USERNAME --config auth.password=PASSWORD pull -u
I personally have never done this, but hopefully this helps.
我个人从未这样做过,但希望这会有所帮助。

