【问题标题】:Mercurial (HG) pull parameters: username and passwordMercurial (HG) 拉取参数:用户名和密码
【发布时间】:2012-08-24 11:35:53
【问题描述】:

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

【问题讨论】:

    标签: php mercurial


    【解决方案1】:

    我找到了两个解决方案:

    1。明确提供 URL,包括完整的凭据:

    hg pull -u https://user:pass@host/path

    2。使用 --config 提供凭据,使用 * 作为前缀(来自 Victor 的回答):

    hg pull -u --config auth.x.prefix=* --config auth.x.username=user --config auth.x.password=pass

    【讨论】:

    • 解决方案 2 的奖励信息:prefix=* 对我不起作用,因为我的实际配置文件中的 [auth] 部分已经包含同一主机的此类条目。示例:我的配置文件包含bb.prefix = https://bitbucket.org/bb.username = christianspecht。使用解决方案 2 从 Bitbucket 中提取仅适用于 --config auth.x.prefix=https://bitbucket.org/ 而不是 --config auth.x.prefix=*
    【解决方案2】:

    解决方案 2 特定于存储库。做

    [auth]
    x.prefix = *
    x.username = USERNAME
    x.password = PASSWORD
    

    使其适用于所有存储库。此外,如果你把它放在 "~/.hgrc" 中并给予 600 的权限,你就不必使用 HGRCPATH 技巧。把它放在那里。

    【讨论】:

      【解决方案3】:

      在命令行上传递密码是不安全的,因为其他用户可以使用ps 和类似的方式看到它。你应该做的是:

      1. 在您从proc_open 返回的管道上与hg 通信。您需要仔细解析 Mercurial 的输出,并将用户名和密码输入标准输入。将 Mercurial 运行为:

        hg --config ui.interactive=yes
        

        确保它会和你对话——否则我担心它会检测到没有 TTY 并且根本不会提示你。

      2. 使用[auth] section 编写一个临时配置文件。它看起来像这样:

        [auth]
        x.prefix = output of "hg paths default"
        x.username = USERNAME
        x.password = PASSWORD
        

        x 并不重要,它可以是任何东西。) 确保文件只能由您运行脚本的用户读取。然后设置HGRCPATH 环境变量指向该文件并执行Mercurial。完成后删除文件!

      command server 有一个 libhg PHP library,但我在该库中看不到任何有关拉取或克隆的用户名或密码的信息。该库正在进行中,但当它更成熟时,我建议使用它。但目前,上述方法应该对您有所帮助。

      【讨论】:

        【解决方案4】:

        成功了:

        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
        

        【讨论】:

          【解决方案5】:

          您也许可以像这样使用--config 开关来做到这一点:

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

          我个人从未这样做过,但希望这会有所帮助。

          【讨论】:

          • No 不起作用,忽略无效的 [auth] key 'username' ignoring invalid [auth] key 'password'
          猜你喜欢
          • 1970-01-01
          • 2012-09-01
          • 2011-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-07
          • 2017-08-03
          • 2018-07-05
          相关资源
          最近更新 更多