cz-path

Commitizen plugin that prefixes commit messages with the common path or prefix of staged files.

View on GitHub

cz-path

Python versions PyPI - Version GitHub tag (with filter) License GitHub commits since latest release (by SemVer including pre-releases) CodeQL QA Tests Coverage Status Dependabot Documentation Status mypy uv pytest Ruff Downloads Stargazers pre-commit Prettier

@Tatsh Buy Me A Coffee Libera.Chat Mastodon Follow Patreon

Commitizen plugin that prefixes commit messages with the common path or prefix of staged files.

Installation

pip install cz-path

The package depends on Commitizen, so you get the cz command when you install cz-path.

Usage

Configure Commitizen

Point Commitizen at this plugin by setting name to cz_path in your configuration file (see below). To use this plugin for a single run without changing the config file, pass -n cz_path or --name cz_path to cz.

Create a commit

  1. Use a Git repository. Run cz from the repository root. The plugin opens the Git index in the current directory, so running it elsewhere will not see your project.

  2. Stage your changes with git add. The plugin looks at the staged diff against HEAD to suggest a path prefix. If nothing is staged, Commitizen fails with a message that no staged files were found.

  3. Start Commitizen to write the message and create the commit:

    cz commit
    

    Short form: cz c.

  4. Answer the prompts:

    • Prefix — pick the suggested path prefix (if shown), or project, or (empty) for no prefix.
    • Commit title — the rest of the subject line. The full message is <prefix>: <title>.

Message format

Commits follow <prefix>: <title>, for example module/component: short description of the change. The pattern is a prefix (which may be empty), then a colon and space, then the title.

By default, src/ is stripped from the computed prefix. Set remove_path_prefixes to [] to turn that off, or add other path segments (such as a top-level package name) to strip. You do not need to include a trailing / in each entry.

pyproject.toml

[tool.commitizen]
name = "cz_path"
remove_path_prefixes = ["src", "module_name"]

.cz.json

{
  "commitizen": {
    "name": "cz_path",
    "remove_path_prefixes": ["src", "module_name"]
  }
}

Scenarios

Staged files Path prefix String prefix
src/a.c, src/b.c src src/
src/a1.c, src/a2.c src src/a
a.c, b.c (no option) (no option)

If no prefix is found amongst the staged files, only the choices project and empty will be given.