Skip to content

Commit 20a54bf

Browse files
diefanscliles
authored andcommitted
feat(#271): enable creation of annotated tags when bumping
1 parent 99d4024 commit 20a54bf

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

commitizen/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@
137137
),
138138
"action": "store_true",
139139
},
140+
{
141+
"name": ["--annotated-tag", "-at"],
142+
"help": "create annotated tag instead of lightweight one",
143+
"action": "store_true",
144+
},
140145
],
141146
},
142147
{

commitizen/commands/bump.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ def __init__(self, config: BaseConfig, arguments: dict):
3232
**config.settings,
3333
**{
3434
key: arguments[key]
35-
for key in ["tag_format", "prerelease", "increment", "bump_message"]
35+
for key in [
36+
"tag_format",
37+
"prerelease",
38+
"increment",
39+
"bump_message",
40+
"annotated_tag",
41+
]
3642
if arguments[key] is not None
3743
},
3844
}
@@ -183,7 +189,11 @@ def __call__(self): # noqa: C901
183189
c = git.commit(message, args=self._get_commit_args())
184190
if c.return_code != 0:
185191
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')
186-
c = git.tag(new_tag_version)
192+
c = git.tag(
193+
new_tag_version,
194+
annotated=self.bump_settings.get("annotated_tag", False)
195+
or bool(self.config.settings.get("annotated_tag", False)),
196+
)
187197
if c.return_code != 0:
188198
raise BumpTagFailedError(c.err)
189199
out.success("Done!")

commitizen/git.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def __repr__(self):
4545
return f"GitTag('{self.name}', '{self.rev}', '{self.date}')"
4646

4747

48-
def tag(tag: str):
49-
c = cmd.run(f"git tag {tag}")
48+
def tag(tag: str, annotated: bool = False):
49+
c = cmd.run(f"git tag -a {tag} -m {tag}" if annotated else f"git tag {tag}")
5050
return c
5151

5252

docs/bump.md

+23-9
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,29 @@ $ cz bump --help
5757
usage: cz bump [-h] [--dry-run] [--files-only] [--changelog] [--no-verify] [--local-version]
5858
[--yes] [--tag-format TAG_FORMAT] [--bump-message BUMP_MESSAGE]
5959
[--prerelease {alpha,beta,rc}]
60-
[--increment {MAJOR,MINOR,PATCH}] [--check-consistency]
60+
[--increment {MAJOR,MINOR,PATCH}] [--check-consistency] [--annotated-tag]
6161

6262
optional arguments:
6363
-h, --help show this help message and exit
6464
--dry-run show output to stdout, no commit, no modified files
6565
--files-only bump version in the files from the config
6666
--changelog, -ch generate the changelog for the newest version
67-
--no-verify this option bypasses the pre-commit and commit-msg
68-
hooks
67+
--no-verify this option bypasses the pre-commit and commit-msg hooks
6968
--yes accept automatically questions done
7069
--local-version bump the local portion of the version
7170
--tag-format TAG_FORMAT
72-
the format used to tag the commit and read it, use it
73-
in existing projects, wrap around simple quotes
71+
the format used to tag the commit and read it, use it in existing projects, wrap
72+
around simple quotes
7473
--bump-message BUMP_MESSAGE
75-
template used to create the release commit, useful
76-
when working with CI
74+
template used to create the release commit, useful when working with CI
7775
--prerelease {alpha,beta,rc}, -pr {alpha,beta,rc}
7876
choose type of prerelease
7977
--increment {MAJOR,MINOR,PATCH}
8078
manually specify the desired increment
8179
--check-consistency, -cc
82-
check consistency among versions defined in commitizen
83-
configuration and version_files
80+
check consistency among versions defined in commitizen configuration and
81+
version_files
82+
--annotated-tag, -at create annotated tag instead of lightweight one
8483
```
8584

8685
### `--files-only`
@@ -158,6 +157,10 @@ version = "5.3.5+0.1.0"
158157

159158
If `--local-version` is used, it will bump only the local version `0.1.0` and keep the public version `5.3.5` intact, bumping to the version `5.3.5+0.2.0`.
160159

160+
### `--annotated-tag`
161+
162+
If `--annotated-tag` is used, commitizen will create annotated tags. Also available via configuration, in `pyproject.toml` or `.cz.toml`.
163+
161164
## Configuration
162165

163166
### `tag_format`
@@ -256,6 +259,17 @@ defaults to: `false`
256259
update_changelog_on_bump = true
257260
```
258261

262+
---
263+
264+
### `annotated_tag`
265+
266+
When set to `true` commitizen will create annotated tags.
267+
268+
```toml
269+
[tool.commitizen]
270+
annotated_tag = true
271+
```
272+
259273
## Custom bump
260274

261275
Read the [customizing section](./customization.md).

tests/commands/test_bump_command.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,37 @@ def test_bump_minor_increment(commit_msg, mocker):
4949
mocker.patch.object(sys, "argv", testargs)
5050
cli.main()
5151
tag_exists = git.tag_exist("0.2.0")
52-
assert tag_exists is True
52+
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
53+
assert tag_exists is True and "commit:refs/tags/0.2.0\n" in cmd_res.out
54+
55+
56+
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
57+
@pytest.mark.usefixtures("tmp_commitizen_project")
58+
def test_bump_minor_increment_annotated(commit_msg, mocker):
59+
create_file_and_commit(commit_msg)
60+
testargs = ["cz", "bump", "--yes", "--annotated-tag"]
61+
mocker.patch.object(sys, "argv", testargs)
62+
cli.main()
63+
tag_exists = git.tag_exist("0.2.0")
64+
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
65+
assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
66+
67+
68+
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
69+
def test_bump_minor_increment_annotated_config_file(
70+
commit_msg, mocker, tmp_commitizen_project
71+
):
72+
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
73+
tmp_commitizen_cfg_file.write(
74+
f"{tmp_commitizen_cfg_file.read()}\n" f"annotated_tag = 1"
75+
)
76+
create_file_and_commit(commit_msg)
77+
testargs = ["cz", "bump", "--yes"]
78+
mocker.patch.object(sys, "argv", testargs)
79+
cli.main()
80+
tag_exists = git.tag_exist("0.2.0")
81+
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
82+
assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
5383

5484

5585
@pytest.mark.usefixtures("tmp_commitizen_project")

0 commit comments

Comments
 (0)