在C / C ++源代码中包含Git提交哈希和/或分支名称 [英] Include Git commit hash and/or branch name in C/C++ source

查看:160
本文介绍了在C / C ++源代码中包含Git提交哈希和/或分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将Git提交哈希和/或其他信息放入已编译二进制文件中的C ++变量的内容中,而不使其成为Git跟踪的源代码的一部分。

I would like to know how you can get a Git commit hash and/or other information into the contents of a C++ variable in the compiled binary without having it be part of the source that's tracked by Git.

我需要在嵌入式处理器上运行的已编译可执行文件中跟踪固件发行信息。在此封闭环境中(例如,没有文件系统),无法识别固件二进制文件的特定版本,例如有意义的文件名,MD5校验和甚至日期/时间戳。

I need to keep track of firmware release information in a compiled executable running on an embedded processor. Means to identify the particular release of the firmware binary such as meaningful filenames, MD5 checksums or even date/time stamps are not available in this closed environment (i.e., there is no filesystem).

一种方法是让设备的控制台输出产生标识文本,例如 Release 1.2.3, commit hash 1bc123 ...或类似内容。固件发布信息仅对维护人员有用,因此,受过培训的操作员可以检查控制台输出。要实现此目标,可能需要手动编辑版本字符串,然后在程序启动时将其编译为代码并输出到控制台。

One approach is to have the device's console output produce identifying text, such as 'Release 1.2.3', 'commit hash 1bc123...', or similar. The firmware release information is only of interest to maintenance personnel, so a trained operator could examine the console output. To implement this it could potentially involve manual editing of a version string, which is then compiled into the code and output to the console at program startup.

发行使用签核工作流再次检查版本信息正确的版本。但是,这是手动过程,因此本质上是不可靠的。例如,如果开发人员忘记更新版本信息,该怎么办? -编译后的代码与其报告的版本字符串之间现在断开连接。

This is acceptable for major releases where a signoff workflow is used to double-check that the version information is correct. However, this being a manual process it is inherently unreliable. For example, what if the developer forgets to update the version information? - There is now a disconnect between the compiled code and its reported version string.

一个工作流,每次用户要测试硬件时,都会重新编译并下载代码在有问题的情况下是不切实际的,即更新固件非常繁琐。

A workflow where the code is freshly compiled and downloaded each time the user wants to test the hardware is not practical in the situation in question, ie., it is quite onerous to update the firmware.

因此,需要一种自动的方法来识别代码版本。在这种情况下,使用Git,并且开发人员会定期将他们的工作提交给功能分支。显示Git提交哈希,也许还显示是否有未暂存的更改,将是一种识别用于编译固件的源代码状态的方法。

An automatic way of identifying the version of the code is thus required. In the situation in question, Git is used, and the developers regularly commit their work to feature branches. Displaying the Git commit hash, and perhaps also whether or not there are unstaged changes, would be a way of identifying the status of the source code used to compile the firmware.

要求是我希望我的应用程序具有可用的信息,以便能够显示:
Git commit:[01abcdef ... etc],分支:experimentalStuffDoNotRelease

The requirement is that I would like my application to have information available to it so that it is able to display: "Git commit:[01abcdef...etc], branch: experimentalStuffDoNotRelease"

因此,我想在编译的C和/或C ++代码中自动包括Git信息,例如提交哈希和分支。

Thus, I would like to automatically include Git information, such as commit hash and branch, in the compiled C and/or C++ code.

开发环境使开发人员同时使用Windows和Linux,并使用Eclipse CDT和相对简单的工作流程:编译

The development environment has developers using both Windows and Linux, and uses Eclipse CDT with a relatively unsophisticated workflow of: check out; compile; download to the hardware.

推荐答案

我使用makefile,如下所示:

I use a makefile, like so:

GIT_HASH=`git rev-parse HEAD`
COMPILE_TIME=`date -u +'%Y-%m-%d %H:%M:%S UTC'`
GIT_BRANCH=`git branch | grep "^\*" | sed 's/^..//'`
export VERSION_FLAGS=-DGIT_HASH="\"$(GIT_HASH)\"" -DCOMPILE_TIME="\"$(COMPILE_TIME)\"" -DGIT_BRANCH="\"$(GIT_BRANCH)\""

all:
    g++ main.cpp $(VERSION_FLAGS)

运行makefile时,git哈希和编译时间都被加载到源代码中可访问的宏中,例如:

When the makefile is run, the git hash and the time of compilation are both loaded into macros accessible within the source, like so:

#include <iostream>

int main(){
  std::cerr<<"hash="<<GIT_HASH<<", time="<<COMPILE_TIME<<", branch="<<GIT_BRANCH<<std::endl; 
}

输出如下:

hash=35f531bf1c959626e1b95f2d3e1a7d1e4c58e5ec, time=2017-05-18 04:17:25 UTC, branch=master

这篇关于在C / C ++源代码中包含Git提交哈希和/或分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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