.pre-commit-config.yaml 7.7 KB

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