Default to using --3way when using `git cl patch` with git >= 1.7.12

Git introduced a --3way argument to `apply` in version 1.7.12 [1]. This
provides a much nicer way to apply issues from Rietveld.

After this change, `git cl patch` will add --3way after checking the git
version for support.

[1] https://github.com/git/git/commit/f247b10aa0f75727f1b4bdd67b060720b8219b29

BUG=None
TEST=Ran `git cl patch <issue>` with both clean and unclean patches,
also checked behaviour of --reject is preserved.
R=maruel@chromium.org

Review URL: https://codereview.chromium.org/18966004

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@210695 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 2ed721e..9ea856f 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -8,6 +8,7 @@
 """A git-command for integrating reviews on Rietveld."""
 
 import difflib
+from distutils.version import LooseVersion
 import json
 import logging
 import optparse
@@ -88,6 +89,13 @@
     return 1, ''
 
 
+def IsGitVersionAtLeast(min_version):
+  PREFIX='git version '
+  version = RunGit(['--version']).strip()
+  return (version.startswith(PREFIX) and
+      LooseVersion(version[len(PREFIX):]) >= LooseVersion(min_version))
+
+
 def usage(more):
   def hook(fn):
     fn.usage_more = more
@@ -1714,7 +1722,8 @@
   parser.add_option('-f', action='store_true', dest='force',
                     help='with -b, clobber any existing branch')
   parser.add_option('--reject', action='store_true', dest='reject',
-                    help='allow failed patches and spew .rej files')
+                    help='failed patches spew .rej files rather than '
+                        'attempting a 3-way merge')
   parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
                     help="don't commit after patch applies")
   (options, args) = parser.parse_args(args)
@@ -1776,6 +1785,8 @@
   cmd = ['git', 'apply', '--index', '-p0']
   if options.reject:
     cmd.append('--reject')
+  elif IsGitVersionAtLeast('1.7.12'):
+    cmd.append('--3way')
   try:
     subprocess2.check_call(cmd, env=env,
                            stdin=patch_data, stdout=subprocess2.VOID)