.pre-commit-config.yaml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.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. - id: markdown-toc
  34. - repo: local
  35. hooks:
  36. - id: fix-cc-deps
  37. name: Fix missing C++ deps
  38. entry: scripts/fix_cc_deps.py
  39. language: python
  40. files: ^.*/(BUILD|[^/]+\.(h|cpp))$
  41. pass_filenames: false
  42. # Formatters should be run late so that they can re-format any prior changes.
  43. - repo: https://github.com/psf/black
  44. rev: 8a737e727ac5ab2f1d4cf5876720ed276dc8dc4b # frozen: 25.1.0
  45. hooks:
  46. - id: black
  47. - repo: local
  48. hooks:
  49. - id: prettier
  50. name: prettier
  51. language: node
  52. additional_dependencies: ['prettier@3.3.3']
  53. types_or: [html, javascript, json, markdown, yaml]
  54. entry: npx prettier@3.3.3 --write --log-level=warn
  55. - repo: local
  56. hooks:
  57. - id: buildifier
  58. name: Bazel buildifier
  59. entry: scripts/run_buildifier.py
  60. # Beyond just formatting, explicitly fix lint warnings.
  61. args: ['--lint=fix', '--warnings=all', '-r', '.']
  62. language: python
  63. files: |
  64. (?x)^(
  65. .*BUILD.*|
  66. .*MODULE.bazel.*|
  67. .*WORKSPACE.*|
  68. .*\.bzl
  69. )$
  70. - id: check-bazel-mod-deps
  71. # Check this after buildifier because buildifier may modify inputs, and
  72. # MODULE.bazel.lock includes line/column details.
  73. name: Check bazel mod deps
  74. entry: scripts/bazel_mod_deps.py
  75. language: python
  76. files: |
  77. (?x)^(
  78. .*MODULE.bazel.*|
  79. .*WORKSPACE.*
  80. )$
  81. - id: clang-format
  82. name: clang-format
  83. entry: clang-format
  84. types_or: [c++, def]
  85. language: python
  86. args: ['-i']
  87. additional_dependencies: ['clang-format==20.1.4']
  88. - repo: local
  89. hooks:
  90. - id: check-header-guards
  91. # This should run after clang-format, which may reformat a guard.
  92. name: Check header guards
  93. entry: scripts/check_header_guards.py
  94. language: python
  95. files: ^.*\.h$
  96. - id: check-sha-filenames
  97. # This may rename files, so it's deliberately between formatters and
  98. # linters.
  99. name: Check fuzzer SHA filenames
  100. entry: scripts/check_sha_filenames.py
  101. language: python
  102. files: ^.*/fuzzer_corpus/.*$
  103. - id: check-toolchain-diagnostics
  104. name: Check toolchain diagnostics
  105. entry: toolchain/diagnostics/check_diagnostics.py
  106. language: python
  107. files: |
  108. (?x)^(
  109. toolchain/.*\.cpp|
  110. toolchain/.*\.h|
  111. toolchain/diagnostics/check_diagnostics\.py|
  112. toolchain/diagnostics/diagnostic_kind\.def
  113. )$
  114. pass_filenames: false
  115. # Run linters last, as formatters and other checks may fix issues.
  116. - repo: local
  117. hooks:
  118. - id: forbid-llvm-googletest
  119. name: Checks for deps on LLVM's version of GoogleTest
  120. entry: scripts/forbid_llvm_googletest.py
  121. language: python
  122. files: ^.*/BUILD$
  123. pass_filenames: false
  124. - repo: https://github.com/PyCQA/flake8
  125. rev: 4b5e89b4b108a6c1a000c591d334a99a80d34c7b # frozen: 7.2.0
  126. hooks:
  127. - id: flake8
  128. - repo: https://github.com/pre-commit/mirrors-mypy
  129. rev: 'f40886d54c729f533f864ed6ce584e920feb0af7' # frozen: v1.15.0
  130. hooks:
  131. - id: mypy
  132. # Use setup.cfg to match the command line.
  133. args:
  134. - --config-file=setup.cfg
  135. # This should match the requirements added in the WORKSPACE pip_install.
  136. additional_dependencies:
  137. - gql >= 2.0.0, < 3.0.0
  138. - PyGitHub
  139. # Exclusions are:
  140. # - p#### scripts because they're not tested or maintained.
  141. # - lit.cfg.py because it has multiple copies, breaking mypy.
  142. # - Unit tests because they sometimes violate typing, such as by
  143. # assigning a mock to a function.
  144. exclude: |
  145. (?x)^(
  146. proposals/(?!scripts/).*|
  147. .*/lit\.cfg\.py|
  148. .*_test\.py
  149. )$
  150. - repo: https://github.com/codespell-project/codespell
  151. rev: 63c8f8312b7559622c0d82815639671ae42132ac # frozen: v2.4.1
  152. hooks:
  153. - id: codespell
  154. args: ['-I', '.codespell_ignore', '--uri-ignore-words-list', '*']
  155. # Test data may contain intentional misspellings, as well as short,
  156. # meaningless identifiers that codespell incorrectly identifies as
  157. # typos but that we would want to detect in other contexts.
  158. exclude: |
  159. (?x)^(
  160. .*/testdata/.*|
  161. .*/fuzzer_corpus/.*
  162. )$
  163. - repo: https://github.com/google/pre-commit-tool-hooks
  164. rev: efaea7c61c774c0b1a9805fd999e754a2d19dbd1 # frozen: v1.2.5
  165. hooks:
  166. - id: check-copyright
  167. args:
  168. - --copyright
  169. - |+
  170. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  171. Exceptions. See /LICENSE for license information.
  172. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  173. - --custom_format
  174. - '\.(carbon|c|json|scss|ypp)(\.tmpl)?$'
  175. - ''
  176. - '// '
  177. - ''
  178. - --custom_format
  179. - '\.(js|ts|mjs)$'
  180. - '/*'
  181. - ' * '
  182. - ' */'
  183. - --custom_format
  184. - '\.(l|lpp|y)$'
  185. - '/*'
  186. - ''
  187. - '*/'
  188. - --custom_format
  189. - '\.(plist)$'
  190. - '<!--'
  191. - ''
  192. - '\-->'
  193. - --custom_format
  194. - '\.vim$'
  195. - ''
  196. - '" '
  197. - ''
  198. - --custom_format
  199. - '\.scm$'
  200. - ''
  201. - '; '
  202. - ''
  203. - --custom_format
  204. - '\.lua$'
  205. - ''
  206. - '-- '
  207. - ''
  208. exclude: |
  209. (?x)^(
  210. .bazelversion|
  211. .github/pull_request_template.md|
  212. .python-version|
  213. compile_flags.txt|
  214. github_tools/requirements.txt|
  215. third_party/.*|
  216. utils/vscode/esbuild.js|
  217. website/.ruby-version|
  218. website/Gemfile.lock|
  219. .*\.def|
  220. .*\.png|
  221. .*\.svg|
  222. .*/fuzzer_corpus/.*|
  223. .*/testdata/.*\.golden
  224. )$
  225. - id: check-links
  226. # This excludes third-party code, and patches to third-party code.
  227. exclude: |
  228. (?x)^(
  229. MODULE.bazel.lock|
  230. bazel/bazel_clang_tidy/.*\.patch|
  231. bazel/google_benchmark/.*\.patch|
  232. bazel/libpfm/.*\.patch|
  233. bazel/llvm_project/.*\.patch|
  234. third_party/examples/.*/carbon/.*|
  235. )$