no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / tools / lint / hooks_clang_format.py
blob9adb81b7f08ff944aaa8c1e992576792fa43aba5
1 #!/usr/bin/env python3
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 import os
7 import subprocess
8 import sys
9 from subprocess import CalledProcessError, check_output
11 here = os.path.dirname(os.path.realpath(__file__))
12 topsrcdir = os.path.join(here, os.pardir, os.pardir)
14 EXTRA_PATHS = (
15 "python/mach",
16 "python/mozbuild",
17 "python/mozversioncontrol",
18 "testing/mozbase/mozfile",
19 "third_party/python/jsmin",
20 "third_party/python/six",
22 sys.path[:0] = [os.path.join(topsrcdir, p) for p in EXTRA_PATHS]
24 from mozversioncontrol import InvalidRepoPath, get_repository_object
27 def run_clang_format(hooktype, changedFiles):
28 try:
29 vcs = get_repository_object(topsrcdir)
30 except InvalidRepoPath:
31 return
33 if not changedFiles:
34 # No files have been touched
35 return
37 # We have also a copy of this list in:
38 # python/mozbuild/mozbuild/mach_commands.py
39 # version-control-tools/hgext/clang-format/__init__.py
40 # release-services/src/staticanalysis/bot/static_analysis_bot/config.py
41 # Too heavy to import the full class just for this variable
42 extensions = (".cpp", ".c", ".cc", ".h", ".m", ".mm")
43 path_list = []
44 for filename in sorted(changedFiles):
45 # Ignore files unsupported in clang-format
46 if filename.endswith(extensions):
47 path_list.append(filename)
49 if not path_list:
50 # No files have been touched
51 return
53 arguments = ["clang-format", "-p"] + path_list
54 # On windows we need this to call the command in a shell, see Bug 1511594
55 if os.name == "nt":
56 clang_format_cmd = [sys.executable, "mach"] + arguments
57 else:
58 clang_format_cmd = [os.path.join(topsrcdir, "mach")] + arguments
59 if "commit" in hooktype:
60 # don't prevent commits, just display the clang-format results
61 subprocess.call(clang_format_cmd)
63 vcs.add_remove_files(*path_list)
65 return False
66 print("warning: '{}' is not a valid clang-format hooktype".format(hooktype))
67 return False
70 def hg(ui, repo, node, **kwargs):
71 print(
72 "warning: this hook has been deprecated. Please use the hg extension instead.\n"
73 "please add 'clang-format = ~/.mozbuild/version-control-tools/hgext/clang-format'"
74 " to hgrc\n"
75 "Or run 'mach bootstrap'"
77 return False
80 def git():
81 hooktype = os.path.basename(__file__)
82 if hooktype == "hooks_clang_format.py":
83 hooktype = "pre-push"
85 try:
86 changedFiles = check_output(
87 ["git", "diff", "--staged", "--diff-filter=d", "--name-only", "HEAD"],
88 text=True,
89 ).split()
90 # TODO we should detect if we are in a "add -p" mode and show a warning
91 return run_clang_format(hooktype, changedFiles)
93 except CalledProcessError:
94 print("Command to retrieve local files failed")
95 return 1
98 if __name__ == "__main__":
99 sys.exit(git())