无法在 Windows / PHP 上执行 Git 提交

Cannot do Git Commit on Windows / PHP

我正在使用 Windows / PHP.

通过使用 PHP 脚本我想制作一个 commit 但是这个操作失败了。

为此,我使用了以下代码:

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

我也尝试使用 chdir 来确保我在 Git 存储库目录中,但它不起作用(无论如何我认为我不需要 chdir。问题是另一回事)。

通常从 Windows console 我使用上面相同的命令(完美运行):

> git commit -am .

此外,如果我从 Windows 控制台执行:

> php myscript.php

它工作正常。但是如果通过浏览器我去 url:

http://localhost/mysite/myscript.php

然后,提交没有通过。但正如我之前所说,状态是从浏览器正确读取的。

另一方面,如果我只是通过以下方式检查 status

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

那么Git status就正确返回了。

关于如何让 commitWindows 上通过 PHP 工作有什么想法吗?

[编辑 1]

我检查了 Apache 错误日志,发现每次尝试提交时都会出现错误,提示我需要指定电子邮件和姓名。然后我做到了(也通过 PHP)。但现在我收到另一个错误,它说:致命:带有 -a 的路径没有意义。 (在 Apache 错误日志上)当我尝试通过 PHP 进行以下提交时:shell_exec("git commit -am 'my commit message here'");。你知道现在要做什么了吗?

添加带有 git add . 的文件并将消息作为字符串参数提供给 -m:

提交:-am

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

编辑 1

git commit -am . 也适用于我,也许您只需要 escapeshellcmd 和单引号。我在测试时遇到了外部双引号的问题。

编辑 2

Windows 7

编辑 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);

重要:注意:

  1. 引号和双引号(需要按此顺序)
  2. escapeshellcmd
  3. Apache log对于了解幕后发生的事情(和调试)非常重要