2012-07-19 69 views
1

我尝试执行此代码,但它什么都不做。 但是,当在shell_exec中放入“git show --summary”时,它会返回git statuss。从当前目录中的php文件执行git commit

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,以及如何?

+0

我假设你使用的是Apache?你的apache用户运行在什么特权级别? Git涉及写入隐藏的'.git'文件夹。没有适当的写入权限,它将失败。 – DeaconDesperado 2012-07-19 15:43:41

回答

3

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编辑到repo所在的正确文件夹。您可能需要将cd path_to_repo && git commit添加到您的命令中才能首先移至正确的目录。我首先用绝对路径进行调试。

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

Is there a good php git client with http support?

+0

我不建设客户端,我需要快速的方式添加/提交一个文件夹。我每天都用命令行来做。 – Edmhs 2012-09-05 14:25:59

0

我建议你在你自己的Git添加的姓名和电子邮件提交:

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

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

cat /var/log/apache2/error.log