When using Autoconf in a project managed with Subversion, I would put this code in configure.ac
:
AC_REVISION($Revision: 1234 $)
With svn:keywords Revision
, AC_REVISION
would insert the revision number of configure.ac
into the generated configure
script.
How can I do something similar in a project managed with Git?
Git doesn't have keywords like $Revision$
, and doesn't have revision numbers as such. But it does have SHA1s for commits, and git describe
. I'm just not sure how to incorporate that into configure.ac
.
Commit hashesThe long string following the word commit is called the commit hash. It's unique identifier generated by Git. Every commit has one, and I'll show you what they're used for shortly. Note: The “commit hash” is sometimes called a Git commit “reference” or “SHA”.
A revision parameter <rev> typically, but not necessarily, names a commit object. It uses what is called an extended SHA1 syntax, [and includes] various ways to spell object names. So "revision" refers to the id you can use as a parameter to reference an object in git (usually a commit).
You can actually execute any command with M4 when Autoconf runs. Thus maybe you want something like:
AC_REVISION([m4_esyscmd_s([git describe --always])])
Note that unlike with $Revision$
strings your configure.ac
will not change every time you update your tree. Hence configure
will not get regenerated after each update and the revision put into configure
will simply be the last version for which configure
was generated.
I was trying to achieve something similar as the OP; I wanted to embed Git commit-id in Postgres version string. The code in Postgres' configure.in, in the same line that I intended to modify, already had an example.
The gist of it is that you can embed shell snippet in string literals in configure.in
, and the resulting configure
file (the shell executing the shell script, actually) will always execute that shell snippet to build the resultant string.
Please see the patch. Following are the patches to configure.in
and relevant section from resulting configure
file.
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])
Resulting configure
code:
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 version string before and after the patch:
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With