diff --git a/.bazelignore b/.bazelignore index c7ca8d09784be38d09265f6af319ceeec1a5dea8..f9e148ef78a97cef4e0a298687aad5037d61ad03 100644 --- a/.bazelignore +++ b/.bazelignore @@ -1 +1,2 @@ e2e +.cache diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a8d317f334bf948e8e27f3f5e2a44730cd28e280..0857033cd8e4c5086874fb28b73d4bb7a51a4ea0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,6 +4,10 @@ include: oses: - linux - macos + # TODO: allow default `versions` once `--repo_contents_cache` is resolved + versions: + - 7.4.0 + - 7.x configs: - local diff --git a/hooks/.pre-commit-config.yaml b/hooks/.pre-commit-config.yaml index 905853255c9ee7e3b80f714d7ce681980adbfb99..0502b6d8298ee8268ed924bd32eb0a3c485bf735 100644 --- a/hooks/.pre-commit-config.yaml +++ b/hooks/.pre-commit-config.yaml @@ -1,71 +1,65 @@ # Regenerate with `bazel run -- //hooks:config`. Do not edit minimum_pre_commit_version: 2.18.0 default_install_hook_types: -- pre-commit + - pre-commit repos: -- repo: local - hooks: - - id: //pre-commit/hook/buildifier:format - name: Format Bazel files - description: Will load a `.buildifier.json` at the root of the workspace for extra - configuration. - language: system - require_serial: true - stages: - - pre-commit - entry: bazel run --config=pre-commit -- //pre-commit/hook/buildifier:format - types_or: - - bazel - - id: //pre-commit/hook/buildifier:lint - name: Lint Bazel files - description: Will load a `.buildifier.json` at the root of the workspace for extra - configuration. - language: system - require_serial: true - stages: - - pre-commit - entry: bazel run --config=pre-commit -- //pre-commit/hook/buildifier:lint - types_or: - - bazel - - id: //pre-commit/hook/ruff:check - name: Check Python files with `ruff` - description: Performs checks with `ruff` against Python files. Will read a `ruff.toml` - for configuration. - language: system - require_serial: true - stages: - - pre-commit - entry: bazel run --config=pre-commit -- //pre-commit/hook/ruff:check - types: - - python - - id: //pre-commit/hook/ruff:format - name: Format Python files with `ruff` - description: Performs formatting with `ruff` against Python files. Will read a - `ruff.toml` for configuration. - language: system - require_serial: true - stages: - - pre-commit - entry: bazel run --config=pre-commit -- //pre-commit/hook/ruff:format - types: - - python - - id: //pre-commit/hook/check-newline-at-end-of-file - name: Validate newline at the end of text files - description: Uses Python `mmap` function to efficiently load the last character - of the file and validate it is an end-of-line character (\r|\n) - language: system - require_serial: true - stages: - - pre-commit - entry: bazel run --config=pre-commit -- //pre-commit/hook/check-newline-at-end-of-file - - id: //hooks:config - name: Update `pre-commit` config - description: Keeps the `.pre-commit-config.yaml` in sync with any changes to the - `pre_commit_config` rule. - language: system - require_serial: true - stages: - - pre-commit - entry: bazel run --config=pre-commit -- //hooks:config - pass_filenames: false - files: ^(MODULE.bazel.lock|hooks/BUILD.bazel)$ + - repo: local + hooks: + - id: //pre-commit/hook/buildifier:format + name: Format Bazel files + description: Will load a `.buildifier.json` at the root of the workspace for extra configuration. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //pre-commit/hook/buildifier:format + types_or: + - bazel + - id: //pre-commit/hook/buildifier:lint + name: Lint Bazel files + description: Will load a `.buildifier.json` at the root of the workspace for extra configuration. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //pre-commit/hook/buildifier:lint + types_or: + - bazel + - id: //pre-commit/hook/ruff:check + name: Check Python files with `ruff` + description: Performs checks with `ruff` against Python files. Will read a `ruff.toml` for configuration. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //pre-commit/hook/ruff:check + types: + - python + - id: //pre-commit/hook/ruff:format + name: Format Python files with `ruff` + description: Performs formatting with `ruff` against Python files. Will read a `ruff.toml` for configuration. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //pre-commit/hook/ruff:format + types: + - python + - id: //pre-commit/hook/check-newline-at-end-of-file + name: Validate newline at the end of text files + description: Uses Python `mmap` function to efficiently load the last character of the file and validate it is an end-of-line character (\r|\n) + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //pre-commit/hook/check-newline-at-end-of-file + - id: //hooks:config + name: Update `pre-commit` config + description: Keeps the `.pre-commit-config.yaml` in sync with any changes to the `pre_commit_config` rule. + language: system + require_serial: true + stages: + - pre-commit + entry: bazel run --config=pre-commit -- //hooks:config + pass_filenames: false + files: ^(MODULE.bazel.lock|hooks/BUILD.bazel)$ diff --git a/pre-commit/config/BUILD.bazel b/pre-commit/config/BUILD.bazel index e21397114394a08d93f279ab1cca12be5bf91960..e6740cb46847ac3b314a14df05695b987d7bf27e 100644 --- a/pre-commit/config/BUILD.bazel +++ b/pre-commit/config/BUILD.bazel @@ -15,6 +15,7 @@ py_library( srcs = [ "__init__.py", "cli.py", + "dumper.py", ], deps = [ ":pyyaml", diff --git a/pre-commit/config/cli.py b/pre-commit/config/cli.py index 0239998927b0bc54c603fdcbbc35439c5283e40a..cceabc989b3b27230fbf49f82d6f90fe3fb8a832 100644 --- a/pre-commit/config/cli.py +++ b/pre-commit/config/cli.py @@ -12,11 +12,7 @@ from typing import Collection, Literal, NoReturn, Protocol, TypeAlias, TypeVar from python.runfiles import Runfiles -try: - from yaml import CSafeDumper as Dumper -except ImportError: - from yaml import SafeDumper as Dumper - +from .dumper import Dumper from ..hook import Hook, from_json, representer @@ -122,7 +118,7 @@ def dump( ], } - dumper = Dumper(output, sort_keys=False) + dumper = Dumper(output) dumper.add_representer(Hook, representer) dumper.open() try: diff --git a/pre-commit/config/dumper.py b/pre-commit/config/dumper.py new file mode 100644 index 0000000000000000000000000000000000000000..a183b5800d39ad4bf653a550c20454028ee8b900 --- /dev/null +++ b/pre-commit/config/dumper.py @@ -0,0 +1,17 @@ +from yaml.emitter import Emitter +from yaml.serializer import Serializer +from yaml.representer import SafeRepresenter +from yaml.resolver import Resolver + + +class IndentingEmitter(Emitter): + def increase_indent(self, flow=False, indentless=False): + return super().increase_indent(flow=False, indentless=False) + + +class Dumper(IndentingEmitter, Serializer, SafeRepresenter, Resolver): + def __init__(self, stream, sort_keys=False): + IndentingEmitter.__init__(self, stream, width=4096) + Serializer.__init__(self) + SafeRepresenter.__init__(self, sort_keys=sort_keys) + Resolver.__init__(self)