Adding 'git cl format' command for clang-format.

As per the discussion in
https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-dev/Hl2QPNQ9E5o,
this CL adds a 'git cl format' command. Users may use it at their own
discretion. By default, it generates a diff against the local tracking
(upstream) branch and runs clang-format-diff on that diff.  When given the
'--full' flag, it instead identifies all .cc, .cpp, and .h files in the diff and
runs clang-format on the entirety of each of those files.

Review URL: https://chromiumcodereview.appspot.com/14629012

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@198477 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 9f69176..a93a04b 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1931,6 +1931,36 @@
   return 0
 
 
+def CMDformat(parser, args):
+  """run clang-format on the diff"""
+  CLANG_EXTS = ['.cc', '.cpp', '.h']
+  parser.add_option('--full', action='store_true', default=False)
+  opts, args = parser.parse_args(args)
+  if args:
+    parser.error('Unrecognized args: %s' % ' '.join(args))
+
+  if opts.full:
+    cmd = ['diff', '--name-only', '--'] + ['.*' + ext for ext in CLANG_EXTS]
+    files = RunGit(cmd).split()
+    if not files:
+      print "Nothing to format."
+      return 0
+    RunCommand(['clang-format', '-i'] + files)
+  else:
+    cfd_path = os.path.join('/usr', 'lib', 'clang-format',
+                            'clang-format-diff.py')
+    if not os.path.exists(cfd_path):
+      print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path
+      return 2
+    cmd = ['diff', '-U0', '@{u}', '--'] + ['.*' + ext for ext in CLANG_EXTS]
+    diff = RunGit(cmd)
+    cmd = [sys.executable, '/usr/lib/clang-format/clang-format-diff.py',
+           '-style', 'Chromium']
+    RunCommand(cmd, stdin=diff)
+
+  return 0
+
+
 def Command(name):
   return getattr(sys.modules[__name__], 'CMD' + name, None)