2012-07-19 45 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