Jon*_*age 4 windows git batch-file
我有一个 git 命令来获取当前存储库的最新 SHA,如下所示:
git log --pretty=format:"%H" -n 1
Run Code Online (Sandbox Code Playgroud)
我有一个 Windows 批处理脚本,我想按如下方式使用它:
SET CURRENT_SHA=???
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将从 git 调用的输出获取到变量中,以便我可以使用它。
编辑
我尝试了以下方法(这似乎是我在这里和其他地方读到的一般建议):
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=* USEBACKQ" %%i IN (`git log --pretty=format:"%H" -n 1`) DO (SET CURRENT_SHA=%%i)
ECHO Current Sha: %CURRENT_SHA%
Run Code Online (Sandbox Code Playgroud)
..但我得到:
fatal: failed to stat 'format:i) ECHO Current Sha: 48bce83e800b96607afb2a387c4fcd7b0b0f037e
Run Code Online (Sandbox Code Playgroud)
那么引号可能有问题吗?
我没有方便测试的 Windows 系统,但我认为有以下几点:
FOR /F %i IN (`git log --pretty=format:"%%H" -n 1`) DO SET CURRENT_SHA=%i
Run Code Online (Sandbox Code Playgroud)
请注意,“%H”需要转义,但要在批处理文件中使用此行,您还需要对所有内容进行双重转义。您可能还需要使用 转义双引号^
。我认为这应该有效:
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=* USEBACKQ" %%a in (`git log --pretty^=format:"%%H" -n 1`) do (SET CURRENT_SHA=%%a)
ECHO Current Sha: %CURRENT_SHA%
Run Code Online (Sandbox Code Playgroud)
但实际上,如果你想在 Windows 中进行 shell 编程,只需使用 Powershell,然后你可以这样做:
$CURRENT_SHA=git log --pretty=format:"%H" -n 1
Run Code Online (Sandbox Code Playgroud)