如何在编译时确定git存储库的状态
How can the status of a git repository be determined at compile time?
我正在开发一个用C++编写的模拟系统,该系统由git管理。我使用GNU make作为构建工具。为了使模拟结果可再现,git非常有用,因为您可以返回到创建结果的模拟程序的确切版本。
目前,git存储库的状态和SHA1是在运行时以编程方式确定的,并与结果一起写入文件。但是,如果源在程序编译后发生了更改,我的日志文件中的状态将不会反映程序的实际版本。因此,我正在寻找一种方法来确定编译时的git状态。有机会做到这一点吗?
一个解决方案是让构建系统提取该值,并让它生成一些包含该值的C++头(或源文件)。
例如,如果使用CMake,您可以使用FindGit模块来执行以下操作:
project(...)
# load module.
find_package(Git)
# create "${GIT_HEAD_HASH}" variable that contains
# the SHA-1 of the current tree. This assumes that the
# root CMakeLists is in the root of the Git repository.
git_tree_info(${CMAKE_SOURCE_DIR} GIT_HEAD)
# generate a 'version.h' file based on the 'version.h.in'
# file. replace all @...@ strings with variables in the
# current scope.
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/version.h
@ONLY
)
然后,添加以下version.h.in
文件:
#ifndef _version_h__
#define _version_h__
static const char VERSION[] = "@GIT_HEAD_HASH@";
#endif
CMake将用它使用get_tree_info()
提取的值替换@GIT_HEAD_HASH@
字符串。
然后,从您的常规代码:
#include "version.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
int main(int argc, char ** argv)
{
if ((argc == 2) && (std::strcmp(argv[1],"--version") == 0))
{
std::cerr
<< VERSION
<< std::endl;
return (EXIT_FAILURE);
}
// ...
}
这是一个简化的、绝对未经测试的例子。如果您查看FindGit
CMake模块的源代码,您会发现它只是在构建时运行一个execute_process()
命令来提取信息。您可以根据Git命令行接口的调用对其进行修改以提取任何内容。
由于您已经在使用Makefile
,因此可以在那里检查状态。
如果您想跟踪当时的HEAD
是什么提交,可以使用git rev-parse HEAD
来获取提交的sha1。如果在运行时需要,可以将其存储在文件中。
相关文章:
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- 自动完成在git存储库中的Visual Studio项目中不起作用
- git 子模块的替代方案,用于使用多个存储库的项目
- 使用 libgit2 从另一个分支创建新的 git 存储库?
- 我被困在尝试克隆需要 cmake 文件和依赖项的 git 存储库中
- Visual Studio 2017 C++:克隆的 git 存储库中缺少包含的内容
- 使用 Git init、fetch 和 checkout 克隆 Git 存储库
- git 存储库中的多个引用
- 为 git 存储库制作 c 文件
- 使用 iOS 或 c/c++ 访问 SVN 或远程 Git 存储库等
- 如何在多个 GIT 存储库中组织大型项目
- 如何将更改后的行与C代码的git存储库中的函数关联起来
- Git 1.56 boost存储库中缺少boost::posix_time::to_time_t
- 如何在编译时确定git存储库的状态
- Visual Studio 在从 GIT 存储库拉取文件时不会通知我文件更改(使用 samba)
- 如何有效地为一个有很多依赖的c++产品使用git存储库/子模块
- 如何使用libgit2从git存储库中的HEAD获取最后一次提交
- 使用libgit2从git存储库中删除文件
- 如何使用libgit2提交到git存储库