user10205278
user10205278

Reputation: 43

batch file writing changed files of a git commit into variables

I would like to write a batch script (and run it via cmd) which writes the changed files of a specific git commit into variables.

I have got the following code:

@ECHO OFF
SetLocal EnableDelayedExpansion
set count=1
for %%m in ('git diff-tree --no-commit-id --name-only -r sha1-hash') do (
        set var!count!=%%m 
        set /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ENDLOCAL

The echo of var1 and var2 gives back: 'git for var1 and diff-tree for var2

If I add echo %var3% echo %var4% and so on, the result is the following: 'git diff-tree --no-commit-id --name-only -r sha1-hash' echo off echo off echo off...

So I guess my git diff-tree...-command is not seen as a command. Tried to fix it on my own, but had to surrender... Can you help me please?

Thanks a lot!

Upvotes: 4

Views: 1005

Answers (1)

sergej
sergej

Reputation: 17999

  • Use the /F flag to loop through tokens in text.
  • Use the /L flag to loop through a range of numbers.

Example:

@echo off
SetLocal EnableDelayedExpansion

set REVISION=HEAD
set count=0
for /F "tokens=*" %%m in ('git diff-tree --no-commit-id --name-only -r %REVISION%') do (
    set /a count=!count!+1
    set var!count!=%%m
)

for /L %%i in (1, 1, %count%) do (
    echo %%i: !var%%i!
)

See here for details: https://ss64.com/nt/for.html

Upvotes: 3

Related Questions