windows下bat脚本查看git是否存在某个分支名称或者tag名称,linux同理

@echo off

set /p branchName=Please input branch or tag name :

rem 这里我用的是当前目录, 可以换成自己任意工作目录
set workDir=%~dp0
call:checkFunc "%workDir%" "%branchName%"

echo.&pause&goto:eof

:printFunc
    echo.
    echo *************************************************
    echo %~1
    echo *************************************************
    echo.
    goto:eof

rem 功能是本地与远程分支关联, 这样就可以使用简短语句,如: git pull/git push...
rem 如果已经关联,此功能代码可以注释掉
:setUpstream
    cd /d "%~1"
    for /f "tokens=1" %%m in ('git rev-parse --abbrev-ref head') do (
        git branch --set-upstream-to=origin/%%m
    )
    goto:eof

:fetchFunc
    git fetch
    goto:eof
    
:checkFunc
    cd /d "%~1"
    Call:setUpstream "%~1"
    Call:fetchFunc
    
    for /f "tokens=1" %%a in ('git rev-parse --abbrev-ref head') do (
        if "%%a" equ "%~2" (
            Call:printFunc "Branch: %~2 exist!"
            goto:eof
        )
    )
    
    rem check branch
    rem 考虑到可能要查找本地未上传到远程的代码分支, 这里用到参数-a, 如果不需要
    rem 考虑这种情况, 可以换成 -r, 那就只需要判断if "%%x" equ "origin/%~2"就可以了
    for /f "tokens=1" %%x in ('git branch -a ^|findstr %~2') do (
        if "%%x" equ "%~2" (
            Call:printFunc "Branch: %%x exist!"
            goto:eof
        ) else if "%%x" equ "origin/%~2" (
            Call:printFunc "Branch: %%x exist!"
            goto:eof
        ) else if "%%x" equ "remotes/origin/%~2" (
            Call:printFunc "Branch: %%x exist!"
            goto:eof
        )
    )
    
    rem check tag
    for /f "tokens=1" %%x in ('git tag ^|findstr %~2') do (
        if "%%x" equ "%~2" (
            Call:printFunc "Tag: %%x exist!"
            goto:eof
        )
    )
    
    Call:printFunc "%~2: branch or tag name cannot be found"
    goto:eof

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值