2015-04-22 66 views
2

我無法將輸出「轉發」到變量。 我正在使用Atlassian SourceTree和我的GIT版本控制。在那裏,我想創建一個自定義操作,我可以輕鬆創建一個提交的所有修改文件的zip文件。創建的文件的文件名格式化日期應該爲提交時間update-<YYYYmmdd>T<hhMM>.zip如何使用windows cmd /批處理從git命令輸出設置變量

我的工作批處理文件至今(行號以供日後參考)

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參數(用於格式化輸出)。

有人可以幫助我嗎?

+0

嘗試%c.Not知道,如果在這種情況下,該%應該逃脫。 – npocmaka

+0

但是*'%c' * ist不是'--format'的有效參數。 'git show -s --format =%ci '返回提交時間戳的一個iso格式(我知道,不是嚴格的)日期字符串。如果在git bash中使用,你的suggenstion只返回「%c」。 – Seika85

回答

0

我不知道你爲什麼要添加尾隨^插入符(一抑揚口音)在第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" 

==> 
相關問題