Bug 1845017 - Disable the TestPHCExhaustion test r=glandium
[gecko.git] / tools / lint / hooks_js_format.py
blob1b0386685e47fe54f2a60efad7cfaf2d8c181cb1
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",
21 sys.path[:0] = [os.path.join(topsrcdir, p) for p in EXTRA_PATHS]
23 from mozversioncontrol import InvalidRepoPath, get_repository_object
26 def run_js_format(hooktype, changedFiles):
27 try:
28 vcs = get_repository_object(topsrcdir)
29 except InvalidRepoPath:
30 return
32 if not changedFiles:
33 # No files have been touched
34 return
36 extensions = (".js", ".jsx", ".jsm", ".json", ".mjs", "sjs", "html", "xhtml")
37 path_list = []
38 for filename in sorted(changedFiles):
39 # Ignore files unsupported in eslint and prettier
40 if filename.endswith(extensions):
41 path_list.append(filename)
43 if not path_list:
44 # No files have been touched
45 return
47 arguments = ["eslint", "--fix"] + path_list
48 # On windows we need this to call the command in a shell, see Bug 1511594
49 if os.name == "nt":
50 js_format_cmd = ["sh", "mach"] + arguments
51 else:
52 js_format_cmd = [os.path.join(topsrcdir, "mach")] + arguments
53 if "commit" in hooktype:
54 # don't prevent commits, just display the eslint and prettier results
55 subprocess.call(js_format_cmd)
57 vcs.add_remove_files(*path_list)
59 return False
60 print("warning: '{}' is not a valid js-format hooktype".format(hooktype))
61 return False
64 def git():
65 hooktype = os.path.basename(__file__)
66 if hooktype == "hooks_js_format.py":
67 hooktype = "pre-push"
69 try:
70 changedFiles = check_output(
71 ["git", "diff", "--staged", "--diff-filter=d", "--name-only", "HEAD"],
72 text=True,
73 ).split()
74 # TODO we should detect if we are in a "add -p" mode and show a warning
75 return run_js_format(hooktype, changedFiles)
77 except CalledProcessError:
78 print("Command to retrieve local files failed")
79 return 1
82 if __name__ == "__main__":
83 sys.exit(git())