1

I try to execute this code, but it doesn't do anything. But when put "git show --summary" in shell_exec it return 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>";

}

Is it posible to commit git from php, and how?

Edmhs
  • 3,645
  • 27
  • 39
  • I assume you are using apache? What privilege level is your apache user running at? Git involves writes to the hidden `.git` folder. It will fail without proper write permissions. – DeaconDesperado Jul 19 '12 at 15:43

2 Answers2

3

shell_exec is going to return NULL if the command fails. This is almost certainly happening... the question is why. You aren't going to be able to find out using shell_exec.

I'd recommend instead using exec, so you can at least figure out the status of the call and what's going wrong.

http://www.php.net/manual/en/function.exec.php

This will allow you to setup some vars that will be populated with the return values of the system's internal os call.

$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);
}

My guess is you have a problem with permissions. A commit in git requires write permission to that directory's '.git' folder.

Also, make sure you are operating in the proper directory! The apache user that is running the PHP instance may or may not be already cded to the right folder where the repo is. You may need to add a cd path_to_repo && git commit to your command to first move to the right directory. I'd first debug this with absolute paths.

Just a word of advice as well... if you are trying to set up a PHP based git client, you are going to have to handle a ton of fail cases, one of which is readily apparent in this code... attempting a commit when no new files have been modified. I'd encourage you to look at community solutions if you need to be concerned with these cases:

Is there a good php git client with http support?

Community
  • 1
  • 1
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
  • Im not building client, i need fast way of adding/commiting one folder. Im doing it with command line daily. – Edmhs Sep 05 '12 at 14:25
0

I Recommend you to add name and email in your 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);

The errors can be found in server error file, ex.: /var/log/apache2/error.log

cat /var/log/apache2/error.log
Arthur Ronconi
  • 2,290
  • 25
  • 25