Skip to content

Commit 7d1bcc8

Browse files
authored
feat(prek): supporting prek as an alternative to pre-commit and switching to prek (#1799)
* feat(prek): support 'prek' as an alternative to 'pre-commit' in project_info * build(prek): replace 'pre-commit' with 'prek' in project configuration * docs(auto_check): update installation instructions to include 'prek' as an alternative to 'pre-commit' * test: brought back the `pre-commit` tests along with `prek` * docs(README): update badge to reflect support for 'prek' instead of 'pre-commit' * refactor(project_info): enhance pre-commit check to support multiple tools * docs(auto_check): update auto check installation by only keeping one example * docs: update documentation to reflect 'prek' integration and improve clarity * docs(auto_check): add tip for using pre-commit framework alongside prek * docs: improve formatting and clarity for rendering * docs(auto_check): clarify usage of pre-commit framework in instructions
1 parent c176f7c commit 7d1bcc8

File tree

7 files changed

+80
-34
lines changed

7 files changed

+80
-34
lines changed

commitizen/project_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def is_pre_commit_installed() -> bool:
9-
return bool(shutil.which("pre-commit"))
9+
return any(shutil.which(tool) for tool in ("pre-commit", "prek"))
1010

1111

1212
def get_default_version_provider() -> Literal[

docs/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Conda Version](https://img.shields.io/conda/vn/conda-forge/commitizen?style=flat-square)](https://anaconda.org/conda-forge/commitizen)
88
[![homebrew](https://img.shields.io/homebrew/v/commitizen?color=teal&style=flat-square)](https://formulae.brew.sh/formula/commitizen)
99
[![Codecov](https://img.shields.io/codecov/c/github/commitizen-tools/commitizen.svg?style=flat-square)](https://codecov.io/gh/commitizen-tools/commitizen)
10-
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?style=flat-square&logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
10+
[![prek](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json&style=flat-square&color=brightgreen)](https://github.com/j178/prek)
1111

1212
![Using Commitizen cli](images/demo.gif)
1313

@@ -182,7 +182,7 @@ This command is particularly useful for automation scripts and CI/CD pipelines.
182182

183183
For example, you can use the output of the command `cz changelog --dry-run "$(cz version -p)"` to notify your team about a new release in Slack.
184184

185-
#### Pre-commit Integration
185+
#### Prek and Pre-commit Integration
186186

187187
Commitizen can automatically validate your commit messages using pre-commit hooks.
188188

@@ -200,7 +200,7 @@ repos:
200200
201201
2. Install the hooks:
202202
```sh
203-
pre-commit install --hook-type commit-msg --hook-type pre-push
203+
prek install --hook-type commit-msg --hook-type pre-push
204204
```
205205

206206
| Hook | Recommended Stage |
@@ -210,7 +210,7 @@ pre-commit install --hook-type commit-msg --hook-type pre-push
210210

211211
> **Note**: Replace `master` with the [latest tag](https://github.com/commitizen-tools/commitizen/tags) to avoid warnings. You can automatically update this with:
212212
> ```sh
213-
> pre-commit autoupdate
213+
> prek autoupdate
214214
> ```
215215
216216
For more details about commit validation, see the [check command documentation](https://commitizen-tools.github.io/commitizen/commands/check/).

docs/tutorials/auto_check.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
22

33
## About
44

5-
To automatically check a commit message prior to committing, you can use a [Git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). This ensures that all commit messages follow your project's commitizen format before they are accepted into the repository.
5+
To automatically check a commit message before committing, use a [Git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). This ensures all commit messages match your project's commitizen format before they are accepted into the repository.
66

7-
When a commit message fails validation, Git will reject the commit and display an error message explaining what went wrong. You'll need to amend your commit message to follow the required format before the commit can proceed.
7+
When a commit message fails validation, Git rejects the commit and displays an error explaining what went wrong. Update the message to the required format before trying again.
88

99
## How to
1010

1111
There are two common methods for installing the hooks:
1212

13-
### Method 1: Using [pre-commit](https://pre-commit.com/) (Recommended)
13+
### Method 1: Using pre-commit hook frameworks (Recommended)
1414

15-
[pre-commit](https://pre-commit.com/) is a framework for managing and maintaining multi-language pre-commit hooks. It's the recommended approach as it handles hook installation, updates, and execution automatically.
15+
Using pre-commit hook frameworks is the recommended approach because hook installation, updates, and execution are handled automatically.
16+
Two common frameworks are:
1617

17-
#### Step 1: Install pre-commit
18+
1. [prek](https://prek.j178.dev) (faster)
19+
2. [pre-commit](https://pre-commit.com/)
20+
21+
22+
In the steps below, we'll use `prek`.
23+
24+
25+
!!! tip "Using pre-commit framework"
26+
If you use pre-commit instead of prek, you can run the same commands. Simply replace prek with pre-commit in the steps below.
27+
28+
29+
30+
#### Step 1: Install prek
1831

1932
```sh
20-
python -m pip install pre-commit
33+
python -m pip install prek
2134
```
2235

2336
#### Step 2: Create `.pre-commit-config.yaml`
@@ -42,14 +55,14 @@ repos:
4255
Install the configuration into Git's hook system:
4356

4457
```bash
45-
pre-commit install --hook-type commit-msg
58+
prek install --hook-type commit-msg
4659
```
4760

4861
The hook is now active! Every time you create a commit, commitizen will automatically validate your commit message.
4962

5063
### Method 2: Manual Git hook installation
5164

52-
If you prefer not to use pre-commit, you can manually create a Git hook. This gives you full control over the hook script but requires manual maintenance.
65+
If you prefer not to use a pre-commit framework, you can manually create a Git hook. This gives you full control over the hook script but requires manual maintenance.
5366

5467
#### Step 1: Create the commit-msg hook
5568

@@ -90,7 +103,7 @@ git commit -m "invalid commit message"
90103
git commit -m "feat: add new feature"
91104
```
92105

93-
If the hook is working correctly, invalid commit messages will be rejected with an error message explaining what's wrong.
106+
If the hook is working correctly, invalid commit messages are rejected with an error explaining what's wrong.
94107

95108
## What happens when validation fails?
96109

@@ -123,12 +136,12 @@ pattern: ^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))
123136
- **Verify commitizen is installed**: Run `cz --version` to confirm commitizen is available in your PATH
124137
- **Check Git version**: Ensure you're using a recent version of Git that supports hooks
125138

126-
### Pre-commit hook not working
139+
### Prek hook not working
127140

128-
- **Verify installation**: Run `pre-commit --version` to confirm pre-commit is installed
129-
- **Reinstall the hook**: Try running `pre-commit install --hook-type commit-msg` again
141+
- **Verify installation**: Run `prek --version` to confirm pre-commit is installed
142+
- **Reinstall the hook**: Try running `prek install --hook-type commit-msg` again
130143
- **Check configuration**: Verify your `.pre-commit-config.yaml` file is valid YAML and in the project root
131-
- **Update hooks**: Run `pre-commit autoupdate` to update to the latest versions
144+
- **Update hooks**: Run `prek autoupdate` to update to the latest versions
132145

133146
### Bypassing the hook (when needed)
134147

@@ -145,4 +158,5 @@ git commit --no-verify -m "your message"
145158

146159
- Learn more about [Git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)
147160
- See the [check command documentation](../commands/check.md) for more validation options
161+
- Check out [prek documentation](https://prek.j178.dev/) for advanced hook management
148162
- Check out [pre-commit documentation](https://pre-commit.com/) for advanced hook management

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ test = [
107107
"pytest-freezer>=0.4.6",
108108
"pytest-xdist>=3.1.0",
109109
"pytest-gitconfig>=0.9.0",
110+
"pre-commit>=4.5.1",
110111
]
111112

112113
linters = [
113114
"ruff>=0.11.5",
114-
"pre-commit>=3.2.0",
115115
"mypy>=1.16.0",
116116
"types-deprecated>=1.2.9.2",
117117
"types-python-dateutil>=2.8.19.13",
118118
"types-PyYAML>=5.4.3",
119119
"types-termcolor>=0.1.1",
120120
"types-colorama>=0.4.15.20240311",
121+
"prek>=0.2.28",
121122
]
122123

123124
documentation = ["mkdocs>=1.4.2", "mkdocs-material>=9.1.6"]
@@ -303,8 +304,8 @@ doc.help = "Live documentation server"
303304
doc.cmd = "mkdocs serve --livereload" # mkdocs hot reload failure workaround. Ref: https://github.com/mkdocs/mkdocs/issues/4032#issuecomment-3591002290
304305

305306
ci.help = "Run all tasks in CI"
306-
ci.sequence = ["check-commit", { cmd = "pre-commit run --all-files" }, "cover"]
307+
ci.sequence = ["check-commit", { cmd = "prek run --all-files" }, "cover"]
307308
ci.env = { SKIP = "no-commit-to-branch" }
308309

309310
setup-pre-commit.help = "Install pre-commit hooks"
310-
setup-pre-commit.cmd = "pre-commit install"
311+
setup-pre-commit.cmd = "prek install"

tests/test_bump_create_commit_message.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_create_tag(test_input, expected):
2424
assert new_tag == expected
2525

2626

27+
@pytest.mark.parametrize("hook_runner", ("pre-commit", "prek"))
2728
@pytest.mark.parametrize(
2829
"retry",
2930
(
@@ -38,7 +39,7 @@ def test_create_tag(test_input, expected):
3839
),
3940
)
4041
@pytest.mark.usefixtures("tmp_commitizen_project")
41-
def test_bump_pre_commit_changelog(util: UtilFixture, retry):
42+
def test_bump_pre_commit_changelog(util: UtilFixture, retry, hook_runner):
4243
util.freezer.move_to("2022-04-01")
4344
bump_args = ["bump", "--changelog", "--yes"]
4445
if retry:
@@ -69,7 +70,7 @@ def test_bump_pre_commit_changelog(util: UtilFixture, retry):
6970
)
7071
cmd.run("git add -A")
7172
cmd.run('git commit -m "fix: _test"')
72-
cmd.run("pre-commit install")
73+
cmd.run(f"{hook_runner} install")
7374
util.run_cli(*bump_args)
7475
# Pre-commit fixed last line adding extra indent and "\" char
7576
assert Path("CHANGELOG.md").read_text() == dedent(
@@ -83,9 +84,10 @@ def test_bump_pre_commit_changelog(util: UtilFixture, retry):
8384
)
8485

8586

87+
@pytest.mark.parametrize("hook_runner", ("pre-commit", "prek"))
8688
@pytest.mark.parametrize("retry", (True, False))
8789
@pytest.mark.usefixtures("tmp_commitizen_project")
88-
def test_bump_pre_commit_changelog_fails_always(util: UtilFixture, retry):
90+
def test_bump_pre_commit_changelog_fails_always(util: UtilFixture, retry, hook_runner):
8991
util.freezer.move_to("2022-04-01")
9092
bump_args = ["bump", "--changelog", "--yes"]
9193
if retry:
@@ -106,7 +108,7 @@ def test_bump_pre_commit_changelog_fails_always(util: UtilFixture, retry):
106108
)
107109
cmd.run("git add -A")
108110
cmd.run('git commit -m "feat: forbid changelogs"')
109-
cmd.run("pre-commit install")
111+
cmd.run(f"{hook_runner} install")
110112
with pytest.raises(exceptions.BumpCommitFailedError):
111113
util.run_cli(*bump_args)
112114

tests/test_project_info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def _create_project_files(files: dict[str, str | None]) -> None:
2222
"which_return, expected",
2323
[
2424
("/usr/local/bin/pre-commit", True),
25+
("/usr/local/bin/prek", True),
2526
(None, False),
2627
("", False),
2728
],

uv.lock

Lines changed: 37 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)