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

Federico
  • 851
  • 3
  • 14
  • 27
  • Does this answer your question? [How to get the current branch name in Git?](https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git) – RomainValeri Jul 22 '20 at 14:41
  • 1
    No, because I need suggestion about how to write a batch script to do that – Federico Jul 22 '20 at 14:42
  • `git rev-parse HEAD` works fine in bash context. Is it more generally about bash syntax? – RomainValeri Jul 22 '20 at 14:51
  • Yes, my question is about the bash sintax. – Federico Jul 22 '20 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)_? – Compo Jul 22 '20 at 15:09
  • It was a typo, if you read the code you can understand that is code for Cmd shell – Federico Jul 22 '20 at 15:18
  • If you're working on a `C` 'programming' project, why use inferior 'scripting' to perform a task you should be able to perform in that language? – Compo Jul 22 '20 at 15:21
  • Because I'm writing code for an embedded system that cannot have access to git. So I need a script to modify an header file used by the compiler. In that way my embedded system can communicate which code is running. – Federico Jul 22 '20 at 15:23
  • You don't need access to git, what I'm telling you is it would make more sense to write a small find and replace C program to do this task, not trying to use a batch script for programming, when it was only designed to execute a series of simple commands. – Compo Jul 22 '20 at 16:04

1 Answers1

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
 )
Gerhard
  • 18,114
  • 5
  • 20
  • 38