如何才能像的git命令git add .,并git commit -m使用PHP来执行?
它是为一个项目创建一个集中的存储库设施来维护学术项目.
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>";
?>
Run Code Online (Sandbox Code Playgroud)
这绝对是可能的.我在我的一个项目中实现了它.但是你应该注意权限.
在linux上,通常,exec命令将使用www-data用户执行.所以你应该允许www-data在你的工作目录上写和读.
一个快速而肮脏的方法是: chmod o+rw -R git_directory
还有另一种解决方案,就是像这样简单地使用 php 反引号
$message = `git log -1 --pretty=%B`; // get commit message
Run Code Online (Sandbox Code Playgroud)
或者
$deploy_tag = `git describe --tags`; // get commit tags
Run Code Online (Sandbox Code Playgroud)
或者
$hash = `git log -1 --pretty=%h`; // get the hash
Run Code Online (Sandbox Code Playgroud)