How to set variable from git command output using windows cmd/batch
I have trouble “forwarding” an output to a variable.
I’m using Atlassian SourceTree with my GIT versioning. There I want to create a custom action, with which I can easily create a zip file of all modified files of one commit. The created file should have a formatted date of the commit time in the filename as
My working batch-file so far (line numbers for later reference):
- “permission denied” while removing git directory
- How to install go-sql-driver on Windows
- Git Extensions usable with SVN repos?
- Using Git as a source control for webdevelopment and multiple environment
- Why is “MINGW64” appearing on my Git bash?
- PHP SVN update - TortoiseSVN
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
I’m having trouble to rewrite line 5 (hours-line) to avoid creating a temporary file.
If I do the same as in line 3 (output-line) like:
for /f "delims=" %%a in ('git show -s --format=%%ci %1^^') do ( set hours=!hours! "%%a" )
I get the following error:
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>...]
I assume this has something to do with the needed
%ci parameter (for formatting the output).
Can somebody help me with this?
One Solution collect form web for “How to set variable from git command output using windows cmd/batch”
I do not know why do you add trailing
^ caret (a circumflex accent) over the same command used in line 5
git show -s --format=%%ci %1.
= equals sign in
--format=%%ci should be escaped as follows:
for /f "delims=" %%a in ('git show -s --format^=%%ci %1^^') do set hours=!hours! "%%a"
A proof sample:
==>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" ==>