批处理文件,将git分支添加到头文件中 [英] Batch file to add git branch into header file

查看:179
本文介绍了批处理文件,将git分支添加到头文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在git下开发一个C项目,我想将分支名称添加到头文件中.

I'm working on a C project, under git, and I would like to add the branch name into an 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_ */

,我想用当前分支的名称替换与gitBranch关联的字符串. 这样,我可以在预构建过程中执行批处理文件并更新gitBranch变量.

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

但目前我不能:

  • 更新头文件中的分支名称.

有什么建议吗?

提前感谢您的帮助!

最诚挚的问候, 费德里科

Best regards, Federico

推荐答案

未经测试.只要确保您将正确的文件路径添加到set infile=

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
 )

这篇关于批处理文件,将git分支添加到头文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆