这是我的情况:
我在我的 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/