我尝试执行这段代码,但它什么也没做。 但是当在 shell_exec 中放置“git show --summary”时,它会返回 git 状态。
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
这将允许您设置一些变量,这些变量将填充系统内部操作系统调用的返回值。
$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
ed 到 repo 所在的正确文件夹,也可能尚未。您可能需要在命令中添加 cd path_to_repo && git commit
以首先移动到正确的目录。我会先用绝对路径调试它。
还有一个忠告……如果您正在尝试设置一个基于 PHP 的 git 客户端,您将不得不处理大量的失败案例,其中一个在这段代码中很明显。 . 在没有修改新文件时尝试提交。如果您需要关注这些情况,我鼓励您查看社区解决方案:
关于php - 从当前目录下的 php 文件执行 git commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11564289/