简体   繁体   中英

Cannot do Git Commit on Windows / PHP

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?

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

在此处输入图片说明

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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