【问题标题】:git config not used when git pull via PHP file通过 PHP 文件进行 git pull 时未使用 git config
【发布时间】:2012-08-05 12:43:29
【问题描述】:

我正在尝试从 PHP 文件中git pull,但似乎没有使用配置。

git config --global user.namegit config --global user.email 已设置并且 git pull 从命令行运行。

现在当我尝试执行这个脚本时:

<?php

header('Content-type: text/plain');
system('git pull 2>&1');
echo "\nCompleted!";

我得到以下输出:

*请告诉我你是谁。

运行

git config --global user.email "you@example.com" git config --global user.name "你的名字"

设置您帐户的默认身份。省略 --global 来设置 身份仅在此存储库中。

致命:不允许为空标识

完成!

可能是因为git config 是用户特定的,而 PHP 是从另一个用户运行的?

【问题讨论】:

    标签: php git


    【解决方案1】:

    只需在 git 本身上使用 -c 标志:

    -c <name>=<value>
    Pass a configuration parameter to the command. The value given will override values from configuration files. The <name> is expected in the same format
    as listed by git config (subkeys separated by dots).
    

    比如:

    git -c user.name=apache pull
    

    应该能带你到那里。添加您需要的任何参数。如果要指定特定文件,则将相关配置选项添加到执行脚本的用户,或通过exporting a different HOME env variable first. 破解它

    【讨论】:

    • 谢谢。我现在使用system('/usr/bin/env -i HOME=/home/myself git pull 2&gt;&amp;1');
    【解决方案2】:

    您希望您的网络服务器即使在您未登录的情况下也能正常工作,不是吗?这意味着它由您以外的其他用户运行(除非您另外指定)。除非您通过 CLI 运行脚本,否则脚本将由 apache、www-data 或您配置的任何用户运行。

    【讨论】:

      【解决方案3】:

      我设法让它工作,但这不是一个理想的解决方案。我需要找到一种方法为 apache 用户设置一次 git config,而不是每次。我在共享主机上,所以我不知道它是否真的可能。

      <?php
      
      header('Content-type: text/plain');
      system('git config user.name "Server"');
      system('git config user.email "deploy@server.com"');
      system('git pull 2>&1');
      echo "\nCompleted!";
      

      【讨论】:

        【解决方案4】:

        有点杂乱无章,不过你也可以设置系统的git config:

        git config --system user.name <user>
        git config --system user.email <user>@<domain>
        

        这有一个相当不幸的副作用,即成为服务器所有用户的默认配置,所以它确实不是很好。

        【讨论】:

          猜你喜欢
          • 2011-12-28
          • 1970-01-01
          • 2015-02-11
          • 1970-01-01
          • 2015-07-08
          • 2022-01-06
          • 2020-03-25
          • 2014-01-02
          • 1970-01-01
          相关资源
          最近更新 更多