0

这是我的 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并且是空的。remoteCommitIdSET

我猜git命令的功能与问题无关,因为如果我将它们替换为ver.

4

1 回答 1

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%
于 2013-02-21T17:01:07.170 回答