Move code starting the editor into a common function.

Windows users wouldn't get the same behavior on git cl vs gcl.
Improve automatic CRLF<->LF conversion, some gcl users would be \n repeated in
their description depending on the editor used.

R=dpranke@chromium.org
BUG=
TEST=


Review URL: http://codereview.chromium.org/8360007

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@107106 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 9566118..1300267 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -12,7 +12,6 @@
 import os
 import re
 import sys
-import tempfile
 import textwrap
 import urlparse
 import urllib2
@@ -36,6 +35,7 @@
 from third_party import upload
 import breakpad  # pylint: disable=W0611
 import fix_encoding
+import gclient_utils
 import presubmit_support
 import rietveld
 import scm
@@ -648,7 +648,13 @@
       initial_text += '\nBUG='
     if 'TEST=' not in self.description:
       initial_text += '\nTEST='
-    self._ParseDescription(UserEditedLog(initial_text))
+    content = gclient_utils.RunEditor(initial_text, True)
+    if not content:
+      DieWithError('Running editor failed')
+    content = re.compile(r'^#.*$', re.MULTILINE).sub('', content).strip()
+    if not content:
+      DieWithError('No CL description, aborting')
+    self._ParseDescription(content)
 
   def _ParseDescription(self, description):
     if not description:
@@ -824,39 +830,6 @@
   return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args)
 
 
-def UserEditedLog(starting_text):
-  """Given some starting text, let the user edit it and return the result."""
-  editor = os.getenv('EDITOR', 'vi')
-
-  (file_handle, filename) = tempfile.mkstemp()
-  fileobj = os.fdopen(file_handle, 'w')
-  fileobj.write(starting_text)
-  fileobj.close()
-
-  # Open up the default editor in the system to get the CL description.
-  try:
-    cmd = '%s %s' % (editor, filename)
-    if sys.platform == 'win32' and os.environ.get('TERM') == 'msys':
-      # Msysgit requires the usage of 'env' to be present.
-      cmd = 'env ' + cmd
-    # shell=True to allow the shell to handle all forms of quotes in $EDITOR.
-    try:
-      subprocess2.check_call(cmd, shell=True)
-    except subprocess2.CalledProcessError, e:
-      DieWithError('Editor returned %d' % e.returncode)
-    fileobj = open(filename)
-    text = fileobj.read()
-    fileobj.close()
-  finally:
-    os.remove(filename)
-
-  if not text:
-    return
-
-  stripcomment_re = re.compile(r'^#.*$', re.MULTILINE)
-  return stripcomment_re.sub('', text).strip()
-
-
 def ConvertToInteger(inputval):
   """Convert a string to integer, but returns either an int or None."""
   try: