如何在Git中使用AC_REVISION?

时间:2011-11-21 17:20:11

标签: git autoconf

在使用Autoconf管理的项目中使用Subversion时,我会将此代码放在configure.ac中:

AC_REVISION($Revision: 1234 $)

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

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

Git没有像$Revision$这样的关键字,也没有这样的修订号。但它确实有提交的SHA1和git describe。我只是不确定如何将其纳入configure.ac

6 个答案:

答案 0 :(得分:4)

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

将其放入configure.ac

AC_REVISION([m4_esyscmd([./tools/configure.commit])])

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

#! /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将继续包含OLDSHA-dirty值而不是更新,因为autoconf不会意识到它已过时。

您可以使用post-commit钩子解决这个问题。将其另存为.git/hooks/post-commit(并确保您chmod为可执行文件,否则将无法运行):

#!/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

答案 1 :(得分:3)

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

AC_REVISION([m4_esyscmd_s([git describe --always])])

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

答案 2 :(得分:0)

Git有类似的东西,但您必须通过.gitattributes文件专门为相关路径启用它。

   ident
       When the attribute ident is set for a path, git replaces $Id$ in
       the blob object with $Id:, followed by the 40-character hexadecimal
       blob object name, followed by a dollar sign $ upon checkout. Any
       byte sequence that begins with $Id: and ends with $ in the worktree
       file is replaced with $Id$ upon check-in.

答案 3 :(得分:0)

答案 4 :(得分:0)

我试图获得与OP相似的东西;我想在Postgres版本字符串中嵌入Git commit-id。 Postgres的configure.in中的代码,与我打算修改的行相同,已经有了一个例子。

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

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

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代码:

 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版本字符串:

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 ...

答案 5 :(得分:0)

已建立的git autoconf解决方案正在使用build-aux/git-version-gen, 见。 https://github.com/kergoth/autoconf/blob/master/build-aux/git-version-gen

,然后只需确保您的代码使用与版本相同的前缀(即v)即可。

AC_INIT([GNU project], m4_esyscmd([build-aux/git-version-gen .tarball-version]), [bug-project@example])

这适用于git和非git发行版,其中.version跟踪.git dev版本,而.tarball-version跟踪非git发行版本。

相关问题