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

标签 php git

这是我的情况:

我在我的 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 dev 的 ruby​​ 解决方案)?这在我切换分支时非常有用,并且在我的浏览器中刷新时,我看到我会在页面顶部的“master”上,或者“你的主题分支名称这里” .

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

最佳答案

这在 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 - 如何在我的开发网站的页面顶部显示当前的 git 分支名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7447472/

相关文章:

php - 如何在 WordPress 中为其他子网站共享 WP_POST

javascript - 页面刷新后 OnChange 选项保持不变

git:未注释的标签是否比带有错误注释的标签更糟糕?

包含 "key : value"项的 php 搜索数组

javascript - 如何阻止页面之间简单的 javascript 冲突?

php - 单次插入大数据还是每次循环插入大数据?

git - VSTS : How to get all linked work items since last successful release to production?

git - 在本地跟踪文件,但绝不允许将它们推送到远程存储库

git - 为什么 "git log --tags=<pattern>"会在输出中添加 "refs/tags"?

git - 如何使用 tortoisegit 执行 rebase (squash)