[gclient_utils] Make git tools set `gitcommit` modeline in vim.

This will improve the ergonomics of `git cl upload`, `git cl
description`, etc. for `vim` users.

I'm sure there are analogues for emacs, vscode, etc, but I'll leave
that as an exercise for a user of those editors :)

R=aravindvasudev@google.com

Change-Id: I194718326bc34092ef8c669a97eae9598592137d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4676559
Reviewed-by: Aravind Vasudevan <aravindvasudev@google.com>
Auto-Submit: Robbie Iannucci <iannucci@chromium.org>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
diff --git a/gclient_utils.py b/gclient_utils.py
index 6a1659d..c7764ce 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -1141,24 +1141,37 @@
 
 def RunEditor(content, git, git_editor=None):
   """Opens up the default editor in the system to get the CL description."""
-  file_handle, filename = tempfile.mkstemp(text=True, prefix='cl_description')
+  editor = GetEditor(git_editor=git_editor)
+  if not editor:
+    return None
   # Make sure CRLF is handled properly by requiring none.
   if '\r' in content:
     print(
         '!! Please remove \\r from your change description !!', file=sys.stderr)
+
+  file_handle, filename = tempfile.mkstemp(text=True, prefix='cl_description.')
   fileobj = os.fdopen(file_handle, 'wb')
   # Still remove \r if present.
   content = re.sub('\r?\n', '\n', content)
   # Some editors complain when the file doesn't end in \n.
   if not content.endswith('\n'):
     content += '\n'
+
+  if 'vim' in editor or editor == 'vi':
+    # If the user is using vim and has 'modelines' enabled, this will change the
+    # filetype from a generic auto-detected 'conf' to 'gitcommit', which is used
+    # to activate proper column wrapping, spell checking, syntax highlighting
+    # for git footers, etc.
+    #
+    # Because of the implementation of GetEditor above, we also check for the
+    # exact string 'vi' here, to help users get a sane default when they have vi
+    # symlink'd to vim (or something like vim).
+    fileobj.write('# vim: ft=gitcommit\n'.encode('utf-8'))
+
   fileobj.write(content.encode('utf-8'))
   fileobj.close()
 
   try:
-    editor = GetEditor(git_editor=git_editor)
-    if not editor:
-      return None
     cmd = '%s %s' % (editor, filename)
     if sys.platform == 'win32' and os.environ.get('TERM') == 'msys':
       # Msysgit requires the usage of 'env' to be present.