3

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 update-<YYYYmmdd>T<hhMM>.zip.

My working batch-file so far (line numbers for later reference):

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?

2
  • try with %c.Not sure if in this case the % should be escaped. Commented Apr 22, 2015 at 16:48
  • But %c ist not a valid parameter for --format. git show -s --format=%ci <commit-id> returns an iso formatted (I know, not strict) date string of the commits timestamp. Your suggenstion only returns "%c" if used in git bash. Commented Apr 23, 2015 at 9:18

1 Answer 1

1

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.

However, the = 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"

==>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.