0

I'm using Windows / PHP.

By using a PHP script I wanna make a commit but this operation fails.

For this I'm using the following code:

<?
$command = "git commit -am .";
shell_exec($command);
?>

I also tried using chdir to be sure I'm in the Git repository directory but it doesn't work (anyway I think I don't need chdir. The problem is another thing).

Normally from the Windows console I use the same command above (which works perfectly):

> git commit -am .

Also, if from the Windows console I do:

> php myscript.php

it works properly. But if through the browser I go to the url:

http://localhost/mysite/myscript.php

Then, the commit doesn't go thru. But as I said you before, the status is read properly from the browser.

In the other hand, if I just check the status by doing:

<?
$command = "git status";
$output = shell_exec($command);
echo $output
?>

Then the Git status is returned correctly.

Any idea on how to make the commit works through PHP on Windows?

[EDIT 1]

I checked the Apache error log and I saw that everytime I tried to do a commit there was an error saying that I need to specify email and name. Then I did that (via PHP too). But now I'm getting another error, which says: fatal: Paths with -a does not make sense. (on the Apache error log) when I try to make the following commit via PHP: shell_exec("git commit -am 'my commit message here'");. Do you know what to do now?

David Smith
  • 481
  • 1
  • 5
  • 14

1 Answers1

0

Add the files with git add . and provide a message as a string argument to -m:

Commit with: -am

$command = 'git commit -am "php wuz here"';
$escaped_command = escapeshellcmd($command);
shell_exec($escaped_command);

edit 1

the git commit -am . is working for me, too, perhaps you just need the escapeshellcmd and single quotes. i had trouble with outer double quotes when i tested it.

edit 2

Windows 7

enter image description here

edit 3

Solution (validated by the author of the thread. It works!):

$command = 'git config user.name "Will Smith"';
$escaped_command = escapeshellcmd($command);
$result = shell_exec($escaped_command);

$command = 'git config user.email "myemail@gmail.com"';
$escaped_command = escapeshellcmd($command);
$result = shell_exec($escaped_command);

$command = 'git commit -am "my commit message here"';
$escaped_command = escapeshellcmd($command);
$result = shell_exec($escaped_command);

Important: Pay attention to:

  1. the quotes and double quotes (need to be in that order)
  2. escapeshellcmd
  3. the Apache log is very important to know what is happening behind the scene (and debug)
David Smith
  • 481
  • 1
  • 5
  • 14
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • your code didn't work for me. are you using windows or linux? – David Smith Dec 02 '16 at 18:26
  • windows and linux both showing the same result. when you say, "it doesn't work", does that mean you get an error or no output or it just doesn't create a commit? – WEBjuju Dec 02 '16 at 18:32
  • I added other experiments to my post above based on what you did. `Summarizing:` if from the Windows console I do: `> php myscript.php` the `commit` goes thru, but if I access to `myscript.php` through the browser, then the `commit` fails. In both cases, Windows console and browser, the `status` check works properly. – David Smith Dec 02 '16 at 19:13
  • if it fails when accessing it via the web browser, it is a permissions problem and not a git syntax or `shell_exec` problem – WEBjuju Dec 02 '16 at 19:27
  • if you can `sudo su` to become another user, find out what user your webserver is (`shell_exec(\`who\`);`) and `sudo su` to that user to find out what the *git* is going on when you run that command directly as that user. (ah, you may not have `who` on windows...well, that is an issue isn't it. what webserver are you using?) – WEBjuju Dec 02 '16 at 19:30
  • ah, the `git` binary may not be in the path of the webserver user. check that. – WEBjuju Dec 02 '16 at 19:31
  • I'm totally agree with that. Do you know how to configure the permissions for Apache on Windows in order it can do a Git commit? – David Smith Dec 02 '16 at 19:31
  • check [this thread on determining the user](http://stackoverflow.com/questions/7771586/how-to-check-what-user-php-is-running-as) php is running under. i think you need a new question - we have determined the solution to this one. – WEBjuju Dec 02 '16 at 19:34
  • I'm using Windows, not Linux, so Linux solutions don't apply for me. I think Git is on the path of the webserver user since the `git status` works properly (double checked on Windows system). The problem is with the `commit`. – David Smith Dec 02 '16 at 19:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129654/discussion-between-david-smith-and-webjuju). – David Smith Dec 02 '16 at 19:42
  • [this thread](http://stackoverflow.com/questions/7771586/how-to-check-what-user-php-is-running-as) has solutions that are platform independent. – WEBjuju Dec 02 '16 at 20:24
  • I checked the Apache error log and I saw that everytime I tried to do a commit there was an error saying that I need to specify email and name. Then I did that (via PHP too). But now I'm getting another error, which says: `fatal: Paths with -a does not make sense.` (on the Apache error log) when I try to make the following commit via PHP: `shell_exec("git commit -am 'my commit message here'");`. Do you know what to do now? – David Smith Dec 02 '16 at 21:12
  • i think you can drop the -a. but you will have to run the `git add .` first and that won't add deleted files. see my screenshot how i added using `git add .` and then separately called the `git commit -m "message here"`. – WEBjuju Dec 02 '16 at 22:06
  • are you escaping the shell arguments? if not, that's your issue with the `-a` – WEBjuju Dec 02 '16 at 22:06
  • escaping the shell arguments is very important – David Smith Dec 02 '16 at 22:22
  • indeed! indeed! – WEBjuju Dec 02 '16 at 22:26