这是我的Windows批处理文件:
FOR /F %%a IN ('git rev-parse --abbrev-ref HEAD') DO SET branchName = %%a
FOR /F %%a IN ('git rev-list --max-count^=1 %branchName%') DO SET localCommitId = %%a
FOR /F %%a IN ('git rev-list --max-count^=1 origin/%branchName%') DO SET remoteCommitId = %%a
@ECHO branchName = %branchName%
@ECHO localCommitId = %localCommitId%
@ECHO remoteCommitId = %remoteCommitId%
这是我运行时看到的输出:
>FOR /F %a IN ('git rev-parse --abbrev-ref HEAD') DO SET branchName = %a
>SET branchName = develop
>FOR /F %a IN ('git rev-list --max-count=1 develop') DO SET localCommitId = %a
>SET localCommitId = e4375fa4753b3956e1454020a812cc7591cf606e
>FOR /F %a IN ('git rev-list --max-count=1 origin/develop') DO SET remoteCommitId = %a
>SET remoteCommitId = e4375fa4753b3956e1454020a812cc7591cf606e
branchName = develop
localCommitId =
remoteCommitId =
我不明白为什么localCommitId
和remoteCommitId
为空,尽管打印的SET
命令将它们设置为非空字符串。
我想git
命令的功能与问题无关,因为如果我用ver
替换它们,我会遇到同样的问题。
答案 0 :(得分:2)
我还会添加一些解释。如果您输入set branch
,则会看到branchName = develop
。这意味着变量实际上将使用%branchName %
调用,并且会给出值develop
更正了以下代码。
FOR /F %%a IN ('git rev-parse --abbrev-ref HEAD') DO SET branchName=%%a
FOR /F %%a IN ('git rev-list --max-count^=1 %branchName%') DO SET localCommitId=%%a
FOR /F %%a IN ('git rev-list --max-count^=1 origin/%branchName%') DO SET remoteCommitId=%%a
@ECHO branchName = %branchName%
@ECHO localCommitId = %localCommitId%
@ECHO remoteCommitId = %remoteCommitId%