Commit fab8f82d authored by agable@chromium.org's avatar agable@chromium.org

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
parent abc4f6c2
......@@ -1931,6 +1931,36 @@ def CMDset_close(parser, args):
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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment