Home > front end >  Baffling windows command behavior (for /F...)
Baffling windows command behavior (for /F...)

Time:09-03

I'm trying to write a batch script that checks for local mods in a git repo. I just need a yes/no output but I want to exclude untracked files.

Here's what I see at the command line when I include untracked files:

C:\local_workspace>git status --porcelain
 M EWARM/stm32u575xx_flash.icf
 M EWARM/stm32u575xx_sram.icf
?? EWARM/bui.h
?? EWARM/gen_build_info_header_git.bat
?? EWARM/test.h
?? EWARM/test2.h

I can work with that using the for command:

C:\local_workspace>for /F "delims=" %i in ('git status --porcelain') do (echo [%i])

C:\local_workspace>(echo [ M EWARM/stm32u575xx_flash.icf] )
[ M EWARM/stm32u575xx_flash.icf]

C:\local_workspace>(echo [ M EWARM/stm32u575xx_sram.icf] )
[ M EWARM/stm32u575xx_sram.icf]

C:\local_workspace>(echo [?? EWARM/bui.h] )
[?? EWARM/bui.h]

C:\local_workspace>(echo [?? EWARM/gen_build_info_header_git.bat] )
[?? EWARM/gen_build_info_header_git.bat]

C:\local_workspace>(echo [?? EWARM/test.h] )
[?? EWARM/test.h]

C:\local_workspace>(echo [?? EWARM/test2.h] )
[?? EWARM/test2.h]

Here's what I see if I exclude tracked files:

C:\local_workspace>git status --porcelain --untracked-files=no
 M EWARM/stm32u575xx_flash.icf
 M EWARM/stm32u575xx_sram.icf

Now let's try for:

C:\local_workspace>for /F "delims=" %i in ('git status --porcelain --untracked-files=no') do (echo [%i])

C:\local_workspace>

There's no output! Why not?

CodePudding user response:

for loops treat = as whitespace, so for all intents and purposes, your code is trying to process the command git status --porcelain --untracked-files no, which is invalid.

In order to preserve the =, it needs to be escaped with a ^: git status --porcelain --untracked-files^=no

  • Related