Hello there,

I have for some time been disturbed by the fact that 'git-version-gen'
relies on the presence of an annotated tag in order to pick a good
revision string.  The following patch is adding a mechanism to use
the call 'git describe --always' as the very last resort, in the
sense that it enters only if the fallback string is empty.

The addition of a leading 'g' to the revision hash is mostly a
convenience, but the usability of the patched version applied
to some cloned directory of small depth turned out more pleasant
than I had expected.

Best regards,
  Mats Erik Andersson, on behalf of GNU Inetutils
>From c67172c1c9a111b8fddebf997c8a9bf7f6907516 Mon Sep 17 00:00:00 2001
From: Mats Erik Andersson <g...@gisladisker.se>
Date: Mon, 13 Apr 2015 23:00:35 +0200
Subject: [PATCH] git-version-gen: detect untagged revisions

* build-aux/git-version-gen: If fallback string is empty,
but git is available, use "git describe --always" to extract
a revision, and prefix it with "g".  This is much better than
responding "UNKNOWN" and it applies to shallow clones!
---
 ChangeLog                 |    8 ++++++++
 build-aux/git-version-gen |    8 ++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0bf9716..314c216 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-04-13  Mats Erik Andersson  <g...@gisladisker.se>
+
+	git-version-gen: detect untagged revisions
+	* build-aux/git-version-gen: If fallback string is empty,
+	but git is available, use "git describe --always" to extract
+	a revision, and prefix it with "g".  This is much better than
+	responding "UNKNOWN" and it applies to shallow clones!
+
 2015-04-09  Paul Eggert  <egg...@cs.ucla.edu>
 
 	gitlog-to-changelog: port to MS-Windows
diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen
index 8e92c0a..ddc0061 100755
--- a/build-aux/git-version-gen
+++ b/build-aux/git-version-gen
@@ -189,12 +189,16 @@ then
     v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
     v_from_git=1
 elif test "x$fallback" = x || git --version >/dev/null 2>&1; then
-    v=UNKNOWN
+    if v=g`git describe --always HEAD 2>/dev/null`; then
+	v_from_git=1
+    else
+	v=UNKNOWN
+    fi
 else
     v=$fallback
 fi
 
-v=`echo "$v" |sed "s/^$prefix//"`
+v=`echo "$v" | sed "s/^$prefix//"`
 
 # Test whether to append the "-dirty" suffix only if the version
 # string we're using came from git.  I.e., skip the test if it's "UNKNOWN"
-- 
1.7.3.2

Reply via email to