我怎样才能让我的C code,自动打印出它的Git版本的哈希? [英] How can I get my C code to automatically print out its Git version hash?

查看:223
本文介绍了我怎样才能让我的C code,自动打印出它的Git版本的哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来写C code,它可以访问其Git版本哈希?

Is there an easy way to write C code that can access its Git version hash?

我用C写的软件在实验室收集科学数据。我的code记录它收集以供日后分析.yaml文件中的数据。我的实验,从一天到一天改变,我经常要修改code。要跟踪修订的,我用一个Git仓库。

I wrote software in C to collect scientific data in a laboratory setting. My code records the data it collects in a .yaml file for later analysis. My experiments change from day-to-day and I often have to modify the code. To keep track of revisions, I use a git repository.

我希望能够包括Git的修改散列在我.yaml数据文件的注释。这样一来,我可以看.yaml文件,并确切地知道code用于生成该文件中显示的数据。有一种简单的方法来自动做到这一点?

I would like to be able to include the Git revision hash as a comment in my .yaml data files. That way, I could look at the .yaml file and know exactly what code was used to generate the data shown in that file. Is there an easy way to do this automatically?

推荐答案

在我的计划,我认为Git版本号和构建在一个单独的文件中的日期,名为 version.c ,它看起来像这样:

In my program, I hold the git version number and the date of the build in a separate file, called version.c, which looks like this:

#include "version.h"
const char * build_date = "2009-11-10 11:09";
const char * build_git_sha = "6b54ea36e92d4907aba8b3fade7f2d58a921b6cd";

还有一个头文件,它看起来像这样:

There is also a header file, which looks like this:

#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脚本,它看起来像这样产生的:

Both the header file and the C file are generated by a Perl script which looks like this:

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 使得如图字符串。

Here hash_to_c_file does all the work of creating version.c and version.h and make_date_time makes a string as shown.

在主程序中,我有一个例行

In the main program, I have a 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);
}

我不是那种对知识的混帐,所以我欢迎大家提出意见,如果有更好的方法来做到这一点。

I'm not that knowledgeable about git, so I'd welcome comments if there is a better way to do this.

这篇关于我怎样才能让我的C code,自动打印出它的Git版本的哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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