[英]How to set variable from git command output using windows cmd/batch
我无法将输出“转发”到变量。 我正在将Atlassian SourceTree与我的GIT版本一起使用。 我想在那里创建一个自定义操作,通过该操作,我可以轻松地创建一次提交的所有修改文件的zip文件。 创建的文件的文件名中应具有提交时间的格式化日期,格式为update-<YYYYmmdd>T<hhMM>.zip
。
到目前为止,我的工作批处理文件(行号供以后参考) :
1: setlocal enabledelayedexpansion
2: set output=
3: for /f "delims=" %%a in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%a" )
4: set hours=
5: git show -s --format=%%ci %1 > tmptmptmp & set /p hours= < tmptmptmp & del tmptmptmp
6: set hours=%hours:~0,4%%hours:~5,2%%hours:~8,2%T%hours:~11,2%%hours:~14,2%
7: git archive -o update-%hours%.zip HEAD %output%
8: endlocal
我在重写第5行(小时行)时遇到麻烦,以避免创建临时文件。 如果我像第3行(输出行)那样做:
for /f "delims=" %%a in ('git show -s --format=%%ci %1^^') do ( set hours=!hours! "%%a" )
我收到以下错误:
致命:参数'%ci'模棱两可:未知修订或路径不在工作树中。 使用'-'将路径与修订分开,例如:
git <command> [<revision>...] -- [<file>...]
我认为这与所需的%ci
参数有关(用于格式化输出)。
有人可以帮我吗?
我不知道为什么在第5行git show -s --format=%%ci %1
使用的同一命令上添加尾随^
插入符 ( 抑扬音 )。
但是,-- --format=%%ci
的=
等号应按以下方式转义:
for /f "delims=" %%a in ('git show -s --format^=%%ci %1^^') do set hours=!hours! "%%a"
证明样本 :
==>type 29803777.bat
IF NOT [%1]==[] dir /B %1=
for /f "delims=" %%a in ('IF NOT [%1]==[] dir /B %1=') do @echo "%%a"
for /f "delims=" %%a in ('IF NOT [%1]^=^=[] dir /B %1^=') do @echo "%%a"
==>29803777.bat x
==>IF NOT [x] == [] dir /B x=
x.txt
==>for /F "delims=" %a in ('IF NOT [x] [] dir /B x ') do @echo "%a"
[] was unexpected at this time.
==>for /F "delims=" %a in ('IF NOT [x]==[] dir /B x=') do @echo "%a"
"x.txt"
==>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.