精华内容
下载资源
问答
  • git 获取当前分支名

    千次阅读 2018-03-16 11:06:48
    git 获取当前分支名 git symbolic-ref --short -q HEAD 参考链接 https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git

    git 获取当前分支名

    git symbolic-ref --short -q HEAD

    参考链接
    https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git

    展开全文
  • git 获得当前分支名及其对应的远程分支 git symbolic-ref -q --short HEADgit rev-parse --abbrev-ref --symbolic-full-name @{u} 获得某个分支所对应的远程分支: git rev-parse --abbrev-ref master@{upstream}...

    git 获得当前分支名及其对应的远程分支

    git symbolic-ref -q --short HEAD
    git rev-parse --abbrev-ref --symbolic-full-name @{u}

    获得某个分支所对应的远程分支:

    git rev-parse --abbrev-ref master@{upstream}

    注意:@{u} 是 @{upstream} 的简写

    
    
     

    转载于:https://www.cnblogs.com/ibingshan/p/11162400.html

    展开全文
  • 最近编译一个Lib库的时候,因为团队规定上传Release版本的AAR到Maven的话,必须需要在Jenkins上编译而且Git Branch 必须是master分支才能够上传到Maven。 因此我们就需要在Gradle脚本中,获取Git Branch ,Git ...

    一、背景

    因为代码都迁移到了Gitlab,所以Jenkins编译的时候我们都需要将之前的SVN信息换成现在的Git信息。最近编译一个Lib库的时候,因为团队规定上传Release版本的AAR到Maven的话,必须需要在Jenkins上编译而且Git Branch 必须是master分支才能够上传到Maven。
    因此我们就需要在Gradle脚本中,获取Git Branch ,Git Commit等相关信息。但是在获取Git Branch的时候出现了问题,在本地Android Studio编译的时候能够获取到Git Branch的名字,但是使用Jenkins编译的时候,一直获取不到信息。

    下面是我写的一份gradle文件,用于获取Git和Jenkins的相关信息

    /**
     * 获取Git 分支名
     */
    def getGitBranch() {
        return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
    }
    
    /**
     * 获取Git 版本号
     */
    def getGitSHA() {
        return 'git rev-parse --short HEAD'.execute().text.trim()
    }
    
    /**
     * 获取Git Tag
     */
    def getGitTag() {
        return 'git describe --tags'.execute([], project.rootDir).text.trim()
    }
    
    /**
     * 获取Git 提交次数
     */
    def getGitCommitCount() {
        return 100 + Interger.parse('git rev-list --count HEAD'.execute([], project.rootDir).text.trim())
    }
    
    /**
     * 判断是否有jenkins
     */
    boolean isInJenkins() {
        Map<String, String> map = System.getenv()
        if (map == null) {
            return false
        }
        String str = map.get("Path")
        if (str != null) {
            //it's windows
            return false
        } else {
            str = ""
            Iterator it = map.iterator()
            while (it.hasNext()) {
                str += it.next()
            }
            return str.contains("jenkins")
        }
    }
    /**
     * 获取jenkins任务名
     */
    def getJenkinsName() {
        boolean flag = isInJenkins()
        if (flag) {
            ext.env = System.getenv()
            ext.name = env.JOB_URL
            String[] stringArray = ext.name.split("/")
            if (stringArray.length > 0) {
                return stringArray[stringArray.length - 1]
            } else {
                return "Local"
            }
        } else {
            return "Local"
        }
    }
    
    /**
     * 获取Jenkins Build 号
     * @return
     */
    def getJenkinsBuildCode() {
        boolean flag = isInJenkins()
        if (flag) {
            ext.env = System.getenv()
            ext.buildNumber = env.BUILD_NUMBER?.toInteger()
            return "$buildNumber"
        } else {
            return 0
        }
    }
    
    /**
     * 定义几个变量,在build.gradle里面引用
     */
    ext {
        gitTag = getGitTag()
        gitBranch = getGitBranch()
        gitSHA = getGitSHA()
        jenkinsRevision = getJenkinsBuildCode()
        jenkinsName = getJenkinsName()
    }
    

    其中的方法,getGitBranch方法在Android Studio编译的时候,能够正常获取到Git分支名。

        println "pom_version_type = " + pom_version_type
        println "jenkinsName = " + jenkinsName
        println "gitBranch = " + gitBranch

    我在进行编译的时候,是会通过如上代码打印出Git Branch的信息。

    在Android Studio 本地编译的时候,是可以打印出相关的信息的。

    这里写图片描述

    但是在Jenkins编译的时候,是不能够上传的,如下所示:
    这里写图片描述

    二、解决方法

    后来我尝试找了很多种方法去获取Git Branch的名字,在Android Studio本地都可以获取到,如下所示:

    参考链接:https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git

    方法1、git symbolic-ref --short -q HEAD

    D:\GitLab Source\XTCLint>git symbolic-ref --short -q HEAD
    master
    
    D:\GitLab Source\XTCLint>
    

    这里写图片描述

    方法2、git rev-parse --abbrev-ref HEAD

    D:\GitLab Source\XTCLint>git rev-parse --abbrev-ref HEAD
    master
    

    这里写图片描述

    方法3、git branch | grep \* | cut -d ' ' -f2

    D:\GitLab Source\XTCLint>git branch | grep \* | cut -d ' ' -f2
    master
    

    这里写图片描述

    方法4、git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"

    D:\GitLab Source\XTCLint>git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
    master
    

    这里写图片描述

    以上所有的方法,仅仅在Android Studio的终端或者本地gradle代码中有效,然而在Jenkins服务器编译的时候都是获取为空。

    后来我查看了Jenkins的Git插件上的介绍,参考链接:https://wiki.jenkins.io/display/JENKINS/Git+Plugin

    这里写图片描述

    如上所示,在上面的链接中有介绍,有几个Environment variables环境变量可以使用。

    Environment variables

    The git plugin sets several environment variables you can use in your scripts:

    • GIT_COMMIT - SHA of the current
    • GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, e.g. “origin/master” or “origin/foo”
    • GIT_LOCAL_BRANCH - Name of the branch on Jenkins. When the “checkout to specific local branch” behavior is configured, the variable is published. If the behavior is configured as null or **, the property will contain the resulting local branch name sans the remote name.
    • GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)
    • GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch.
    • GIT_URL - Repository remote URL
    • GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2
    • GIT_AUTHOR_NAME and GIT_COMMITTER_NAME - The name entered if the “Custom user name/e-mail address” behaviour is enabled; falls back to the value entered in the Jenkins system config under “Global Config user.name Value” (if any)
    • GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL - The email entered if the “Custom user name/e-mail address” behaviour is enabled; falls back to the value entered in the Jenkins system config under “Global Config user.email Value” (if any)

    然后我将这几个变量,在一个app的Jenkins任务中,编译完成后的邮件内容中添加了这几个变量的内容,如下所示:

    这里写图片描述

    在构建后的操作中,Editable Email Notification的邮件通知中,将邮件内容改为如下所示的代码。

    $DEFAULT_CONTENT
    
    <br />
    <font color="#0B610B">单元测试</font>
      <li>Launcher单元测试报告&nbsp;:<a href="${BUILD_URL}testReport">点击查看测试报告</a></li>
      <li>Launcher代码覆盖率&nbsp;:<a href="${BUILD_URL}jacoco">点击查看代码覆盖率</a></li>
      <li>Launcher Android Lint&nbsp;:<a href="${BUILD_URL}androidLintResult">点击查看Android Lint</a></li>
    
    
    <br />
     <li>GIT_COMMIT&nbsp;:${GIT_COMMIT}</a></li>
     <li>GIT_BRANCH&nbsp;:${GIT_BRANCH}</a></li>
     <li>GIT_LOCAL_BRANCH&nbsp;:${GIT_LOCAL_BRANCH}</a></li>
     <li>GIT_PREVIOUS_COMMIT&nbsp;:${GIT_PREVIOUS_COMMIT}</a></li>
     <li>GIT_PREVIOUS_SUCCESSFUL_COMMIT&nbsp;:${GIT_PREVIOUS_SUCCESSFUL_COMMIT}</a></li>
     <li>GIT_URL&nbsp;:${GIT_URL}</a></li>
     <li>GIT_URL_N&nbsp;:${GIT_URL_N}</a></li>
     <li>GIT_AUTHOR_NAME&nbsp;:${GIT_AUTHOR_NAME}</a></li>
     <li>GIT_COMMITTER_NAME&nbsp;:${GIT_COMMITTER_NAME}</a></li>
     <li>GIT_AUTHOR_EMAIL&nbsp;:${GIT_AUTHOR_EMAIL}</a></li>
     <li> GIT_COMMITTER_EMAIL&nbsp;:${ GIT_COMMITTER_EMAIL}</a></li>
    

    这样编译完后,收到的邮件内容如下:
    这里写图片描述

    如上所示,收到的邮件内容包含了Git的相关信息:

    GIT_COMMIT118fa74e6a09c8c5ae713523692add256bfa6afb
    GIT_BRANCH :origin/feature/UseByAnonymousDBMigrateAndApiChange
    GIT_LOCAL_BRANCH${GIT_LOCAL_BRANCH}
    GIT_PREVIOUS_COMMIT118fa74e6a09c8c5ae713523692add256bfa6afb
    GIT_PREVIOUS_SUCCESSFUL_COMMIT118fa74e6a09c8c5ae713523692add256bfa6afb
    GIT_URL :git@172.28.1.116:Android/WatchApp/Third/NetEaseCloudMusic.git
    GIT_URL_N${GIT_URL_N}
    GIT_AUTHOR_NAME${GIT_AUTHOR_NAME}
    GIT_COMMITTER_NAME${GIT_COMMITTER_NAME}
    GIT_AUTHOR_EMAIL${GIT_AUTHOR_EMAIL}
    GIT_COMMITTER_EMAIL${ GIT_COMMITTER_EMAIL}

    其中,GIT_BRANCH这个环境变量的值为origin/feature/UseByAnonymousDBMigrateAndApiChange,代表Jenkins上/UseByAnonymousDBMigrateAndApiChange分支远程Gitlab上该分支映射的远程分支。因此我们可以对GIT_BRANCH这个环境变量做做文章。

    将之前gradle脚本中的getGitBranch方法,做如下修改,区分编译环境是Jenkins还是本地。环境不同,运行不同的脚本获取Git Branch的名字。当处于Jenkins环境的时候,先通过GIT_BRANCH这个环境变量获取到Jenkins拉下来的分支对应的远程分支,然后通过字符串分离,获取到分支名。

    /**
     * 获取Git 分支名
     *
     *参考Jenkins git 创建文档: https://wiki.jenkins.io/display/JENKINS/Git+Plugin
     *   Environment variables
    
     The git plugin sets several environment variables you can use in your scripts:
    
     GIT_COMMIT - SHA of the current
     GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, e.g. "origin/master" or "origin/foo"
     GIT_LOCAL_BRANCH - Name of the branch on Jenkins. When the "checkout to specific local branch" behavior is configured, the variable is published.  If the behavior is configured as null or **, the property will contain the resulting local branch name sans the remote name.
     GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)
     GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch.
     GIT_URL - Repository remote URL
     GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2
     GIT_AUTHOR_NAME and GIT_COMMITTER_NAME - The name entered if the "Custom user name/e-mail address" behaviour is enabled; falls back to the value entered in the Jenkins system config under "Global Config user.name Value" (if any)
     GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL - The email entered if the "Custom user name/e-mail address" behaviour is enabled; falls back to the value entered in the Jenkins system config under "Global Config user.email Value" (if any)
     *
     *
     */
    def getGitBranch() {
        //判断是否处于Jenkins编译环境
        boolean flag = isInJenkins()
        if (flag) {
            ext.env = System.getenv()
            ext.gitBranch = env.GIT_BRANCH
            String[] stringArray = ext.gitBranch.split("/")
            if (stringArray.length > 0) {
                return stringArray[stringArray.length - 1]
            } else {
                return "UnKnown Branch"
            }
        } else {
            return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
        }
    }

    完整代码如下所示:

    
    
    /**
     * 获取Git 分支名
     *
     *参考Jenkins git 创建文档: https://wiki.jenkins.io/display/JENKINS/Git+Plugin
     *   Environment variables
    
     The git plugin sets several environment variables you can use in your scripts:
    
     GIT_COMMIT - SHA of the current
     GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, e.g. "origin/master" or "origin/foo"
     GIT_LOCAL_BRANCH - Name of the branch on Jenkins. When the "checkout to specific local branch" behavior is configured, the variable is published.  If the behavior is configured as null or **, the property will contain the resulting local branch name sans the remote name.
     GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)
     GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch.
     GIT_URL - Repository remote URL
     GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2
     GIT_AUTHOR_NAME and GIT_COMMITTER_NAME - The name entered if the "Custom user name/e-mail address" behaviour is enabled; falls back to the value entered in the Jenkins system config under "Global Config user.name Value" (if any)
     GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL - The email entered if the "Custom user name/e-mail address" behaviour is enabled; falls back to the value entered in the Jenkins system config under "Global Config user.email Value" (if any)
     *
     *
     */
    def getGitBranch() {
        //判断是否处于Jenkins编译环境
        boolean flag = isInJenkins()
        if (flag) {
            ext.env = System.getenv()
            ext.gitBranch = env.GIT_BRANCH
            String[] stringArray = ext.gitBranch.split("/")
            if (stringArray.length > 0) {
                return stringArray[stringArray.length - 1]
            } else {
                return "UnKnown Branch"
            }
        } else {
            return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
        }
    }
    
    
    /**
     * 获取Git 版本号
     */
    def getGitSHA() {
        return 'git rev-parse --short HEAD'.execute().text.trim()
    }
    
    /**
     * 获取Git Tag
     */
    def getGitTag() {
        return 'git describe --tags'.execute([], project.rootDir).text.trim()
    }
    
    /**
     * 获取Git 提交次数
     */
    def getGitCommitCount() {
        return 100 + Interger.parse('git rev-list --count HEAD'.execute([], project.rootDir).text.trim())
    }
    
    /**
     * 判断是否有jenkins
     */
    boolean isInJenkins() {
        Map<String, String> map = System.getenv()
        if (map == null) {
            return false
        }
        String str = map.get("Path")
        if (str != null) {
            //it's windows
            return false
        } else {
            str = ""
            Iterator it = map.iterator()
            while (it.hasNext()) {
                str += it.next()
            }
            return str.contains("jenkins")
        }
    }
    /**
     * 获取jenkins任务名
     */
    def getJenkinsName() {
        boolean flag = isInJenkins()
        if (flag) {
            ext.env = System.getenv()
            ext.name = env.JOB_URL
            String[] stringArray = ext.name.split("/")
            if (stringArray.length > 0) {
                return stringArray[stringArray.length - 1]
            } else {
                return "Local"
            }
        } else {
            return "Local"
        }
    }
    
    /**
     * 获取Jenkins Build 号
     * @return
     */
    def getJenkinsBuildCode() {
        boolean flag = isInJenkins()
        if (flag) {
            ext.env = System.getenv()
            ext.buildNumber = env.BUILD_NUMBER?.toInteger()
            return "$buildNumber"
        } else {
            return 0
        }
    }
    
    /**
     * 定义几个变量,在build.gradle里面引用
     */
    ext {
        gitTag = getGitTag()
        gitBranch = getGitBranch()
        gitSHA = getGitSHA()
        jenkinsRevision = getJenkinsBuildCode()
        jenkinsName = getJenkinsName()
    }
    

    现在测试下Jenkins编译是否正常,可以看到一切都正常了。

    这里写图片描述

    参考链接


    这里写图片描述

    作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
    转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77802596

    如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作!

    这里写图片描述

    展开全文
  • 1 git branch | grep "*" 2 git symbolic-ref --short -q HEAD

    1

    git branch | grep "*"

    2

    git symbolic-ref --short -q HEAD
    

    展开全文
  • linux bash 脚本获取git当前分支名

    万次阅读 2017-12-05 14:37:53
    linux bash 脚本获取git当前分支名 1、方法一 #!/bin/bash function git.branch { br=`git branch | grep "*"` echo ${br/* /} } git.branch 2、方法二 git symbolic-ref --short -q HEAD 3、测试脚本 #...
  • git获取当前分支名称

    2019-09-19 21:50:16
    git branch | sed -n '/\* /s///p' 本地分支:master 远程跟踪分支(同步) :(detached from origin/master) 远程跟踪分支(非同步) :(detached from origin/fea...
  • 主要介绍了Git获取本地分支对应的远端服务器分支名的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
  • shell:获取git当前分支或tag

    千次阅读 2019-09-01 11:14:07
    shell下如何获取git的当前所在分支名或tag呢? 为了解决这个小问题,我之前是走了弯路的,之前我简单的想法就是解析git branch输出的内容从中获取 分支或tag名。 如下代码, # 返回$1指定的git项目的当前分支(branch)...
  • 【注】~/.bashrc 是Linux的,对应到Mac OSX 是 ~/.bash_profile ,看网上的教程要注意区分和...contrib/completion/git-prompt.sh 高亮显示当前分支名称 2、执行以下命令,将两个文件复制到用户目录,并设置隐藏 cp
  • 如何获取 Git 当前分支名称

    千次阅读 2018-11-02 16:49:50
    之所以要获取 Git 分支名称,是因为最近在使用 Vue 的 vconsole。当然这个针对手机网页的前端开发者调试面板只需要在开发环境下使用,生产环境下自动过滤掉就行,那么就有了下面的代码。 if (process.env.NODE_...
  • 文章目录webhook给jenkins发了什么找对分支名了吗第一种方法调整分支第二种方法调整分支感谢 webhook给jenkins发了什么 gitlab触发jenkins的job是通过webhook完成的,也就是一次http请求,请求体在jenkins的job的...
  • git显示当前分支的父分支名称

    千次阅读 2019-09-03 12:06:27
    git显示当前分支的创建来源分支 git reflog --date=local --all | grep v1.0.1_dev git reflog show v1.0.1_dev 当前分支 git branch -vv # 显示本地分支和服务器分支的映射关系 git log --graph --decorate git ...
  • 文章目录问题重现:解决思路:实现:第一步:根据主分支(origin)代码在本地创建新的临时分支,命名为temp第二步:将临时分支(temp)合并到开发分支(feature/dev)第三步:解决合并后的冲突第四步:提交开发分支...
  • 本地Git分支名称记录在本地仓库...PowerShell中获取的方法为: $GitHEADFile = Get-Content '..\.git\HEAD' $GitHEADFileLine = [System.Environment]::NewLine $GitHEADFileContent = [String]::Join($GitHEADF
  • 菜单栏VCS-》选中Git 选择Fetch 获取最新分支 转载于:https://www.cnblogs.com/mengjianzhou/p/8572008.html
  • git branch | grep \"*\" | awk -F \"_\" '{print $2}' 转载于:https://www.cnblogs.com/fang9159/p/3772726.html

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 162,366
精华内容 64,946
关键字:

获取当前分支名