我的情况如下:
我在我的Mac上使用MAMP (PHP)本地开发。我的站点在Git版本控制下,我将我的开发服务器指向磁盘上版本控制下站点的根目录。
File structure:
--mysitehere/
---.git/ (.git folder is here versioning everything below)
---src/ (<-- web server root)
----index.php (need the codez here for displaying current git branch)
任何人都有示例代码,我可以使用它在。git文件夹中查看当前分支是什么,并在index.php页面上输出它(以及RoR开发的ruby解决方案)?这将是超级有用的,当我切换分支,,在我的浏览器中,当我刷新,我看到我将在'master'在页面顶部,或'your-topic-branch-name-here'。
我愿意使用第三方库在PHP中以编程方式访问git,或者从磁盘上的文件中从。git中获取正确的'current-branch'变量。
这在我的PHP工作,包括它在我的网站的顶部:
/**
* @filename: currentgitbranch.php
* @usage: Include this file after the '<body>' tag in your project
* @author Kevin Ridgway
*/
$stringfromfile = file('.git/HEAD', FILE_USE_INCLUDE_PATH);
$firstLine = $stringfromfile[0]; //get the string from the array
$explodedstring = explode("/", $firstLine, 3); //seperate out by the "/" in the string
$branchname = $explodedstring[2]; //get the one that is always the branch name
echo "<div style='clear: both; width: 100%; font-size: 14px; font-family: Helvetica; color: #30121d; background: #bcbf77; padding: 20px; text-align: center;'>Current branch: <span style='color:#fff; font-weight: bold; text-transform: uppercase;'>" . $branchname . "</span></div>"; //show it on the page
分支,最后提交日期和哈希值
<?php
$gitBasePath = '.git'; // e.g in laravel: base_path().'/.git';
$gitStr = file_get_contents($gitBasePath.'/HEAD');
$gitBranchName = rtrim(preg_replace("/(.*?'/){2}/", '', $gitStr));
$gitPathBranch = $gitBasePath.'/refs/heads/'.$gitBranchName;
$gitHash = file_get_contents($gitPathBranch);
$gitDate = date(DATE_ATOM, filemtime($gitPathBranch));
echo "version date: ".$gitDate."<br>branch: ".$gitBranchName."<br> commit: ".$gitHash;
?>
示例输出:
版本日期:2018-10-31T23:52:49+01:00
分支:dev
提交:2 a52054ef38ba4b76d2c14850fa81ceb25847bab
文件refs/heads/your_branch
的修改日期是(可接受的)上次提交日期的近似值(特别是对于我们假设将部署新提交(没有旧提交)的测试/staging环境)
我使用这个函数:
protected function getGitBranch()
{
$shellOutput = [];
exec('git branch | ' . "grep ' * '", $shellOutput);
foreach ($shellOutput as $line) {
if (strpos($line, '* ') !== false) {
return trim(strtolower(str_replace('* ', '', $line)));
}
}
return null;
}
PHP中的简单方法:
-
短散列:
$rev = exec('git rev-parse --short HEAD');
-
全散列:
$rev = exec('git rev-parse HEAD');
为了扩展program247365的答案,我为此编写了一个助手类,这对使用Git Flow的人也很有用。
class GitBranch
{
/**
* @var string
*/
private $branch;
const MASTER = 'master';
const DEVELOP = 'develop';
const HOTFIX = 'hotfix';
const FEATURE = 'feature';
/**
* @param 'SplFileObject $gitHeadFile
*/
public function __construct('SplFileObject $gitHeadFile)
{
$ref = explode("/", $gitHeadFile->current(), 3);
$this->branch = rtrim($ref[2]);
}
/**
* @param string $dir
*
* @return static
*/
public static function createFromGitRootDir($dir)
{
try {
$gitHeadFile = new 'SplFileObject($dir.'/.git/HEAD', 'r');
} catch ('RuntimeException $e) {
throw new 'RuntimeException(sprintf('Directory "%s" is not a Git repository.', $dir));
}
return new static($gitHeadFile);
}
/**
* @return string
*/
public function getName()
{
return $this->branch;
}
/**
* @return boolean
*/
public function isBasedOnMaster()
{
return $this->getFlowType() === self::HOTFIX || $this->getFlowType() === self::MASTER;
}
/**
* @return boolean
*/
public function isBasedOnDevelop()
{
return $this->getFlowType() === self::FEATURE || $this->getFlowType() === self::DEVELOP;
}
/**
* @return string
*/
private function getFlowType()
{
$name = explode('/', $this->branch);
return $name[0];
}
}
你可以这样使用:
echo GitBranch::createFromGitRootDir(__DIR__)->getName();
Git Library in PHP (GLIP)是一个用于与Git库交互的PHP库。它不需要在服务器上安装Git,可以在GitHub上找到。
如果您在repo的任何子目录中,则可以使用此快捷选项:
$dir = __DIR__;
exec( "cd '$dir'; git br", $lines );
$branch = '';
foreach ( $lines as $line ) {
if ( strpos( $line, '*' ) === 0 ) {
$branch = ltrim( $line, '* ' );
break;
}
}
主旨:https://gist.github.com/reiaguilera/82d164c7211e299d63ac
<?php
// forked from lukeoliff/QuickGit.php
class QuickGit {
private $version;
function __construct() {
exec('git describe --always',$version_mini_hash);
exec('git rev-list HEAD | wc -l',$version_number);
exec('git log -1',$line);
$this->version['short'] = "v1.".trim($version_number[0]).".".$version_mini_hash[0];
$this->version['full'] = "v1.".trim($version_number[0]).".$version_mini_hash[0] (".str_replace('commit ','',$line[0]).")";
}
public function output() {
return $this->version;
}
public function show() {
echo $this->version;
}
}
读取.git/HEAD
文件的给定解决方案导致我的管道失败。
受这个问题的启发:如何通过编程确定当前签出的Git分支
你可以通过以下命令获取git分支名称:
$gitBranchName = trim(shell_exec("git rev-parse --abbrev-ref HEAD"));
为什么不这样呢?它可能只会在模板文件中出现一次,仅供开发人员查看。
Currently on git branch: <?php echo `git branch --show-current`; ?>