0

I'm working on a C project, under git, and I would like to add the branch name into an header file.

This is my idea:

I have a version header file:

/* Define to prevent recursive inclusion -------------------------------------*/ 
#ifndef _VERSION_INTERFACE_H_
#define _VERSION_INTERFACE_H_
/* Includes ------------------------------------------------------------------*/
/* Exported defines ----------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
const char *gitBranch = "develop";
/* Exported macro ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/ 
#endif  /* _VERSION_INTERFACE_H_ */

and I would like to replace the string associated to gitBranch with the name of the current branch. In this way I can execute the batch file during the pre-build process and update the gitBranch variable.

I write a first version of a batch file:

@echo off
setlocal enabledelayedexpansion

SET GIT_CMD="C:\Program Files\Git\bin\git.exe"

rem Specify input file name
SET inputFileName=include\version_interface.h

rem String to find
SET stringToFind=const char *gitBranch

FOR /F "tokens=*" %%a in ( '"C:\Program Files\Git\bin\git.exe" branch --show-current' ) do SET branchName=%%a

rem String to replace
SET stringToReplace=%branchName%

for /F "tokens=*" %%n in (!infile!) do (
SET LINE=%%n
SET TMPR=!LINE:%stringToFind%=%stringToReplace%!
Echo !TMPR!>>tmp.txt
)

move tmp.txt %infile%
pause

but at the moment I cannot:

  • Update the branch name inside the header file.

Any suggestion?

Thanks in advance for the help!

Best regards, Federico

9
  • Does this answer your question? How to get the current branch name in Git? Commented Jul 22, 2020 at 14:41
  • 1
    No, because I need suggestion about how to write a batch script to do that Commented Jul 22, 2020 at 14:42
  • git rev-parse HEAD works fine in bash context. Is it more generally about bash syntax? Commented Jul 22, 2020 at 14:51
  • Yes, my question is about the bash sintax. Commented Jul 22, 2020 at 15:00
  • If your question is about bash, the 'nix Bourne Again SHell, why have you written a Windows Cmd shell script (batch file)? Commented Jul 22, 2020 at 15:09

1 Answer 1

1

Untested. Just make sure you add the correct file path to set infile=

@echo off
set "infile=C:\Path\to\FILE.h"
for /f "tokens=*" %%a in ('"C:\Program Files\Git\bin\git.exe" branch --show-current' ) do set "branchName=%%a"
for /f "delims=" %%i in ('type "%infile%" ^| find /v /n "" ^& break^>%infile%') do (
    setlocal enabledelayedexpansion
    set "line=%%i"
    set "line=!line:*]=!"
    if "!line:~0,21!" == "const char *gitBranch" set "line=const char *gitBranch ="%branchName%";"
    echo(!line!>>!infile!
    endlocal
 )
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! It works like a charm!!! It is what I'm looking for!

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.