通过 PHP 执行 git 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8562544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing git commands via PHP
提问by rjv
How can git commands like git add .
and git commit -m
be executed using php?
如何使用 php 执行git 命令git add .
和git commit -m
执行?
It is for a project that creates a centralized repository facility for maintaining academic projects.
它适用于创建用于维护学术项目的集中存储库设施的项目。
exec() didn't seem to work.
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>";
?>
采纳答案by Oli
It is definetly possible. I implemented it in one of my projects. However you should be careful about permissions.
这绝对是可能的。我在我的一个项目中实现了它。但是,您应该注意权限。
On linux, usually, the exec
command will execute using the www-data
user.
So you should allow www-data
to write and read on your work directory.
在 linux 上,通常,exec
命令将使用www-data
用户执行。所以你应该允许www-data
在你的工作目录上写入和读取。
One quick and dirty way to do it is : chmod o+rw -R git_directory
一种快速而肮脏的方法是: chmod o+rw -R git_directory
回答by Tim Hallman
There is another solution, which is to simply use php backticks like so
还有另一种解决方案,就是像这样简单地使用php反引号
$message = `git log -1 --pretty=%B`; // get commit message
or
或者
$deploy_tag = `git describe --tags`; // get commit tags
or
或者
$hash = `git log -1 --pretty=%h`; // get the hash
回答by Raaghu
I found a pretty simple solutions to 2 problems here
我在这里找到了两个问题的非常简单的解决方案
1) Git pull from a secured remote without using password
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
- 创建一个 ssh 密钥对,使用
ssh-keygen
- 将公钥上传到您的 git 远程主机
配置 ~/.ssh/config 文件以添加指示以通过 git 使用此生成的私钥
host gitlab.com HostName gitlab.com IdentityFile ~/.ssh/private_key User git
这就是现在任何 git 命令都将使用此 private_key 连接到远程
2) Running git command from www-data user
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 fromALL
terminals acting asraaghu
can run/usr/bin/git
withNOPASSWD
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");
更新 sudoers 文件以允许 www-data 用户运行 git 命令,编辑运行以下命令
$ sudo visudo
在用户首选项下添加以下行
www-data ALL=(raaghu) NOPASSWD: /usr/bin/git
这意味着
www-data
从用户ALL
作为终端raaghu
可以运行/usr/bin/git
与NOPASSWD
请注意:如果您不了解 sudoers 文件,请不要尝试此方法,否则可能会造成安全漏洞
创建 git-pull.sh 文件以运行 git 命令
cd /var/www/my-repo sudo -u raaghu git pull origin
从 php 运行这个 git-pull.sh 文件
exec("/bin/bash /var/www/git-pull.sh");
回答by webcoder.eu
I first run a test against my bitbucket server instance to get any error messages output on screen:
我首先对我的 bitbucket 服务器实例运行测试以获取屏幕上输出的任何错误消息:
echo shell_exec("cd /website/root/htdocs && git status 2>&1");
this threw an error that it could not find git command hence had to provide a full path to git's binary:
这引发了一个错误,它找不到 git 命令,因此必须提供 git 二进制文件的完整路径:
'which git'
returned (further called YOU_FULL_GIT_BINARY_PATH_HERE):
返回(进一步称为 YOU_FULL_GIT_BINARY_PATH_HERE):
/usr/local/git/bin/git
A full path e.g. '/usr/local/git/bin/git status' now runs git commands nicely.
一个完整的路径,例如 '/usr/local/git/bin/git status' 现在可以很好地运行 git 命令。
This doesn't overcome git password required to use 'git pull' command for a set in .git/config git user. Running below command in git repo:
这并不能克服在 .git/config git 用户中使用“git pull”命令所需的 git 密码。在 git repo 中运行以下命令:
git config credential.helper store
[command][1] will prompt for password and let you store it locally unencrypted (protected only by file system e.g. in /root/.git-credentials). This will allow to run 'git pull' without prompting for password. Alternatively (probably better) is to generate ssh keys for your web server such as apached then add them to your bitbucket user dedicated account or repo keys.
[command][1] 将提示输入密码并让您将其存储在本地未加密(仅受文件系统保护,例如在 /root/.git-credentials 中)。这将允许在不提示输入密码的情况下运行“git pull”。或者(可能更好)是为您的 Web 服务器(例如 apached)生成 ssh 密钥,然后将它们添加到您的 bitbucket 用户专用帐户或 repo 密钥。
All my folders have been set to be owned by apache user (Centos 6.8 other releases might be www-data:www-data etc.):
我所有的文件夹都设置为 apache 用户所有(Centos 6.8 其他版本可能是 www-data:www-data 等):
chown -R apache:apache YOUR_WEB_FODLER
I did not have to use the dirty trick 'chmod o+rw -R' to get all working.
我不必使用肮脏的技巧 'chmod o+rw -R' 来完成所有工作。