#!/usr/bin/env python3 # 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/. # # Pre-commit hook for newtab developers. # # Install: # ln -sf "$(git rev-parse --show-toplevel)/tools/lint/hooks_newtab.py" \ # "$(git rev-parse --show-toplevel)/.git/hooks/pre-commit" # # Configure tests (optional, default is none): # git config newtab.pre-commit.tests none # skip tests (default) # git config newtab.pre-commit.tests jest # Jest only (~30s) # git config newtab.pre-commit.tests jest-karma # Jest + Karma/Enzyme (~3-5min) # git config newtab.pre-commit.tests all # all tests including xpcshell and browser (10+ min)
# Runs three checks in order, stopping at the first failure: # 1. Bundle: if newtab source or bundle output files are staged, runs # `mach newtab bundle` and blocks if the output differs from what is staged. # 2. Lint: runs `mach lint --workdir=staged` and blocks on errors. # 3. Tests: optionally runs unit tests based on `newtab.pre-commit.tests` git config.
import os import shutil import signal import subprocess import sys from subprocess import CalledProcessError, check_output
RED = "\033[31m"if sys.stderr.isatty() else""
RESET = "\033[0m"if sys.stderr.isatty() else""
here = os.path.dirname(os.path.realpath(__file__))
topsrcdir = os.path.join(here, os.pardir, os.pardir)
def newtab_files_staged(): try:
staged = check_output(
["git", "diff", "--staged", "--diff-filter=d", "--name-only", "HEAD"],
text=True,
stderr=subprocess.DEVNULL,
).split() return any(f.startswith(f"{NEWTAB_SRC}/") or f in BUNDLE_FILES for f in staged) except CalledProcessError: returnFalse
def check_bundle(python):
print("[newtab hook] Newtab source files staged — verifying bundle...")
ret = run_process([python, os.path.join(topsrcdir, "mach"), "newtab", "bundle"]) if ret != 0: return ret
if run_process(["git", "diff", "--quiet", "--"] + BUNDLE_FILES) != 0:
dirty = check_output(
["git", "diff", "--name-only", "--"] + BUNDLE_FILES, text=True
).strip()
print(
f"\n{RED}[newtab hook] ERROR: Bundle output changed after running './mach newtab bundle'.{RESET}"
)
print(
f"{RED}[newtab hook] Please stage the following files before committing:{RESET}"
)
print(dirty) return1
print("[newtab hook] Bundle is up to date.") return0
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.