[repohooks] Update pre-upload.py script to check git version

This change checks the git version and fails if it is 1.7.2.0 or
lower.

BUG=none
TEST=Ran repo upload in environment with sufficient and
     insufficient git versions. Ensured it fails when the version
     is 1.7.1 and succeeds when the version is 1.7.2.3

Change-Id: I34ee42fbc40f2419529e87da4f9bf79d8583c591
diff --git a/pre-upload.py b/pre-upload.py
index 02e08a2..5607b40 100644
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -33,6 +33,8 @@
   r".*[\\\/]debian[\\\/]rules$",
 ]
 
+MIN_GIT_VERSION = [1, 7, 2, 0]
+
 def _get_hooks_dir():
   """Returns the absolute path to the repohooks directory."""
   cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd']
@@ -92,6 +94,24 @@
 
 
 # Git Helpers
+def _check_git_version():
+  """Checks the git version installed, dies if it is insufficient"""
+  cmd = ['git', '--version']
+  output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
+
+  m = re.match('(git version )(.*)\n', output)
+  if not m or not m.group(2):
+    _report_error('Failed to get git version, git output=' + output)
+
+  version = m.group(2).split('.')
+  version = map(lambda x: int(x), version)
+  for v, mv in zip(version, MIN_GIT_VERSION):
+    if v < mv:
+      _report_error('Invalid version of git (' + m.group(2) + '), you need '
+                    + 'at least version '
+                    + ''.join([`num` + '.' for num in MIN_GIT_VERSION]))
+    elif v > mv:
+      break
 
 def _get_diff(commit):
   """Returns the diff for this commit."""
@@ -325,6 +345,7 @@
 # Main
 
 def main(project_list, **kwargs):
+  _check_git_version()
   hooks = _setup_project_hooks()
   for project in project_list:
     _run_project_hooks(project, hooks)