Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use AC_REVISION with Git?

Tags:

git

autoconf

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.

like image 456
cjm Avatar asked Nov 21 '11 17:11

cjm


People also ask

What is commit SHA in git?

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

What is Rev in git?

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


2 Answers

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.

like image 142
adl Avatar answered Oct 27 '22 12:10

adl


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 ...
like image 26
Gurjeet Singh Avatar answered Oct 27 '22 13:10

Gurjeet Singh