首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我如何在我的开发网站的页面顶部显示当前的git分支名称?

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

Stack Overflow用户
提问于 2011-09-17 00:01:51
回答 9查看 23.5K关注 0票数 27

这是我的情况:

我在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解决方案)?当我切换分支时,这将是非常有用的,当我刷新时,在我的浏览器中,我看到我将在页面顶部的‘’上,或'your-topic-branch-name-here'.

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

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2011-09-17 01:07:57

这对我在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
票数 55
EN

Stack Overflow用户

发布于 2018-11-08 17:59:38

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

<?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

分支:开发

提交: 2a52054ef38ba4b76d2c14850fa81ceb25847bab

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

票数 8
EN

Stack Overflow用户

发布于 2014-09-11 05:33:07

我使用这个函数:

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;
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7447472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档