有没有一种简单的方法来编写可以访问其Git版本哈希的C代码?
我用C语言编写软件,在实验室环境中收集科学数据.我的代码将收集的数据记录在.yaml文件以供以后分析.我的实验每天都在变化,我经常不得不修改代码.为了跟踪修订,我使用git存储库.
我希望能够将Git修订哈希作为注释包含在我的文档中.yaml数据文件.那样的话,我就可以看到.yaml文件,并确切地知道生成该文件中显示的数据所使用的代码.有没有一种简单的方法可以自动做到这一点?
有没有一种简单的方法来编写可以访问其Git版本哈希的C代码?
我用C语言编写软件,在实验室环境中收集科学数据.我的代码将收集的数据记录在.yaml文件以供以后分析.我的实验每天都在变化,我经常不得不修改代码.为了跟踪修订,我使用git存储库.
我希望能够将Git修订哈希作为注释包含在我的文档中.yaml数据文件.那样的话,我就可以看到.yaml文件,并确切地知道生成该文件中显示的数据所使用的代码.有没有一种简单的方法可以自动做到这一点?
在我的程序中,我将GIT版本号和构建日期保存在一个名为version.c
的单独文件中,如下所示:
#include "version.h"
const char * build_date = "2009-11-10 11:09";
const char * build_git_sha = "6b54ea36e92d4907aba8b3fade7f2d58a921b6cd";
还有一个头文件,如下所示:
#ifndef VERSION_H
#define VERSION_H
extern const char * build_date; /* 2009-11-10 11:09 */
extern const char * build_git_sha; /* 6b54ea36e92d4907aba8b3fade7f2d58a921b6cd */
#endif /* VERSION_H */
头文件和C文件都是由Perl脚本生成的,如下所示:
my $git_sha = `git rev-parse HEAD`;
$git_sha =~ s/\s+//g;
# This contains all the build variables.
my %build;
$build{date} = make_date_time ();
$build{git_sha} = $git_sha;
hash_to_c_file ("version.c", \%build, "build_");
这里hash_to_c_file
完成了创建version.c
和version.h
的所有工作,make_date_time
生成了一个字符串,如图所示.
在主程序中,我有一个 routine
#include "version.h"
// The name of this program.
const char * program_name = "magikruiser";
// The version of this program.
const char * version = "0.010";
/* Print an ID stamp for the program. */
static void _program_id_stamp (FILE * output)
{
fprintf (output, "%s / %s / %s / %s\n",
program_name, version,
build_date, build_git_sha);
}
我对git不是很了解,所以如果有更好的方法,我欢迎 comments .