From 6e4245954bf964964cc89c11c660420948be24b5 Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Sun, 22 May 2022 15:50:17 -0700 Subject: [PATCH 1/2] ci(pre-commit): Remove unneeded commit-msg stage In v2.27.1, the commitizen pre-commit hook began specifying that it runs on the commit-msg stage in .pre-commit-hooks.yaml, so it is no longer necessary to specify the hook stage when using the hook in .pre-commit-config.yaml. --- .pre-commit-config.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e54255193f..d0765d5b75 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,6 @@ repos: rev: v2.31.0 # automatically updated by Commitizen hooks: - id: commitizen - stages: [commit-msg] - repo: local hooks: From bd4aa6f280e5ca2668b757337b60291de04abd69 Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Sun, 22 May 2022 16:04:51 -0700 Subject: [PATCH 2/2] feat(pre-commit): Add commitizen-branch hook Check all commit messages on the current branch. This is useful for checking commit messages after the fact (e.g., pre-push or in CI) since the existing hook only works at commit time. Expand the documentation of the pre-existing commitizen hook to clarify the relationship between them. --- .pre-commit-hooks.yaml | 20 +++++++++++++++++++- docs/README.md | 15 ++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index b32bc56e36..619e58622b 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,9 +1,27 @@ - id: commitizen name: commitizen check - description: "Check whether the commit message follows commiting rules" + description: > + Check whether the current commit message follows commiting rules. Allow + empty commit messages by default, because they typically indicate to Git + that the commit should be aborted. entry: cz check args: [--allow-abort, --commit-msg-file] stages: [commit-msg] language: python language_version: python3 minimum_pre_commit_version: "1.4.3" + +- id: commitizen-branch + name: commitizen check branch + description: > + Check all commit messages that are already on the current branch but not the + default branch on the origin repository. Useful for checking messages after + the fact (e.g., pre-push or in CI) without an expensive check of the entire + repository history. + entry: cz check + args: [--rev-range, origin/HEAD..HEAD] + always_run: true + pass_filenames: false + language: python + language_version: python3 + minimum_pre_commit_version: "1.4.3" diff --git a/docs/README.md b/docs/README.md index 9a99d11fd1..555d32377c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -101,7 +101,7 @@ cz commit -s ``` ### Integrating with Pre-commit -Commitizen can lint your commit message for you with `cz check`. +Commitizen can lint your commit messages for you with `cz check`. You can integrate this in your [pre-commit](https://pre-commit.com/) config with: ```yaml @@ -111,14 +111,23 @@ repos: rev: master hooks: - id: commitizen + - id: commitizen-branch + stages: [push] ``` -After the configuration is added, you'll need to run +After the configuration is added, you'll need to run: ```sh -pre-commit install --hook-type commit-msg +pre-commit install --hook-type commit-msg pre-push ``` +If you aren't using both hooks, you needn't install both stages. + +| Hook | Recommended Stage | +| ----------------- | ----------------- | +| commitizen | commit-msg | +| commitizen-branch | pre-push | + Read more about the `check` command [here](check.md). ### Help