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

标签 php linux git apache shell

我尝试执行这段代码,但它什么也没做。 但是当在 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 用户可能已cded 到 repo 所在的正确文件夹,也可能尚未。您可能需要在命令中添加 cd path_to_repo && git commit 以首先移动到正确的目录。我会先用绝对路径调试它。

还有一个忠告……如果您正在尝试设置一个基于 PHP 的 git 客户端,您将不得不处理大量的失败案例,其中一个在这段代码中很明显。 . 在没有修改新文件时尝试提交。如果您需要关注这些情况,我鼓励您查看社区解决方案:

Is there a good php git client with http support?

关于php - 从当前目录下的 php 文件执行 git commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11564289/

相关文章:

php - 为什么curl extension_loaded显示为false而不是curl存在

php - 自定义 Woocommerce 模板 my-account/form-edit-addresses

php - 以页面而非用户身份在 Facebook 墙上发帖

linux - 安装Pymol报错

linux - 使用 Ansible 列出已安装软件包的通用方法

git - Windows 文件服务器上的共享 Git 存储库(失败)

php - 如何使用 PHP/MYSQLI 更新数据库记录并与传递的变量保持在同一页面上

php - 错误! : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Windows 10 上的 Git Bash 运行缓慢

macos - 是否可以在 Mac 上使用 GUI 并排工具查看 git diff?