gitbatch-fileformatgit-describe

How to format "git describe" output in simple file with .bat script?


I want to get output in simple txt file inside git repo folder:

About:
date = (2022_06_04)
version = (1d34a1b1)

And I stuck with formatting data in ( ) brackets.

Here is my attempt:

@echo off
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%%
set dt=%dt: =0%
set wrap=About:
set file_name="vers.txt"
set git_cmd=git describe --abbrev=8 --always
echo %wrap% >%file_name%
echo.date = (%dt%)>>%file_name%
echo|set /p ="version = (" >>%file_name%
%git_cmd% >>%file_name%
echo.) >>%file_name%
%NL%>>%file_name% 

Problem is with ')' for git output formatting. Here is what I'm getting:

About: 
date = (2022_06_04)
version = (1d34a1b1
) 

I've tried to play with echo -n, but it doesn't work on Windows.

UPDATE

NL is just new line:

set NL=echo.

Here is example of command line with whole git command output:

USR@DESKTOP-N1 MINGW64 /c/project/prj (v1.1.2)<CR><LF>
$ git describe --abbrev=8 --always<CR><LF>
1d34a1b1<CR><LF>
<CR><LF>
USR@DESKTOP-N1 MINGW64 /c/project/prj (v1.1.2)<CR><LF>

And here is how my output text file looks like:

About:<CR><LF>
date = (2022_06_06)<CR><LF>
version = (1d34a1b1<LF>
) <CR><LF>

Seems <LF> appeared at some stage. For me it looks like <CR> was cutted but <LF> stayed because of some Windows standard formatting or something like that, I'm no sure.


Solution

  • Two possibilities leap to mind

    %git_cmd%|set /p ="version = (" >>%file_name%
    echo.) >>%file_name%
    

    and

    for /f "delims=" %%e in ('%git_cmd%') do echo version = (%%e)>>%file_name%
    

    but I've no git applications, so this is a guess...


    This worked for me to give the output desired:

    @ECHO Off
    SETLOCAL
    set dt=%date%
    set wrap=About:
    set file_name="u:\vers.txt"
    set git_cmd=q72496859s describe --abbrev=8 --always
    echo %wrap% >%file_name%
    echo.date = (%dt%)>>%file_name%
    for /f "delims=" %%e in ('%git_cmd%') do echo version = (%%e)>>%file_name%
    
    TYPE %file_name%
    
    GOTO :EOF
    

    Noting:
    My %date% format is YYYYMMDD

    I've removed the %NL%... line. NL is not defined in the original post. I'd guess it's supposed to add a new line.

    q72496859s is a batch file that simply responds 1d34a1b1 on a single line.

    I've no idea what git actually generates - does it contain empty lines or perhaps <CR><LF><CR> sequences?

    It's SOP for me to use set "var=value" for setting string values - this avoids problems caused by trailing spaces and not assign a terminal \, Space or " - but build pathnames from the elements - counterintuitively, it is likely to make the process easier.

    Using these principles, this batch becomes

    @ECHO Off
    SETLOCAL
    set "dt=%date%"
    set "wrap=About:"
    set "file_name=u:\vers.txt"
    set "git_cmd=q72496859s describe --abbrev=8 --always"
    echo %wrap% >"%file_name%"
    ECHO date = (%dt%)>>"%file_name%"
    for /f "delims=" %%e in ('%git_cmd%') do echo version = (%%e)>>"%file_name%"
    
    TYPE "%file_name%"
    
    GOTO :EOF
    

    Edit to second version of above.

    @ECHO Off
    SETLOCAL
    set "dt=%date%"
    set "wrap=About:"
    set "file_name=u:\vers.txt"
    set "git_cmd=q72496859s describe --abbrev=8 --always"
    echo %wrap% >"%file_name%"
    ECHO date = (%dt%)>>"%file_name%"
    

    for /f "delims=" %%e in ('%git_cmd% ^|findstr /v "/ -"') do echo version = (%%e)>>"%file_name%"

    TYPE "%file_name%"
    
    GOTO :EOF
    

    I set q72496859s to deliver the git response described.

    The pipe needs to be escaped (by a caret) to tell cmd that it's part of the command-to-be-executed, not of the actual for command.

    The findstr ignores empty lines and reports those lines that do not (/v) contain either / or - - the / needs to be escaped (using backslash this time), which should isolate the required data.

    The result I obtained was

    About:
    date = (06/06/2022)
    version = (1d34a1b1)