@subcommand.usage('[files or directories to diff]')
def CMDformat(parser, args):
    'Runs auto-formatting tools (clang-format etc.) on the diff.'
    CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java']
    GN_EXTS = ['.gn', '.gni', '.typemap']
    parser.add_option('--full', action='store_true', help='Reformat the full content of all touched files')
    parser.add_option('--dry-run', action='store_true', help="Don't modify any file on disk.")
    parser.add_option('--python', action='store_true', help='Format python code with yapf (experimental).')
    parser.add_option('--diff', action='store_true', help='Print diff to stdout rather than modifying files.')
    (opts, args) = parser.parse_args(args)
    rel_base_path = settings.GetRelativeRoot()
    if rel_base_path:
        os.chdir(rel_base_path)
    upstream_commit = None
    cl = Changelist()
    upstream_branch = cl.GetUpstreamBranch()
    if upstream_branch:
        upstream_commit = RunGit(['merge-base', 'HEAD', upstream_branch])
        upstream_commit = upstream_commit.strip()
    if (not upstream_commit):
        DieWithError('Could not find base commit for this branch. Are you in detached state?')
    changed_files_cmd = BuildGitDiffCmd('--name-only', upstream_commit, args)
    diff_output = RunGit(changed_files_cmd)
    diff_files = diff_output.splitlines()
    diff_files = [x for x in diff_files if os.path.isfile(x)]
    clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)]
    python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])]
    dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])]
    gn_diff_files = [x for x in diff_files if MatchingFileType(x, GN_EXTS)]
    top_dir = os.path.normpath(RunGit(['rev-parse', '--show-toplevel']).rstrip('\n'))
    return_value = 0
    if clang_diff_files:
        try:
            clang_format_tool = clang_format.FindClangFormatToolInChromiumTree()
        except clang_format.NotFoundError as e:
            DieWithError(e)
        if opts.full:
            cmd = [clang_format_tool]
            if ((not opts.dry_run) and (not opts.diff)):
                cmd.append('-i')
            stdout = RunCommand((cmd + clang_diff_files), cwd=top_dir)
            if opts.diff:
                sys.stdout.write(stdout)
        else:
            env = os.environ.copy()
            env['PATH'] = str(os.path.dirname(clang_format_tool))
            try:
                script = clang_format.FindClangFormatScriptInChromiumTree('clang-format-diff.py')
            except clang_format.NotFoundError as e:
                DieWithError(e)
            cmd = [sys.executable, script, '-p0']
            if ((not opts.dry_run) and (not opts.diff)):
                cmd.append('-i')
            diff_cmd = BuildGitDiffCmd('-U0', upstream_commit, clang_diff_files)
            diff_output = RunGit(diff_cmd)
            stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env)
            if opts.diff:
                sys.stdout.write(stdout)
            if (opts.dry_run and (len(stdout) > 0)):
                return_value = 2
    if opts.python:
        yapf_tool = gclient_utils.FindExecutable('yapf')
        if (yapf_tool is None):
            DieWithError('yapf not found in PATH')
        if opts.full:
            if python_diff_files:
                cmd = [yapf_tool]
                if ((not opts.dry_run) and (not opts.diff)):
                    cmd.append('-i')
                stdout = RunCommand((cmd + python_diff_files), cwd=top_dir)
                if opts.diff:
                    sys.stdout.write(stdout)
        else:
            DieWithError('--python currently only works with --full')
    if dart_diff_files:
        try:
            command = [dart_format.FindDartFmtToolInChromiumTree()]
            if ((not opts.dry_run) and (not opts.diff)):
                command.append('-w')
            command.extend(dart_diff_files)
            stdout = RunCommand(command, cwd=top_dir)
            if (opts.dry_run and stdout):
                return_value = 2
        except dart_format.NotFoundError as e:
            print('Warning: Unable to check Dart code formatting. Dart SDK not found in this checkout. Files in other languages are still formatted.')
    if gn_diff_files:
        cmd = ['gn', 'format']
        if ((not opts.dry_run) and (not opts.diff)):
            cmd.append('--in-place')
        for gn_diff_file in gn_diff_files:
            stdout = RunCommand((cmd + [gn_diff_file]), shell=(sys.platform == 'win32'), cwd=top_dir)
            if opts.diff:
                sys.stdout.write(stdout)
    return return_value
Runs auto-formatting tools (clang-format etc.) on the diff.
@subcommand.usage('[files or directories to diff]')
def CMDformat(parser, args):
    
    CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java']
    GN_EXTS = ['.gn', '.gni', '.typemap']
    parser.add_option('--full', action='store_true', help='Reformat the full content of all touched files')
    parser.add_option('--dry-run', action='store_true', help="Don't modify any file on disk.")
    parser.add_option('--python', action='store_true', help='Format python code with yapf (experimental).')
    parser.add_option('--diff', action='store_true', help='Print diff to stdout rather than modifying files.')
    (opts, args) = parser.parse_args(args)
    rel_base_path = settings.GetRelativeRoot()
    if rel_base_path:
        os.chdir(rel_base_path)
    upstream_commit = None
    cl = Changelist()
    upstream_branch = cl.GetUpstreamBranch()
    if upstream_branch:
        upstream_commit = RunGit(['merge-base', 'HEAD', upstream_branch])
        upstream_commit = upstream_commit.strip()
    if (not upstream_commit):
        DieWithError('Could not find base commit for this branch. Are you in detached state?')
    changed_files_cmd = BuildGitDiffCmd('--name-only', upstream_commit, args)
    diff_output = RunGit(changed_files_cmd)
    diff_files = diff_output.splitlines()
    diff_files = [x for x in diff_files if os.path.isfile(x)]
    clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)]
    python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])]
    dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])]
    gn_diff_files = [x for x in diff_files if MatchingFileType(x, GN_EXTS)]
    top_dir = os.path.normpath(RunGit(['rev-parse', '--show-toplevel']).rstrip('\n'))
    return_value = 0
    if clang_diff_files:
        try:
            clang_format_tool = clang_format.FindClangFormatToolInChromiumTree()
        except clang_format.NotFoundError as e:
            DieWithError(e)
        if opts.full:
            cmd = [clang_format_tool]
            if ((not opts.dry_run) and (not opts.diff)):
                cmd.append('-i')
            stdout = RunCommand((cmd + clang_diff_files), cwd=top_dir)
            if opts.diff:
                sys.stdout.write(stdout)
        else:
            env = os.environ.copy()
            env['PATH'] = str(os.path.dirname(clang_format_tool))
            try:
                script = clang_format.FindClangFormatScriptInChromiumTree('clang-format-diff.py')
            except clang_format.NotFoundError as e:
                DieWithError(e)
            cmd = [sys.executable, script, '-p0']
            if ((not opts.dry_run) and (not opts.diff)):
                cmd.append('-i')
            diff_cmd = BuildGitDiffCmd('-U0', upstream_commit, clang_diff_files)
            diff_output = RunGit(diff_cmd)
            stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env)
            if opts.diff:
                sys.stdout.write(stdout)
            if (opts.dry_run and (len(stdout) > 0)):
                return_value = 2
    if opts.python:
        yapf_tool = gclient_utils.FindExecutable('yapf')
        if (yapf_tool is None):
            DieWithError('yapf not found in PATH')
        if opts.full:
            if python_diff_files:
                cmd = [yapf_tool]
                if ((not opts.dry_run) and (not opts.diff)):
                    cmd.append('-i')
                stdout = RunCommand((cmd + python_diff_files), cwd=top_dir)
                if opts.diff:
                    sys.stdout.write(stdout)
        else:
            DieWithError('--python currently only works with --full')
    if dart_diff_files:
        try:
            command = [dart_format.FindDartFmtToolInChromiumTree()]
            if ((not opts.dry_run) and (not opts.diff)):
                command.append('-w')
            command.extend(dart_diff_files)
            stdout = RunCommand(command, cwd=top_dir)
            if (opts.dry_run and stdout):
                return_value = 2
        except dart_format.NotFoundError as e:
            print('Warning: Unable to check Dart code formatting. Dart SDK not found in this checkout. Files in other languages are still formatted.')
    if gn_diff_files:
        cmd = ['gn', 'format']
        if ((not opts.dry_run) and (not opts.diff)):
            cmd.append('--in-place')
        for gn_diff_file in gn_diff_files:
            stdout = RunCommand((cmd + [gn_diff_file]), shell=(sys.platform == 'win32'), cwd=top_dir)
            if opts.diff:
                sys.stdout.write(stdout)
    return return_value