Skip to content

Provide shell completion for cz #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from functools import partial

import argcomplete
from decli import cli

from commitizen import commands, config
Expand Down Expand Up @@ -265,6 +266,7 @@ def main():
conf = config.read_cfg()
parser = cli(data)

argcomplete.autocomplete(parser)
# Show help if no arg provided
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
Expand Down
26 changes: 26 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ commands:
current project (default: installed commitizen)
```

## Setting up bash completion

When using bash as your shell (limited support for zsh, fish, and tcsh is available), Commitizen can use [argcomplete](https://kislyuk.github.io/argcomplete/) for auto-completion. For this argcomplete needs to be enabled.

argcomplete is installed when you install Commitizen since it's a dependency.

If Commitizen is installed globally, global activation can be executed:

```bash
sudo activate-global-python-argcomplete
```

For permanent (but not global) Commitizen activation, use:

```bash
register-python-argcomplete cz >> ~/.bashrc
```

For one-time activation of argcomplete for Commitizen only, use:

```bash
eval "$(register-python-argcomplete cz)"
```

For further information on activation, please visit the [argcomplete website](https://kislyuk.github.io/argcomplete/).

## Third-Party Commitizen Templates

See [Third-Party Commitizen Templates](third-party-commitizen.md).
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ packaging = ">=19,<21"
tomlkit = "^0.5.3"
jinja2 = "^2.10.3"
pyyaml = ">=3.08"
argcomplete = "^1.12.1"

[tool.poetry.dev-dependencies]
ipython = "^7.2"
Expand Down
14 changes: 14 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import subprocess
import sys

import pytest
Expand Down Expand Up @@ -78,3 +79,16 @@ def test_commitizen_debug_excepthook(capsys):
assert excinfo.type == SystemExit
assert excinfo.value.code == NotAGitProjectError.exit_code
assert "NotAGitProjectError" in str(excinfo.traceback[0])


def test_argcomplete_activation():
"""
This function is testing the one-time activation of argcomplete for
commitizen only.

Equivalent to run:
$ eval "$(register-python-argcomplete pytest)"
"""
output = subprocess.run(["register-python-argcomplete", "cz"])

assert output.returncode == 0