Skip to content

Commit d2b0ebe

Browse files
committed
fix(#271): add annotated_tag option to bump
Enable creation of annotated tags when bumping.
1 parent d8e9bad commit d2b0ebe

File tree

6 files changed

+87
-26
lines changed

6 files changed

+87
-26
lines changed

CHANGELOG.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### Fix
4+
5+
- **#271**: add annotated_tag option to bump
6+
17
## v2.4.0 (2020-09-18)
28

39
### Feat
@@ -97,17 +103,17 @@
97103
### Fix
98104

99105
- **commands/bump**: use `return_code` in commands used by bump
100-
- **commands/commit**: use return_code to raise commit error, not stderr
101-
102-
### Refactor
103-
104-
- **cmd**: add return code to Command
105106

106107
## v1.23.2 (2020-07-25)
107108

108109
### Fix
109110

110111
- **bump**: add changelog file into stage when running `cz bump --changelog`
112+
- **commands/commit**: use return_code to raise commit error, not stderr
113+
114+
### Refactor
115+
116+
- **cmd**: add return code to Command
111117

112118
## v1.23.1 (2020-07-14)
113119

@@ -124,17 +130,20 @@
124130
- **commands/init**: add test case and remove unaccessible code
125131
- **exception**: move output message related to exception into exception
126132
- **exception**: implement message handling mechanism for CommitizenException
127-
- **cli**: do not show traceback if the raised exception is CommitizenException
128-
- introduce DryRunExit, ExpectedExit, NoCommandFoundError, InvalidCommandArgumentError
129-
- use custom exception for error handling
130-
- **error_codes**: remove unused NO_COMMIT_MSG error code
131133

132134
### Feat
133135

134136
- **cli**: enable displaying all traceback for CommitizenException when --debug flag is used
135137

136138
## v1.22.3 (2020-06-10)
137139

140+
### Refactor
141+
142+
- **cli**: do not show traceback if the raised exception is CommitizenException
143+
- introduce DryRunExit, ExpectedExit, NoCommandFoundError, InvalidCommandArgumentError
144+
- use custom exception for error handling
145+
- **error_codes**: remove unused NO_COMMIT_MSG error code
146+
138147
## v1.22.2 (2020-05-29)
139148

140149
### Fix

commitizen/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
),
132132
"action": "store_true",
133133
},
134+
{
135+
"name": ["--annotated-tag", "-at"],
136+
"help": "create annotated tag instead of lightweight one",
137+
"action": "store_true",
138+
},
134139
],
135140
},
136141
{

commitizen/commands/bump.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ def __init__(self, config: BaseConfig, arguments: dict):
3131
**config.settings,
3232
**{
3333
key: arguments[key]
34-
for key in ["tag_format", "prerelease", "increment", "bump_message"]
34+
for key in [
35+
"tag_format",
36+
"prerelease",
37+
"increment",
38+
"bump_message",
39+
"annotated_tag",
40+
]
3541
if arguments[key] is not None
3642
},
3743
}
@@ -157,7 +163,11 @@ def __call__(self): # noqa: C901
157163
c = git.commit(message, args=self._get_commit_args())
158164
if c.return_code != 0:
159165
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')
160-
c = git.tag(new_tag_version)
166+
c = git.tag(
167+
new_tag_version,
168+
annotated=self.bump_settings.get("annotated_tag", False)
169+
or bool(self.config.settings.get("annotated_tag", False)),
170+
)
161171
if c.return_code != 0:
162172
raise BumpTagFailedError(c.err)
163173
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

+19-12
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,30 @@ Some examples:
5454

5555
```bash
5656
$ cz bump --help
57-
usage: cz bump [-h] [--dry-run] [--files-only] [--changelog] [--no-verify]
58-
[--yes] [--tag-format TAG_FORMAT] [--bump-message BUMP_MESSAGE]
59-
[--prerelease {alpha,beta,rc}]
60-
[--increment {MAJOR,MINOR,PATCH}] [--check-consistency]
57+
usage: cz bump [-h] [--dry-run] [--files-only] [--changelog] [--no-verify] [--yes]
58+
[--tag-format TAG_FORMAT] [--bump-message BUMP_MESSAGE] [--prerelease {alpha,beta,rc}]
59+
[--increment {MAJOR,MINOR,PATCH}] [--check-consistency] [--annotated-tag]
6160

6261
optional arguments:
6362
-h, --help show this help message and exit
6463
--dry-run show output to stdout, no commit, no modified files
6564
--files-only bump version in the files from the config
6665
--changelog, -ch generate the changelog for the newest version
67-
--no-verify this option bypasses the pre-commit and commit-msg
68-
hooks
66+
--no-verify this option bypasses the pre-commit and commit-msg hooks
6967
--yes accept automatically questions done
7068
--tag-format TAG_FORMAT
71-
the format used to tag the commit and read it, use it
72-
in existing projects, wrap around simple quotes
69+
the format used to tag the commit and read it, use it in existing projects, wrap
70+
around simple quotes
7371
--bump-message BUMP_MESSAGE
74-
template used to create the release commit, useful
75-
when working with CI
72+
template used to create the release commit, useful when working with CI
7673
--prerelease {alpha,beta,rc}, -pr {alpha,beta,rc}
7774
choose type of prerelease
7875
--increment {MAJOR,MINOR,PATCH}
7976
manually specify the desired increment
8077
--check-consistency, -cc
81-
check consistency among versions defined in commitizen
82-
configuration and version_files
78+
check consistency among versions defined in commitizen configuration and
79+
version_files
80+
--annotated-tag, -at create annotated tag instead of lightweight one
8381
```
8482

8583
### `--changelog`
@@ -216,6 +214,15 @@ Some examples
216214
bump_message = "release $current_version → $new_version [skip-ci]"
217215
```
218216

217+
### `annotated_tag`
218+
219+
Whether to create annotated tags or lightweight ones.
220+
221+
```toml
222+
[tool.commitizen]
223+
annotated_tag = true
224+
```
225+
219226
## Custom bump
220227

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

tests/commands/test_bump_command.py

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

5080

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

0 commit comments

Comments
 (0)