Skip to content

Commit b22dfb0

Browse files
committed
ci: add hooks for pre-commit checks
Added pre-commit hooks to check common errors in source code files and the commit message format. @TarantoolBot Title: Using pre-commit hooks Add support to use pre-commit hooks to check code against Golang and Python standards. Commit message checked for lines length and spelling errors. Subject of commit must follow these rules: - "module: at least three words description" - "module1/module2: three words info" - "Release v1.2.3" There is verification for references to issues in commit message. The preferred way is to install required tools and dependencies from `requirements.txt`: ```sh pip install -r test/requirements.txt ``` To install only the `pre-commit` tool itself, you can use `pip` to install it directly: ```sh pip install pre-commit ``` To install pre-commit hooks in a git repository, use: ```sh pre-commit install ``` After that, pre-commit hooks will be launched automatically at every commit. To skip a specific check in pre-commit, you can use: ```sh SKIP=<hook1-id>,<hook2-id> git commit -m "your commit message" ``` Be careful with this option, as it can cause you to miss important checks. It is best to use it only if you are sure you want to skip specific checks. Closes #TNTP-2937 PreCommit-Verified: Passed
1 parent 6e68ac7 commit b22dfb0

9 files changed

+407
-1
lines changed

.cspell-precommit.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
3+
"import": [
4+
"@cspell/dict-ru_ru/cspell-ext.json",
5+
],
6+
"language": "en,ru",
7+
"dictionaryDefinitions": [
8+
{
9+
"name": "project-words",
10+
"path": ".cspell_project-words.txt",
11+
"description": "Words used in this project",
12+
"addWords": true
13+
}
14+
],
15+
"dictionaries": [
16+
"project-words"
17+
]
18+
}

.cspell_project-words.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
catridge
2+
cli
3+
coredump
4+
distfiles
5+
dpkg
6+
etcdctl
7+
evaler
8+
getenv
9+
golangci
10+
Infof
11+
iproto
12+
isort
13+
logfile
14+
luarocks
15+
MAKEFLAGS
16+
mapstructure
17+
memtx
18+
myapp
19+
packignore
20+
pidfile
21+
postinst
22+
preinst
23+
protoc
24+
pytest
25+
rebootstrap
26+
replicaset
27+
replicasets
28+
rocks
29+
rockspec
30+
sslcafile
31+
sslcertfile
32+
sslciphers
33+
sslkeyfile
34+
stateboard
35+
Svacer
36+
tarantool
37+
tarantoolctl
38+
tarantools
39+
testdata
40+
testfull
41+
TNTP
42+
ttable
43+
venv
44+
vshard
45+
Warnf
46+
xlog
47+
xlogs

.gitlint-precommit.ini

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[general]
2+
debug = true
3+
regex-style-search = true
4+
5+
[ignore-by-title]
6+
regex = ^build\(deps\):.*dependabot\[bot\]$
7+
ignore = title-max-length,body-min-length
8+
9+
[title-min-length]
10+
min-length=12
11+
12+
[title-max-length]
13+
line-length = 50
14+
15+
[title-match-regex]
16+
; Allows following subjects like:
17+
; - "module: at least three words description"
18+
; - "module1/module2: three words info"
19+
; - "Release v1.2.3"
20+
regex = ^(?:[\w\/-]+: (?:\S+ ){2,}\S+.*?|Release\s+v?\d+\.\d+\.\d)$
21+
22+
[body-min-length]
23+
; Note: to allow almost empty commit body with single text: "Closes #123"
24+
min-length=10
25+
26+
[body-max-line-length]
27+
line-length = 72
28+
29+
[body-match-regex:reference_task_id]
30+
; Task ID can be in digital format as "#123" or with capital letters "#ABC-123"
31+
; Allows following reference like:
32+
; - Closes #123
33+
; - Closes #TNTP-123
34+
; - Fixes #TNTP-123
35+
; - Related to #123
36+
; - Part of #TNTP-123
37+
; - Needed for #123
38+
regex = (?m)^(?:Closes|Fixes|Related to|Part of|Needed for) #(?:[A-Z]+-)?\d+$
39+
40+
; TODO: If required to let following markers: "See also #1234, #TNTP-3456, ..." (#TNTP-3108).
41+
; https://www.tarantool.io/en/doc/latest/contributing/developer_guidelines/#how-to-write-a-commit-message
42+
; Need solve how to enable both regexes at once. Now they works as AND rules, while we need here OR.
43+
;? [body-match-regex:multiple_tasks]
44+
;? regex = (?m)^See also (?:#(?:[A-Z]+-)?\d+(?:\b|[,;] ?| )){1,}$

.markdownlint.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See full set of rules:
2+
# - https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md
3+
# - https://github.com/DavidAnson/markdownlint/blob/main/schema/.markdownlint.yaml
4+
#
5+
# Note: Different rules could be specified for different subdirectories. Put similar rules
6+
# in a subdirectory and adjust them.
7+
8+
# MD004/ul-style : Unordered list style : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md004.md
9+
MD004:
10+
# List style
11+
style: sublist
12+
13+
# MD013/line-length : Line length : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md013.md
14+
MD013:
15+
# Number of characters
16+
line_length: 80
17+
# Number of characters for headings
18+
heading_line_length: 80
19+
# Number of characters for code blocks
20+
code_block_line_length: 80
21+
22+
# MD024/no-duplicate-heading : Multiple headings with the same content : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md024.md
23+
MD024:
24+
# Only check sibling headings
25+
siblings_only: true
26+
27+
# MD033/no-inline-html : Inline HTML : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md033.md
28+
MD033:
29+
# Allowed elements
30+
allowed_elements:
31+
- version
32+
33+
# MD046/code-block-style : Code block style : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md046.md
34+
MD046:
35+
# Block style
36+
style: underscore
37+
38+
# MD050/strong-style : Strong style : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md050.md
39+
MD050:
40+
# Strong style
41+
style: asterisk

.pre-commit-config.yaml

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
default_install_hook_types:
2+
- pre-commit
3+
- commit-msg
4+
5+
repos:
6+
- repo: https://github.com/pre-commit/pre-commit-hooks
7+
rev: v5.0.0
8+
hooks:
9+
- id: trailing-whitespace
10+
- id: end-of-file-fixer
11+
- id: check-yaml
12+
stages: [pre-commit]
13+
# Ignore some Yaml's with following pattern in `testdata` subdirectories.
14+
exclude: testdata/.*?(?:invalid|bad|broken|wrong)[^/]*\.ya?ml$
15+
- id: check-merge-conflict
16+
stages: [pre-commit]
17+
- id: no-commit-to-branch
18+
stages: [pre-commit]
19+
args: [--branch, master, --pattern, release/.*]
20+
- id: file-contents-sorter
21+
name: "Sort cspell dictionary"
22+
stages: [pre-commit]
23+
files: ".cspell_project-words.txt"
24+
args: [--ignore-case, --unique]
25+
26+
- repo: https://github.com/codespell-project/codespell
27+
rev: v2.4.1
28+
hooks:
29+
- id: codespell
30+
name: "Check for common misspellings"
31+
stages: [pre-commit]
32+
types_or: [go, python, lua]
33+
additional_dependencies:
34+
- tomli
35+
36+
- repo: https://github.com/streetsidesoftware/cspell-cli
37+
rev: v9.0.1
38+
hooks:
39+
- id: cspell
40+
name: "Spell-checking documentation files"
41+
stages: [pre-commit]
42+
args:
43+
[--config=.cspell-precommit.json, --show-context, --show-suggestions]
44+
types_or: [markdown]
45+
additional_dependencies:
46+
- "@cspell/dict-markdown"
47+
- "@cspell/dict-ru_ru"
48+
49+
- id: cspell
50+
name: "Spell-checking commit message"
51+
stages: [commit-msg]
52+
args:
53+
[--config=.cspell-precommit.json, --show-context, --show-suggestions]
54+
additional_dependencies:
55+
- "@cspell/dict-ru_ru"
56+
# TODO: add words from [.go, .py] with "errors" to `.cspell_project-words.txt` (#TNTP-3105).
57+
# - id: cspell
58+
# name: "Py: Spell check files"
59+
# types: [python]
60+
# additional_dependencies:
61+
# - "@cspell/dict-python"
62+
# - id: cspell
63+
# name: "Go: Spell check files"
64+
# types: [go]
65+
# additional_dependencies:
66+
# - "@cspell/dict-golang"
67+
68+
- repo: local
69+
hooks:
70+
- id: go-format
71+
language: golang
72+
types: [go]
73+
name: "Go: formatting sources"
74+
stages: [pre-commit]
75+
exclude: \.pb\.go$
76+
entry: bash -c "GOFUMPT_SPLIT_LONG_LINES=on gofumpt $@"
77+
args: [-e, -w, -extra]
78+
additional_dependencies:
79+
- mvdan.cc/[email protected]
80+
81+
- repo: https://github.com/segmentio/golines
82+
rev: fc305205784a70b4cfc17397654f4c94e3153ce4
83+
hooks:
84+
- id: golines
85+
name: "Go: shorten long lines"
86+
stages: [pre-commit]
87+
exclude: \.pb\.go$
88+
args: [--max-len=100, --tab-len=4, --no-reformat-tags]
89+
90+
- repo: https://github.com/golangci/golangci-lint
91+
rev: v2.1.6
92+
hooks:
93+
- id: golangci-lint
94+
name: "Go: verify sources"
95+
description: |
96+
Checking only `staged` changes, added to be committed.
97+
Issue `golangci-lint` command does not have an option to accept specified list of files.
98+
pass_filenames: false
99+
stages: [pre-commit]
100+
args: [--fast-only, --config=golint-precommit.yml]
101+
102+
- id: golangci-lint-full
103+
name: "Go: full verify sources"
104+
description: "Command to run: `pre-commit run --hook-stage manual golangci-lint-full`"
105+
stages: [manual]
106+
args: [--config=golint-precommit.yml]
107+
108+
- repo: https://github.com/astral-sh/ruff-pre-commit
109+
rev: v0.11.11
110+
hooks:
111+
- id: ruff-format
112+
name: "Py: Run the formatter"
113+
stages: [pre-commit]
114+
files: test/.*\.py$
115+
116+
- id: ruff-check
117+
name: "Py: Run the linter"
118+
args: [--fix]
119+
stages: [pre-commit]
120+
files: test/.*\.py$
121+
122+
- repo: https://github.com/DavidAnson/markdownlint-cli2
123+
rev: v0.18.1
124+
hooks:
125+
- id: markdownlint-cli2
126+
name: "MD: check markdown files"
127+
stages: [pre-commit]
128+
# TODO: configure the rules and fix the rest of the .md files (#TNTP-3107).
129+
files: CHANGELOG.md
130+
args: [--fix]
131+
132+
- repo: https://github.com/jorisroovers/gitlint
133+
rev: v0.19.1
134+
hooks:
135+
- id: gitlint
136+
name: "Verify commit message format"
137+
stages: [commit-msg]
138+
args: [--staged, --config, .gitlint-precommit.ini, --msg-filename]

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- Add support manage installed `tcm` versions via `tt binaries` CLI.
2323
- Added support for completion with shell `fish` see
2424
the command `tt completion fish`.
25+
- Repository use `pre-commit` hooks to check code style.
2526

2627
### Changed
2728

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ To run full set of tests:
251251
mage testfull
252252
```
253253

254+
### Commit changes
255+
256+
The project uses [pre-commit](https://pre-commit.com/) to check code before committing.
257+
After activating the virtual environment for [run tests](#run-tests), the `pre-commit` hooks must be installed in the repository:
258+
259+
``` console
260+
pre-commit install
261+
```
262+
263+
Now you will perform automatic checks at every commit.
264+
The first time you use `pre-commit` it will install the dependencies needed for it to work, which may take some time.
265+
Subsequent runs will be much faster.
266+
267+
If errors were found, the commit will fail. If the errors can be corrected automatically, you will receive modified files to add to the commit, checking for correctness.
268+
In other cases you will get a message with errors that need to be corrected manually.
269+
254270
## Configuration
255271

256272
Taratool CLI can be launched in several modes:
@@ -438,7 +454,7 @@ Arguments of Tarantool CLI:
438454

439455
### Autocompletion
440456

441-
You can generate autocompletion for `bash` or `zsh` shell:
457+
You can generate autocompletion for `bash`, `zsh` and `fish` shell:
442458

443459
``` console
444460
. <(tt completion bash)

0 commit comments

Comments
 (0)