我在一个个人项目中使用Mercurial,每次我想向服务器推送一些东西时,我都要输入我的用户名和密码。

我尝试将以下内容添加到主目录中的.hgrc文件中,但它似乎完全被忽略了。

[ui]
username = MY_USER_NAME
password = MY_PASSWORD

如何正确地做到这一点?


当前回答

有三种方法:使用.hgrc文件,使用ssh或使用keyring扩展名

1. 不安全的方式-更新您的~/。hgrc文件

适合我的格式(在我的~/。HGRC文件)是这样的

[ui]
username=Chris McCauley <chris.mccauley@mydomain.com>

[auth]
repo.prefix = https://server/repo_path
repo.username = username
repo.password = password

You can configure as many repos as you want by adding more triplets of prefix,username, password by prepending a unique tag. This only works in Mercurial 1.3 and obviously your username and password are in plain text - not good. 2. The secure way - Use SSH to AVOID using passwords Mercurial fully supports SSH so we can take advantage of SSH's ability to log into a server without a password - you do a once off configuration to provide a self-generated certificate. This is by far the safest way to do what you want. You can find more information on configuring passwordless login here 3. The keyring Extension If you want a secure option, but aren't familiar with SSH, why not try this? From the docs ... The extension prompts for the HTTP password on the first pull/push to/from given remote repository (just like it is done by default), but saves the password (keyed by the combination of username and remote repository url) in the password database. On the next run it checks for the username in .hg/hgrc, then for suitable password in the password database, and uses those credentials if found. There is more detailed information here

其他回答

上面没有人向新手解释/澄清术语。他们被这些术语弄糊涂了

.hg/hgrc——此文件用于存储库,在本地/工作空间位置/实际存储库的.hg文件夹中。

~ /。HGRC——这个文件与下面的文件不同。该文件位于~或主目录。

myremote.xxxx =… bb.xxxx =……

This is one of the lines under [auth] section/directive, while using mercurial keyring extension. Make sure the server name you put there, matches with what you use while doing "hg clone" otherwise keyring will say, user not found. bb or myremote in the line below, are "alias name" that you MUST give while doing "hg clone http:/.../../repo1 bb or myremote" otherwise, it wont work or you have to make sure your local repository's .hg/hgrc file contain same alias, ie (what you gave while doing hg clone .. as last parameter).

PS以下链接为清晰的细节,抱歉太快写语法。

https://stackoverflow.com/questions/14267873/mercurial-hg-no-changes-found-cant-hg-push-out/14269997#14269997 http://www.linuxquestions.org/questions/showthread.php?p=4867412#post4867412 https://stackoverflow.com/questions/12503421/hg-push-error-and-username-not-specified-in-hg-hgrc-keyring-will-not-be-used/14270602#14270602 —Windows LDAP—组用户认证—Mercurial

如果在~/。hgrc (Linux/Unix中用户的主目录)或Windows中的mercury .ini在用户的主目录中,包含,下面的行,如果你做

`"hg clone http://.../.../reponame myremote"`

,那么在每个HTTP回收链接中,用户凭证的提示永远不会超过一次。在~ /。HGRC下的[扩展]一行“mercural_keyring =”或“hgeext。mercural_keyring = /path/to/your/ mercural_keyring .py" ..这里应该有一条线。

[auth]
myremote.schemes = http https
myremote.prefix = thsusncdnvm99/hg
myremote.username = c123456

我试图找出如何设置前缀属性,以便用户可以克隆或执行任何Hg操作,没有用户名/密码提示,而不用担心他在http://..../中提到的…当使用Hg repo链接时,为服务器名。它可以是IP、服务器名或服务器的FQDN

虽然它在您的情况下可能工作,也可能不工作,但我发现使用Putty的Pageant生成公钥/私钥非常有用。

如果你也在使用bitbucket (.org),它应该能让你为你的用户帐户提供一个公钥,然后到达存储库的命令将被自动保护。

如果重新启动后Pageant没有启动,您可以在Windows“开始菜单”中添加Pageant的快捷方式,并且该快捷方式可能需要有一个“属性”,其中包含您的私人(.ppk)文件的位置。

有了这些,需要将Mercurial和本地存储库设置为使用SSH格式进行推送/拉取。

以下是Atlassian网站上针对Windows或Mac/Linux的一些详细说明。

你不必相信我的话,毫无疑问还有其他方法可以做到这一点。也许这里描述的这些步骤更适合你:

Start PuttyGen from Start -> PuTTY-> PuttyGen Generate a new key and save it as a .ppk file without a passphrase Use Putty to login to the server you want to connect to Append the Public Key text from PuttyGen to the text of ~/.ssh/authorized_keys Create a shortcut to your .ppk file from Start -> Putty to Start -> Startup Select the .ppk shortcut from the Startup menu (this will happen automatically at every startup) See the Pageant icon in the system tray? Right-click it and select “New session” Enter username@hostname in the “Host name” field You will now log in automatically.

你可以在你的.hgrc或mercury .ini文件中创建一个认证部分,如下所示:

[auth]
bb.prefix = https://bitbucket.org/repo/path
bb.username = foo
bb.password = foo_passwd

' bb '部分是一个任意标识符,用于将前缀与用户名和密码匹配-方便管理不同站点的不同用户名/密码组合(前缀)

您也可以只指定用户名,然后在按下按钮时只需输入密码。

我还建议看看密匙环扩展。因为它将密码存储在系统的密匙环中,而不是纯文本文件,所以更安全。它在Windows上与TortoiseHg捆绑在一起,目前正在讨论将其作为捆绑扩展分发到所有平台上。

没人提到密匙环扩展。它会将用户名和密码保存到系统密匙环中,这比上面提到的将密码存储在静态文件中要安全得多。执行下面的步骤,您就可以开始了。我用了大约2分钟就在Ubuntu上安装并运行了它。

>> sudo apt-get install python-pip
>> sudo pip install keyring
>> sudo pip install mercurial_keyring

**Edit your .hgrc file to include the extension**
[extensions]
mercurial_keyring = 

https://www.mercurial-scm.org/wiki/KeyringExtension

一个简单的破解方法是在项目的。hg/hgrc文件中添加用户名和密码到推送url:

[paths]
default = http://username:password@mydomain.com/myproject

(注意,通过这种方式,您可以以纯文本的形式存储密码)

如果您在同一个域下处理多个项目,您可能希望在~/中添加一个重写规则。HGRC文件,以避免对所有项目重复此操作:

[rewrite]
http.//mydomain.com = http://username:password@mydomain.com

同样,由于密码是以纯文本形式存储的,所以我通常只存储我的用户名。

如果你在Gnome下工作,我在这里解释如何集成Mercurial和Gnome Keyring:

http://aloiroberto.wordpress.com/2009/09/16/mercurial-gnome-keyring-integration/