Skip to content
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

Add custom tag message #631

Merged
merged 15 commits into from
Dec 3, 2023
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
5 changes: 5 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
option_string: str | None = None,
):
if not isinstance(kwarg, str):
return

Check warning on line 54 in commitizen/cli.py

View check run for this annotation

Codecov / codecov/patch

commitizen/cli.py#L54

Added line #L54 was not covered by tests
if "=" not in kwarg:
raise InvalidCommandArgumentError(
f"Option {option_string} expect a key=value format"
Expand Down Expand Up @@ -243,6 +243,11 @@
"help": "create annotated tag instead of lightweight one",
"action": "store_true",
},
{
"name": ["--annotated-tag-message", "-atm"],
"help": "create annotated tag message",
"type": str,
},
{
"name": ["--gpg-sign", "-s"],
"help": "sign tag instead of lightweight one",
Expand Down Expand Up @@ -540,12 +545,12 @@
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
)
# Log warning for -- without any extra args
elif len(unknown_args) == 1:
logger.warning(

Check warning on line 549 in commitizen/cli.py

View check run for this annotation

Codecov / codecov/patch

commitizen/cli.py#L548-L549

Added lines #L548 - L549 were not covered by tests
"\nWARN: Incomplete commit command: received -- separator without any following git arguments\n"
)
extra_args = " ".join(unknown_args[1:])
arguments["extra_cli_args"] = extra_args

Check warning on line 553 in commitizen/cli.py

View check run for this annotation

Codecov / codecov/patch

commitizen/cli.py#L552-L553

Added lines #L552 - L553 were not covered by tests

if args.name:
conf.update({"name": args.name})
Expand Down
9 changes: 6 additions & 3 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
"bump_message",
"gpg_sign",
"annotated_tag",
"annotated_tag_message",
"major_version_zero",
"prerelease_offset",
"template",
Expand Down Expand Up @@ -197,8 +198,7 @@ def __call__(self): # noqa: C901

# If user specified changelog_to_stdout, they probably want the
# changelog to be generated as well, this is the most intuitive solution
if not self.changelog and self.changelog_to_stdout:
self.changelog = True
self.changelog = self.changelog or bool(self.changelog_to_stdout)

# No commits, there is no need to create an empty tag.
# Unless we previously had a prerelease.
Expand Down Expand Up @@ -365,7 +365,10 @@ def __call__(self): # noqa: C901
signed=self.bump_settings.get("gpg_sign", False)
or bool(self.config.settings.get("gpg_sign", False)),
annotated=self.bump_settings.get("annotated_tag", False)
or bool(self.config.settings.get("annotated_tag", False)),
or bool(self.config.settings.get("annotated_tag", False))
or bool(self.bump_settings.get("annotated_tag_message", False)),
msg=self.bump_settings.get("annotated_tag_message", None),
# TODO: also get from self.config.settings?
)
if c.return_code != 0:
raise BumpTagFailedError(c.err)
Expand Down
17 changes: 15 additions & 2 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,24 @@
return cls(name=name, rev=obj, date=date)


def tag(tag: str, annotated: bool = False, signed: bool = False) -> cmd.Command:
def tag(
tag: str, annotated: bool = False, signed: bool = False, msg: str | None = None
) -> cmd.Command:
_opt = ""
if annotated:
_opt = f"-a {tag} -m"
if signed:
_opt = f"-s {tag} -m"
c = cmd.run(f"git tag {_opt} {tag}")

# according to https://git-scm.com/book/en/v2/Git-Basics-Tagging,
# we're not able to create lightweight tag with message.
# by adding message, we make it a annotated tags
c = cmd.run(f'git tag {_opt} "{tag if _opt == "" or msg is None else msg}"')
LuisHenri marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@woile do you mean we should add a warning here to let the users know if they're using this feature, they're actually creating annotated tag?

To create a lightweight tag, don’t supply any of the -a, -s, or -m options, just provide a tag name:

https://git-scm.com/book/en/v2/Git-Basics-Tagging

not sure whether my understanding is correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, maybe a comment there explaining that. I didn't know that if you provide a message it automatically becomes an annotated tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@woile I just added the comment and resolved the conflict. Could you please take a look? I think we're pretty close to merge this one :)

cc @noirbizarre

return c


def add(args: str = "") -> cmd.Command:
c = cmd.run(f"git add {args}")

Check warning on line 102 in commitizen/git.py

View check run for this annotation

Codecov / codecov/patch

commitizen/git.py#L102

Added line #L102 was not covered by tests
return c


Expand Down Expand Up @@ -200,6 +206,13 @@
return c.out.strip()


def get_tag_message(tag: str) -> str | None:
c = cmd.run(f"git tag -l --format='%(contents:subject)' {tag}")
if c.err:
return None

Check warning on line 212 in commitizen/git.py

View check run for this annotation

Codecov / codecov/patch

commitizen/git.py#L212

Added line #L212 was not covered by tests
return c.out.strip()


def get_tag_names() -> list[str | None]:
c = cmd.run("git tag --list")
if c.err:
Expand Down
38 changes: 16 additions & 22 deletions docs/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ Some examples of pep440:

```bash
$ cz bump --help
usage: cz bump [-h] [--dry-run] [--files-only] [--local-version] [--changelog]
[--no-verify] [--yes] [--tag-format TAG_FORMAT]
[--bump-message BUMP_MESSAGE] [--prerelease {alpha,beta,rc}]
[--devrelease DEVRELEASE] [--increment {MAJOR,MINOR,PATCH}]
[--check-consistency] [--annotated-tag] [--gpg-sign]
[--changelog-to-stdout] [--git-output-to-stderr] [--retry] [--major-version-zero]
[--template TEMPLATE] [--extra EXTRA]
usage: cz bump [-h] [--dry-run] [--files-only] [--local-version] [--changelog] [--no-verify] [--yes] [--tag-format TAG_FORMAT]
[--bump-message BUMP_MESSAGE] [--prerelease {alpha,beta,rc}] [--devrelease DEVRELEASE] [--increment {MAJOR,MINOR,PATCH}]
[--check-consistency] [--annotated-tag] [--gpg-sign] [--changelog-to-stdout] [--git-output-to-stderr] [--retry] [--major-version-zero]
[--prerelease-offset PRERELEASE_OFFSET] [--version-scheme {semver,pep440}] [--version-type {semver,pep440}]
[MANUAL_VERSION]

positional arguments:
Expand All @@ -70,24 +67,20 @@ options:
--files-only bump version in the files from the config
--local-version bump only the local version portion
--changelog, -ch generate the changelog for the newest version
--no-verify this option bypasses the pre-commit and commit-msg
hooks
--no-verify this option bypasses the pre-commit and commit-msg hooks
--yes accept automatically questions done
--tag-format TAG_FORMAT
the format used to tag the commit and read it, use it
in existing projects, wrap around simple quotes
the format used to tag the commit and read it, use it in existing projects, wrap around simple quotes
--bump-message BUMP_MESSAGE
template used to create the release commit, useful
when working with CI
template used to create the release commit, useful when working with CI
--prerelease {alpha,beta,rc}, -pr {alpha,beta,rc}
choose type of prerelease
--devrelease DEVRELEASE, -d DEVRELEASE
specify non-negative integer for dev. release
--increment {MAJOR,MINOR,PATCH}
manually specify the desired increment
--check-consistency, -cc
check consistency among versions defined in commitizen
configuration and version_files
check consistency among versions defined in commitizen configuration and version_files
--annotated-tag, -at create annotated tag instead of lightweight one
--gpg-sign, -s sign tag instead of lightweight one
--changelog-to-stdout
Expand All @@ -96,14 +89,12 @@ options:
Redirect git output to stderr
--retry retry commit if it fails the 1st time
--major-version-zero keep major version at zero, even for breaking changes
--prerelease-offset start pre-releases with this offset
--version-scheme {pep440,semver}
--prerelease-offset PRERELEASE_OFFSET
start pre-releases with this offset
--version-scheme {semver,pep440}
choose version scheme

--template TEMPLATE, -t TEMPLATE
changelog template file name (relative to the current working directory)
--extra EXTRA, -e EXTRA
a changelog extra variable (in the form 'key=value')
--version-type {semver,pep440}
Deprecated, use --version-scheme
```

### `--files-only`
Expand Down Expand Up @@ -184,6 +175,9 @@ If `--local-version` is used, it will bump only the local version `0.1.0` and ke

If `--annotated-tag` is used, commitizen will create annotated tags. Also available via configuration, in `pyproject.toml` or `.cz.toml`.

### `--annotated-tag-message`
If `--annotated-tag-message` is used, commitizen will create annotated tags with the given message.

### `--changelog-to-stdout`

If `--changelog-to-stdout` is used, the incremental changelog generated by the bump
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def test_bump_with_git_to_stdout_arg(mocker: MockFixture, capsys, changelog_path
"bump",
"--changelog",
"--changelog-to-stdout",
"--annotated",
"--annotated-tag",
"--check-consistency",
"--yes",
),
Expand Down
15 changes: 14 additions & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import inspect
import os
import platform
import shutil

import pytest
from commitizen import cmd, exceptions, git
from pytest_mock import MockFixture

from tests.utils import FakeCommand, create_file_and_commit
from tests.utils import FakeCommand, create_file_and_commit, create_tag


def test_git_object_eq():
Expand Down Expand Up @@ -239,3 +240,15 @@ def test_eoltypes_get_eol_for_open():
assert git.EOLTypes.get_eol_for_open(git.EOLTypes.NATIVE) == os.linesep
assert git.EOLTypes.get_eol_for_open(git.EOLTypes.LF) == "\n"
assert git.EOLTypes.get_eol_for_open(git.EOLTypes.CRLF) == "\r\n"


def test_create_tag_with_message(tmp_commitizen_project):
with tmp_commitizen_project.as_cwd():
create_file_and_commit("feat(test): test")
tag_name = "1.0"
tag_message = "test message"
create_tag(tag_name, tag_message)
assert git.get_latest_tag_name() == tag_name
assert git.get_tag_message(tag_name) == (
tag_message if platform.system() != "Windows" else f"'{tag_message}'"
)
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def get_current_branch() -> str:
return c.out


def create_tag(tag: str):
c = git.tag(tag)
def create_tag(tag: str, message: str | None = None) -> None:
c = git.tag(tag, annotated=(message is not None), msg=message)
if c.return_code != 0:
raise exceptions.CommitError(c.err)

Expand Down
Loading