Improve --version per discussion

gn --version looks like

    1431 (31a2470d)

now.

Change-Id: Ia9fb52e8f24daf4fe6ae31e7c0ab9cf83f793e03
Reviewed-on: https://gn-review.googlesource.com/2000
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
diff --git a/build/gen.py b/build/gen.py
index 6fdf6ef..605d2e4 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -10,6 +10,7 @@
 import optparse
 import os
 import platform
+import re
 import shutil
 import subprocess
 import sys
@@ -46,21 +47,23 @@
 
 
 def GenerateLastCommitPosition(header):
-  # When gn was forked from Chromium the revisions were ~550000. Use 600000 as a
-  # non-conflicting base that also makes it relatively easy to see that it's
-  # built from a post-fork build.
-  BASE_COMMIT_POSITION = 600000
-  commit_position = int(len(
-      subprocess.check_output('git log --oneline', shell=True).splitlines()))
+  ROOT_TAG = 'initial-commit'
+  describe_output = subprocess.check_output(
+      ['git', 'describe', 'HEAD', '--match', ROOT_TAG], shell=is_win)
+  mo = re.match(ROOT_TAG + '-(\d+)-g([0-9a-f]+)', describe_output)
+  if not mo:
+    raise ValueError(
+        'Unexpected output from git describe when generating version header')
+
   contents = '''// Generated by build/gen.py.
 
 #ifndef OUT_LAST_COMMIT_POSITION_H_
 #define OUT_LAST_COMMIT_POSITION_H_
 
-#define LAST_COMMIT_POSITION "%d"
+#define LAST_COMMIT_POSITION "%s (%s)"
 
 #endif  // OUT_LAST_COMMIT_POSITION_H_
-''' % (BASE_COMMIT_POSITION + commit_position)
+''' % (mo.group(1), mo.group(2))
 
   # Only write/touch this file if the commit position has changed.
   old_contents = ''