.pre-commit-config.yaml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. # Update versions with:
  5. # pre-commit autoupdate --freeze && pre-commit run -a
  6. # See https://pre-commit.com for more information
  7. # See https://pre-commit.com/hooks.html for more hooks
  8. default_language_version:
  9. python: python3 # Defaults to python2, so override it.
  10. repos:
  11. - repo: https://github.com/pre-commit/pre-commit-hooks
  12. rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0
  13. hooks:
  14. - id: check-added-large-files
  15. - id: check-case-conflict
  16. - id: check-executables-have-shebangs
  17. - id: check-merge-conflict
  18. - id: check-symlinks
  19. exclude: ^bazel-execroot$
  20. - id: check-yaml
  21. - id: detect-private-key
  22. - id: end-of-file-fixer
  23. exclude: '^(.*/fuzzer_corpus/.*|.*\.svg)$'
  24. - id: mixed-line-ending
  25. args: ['--fix=lf']
  26. exclude: '^(.*/fuzzer_corpus/.*|.*\.svg)$'
  27. - id: trailing-whitespace
  28. exclude: '^(.*/fuzzer_corpus/.*|.*/testdata/.*\.golden|.*\.svg)$'
  29. - repo: https://github.com/google/pre-commit-tool-hooks
  30. rev: efaea7c61c774c0b1a9805fd999e754a2d19dbd1 # frozen: v1.2.5
  31. hooks:
  32. - id: check-google-doc-style
  33. exclude: |
  34. (?x)^(
  35. .*\.agents/.*|
  36. .*AGENTS.md
  37. )$
  38. - id: markdown-toc
  39. - repo: local
  40. hooks:
  41. - id: fix-cc-deps
  42. name: Fix missing C++ deps
  43. entry: scripts/fix_cc_deps.py
  44. language: python
  45. files: ^.*/(BUILD|[^/]+\.(h|cpp))$
  46. pass_filenames: false
  47. # Formatters should be run late so that they can re-format any prior changes.
  48. - repo: https://github.com/psf/black
  49. rev: 35ea67920b7f6ac8e09be1c47278752b1e827f76 # frozen: 26.3.0
  50. hooks:
  51. - id: black
  52. - repo: local
  53. hooks:
  54. - id: prettier
  55. name: prettier
  56. language: node
  57. # TODO: Not upgrading to/past 3.4.0 due to list indent changes that may
  58. # get fixed. See: https://github.com/prettier/prettier/issues/16929
  59. additional_dependencies: ['prettier@3.3.3']
  60. types_or: [html, javascript, json, markdown, yaml]
  61. entry: npx prettier@3.3.3 --write --log-level=warn
  62. - repo: local
  63. hooks:
  64. - id: buildifier
  65. name: Bazel buildifier
  66. entry: scripts/run_buildifier.py
  67. # Beyond just formatting, explicitly fix lint warnings.
  68. args: ['--lint=fix', '--warnings=all', '-r', '.']
  69. language: python
  70. files: |
  71. (?x)^(
  72. .*BUILD.*|
  73. .*MODULE.bazel.*|
  74. .*WORKSPACE.*|
  75. .*\.bzl
  76. )$
  77. - id: check-bazel-mod-deps
  78. # Check this after buildifier because buildifier may modify inputs, and
  79. # MODULE.bazel.lock includes line/column details.
  80. name: Check bazel mod deps
  81. entry: scripts/bazel_mod_deps.py
  82. language: python
  83. files: |
  84. (?x)^(
  85. .*MODULE.bazel.*|
  86. .*WORKSPACE.*
  87. )$
  88. - id: clang-format
  89. name: clang-format
  90. entry: clang-format
  91. types_or: [c++, def]
  92. language: python
  93. args: ['-i']
  94. additional_dependencies: ['clang-format==21.1.8']
  95. - repo: local
  96. hooks:
  97. - id: check-header-guards
  98. # This should run after clang-format, which may reformat a guard.
  99. name: Check header guards
  100. entry: scripts/check_header_guards.py
  101. language: python
  102. files: ^.*\.h$
  103. - id: check-sha-filenames
  104. # This may rename files, so it's deliberately between formatters and
  105. # linters.
  106. name: Check fuzzer SHA filenames
  107. entry: scripts/check_sha_filenames.py
  108. language: python
  109. files: ^.*/fuzzer_corpus/.*$
  110. - id: check-toolchain-diagnostics
  111. name: Check toolchain diagnostics
  112. entry: toolchain/diagnostics/check_diagnostics.py
  113. language: python
  114. files: |
  115. (?x)^(
  116. toolchain/.*\.cpp|
  117. toolchain/.*\.h|
  118. toolchain/diagnostics/check_diagnostics\.py|
  119. toolchain/diagnostics/diagnostic_kind\.def
  120. )$
  121. pass_filenames: false
  122. # Run linters last, as formatters and other checks may fix issues.
  123. - repo: local
  124. hooks:
  125. - id: forbid-llvm-googletest
  126. name: Checks for deps on LLVM's version of GoogleTest
  127. entry: scripts/forbid_llvm_googletest.py
  128. language: python
  129. files: ^.*/BUILD$
  130. pass_filenames: false
  131. - repo: https://github.com/PyCQA/flake8
  132. rev: d93590f5be797aabb60e3b09f2f52dddb02f349f # frozen: 7.3.0
  133. hooks:
  134. - id: flake8
  135. - repo: https://github.com/pre-commit/mirrors-mypy
  136. rev: 'a66e98df7b4aeeb3724184b332785976d062b92e' # frozen: v1.19.1
  137. hooks:
  138. - id: mypy
  139. # Use setup.cfg to match the command line.
  140. args:
  141. - --config-file=setup.cfg
  142. # This should match the requirements added in the WORKSPACE pip_install.
  143. additional_dependencies:
  144. - gql >= 2.0.0, < 3.0.0
  145. - PyGitHub
  146. - rich
  147. # Exclusions are:
  148. # - p#### scripts because they're not tested or maintained.
  149. # - lit.cfg.py because it has multiple copies, breaking mypy.
  150. # - `bazel_test_runner.py` which depends on Bazel-specific imports.
  151. # - Unit tests because they sometimes violate typing, such as by
  152. # assigning a mock to a function.
  153. exclude: |
  154. (?x)^(
  155. proposals/(?!scripts/).*|
  156. .*/lit\.cfg\.py|
  157. examples/bazel_test_runner\.py|
  158. .*_test\.py
  159. )$
  160. - repo: https://github.com/codespell-project/codespell
  161. rev: 2ccb47ff45ad361a21071a7eedda4c37e6ae8c5a # frozen: v2.4.2
  162. hooks:
  163. - id: codespell
  164. args: ['-I', '.codespell_ignore', '--uri-ignore-words-list', '*']
  165. # Test data may contain intentional misspellings, as well as short,
  166. # meaningless identifiers that codespell incorrectly identifies as
  167. # typos but that we would want to detect in other contexts.
  168. exclude: |
  169. (?x)^(
  170. .*/testdata/.*|
  171. .*/fuzzer_corpus/.*
  172. )$
  173. - repo: https://github.com/google/pre-commit-tool-hooks
  174. rev: efaea7c61c774c0b1a9805fd999e754a2d19dbd1 # frozen: v1.2.5
  175. hooks:
  176. - id: check-copyright
  177. args:
  178. - --copyright
  179. - |+
  180. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  181. Exceptions. See /LICENSE for license information.
  182. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  183. - --custom_format
  184. - '\.(carbon|c|json|scss|ypp)(\.tmpl)?$'
  185. - ''
  186. - '// '
  187. - ''
  188. - --custom_format
  189. - '\.(js|ts|mjs)$'
  190. - '/*'
  191. - ' * '
  192. - ' */'
  193. - --custom_format
  194. - '\.(l|lpp|y)$'
  195. - '/*'
  196. - ''
  197. - '*/'
  198. - --custom_format
  199. - '\.(plist)$'
  200. - '<!--'
  201. - ''
  202. - '\-->'
  203. - --custom_format
  204. - '\.vim$'
  205. - ''
  206. - '" '
  207. - ''
  208. - --custom_format
  209. - '\.scm$'
  210. - ''
  211. - '; '
  212. - ''
  213. - --custom_format
  214. - '\.lua$'
  215. - ''
  216. - '-- '
  217. - ''
  218. exclude: |
  219. (?x)^(
  220. .bazelversion|
  221. .github/pull_request_template.md|
  222. .python-version|
  223. compile_flags.txt|
  224. github_tools/requirements.txt|
  225. third_party/.*|
  226. utils/vscode/esbuild.js|
  227. website/.ruby-version|
  228. website/Gemfile.lock|
  229. .*\.def|
  230. .*\.png|
  231. .*\.svg|
  232. .*/fuzzer_corpus/.*|
  233. .*/testdata/.*\.golden
  234. )$
  235. - id: check-links
  236. - repo: local
  237. hooks:
  238. - id: check-build-graph
  239. name: Check build graph
  240. entry: scripts/check_build_graph.py
  241. language: python
  242. files: |
  243. (?x)^(
  244. .*BUILD.*|
  245. .*MODULE.bazel.*|
  246. .*WORKSPACE.*|
  247. .*\.bzl
  248. )$
  249. # This excludes third-party code, and patches to third-party code.
  250. exclude: |
  251. (?x)^(
  252. MODULE.bazel.lock|
  253. .*package-lock\.json|
  254. bazel/bazel_clang_tidy/.*\.patch|
  255. bazel/google_benchmark/.*\.patch|
  256. bazel/libpfm/.*\.patch|
  257. bazel/llvm_project/.*\.patch|
  258. third_party/examples/.*/carbon/.*|
  259. )$