在使用Subversion管理的项目中使用Autoconf时,我会将此代码放入:configure.ac
AC_REVISION($Revision: 1234 $)
Run Code Online (Sandbox Code Playgroud)
使用svn:keywords Revision,AC_REVISION会将修订号configure.ac插入生成的configure脚本中.
如何在使用Git管理的项目中做类似的事情?
Git没有像这样的关键字$Revision$,也没有这样的修订号.但它确实有提交的SHA1,和git describe.我只是不确定如何将其纳入其中configure.ac.
当 Autoconf 运行时,您实际上可以使用 M4 执行任何命令。因此,也许你想要这样的东西:
AC_REVISION([m4_esyscmd_s([git describe --always])])
Run Code Online (Sandbox Code Playgroud)
请注意,与$Revision$字符串不同,configure.ac每次更新树时都不会更改。因此configure不会在每次更新后重新生成,并且放入的修订configure将只是为其configure生成的最后一个版本。
adl 的回答并不是我想要的,但它为我指明了正确的方向。这是我想出的:
把这个放在configure.ac:
AC_REVISION([m4_esyscmd([./tools/configure.commit])])
Run Code Online (Sandbox Code Playgroud)
将其另存为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
Run Code Online (Sandbox Code Playgroud)
该组合会将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
Run Code Online (Sandbox Code Playgroud)