通过PHP执行git命令

20

如何使用PHP执行类似于git add .git commit -m的git命令?

这是为了创建一个集中式代码库,用于维护学术项目。

exec()似乎无法工作。

<?php
$path = "/var/www/repos/$_POST[project]"; 
$a='';
chdir($path);
exec("git add .");  
exec("git commit -m'message'");
echo "<h3 align = center> Succesfully commited all the files.</h3>";
?>

使用类库,例如php-git - Mark Baker
只要PHP脚本有更新您的repo的权限,exec应该可以正常工作。尝试使用system()并查看返回值。 - Prisoner
能否通过对给定的代码进行修改来实现它? - rjv
1
已解决...问题出在.git文件夹的所有权,不是www-data。 - rjv
4个回答

14

这是完全可能的。我在我的一个项目中实现了它。 但是你应该注意权限。

在 Linux 上,通常情况下,exec 命令将使用 www-data 用户执行。 因此,您应该允许 www-data 在您的工作目录中读取和写入。

一种快速而不太规范的方法是: chmod o+rw -R git_directory


简单来说,git init命令必须由用户“www-data”运行。通过使用PHP脚本解决了我的问题。 - rjv
我对GIT和Linux很陌生(我是Windows用户),你能否解释一下这个是如何工作的?你在哪里执行了chmod命令(在PHP还是CMD中?)以及git_directory是什么?它是'.git'吗? - Abhishek Sachan
@asachanfbd,1)应该使用sudo在命令行中执行chmod,2)git_directory是.git的父目录。 - Oli
在Windows(xampp服务器)中怎么样?对我来说它不起作用。 运行whoami时,用户是nt authority\system。 我如何为该用户授权? - reignsly

5

还有另一种解决方案,就是直接使用php反引号,如下所示:

$message = `git log -1 --pretty=%B`; // get commit message

或者

$deploy_tag = `git describe --tags`; // get commit tags

或者

$hash = `git log -1 --pretty=%h`; // get the hash

2

我在这里找到了两个问题的相当简单的解决方案

1) 如何从受保护的远程Git仓库中拉取代码而无需输入密码

  • create a ssh key pairs , using ssh-keygen
  • upload public key to ur git remote host
  • configure ~/.ssh/config file to add instruct to use this generated private key by git

    host gitlab.com
     HostName gitlab.com
     IdentityFile ~/.ssh/private_key
     User git
    

    That's all now any git command will use this private_key to connect to remote

2) 以www-data用户身份运行git命令

  • update sudoers file to allow www-data user to run git commands, to edit run following command

    $ sudo visudo
    

    add folowing line under user preferences

    www-data    ALL=(raaghu) NOPASSWD: /usr/bin/git
    

    meaning www-data user from ALL terminals acting as raaghu can run /usr/bin/git with NOPASSWD

    Please Note: Don't try this method if you don't understand the sudoers file , otherwise this may create security hole

  • Create git-pull.sh file to run git commands

    cd /var/www/my-repo
    sudo -u raaghu git pull origin
    
  • run this git-pull.sh file from php

    exec("/bin/bash /var/www/git-pull.sh");
    

2

我首先对我的Bitbucket服务器实例运行测试,以获取任何在屏幕上输出的错误消息:

echo shell_exec("cd /website/root/htdocs && git status 2>&1");

这导致出现错误,无法找到git命令,因此必须提供git二进制文件的完整路径:
'which git'

返回结果(以下简称YOU_FULL_GIT_BINARY_PATH_HERE):

/usr/local/git/bin/git

完整路径,例如'/usr/local/git/bin/git status',可以很好地运行git命令。

这并不能解决在.git/config git用户设置中需要密码才能使用'git pull'命令的问题。在git存储库中运行以下命令:

git config credential.helper store

[command][1] 会提示您输入密码并让您在本地不加密存储它(仅由文件系统保护,例如在 /root/.git-credentials 中)。这将允许运行 'git pull' 而无需提示密码。另一种选择(可能更好)是为您的 Web 服务器生成 ssh 密钥,例如 apached,然后将它们添加到您的 Bitbucket 用户专用帐户或存储库密钥中。

我的所有文件夹都被设置为 apache 用户拥有 (Centos 6.8 其他版本可能是 www-data:www-data 等):

chown -R apache:apache YOUR_WEB_FODLER

我不需要使用肮脏的技巧 'chmod o+rw -R' 来让一切正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接