从当前目录中的php文件执行git-commit


Execute git commit from php file in current directory

我试图执行这段代码,但它什么都没做。但是当把"git-show--summary"放在shell_exec中时,它会返回git-statuss。

if($_GET['action']=='add'){
    $output = shell_exec('git add *');
    echo "Add:<pre>$output</pre>";
}
if($_GET['action']=='commit'){
    $output = shell_exec('git commit -m "'.$_POST["txt"].'" ');
    echo "Commit:<pre>$output</pre>";
}

是否可以从php提交git,以及如何提交?

如果命令失败,则shell_exec将返回NULL。这几乎肯定会发生。。。问题是为什么。使用shell_exec您将无法找到。

我建议使用exec,这样你至少可以弄清楚调用的状态和出了什么问题。

http://www.php.net/manual/en/function.exec.php

这将允许您设置一些变量,这些变量将用系统内部os调用的返回值填充。

$shell_output = array();
$status = NULL;
if($_GET['action']=='add'){
    $output = shell_exec('git add *',$shell_output,$status);
    print_r($shell_output)
    print_r($status);
}
if($_GET['action']=='commit'){
    $output = shell_exec('git commit -m "'.$_POST["txt"].'" ',$shell_output,$status);
    print_r($shell_output)
    print_r($status);
}

我猜你的权限有问题。git中的commit需要对该目录的".git"文件夹具有写入权限。

此外,请确保您在正确的目录中操作!运行PHP实例的apache用户可能已经cd到repo所在的正确文件夹,也可能没有。您可能需要在命令中添加一个cd path_to_repo && git commit才能首先移动到正确的目录。我会先用绝对路径调试它。

也只是一句忠告。。。如果你试图建立一个基于PHP的git客户端,你将不得不处理大量的失败案例,其中一个在这段代码中很明显。。。在没有修改新文件的情况下尝试提交。如果你需要关注这些案例,我鼓励你考虑社区解决方案:

有一个好的php git客户端支持http吗?

我建议您在gitcommit:中添加名称和电子邮件

$output = shell_exec('git -c user.name="www-data" -c user.email="no-replay@example.org" commit -m "'.$_POST["txt"].'" ', $shell_output, $status);

错误可以在服务器错误文件中找到,例如:/var/log/apache2/error.log

cat /var/log/apache2/error.log