@@ -118,6 +118,100 @@ That's it, your Commitizen now supports custom rules, and you can run.
118118cz -n cz_strange bump
119119```
120120
121+ ### Filter commits before bump and changelog generation
122+
123+ Custom rules can select which commits are relevant before Commitizen calculates a
124+ version increment or generates a changelog.
125+
126+ | Method | Used by | Default behavior |
127+ | ----------------------------------- | ----------------------------------------------------- | ---------------------------- |
128+ | ` filter_commits ` | Shared by the operation-specific methods | Return all commits unchanged |
129+ | ` filter_commits_before_bump ` | ` cz bump ` and ` cz version --next USE_GIT_COMMITS ` | Call ` filter_commits ` |
130+ | ` filter_commits_before_changelog ` | ` cz changelog ` , including changelogs created by bump | Call ` filter_commits ` |
131+
132+ Override ` filter_commits ` when bump and changelog generation should use the same
133+ selection. Override an operation-specific method when their selections should
134+ differ. These methods do not affect ` cz check ` .
135+
136+ For example, a monorepo can use a full commit-message metadata line to associate
137+ commits with applications:
138+
139+ ``` text
140+ feat: add shared library
141+
142+ Applications: ['AppA', 'AppB']
143+ ```
144+
145+ A plugin can retain the built-in Conventional Commits behavior while filtering
146+ on that metadata:
147+
148+ ``` python title="cz_applications.py"
149+ import re
150+
151+ from commitizen import git
152+ from commitizen.cz.conventional_commits import ConventionalCommitsCz
153+ from commitizen.exceptions import InvalidConfigurationError
154+
155+
156+ class ApplicationsCommitizen (ConventionalCommitsCz ):
157+ """ Apply Conventional Commits rules to one configured application.
158+
159+ Example:
160+ Configure `app = "AppA"` to keep commits whose `Applications:` metadata
161+ includes `AppA`.
162+ """
163+
164+ def filter_commits (self , commits : list[git.GitCommit]) -> list[git.GitCommit]:
165+ """ Keep commits associated with the configured application.
166+
167+ Args:
168+ commits: The commits available to the current operation.
169+
170+ Returns:
171+ Commits whose full message lists the configured application.
172+
173+ Raises:
174+ InvalidConfigurationError: If the plugin's `app` setting is missing.
175+ """
176+ application = dict (self .config.settings).get(" app" )
177+ if not isinstance (application, str ) or not application:
178+ raise InvalidConfigurationError(
179+ " cz_applications requires a non-empty 'app' setting"
180+ )
181+
182+ application_line = re.compile(
183+ rf """ ^Applications:\s*\[[^\]]*['"] { re.escape(application)} ['"][^\]]*\]\s*$ """ ,
184+ re.MULTILINE ,
185+ )
186+ return [commit for commit in commits if application_line.search(commit.message)]
187+ ```
188+
189+ Expose the class through the plugin package:
190+
191+ ``` toml title="pyproject.toml"
192+ [project .entry-points ."commitizen .plugin" ]
193+ cz_applications = " cz_applications:ApplicationsCommitizen"
194+ ```
195+
196+ Then select the plugin and application in each component's configuration:
197+
198+ ``` toml title="app-a/.cz.toml"
199+ [tool .commitizen ]
200+ name = " cz_applications"
201+ app = " AppA"
202+ version = " 1.0.0"
203+ tag_format = " $version-app-a"
204+ ```
205+
206+ ` app ` is owned and interpreted by this plugin; it is not a built-in Commitizen
207+ setting. TOML keys under ` [tool.commitizen] ` are available through
208+ ` self.config.settings ` . The declarative ` [tool.commitizen.customize] ` section
209+ cannot override Python methods, so this use case requires a Python plugin.
210+
211+ Filtering happens before the existing rule processing. The retained commits are
212+ still interpreted by ` bump_pattern ` and ` bump_map ` for version increments and by
213+ ` changelog_pattern ` and ` commit_parser ` for changelog entries.
214+
121215[ convcomms ] : https://github.com/commitizen-tools/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py
122216
123217### Custom commit validation and error message
0 commit comments