首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Git中使用AC_REVISION?

如何在Git中使用AC_REVISION?
EN

Stack Overflow用户
提问于 2011-11-22 01:20:11
回答 6查看 3.2K关注 0票数 11

在使用Subversion托管的项目中使用Autoconf时,我会将以下代码放在configure.ac

代码语言:javascript
复制
AC_REVISION($Revision: 1234 $)

使用svn:keywords RevisionAC_REVISION会将configure.ac的修订号插入到生成的configure脚本中。

如何在使用Git管理的项目中执行类似的操作

Git没有像$Revision$这样的关键字,也没有这样的修订号。但它确实有用于提交的SHA1s和git describe。我只是不确定如何将其整合到configure.ac中。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-03-13 12:14:28

adl's answer并不完全是我想要的,但它为我指明了正确的方向。这是我想出来的:

将此代码放入configure.ac

代码语言:javascript
复制
AC_REVISION([m4_esyscmd([./tools/configure.commit])])

将其另存为tools/configure.commit (并使其可执行):

代码语言:javascript
复制
#! /bin/sh
# Display the SHA1 of the commit in which configure.ac was last modified.
# If it's not checked in yet, use the SHA1 of HEAD plus -dirty.

if [ ! -d .git ] ; then
  # if no .git directory, assume they're not using Git
  printf 'unknown commit'
elif git diff --quiet HEAD -- configure.ac ; then
  # configure.ac is not modified
  printf 'commit %s' `git rev-list --max-count=1 HEAD -- configure.ac`
else # configure.ac is modified
  printf 'commit %s-dirty' `git rev-parse HEAD`
fi

这种组合将把上次修改configure.ac的提交的SHA-1放到configure中,这正是我要找的。但是有一个问题。Git在提交文件时不会触及文件的修改时间。这意味着configure将继续包含AutoConf值,而不是被更新,因为OLDSHA-dirty不会意识到它已经过期。

你可以用提交后钩子来解决这个问题。将其保存为.git/hooks/post-commit (并确保将其保存为可执行文件,否则它将无法运行):

代码语言:javascript
复制
#!/bin/sh
#
# Copy this to .git/hooks/post-commit

# If configure.ac was just checked in, touch it,
# so that configure will be regenerated and
# AC_REVISION will reflect the new commit.
#
# For some reason, --quiet isn't actually quiet,
# so redirect output to /dev/null

git diff-tree --quiet HEAD -- configure.ac >/dev/null \
 || touch -c configure.ac
票数 5
EN

Stack Overflow用户

发布于 2011-11-23 15:40:23

实际上,当Autoconf运行时,您可以使用M4执行任何命令。因此,你可能想要这样的东西:

代码语言:javascript
复制
AC_REVISION([m4_esyscmd_s([git describe --always])])

请注意,与$Revision$字符串不同,您的configure.ac不会在每次更新树时更改。因此,configure不会在每次更新后重新生成,放入configure的修订版只是为其生成configure的最后一个版本。

票数 6
EN

Stack Overflow用户

发布于 2013-11-02 15:16:57

我试图实现与OP类似的功能;我想将Git commit-id嵌入到Postgres版本字符串中。Postgres的configure.in中的代码,在我打算修改的同一行中,已经有了一个示例。

它的要点是,您可以将shell代码片段嵌入到configure.in中的字符串文字中,生成的configure文件(实际上是执行shell脚本的shell )将始终执行该shell代码片段来构建结果字符串。

请参阅patch。以下是生成的configure文件中configure.in和相关部分的补丁。

代码语言:javascript
复制
AC_DEFINE_UNQUOTED(PG_VERSION_STR,
-                   ["PostgreSQL $PACKAGE_VERSION on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"],
+                   ["PostgreSQL $PACKAGE_VERSION (commit `cd $srcdir && git log -1 --format=format:%h`) on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"],
                    [A string containing the version number, platform, and C compiler])

生成的configure代码:

代码语言:javascript
复制
 cat >>confdefs.h <<_ACEOF
-#define PG_VERSION_STR "PostgreSQL $PACKAGE_VERSION on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"
+#define PG_VERSION_STR "PostgreSQL $PACKAGE_VERSION (commit `cd $srcdir && git log -1 --format=format:%h`) on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"
 _ACEOF

补丁前后的Postgres版本字符串:

代码语言:javascript
复制
PostgreSQL 9.3.0 on x86_64-unknown-linux-gnu, compiled by  ...
PostgreSQL 9.3.0 (commit 2cf9dac) on x86_64-unknown-linux-gnu, compiled by ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8215785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档