当前位置: 首页 > 工具软件 > Git.php > 使用案例 >

PHP得到当前GIT分支的代码

商冠玉
2023-12-01

由于研发,测试,生产环境不同,反馈问题经常不能正确定位环境,可在WEB界面的显著位置中显示当前的环境 

原理为读取.git目录 HEAD文件可获取当前分支。

$ cat .git/HEAD

ref: refs/heads/mytest



   public static function getGitinfo()

    {


        if (\App::environment() == "product") {
            $host = '生产';


        } elseif (\App::environment() == "testing") {
            $host = '测试';
        } else {
            $host = '本机';
        }


        //获取分支名
        $branch = @file_get_contents(base_path() . '/.git/HEAD');
        if (!empty($branch)) {
            $branch = trim($branch);
            $i      = strripos($branch, '/');
            $branch = substr($branch, $i + 1);
        }


        return $host . "【" . $branch . "】";


    }




//非生产才显示

@if (!App::environment()=='production' )


<script>
var content = '{{ getGitinfo() }}      ';
var div = document.createElement("div");
div.style = "font-size:24px; position: absolute;background:#ee6666;padding-left:20px;";
div.innerHTML = content;
var first=document.body.firstChild;
document.body.insertBefore(div,first);
</script>
@endif

 类似资料: