.pre-commit-config.yaml 7.3 KB

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