tools/lint/hooks_clang_format.py
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Fri, 08 Mar 2019 16:28:11 +0000
changeset 513432 641653ef68b3b79744327675054a65fca442b874
parent 508086 d7507f185a63f25f2d1cf2a3925ade57498ab3a5
child 523682 a108772f1d772959dbc622999da19900b76ecd95
permissions -rwxr-xr-x
No bug - Tagging 13bfcad39c39edcd4e3fc5b26fd62b25d338235f with DEVEDITION_66_0b14_RELEASE a=release CLOSED TREE DONTBUILD

#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import subprocess
import sys

here = os.path.dirname(os.path.realpath(__file__))
topsrcdir = os.path.join(here, os.pardir, os.pardir)

EXTRA_PATHS = (
    'python/mozversioncontrol',
)
sys.path[:0] = [os.path.join(topsrcdir, p) for p in EXTRA_PATHS]

from mozversioncontrol import get_repository_object, InvalidRepoPath


def run_clang_format(hooktype, args):
    try:
        vcs = get_repository_object(topsrcdir)
    except InvalidRepoPath:
        return

    changedFiles = vcs.get_outgoing_files('AM')
    if not changedFiles:
        # No files have been touched
        return

    arguments = ['clang-format', '-s', '-p'] + changedFiles
    # On windows we need this to call the command in a shell, see Bug 1511594
    if os.name == 'nt':
        clang_format_cmd = ['sh', 'mach'] + arguments
    else:
        clang_format_cmd = [os.path.join(topsrcdir, "mach")] + arguments
    if 'commit' in hooktype:
        # don't prevent commits, just display the clang-format results
        subprocess.Popen(clang_format_cmd)
        return False

    print("warning: '{}' is not a valid clang-format hooktype".format(hooktype))
    return False


def hg(ui, repo, node, **kwargs):
    hooktype = kwargs['hooktype']
    return run_clang_format(hooktype, kwargs.get('pats', []))


def git():
    hooktype = os.path.basename(__file__)
    if hooktype == 'hooks_clang_format.py':
        hooktype = 'pre-push'
    return run_clang_format(hooktype, [])


if __name__ == '__main__':
    sys.exit(git())