metrics: Collect git version.

Bug: None
Change-Id: I73545ad59134c6e5dbeb47fb2e8168a5afc0e497
Reviewed-on: https://chromium-review.googlesource.com/c/1296861
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
diff --git a/metrics_utils.py b/metrics_utils.py
index 9209344..2be13be 100644
--- a/metrics_utils.py
+++ b/metrics_utils.py
@@ -138,12 +138,28 @@
   'LABELS',
 }
 
+GIT_VERSION_RE = re.compile(
+  r'git version (\d)\.(\d{0,2})\.(\d{0,2})'
+)
+
 
 def get_python_version():
   """Return the python version in the major.minor.micro format."""
   return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
 
 
+def get_git_version():
+  """Return the Git version in the major.minor.micro format."""
+  p = subprocess2.Popen(
+      ['git', '--version'],
+      stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
+  stdout, _ = p.communicate()
+  match = GIT_VERSION_RE.match(stdout)
+  if not match:
+    return None
+  return '%s.%s.%s' % match.groups()
+
+
 def return_code_from_exception(exception):
   """Returns the exit code that would result of raising the exception."""
   if exception is None: