如何在我的开发网站的页面顶部显示当前的git分支名称?

pro*_*365 23 php git

这是我的情况:

我使用MAMP(PHP)在我的Mac上本地开发.我的网站受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)
Run Code Online (Sandbox Code Playgroud)

任何人都有我可以使用的示例代码,它在.git文件夹中查看并查看当前分支是什么,并将其输出到index.php页面(以及RoR dev的ruby解决方案)?当我切换分支时,这将非常有用,当我刷新时,在我的浏览器中,我看到我会在页面顶部的'master'或'your-topic-branch-name-here'.

我愿意使用以PHP编程方式访问git的第三方库,或者从.git中的磁盘上的文件中获取正确"当前分支"变量的东西.

pro*_*365 48

这在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
Run Code Online (Sandbox Code Playgroud)

  • 作为onliner`<?php echo implode('/',array_slice(explode('/',file_get_contents('.git/HEAD')),2)); ?>` (4认同)
  • 如果分支名称包含"/",则这不太有效:大多数git流功能都有.你将不得不切割数组,然后再次内爆:$ explosionstring = array_slice($ explosionstring,2); $ branchname = implode("/",$ explosionstring); (2认同)

Kam*_*ski 8

分支、上次提交日期和哈希

<?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;                                                       
?>
Run Code Online (Sandbox Code Playgroud)

示例输出:

版本日期:2018-10-31T23:52:49+01:00

分支:开发

提交:2a52054ef38ba4b76d2c14850fa81ceb25847bab

文件的修改日期refs/heads/your_branch是(可接受的)上次提交日期的近似值(特别是对于我们假设我们将部署新提交(没有旧提交)的测试/暂存环境)。


Rya*_*yan 6

我用这个函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 可以变得更容易“git分支--show-current” (2认同)

Ser*_*ues 5

PHP中的简单方法:

  • 短哈希: $rev = exec('git rev-parse --short HEAD');
  • 完整哈希: $rev = exec('git rev-parse HEAD');