批处理文件将 git 提交的更改文件写入变量

batch file writing changed files of a git commit into variables

我想编写一个批处理脚本(并通过 cmd 运行 编写),它将特定 git 提交的更改文件写入变量。

我得到了以下代码:

@ECHO OFF
SetLocal EnableDelayedExpansion
set count=1
for %%m in ('git diff-tree --no-commit-id --name-only -r sha1-hash') do (
        set var!count!=%%m 
        set /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ENDLOCAL

var1 和 var2 的回显返回: 'git 对于 var1 和 var2

的差异树

如果我加上 echo %var3% echo %var4% 等等,结果如下: 'git diff-tree --no-commit-id --name-only -r sha1-hash' 回声关闭 回声关闭 回声关闭...

所以我想我的 git diff-tree...-command 不被视为命令。试图自己修复它,但不得不投降...你能帮我吗?

非常感谢!

  • 使用 /F 标志循环遍历文本中的标记。
  • 使用 /L 标志循环遍历一系列数字。

示例:

@echo off
SetLocal EnableDelayedExpansion

set REVISION=HEAD
set count=0
for /F "tokens=*" %%m in ('git diff-tree --no-commit-id --name-only -r %REVISION%') do (
    set /a count=!count!+1
    set var!count!=%%m
)

for /L %%i in (1, 1, %count%) do (
    echo %%i: !var%%i!
)

详情请看这里:https://ss64.com/nt/for.html