繁体   English   中英

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

[英]Execute git commit from php file in current directory

我尝试执行此代码,但它没有做任何事情。 但是当在shell_exec中放入“git show --summary”时,它会返回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

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

$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版,以正确的文件夹,其中回购协议。 您可能需要在命令中添加cd path_to_repo && git commit以首先移动到正确的目录。 我首先用绝对路径调试它。

只是建议一下......如果你试图建立一个基于PHP的git客户端,你将不得不处理大量的失败案例,其中一个在这段代码中很明显...没有修改新文件时提交。 如果您需要关注这些案例,我建议您查看社区解决方案:

是否有一个良好的PHP git客户端与http支持?

我建议你在你的git commit中添加名字和电子邮件:

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM