gitbatch-filegit-describe

Git describe conditional fatal error in FOR /F command


This is a weird one. The code below works as expected inside a batch file:

FOR /F "tokens=1,2,3,* delims=-" %%G IN (
    'git describe --long --always --dirty --broken'
) do (
    set tag_name=%%G
    set versions_from_tag=%%H
    set hash=%%I
    set dirty_broken=%%J
)

When I try to add the --abbrev option I get a fatal error:

FOR /F "tokens=1,2,3,* delims=-" %%G IN (
    'git describe --long --always --dirty --broken --abbrev=8'
) do (
    set tag_name=%%G
    set versions_from_tag=%%H
    set hash=%%I
    set dirty_broken=%%J
)

outputs:fatal: --dirty is incompatible with commit-ishes

but if I run the command outside of the FOR /F it works as expected:

git describe --long --always --dirty --broken --abbrev=8

outputs 2.11-13-ga03306e6-dirty

I'm assuming this probably has something to do with the environment of the FOR /F command?


Solution

  • Thanks to elzooilogico.

    As detailed here, the = needs to be escaped like ^= when used in the subject of the FOR /F command.

    Without the escape I think the 8 was being treated as the commit-ish string.

    FOR /F "tokens=1,2,3,* delims=-" %%G IN (
        'git describe --long --always --dirty --broken --abbrev^=8'
    ) do (
        set tag_name=%%G
        set versions_from_tag=%%H
        set hash=%%I00
        set dirty_broken=%%J
    )
    

    Worked as expected.