windows - 如何使用 Windows cmd/batch 从 git 命令输出设置变量

标签 windows git batch-file atlassian-sourcetree

我无法将输出“转发”到变量。 我在我的 GIT 版本控制中使用 Atlassian SourceTree。我想在那里创建一个自定义操作,通过它我可以轻松地创建一个包含一次提交的所有修改文件的 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" )

我收到以下错误:

fatal: ambiguous argument '%ci': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 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"

==>

关于windows - 如何使用 Windows cmd/batch 从 git 命令输出设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29803777/

相关文章:

Git,如何删除路径中带有有趣字符的文件?

git - 如何在git中删除单个文件的所有痕迹

windows - 以相反顺序循环目录中的文件

batch-file - 批处理 : Get the full path of the folder in the previous directory?

java - 我可以使用 JavaFX 向我的程序添加更多开始菜单项吗?

windows - 如何在 Windows 上使用 GUI 添加或删除 git 标签?

windows - 应用程序错误: fault address 0x00012afb (Expert)

windows - 如何提高高延迟网络上的 RPC 数据吞吐量

php - 如何将 PhpCsFixer 限制为暂存文件?

windows - Apache Ant : Terminate process started by <exec> when ant process is terminated